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

Added android device id to android device info #639

Merged
merged 7 commits into from
Oct 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/device_info/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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