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

Commit e056b0d8 authored by Soonil Nagarkar's avatar Soonil Nagarkar
Browse files

Move UID update code to location thread to prevent ANRs.

BUG: 62776535
Test: Not tested
Change-Id: Icecb6b32c9bf637fa15cf13a0c44ab3d29de1658
parent 58aeff81
Loading
Loading
Loading
Loading
+59 −51
Original line number Diff line number Diff line
@@ -324,58 +324,13 @@ public class LocationManagerService extends ILocationManager.Stub {
            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);
                                    }
                                }
                            }
                        }
                        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());
                                }
                            }
                        }
                public void onUidImportance(final int uid, final int importance) {
                    mLocationHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            onUidImportanceChanged(uid, importance);
                        }

                    });
                }
            };
            mActivityManager.addOnUidImportanceListener(uidImportanceListener,
@@ -455,6 +410,59 @@ public class LocationManagerService extends ILocationManager.Stub {
        }, 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;
    }