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:
parent
ef46035900
commit
e28beaae48
@ -20,7 +20,6 @@ package nodomain.freeyourgadget.gadgetbridge;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.app.Application;
|
||||
import android.app.NotificationChannel;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.NotificationManager.Policy;
|
||||
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.fromKey;
|
||||
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
|
||||
@ -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
|
||||
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 Prefs prefs;
|
||||
private static GBPrefs gbPrefs;
|
||||
@ -205,35 +202,19 @@ public class GBApplication extends Application {
|
||||
|
||||
if (isRunningMarshmallowOrLater()) {
|
||||
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
//the following will ensure the notification manager is kept alive
|
||||
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();
|
||||
registerReceiver(bluetoothStateChangeReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
|
||||
}
|
||||
try {
|
||||
//the following will ensure the notification manager is kept alive
|
||||
startService(new Intent(this, NotificationCollectorMonitorService.class));
|
||||
} catch (IllegalStateException e) {
|
||||
String message = e.toString();
|
||||
if (message == null) {
|
||||
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)
|
||||
.setSmallIcon(R.drawable.gadgetbridge_img)
|
||||
.setContentTitle(getString(R.string.error_background_service))
|
||||
@ -241,7 +222,7 @@ public class GBApplication extends Application {
|
||||
.setStyle(new NotificationCompat.BigTextStyle()
|
||||
.bigText(getString(R.string.error_background_service_reason) + "\"" + message + "\""))
|
||||
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
||||
.build());
|
||||
.build(), context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -20,7 +20,6 @@ package nodomain.freeyourgadget.gadgetbridge.activities;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.app.DatePickerDialog;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.appwidget.AppWidgetHost;
|
||||
import android.appwidget.AppWidgetManager;
|
||||
@ -474,8 +473,6 @@ public class DebugActivity extends AbstractGBActivity {
|
||||
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0,
|
||||
notificationIntent, 0);
|
||||
|
||||
NotificationManager nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
||||
|
||||
RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_REPLY)
|
||||
.build();
|
||||
|
||||
@ -499,9 +496,7 @@ public class DebugActivity extends AbstractGBActivity {
|
||||
.setContentIntent(pendingIntent)
|
||||
.extend(wearableExtender);
|
||||
|
||||
if (nManager != null) {
|
||||
nManager.notify((int) System.currentTimeMillis(), ncomp.build());
|
||||
}
|
||||
GB.notify((int) System.currentTimeMillis(), ncomp.build(), this);
|
||||
}
|
||||
|
||||
private void testPebbleKitNotification() {
|
||||
|
@ -17,7 +17,6 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
package nodomain.freeyourgadget.gadgetbridge.activities;
|
||||
|
||||
import android.app.NotificationManager;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
@ -86,8 +85,7 @@ public class FindPhoneActivity extends AbstractGBActivity {
|
||||
}
|
||||
});
|
||||
|
||||
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
notificationManager.cancel( GB.NOTIFICATION_ID_PHONE_FIND );
|
||||
GB.removeNotification(GB.NOTIFICATION_ID_PHONE_FIND, this);
|
||||
|
||||
vibrate();
|
||||
playRingtone();
|
||||
|
@ -19,7 +19,6 @@
|
||||
package nodomain.freeyourgadget.gadgetbridge.service;
|
||||
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
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);
|
||||
|
||||
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
|
||||
NotificationCompat.Builder notification = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_HIGH_PRIORITY_ID )
|
||||
.setSmallIcon(R.drawable.ic_notification)
|
||||
.setOngoing(false)
|
||||
@ -226,11 +223,11 @@ public abstract class AbstractDeviceSupport implements DeviceSupport {
|
||||
|
||||
CompanionDeviceManager manager = (CompanionDeviceManager) context.getSystemService(Context.COMPANION_DEVICE_SERVICE);
|
||||
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);
|
||||
LOG.debug("CompanionDeviceManager associations were found, starting intent");
|
||||
} 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");
|
||||
}
|
||||
}
|
||||
@ -343,8 +340,7 @@ public abstract class AbstractDeviceSupport implements DeviceSupport {
|
||||
.setAutoCancel(true)
|
||||
.build();
|
||||
|
||||
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
nm.notify(NOTIFICATION_ID_SCREENSHOT, notif);
|
||||
GB.notify(NOTIFICATION_ID_SCREENSHOT, notif, context);
|
||||
} catch (IOException ex) {
|
||||
LOG.error("Error writing screenshot", ex);
|
||||
}
|
||||
|
@ -22,7 +22,6 @@ package nodomain.freeyourgadget.gadgetbridge.service;
|
||||
|
||||
import android.Manifest;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.Service;
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.content.BroadcastReceiver;
|
||||
@ -653,6 +652,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
|
||||
|
||||
private void start() {
|
||||
if (!mStarted) {
|
||||
GB.createNotificationChannels(this);
|
||||
startForeground(GB.NOTIFICATION_ID, GB.createNotification(getString(R.string.gadgetbridge_running), this));
|
||||
mStarted = true;
|
||||
}
|
||||
@ -866,10 +866,7 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
|
||||
setReceiversEnableState(false, false, null); // disable BroadcastReceivers
|
||||
|
||||
setDeviceSupport(null);
|
||||
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
if (nm != null) {
|
||||
nm.cancel(GB.NOTIFICATION_ID); // need to do this because the updated notification won't be cancelled when service stops
|
||||
}
|
||||
GB.removeNotification(GB.NOTIFICATION_ID, this); // need to do this because the updated notification won't be cancelled when service stops
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -18,7 +18,6 @@
|
||||
package nodomain.freeyourgadget.gadgetbridge.service.devices.qhybrid;
|
||||
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.bluetooth.BluetoothGatt;
|
||||
import android.bluetooth.BluetoothGattCharacteristic;
|
||||
@ -709,7 +708,7 @@ public class QHybridSupport extends QHybridBaseSupport {
|
||||
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
|
||||
|
@ -31,7 +31,7 @@ import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.core.app.NotificationCompat;
|
||||
import androidx.core.app.NotificationManagerCompat;
|
||||
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_EXPORT_FAILED = 5;
|
||||
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);
|
||||
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_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) {
|
||||
Intent notificationIntent = new Intent(context, ControlCenterv2.class);
|
||||
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
|
||||
@ -167,24 +198,17 @@ public class GB {
|
||||
|
||||
public static void updateNotification(GBDevice device, Context 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) {
|
||||
if (notification == null) {
|
||||
return;
|
||||
}
|
||||
// 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);
|
||||
public static void notify(int id, @NonNull Notification notification, Context context) {
|
||||
createNotificationChannels(context);
|
||||
|
||||
NotificationManagerCompat.from(context).notify(id, notification);
|
||||
}
|
||||
|
||||
private 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 nm = NotificationManagerCompat.from(context);
|
||||
// NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
nm.cancel(id);
|
||||
public static void removeNotification(int id, Context context) {
|
||||
NotificationManagerCompat.from(context).cancel(id);
|
||||
}
|
||||
|
||||
public static boolean isBluetoothEnabled() {
|
||||
@ -366,16 +390,6 @@ public class GB {
|
||||
private static Notification createTransferNotification(String title, String text, boolean ongoing,
|
||||
int percentage, Context context) {
|
||||
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
|
||||
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
||||
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
|
||||
@ -401,18 +415,12 @@ public class GB {
|
||||
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) {
|
||||
if (percentage == 100) {
|
||||
removeNotification(NOTIFICATION_ID_TRANSFER, context);
|
||||
} else {
|
||||
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) {
|
||||
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) {
|
||||
@ -474,7 +482,7 @@ public class GB {
|
||||
return;
|
||||
}
|
||||
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) {
|
||||
@ -504,14 +512,9 @@ public class GB {
|
||||
return;
|
||||
}
|
||||
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) {
|
||||
if (!condition) {
|
||||
throw new AssertionError(errorMessage);
|
||||
|
Loading…
Reference in New Issue
Block a user