|
|
|
@@ -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<Integer, Entry> 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;
|
|
|
|
|