Simplify account notification management

pull/156/head
M66B 6 years ago
parent b0655f0668
commit 42f9193179

@ -770,7 +770,7 @@ public class ActivitySetup extends ActivityBilling implements FragmentManager.On
account.id = db.account().insertAccount(account); account.id = db.account().insertAccount(account);
Log.i("Imported account=" + account.name); Log.i("Imported account=" + account.name);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) account.deleteNotificationChannel(context);
if (account.notify) if (account.notify)
account.createNotificationChannel(context); account.createNotificationChannel(context);

@ -23,10 +23,8 @@ import android.app.Notification;
import android.app.NotificationChannel; import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.content.Context; import android.content.Context;
import android.os.Build;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.room.Entity; import androidx.room.Entity;
import androidx.room.PrimaryKey; import androidx.room.PrimaryKey;
@ -105,8 +103,8 @@ public class EntityAccount extends EntityOrder implements Serializable {
return "notification" + (account == 0 ? "" : "." + account); return "notification" + (account == 0 ? "" : "." + account);
} }
@RequiresApi(api = Build.VERSION_CODES.O)
void createNotificationChannel(Context context) { void createNotificationChannel(Context context) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel( NotificationChannel channel = new NotificationChannel(
getNotificationChannelId(id), name, getNotificationChannelId(id), name,
@ -114,12 +112,14 @@ public class EntityAccount extends EntityOrder implements Serializable {
channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
nm.createNotificationChannel(channel); nm.createNotificationChannel(channel);
} }
}
@RequiresApi(api = Build.VERSION_CODES.O)
void deleteNotificationChannel(Context context) { void deleteNotificationChannel(Context context) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.deleteNotificationChannel(getNotificationChannelId(id)); nm.deleteNotificationChannel(getNotificationChannelId(id));
} }
}
@Override @Override
Long getSortId() { Long getSortId() {

@ -965,12 +965,9 @@ public class FragmentAccount extends FragmentBase {
EntityLog.log(context, (update ? "Updated" : "Added") + " account=" + account.name); EntityLog.log(context, (update ? "Updated" : "Added") + " account=" + account.name);
// Make sure the channel exists on commit // Make sure the channel exists on commit
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) if (account.notify)
if (account.notify) {
// Add or update notification channel
account.deleteNotificationChannel(context);
account.createNotificationChannel(context); account.createNotificationChannel(context);
} else if (!account.synchronize) else if (!account.synchronize)
account.deleteNotificationChannel(context); account.deleteNotificationChannel(context);
List<EntityFolder> folders = new ArrayList<>(); List<EntityFolder> folders = new ArrayList<>();

@ -447,7 +447,6 @@ public class ServiceSynchronize extends LifecycleService {
// Start monitoring accounts // Start monitoring accounts
List<EntityAccount> accounts = db.account().getSynchronizingAccounts(); List<EntityAccount> accounts = db.account().getSynchronizingAccounts();
for (final EntityAccount account : accounts) { for (final EntityAccount account : accounts) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
if (account.notify) if (account.notify)
account.createNotificationChannel(ServiceSynchronize.this); account.createNotificationChannel(ServiceSynchronize.this);
else else

Loading…
Cancel
Save