Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
draft
  • Loading branch information
Chris Yang committed Oct 7, 2019
commit 952b5d065e72be2e5b752a91be26d7415e4f51a1
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package dev.flutter.plugins.deviceinfo;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.deviceinfo.MethodCallHandlerImpl;

/**
* Entry point of the plugin. It sets up the {@link io.flutter.plugin.common.MethodChannel.MethodCallHandler}
* during {@link #onAttachedToEngine(FlutterPluginBinding)}.
*/
public class DeviceInfoPlugin implements FlutterPlugin {

@Override
public void onAttachedToEngine(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);
}

@Override
public void onDetachedFromEngine(FlutterPluginBinding binding) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,120 +4,19 @@

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;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/** DeviceInfoPlugin */
public class DeviceInfoPlugin implements MethodCallHandler {
private final Context context;

/** Substitute for missing values. */
private static final String[] EMPTY_STRING_LIST = new String[] {};
/** DeviceInfoPlugin */
public class DeviceInfoPlugin {

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

/** Do not allow direct instantiation. */
private DeviceInfoPlugin(Context context) {
this.context = context;
}

@Override
public void onMethodCall(MethodCall call, Result result) {
if (call.method.equals("getAndroidDeviceInfo")) {
Map<String, Object> build = new HashMap<>();
build.put("board", Build.BOARD);
build.put("bootloader", Build.BOOTLOADER);
build.put("brand", Build.BRAND);
build.put("device", Build.DEVICE);
build.put("display", Build.DISPLAY);
build.put("fingerprint", Build.FINGERPRINT);
build.put("hardware", Build.HARDWARE);
build.put("host", Build.HOST);
build.put("id", Build.ID);
build.put("manufacturer", Build.MANUFACTURER);
build.put("model", Build.MODEL);
build.put("product", Build.PRODUCT);
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
build.put("supported32BitAbis", Arrays.asList(Build.SUPPORTED_32_BIT_ABIS));
build.put("supported64BitAbis", Arrays.asList(Build.SUPPORTED_64_BIT_ABIS));
build.put("supportedAbis", Arrays.asList(Build.SUPPORTED_ABIS));
} else {
build.put("supported32BitAbis", Arrays.asList(EMPTY_STRING_LIST));
build.put("supported64BitAbis", Arrays.asList(EMPTY_STRING_LIST));
build.put("supportedAbis", Arrays.asList(EMPTY_STRING_LIST));
}
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) {
version.put("baseOS", VERSION.BASE_OS);
version.put("previewSdkInt", VERSION.PREVIEW_SDK_INT);
version.put("securityPatch", VERSION.SECURITY_PATCH);
}
version.put("codename", VERSION.CODENAME);
version.put("incremental", VERSION.INCREMENTAL);
version.put("release", VERSION.RELEASE);
version.put("sdkInt", VERSION.SDK_INT);
build.put("version", version);

result.success(build);
} else {
result.notImplemented();
}
}

/**
* 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
*/
private boolean isEmulator() {
return (Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic"))
|| Build.FINGERPRINT.startsWith("generic")
|| Build.FINGERPRINT.startsWith("unknown")
|| Build.HARDWARE.contains("goldfish")
|| Build.HARDWARE.contains("ranchu")
|| Build.MODEL.contains("google_sdk")
|| Build.MODEL.contains("Emulator")
|| Build.MODEL.contains("Android SDK built for x86")
|| Build.MANUFACTURER.contains("Genymotion")
|| Build.PRODUCT.contains("sdk_google")
|| Build.PRODUCT.contains("google_sdk")
|| Build.PRODUCT.contains("sdk")
|| Build.PRODUCT.contains("sdk_x86")
|| Build.PRODUCT.contains("vbox86p")
|| Build.PRODUCT.contains("emulator")
|| Build.PRODUCT.contains("simulator");
final MethodCallHandlerImpl handler = new MethodCallHandlerImpl(registrar.context().getContentResolver());
channel.setMethodCallHandler(handler);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package io.flutter.plugins.deviceinfo;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.os.Build;
import android.provider.Settings;

import androidx.annotation.NonNull;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

/**
* The implementation of {@link MethodChannel.MethodCallHandler} for the plugin.
* Responsible for receiving method calls from method channel.
*/
public class MethodCallHandlerImpl implements MethodChannel.MethodCallHandler {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we make this class default (package-private) so that we don't need to worry about breaking other clients who may be using it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done


private ContentResolver contentResolver;

/** Substitute for missing values. */
private static final String[] EMPTY_STRING_LIST = new String[] {};

/**
* Constructs DeviceInfo. The {@code contentResolver} must not be null.
*/
public MethodCallHandlerImpl(@NonNull ContentResolver contentResolver) {
this.contentResolver = contentResolver;
}

@Override
public void onMethodCall(MethodCall call, MethodChannel.Result result) {
if (call.method.equals("getAndroidDeviceInfo")) {
Map<String, Object> build = new HashMap<>();
build.put("board", Build.BOARD);
build.put("bootloader", Build.BOOTLOADER);
build.put("brand", Build.BRAND);
build.put("device", Build.DEVICE);
build.put("display", Build.DISPLAY);
build.put("fingerprint", Build.FINGERPRINT);
build.put("hardware", Build.HARDWARE);
build.put("host", Build.HOST);
build.put("id", Build.ID);
build.put("manufacturer", Build.MANUFACTURER);
build.put("model", Build.MODEL);
build.put("product", Build.PRODUCT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
build.put("supported32BitAbis", Arrays.asList(Build.SUPPORTED_32_BIT_ABIS));
build.put("supported64BitAbis", Arrays.asList(Build.SUPPORTED_64_BIT_ABIS));
build.put("supportedAbis", Arrays.asList(Build.SUPPORTED_ABIS));
} else {
build.put("supported32BitAbis", Arrays.asList(EMPTY_STRING_LIST));
build.put("supported64BitAbis", Arrays.asList(EMPTY_STRING_LIST));
build.put("supportedAbis", Arrays.asList(EMPTY_STRING_LIST));
}
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 (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
version.put("baseOS", Build.VERSION.BASE_OS);
version.put("previewSdkInt", Build.VERSION.PREVIEW_SDK_INT);
version.put("securityPatch", Build.VERSION.SECURITY_PATCH);
}
version.put("codename", Build.VERSION.CODENAME);
version.put("incremental", Build.VERSION.INCREMENTAL);
version.put("release", Build.VERSION.RELEASE);
version.put("sdkInt", Build.VERSION.SDK_INT);
build.put("version", version);

result.success(build);
} else {
result.notImplemented();
}
}

/**
* 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(contentResolver, Settings.Secure.ANDROID_ID);
}

/**
* A simple emulator-detection based on the flutter tools detection logic and a couple of legacy
* detection systems
*/
private boolean isEmulator() {
return (Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic"))
|| Build.FINGERPRINT.startsWith("generic")
|| Build.FINGERPRINT.startsWith("unknown")
|| Build.HARDWARE.contains("goldfish")
|| Build.HARDWARE.contains("ranchu")
|| Build.MODEL.contains("google_sdk")
|| Build.MODEL.contains("Emulator")
|| Build.MODEL.contains("Android SDK built for x86")
|| Build.MANUFACTURER.contains("Genymotion")
|| Build.PRODUCT.contains("sdk_google")
|| Build.PRODUCT.contains("google_sdk")
|| Build.PRODUCT.contains("sdk")
|| Build.PRODUCT.contains("sdk_x86")
|| Build.PRODUCT.contains("vbox86p")
|| Build.PRODUCT.contains("emulator")
|| Build.PRODUCT.contains("simulator");
}
}
3 changes: 3 additions & 0 deletions packages/device_info/example/android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
org.gradle.jvmargs=-Xmx1536M
android.enableR8=true
android.useAndroidX=true
android.enableJetifier=true