Skip to content

Commit

Permalink
feat(device_info_plus): Add User Device Name in Android (PR #3437) (#…
Browse files Browse the repository at this point in the history
…3456)

Co-authored-by: Miguel Beltran <[email protected]>
  • Loading branch information
FaizanUllahDev and miquelbeltran authored Feb 4, 2025
1 parent 438ae56 commit 8c38a31
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package dev.fluttercommunity.plus.device_info
import android.app.ActivityManager
import android.content.Context
import android.content.pm.PackageManager
import android.view.WindowManager
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.MethodChannel
Expand All @@ -24,8 +23,10 @@ class DeviceInfoPlusPlugin : FlutterPlugin {
private fun setupMethodChannel(messenger: BinaryMessenger, context: Context) {
methodChannel = MethodChannel(messenger, "dev.fluttercommunity.plus/device_info")
val packageManager: PackageManager = context.packageManager
val activityManager: ActivityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
val handler = MethodCallHandlerImpl(packageManager, activityManager)
val activityManager: ActivityManager =
context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
val contentResolver = context.contentResolver
val handler = MethodCallHandlerImpl(packageManager, activityManager, contentResolver)
methodChannel.setMethodCallHandler(handler)
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package dev.fluttercommunity.plus.device_info

import android.app.ActivityManager
import android.content.ContentResolver
import android.content.pm.FeatureInfo
import android.content.pm.PackageManager
import android.os.Build
import android.util.DisplayMetrics
import android.view.Display
import android.view.WindowManager
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import kotlin.collections.HashMap
import android.provider.Settings

/**
* The implementation of [MethodChannel.MethodCallHandler] for the plugin. Responsible for
Expand All @@ -19,6 +18,7 @@ import kotlin.collections.HashMap
internal class MethodCallHandlerImpl(
private val packageManager: PackageManager,
private val activityManager: ActivityManager,
private val contentResolver: ContentResolver,
) : MethodCallHandler {

override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
Expand All @@ -38,6 +38,10 @@ internal class MethodCallHandlerImpl(
build["model"] = Build.MODEL
build["product"] = Build.PRODUCT

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
build["name"] = Settings.Global.getString(contentResolver, Settings.Global.DEVICE_NAME)
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
build["supported32BitAbis"] = listOf(*Build.SUPPORTED_32_BIT_ABIS)
build["supported64BitAbis"] = listOf(*Build.SUPPORTED_64_BIT_ABIS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ void main() {
expect(androidInfo.manufacturer, isNotNull);
expect(androidInfo.model, isNotNull);
expect(androidInfo.product, isNotNull);
expect(androidInfo.name, isNotNull);

expect(androidInfo.supported32BitAbis, isNotNull);
expect(androidInfo.supported64BitAbis, isNotNull);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class _MyAppState extends State<MyApp> {
'manufacturer': build.manufacturer,
'model': build.model,
'product': build.product,
'name': build.name,
'supported32BitAbis': build.supported32BitAbis,
'supported64BitAbis': build.supported64BitAbis,
'supportedAbis': build.supportedAbis,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class AndroidDeviceInfo extends BaseDeviceInfo {
required this.manufacturer,
required this.model,
required this.product,
required this.name,
required List<String> supported32BitAbis,
required List<String> supported64BitAbis,
required List<String> supportedAbis,
Expand Down Expand Up @@ -89,6 +90,10 @@ class AndroidDeviceInfo extends BaseDeviceInfo {
/// https://developer.android.com/reference/android/os/Build#PRODUCT
final String product;

/// The name of the device.
/// https://developer.android.com/reference/android/provider/Settings.Global#DEVICE_NAME
final String name;

/// An ordered list of 32 bit ABIs supported by this device.
/// Available only on Android L (API 21) and newer
/// https://developer.android.com/reference/android/os/Build#SUPPORTED_32_BIT_ABIS
Expand Down Expand Up @@ -158,6 +163,7 @@ class AndroidDeviceInfo extends BaseDeviceInfo {
manufacturer: map['manufacturer'],
model: map['model'],
product: map['product'],
name: map['name'] ?? '',
supported32BitAbis: _fromList(map['supported32BitAbis'] ?? <String>[]),
supported64BitAbis: _fromList(map['supported64BitAbis'] ?? <String>[]),
supportedAbis: _fromList(map['supportedAbis'] ?? []),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const _fakeAndroidDeviceInfo = <String, dynamic>{
'brand': 'Google',
'device': 'device',
'product': 'product',
"name": "Custom Device Name",
'display': 'display',
'hardware': 'hardware',
'isPhysicalDevice': true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ void main() {
expect(androidDeviceInfo.brand, 'Google');
expect(androidDeviceInfo.device, 'device');
expect(androidDeviceInfo.product, 'product');
expect(androidDeviceInfo.name, "Custom Device Name");
expect(androidDeviceInfo.display, 'display');
expect(androidDeviceInfo.hardware, 'hardware');
expect(androidDeviceInfo.bootloader, 'bootloader');
Expand Down

0 comments on commit 8c38a31

Please sign in to comment.