From 1779144659d4181cb9bc9492a5f50fa44bd7178d Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 25 Jun 2022 11:31:35 +0200 Subject: [PATCH] Added create/delete account notification channel --- .../eu/faircode/email/AdapterAccount.java | 84 ++++++++++++++++++- 1 file changed, 80 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/AdapterAccount.java b/app/src/main/java/eu/faircode/email/AdapterAccount.java index b5c358f0f3..5bb2f958d1 100644 --- a/app/src/main/java/eu/faircode/email/AdapterAccount.java +++ b/app/src/main/java/eu/faircode/email/AdapterAccount.java @@ -51,6 +51,7 @@ import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; +import androidx.annotation.RequiresApi; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.widget.PopupMenu; import androidx.constraintlayout.widget.Group; @@ -385,13 +386,16 @@ public class AdapterAccount extends RecyclerView.Adapter= Build.VERSION_CODES.O) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { String channelId = EntityAccount.getNotificationChannelId(account.id); NotificationManager nm = Helper.getSystemService(context, NotificationManager.class); NotificationChannel channel = nm.getNotificationChannel(channelId); - if (channel != null) + if (channel == null) + popupMenu.getMenu().add(Menu.NONE, R.string.title_create_channel, order++, R.string.title_create_channel); + else { popupMenu.getMenu().add(Menu.NONE, R.string.title_edit_channel, order++, R.string.title_edit_channel); + popupMenu.getMenu().add(Menu.NONE, R.string.title_delete_channel, order++, R.string.title_delete_channel); + } } if (settings) @@ -418,8 +422,17 @@ public class AdapterAccount extends RecyclerView.Adapter= Build.VERSION_CODES.O) + onActionCreateChannel(); + return true; } else if (itemId == R.string.title_edit_channel) { - onActionEditChannel(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) + onActionEditChannel(); + return true; + } else if (itemId == R.string.title_delete_channel) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) + onActionDeleteChannel(); return true; } else if (itemId == R.string.title_edit_properties) { ViewHolder.this.onClick(view); @@ -514,6 +527,41 @@ public class AdapterAccount extends RecyclerView.Adapter() { + @Override + protected Void onExecute(Context context, Bundle args) { + long id = args.getLong("id"); + + DB db = DB.getInstance(context); + db.account().setAccountNotify(id, true); + + return null; + } + + @Override + protected void onExecuted(Bundle args, Void data) { + onActionEditChannel(); + } + + @Override + protected void onException(Bundle args, Throwable ex) { + Log.unexpectedError(parentFragment.getParentFragmentManager(), ex); + } + }.execute(context, owner, args, "create:channel"); + } + @TargetApi(Build.VERSION_CODES.O) private void onActionEditChannel() { Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS) @@ -526,6 +574,34 @@ public class AdapterAccount extends RecyclerView.Adapter() { + @Override + protected Void onExecute(Context context, Bundle args) { + long id = args.getLong("id"); + + DB db = DB.getInstance(context); + db.account().setAccountNotify(id, false); + + return null; + } + + @Override + protected void onExecuted(Bundle args, Void data) { + account.deleteNotificationChannel(context); + } + + @Override + protected void onException(Bundle args, Throwable ex) { + Log.unexpectedError(parentFragment.getParentFragmentManager(), ex); + } + }.execute(context, owner, args, "create:channel"); + } + private void onActionCopy() { LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context); lbm.sendBroadcast(