Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d5706f0f authored by Geoffrey Pitsch's avatar Geoffrey Pitsch
Browse files

Fix potential race bug where notification may persist after job finish.

The job's OnFinished callback occurs on a separate thread from the
JobWatcher.  It's hypothetically possible that OnFinished is called
after the JobWatcher checks isFinished but before it calls notify().
The onFinished call's actions cancel the notification, but JobWatcher
could then reinstate it as a non-foreground notification.

Bug: 64974697
Test: docsui unit tests
Change-Id: Id5b9e4ae6ca31b560c243022283ea7be211ece98
parent 028b2630
Loading
Loading
Loading
Loading
+19 −14
Original line number Diff line number Diff line
@@ -354,7 +354,7 @@ public class FileOperationService extends Service implements Job.Listener {
                job.id, NOTIFICATION_ID_PROGRESS, notification);

        // Set up related monitor
        JobMonitor monitor = new JobMonitor(job, notificationManager, handler);
        JobMonitor monitor = new JobMonitor(job, notificationManager, handler, mJobs);
        monitor.start();
    }

@@ -458,11 +458,14 @@ public class FileOperationService extends Service implements Job.Listener {
        private final Job mJob;
        private final NotificationManager mNotificationManager;
        private final Handler mHandler;
        private final Object mJobsLock;

        private JobMonitor(Job job, NotificationManager notificationManager, Handler handler) {
        private JobMonitor(Job job, NotificationManager notificationManager, Handler handler,
                Object jobsLock) {
            mJob = job;
            mNotificationManager = notificationManager;
            mHandler = handler;
            mJobsLock = jobsLock;
        }

        private void start() {
@@ -471,6 +474,7 @@ public class FileOperationService extends Service implements Job.Listener {

        @Override
        public void run() {
            synchronized (mJobsLock) {
                if (mJob.isFinished()) {
                    // Finish notification is already shown. Progress notification is removed.
                    // Just finish itself.
@@ -486,6 +490,7 @@ public class FileOperationService extends Service implements Job.Listener {
                mHandler.postDelayed(this, PROGRESS_INTERVAL_MILLIS);
            }
        }
    }

    @Override
    public IBinder onBind(Intent intent) {