Move UID update code to location thread to prevent ANRs.

BUG: 62776535
Test: Not tested
Change-Id: Icecb6b32c9bf637fa15cf13a0c44ab3d29de1658
diff --git a/services/core/java/com/android/server/LocationManagerService.java b/services/core/java/com/android/server/LocationManagerService.java
index 7275461..eda283e 100644
--- a/services/core/java/com/android/server/LocationManagerService.java
+++ b/services/core/java/com/android/server/LocationManagerService.java
@@ -324,58 +324,13 @@
             ActivityManager.OnUidImportanceListener uidImportanceListener
                     = new ActivityManager.OnUidImportanceListener() {
                 @Override
-                public void onUidImportance(int uid, int importance) {
-                    boolean foreground = isImportanceForeground(importance);
-                    HashSet<String> affectedProviders = new HashSet<>(mRecordsByProvider.size());
-                    synchronized (mLock) {
-                        for (Entry<String, ArrayList<UpdateRecord>> entry
-                                : mRecordsByProvider.entrySet()) {
-                            String provider = entry.getKey();
-                            for (UpdateRecord record : entry.getValue()) {
-                                if (record.mReceiver.mIdentity.mUid == uid
-                                        && record.mIsForegroundUid != foreground) {
-                                    if (D) Log.d(TAG, "request from uid " + uid + " is now "
-                                            + (foreground ? "foreground" : "background)"));
-                                    record.mIsForegroundUid = foreground;
-
-                                    if (!isThrottlingExemptLocked(record.mReceiver.mIdentity)) {
-                                        affectedProviders.add(provider);
-                                    }
-                                }
-                            }
+                public void onUidImportance(final int uid, final int importance) {
+                    mLocationHandler.post(new Runnable() {
+                        @Override
+                        public void run() {
+                            onUidImportanceChanged(uid, importance);
                         }
-                        for (String provider : affectedProviders) {
-                            applyRequirementsLocked(provider);
-                        }
-
-                        for (Entry<IGnssMeasurementsListener, Identity> entry
-                                : mGnssMeasurementsListeners.entrySet()) {
-                            if (entry.getValue().mUid == uid) {
-                                if (D) Log.d(TAG, "gnss measurements listener from uid " + uid
-                                    + " is now " + (foreground ? "foreground" : "background)"));
-                                if (foreground || isThrottlingExemptLocked(entry.getValue())) {
-                                    mGnssMeasurementsProvider.addListener(entry.getKey());
-                                } else {
-                                    mGnssMeasurementsProvider.removeListener(entry.getKey());
-                                }
-                            }
-                        }
-
-                        for (Entry<IGnssNavigationMessageListener, Identity> entry
-                            : mGnssNavigationMessageListeners.entrySet()) {
-                            if (entry.getValue().mUid == uid) {
-                                if (D) Log.d(TAG, "gnss navigation message listener from uid "
-                                    + uid + " is now "
-                                    + (foreground ? "foreground" : "background)"));
-                                if (foreground || isThrottlingExemptLocked(entry.getValue())) {
-                                    mGnssNavigationMessageProvider.addListener(entry.getKey());
-                                } else {
-                                    mGnssNavigationMessageProvider.removeListener(entry.getKey());
-                                }
-                            }
-                        }
-                    }
-
+                    });
                 }
             };
             mActivityManager.addOnUidImportanceListener(uidImportanceListener,
@@ -455,6 +410,59 @@
         }, UserHandle.ALL, intentFilter, null, mLocationHandler);
     }
 
+    private void onUidImportanceChanged(int uid, int importance) {
+        boolean foreground = isImportanceForeground(importance);
+        HashSet<String> affectedProviders = new HashSet<>(mRecordsByProvider.size());
+        synchronized (mLock) {
+            for (Entry<String, ArrayList<UpdateRecord>> entry
+                : mRecordsByProvider.entrySet()) {
+                String provider = entry.getKey();
+                for (UpdateRecord record : entry.getValue()) {
+                    if (record.mReceiver.mIdentity.mUid == uid
+                        && record.mIsForegroundUid != foreground) {
+                        if (D) Log.d(TAG, "request from uid " + uid + " is now "
+                            + (foreground ? "foreground" : "background)"));
+                        record.mIsForegroundUid = foreground;
+
+                        if (!isThrottlingExemptLocked(record.mReceiver.mIdentity)) {
+                            affectedProviders.add(provider);
+                        }
+                    }
+                }
+            }
+            for (String provider : affectedProviders) {
+                applyRequirementsLocked(provider);
+            }
+
+            for (Entry<IGnssMeasurementsListener, Identity> entry
+                : mGnssMeasurementsListeners.entrySet()) {
+                if (entry.getValue().mUid == uid) {
+                    if (D) Log.d(TAG, "gnss measurements listener from uid " + uid
+                        + " is now " + (foreground ? "foreground" : "background)"));
+                    if (foreground || isThrottlingExemptLocked(entry.getValue())) {
+                        mGnssMeasurementsProvider.addListener(entry.getKey());
+                    } else {
+                        mGnssMeasurementsProvider.removeListener(entry.getKey());
+                    }
+                }
+            }
+
+            for (Entry<IGnssNavigationMessageListener, Identity> entry
+                : mGnssNavigationMessageListeners.entrySet()) {
+                if (entry.getValue().mUid == uid) {
+                    if (D) Log.d(TAG, "gnss navigation message listener from uid "
+                        + uid + " is now "
+                        + (foreground ? "foreground" : "background)"));
+                    if (foreground || isThrottlingExemptLocked(entry.getValue())) {
+                        mGnssNavigationMessageProvider.addListener(entry.getKey());
+                    } else {
+                        mGnssNavigationMessageProvider.removeListener(entry.getKey());
+                    }
+                }
+            }
+        }
+    }
+
     private static boolean isImportanceForeground(int importance) {
         return importance <= FOREGROUND_IMPORTANCE_CUTOFF;
     }