| // Copyright 2013 The Flutter Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| package io.flutter.plugins.sharedpreferences; |
| |
| import android.content.Context; |
| import io.flutter.embedding.engine.plugins.FlutterPlugin; |
| import io.flutter.plugin.common.BinaryMessenger; |
| import io.flutter.plugin.common.MethodChannel; |
| |
| /** SharedPreferencesPlugin */ |
| public class SharedPreferencesPlugin implements FlutterPlugin { |
| private static final String CHANNEL_NAME = "plugins.flutter.io/shared_preferences"; |
| private MethodChannel channel; |
| private MethodCallHandlerImpl handler; |
| |
| @SuppressWarnings("deprecation") |
| public static void registerWith(io.flutter.plugin.common.PluginRegistry.Registrar registrar) { |
| final SharedPreferencesPlugin plugin = new SharedPreferencesPlugin(); |
| plugin.setupChannel(registrar.messenger(), registrar.context()); |
| } |
| |
| @Override |
| public void onAttachedToEngine(FlutterPlugin.FlutterPluginBinding binding) { |
| setupChannel(binding.getBinaryMessenger(), binding.getApplicationContext()); |
| } |
| |
| @Override |
| public void onDetachedFromEngine(FlutterPlugin.FlutterPluginBinding binding) { |
| teardownChannel(); |
| } |
| |
| private void setupChannel(BinaryMessenger messenger, Context context) { |
| channel = new MethodChannel(messenger, CHANNEL_NAME); |
| handler = new MethodCallHandlerImpl(context); |
| channel.setMethodCallHandler(handler); |
| } |
| |
| private void teardownChannel() { |
| handler.teardown(); |
| handler = null; |
| channel.setMethodCallHandler(null); |
| channel = null; |
| } |
| } |