From e6f85ba985fefcdeec8ee35266524ce0699e3215 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bernhard=20M=C3=BCller?= Date: Wed, 3 Oct 2018 00:15:36 +0200 Subject: [PATCH] Added android device id to android device info (#639) * Added android device id to android device info * Bumped device info to version 0.3.0 --- packages/device_info/CHANGELOG.md | 4 ++++ .../plugins/deviceinfo/DeviceInfoPlugin.java | 23 +++++++++++++++++-- packages/device_info/example/lib/main.dart | 1 + packages/device_info/lib/device_info.dart | 5 ++++ packages/device_info/pubspec.yaml | 2 +- 5 files changed, 32 insertions(+), 3 deletions(-) diff --git a/packages/device_info/CHANGELOG.md b/packages/device_info/CHANGELOG.md index 01325f58c1dd..73f00e8c66e8 100644 --- a/packages/device_info/CHANGELOG.md +++ b/packages/device_info/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.3.0 + +* Added ability to get Android ID for Android devices + ## 0.2.1 * Updated Gradle tooling to match Android Studio 3.1.2. diff --git a/packages/device_info/android/src/main/java/io/flutter/plugins/deviceinfo/DeviceInfoPlugin.java b/packages/device_info/android/src/main/java/io/flutter/plugins/deviceinfo/DeviceInfoPlugin.java index 4ff49fd3a6da..44d197c12d7d 100644 --- a/packages/device_info/android/src/main/java/io/flutter/plugins/deviceinfo/DeviceInfoPlugin.java +++ b/packages/device_info/android/src/main/java/io/flutter/plugins/deviceinfo/DeviceInfoPlugin.java @@ -4,9 +4,12 @@ package io.flutter.plugins.deviceinfo; +import android.annotation.SuppressLint; +import android.content.Context; import android.os.Build; import android.os.Build.VERSION; import android.os.Build.VERSION_CODES; +import android.provider.Settings; import io.flutter.plugin.common.MethodCall; import io.flutter.plugin.common.MethodChannel; import io.flutter.plugin.common.MethodChannel.MethodCallHandler; @@ -18,6 +21,7 @@ /** DeviceInfoPlugin */ public class DeviceInfoPlugin implements MethodCallHandler { + private final Context context; /** Substitute for missing values. */ private static final String[] EMPTY_STRING_LIST = new String[] {}; @@ -26,11 +30,13 @@ public class DeviceInfoPlugin implements MethodCallHandler { public static void registerWith(Registrar registrar) { final MethodChannel channel = new MethodChannel(registrar.messenger(), "plugins.flutter.io/device_info"); - channel.setMethodCallHandler(new DeviceInfoPlugin()); + channel.setMethodCallHandler(new DeviceInfoPlugin(registrar.activity())); } /** Do not allow direct instantiation. */ - private DeviceInfoPlugin() {} + private DeviceInfoPlugin(Context context) { + this.context = context; + } @Override public void onMethodCall(MethodCall call, Result result) { @@ -60,6 +66,7 @@ public void onMethodCall(MethodCall call, Result result) { build.put("tags", Build.TAGS); build.put("type", Build.TYPE); build.put("isPhysicalDevice", !isEmulator()); + build.put("androidId", getAndroidId()); Map version = new HashMap<>(); if (VERSION.SDK_INT >= VERSION_CODES.M) { @@ -79,6 +86,18 @@ public void onMethodCall(MethodCall call, Result result) { } } + /** + * Returns the Android hardware device ID that is unique between the device + user and app + * signing. This key will change if the app is uninstalled or its data is cleared. Device factory + * reset will also result in a value change. + * + * @return The android ID + */ + @SuppressLint("HardwareIds") + private String getAndroidId() { + return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID); + } + /** * A simple emulator-detection based on the flutter tools detection logic and a couple of legacy * detection systems diff --git a/packages/device_info/example/lib/main.dart b/packages/device_info/example/lib/main.dart index a6088b5dd607..58cf460334f8 100644 --- a/packages/device_info/example/lib/main.dart +++ b/packages/device_info/example/lib/main.dart @@ -82,6 +82,7 @@ class _MyAppState extends State { 'tags': build.tags, 'type': build.type, 'isPhysicalDevice': build.isPhysicalDevice, + 'androidId': build.androidId, }; } diff --git a/packages/device_info/lib/device_info.dart b/packages/device_info/lib/device_info.dart index f36d1d435046..3b3d6ae52c60 100644 --- a/packages/device_info/lib/device_info.dart +++ b/packages/device_info/lib/device_info.dart @@ -58,6 +58,7 @@ class AndroidDeviceInfo { this.tags, this.type, this.isPhysicalDevice, + this.androidId, }) : supported32BitAbis = List.unmodifiable(supported32BitAbis), supported64BitAbis = List.unmodifiable(supported64BitAbis), supportedAbis = List.unmodifiable(supportedAbis); @@ -119,6 +120,9 @@ class AndroidDeviceInfo { /// `false` if the application is running in an emulator, `true` otherwise. final bool isPhysicalDevice; + /// The Android hardware device ID that is unique between the device + user and app signing. + final String androidId; + /// Deserializes from the message received from [_kChannel]. static AndroidDeviceInfo _fromMap(dynamic message) { final Map map = message; @@ -142,6 +146,7 @@ class AndroidDeviceInfo { tags: map['tags'], type: map['type'], isPhysicalDevice: map['isPhysicalDevice'], + androidId: map['androidId'], ); } diff --git a/packages/device_info/pubspec.yaml b/packages/device_info/pubspec.yaml index 2dec3e9f6c4b..fd4dd437bb25 100644 --- a/packages/device_info/pubspec.yaml +++ b/packages/device_info/pubspec.yaml @@ -3,7 +3,7 @@ description: Flutter plugin providing detailed information about the device (make, model, etc.), and Android or iOS version the app is running on. author: Flutter Team homepage: https://github.com/flutter/plugins/tree/master/packages/device_info -version: 0.2.1 +version: 0.3.0 flutter: plugin: