diff --git a/src/gplay/java/org/thoughtcrime/securesms/notifications/FcmReceiveService.java b/src/gplay/java/org/thoughtcrime/securesms/notifications/FcmReceiveService.java index b52d2ca0a..999b762f7 100644 --- a/src/gplay/java/org/thoughtcrime/securesms/notifications/FcmReceiveService.java +++ b/src/gplay/java/org/thoughtcrime/securesms/notifications/FcmReceiveService.java @@ -102,7 +102,7 @@ public class FcmReceiveService extends FirebaseMessagingService { public void onMessageReceived(@NonNull RemoteMessage remoteMessage) { Log.i(TAG, "FCM push notification received"); synchronized (NOTIFICATION_CONTROLLER_LOCK) { - notificationController = GenericForegroundService.startForegroundTask(this, getString(R.string.connectivity_updating), false); + notificationController = GenericForegroundService.startForegroundTask(this, getString(R.string.connectivity_updating)); if (!ApplicationContext.dcAccounts.backgroundFetch(19)) { // we should complete within 20 seconds notificationController.close(); notificationController = null; diff --git a/src/main/java/org/thoughtcrime/securesms/PassphraseRequiredActionBarActivity.java b/src/main/java/org/thoughtcrime/securesms/PassphraseRequiredActionBarActivity.java index 4ede775c5..c043a046b 100644 --- a/src/main/java/org/thoughtcrime/securesms/PassphraseRequiredActionBarActivity.java +++ b/src/main/java/org/thoughtcrime/securesms/PassphraseRequiredActionBarActivity.java @@ -20,7 +20,7 @@ public abstract class PassphraseRequiredActionBarActivity extends BaseActionBarA return; } - if (GenericForegroundService.hasPinnedActivity()) { + if (GenericForegroundService.isForegroundTaskStarted()) { // this does not prevent intent set by onNewIntent(), // however, at least during onboarding, // this catches a lot of situations with otherwise weird app states. diff --git a/src/main/java/org/thoughtcrime/securesms/WelcomeActivity.java b/src/main/java/org/thoughtcrime/securesms/WelcomeActivity.java index 202a05db9..54d45c075 100644 --- a/src/main/java/org/thoughtcrime/securesms/WelcomeActivity.java +++ b/src/main/java/org/thoughtcrime/securesms/WelcomeActivity.java @@ -202,7 +202,7 @@ public class WelcomeActivity extends BaseActionBarActivity implements DcEventCen private void startImport(@Nullable final String backupFile, final @Nullable Uri backupFileUri) { - notificationController = GenericForegroundService.startForegroundTask(this, getString(R.string.import_backup_title), true); + notificationController = GenericForegroundService.startForegroundTask(this, getString(R.string.import_backup_title)); if( progressDialog!=null ) { progressDialog.dismiss(); diff --git a/src/main/java/org/thoughtcrime/securesms/preferences/ListSummaryPreferenceFragment.java b/src/main/java/org/thoughtcrime/securesms/preferences/ListSummaryPreferenceFragment.java index d60df44f0..361c427a1 100644 --- a/src/main/java/org/thoughtcrime/securesms/preferences/ListSummaryPreferenceFragment.java +++ b/src/main/java/org/thoughtcrime/securesms/preferences/ListSummaryPreferenceFragment.java @@ -151,7 +151,7 @@ public abstract class ListSummaryPreferenceFragment extends CorrectedPreferenceF } private void showProgressDialog() { - notificationController = GenericForegroundService.startForegroundTask(getContext(), getString(R.string.export_backup_desktop), true); + notificationController = GenericForegroundService.startForegroundTask(getContext(), getString(R.string.export_backup_desktop)); if( progressDialog!=null ) { progressDialog.dismiss(); progressDialog = null; diff --git a/src/main/java/org/thoughtcrime/securesms/qr/BackupTransferActivity.java b/src/main/java/org/thoughtcrime/securesms/qr/BackupTransferActivity.java index 6d29fff0d..1b203b358 100644 --- a/src/main/java/org/thoughtcrime/securesms/qr/BackupTransferActivity.java +++ b/src/main/java/org/thoughtcrime/securesms/qr/BackupTransferActivity.java @@ -67,7 +67,7 @@ public class BackupTransferActivity extends BaseActionBarActivity { DcHelper.getAccounts(this).stopIo(); String title = getString(transferMode == TransferMode.RECEIVER_SCAN_QR ? R.string.multidevice_receiver_title : R.string.multidevice_title); - notificationController = GenericForegroundService.startForegroundTask(this, title, true); + notificationController = GenericForegroundService.startForegroundTask(this, title); setContentView(R.layout.backup_provider_activity); diff --git a/src/main/java/org/thoughtcrime/securesms/service/GenericForegroundService.java b/src/main/java/org/thoughtcrime/securesms/service/GenericForegroundService.java index 96e6e6766..60b5b912c 100644 --- a/src/main/java/org/thoughtcrime/securesms/service/GenericForegroundService.java +++ b/src/main/java/org/thoughtcrime/securesms/service/GenericForegroundService.java @@ -12,6 +12,7 @@ import android.os.Build; import android.os.IBinder; import android.util.Log; +import androidx.annotation.DrawableRes; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.app.NotificationCompat.Builder; @@ -37,7 +38,7 @@ public final class GenericForegroundService extends Service { private static final String EXTRA_TITLE = "extra_title"; private static final String EXTRA_CONTENT_TEXT = "extra_content_text"; private static final String EXTRA_CHANNEL_ID = "extra_channel_id"; - private static final String EXTRA_PIN_ACTIVITY = "extra_pin_activity"; + private static final String EXTRA_ICON_RES = "extra_icon_res"; private static final String EXTRA_ID = "extra_id"; private static final String EXTRA_PROGRESS = "extra_progress"; private static final String EXTRA_PROGRESS_MAX = "extra_progress_max"; @@ -49,11 +50,11 @@ public final class GenericForegroundService extends Service { private static final AtomicInteger NEXT_ID = new AtomicInteger(); private static final AtomicBoolean CHANNEL_CREATED = new AtomicBoolean(false); - private static int pinnedActivityCounter = 0; + private static int startedCounter = 0; private final LinkedHashMap allActiveMessages = new LinkedHashMap<>(); - private static final Entry DEFAULTS = new Entry("", "", NotificationCenter.CH_GENERIC, true, -1, 0, 0, false); + private static final Entry DEFAULTS = new Entry("", "", NotificationCenter.CH_GENERIC, R.drawable.icon_notification, -1, 0, 0, false); private @Nullable Entry lastPosted; @@ -91,24 +92,28 @@ public final class GenericForegroundService extends Service { private synchronized void handleStart(@NonNull Intent intent) { Entry entry = Entry.fromIntent(intent); + + Log.i(TAG, String.format(Locale.ENGLISH, "handleStart() %s", entry)); + allActiveMessages.put(entry.id, entry); - if (entry.pinActivity) { - pinnedActivityCounter++; - } } private synchronized void handleStop(@NonNull Intent intent) { + Log.i(TAG, "handleStop()"); + int id = intent.getIntExtra(EXTRA_ID, -1); + Entry removed = allActiveMessages.remove(id); - if (removed != null && removed.pinActivity) { - pinnedActivityCounter = Math.max(pinnedActivityCounter-1, 0); + + if (removed == null) { + Log.w(TAG, "Could not find entry to remove"); } } private void postObligatoryForegroundNotification(@NonNull Entry active) { lastPosted = active; startForeground(NotificationCenter.ID_GENERIC, new Builder(this, active.channelId) - .setSmallIcon(R.drawable.notification_permanent) + .setSmallIcon(active.iconRes) .setContentTitle(active.title) .setTicker(active.contentText) .setContentText(active.contentText) @@ -122,9 +127,9 @@ public final class GenericForegroundService extends Service { return binder; } - // pinActivity makes the recent activity stay on top - // and tries to avoid it being replaced eg. by the chatlist when tapping the app icon. - public static NotificationController startForegroundTask(@NonNull Context context, @NonNull String task, boolean pinActivity) { + + public static NotificationController startForegroundTask(@NonNull Context context, @NonNull String task) { + startedCounter++; final int id = NEXT_ID.getAndIncrement(); createFgNotificationChannel(context); @@ -132,7 +137,7 @@ public final class GenericForegroundService extends Service { intent.setAction(ACTION_START); intent.putExtra(EXTRA_TITLE, task); intent.putExtra(EXTRA_CHANNEL_ID, NotificationCenter.CH_GENERIC); - intent.putExtra(EXTRA_PIN_ACTIVITY, pinActivity); + intent.putExtra(EXTRA_ICON_RES, R.drawable.notification_permanent); intent.putExtra(EXTRA_ID, id); ContextCompat.startForegroundService(context, intent); @@ -146,10 +151,11 @@ public final class GenericForegroundService extends Service { intent.putExtra(EXTRA_ID, id); ContextCompat.startForegroundService(context, intent); + startedCounter = Math.max(startedCounter-1, 0); } - public static boolean hasPinnedActivity() { - return pinnedActivityCounter > 0; + public static boolean isForegroundTaskStarted() { + return startedCounter > 0; } synchronized void replaceProgress(int id, int progressMax, int progress, boolean indeterminate, String message) { @@ -164,7 +170,7 @@ public final class GenericForegroundService extends Service { message = oldEntry.contentText; } - Entry newEntry = new Entry(oldEntry.title, message, oldEntry.channelId, oldEntry.pinActivity, oldEntry.id, progressMax, progress, indeterminate); + Entry newEntry = new Entry(oldEntry.title, message, oldEntry.channelId, oldEntry.iconRes, oldEntry.id, progressMax, progress, indeterminate); if (oldEntry.equals(newEntry)) { Log.d(TAG, String.format("handleReplace() skip, no change %s", newEntry)); @@ -196,16 +202,16 @@ public final class GenericForegroundService extends Service { final @NonNull String contentText; final @NonNull String channelId; final int id; - final boolean pinActivity; + final @DrawableRes int iconRes; final int progress; final int progressMax; final boolean indeterminate; - private Entry(@NonNull String title, @NonNull String contentText, @NonNull String channelId, boolean pinActivity, int id, int progressMax, int progress, boolean indeterminate) { + private Entry(@NonNull String title, @NonNull String contentText, @NonNull String channelId, @DrawableRes int iconRes, int id, int progressMax, int progress, boolean indeterminate) { this.title = title; this.contentText = contentText; this.channelId = channelId; - this.pinActivity = pinActivity; + this.iconRes = iconRes; this.id = id; this.progress = progress; this.progressMax = progressMax; @@ -224,12 +230,12 @@ public final class GenericForegroundService extends Service { String channelId = intent.getStringExtra(EXTRA_CHANNEL_ID); if (channelId == null) channelId = DEFAULTS.channelId; - boolean pinActivity = intent.getBooleanExtra(EXTRA_PIN_ACTIVITY, DEFAULTS.pinActivity); + int iconRes = intent.getIntExtra(EXTRA_ICON_RES, DEFAULTS.iconRes); int progress = intent.getIntExtra(EXTRA_PROGRESS, DEFAULTS.progress); int progressMax = intent.getIntExtra(EXTRA_PROGRESS_MAX, DEFAULTS.progressMax); boolean indeterminate = intent.getBooleanExtra(EXTRA_PROGRESS_INDETERMINATE, DEFAULTS.indeterminate); - return new Entry(title, contentText, channelId, pinActivity, id, progressMax, progress, indeterminate); + return new Entry(title, contentText, channelId, iconRes, id, progressMax, progress, indeterminate); } @Override @@ -244,7 +250,7 @@ public final class GenericForegroundService extends Service { Entry entry = (Entry) o; return id == entry.id && - pinActivity == entry.pinActivity && + iconRes == entry.iconRes && progress == entry.progress && progressMax == entry.progressMax && indeterminate == entry.indeterminate && @@ -261,7 +267,7 @@ public final class GenericForegroundService extends Service { hashCode *= 31; hashCode += id; hashCode *= 31; - hashCode += pinActivity ? 1 : 0; + hashCode += iconRes; hashCode *= 31; hashCode += progress; hashCode *= 31;