use distinct notification id for video transcoder

This commit is contained in:
Daniel Gultsch 2024-03-28 13:53:29 +01:00
parent 378efe1a8a
commit 26dde5370a
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2
3 changed files with 66 additions and 37 deletions

View file

@ -90,7 +90,7 @@ public class AttachFileToConversationRunnable implements Runnable, TranscoderLis
private void processAsVideo() throws FileNotFoundException { private void processAsVideo() throws FileNotFoundException {
Log.d(Config.LOGTAG, "processing file as video"); Log.d(Config.LOGTAG, "processing file as video");
mXmppConnectionService.startForcingForegroundNotification(); mXmppConnectionService.startOngoingVideoTranscodingForegroundNotification();
mXmppConnectionService.getFileBackend().setupRelativeFilePath(message, String.format("%s.%s", message.getUuid(), "mp4")); mXmppConnectionService.getFileBackend().setupRelativeFilePath(message, String.format("%s.%s", message.getUuid(), "mp4"));
final DownloadableFile file = mXmppConnectionService.getFileBackend().getFile(message); final DownloadableFile file = mXmppConnectionService.getFileBackend().getFile(message);
if (Objects.requireNonNull(file.getParentFile()).mkdirs()) { if (Objects.requireNonNull(file.getParentFile()).mkdirs()) {
@ -109,7 +109,7 @@ public class AttachFileToConversationRunnable implements Runnable, TranscoderLis
.transcode(); .transcode();
} catch (final RuntimeException e) { } catch (final RuntimeException e) {
// transcode can already throw if there is an invalid file format or a platform bug // transcode can already throw if there is an invalid file format or a platform bug
mXmppConnectionService.stopForcingForegroundNotification(); mXmppConnectionService.stopOngoingVideoTranscodingForegroundNotification();
processAsFile(); processAsFile();
return; return;
} }
@ -119,7 +119,7 @@ public class AttachFileToConversationRunnable implements Runnable, TranscoderLis
throw new AssertionError(e); throw new AssertionError(e);
} catch (final ExecutionException e) { } catch (final ExecutionException e) {
if (e.getCause() instanceof Error) { if (e.getCause() instanceof Error) {
mXmppConnectionService.stopForcingForegroundNotification(); mXmppConnectionService.stopOngoingVideoTranscodingForegroundNotification();
processAsFile(); processAsFile();
} else { } else {
Log.d(Config.LOGTAG, "ignoring execution exception. Should get handled by onTranscodeFiled() instead", e); Log.d(Config.LOGTAG, "ignoring execution exception. Should get handled by onTranscodeFiled() instead", e);
@ -138,7 +138,7 @@ public class AttachFileToConversationRunnable implements Runnable, TranscoderLis
@Override @Override
public void onTranscodeCompleted(int successCode) { public void onTranscodeCompleted(int successCode) {
mXmppConnectionService.stopForcingForegroundNotification(); mXmppConnectionService.stopOngoingVideoTranscodingForegroundNotification();
final File file = mXmppConnectionService.getFileBackend().getFile(message); final File file = mXmppConnectionService.getFileBackend().getFile(message);
long convertedFileSize = mXmppConnectionService.getFileBackend().getFile(message).getSize(); long convertedFileSize = mXmppConnectionService.getFileBackend().getFile(message).getSize();
Log.d(Config.LOGTAG, "originalFileSize=" + originalFileSize + " convertedFileSize=" + convertedFileSize); Log.d(Config.LOGTAG, "originalFileSize=" + originalFileSize + " convertedFileSize=" + convertedFileSize);
@ -162,13 +162,13 @@ public class AttachFileToConversationRunnable implements Runnable, TranscoderLis
@Override @Override
public void onTranscodeCanceled() { public void onTranscodeCanceled() {
mXmppConnectionService.stopForcingForegroundNotification(); mXmppConnectionService.stopOngoingVideoTranscodingForegroundNotification();
processAsFile(); processAsFile();
} }
@Override @Override
public void onTranscodeFailed(@NonNull @NotNull Throwable exception) { public void onTranscodeFailed(@NonNull @NotNull Throwable exception) {
mXmppConnectionService.stopForcingForegroundNotification(); mXmppConnectionService.stopOngoingVideoTranscodingForegroundNotification();
Log.d(Config.LOGTAG, "video transcoding failed", exception); Log.d(Config.LOGTAG, "video transcoding failed", exception);
processAsFile(); processAsFile();
} }

View file

@ -30,6 +30,7 @@ import android.text.style.StyleSpan;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.util.Log; import android.util.Log;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import androidx.core.app.ActivityCompat; import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
@ -108,6 +109,7 @@ public class NotificationService {
public static final int ONGOING_CALL_NOTIFICATION_ID = NOTIFICATION_ID_MULTIPLIER * 10; public static final int ONGOING_CALL_NOTIFICATION_ID = NOTIFICATION_ID_MULTIPLIER * 10;
public static final int MISSED_CALL_NOTIFICATION_ID = NOTIFICATION_ID_MULTIPLIER * 12; public static final int MISSED_CALL_NOTIFICATION_ID = NOTIFICATION_ID_MULTIPLIER * 12;
private static final int DELIVERY_FAILED_NOTIFICATION_ID = NOTIFICATION_ID_MULTIPLIER * 13; private static final int DELIVERY_FAILED_NOTIFICATION_ID = NOTIFICATION_ID_MULTIPLIER * 13;
public static final int ONGOING_VIDEO_TRANSCODING_NOTIFICATION_ID = NOTIFICATION_ID_MULTIPLIER * 14;
private final XmppConnectionService mXmppConnectionService; private final XmppConnectionService mXmppConnectionService;
private final LinkedHashMap<String, ArrayList<Message>> notifications = new LinkedHashMap<>(); private final LinkedHashMap<String, ArrayList<Message>> notifications = new LinkedHashMap<>();
private final HashMap<Conversation, AtomicInteger> mBacklogMessageCounter = new HashMap<>(); private final HashMap<Conversation, AtomicInteger> mBacklogMessageCounter = new HashMap<>();
@ -1919,18 +1921,33 @@ public class NotificationService {
notify(ERROR_NOTIFICATION_ID, mBuilder.build()); notify(ERROR_NOTIFICATION_ID, mBuilder.build());
} }
void updateFileAddingNotification(int current, Message message) { void updateFileAddingNotification(final int current, final Message message) {
Notification.Builder mBuilder = new Notification.Builder(mXmppConnectionService);
mBuilder.setContentTitle(mXmppConnectionService.getString(R.string.transcoding_video)); final Notification notification = videoTranscoding(current, message);
mBuilder.setProgress(100, current, false); notify(ONGOING_VIDEO_TRANSCODING_NOTIFICATION_ID, notification);
mBuilder.setSmallIcon(R.drawable.ic_hourglass_empty_white_24dp); }
mBuilder.setContentIntent(createContentIntent(message.getConversation()));
mBuilder.setOngoing(true); private Notification videoTranscoding(final int current, @Nullable final Message message) {
if (Compatibility.runsTwentySix()) { final Notification.Builder builder = new Notification.Builder(mXmppConnectionService);
mBuilder.setChannelId("compression"); builder.setContentTitle(mXmppConnectionService.getString(R.string.transcoding_video));
if (current >= 0) {
builder.setProgress(100, current, false);
} else {
builder.setProgress(100, 0, true);
} }
Notification notification = mBuilder.build(); builder.setSmallIcon(R.drawable.ic_hourglass_empty_white_24dp);
notify(FOREGROUND_NOTIFICATION_ID, notification); if (message != null) {
builder.setContentIntent(createContentIntent(message.getConversation()));
}
builder.setOngoing(true);
if (Compatibility.runsTwentySix()) {
builder.setChannelId("compression");
}
return builder.build();
}
public Notification getIndeterminateVideoTranscoding() {
return videoTranscoding(-1, null);
} }
private void notify(final String tag, final int id, final Notification notification) { private void notify(final String tag, final int id, final Notification notification) {

View file

@ -243,7 +243,7 @@ public class XmppConnectionService extends Service {
private final ChannelDiscoveryService mChannelDiscoveryService = new ChannelDiscoveryService(this); private final ChannelDiscoveryService mChannelDiscoveryService = new ChannelDiscoveryService(this);
private final ShortcutService mShortcutService = new ShortcutService(this); private final ShortcutService mShortcutService = new ShortcutService(this);
private final AtomicBoolean mInitialAddressbookSyncCompleted = new AtomicBoolean(false); private final AtomicBoolean mInitialAddressbookSyncCompleted = new AtomicBoolean(false);
private final AtomicBoolean mForceForegroundService = new AtomicBoolean(false); private final AtomicBoolean mOngoingVideoTranscoding = new AtomicBoolean(false);
private final AtomicBoolean mForceDuringOnCreate = new AtomicBoolean(false); private final AtomicBoolean mForceDuringOnCreate = new AtomicBoolean(false);
private final AtomicReference<OngoingCall> ongoingCall = new AtomicReference<>(); private final AtomicReference<OngoingCall> ongoingCall = new AtomicReference<>();
private final OnMessagePacketReceived mMessageParser = new MessageParser(this); private final OnMessagePacketReceived mMessageParser = new MessageParser(this);
@ -526,13 +526,13 @@ public class XmppConnectionService extends Service {
} }
} }
public void startForcingForegroundNotification() { public void startOngoingVideoTranscodingForegroundNotification() {
mForceForegroundService.set(true); mOngoingVideoTranscoding.set(true);
toggleForegroundService(); toggleForegroundService();
} }
public void stopForcingForegroundNotification() { public void stopOngoingVideoTranscodingForegroundNotification() {
mForceForegroundService.set(false); mOngoingVideoTranscoding.set(false);
toggleForegroundService(); toggleForegroundService();
} }
@ -1467,35 +1467,47 @@ public class XmppConnectionService extends Service {
private void toggleForegroundService(final boolean force) { private void toggleForegroundService(final boolean force) {
final boolean status; final boolean status;
final OngoingCall ongoing = ongoingCall.get(); final OngoingCall ongoing = ongoingCall.get();
if (force || mForceDuringOnCreate.get() || mForceForegroundService.get() || ongoing != null || (Compatibility.keepForegroundService(this) && hasEnabledAccounts())) { final boolean ongoingVideoTranscoding = mOngoingVideoTranscoding.get();
final int id;
if (force
|| mForceDuringOnCreate.get()
|| ongoingVideoTranscoding
|| ongoing != null
|| (Compatibility.keepForegroundService(this) && hasEnabledAccounts())) {
final Notification notification; final Notification notification;
final int id;
if (ongoing != null) { if (ongoing != null) {
notification = this.mNotificationService.getOngoingCallNotification(ongoing); notification = this.mNotificationService.getOngoingCallNotification(ongoing);
id = NotificationService.ONGOING_CALL_NOTIFICATION_ID; id = NotificationService.ONGOING_CALL_NOTIFICATION_ID;
startForegroundOrCatch(id, notification, true); startForegroundOrCatch(id, notification, true);
mNotificationService.cancel(NotificationService.FOREGROUND_NOTIFICATION_ID); } else if (ongoingVideoTranscoding) {
notification = this.mNotificationService.getIndeterminateVideoTranscoding();
id = NotificationService.ONGOING_VIDEO_TRANSCODING_NOTIFICATION_ID;
startForegroundOrCatch(id, notification, false);
} else { } else {
notification = this.mNotificationService.createForegroundNotification(); notification = this.mNotificationService.createForegroundNotification();
id = NotificationService.FOREGROUND_NOTIFICATION_ID; id = NotificationService.FOREGROUND_NOTIFICATION_ID;
startForegroundOrCatch(id, notification, false); startForegroundOrCatch(id, notification, false);
} }
mNotificationService.notify(id, notification);
if (!mForceForegroundService.get()) {
mNotificationService.notify(id, notification);
}
status = true; status = true;
} else { } else {
id = 0;
stopForeground(true); stopForeground(true);
status = false; status = false;
} }
if (!mForceForegroundService.get()) {
mNotificationService.cancel(NotificationService.FOREGROUND_NOTIFICATION_ID); for (final int toBeRemoved :
Collections2.filter(
Arrays.asList(
NotificationService.FOREGROUND_NOTIFICATION_ID,
NotificationService.ONGOING_CALL_NOTIFICATION_ID,
NotificationService.ONGOING_VIDEO_TRANSCODING_NOTIFICATION_ID),
i -> i != id)) {
mNotificationService.cancel(toBeRemoved);
} }
if (ongoing == null) { Log.d(
mNotificationService.cancel(NotificationService.ONGOING_CALL_NOTIFICATION_ID); Config.LOGTAG,
} "ForegroundService: " + (status ? "on" : "off") + ", notification: " + id);
Log.d(Config.LOGTAG, "ForegroundService: " + (status ? "on" : "off"));
} }
private void startForegroundOrCatch( private void startForegroundOrCatch(
@ -1531,13 +1543,13 @@ public class XmppConnectionService extends Service {
} }
public boolean foregroundNotificationNeedsUpdatingWhenErrorStateChanges() { public boolean foregroundNotificationNeedsUpdatingWhenErrorStateChanges() {
return !mForceForegroundService.get() && ongoingCall.get() == null && Compatibility.keepForegroundService(this) && hasEnabledAccounts(); return !mOngoingVideoTranscoding.get() && ongoingCall.get() == null && Compatibility.keepForegroundService(this) && hasEnabledAccounts();
} }
@Override @Override
public void onTaskRemoved(final Intent rootIntent) { public void onTaskRemoved(final Intent rootIntent) {
super.onTaskRemoved(rootIntent); super.onTaskRemoved(rootIntent);
if ((Compatibility.keepForegroundService(this) && hasEnabledAccounts()) || mForceForegroundService.get() || ongoingCall.get() != null) { if ((Compatibility.keepForegroundService(this) && hasEnabledAccounts()) || mOngoingVideoTranscoding.get() || ongoingCall.get() != null) {
Log.d(Config.LOGTAG, "ignoring onTaskRemoved because foreground service is activated"); Log.d(Config.LOGTAG, "ignoring onTaskRemoved because foreground service is activated");
} else { } else {
this.logoutAndSave(false); this.logoutAndSave(false);