From fccca5ee4e7df780c374cb78ce9616ba48140e2c Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 5 Aug 2018 10:56:24 +0000 Subject: [PATCH] Disable separate notification for new email --- app/src/main/AndroidManifest.xml | 4 +- .../java/eu/faircode/email/ActivityView.java | 15 +++++ .../eu/faircode/email/ServiceSynchronize.java | 65 ++++++++++++++++--- 3 files changed, 75 insertions(+), 9 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 9b76e6f9e3..e019ca6154 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -29,7 +29,9 @@ android:name=".ActivitySetup" android:parentActivityName=".ActivityView" /> - + diff --git a/app/src/main/java/eu/faircode/email/ActivityView.java b/app/src/main/java/eu/faircode/email/ActivityView.java index df7f12f158..3f222ab485 100644 --- a/app/src/main/java/eu/faircode/email/ActivityView.java +++ b/app/src/main/java/eu/faircode/email/ActivityView.java @@ -164,6 +164,20 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack return true; } + @Override + public boolean onPrepareOptionsMenu(Menu menu) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); + menu.findItem(R.id.menu_folders).setVisible(prefs.getBoolean("eula", false)); + return super.onPrepareOptionsMenu(menu); + } + + @Override + protected void onNewIntent(Intent intent) { + Log.i(Helper.TAG, "New intent=" + intent); + getSupportFragmentManager().popBackStack("unified", 0); + super.onNewIntent(intent); + } + @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); @@ -222,6 +236,7 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack private void init() { syncState(); + invalidateOptionsMenu(); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); if (prefs.getBoolean("eula", false)) { diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index 79b76dc355..74423bb526 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -29,10 +29,12 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.media.RingtoneManager; import android.net.ConnectivityManager; import android.net.Network; import android.net.NetworkCapabilities; import android.net.NetworkRequest; +import android.net.Uri; import android.os.Build; import android.os.SystemClock; import android.support.annotation.Nullable; @@ -95,6 +97,7 @@ public class ServiceSynchronize extends LifecycleService { private ExecutorService executor = Executors.newSingleThreadExecutor(); private static final int NOTIFICATION_SYNCHRONIZE = 1; + private static final int NOTIFICATION_UNSEEN = 2; private static final long NOOP_INTERVAL = 9 * 60 * 1000L; // ms private static final int FETCH_BATCH_SIZE = 10; @@ -120,7 +123,7 @@ public class ServiceSynchronize extends LifecycleService { public void onCreate() { Log.i(Helper.TAG, "Service create"); super.onCreate(); - startForeground(NOTIFICATION_SYNCHRONIZE, getNotification(0, -1, 0).build()); + startForeground(NOTIFICATION_SYNCHRONIZE, getNotification(0, 0).build()); // Listen for network changes ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); @@ -130,11 +133,25 @@ public class ServiceSynchronize extends LifecycleService { cm.registerNetworkCallback(builder.build(), networkCallback); DB.getInstance(this).account().liveStats().observe(this, new Observer() { + private int prev_unseen = -1; + @Override public void onChanged(@Nullable TupleAccountStats stats) { if (stats != null) { NotificationManager nm = getSystemService(NotificationManager.class); - nm.notify(NOTIFICATION_SYNCHRONIZE, getNotification(stats.accounts, stats.operations, stats.unseen).build()); + nm.notify(NOTIFICATION_SYNCHRONIZE, + getNotification(stats.accounts, stats.operations).build()); + + if (stats.unseen > 0) { + if (stats.unseen != prev_unseen) { + boolean sound = (stats.unseen > prev_unseen); + nm.cancel(NOTIFICATION_UNSEEN); + nm.notify(NOTIFICATION_UNSEEN, getNotification(stats.unseen, sound).build()); + } + } else + nm.cancel(NOTIFICATION_UNSEEN); + + prev_unseen = stats.unseen; } } }); @@ -165,7 +182,7 @@ public class ServiceSynchronize extends LifecycleService { return START_STICKY; } - private Notification.Builder getNotification(int accounts, int operations, int unseen) { + private Notification.Builder getNotification(int accounts, int operations) { // Build pending intent Intent intent = new Intent(this, ActivityView.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); @@ -175,25 +192,57 @@ public class ServiceSynchronize extends LifecycleService { // Build notification Notification.Builder builder; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) - builder = new Notification.Builder(this, unseen == 0 ? "service" : "notification"); + builder = new Notification.Builder(this, "service"); else builder = new Notification.Builder(this); builder .setSmallIcon(R.drawable.baseline_mail_outline_24) .setContentTitle(getString(R.string.title_notification_synchronizing, accounts)) - .setContentText(getString(R.string.title_notification_unseen, unseen)) + .setContentText(getString(R.string.title_notification_operations, operations)) .setContentIntent(pi) - .setStyle(new Notification.BigTextStyle().setSummaryText(getString(R.string.title_operations, operations))) .setAutoCancel(false) - .setShowWhen(unseen > 0) - .setPriority(unseen == 0 ? Notification.PRIORITY_MIN : Notification.PRIORITY_DEFAULT) + .setShowWhen(false) + .setPriority(Notification.PRIORITY_MIN) .setCategory(Notification.CATEGORY_STATUS) .setVisibility(Notification.VISIBILITY_SECRET); return builder; } + private Notification.Builder getNotification(int unseen, boolean sound) { + // Build pending intent + Intent intent = new Intent(this, ActivityView.class); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + PendingIntent pi = PendingIntent.getActivity( + this, ActivityView.REQUEST_VIEW, intent, PendingIntent.FLAG_UPDATE_CURRENT); + + // Build notification + Notification.Builder builder; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) + builder = new Notification.Builder(this, "notification"); + else + builder = new Notification.Builder(this); + + builder + .setSmallIcon(R.drawable.baseline_mail_24) + .setContentTitle(getString(R.string.title_notification_unseen, unseen)) + .setContentIntent(pi) + .setAutoCancel(true) + .setShowWhen(false) // when of first or last new email? + .setPriority(Notification.PRIORITY_DEFAULT) + .setCategory(Notification.CATEGORY_STATUS) + .setVisibility(Notification.VISIBILITY_PUBLIC); + + if (sound) { + Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); + builder.setSound(uri); + } + + return builder; + } + + private Notification.Builder getNotification(String action, Throwable ex) { // Build pending intent Intent intent = new Intent(this, ActivityView.class);