Merge \"Fix #29737133 : content-trigger jobs not running properly\" into nyc-dev
am: f480698298

Change-Id: I56aa1a6ecb2409b905de4a8419e1b76523120efc
diff --git a/services/core/java/com/android/server/job/controllers/ContentObserverController.java b/services/core/java/com/android/server/job/controllers/ContentObserverController.java
index 9dce070..a42d0cd 100644
--- a/services/core/java/com/android/server/job/controllers/ContentObserverController.java
+++ b/services/core/java/com/android/server/job/controllers/ContentObserverController.java
@@ -22,6 +22,7 @@
 import android.net.Uri;
 import android.os.Handler;
 import android.os.UserHandle;
+import android.util.Slog;
 import android.util.TimeUtils;
 import android.util.ArrayMap;
 import android.util.ArraySet;
@@ -40,6 +41,7 @@
  */
 public class ContentObserverController extends StateController {
     private static final String TAG = "JobScheduler.Content";
+    private static final boolean DEBUG = false;
 
     /**
      * Maximum number of changing URIs we will batch together to report.
@@ -88,6 +90,9 @@
             if (taskStatus.contentObserverJobInstance == null) {
                 taskStatus.contentObserverJobInstance = new JobInstance(taskStatus);
             }
+            if (DEBUG) {
+                Slog.i(TAG, "Tracking content-trigger job " + taskStatus);
+            }
             mTrackedTasks.add(taskStatus);
             boolean havePendingUris = false;
             // If there is a previous job associated with the new job, propagate over
@@ -175,6 +180,9 @@
                     taskStatus.contentObserverJobInstance = null;
                 }
             }
+            if (DEBUG) {
+                Slog.i(TAG, "No longer tracking job " + taskStatus);
+            }
             mTrackedTasks.remove(taskStatus);
         }
     }
@@ -194,16 +202,20 @@
     }
 
     final class ObserverInstance extends ContentObserver {
-        final Uri mUri;
+        final JobInfo.TriggerContentUri mUri;
         final ArraySet<JobInstance> mJobs = new ArraySet<>();
 
-        public ObserverInstance(Handler handler, Uri uri) {
+        public ObserverInstance(Handler handler, JobInfo.TriggerContentUri uri) {
             super(handler);
             mUri = uri;
         }
 
         @Override
         public void onChange(boolean selfChange, Uri uri) {
+            if (DEBUG) {
+                Slog.i(TAG, "onChange(self=" + selfChange + ") for " + uri
+                        + " when mUri=" + mUri);
+            }
             synchronized (mLock) {
                 final int N = mJobs.size();
                 for (int i=0; i<N; i++) {
@@ -255,14 +267,25 @@
                 for (JobInfo.TriggerContentUri uri : uris) {
                     ObserverInstance obs = mObservers.get(uri);
                     if (obs == null) {
-                        obs = new ObserverInstance(mHandler, uri.getUri());
+                        obs = new ObserverInstance(mHandler, uri);
                         mObservers.put(uri, obs);
+                        final boolean andDescendants = (uri.getFlags() &
+                                JobInfo.TriggerContentUri.FLAG_NOTIFY_FOR_DESCENDANTS) != 0;
+                        if (DEBUG) {
+                            Slog.v(TAG, "New observer " + obs + " for " + uri.getUri()
+                                    + " andDescendants=" + andDescendants);
+                        }
                         mContext.getContentResolver().registerContentObserver(
                                 uri.getUri(),
-                                (uri.getFlags() &
-                                        JobInfo.TriggerContentUri.FLAG_NOTIFY_FOR_DESCENDANTS)
-                                    != 0,
+                                andDescendants,
                                 obs);
+                    } else {
+                        if (DEBUG) {
+                            final boolean andDescendants = (uri.getFlags() &
+                                    JobInfo.TriggerContentUri.FLAG_NOTIFY_FOR_DESCENDANTS) != 0;
+                            Slog.v(TAG, "Reusing existing observer " + obs + " for " + uri.getUri()
+                                    + " andDescendants=" + andDescendants);
+                        }
                     }
                     obs.mJobs.add(this);
                     mMyObservers.add(obs);
@@ -315,8 +338,11 @@
                 final ObserverInstance obs = mMyObservers.get(i);
                 obs.mJobs.remove(this);
                 if (obs.mJobs.size() == 0) {
+                    if (DEBUG) {
+                        Slog.i(TAG, "Unregistering observer " + obs + " for " + obs.mUri.getUri());
+                    }
                     mContext.getContentResolver().unregisterContentObserver(obs);
-                    mObservers.remove(obs);
+                    mObservers.remove(obs.mUri);
                 }
             }
         }