Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Prev Previous commit
Next Next commit
refactor
  • Loading branch information
Chris Yang committed Oct 7, 2019
commit 5c8ebe7d7089ead9da01b0b6806ce462de8da452
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,38 @@

package io.flutter.plugins.deviceinfo;

import android.content.ContentResolver;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.PluginRegistry.Registrar;

/** DeviceInfoPlugin */
public class DeviceInfoPlugin implements FlutterPlugin {

MethodChannel channel;

/** Plugin registration. */
public static void registerWith(Registrar registrar) {
final MethodChannel channel =
new MethodChannel(registrar.messenger(), "plugins.flutter.io/device_info");

final MethodCallHandlerImpl handler =
new MethodCallHandlerImpl(registrar.context().getContentResolver());
channel.setMethodCallHandler(handler);
DeviceInfoPlugin plugin = new DeviceInfoPlugin();
plugin.setupMethodChannel(registrar.messenger(), registrar.context().getContentResolver());
}

@Override
public void onAttachedToEngine(FlutterPlugin.FlutterPluginBinding binding) {
final MethodChannel channel =
new MethodChannel(
binding.getFlutterEngine().getDartExecutor(), "plugins.flutter.io/device_info");

final MethodCallHandlerImpl handler =
new MethodCallHandlerImpl(binding.getApplicationContext().getContentResolver());
channel.setMethodCallHandler(handler);
setupMethodChannel(
binding.getFlutterEngine().getDartExecutor(),
binding.getApplicationContext().getContentResolver());
}

@Override
public void onDetachedFromEngine(FlutterPlugin.FlutterPluginBinding binding) {}
public void onDetachedFromEngine(FlutterPlugin.FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommendation: consider defining a teardownMethodChannel() for symmetry with setupMethodChannel().

}

private void setupMethodChannel(BinaryMessenger messenger, ContentResolver contentResolver) {
channel = new MethodChannel(messenger, "plugins.flutter.io/device_info");
final MethodCallHandlerImpl handler = new MethodCallHandlerImpl(contentResolver);
channel.setMethodCallHandler(handler);
}
}