From 60d5a2ae706ad629b7fa0fb5f9bc576f0e6b82ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Rebelo?= Date: Sun, 4 Aug 2024 14:10:11 +0100 Subject: [PATCH] Display warning on find phone without companion pairing --- .../service/AbstractDeviceSupport.java | 45 ++++++++++++------- app/src/main/res/values/strings.xml | 1 + 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractDeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractDeviceSupport.java index dbc9fa8a3..a81eb5e52 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractDeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractDeviceSupport.java @@ -292,32 +292,45 @@ public abstract class AbstractDeviceSupport implements DeviceSupport { @RequiresApi(Build.VERSION_CODES.Q) private void handleGBDeviceEventFindPhoneStartNotification(final boolean ring) { LOG.info("Got handleGBDeviceEventFindPhoneStartNotification"); - Intent intent = new Intent(context, FindPhoneActivity.class); + final CompanionDeviceManager manager = (CompanionDeviceManager) context.getSystemService(Context.COMPANION_DEVICE_SERVICE); + if (manager.getAssociations().isEmpty()) { + // On Android Q and above, we need the device to be paired as companion. If it is not, display a notification + // notifying the user and linking to further instructions + final Intent instructionsIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://gadgetbridge.org/basics/pairing/companion-device/")); + final PendingIntent pi = PendingIntentUtils.getActivity(context, 0, instructionsIntent, PendingIntent.FLAG_ONE_SHOT, false); + final NotificationCompat.Builder notification = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_HIGH_PRIORITY_ID) + .setSmallIcon(R.drawable.ic_warning) + .setOngoing(false) + .setPriority(NotificationCompat.PRIORITY_HIGH) + .setContentIntent(pi) + .setAutoCancel(true) + .setContentTitle(context.getString(R.string.find_my_phone_notification)) + .setContentText(context.getString(R.string.find_my_phone_companion_warning)); + + GB.notify(GB.NOTIFICATION_ID_PHONE_FIND, notification.build(), context); + + return; + } + + final Intent intent = new Intent(context, FindPhoneActivity.class); intent.setPackage(BuildConfig.APPLICATION_ID); intent.putExtra(FindPhoneActivity.EXTRA_RING, ring); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - PendingIntent pi = PendingIntentUtils.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT, false); + final PendingIntent pi = PendingIntentUtils.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT, false); - NotificationCompat.Builder notification = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_HIGH_PRIORITY_ID ) + final NotificationCompat.Builder notification = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_HIGH_PRIORITY_ID) .setSmallIcon(R.drawable.ic_notification) .setOngoing(false) .setFullScreenIntent(pi, true) .setPriority(NotificationCompat.PRIORITY_HIGH) .setAutoCancel(true) - .setContentTitle( context.getString( R.string.find_my_phone_notification ) ); + .setGroup("BackgroundService") + .setContentTitle(context.getString(R.string.find_my_phone_notification)); - notification.setGroup("BackgroundService"); - - CompanionDeviceManager manager = (CompanionDeviceManager) context.getSystemService(Context.COMPANION_DEVICE_SERVICE); - if (manager.getAssociations().size() > 0) { - GB.notify(GB.NOTIFICATION_ID_PHONE_FIND, notification.build(), context); - context.startActivity(intent); - LOG.debug("CompanionDeviceManager associations were found, starting intent"); - } else { - GB.notify(GB.NOTIFICATION_ID_PHONE_FIND, notification.build(), context); - LOG.warn("CompanionDeviceManager associations were not found, can't start intent"); - } + GB.notify(GB.NOTIFICATION_ID_PHONE_FIND, notification.build(), context); + context.startActivity(intent); + LOG.debug("CompanionDeviceManager associations were found, starting intent"); } @@ -1218,7 +1231,7 @@ public abstract class AbstractDeviceSupport implements DeviceSupport { @Override public void onSleepAsAndroidAction(String action, Bundle extras) { - + } @Override diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1a730fce7..6c88769a8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -598,6 +598,7 @@ Test notification This is a test notification from Gadgetbridge Find my phone + Companion pairing is required for find phone. Click here for more information. Bluetooth is not supported. Bluetooth is disabled. Tap connected device for App manager