Skip to content

Commit

Permalink
Ensure that notification id is always read from thread local state, t…
Browse files Browse the repository at this point in the history
…o prevent mixups.
  • Loading branch information
rtibbles committed Nov 20, 2023
1 parent 5cac903 commit 13f75f6
Showing 1 changed file with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ public class PythonWorker extends RemoteListenableWorker {

public static PythonWorker mWorker = null;

public int notificationId;

public static ThreadLocal<Integer> threadNotificationId = new ThreadLocal<>();

private String notificationTitle;
Expand All @@ -50,7 +48,7 @@ public PythonWorker(

notificationTitle = context.getString(R.string.app_name);

notificationId = ThreadLocalRandom.current().nextInt(1, 65537);
threadNotificationId.set(ThreadLocalRandom.current().nextInt(1, 65537));

PythonWorker.mWorker = this;

Expand Down Expand Up @@ -86,6 +84,8 @@ public ListenableFuture<Result> startRemoteWork() {
runAsForeground();
}

int notificationId = getNotificationId();

// The python thread handling the work needs to be run in a
// separate thread so that future can be returned. Without
// it, any cancellation can't be processed.
Expand Down Expand Up @@ -138,7 +138,7 @@ public static native int nativeStart(
);

public ForegroundInfo getForegroundInfo() {
return new ForegroundInfo(notificationId, Notifications.createNotification(notificationTitle, null, -1, -1));
return new ForegroundInfo(getNotificationId(), Notifications.createNotification(notificationTitle, null, -1, -1));
}

public void runAsForeground() {
Expand Down

0 comments on commit 13f75f6

Please sign in to comment.