Fix for JobScheduler dropping periodic jobs

Skip writing to disk when a periodic job is
removed from jobStore after it completes
execution.

Change-Id: Ib4f2cf18554bf9c87138c1984c96cc62f8dbf7e2
Bug: 27147454
diff --git a/services/core/java/com/android/server/job/JobSchedulerService.java b/services/core/java/com/android/server/job/JobSchedulerService.java
index e5e86ac..4028372 100644
--- a/services/core/java/com/android/server/job/JobSchedulerService.java
+++ b/services/core/java/com/android/server/job/JobSchedulerService.java
@@ -330,7 +330,7 @@
 
     private void cancelJobImpl(JobStatus cancelled) {
         if (DEBUG) Slog.d(TAG, "CANCEL: " + cancelled.toShortString());
-        stopTrackingJob(cancelled);
+        stopTrackingJob(cancelled, true /* writeBack */);
         synchronized (mJobs) {
             // Remove from pending queue.
             mPendingJobs.remove(cancelled);
@@ -509,12 +509,12 @@
      * Called when we want to remove a JobStatus object that we've finished executing. Returns the
      * object removed.
      */
-    private boolean stopTrackingJob(JobStatus jobStatus) {
+    private boolean stopTrackingJob(JobStatus jobStatus, boolean writeBack) {
         boolean removed;
         boolean rocking;
         synchronized (mJobs) {
             // Remove from store as well as controllers.
-            removed = mJobs.remove(jobStatus);
+            removed = mJobs.remove(jobStatus, writeBack);
             rocking = mReadyToRock;
         }
         if (removed && rocking) {
@@ -645,7 +645,9 @@
         if (DEBUG) {
             Slog.d(TAG, "Completed " + jobStatus + ", reschedule=" + needsReschedule);
         }
-        if (!stopTrackingJob(jobStatus)) {
+        // Do not write back immediately if this is a periodic job. The job may get lost if system
+        // shuts down before it is added back.
+        if (!stopTrackingJob(jobStatus, !jobStatus.getJob().isPeriodic())) {
             if (DEBUG) {
                 Slog.d(TAG, "Could not find job to remove. Was job removed while executing?");
             }