forked from flutter/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[device_info] Support v2 android embedding. (flutter#2163)
- Loading branch information
Chris Yang
authored and
Park Sung Min
committed
Dec 17, 2019
1 parent
8af33b3
commit a3bbef7
Showing
14 changed files
with
301 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
...evice_info/android/src/main/java/io/flutter/plugins/deviceinfo/MethodCallHandlerImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// 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 io.flutter.plugin.common.MethodCall; | ||
import io.flutter.plugin.common.MethodChannel; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* The implementation of {@link MethodChannel.MethodCallHandler} for the plugin. Responsible for | ||
* receiving method calls from method channel. | ||
*/ | ||
class MethodCallHandlerImpl implements MethodChannel.MethodCallHandler { | ||
|
||
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. */ | ||
MethodCallHandlerImpl(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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...e/android/app/src/main/java/io/flutter/plugins/deviceinfoexample/EmbeddingV1Activity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// 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.deviceinfoexample; | ||
|
||
import android.os.Bundle; | ||
import io.flutter.app.FlutterActivity; | ||
import io.flutter.plugins.GeneratedPluginRegistrant; | ||
|
||
public class EmbeddingV1Activity extends FlutterActivity { | ||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
GeneratedPluginRegistrant.registerWith(this); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...droid/app/src/main/java/io/flutter/plugins/deviceinfoexample/EmbeddingV1ActivityTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.flutter.plugins.deviceinfoexample; | ||
|
||
import androidx.test.rule.ActivityTestRule; | ||
import dev.flutter.plugins.e2e.FlutterRunner; | ||
import org.junit.Rule; | ||
import org.junit.runner.RunWith; | ||
|
||
@RunWith(FlutterRunner.class) | ||
public class EmbeddingV1ActivityTest { | ||
@Rule | ||
public ActivityTestRule<EmbeddingV1Activity> rule = | ||
new ActivityTestRule<>(EmbeddingV1Activity.class); | ||
} |
17 changes: 10 additions & 7 deletions
17
.../example/android/app/src/main/java/io/flutter/plugins/deviceinfoexample/MainActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,20 @@ | ||
// Copyright 2017 The Chromium Authors. All rights reserved. | ||
// Copyright 2019 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.deviceinfoexample; | ||
|
||
import android.os.Bundle; | ||
import io.flutter.app.FlutterActivity; | ||
import io.flutter.plugins.GeneratedPluginRegistrant; | ||
import io.flutter.embedding.android.FlutterActivity; | ||
import io.flutter.embedding.engine.FlutterEngine; | ||
import io.flutter.plugins.deviceinfo.DeviceInfoPlugin; | ||
|
||
public class MainActivity extends FlutterActivity { | ||
|
||
// TODO(cyanglaz): Remove this once v2 of GeneratedPluginRegistrant rolls to stable. | ||
// https://github.com/flutter/flutter/issues/42694 | ||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
GeneratedPluginRegistrant.registerWith(this); | ||
public void configureFlutterEngine(FlutterEngine flutterEngine) { | ||
super.configureFlutterEngine(flutterEngine); | ||
flutterEngine.getPlugins().add(new DeviceInfoPlugin()); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...mple/android/app/src/main/java/io/flutter/plugins/deviceinfoexample/MainActivityTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright 2019 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.deviceinfoexample; | ||
|
||
import androidx.test.rule.ActivityTestRule; | ||
import dev.flutter.plugins.e2e.FlutterRunner; | ||
import org.junit.Rule; | ||
import org.junit.runner.RunWith; | ||
|
||
@RunWith(FlutterRunner.class) | ||
public class MainActivityTest { | ||
@Rule public ActivityTestRule<MainActivity> rule = new ActivityTestRule<>(MainActivity.class); | ||
} |
Oops, something went wrong.