Skip to content

Commit

Permalink
fix(device_info_plus)!: Remove Display Metrics from Android Device In…
Browse files Browse the repository at this point in the history
…fo (#2731)
  • Loading branch information
miquelbeltran committed Mar 19, 2024
1 parent 21ccfa4 commit c5af332
Show file tree
Hide file tree
Showing 7 changed files with 4 additions and 137 deletions.
Expand Up @@ -4,52 +4,26 @@ 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 handler = MethodCallHandlerImpl(packageManager, windowManager)
val handler = MethodCallHandlerImpl(packageManager)
methodChannel.setMethodCallHandler(handler)
}

}
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<String, Any> = 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()
Expand Down
Expand Up @@ -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);
Expand Down
Expand Up @@ -101,14 +101,6 @@ class _MyAppState extends State<MyApp> {
'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,
};
}
Expand Down
Expand Up @@ -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`.
Expand Down Expand Up @@ -31,7 +30,6 @@ class AndroidDeviceInfo extends BaseDeviceInfo {
required this.type,
required this.isPhysicalDevice,
required List<String> systemFeatures,
required this.displayMetrics,
required this.serialNumber,
}) : supported32BitAbis = List<String>.unmodifiable(supported32BitAbis),
supported64BitAbis = List<String>.unmodifiable(supported64BitAbis),
Expand Down Expand Up @@ -132,9 +130,6 @@ class AndroidDeviceInfo extends BaseDeviceInfo {
/// https://developer.android.com/reference/android/content/pm/PackageManager
final List<String> 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:
Expand Down Expand Up @@ -166,8 +161,6 @@ class AndroidDeviceInfo extends BaseDeviceInfo {
type: map['type'],
isPhysicalDevice: map['isPhysicalDevice'],
systemFeatures: _fromList(map['systemFeatures'] ?? []),
displayMetrics: AndroidDisplayMetrics._fromMap(
map['displayMetrics']?.cast<String, dynamic>() ?? {}),
serialNumber: map['serialNumber'],
);
}
Expand Down Expand Up @@ -247,62 +240,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<String, dynamic> toMap() {
return {
'widthPx': widthPx,
'heightPx': heightPx,
'xDpi': xDpi,
'yDpi': yDpi,
};
}

/// Deserializes from the map message received from the [MethodChannel].
static AndroidDisplayMetrics _fromMap(Map<String, dynamic> map) {
return AndroidDisplayMetrics._(
widthPx: map['widthPx'],
heightPx: map['heightPx'],
xDpi: map['xDpi'],
yDpi: map['yDpi'],
);
}
}
Expand Up @@ -10,13 +10,6 @@ const _fakeAndroidBuildVersion = <String, dynamic>{
'securityPatch': 'securityPatch',
};

const _fakeDisplayMetrics = <String, dynamic>{
'widthPx': 1080.0,
'heightPx': 2220.0,
'xDpi': 530.0859,
'yDpi': 529.4639,
};

const _fakeSupportedAbis = <String>['arm64-v8a', 'x86', 'x86_64'];
const _fakeSupported32BitAbis = <String?>['x86 (IA-32)', 'MMX'];
const _fakeSupported64BitAbis = <String?>['x86-64', 'MMX', 'SSSE3'];
Expand All @@ -43,6 +36,5 @@ const _fakeAndroidDeviceInfo = <String, dynamic>{
'version': _fakeAndroidBuildVersion,
'supported64BitAbis': _fakeSupported64BitAbis,
'supported32BitAbis': _fakeSupported32BitAbis,
'displayMetrics': _fakeDisplayMetrics,
'serialNumber': 'SERIAL',
};
Expand Up @@ -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');
});

Expand Down

0 comments on commit c5af332

Please sign in to comment.