From e398ecd204c2e3e5937b9193c7468e80dbf7c5b7 Mon Sep 17 00:00:00 2001 From: muhib349 Date: Thu, 28 Mar 2024 17:13:04 +0000 Subject: [PATCH 1/3] exposed isLowRamDevice property for androidDeviceInfo --- .../plus/device_info/DeviceInfoPlusPlugin.kt | 2 +- .../plus/device_info/MethodCallHandlerImpl.kt | 13 ++++- .../device_info_plus/example/lib/main.dart | 1 + .../lib/src/model/android_device_info.dart | 52 ++++++++++--------- .../test/model/android_device_info_fake.dart | 1 + .../test/model/android_device_info_test.dart | 1 + 6 files changed, 44 insertions(+), 26 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 72775a9eaa..c5f8660a2b 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,7 +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 handler = MethodCallHandlerImpl(packageManager) + val handler = MethodCallHandlerImpl(packageManager, context) 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 ff803775fb..52c37be55e 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 @@ -1,5 +1,7 @@ package dev.fluttercommunity.plus.device_info +import android.app.ActivityManager +import android.content.Context import android.content.pm.FeatureInfo import android.content.pm.PackageManager import android.os.Build @@ -17,6 +19,7 @@ import kotlin.collections.HashMap */ internal class MethodCallHandlerImpl( private val packageManager: PackageManager, + private val context: Context, ) : MethodCallHandler { override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) { @@ -62,7 +65,7 @@ internal class MethodCallHandlerImpl( version["release"] = Build.VERSION.RELEASE version["sdkInt"] = Build.VERSION.SDK_INT build["version"] = version - + build["isLowRamDevice"] = isLowRamDevice() if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { build["serialNumber"] = try { Build.getSerial() @@ -104,4 +107,12 @@ internal class MethodCallHandlerImpl( || Build.PRODUCT.contains("vbox86p") || Build.PRODUCT.contains("emulator") || Build.PRODUCT.contains("simulator")) + + /** + * To detect whether it is running on a low-memory device + */ + private fun isLowRamDevice(): Boolean { + val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager + return activityManager.isLowRamDevice + } } 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 e6c177c452..58f4888b3f 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 @@ -102,6 +102,7 @@ class _MyAppState extends State { 'isPhysicalDevice': build.isPhysicalDevice, 'systemFeatures': build.systemFeatures, 'serialNumber': build.serialNumber, + 'isLowRamDevice': build.isLowRamDevice, }; } 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 56df796a2e..7b5798c888 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,6 +31,7 @@ class AndroidDeviceInfo extends BaseDeviceInfo { required this.isPhysicalDevice, required List systemFeatures, required this.serialNumber, + required this.isLowRamDevice, }) : supported32BitAbis = List.unmodifiable(supported32BitAbis), supported64BitAbis = List.unmodifiable(supported64BitAbis), supportedAbis = List.unmodifiable(supportedAbis), @@ -136,33 +137,36 @@ class AndroidDeviceInfo extends BaseDeviceInfo { /// https://developer.android.com/reference/android/os/Build#getSerial() final String serialNumber; + /// `true` is it running on a low ram device `false` otherwise + final bool isLowRamDevice; + /// Deserializes from the message received from [_kChannel]. static AndroidDeviceInfo fromMap(Map map) { return AndroidDeviceInfo._( - data: map, - version: AndroidBuildVersion._fromMap( - map['version']?.cast() ?? {}), - board: map['board'], - bootloader: map['bootloader'], - brand: map['brand'], - device: map['device'], - display: map['display'], - fingerprint: map['fingerprint'], - hardware: map['hardware'], - host: map['host'], - id: map['id'], - manufacturer: map['manufacturer'], - model: map['model'], - product: map['product'], - supported32BitAbis: _fromList(map['supported32BitAbis'] ?? []), - supported64BitAbis: _fromList(map['supported64BitAbis'] ?? []), - supportedAbis: _fromList(map['supportedAbis'] ?? []), - tags: map['tags'], - type: map['type'], - isPhysicalDevice: map['isPhysicalDevice'], - systemFeatures: _fromList(map['systemFeatures'] ?? []), - serialNumber: map['serialNumber'], - ); + data: map, + version: AndroidBuildVersion._fromMap( + map['version']?.cast() ?? {}), + board: map['board'], + bootloader: map['bootloader'], + brand: map['brand'], + device: map['device'], + display: map['display'], + fingerprint: map['fingerprint'], + hardware: map['hardware'], + host: map['host'], + id: map['id'], + manufacturer: map['manufacturer'], + model: map['model'], + product: map['product'], + supported32BitAbis: _fromList(map['supported32BitAbis'] ?? []), + supported64BitAbis: _fromList(map['supported64BitAbis'] ?? []), + supportedAbis: _fromList(map['supportedAbis'] ?? []), + tags: map['tags'], + type: map['type'], + isPhysicalDevice: map['isPhysicalDevice'], + systemFeatures: _fromList(map['systemFeatures'] ?? []), + serialNumber: map['serialNumber'], + isLowRamDevice: map['isLowRamDevice']); } /// Deserializes message as List 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 6baae385eb..9286bd5d7d 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 @@ -37,4 +37,5 @@ const _fakeAndroidDeviceInfo = { 'supported64BitAbis': _fakeSupported64BitAbis, 'supported32BitAbis': _fakeSupported32BitAbis, 'serialNumber': 'SERIAL', + 'isLowRamDevice': false, }; 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 7c1d272630..bd88344cd2 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 @@ -38,6 +38,7 @@ void main() { expect(androidDeviceInfo.version.incremental, 'incremental'); expect(androidDeviceInfo.version.securityPatch, 'securityPatch'); expect(androidDeviceInfo.serialNumber, 'SERIAL'); + expect(androidDeviceInfo.isLowRamDevice, false); }); test('toMap should return map with correct key and map', () { From 2535f24fb7c10fcfe1e0e80661a4c1222e7cd55c Mon Sep 17 00:00:00 2001 From: muhib349 Date: Thu, 28 Mar 2024 22:13:39 +0000 Subject: [PATCH 2/3] implemented review feedbacks --- .../plus/device_info/DeviceInfoPlusPlugin.kt | 4 +- .../plus/device_info/MethodCallHandlerImpl.kt | 13 +---- .../lib/src/model/android_device_info.dart | 49 ++++++++++--------- 3 files changed, 30 insertions(+), 36 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 c5f8660a2b..787086a256 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 @@ -1,5 +1,6 @@ package dev.fluttercommunity.plus.device_info +import android.app.ActivityManager import android.content.Context import android.content.pm.PackageManager import android.view.WindowManager @@ -23,7 +24,8 @@ class DeviceInfoPlusPlugin : FlutterPlugin { private fun setupMethodChannel(messenger: BinaryMessenger, context: Context) { methodChannel = MethodChannel(messenger, "dev.fluttercommunity.plus/device_info") val packageManager: PackageManager = context.packageManager - val handler = MethodCallHandlerImpl(packageManager, context) + val activityManager: ActivityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager + val handler = MethodCallHandlerImpl(packageManager, activityManager) 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 52c37be55e..bf2a15e44d 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 @@ -1,7 +1,6 @@ package dev.fluttercommunity.plus.device_info import android.app.ActivityManager -import android.content.Context import android.content.pm.FeatureInfo import android.content.pm.PackageManager import android.os.Build @@ -19,7 +18,7 @@ import kotlin.collections.HashMap */ internal class MethodCallHandlerImpl( private val packageManager: PackageManager, - private val context: Context, + private val activityManager: ActivityManager, ) : MethodCallHandler { override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) { @@ -65,7 +64,7 @@ internal class MethodCallHandlerImpl( version["release"] = Build.VERSION.RELEASE version["sdkInt"] = Build.VERSION.SDK_INT build["version"] = version - build["isLowRamDevice"] = isLowRamDevice() + build["isLowRamDevice"] = activityManager.isLowRamDevice if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { build["serialNumber"] = try { Build.getSerial() @@ -107,12 +106,4 @@ internal class MethodCallHandlerImpl( || Build.PRODUCT.contains("vbox86p") || Build.PRODUCT.contains("emulator") || Build.PRODUCT.contains("simulator")) - - /** - * To detect whether it is running on a low-memory device - */ - private fun isLowRamDevice(): Boolean { - val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager - return activityManager.isLowRamDevice - } } 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 7b5798c888..e7feca51d2 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 @@ -143,30 +143,31 @@ class AndroidDeviceInfo extends BaseDeviceInfo { /// Deserializes from the message received from [_kChannel]. static AndroidDeviceInfo fromMap(Map map) { return AndroidDeviceInfo._( - data: map, - version: AndroidBuildVersion._fromMap( - map['version']?.cast() ?? {}), - board: map['board'], - bootloader: map['bootloader'], - brand: map['brand'], - device: map['device'], - display: map['display'], - fingerprint: map['fingerprint'], - hardware: map['hardware'], - host: map['host'], - id: map['id'], - manufacturer: map['manufacturer'], - model: map['model'], - product: map['product'], - supported32BitAbis: _fromList(map['supported32BitAbis'] ?? []), - supported64BitAbis: _fromList(map['supported64BitAbis'] ?? []), - supportedAbis: _fromList(map['supportedAbis'] ?? []), - tags: map['tags'], - type: map['type'], - isPhysicalDevice: map['isPhysicalDevice'], - systemFeatures: _fromList(map['systemFeatures'] ?? []), - serialNumber: map['serialNumber'], - isLowRamDevice: map['isLowRamDevice']); + data: map, + version: AndroidBuildVersion._fromMap( + map['version']?.cast() ?? {}), + board: map['board'], + bootloader: map['bootloader'], + brand: map['brand'], + device: map['device'], + display: map['display'], + fingerprint: map['fingerprint'], + hardware: map['hardware'], + host: map['host'], + id: map['id'], + manufacturer: map['manufacturer'], + model: map['model'], + product: map['product'], + supported32BitAbis: _fromList(map['supported32BitAbis'] ?? []), + supported64BitAbis: _fromList(map['supported64BitAbis'] ?? []), + supportedAbis: _fromList(map['supportedAbis'] ?? []), + tags: map['tags'], + type: map['type'], + isPhysicalDevice: map['isPhysicalDevice'], + systemFeatures: _fromList(map['systemFeatures'] ?? []), + serialNumber: map['serialNumber'], + isLowRamDevice: map['isLowRamDevice'], + ); } /// Deserializes message as List From f56dc51175f56f80e52b82af6fe19e9638517aaa Mon Sep 17 00:00:00 2001 From: Gazi Mohib Date: Fri, 29 Mar 2024 10:31:47 +0000 Subject: [PATCH 3/3] Updated code documentation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: K. P. KrasiƄski-Sroka --- .../device_info_plus/lib/src/model/android_device_info.dart | 2 +- 1 file changed, 1 insertion(+), 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 e7feca51d2..da4e78d56f 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 @@ -137,7 +137,7 @@ class AndroidDeviceInfo extends BaseDeviceInfo { /// https://developer.android.com/reference/android/os/Build#getSerial() final String serialNumber; - /// `true` is it running on a low ram device `false` otherwise + /// `true` if the application is running on a low-RAM device, `false` otherwise. final bool isLowRamDevice; /// Deserializes from the message received from [_kChannel].