Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
Added android device id to android device info (#639)
Browse files Browse the repository at this point in the history
* Added android device id to android device info

* Bumped device info to version 0.3.0
  • Loading branch information
BWMuller authored and kroikie committed Oct 2, 2018
1 parent 894f2dd commit e6f85ba
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
4 changes: 4 additions & 0 deletions 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.
Expand Down
Expand Up @@ -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;
Expand All @@ -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[] {};
Expand All @@ -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) {
Expand Down Expand Up @@ -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<String, Object> version = new HashMap<>();
if (VERSION.SDK_INT >= VERSION_CODES.M) {
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions packages/device_info/example/lib/main.dart
Expand Up @@ -82,6 +82,7 @@ class _MyAppState extends State<MyApp> {
'tags': build.tags,
'type': build.type,
'isPhysicalDevice': build.isPhysicalDevice,
'androidId': build.androidId,
};
}

Expand Down
5 changes: 5 additions & 0 deletions packages/device_info/lib/device_info.dart
Expand Up @@ -58,6 +58,7 @@ class AndroidDeviceInfo {
this.tags,
this.type,
this.isPhysicalDevice,
this.androidId,
}) : supported32BitAbis = List<String>.unmodifiable(supported32BitAbis),
supported64BitAbis = List<String>.unmodifiable(supported64BitAbis),
supportedAbis = List<String>.unmodifiable(supportedAbis);
Expand Down Expand Up @@ -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<dynamic, dynamic> map = message;
Expand All @@ -142,6 +146,7 @@ class AndroidDeviceInfo {
tags: map['tags'],
type: map['type'],
isPhysicalDevice: map['isPhysicalDevice'],
androidId: map['androidId'],
);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/device_info/pubspec.yaml
Expand Up @@ -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 <flutter-dev@googlegroups.com>
homepage: https://github.com/flutter/plugins/tree/master/packages/device_info
version: 0.2.1
version: 0.3.0

flutter:
plugin:
Expand Down

0 comments on commit e6f85ba

Please sign in to comment.