From 0f8982045d10b2b73a4c05f293c7a13c43554d3d Mon Sep 17 00:00:00 2001 From: Miguel Beltran Date: Tue, 19 Mar 2024 07:41:02 +0100 Subject: [PATCH 1/5] Revert "fix(device_info_plus): Ensure use of Activity Context to obtain WindowManager (#2688)" This reverts commit 13248d97e617ca7e6850c1775e06db597c26cbf1. --- .../plus/device_info/DeviceInfoPlusPlugin.kt | 33 +++---------------- .../example/android/app/build.gradle | 2 +- .../deviceinfoexample/example/MainActivity.kt | 18 +--------- .../device_info_plus/example/lib/main.dart | 4 +-- 4 files changed, 8 insertions(+), 49 deletions(-) diff --git a/packages/device_info_plus/device_info_plus/android/src/main/kotlin/dev/fluttercommunity/plus/device_info/DeviceInfoPlusPlugin.kt b/packages/device_info_plus/device_info_plus/android/src/main/kotlin/dev/fluttercommunity/plus/device_info/DeviceInfoPlusPlugin.kt index 3c97c7d2b7..ac0a21bca3 100644 --- a/packages/device_info_plus/device_info_plus/android/src/main/kotlin/dev/fluttercommunity/plus/device_info/DeviceInfoPlusPlugin.kt +++ b/packages/device_info_plus/device_info_plus/android/src/main/kotlin/dev/fluttercommunity/plus/device_info/DeviceInfoPlusPlugin.kt @@ -4,52 +4,27 @@ import android.content.Context import android.content.pm.PackageManager import android.view.WindowManager import io.flutter.embedding.engine.plugins.FlutterPlugin -import io.flutter.embedding.engine.plugins.activity.ActivityAware -import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding import io.flutter.plugin.common.BinaryMessenger import io.flutter.plugin.common.MethodChannel /** DeviceInfoPlusPlugin */ -class DeviceInfoPlusPlugin : FlutterPlugin, ActivityAware { +class DeviceInfoPlusPlugin : FlutterPlugin { private lateinit var methodChannel: MethodChannel override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) { - setupMethodChannel(binding.binaryMessenger) + setupMethodChannel(binding.binaryMessenger, binding.applicationContext) } override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) { methodChannel.setMethodCallHandler(null) } - private fun setupMethodChannel(messenger: BinaryMessenger) { + private fun setupMethodChannel(messenger: BinaryMessenger, context: Context) { methodChannel = MethodChannel(messenger, "dev.fluttercommunity.plus/device_info") - } - - override fun onAttachedToActivity(binding: ActivityPluginBinding) { - configureMethodCallHandler(binding) - } - - override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) { - configureMethodCallHandler(binding) - } - - override fun onDetachedFromActivityForConfigChanges() { - methodChannel.setMethodCallHandler(null) - } - - override fun onDetachedFromActivity() { - methodChannel.setMethodCallHandler(null) - } - - private fun configureMethodCallHandler(binding: ActivityPluginBinding) { - val context = binding.activity as Context val packageManager: PackageManager = context.packageManager - // WindowManager must be obtained from Activity Context - val windowManager: WindowManager = - context.getSystemService(Context.WINDOW_SERVICE) as WindowManager + val windowManager: WindowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager val handler = MethodCallHandlerImpl(packageManager, windowManager) methodChannel.setMethodCallHandler(handler) } - } diff --git a/packages/device_info_plus/device_info_plus/example/android/app/build.gradle b/packages/device_info_plus/device_info_plus/example/android/app/build.gradle index 0241f7f27a..b0941df9e9 100644 --- a/packages/device_info_plus/device_info_plus/example/android/app/build.gradle +++ b/packages/device_info_plus/device_info_plus/example/android/app/build.gradle @@ -26,7 +26,7 @@ apply plugin: 'kotlin-android' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" android { - compileSdk 34 + compileSdkVersion 33 namespace 'io.flutter.plugins.deviceinfoexample.example' diff --git a/packages/device_info_plus/device_info_plus/example/android/app/src/main/kotlin/io/flutter/plugins/deviceinfoexample/example/MainActivity.kt b/packages/device_info_plus/device_info_plus/example/android/app/src/main/kotlin/io/flutter/plugins/deviceinfoexample/example/MainActivity.kt index e2bd992575..69d0e0bfdd 100644 --- a/packages/device_info_plus/device_info_plus/example/android/app/src/main/kotlin/io/flutter/plugins/deviceinfoexample/example/MainActivity.kt +++ b/packages/device_info_plus/device_info_plus/example/android/app/src/main/kotlin/io/flutter/plugins/deviceinfoexample/example/MainActivity.kt @@ -1,21 +1,5 @@ package io.flutter.plugins.deviceinfoexample.example -import android.os.Build -import android.os.Bundle -import android.os.StrictMode import io.flutter.embedding.android.FlutterActivity -class MainActivity: FlutterActivity() { - - override fun onCreate(savedInstanceState: Bundle?) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { - // Ensures correct use of Activity Context to obtain the WindowManager - StrictMode.setVmPolicy(StrictMode.VmPolicy.Builder() - .detectIncorrectContextUse() - .penaltyLog() - .penaltyDeath() - .build()) - } - super.onCreate(savedInstanceState) - } -} +class MainActivity: FlutterActivity() diff --git a/packages/device_info_plus/device_info_plus/example/lib/main.dart b/packages/device_info_plus/device_info_plus/example/lib/main.dart index aa94cca50d..cbc69c2728 100644 --- a/packages/device_info_plus/device_info_plus/example/lib/main.dart +++ b/packages/device_info_plus/device_info_plus/example/lib/main.dart @@ -21,7 +21,7 @@ void main() { } class MyApp extends StatefulWidget { - const MyApp({super.key}); + const MyApp({Key? key}) : super(key: key); @override State createState() => _MyAppState(); @@ -148,7 +148,7 @@ class _MyAppState extends State { Map _readWebBrowserInfo(WebBrowserInfo data) { return { - 'browserName': data.browserName.name, + 'browserName': describeEnum(data.browserName), 'appCodeName': data.appCodeName, 'appName': data.appName, 'appVersion': data.appVersion, From 2a666d1d0134f00d102b77d94e13f0338eea55ab Mon Sep 17 00:00:00 2001 From: Miguel Beltran Date: Tue, 19 Mar 2024 07:42:37 +0100 Subject: [PATCH 2/5] re apply lint fixes --- .../device_info_plus/example/android/app/build.gradle | 2 +- .../device_info_plus/device_info_plus/example/lib/main.dart | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/device_info_plus/device_info_plus/example/android/app/build.gradle b/packages/device_info_plus/device_info_plus/example/android/app/build.gradle index b0941df9e9..0241f7f27a 100644 --- a/packages/device_info_plus/device_info_plus/example/android/app/build.gradle +++ b/packages/device_info_plus/device_info_plus/example/android/app/build.gradle @@ -26,7 +26,7 @@ apply plugin: 'kotlin-android' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" android { - compileSdkVersion 33 + compileSdk 34 namespace 'io.flutter.plugins.deviceinfoexample.example' diff --git a/packages/device_info_plus/device_info_plus/example/lib/main.dart b/packages/device_info_plus/device_info_plus/example/lib/main.dart index cbc69c2728..aa94cca50d 100644 --- a/packages/device_info_plus/device_info_plus/example/lib/main.dart +++ b/packages/device_info_plus/device_info_plus/example/lib/main.dart @@ -21,7 +21,7 @@ void main() { } class MyApp extends StatefulWidget { - const MyApp({Key? key}) : super(key: key); + const MyApp({super.key}); @override State createState() => _MyAppState(); @@ -148,7 +148,7 @@ class _MyAppState extends State { Map _readWebBrowserInfo(WebBrowserInfo data) { return { - 'browserName': describeEnum(data.browserName), + 'browserName': data.browserName.name, 'appCodeName': data.appCodeName, 'appName': data.appName, 'appVersion': data.appVersion, From 63e4c4ec2925d2cac80750a2ddf7854448691d10 Mon Sep 17 00:00:00 2001 From: Miguel Beltran Date: Tue, 19 Mar 2024 07:43:53 +0100 Subject: [PATCH 3/5] reapply the strict mode in the example project --- .../deviceinfoexample/example/MainActivity.kt | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/device_info_plus/device_info_plus/example/android/app/src/main/kotlin/io/flutter/plugins/deviceinfoexample/example/MainActivity.kt b/packages/device_info_plus/device_info_plus/example/android/app/src/main/kotlin/io/flutter/plugins/deviceinfoexample/example/MainActivity.kt index 69d0e0bfdd..e2bd992575 100644 --- a/packages/device_info_plus/device_info_plus/example/android/app/src/main/kotlin/io/flutter/plugins/deviceinfoexample/example/MainActivity.kt +++ b/packages/device_info_plus/device_info_plus/example/android/app/src/main/kotlin/io/flutter/plugins/deviceinfoexample/example/MainActivity.kt @@ -1,5 +1,21 @@ package io.flutter.plugins.deviceinfoexample.example +import android.os.Build +import android.os.Bundle +import android.os.StrictMode import io.flutter.embedding.android.FlutterActivity -class MainActivity: FlutterActivity() +class MainActivity: FlutterActivity() { + + override fun onCreate(savedInstanceState: Bundle?) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + // Ensures correct use of Activity Context to obtain the WindowManager + StrictMode.setVmPolicy(StrictMode.VmPolicy.Builder() + .detectIncorrectContextUse() + .penaltyLog() + .penaltyDeath() + .build()) + } + super.onCreate(savedInstanceState) + } +} From 28911f229963f5d3df84a3ebb28a501ce6742d6c Mon Sep 17 00:00:00 2001 From: Miguel Beltran Date: Tue, 19 Mar 2024 07:47:51 +0100 Subject: [PATCH 4/5] remove display metrics on Android --- .../plus/device_info/DeviceInfoPlusPlugin.kt | 3 +- .../plus/device_info/MethodCallHandlerImpl.kt | 16 ----- .../device_info_plus_test.dart | 5 -- .../device_info_plus/example/lib/main.dart | 8 --- .../lib/src/model/android_device_info.dart | 65 ------------------- .../test/model/android_device_info_fake.dart | 8 --- .../test/model/android_device_info_test.dart | 4 -- 7 files changed, 1 insertion(+), 108 deletions(-) diff --git a/packages/device_info_plus/device_info_plus/android/src/main/kotlin/dev/fluttercommunity/plus/device_info/DeviceInfoPlusPlugin.kt b/packages/device_info_plus/device_info_plus/android/src/main/kotlin/dev/fluttercommunity/plus/device_info/DeviceInfoPlusPlugin.kt index ac0a21bca3..72775a9eaa 100644 --- a/packages/device_info_plus/device_info_plus/android/src/main/kotlin/dev/fluttercommunity/plus/device_info/DeviceInfoPlusPlugin.kt +++ b/packages/device_info_plus/device_info_plus/android/src/main/kotlin/dev/fluttercommunity/plus/device_info/DeviceInfoPlusPlugin.kt @@ -23,8 +23,7 @@ class DeviceInfoPlusPlugin : FlutterPlugin { private fun setupMethodChannel(messenger: BinaryMessenger, context: Context) { methodChannel = MethodChannel(messenger, "dev.fluttercommunity.plus/device_info") val packageManager: PackageManager = context.packageManager - val windowManager: WindowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager - val handler = MethodCallHandlerImpl(packageManager, windowManager) + val handler = MethodCallHandlerImpl(packageManager) methodChannel.setMethodCallHandler(handler) } } diff --git a/packages/device_info_plus/device_info_plus/android/src/main/kotlin/dev/fluttercommunity/plus/device_info/MethodCallHandlerImpl.kt b/packages/device_info_plus/device_info_plus/android/src/main/kotlin/dev/fluttercommunity/plus/device_info/MethodCallHandlerImpl.kt index 2b2ea9b72d..ff803775fb 100644 --- a/packages/device_info_plus/device_info_plus/android/src/main/kotlin/dev/fluttercommunity/plus/device_info/MethodCallHandlerImpl.kt +++ b/packages/device_info_plus/device_info_plus/android/src/main/kotlin/dev/fluttercommunity/plus/device_info/MethodCallHandlerImpl.kt @@ -17,7 +17,6 @@ import kotlin.collections.HashMap */ internal class MethodCallHandlerImpl( private val packageManager: PackageManager, - private val windowManager: WindowManager, ) : MethodCallHandler { override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) { @@ -64,21 +63,6 @@ internal class MethodCallHandlerImpl( version["sdkInt"] = Build.VERSION.SDK_INT build["version"] = version - val display: Display = windowManager.defaultDisplay - val metrics = DisplayMetrics() - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { - display.getRealMetrics(metrics) - } else { - display.getMetrics(metrics) - } - - val displayResult: MutableMap = HashMap() - displayResult["widthPx"] = metrics.widthPixels.toDouble() - displayResult["heightPx"] = metrics.heightPixels.toDouble() - displayResult["xDpi"] = metrics.xdpi - displayResult["yDpi"] = metrics.ydpi - build["displayMetrics"] = displayResult - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { build["serialNumber"] = try { Build.getSerial() diff --git a/packages/device_info_plus/device_info_plus/example/integration_test/device_info_plus_test.dart b/packages/device_info_plus/device_info_plus/example/integration_test/device_info_plus_test.dart index b591abcc0f..162389aed0 100644 --- a/packages/device_info_plus/device_info_plus/example/integration_test/device_info_plus_test.dart +++ b/packages/device_info_plus/device_info_plus/example/integration_test/device_info_plus_test.dart @@ -94,11 +94,6 @@ void main() { expect(androidInfo.fingerprint, isNotNull); expect(androidInfo.hardware, isNotNull); - expect(androidInfo.displayMetrics.heightPx, isNotNull); - expect(androidInfo.displayMetrics.widthPx, isNotNull); - expect(androidInfo.displayMetrics.yDpi, isNotNull); - expect(androidInfo.displayMetrics.xDpi, isNotNull); - expect(androidInfo.host, isNotNull); expect(androidInfo.id, isNotNull); expect(androidInfo.manufacturer, isNotNull); diff --git a/packages/device_info_plus/device_info_plus/example/lib/main.dart b/packages/device_info_plus/device_info_plus/example/lib/main.dart index aa94cca50d..e6c177c452 100644 --- a/packages/device_info_plus/device_info_plus/example/lib/main.dart +++ b/packages/device_info_plus/device_info_plus/example/lib/main.dart @@ -101,14 +101,6 @@ class _MyAppState extends State { 'type': build.type, 'isPhysicalDevice': build.isPhysicalDevice, 'systemFeatures': build.systemFeatures, - 'displaySizeInches': - ((build.displayMetrics.sizeInches * 10).roundToDouble() / 10), - 'displayWidthPixels': build.displayMetrics.widthPx, - 'displayWidthInches': build.displayMetrics.widthInches, - 'displayHeightPixels': build.displayMetrics.heightPx, - 'displayHeightInches': build.displayMetrics.heightInches, - 'displayXDpi': build.displayMetrics.xDpi, - 'displayYDpi': build.displayMetrics.yDpi, 'serialNumber': build.serialNumber, }; } diff --git a/packages/device_info_plus/device_info_plus/lib/src/model/android_device_info.dart b/packages/device_info_plus/device_info_plus/lib/src/model/android_device_info.dart index 1d3c60ed50..6fdcbd16f2 100644 --- a/packages/device_info_plus/device_info_plus/lib/src/model/android_device_info.dart +++ b/packages/device_info_plus/device_info_plus/lib/src/model/android_device_info.dart @@ -31,7 +31,6 @@ class AndroidDeviceInfo extends BaseDeviceInfo { required this.type, required this.isPhysicalDevice, required List systemFeatures, - required this.displayMetrics, required this.serialNumber, }) : supported32BitAbis = List.unmodifiable(supported32BitAbis), supported64BitAbis = List.unmodifiable(supported64BitAbis), @@ -132,9 +131,6 @@ class AndroidDeviceInfo extends BaseDeviceInfo { /// https://developer.android.com/reference/android/content/pm/PackageManager final List systemFeatures; - /// Information about the current android display. - final AndroidDisplayMetrics displayMetrics; - /// Hardware serial number of the device, if available /// /// There are special restrictions on this identifier, more info here: @@ -166,8 +162,6 @@ class AndroidDeviceInfo extends BaseDeviceInfo { type: map['type'], isPhysicalDevice: map['isPhysicalDevice'], systemFeatures: _fromList(map['systemFeatures'] ?? []), - displayMetrics: AndroidDisplayMetrics._fromMap( - map['displayMetrics']?.cast() ?? {}), serialNumber: map['serialNumber'], ); } @@ -247,62 +241,3 @@ class AndroidBuildVersion { ); } } - -/// Details for the current display -/// -/// See: https://developer.android.com/reference/android/util/DisplayMetrics -class AndroidDisplayMetrics { - const AndroidDisplayMetrics._({ - required this.widthPx, - required this.heightPx, - required this.xDpi, - required this.yDpi, - }); - - /// Gets the absolute width in pixels of the largest region of the display accessible to an app - /// in the current system state, without subtracting any window decor or applying scaling factors. - final double widthPx; - - /// Gets the absolute height in pixels of the largest region of the display accessible to an app - /// in the current system state, without subtracting any window decor or applying scaling factors. - final double heightPx; - - /// The exact physical pixels per inch of the screen in the X dimension. - final double xDpi; - - /// The exact physical pixels per inch of the screen in the Y dimension. - final double yDpi; - - /// The exact physical display width in inches. - double get widthInches => widthPx / xDpi; - - /// The exact physical display height in inches. - double get heightInches => heightPx / yDpi; - - /// The exact physical size in inches measured diagonally across the display. - double get sizeInches { - final width = widthInches; - final height = heightInches; - return math.sqrt((width * width) + (height * height)); - } - - /// Serializes [AndroidDisplayMetrics] to map. - Map toMap() { - return { - 'widthPx': widthPx, - 'heightPx': heightPx, - 'xDpi': xDpi, - 'yDpi': yDpi, - }; - } - - /// Deserializes from the map message received from the [MethodChannel]. - static AndroidDisplayMetrics _fromMap(Map map) { - return AndroidDisplayMetrics._( - widthPx: map['widthPx'], - heightPx: map['heightPx'], - xDpi: map['xDpi'], - yDpi: map['yDpi'], - ); - } -} diff --git a/packages/device_info_plus/device_info_plus/test/model/android_device_info_fake.dart b/packages/device_info_plus/device_info_plus/test/model/android_device_info_fake.dart index 2c7a4941cf..6baae385eb 100644 --- a/packages/device_info_plus/device_info_plus/test/model/android_device_info_fake.dart +++ b/packages/device_info_plus/device_info_plus/test/model/android_device_info_fake.dart @@ -10,13 +10,6 @@ const _fakeAndroidBuildVersion = { 'securityPatch': 'securityPatch', }; -const _fakeDisplayMetrics = { - 'widthPx': 1080.0, - 'heightPx': 2220.0, - 'xDpi': 530.0859, - 'yDpi': 529.4639, -}; - const _fakeSupportedAbis = ['arm64-v8a', 'x86', 'x86_64']; const _fakeSupported32BitAbis = ['x86 (IA-32)', 'MMX']; const _fakeSupported64BitAbis = ['x86-64', 'MMX', 'SSSE3']; @@ -43,6 +36,5 @@ const _fakeAndroidDeviceInfo = { 'version': _fakeAndroidBuildVersion, 'supported64BitAbis': _fakeSupported64BitAbis, 'supported32BitAbis': _fakeSupported32BitAbis, - 'displayMetrics': _fakeDisplayMetrics, 'serialNumber': 'SERIAL', }; diff --git a/packages/device_info_plus/device_info_plus/test/model/android_device_info_test.dart b/packages/device_info_plus/device_info_plus/test/model/android_device_info_test.dart index 76e969ba5d..7c1d272630 100644 --- a/packages/device_info_plus/device_info_plus/test/model/android_device_info_test.dart +++ b/packages/device_info_plus/device_info_plus/test/model/android_device_info_test.dart @@ -37,10 +37,6 @@ void main() { expect(androidDeviceInfo.version.codename, 'codename'); expect(androidDeviceInfo.version.incremental, 'incremental'); expect(androidDeviceInfo.version.securityPatch, 'securityPatch'); - expect(androidDeviceInfo.displayMetrics.widthPx, 1080); - expect(androidDeviceInfo.displayMetrics.heightPx, 2220); - expect(androidDeviceInfo.displayMetrics.xDpi, 530.0859); - expect(androidDeviceInfo.displayMetrics.yDpi, 529.4639); expect(androidDeviceInfo.serialNumber, 'SERIAL'); }); From bbc5c67be73d52565c9935f0e997f36dca185536 Mon Sep 17 00:00:00 2001 From: Miguel Beltran Date: Tue, 19 Mar 2024 07:56:51 +0100 Subject: [PATCH 5/5] remove unused import --- .../device_info_plus/lib/src/model/android_device_info.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/device_info_plus/device_info_plus/lib/src/model/android_device_info.dart b/packages/device_info_plus/device_info_plus/lib/src/model/android_device_info.dart index 6fdcbd16f2..56df796a2e 100644 --- a/packages/device_info_plus/device_info_plus/lib/src/model/android_device_info.dart +++ b/packages/device_info_plus/device_info_plus/lib/src/model/android_device_info.dart @@ -2,7 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'dart:math' as math show sqrt; import 'package:device_info_plus_platform_interface/model/base_device_info.dart'; /// Information derived from `android.os.Build`.