1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-26 18:45:49 +01:00

Refactoring: localize notification handling in GB

* Handle all notifications via GB.
* Create all notification channels in one place.
* Lazily init notification channels.
* Re-create notification channels to update their localized names.
This commit is contained in:
Dmitriy Bogdanov 2021-05-14 20:30:54 +04:00 committed by Gitea
parent ef46035900
commit e28beaae48
7 changed files with 55 additions and 86 deletions

View File

@ -20,7 +20,6 @@ package nodomain.freeyourgadget.gadgetbridge;
import android.annotation.TargetApi; import android.annotation.TargetApi;
import android.app.Application; import android.app.Application;
import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.NotificationManager.Policy; import android.app.NotificationManager.Policy;
import android.app.UiModeManager; import android.app.UiModeManager;
@ -90,7 +89,7 @@ import static nodomain.freeyourgadget.gadgetbridge.model.DeviceType.MIBAND3;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceType.PEBBLE; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceType.PEBBLE;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceType.fromKey; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceType.fromKey;
import static nodomain.freeyourgadget.gadgetbridge.util.GB.NOTIFICATION_CHANNEL_HIGH_PRIORITY_ID; import static nodomain.freeyourgadget.gadgetbridge.util.GB.NOTIFICATION_CHANNEL_HIGH_PRIORITY_ID;
import static nodomain.freeyourgadget.gadgetbridge.util.GB.NOTIFICATION_CHANNEL_ID; import static nodomain.freeyourgadget.gadgetbridge.util.GB.NOTIFICATION_ID_ERROR;
/** /**
* Main Application class that initializes and provides access to certain things like * Main Application class that initializes and provides access to certain things like
@ -109,8 +108,6 @@ public class GBApplication extends Application {
//if preferences have to be migrated, increment the following and add the migration logic in migratePrefs below; see http://stackoverflow.com/questions/16397848/how-can-i-migrate-android-preferences-with-a-new-version //if preferences have to be migrated, increment the following and add the migration logic in migratePrefs below; see http://stackoverflow.com/questions/16397848/how-can-i-migrate-android-preferences-with-a-new-version
private static final int CURRENT_PREFS_VERSION = 9; private static final int CURRENT_PREFS_VERSION = 9;
private static final int ERROR_IN_GADGETBRIDGE_NOTIFICATION = 42;
private static LimitedQueue mIDSenderLookup = new LimitedQueue(16); private static LimitedQueue mIDSenderLookup = new LimitedQueue(16);
private static Prefs prefs; private static Prefs prefs;
private static GBPrefs gbPrefs; private static GBPrefs gbPrefs;
@ -205,35 +202,19 @@ public class GBApplication extends Application {
if (isRunningMarshmallowOrLater()) { if (isRunningMarshmallowOrLater()) {
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//the following will ensure the notification manager is kept alive
if (isRunningOreoOrLater()) { if (isRunningOreoOrLater()) {
NotificationChannel channel = notificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID);
if (channel == null) {
channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,
getString(R.string.notification_channel_name),
NotificationManager.IMPORTANCE_LOW);
notificationManager.createNotificationChannel(channel);
}
NotificationChannel channelHighPr = notificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_HIGH_PRIORITY_ID);
if (channelHighPr == null) {
channelHighPr = new NotificationChannel(NOTIFICATION_CHANNEL_HIGH_PRIORITY_ID,
getString(R.string.notification_channel_high_priority_name),
NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channelHighPr);
}
bluetoothStateChangeReceiver = new BluetoothStateChangeReceiver(); bluetoothStateChangeReceiver = new BluetoothStateChangeReceiver();
registerReceiver(bluetoothStateChangeReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)); registerReceiver(bluetoothStateChangeReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
} }
try { try {
//the following will ensure the notification manager is kept alive
startService(new Intent(this, NotificationCollectorMonitorService.class)); startService(new Intent(this, NotificationCollectorMonitorService.class));
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
String message = e.toString(); String message = e.toString();
if (message == null) { if (message == null) {
message = getString(R.string._unknown_); message = getString(R.string._unknown_);
} }
notificationManager.notify(ERROR_IN_GADGETBRIDGE_NOTIFICATION, GB.notify(NOTIFICATION_ID_ERROR,
new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_HIGH_PRIORITY_ID) new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_HIGH_PRIORITY_ID)
.setSmallIcon(R.drawable.gadgetbridge_img) .setSmallIcon(R.drawable.gadgetbridge_img)
.setContentTitle(getString(R.string.error_background_service)) .setContentTitle(getString(R.string.error_background_service))
@ -241,7 +222,7 @@ public class GBApplication extends Application {
.setStyle(new NotificationCompat.BigTextStyle() .setStyle(new NotificationCompat.BigTextStyle()
.bigText(getString(R.string.error_background_service_reason) + "\"" + message + "\"")) .bigText(getString(R.string.error_background_service_reason) + "\"" + message + "\""))
.setPriority(NotificationCompat.PRIORITY_DEFAULT) .setPriority(NotificationCompat.PRIORITY_DEFAULT)
.build()); .build(), context);
} }
} }
} }

View File

@ -20,7 +20,6 @@ package nodomain.freeyourgadget.gadgetbridge.activities;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.DatePickerDialog; import android.app.DatePickerDialog;
import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.appwidget.AppWidgetHost; import android.appwidget.AppWidgetHost;
import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetManager;
@ -474,8 +473,6 @@ public class DebugActivity extends AbstractGBActivity {
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0,
notificationIntent, 0); notificationIntent, 0);
NotificationManager nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_REPLY) RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_REPLY)
.build(); .build();
@ -499,9 +496,7 @@ public class DebugActivity extends AbstractGBActivity {
.setContentIntent(pendingIntent) .setContentIntent(pendingIntent)
.extend(wearableExtender); .extend(wearableExtender);
if (nManager != null) { GB.notify((int) System.currentTimeMillis(), ncomp.build(), this);
nManager.notify((int) System.currentTimeMillis(), ncomp.build());
}
} }
private void testPebbleKitNotification() { private void testPebbleKitNotification() {

View File

@ -17,7 +17,6 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. */ along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.activities; package nodomain.freeyourgadget.gadgetbridge.activities;
import android.app.NotificationManager;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
@ -86,8 +85,7 @@ public class FindPhoneActivity extends AbstractGBActivity {
} }
}); });
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); GB.removeNotification(GB.NOTIFICATION_ID_PHONE_FIND, this);
notificationManager.cancel( GB.NOTIFICATION_ID_PHONE_FIND );
vibrate(); vibrate();
playRingtone(); playRingtone();

View File

@ -19,7 +19,6 @@
package nodomain.freeyourgadget.gadgetbridge.service; package nodomain.freeyourgadget.gadgetbridge.service;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.companion.CompanionDeviceManager; import android.companion.CompanionDeviceManager;
@ -212,8 +211,6 @@ public abstract class AbstractDeviceSupport implements DeviceSupport {
PendingIntent pi = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent pi = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notification = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_HIGH_PRIORITY_ID ) NotificationCompat.Builder notification = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_HIGH_PRIORITY_ID )
.setSmallIcon(R.drawable.ic_notification) .setSmallIcon(R.drawable.ic_notification)
.setOngoing(false) .setOngoing(false)
@ -226,11 +223,11 @@ public abstract class AbstractDeviceSupport implements DeviceSupport {
CompanionDeviceManager manager = (CompanionDeviceManager) context.getSystemService(Context.COMPANION_DEVICE_SERVICE); CompanionDeviceManager manager = (CompanionDeviceManager) context.getSystemService(Context.COMPANION_DEVICE_SERVICE);
if (manager.getAssociations().size() > 0) { if (manager.getAssociations().size() > 0) {
notificationManager.notify(GB.NOTIFICATION_ID_PHONE_FIND, notification.build()); GB.notify(GB.NOTIFICATION_ID_PHONE_FIND, notification.build(), context);
context.startActivity(intent); context.startActivity(intent);
LOG.debug("CompanionDeviceManager associations were found, starting intent"); LOG.debug("CompanionDeviceManager associations were found, starting intent");
} else { } else {
notificationManager.notify(GB.NOTIFICATION_ID_PHONE_FIND, notification.build()); GB.notify(GB.NOTIFICATION_ID_PHONE_FIND, notification.build(), context);
LOG.warn("CompanionDeviceManager associations were not found, can't start intent"); LOG.warn("CompanionDeviceManager associations were not found, can't start intent");
} }
} }
@ -343,8 +340,7 @@ public abstract class AbstractDeviceSupport implements DeviceSupport {
.setAutoCancel(true) .setAutoCancel(true)
.build(); .build();
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); GB.notify(NOTIFICATION_ID_SCREENSHOT, notif, context);
nm.notify(NOTIFICATION_ID_SCREENSHOT, notif);
} catch (IOException ex) { } catch (IOException ex) {
LOG.error("Error writing screenshot", ex); LOG.error("Error writing screenshot", ex);
} }

View File

@ -22,7 +22,6 @@ package nodomain.freeyourgadget.gadgetbridge.service;
import android.Manifest; import android.Manifest;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.NotificationManager;
import android.app.Service; import android.app.Service;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
@ -653,6 +652,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
private void start() { private void start() {
if (!mStarted) { if (!mStarted) {
GB.createNotificationChannels(this);
startForeground(GB.NOTIFICATION_ID, GB.createNotification(getString(R.string.gadgetbridge_running), this)); startForeground(GB.NOTIFICATION_ID, GB.createNotification(getString(R.string.gadgetbridge_running), this));
mStarted = true; mStarted = true;
} }
@ -866,10 +866,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
setReceiversEnableState(false, false, null); // disable BroadcastReceivers setReceiversEnableState(false, false, null); // disable BroadcastReceivers
setDeviceSupport(null); setDeviceSupport(null);
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); GB.removeNotification(GB.NOTIFICATION_ID, this); // need to do this because the updated notification won't be cancelled when service stops
if (nm != null) {
nm.cancel(GB.NOTIFICATION_ID); // need to do this because the updated notification won't be cancelled when service stops
}
} }
@Override @Override

View File

@ -18,7 +18,6 @@
package nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid; package nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic; import android.bluetooth.BluetoothGattCharacteristic;
@ -709,7 +708,7 @@ public class QHybridSupport extends QHybridBaseSupport {
notificationBuilder.addAction(0, "report", intent); notificationBuilder.addAction(0, "report", intent);
} }
((NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE)).notify((int) System.currentTimeMillis(), notificationBuilder.build()); GB.notify((int) System.currentTimeMillis(), notificationBuilder.build(), getContext());
} }
@Override @Override

View File

@ -31,7 +31,7 @@ import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.Nullable; import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat; import androidx.core.app.NotificationManagerCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.localbroadcastmanager.content.LocalBroadcastManager;
@ -71,6 +71,7 @@ public class GB {
public static final int NOTIFICATION_ID_TRANSFER = 4; public static final int NOTIFICATION_ID_TRANSFER = 4;
public static final int NOTIFICATION_ID_EXPORT_FAILED = 5; public static final int NOTIFICATION_ID_EXPORT_FAILED = 5;
public static final int NOTIFICATION_ID_PHONE_FIND = 6; public static final int NOTIFICATION_ID_PHONE_FIND = 6;
public static final int NOTIFICATION_ID_ERROR = 42;
private static final Logger LOG = LoggerFactory.getLogger(GB.class); private static final Logger LOG = LoggerFactory.getLogger(GB.class);
public static final int INFO = 1; public static final int INFO = 1;
@ -89,6 +90,36 @@ public class GB {
public static final String ACTION_SET_PROGRESS_TEXT = "GB_Set_Progress_Text"; public static final String ACTION_SET_PROGRESS_TEXT = "GB_Set_Progress_Text";
public static final String ACTION_SET_INFO_TEXT = "GB_Set_Info_Text"; public static final String ACTION_SET_INFO_TEXT = "GB_Set_Info_Text";
private static boolean notificationChannelsCreated;
public static void createNotificationChannels(Context context) {
if (notificationChannelsCreated) return;
if (isRunningOreoOrLater()) {
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
NotificationChannel channelGeneral = new NotificationChannel(
NOTIFICATION_CHANNEL_ID,
context.getString(R.string.notification_channel_name),
NotificationManager.IMPORTANCE_LOW);
notificationManager.createNotificationChannel(channelGeneral);
NotificationChannel channelHighPriority = new NotificationChannel(
NOTIFICATION_CHANNEL_HIGH_PRIORITY_ID,
context.getString(R.string.notification_channel_high_priority_name),
NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channelHighPriority);
NotificationChannel channelTransfer = new NotificationChannel(
NOTIFICATION_CHANNEL_ID_TRANSFER,
context.getString(R.string.notification_channel_name),
NotificationManager.IMPORTANCE_LOW);
notificationManager.createNotificationChannel(channelTransfer);
}
notificationChannelsCreated = true;
}
private static PendingIntent getContentIntent(Context context) { private static PendingIntent getContentIntent(Context context) {
Intent notificationIntent = new Intent(context, ControlCenterv2.class); Intent notificationIntent = new Intent(context, ControlCenterv2.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
@ -167,24 +198,17 @@ public class GB {
public static void updateNotification(GBDevice device, Context context) { public static void updateNotification(GBDevice device, Context context) {
Notification notification = createNotification(device, context); Notification notification = createNotification(device, context);
updateNotification(notification, NOTIFICATION_ID, context); notify(NOTIFICATION_ID, notification, context);
} }
private static void updateNotification(@Nullable Notification notification, int id, Context context) { public static void notify(int id, @NonNull Notification notification, Context context) {
if (notification == null) { createNotificationChannels(context);
return;
} NotificationManagerCompat.from(context).notify(id, notification);
// TODO: I believe it's better do always use the NMC instead of the old call, but old code works
NotificationManagerCompat nm = NotificationManagerCompat.from(context);
// NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(id, notification);
} }
private static void removeNotification(int id, Context context) { public static void removeNotification(int id, Context context) {
// TODO: I believe it's better do always use the NMC instead of the old call, but old code works NotificationManagerCompat.from(context).cancel(id);
NotificationManagerCompat nm = NotificationManagerCompat.from(context);
// NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel(id);
} }
public static boolean isBluetoothEnabled() { public static boolean isBluetoothEnabled() {
@ -366,16 +390,6 @@ public class GB {
private static Notification createTransferNotification(String title, String text, boolean ongoing, private static Notification createTransferNotification(String title, String text, boolean ongoing,
int percentage, Context context) { int percentage, Context context) {
Intent notificationIntent = new Intent(context, ControlCenterv2.class); Intent notificationIntent = new Intent(context, ControlCenterv2.class);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if(isRunningOreoOrLater()) {
NotificationChannel channel = notificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID_TRANSFER);
if(channel == null) {
channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID_TRANSFER,
context.getString(R.string.notification_channel_name),
NotificationManager.IMPORTANCE_LOW);
notificationManager.createNotificationChannel(channel);
}
}
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK); | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
@ -401,18 +415,12 @@ public class GB {
return nb.build(); return nb.build();
} }
public static void removeAllNotifications(Context context) {
removeNotification(NOTIFICATION_ID_TRANSFER, context);
removeNotification(NOTIFICATION_ID_INSTALL, context);
removeNotification(NOTIFICATION_ID_LOW_BATTERY, context);
}
public static void updateTransferNotification(String title, String text, boolean ongoing, int percentage, Context context) { public static void updateTransferNotification(String title, String text, boolean ongoing, int percentage, Context context) {
if (percentage == 100) { if (percentage == 100) {
removeNotification(NOTIFICATION_ID_TRANSFER, context); removeNotification(NOTIFICATION_ID_TRANSFER, context);
} else { } else {
Notification notification = createTransferNotification(title, text, ongoing, percentage, context); Notification notification = createTransferNotification(title, text, ongoing, percentage, context);
updateNotification(notification, NOTIFICATION_ID_TRANSFER, context); notify(NOTIFICATION_ID_TRANSFER, notification, context);
} }
} }
@ -444,7 +452,7 @@ public class GB {
public static void updateInstallNotification(String text, boolean ongoing, int percentage, Context context) { public static void updateInstallNotification(String text, boolean ongoing, int percentage, Context context) {
Notification notification = createInstallNotification(text, ongoing, percentage, context); Notification notification = createInstallNotification(text, ongoing, percentage, context);
updateNotification(notification, NOTIFICATION_ID_INSTALL, context); notify(NOTIFICATION_ID_INSTALL, notification, context);
} }
private static Notification createBatteryNotification(String text, String bigText, Context context) { private static Notification createBatteryNotification(String text, String bigText, Context context) {
@ -474,7 +482,7 @@ public class GB {
return; return;
} }
Notification notification = createBatteryNotification(text, bigText, context); Notification notification = createBatteryNotification(text, bigText, context);
updateNotification(notification, NOTIFICATION_ID_LOW_BATTERY, context); notify(NOTIFICATION_ID_LOW_BATTERY, notification, context);
} }
public static void removeBatteryNotification(Context context) { public static void removeBatteryNotification(Context context) {
@ -504,14 +512,9 @@ public class GB {
return; return;
} }
Notification notification = createExportFailedNotification(text, context); Notification notification = createExportFailedNotification(text, context);
updateNotification(notification, NOTIFICATION_ID_EXPORT_FAILED, context); notify(NOTIFICATION_ID_EXPORT_FAILED, notification, context);
} }
public static void removeExportFailedNotification(Context context) {
removeNotification(NOTIFICATION_ID_EXPORT_FAILED, context);
}
public static void assertThat(boolean condition, String errorMessage) { public static void assertThat(boolean condition, String errorMessage) {
if (!condition) { if (!condition) {
throw new AssertionError(errorMessage); throw new AssertionError(errorMessage);