From 38868e00782d1b81714f59c4edde707eb9c5827b Mon Sep 17 00:00:00 2001 From: edde746 <86283021+edde746@users.noreply.github.com> Date: Sun, 7 Jun 2026 10:26:30 +0200 Subject: [PATCH] fix: use device name for companion remote close #1256 --- .../main/kotlin/com/edde746/plezy/MainActivity.kt | 13 +++++++++++++ lib/providers/companion_remote_provider.dart | 8 +++++--- lib/utils/platform_detector.dart | 14 ++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/android/app/src/main/kotlin/com/edde746/plezy/MainActivity.kt b/android/app/src/main/kotlin/com/edde746/plezy/MainActivity.kt index ead5d482..6957ad8c 100644 --- a/android/app/src/main/kotlin/com/edde746/plezy/MainActivity.kt +++ b/android/app/src/main/kotlin/com/edde746/plezy/MainActivity.kt @@ -14,6 +14,7 @@ import android.os.Build import android.os.Bundle import android.os.Handler import android.os.Looper +import android.provider.Settings import android.util.Log import android.util.Rational import android.view.InputDevice @@ -189,6 +190,17 @@ class MainActivity : FlutterActivity() { ) } + /** User-assigned device name (Settings > About > Device name), or null. */ + private fun getDeviceName(): String? { + // The name the user gave the device; also used by Cast/Nearby. + val name = Settings.Global.getString(contentResolver, Settings.Global.DEVICE_NAME) + if (!name.isNullOrBlank()) return name + // Fallback: the Bluetooth name usually mirrors the device name. Reading the + // settings string needs no BLUETOOTH permission (unlike BluetoothAdapter). + val bt = Settings.Secure.getString(contentResolver, "bluetooth_name") + return if (!bt.isNullOrBlank()) bt else null + } + override fun onCreate(savedInstanceState: Bundle?) { // Apply persisted theme color to the window background before anything // else renders. This prevents a white flash between the native splash @@ -427,6 +439,7 @@ class MainActivity : FlutterActivity() { MethodChannel(flutterEngine.dartExecutor.binaryMessenger, DEVICE_CHANNEL).setMethodCallHandler { call, result -> when (call.method) { "getTvDetection" -> result.success(getAndroidTvDetection()) + "getDeviceName" -> result.success(getDeviceName()) else -> result.notImplemented() } } diff --git a/lib/providers/companion_remote_provider.dart b/lib/providers/companion_remote_provider.dart index e061fcdf..a8b8e679 100644 --- a/lib/providers/companion_remote_provider.dart +++ b/lib/providers/companion_remote_provider.dart @@ -19,6 +19,7 @@ import '../services/companion_remote/lan_discovery_service.dart'; import '../services/companion_remote/remote_auth_context.dart'; import '../services/companion_remote/remote_auth_service.dart'; import '../utils/app_logger.dart'; +import '../utils/platform_detector.dart'; import '../mixins/disposable_change_notifier_mixin.dart'; export '../services/companion_remote/lan_discovery_service.dart' show DiscoveredHost; @@ -79,7 +80,8 @@ class CompanionRemoteProvider with ChangeNotifier, DisposableChangeNotifierMixin try { if (Platform.isAndroid) { final androidInfo = await deviceInfo.androidInfo; - _deviceName = '${androidInfo.brand} ${androidInfo.model}'; + final osName = await TvDetectionService.getAndroidDeviceName(); + _deviceName = osName ?? '${androidInfo.brand} ${androidInfo.model}'; _platform = 'Android'; } else if (Platform.isIOS) { final iosInfo = await deviceInfo.iosInfo; @@ -94,8 +96,8 @@ class CompanionRemoteProvider with ChangeNotifier, DisposableChangeNotifierMixin _deviceName = windowsInfo.computerName; _platform = 'Windows'; } else if (Platform.isLinux) { - final linuxInfo = await deviceInfo.linuxInfo; - _deviceName = linuxInfo.name; + final host = Platform.localHostname.trim(); + _deviceName = (host.isNotEmpty && host != 'localhost') ? host : (await deviceInfo.linuxInfo).name; _platform = 'Linux'; } } catch (e) { diff --git a/lib/utils/platform_detector.dart b/lib/utils/platform_detector.dart index 2d068908..7d63955e 100644 --- a/lib/utils/platform_detector.dart +++ b/lib/utils/platform_detector.dart @@ -116,6 +116,20 @@ class TvDetectionService { } } + /// User-assigned Android device name (Settings > About > Device name), or + /// null if unavailable. Android only. + static Future getAndroidDeviceName() async { + if (!Platform.isAndroid) return null; + try { + final name = (await _deviceChannel.invokeMethod('getDeviceName'))?.trim(); + return (name == null || name.isEmpty) ? null : name; + } on MissingPluginException { + return null; + } on PlatformException { + return null; + } + } + /// Update the user force-TV override and recompute the effective flag. void setForceTv(bool value) { _forceTv = value;