|
4 | 4 |
|
5 | 5 | package io.flutter.plugins.deviceinfo; |
6 | 6 |
|
7 | | -import android.annotation.SuppressLint; |
8 | | -import android.content.Context; |
9 | | -import android.os.Build; |
10 | | -import android.os.Build.VERSION; |
11 | | -import android.os.Build.VERSION_CODES; |
12 | | -import android.provider.Settings; |
13 | | -import io.flutter.plugin.common.MethodCall; |
| 7 | +import android.content.ContentResolver; |
| 8 | +import io.flutter.embedding.engine.plugins.FlutterPlugin; |
| 9 | +import io.flutter.plugin.common.BinaryMessenger; |
14 | 10 | import io.flutter.plugin.common.MethodChannel; |
15 | | -import io.flutter.plugin.common.MethodChannel.MethodCallHandler; |
16 | | -import io.flutter.plugin.common.MethodChannel.Result; |
17 | 11 | import io.flutter.plugin.common.PluginRegistry.Registrar; |
18 | | -import java.util.Arrays; |
19 | | -import java.util.HashMap; |
20 | | -import java.util.Map; |
21 | 12 |
|
22 | 13 | /** DeviceInfoPlugin */ |
23 | | -public class DeviceInfoPlugin implements MethodCallHandler { |
24 | | - private final Context context; |
| 14 | +public class DeviceInfoPlugin implements FlutterPlugin { |
25 | 15 |
|
26 | | - /** Substitute for missing values. */ |
27 | | - private static final String[] EMPTY_STRING_LIST = new String[] {}; |
| 16 | + MethodChannel channel; |
28 | 17 |
|
29 | 18 | /** Plugin registration. */ |
30 | 19 | public static void registerWith(Registrar registrar) { |
31 | | - final MethodChannel channel = |
32 | | - new MethodChannel(registrar.messenger(), "plugins.flutter.io/device_info"); |
33 | | - channel.setMethodCallHandler(new DeviceInfoPlugin(registrar.context())); |
| 20 | + DeviceInfoPlugin plugin = new DeviceInfoPlugin(); |
| 21 | + plugin.setupMethodChannel(registrar.messenger(), registrar.context().getContentResolver()); |
34 | 22 | } |
35 | 23 |
|
36 | | - /** Do not allow direct instantiation. */ |
37 | | - private DeviceInfoPlugin(Context context) { |
38 | | - this.context = context; |
| 24 | + @Override |
| 25 | + public void onAttachedToEngine(FlutterPlugin.FlutterPluginBinding binding) { |
| 26 | + setupMethodChannel( |
| 27 | + binding.getFlutterEngine().getDartExecutor(), |
| 28 | + binding.getApplicationContext().getContentResolver()); |
39 | 29 | } |
40 | 30 |
|
41 | 31 | @Override |
42 | | - public void onMethodCall(MethodCall call, Result result) { |
43 | | - if (call.method.equals("getAndroidDeviceInfo")) { |
44 | | - Map<String, Object> build = new HashMap<>(); |
45 | | - build.put("board", Build.BOARD); |
46 | | - build.put("bootloader", Build.BOOTLOADER); |
47 | | - build.put("brand", Build.BRAND); |
48 | | - build.put("device", Build.DEVICE); |
49 | | - build.put("display", Build.DISPLAY); |
50 | | - build.put("fingerprint", Build.FINGERPRINT); |
51 | | - build.put("hardware", Build.HARDWARE); |
52 | | - build.put("host", Build.HOST); |
53 | | - build.put("id", Build.ID); |
54 | | - build.put("manufacturer", Build.MANUFACTURER); |
55 | | - build.put("model", Build.MODEL); |
56 | | - build.put("product", Build.PRODUCT); |
57 | | - if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { |
58 | | - build.put("supported32BitAbis", Arrays.asList(Build.SUPPORTED_32_BIT_ABIS)); |
59 | | - build.put("supported64BitAbis", Arrays.asList(Build.SUPPORTED_64_BIT_ABIS)); |
60 | | - build.put("supportedAbis", Arrays.asList(Build.SUPPORTED_ABIS)); |
61 | | - } else { |
62 | | - build.put("supported32BitAbis", Arrays.asList(EMPTY_STRING_LIST)); |
63 | | - build.put("supported64BitAbis", Arrays.asList(EMPTY_STRING_LIST)); |
64 | | - build.put("supportedAbis", Arrays.asList(EMPTY_STRING_LIST)); |
65 | | - } |
66 | | - build.put("tags", Build.TAGS); |
67 | | - build.put("type", Build.TYPE); |
68 | | - build.put("isPhysicalDevice", !isEmulator()); |
69 | | - build.put("androidId", getAndroidId()); |
70 | | - |
71 | | - Map<String, Object> version = new HashMap<>(); |
72 | | - if (VERSION.SDK_INT >= VERSION_CODES.M) { |
73 | | - version.put("baseOS", VERSION.BASE_OS); |
74 | | - version.put("previewSdkInt", VERSION.PREVIEW_SDK_INT); |
75 | | - version.put("securityPatch", VERSION.SECURITY_PATCH); |
76 | | - } |
77 | | - version.put("codename", VERSION.CODENAME); |
78 | | - version.put("incremental", VERSION.INCREMENTAL); |
79 | | - version.put("release", VERSION.RELEASE); |
80 | | - version.put("sdkInt", VERSION.SDK_INT); |
81 | | - build.put("version", version); |
82 | | - |
83 | | - result.success(build); |
84 | | - } else { |
85 | | - result.notImplemented(); |
86 | | - } |
| 32 | + public void onDetachedFromEngine(FlutterPlugin.FlutterPluginBinding binding) { |
| 33 | + tearDownChannel(); |
87 | 34 | } |
88 | 35 |
|
89 | | - /** |
90 | | - * Returns the Android hardware device ID that is unique between the device + user and app |
91 | | - * signing. This key will change if the app is uninstalled or its data is cleared. Device factory |
92 | | - * reset will also result in a value change. |
93 | | - * |
94 | | - * @return The android ID |
95 | | - */ |
96 | | - @SuppressLint("HardwareIds") |
97 | | - private String getAndroidId() { |
98 | | - return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID); |
| 36 | + private void setupMethodChannel(BinaryMessenger messenger, ContentResolver contentResolver) { |
| 37 | + channel = new MethodChannel(messenger, "plugins.flutter.io/device_info"); |
| 38 | + final MethodCallHandlerImpl handler = new MethodCallHandlerImpl(contentResolver); |
| 39 | + channel.setMethodCallHandler(handler); |
99 | 40 | } |
100 | 41 |
|
101 | | - /** |
102 | | - * A simple emulator-detection based on the flutter tools detection logic and a couple of legacy |
103 | | - * detection systems |
104 | | - */ |
105 | | - private boolean isEmulator() { |
106 | | - return (Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic")) |
107 | | - || Build.FINGERPRINT.startsWith("generic") |
108 | | - || Build.FINGERPRINT.startsWith("unknown") |
109 | | - || Build.HARDWARE.contains("goldfish") |
110 | | - || Build.HARDWARE.contains("ranchu") |
111 | | - || Build.MODEL.contains("google_sdk") |
112 | | - || Build.MODEL.contains("Emulator") |
113 | | - || Build.MODEL.contains("Android SDK built for x86") |
114 | | - || Build.MANUFACTURER.contains("Genymotion") |
115 | | - || Build.PRODUCT.contains("sdk_google") |
116 | | - || Build.PRODUCT.contains("google_sdk") |
117 | | - || Build.PRODUCT.contains("sdk") |
118 | | - || Build.PRODUCT.contains("sdk_x86") |
119 | | - || Build.PRODUCT.contains("vbox86p") |
120 | | - || Build.PRODUCT.contains("emulator") |
121 | | - || Build.PRODUCT.contains("simulator"); |
| 42 | + private void tearDownChannel() { |
| 43 | + channel.setMethodCallHandler(null); |
| 44 | + channel = null; |
122 | 45 | } |
123 | 46 | } |
0 commit comments