Show location icon when reporting GNSS measurements/status
GNSS measurements and status can be used to compute user location.
Hence, the location icon must be turned on in the status bar to
notify the user every time the measurements are reported to an
application. Also, check for location permission before delivery
as the application may have lost location permission.
Bug: 113332106
Test: Tested it manually using GNSS logger application.
Change-Id: I985610cab207af50a84e7e47390c51359375bb78
diff --git a/services/core/java/com/android/server/LocationManagerService.java b/services/core/java/com/android/server/LocationManagerService.java
index cc7bf33..da19e27 100644
--- a/services/core/java/com/android/server/LocationManagerService.java
+++ b/services/core/java/com/android/server/LocationManagerService.java
@@ -47,7 +47,6 @@
import android.location.IGnssMeasurementsListener;
import android.location.IGnssNavigationMessageListener;
import android.location.IGnssStatusListener;
-import android.location.IGnssStatusProvider;
import android.location.IGpsGeofenceHardware;
import android.location.ILocationListener;
import android.location.ILocationManager;
@@ -92,6 +91,7 @@
import com.android.server.location.GnssLocationProvider;
import com.android.server.location.GnssMeasurementsProvider;
import com.android.server.location.GnssNavigationMessageProvider;
+import com.android.server.location.GnssStatusListenerHelper;
import com.android.server.location.LocationBlacklist;
import com.android.server.location.LocationFudger;
import com.android.server.location.LocationProviderInterface;
@@ -182,7 +182,7 @@
private ActivityManager mActivityManager;
private UserManager mUserManager;
private GeocoderProxy mGeocodeProvider;
- private IGnssStatusProvider mGnssStatusProvider;
+ private GnssStatusListenerHelper mGnssStatusProvider;
private INetInitiatedListener mNetInitiatedListener;
private LocationWorkerHandler mLocationHandler;
private PassiveProvider mPassiveProvider; // track passive provider for special cases
@@ -463,14 +463,16 @@
}
for (Entry<IBinder, Identity> entry : mGnssMeasurementsListeners.entrySet()) {
- if (entry.getValue().mUid == uid) {
+ Identity callerIdentity = entry.getValue();
+ if (callerIdentity.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(
- IGnssMeasurementsListener.Stub.asInterface(entry.getKey()));
+ IGnssMeasurementsListener.Stub.asInterface(entry.getKey()),
+ callerIdentity.mUid, callerIdentity.mPackageName);
} else {
mGnssMeasurementsProvider.removeListener(
IGnssMeasurementsListener.Stub.asInterface(entry.getKey()));
@@ -479,7 +481,8 @@
}
for (Entry<IBinder, Identity> entry : mGnssNavigationMessageListeners.entrySet()) {
- if (entry.getValue().mUid == uid) {
+ Identity callerIdentity = entry.getValue();
+ if (callerIdentity.mUid == uid) {
if (D) {
Log.d(TAG, "gnss navigation message listener from uid "
+ uid + " is now "
@@ -487,13 +490,16 @@
}
if (foreground || isThrottlingExemptLocked(entry.getValue())) {
mGnssNavigationMessageProvider.addListener(
- IGnssNavigationMessageListener.Stub.asInterface(entry.getKey()));
+ IGnssNavigationMessageListener.Stub.asInterface(entry.getKey()),
+ callerIdentity.mUid, callerIdentity.mPackageName);
} else {
mGnssNavigationMessageProvider.removeListener(
IGnssNavigationMessageListener.Stub.asInterface(entry.getKey()));
}
}
}
+
+ // TODO(b/120449926): The GNSS status listeners should be handled similar to the above.
}
}
@@ -2444,31 +2450,20 @@
}
}
-
@Override
public boolean registerGnssStatusCallback(IGnssStatusListener callback, String packageName) {
if (!hasGnssPermissions(packageName) || mGnssStatusProvider == null) {
return false;
}
- try {
- mGnssStatusProvider.registerGnssStatusCallback(callback);
- } catch (RemoteException e) {
- Slog.e(TAG, "mGpsStatusProvider.registerGnssStatusCallback failed", e);
- return false;
- }
- return true;
+ // TODO(b/120449926): The GNSS status listeners should be handled similar to the GNSS
+ // measurements listeners.
+ return mGnssStatusProvider.addListener(callback, Binder.getCallingUid(), packageName);
}
@Override
public void unregisterGnssStatusCallback(IGnssStatusListener callback) {
- synchronized (mLock) {
- try {
- mGnssStatusProvider.unregisterGnssStatusCallback(callback);
- } catch (Exception e) {
- Slog.e(TAG, "mGpsStatusProvider.unregisterGnssStatusCallback failed", e);
- }
- }
+ mGnssStatusProvider.removeListener(callback);
}
@Override
@@ -2481,13 +2476,15 @@
synchronized (mLock) {
Identity callerIdentity
= new Identity(Binder.getCallingUid(), Binder.getCallingPid(), packageName);
+ // TODO(b/120481270): Register for client death notification and update map.
mGnssMeasurementsListeners.put(listener.asBinder(), callerIdentity);
long identity = Binder.clearCallingIdentity();
try {
if (isThrottlingExemptLocked(callerIdentity)
|| isImportanceForeground(
mActivityManager.getPackageImportance(packageName))) {
- return mGnssMeasurementsProvider.addListener(listener);
+ return mGnssMeasurementsProvider.addListener(listener,
+ callerIdentity.mUid, callerIdentity.mPackageName);
}
} finally {
Binder.restoreCallingIdentity(identity);
@@ -2518,13 +2515,15 @@
synchronized (mLock) {
Identity callerIdentity
= new Identity(Binder.getCallingUid(), Binder.getCallingPid(), packageName);
+ // TODO(b/120481270): Register for client death notification and update map.
mGnssNavigationMessageListeners.put(listener.asBinder(), callerIdentity);
long identity = Binder.clearCallingIdentity();
try {
if (isThrottlingExemptLocked(callerIdentity)
|| isImportanceForeground(
mActivityManager.getPackageImportance(packageName))) {
- return mGnssNavigationMessageProvider.addListener(listener);
+ return mGnssNavigationMessageProvider.addListener(listener,
+ callerIdentity.mUid, callerIdentity.mPackageName);
}
} finally {
Binder.restoreCallingIdentity(identity);
diff --git a/services/core/java/com/android/server/location/GnssLocationProvider.java b/services/core/java/com/android/server/location/GnssLocationProvider.java
index d5e4681..787a2d0 100644
--- a/services/core/java/com/android/server/location/GnssLocationProvider.java
+++ b/services/core/java/com/android/server/location/GnssLocationProvider.java
@@ -31,8 +31,6 @@
import android.location.GnssMeasurementsEvent;
import android.location.GnssNavigationMessage;
import android.location.GnssStatus;
-import android.location.IGnssStatusListener;
-import android.location.IGnssStatusProvider;
import android.location.IGpsGeofenceHardware;
import android.location.ILocationManager;
import android.location.INetInitiatedListener;
@@ -382,7 +380,7 @@
private final Context mContext;
private final ILocationManager mILocationManager;
private final LocationExtras mLocationExtras = new LocationExtras();
- private final GnssStatusListenerHelper mListenerHelper;
+ private final GnssStatusListenerHelper mGnssStatusListenerHelper;
private final GnssSatelliteBlacklistHelper mGnssSatelliteBlacklistHelper;
private final GnssMeasurementsProvider mGnssMeasurementsProvider;
private final GnssNavigationMessageProvider mGnssNavigationMessageProvider;
@@ -443,20 +441,8 @@
// GNSS Metrics
private GnssMetrics mGnssMetrics;
- private final IGnssStatusProvider mGnssStatusProvider = new IGnssStatusProvider.Stub() {
- @Override
- public void registerGnssStatusCallback(IGnssStatusListener callback) {
- mListenerHelper.addListener(callback);
- }
-
- @Override
- public void unregisterGnssStatusCallback(IGnssStatusListener callback) {
- mListenerHelper.removeListener(callback);
- }
- };
-
- public IGnssStatusProvider getGnssStatusProvider() {
- return mGnssStatusProvider;
+ public GnssStatusListenerHelper getGnssStatusProvider() {
+ return mGnssStatusListenerHelper;
}
public IGpsGeofenceHardware getGpsGeofenceProxy() {
@@ -730,7 +716,7 @@
mNetInitiatedListener,
mSuplEsEnabled);
- mListenerHelper = new GnssStatusListenerHelper(mHandler) {
+ mGnssStatusListenerHelper = new GnssStatusListenerHelper(mContext, mHandler) {
@Override
protected boolean isAvailableInPlatform() {
return isSupported();
@@ -749,7 +735,7 @@
}
};
- mGnssNavigationMessageProvider = new GnssNavigationMessageProvider(mHandler) {
+ mGnssNavigationMessageProvider = new GnssNavigationMessageProvider(mContext, mHandler) {
@Override
protected boolean isGpsEnabled() {
return isEnabled();
@@ -1473,7 +1459,7 @@
}
// notify status listeners
- mListenerHelper.onFirstFix(mTimeToFirstFix);
+ mGnssStatusListenerHelper.onFirstFix(mTimeToFirstFix);
}
if (mSingleShot) {
@@ -1527,7 +1513,7 @@
}
if (wasNavigating != mNavigating) {
- mListenerHelper.onStatusChanged(mNavigating);
+ mGnssStatusListenerHelper.onStatusChanged(mNavigating);
// send an intent to notify that the GPS has been enabled or disabled
Intent intent = new Intent(LocationManager.GPS_ENABLED_CHANGE_ACTION);
@@ -1563,7 +1549,7 @@
}
private void handleReportSvStatus(SvStatusInfo info) {
- mListenerHelper.onSvStatusChanged(
+ mGnssStatusListenerHelper.onSvStatusChanged(
info.mSvCount,
info.mSvidWithFlags,
info.mCn0s,
@@ -1636,7 +1622,7 @@
if (!mItarSpeedLimitExceeded) {
int length = native_read_nmea(mNmeaBuffer, mNmeaBuffer.length);
String nmea = new String(mNmeaBuffer, 0 /* offset */, length);
- mListenerHelper.onNmeaReceived(timestamp, nmea);
+ mGnssStatusListenerHelper.onNmeaReceived(timestamp, nmea);
}
}
diff --git a/services/core/java/com/android/server/location/GnssMeasurementsProvider.java b/services/core/java/com/android/server/location/GnssMeasurementsProvider.java
index 0add863..3e2ba87 100644
--- a/services/core/java/com/android/server/location/GnssMeasurementsProvider.java
+++ b/services/core/java/com/android/server/location/GnssMeasurementsProvider.java
@@ -38,7 +38,6 @@
private static final String TAG = "GnssMeasurementsProvider";
private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
- private final Context mContext;
private final GnssMeasurementProviderNative mNative;
private boolean mIsCollectionStarted;
@@ -51,8 +50,7 @@
@VisibleForTesting
GnssMeasurementsProvider(Context context, Handler handler,
GnssMeasurementProviderNative aNative) {
- super(handler, TAG);
- mContext = context;
+ super(context, handler, TAG);
mNative = aNative;
}
@@ -98,9 +96,13 @@
}
public void onMeasurementsAvailable(final GnssMeasurementsEvent event) {
- ListenerOperation<IGnssMeasurementsListener> operation =
- listener -> listener.onGnssMeasurementsReceived(event);
- foreach(operation);
+ foreach((IGnssMeasurementsListener listener, int uid, String packageName) -> {
+ if (!hasPermission(uid, packageName)) {
+ logPermissionDisabledEventNotReported(TAG, packageName, "GNSS measurements");
+ return;
+ }
+ listener.onGnssMeasurementsReceived(event);
+ });
}
public void onCapabilitiesUpdated(boolean isGnssMeasurementsSupported) {
@@ -149,7 +151,8 @@
}
@Override
- public void execute(IGnssMeasurementsListener listener) throws RemoteException {
+ public void execute(IGnssMeasurementsListener listener,
+ int uid, String packageName) throws RemoteException {
listener.onStatusChanged(mStatus);
}
}
diff --git a/services/core/java/com/android/server/location/GnssNavigationMessageProvider.java b/services/core/java/com/android/server/location/GnssNavigationMessageProvider.java
index 1b4fd18..679919f 100644
--- a/services/core/java/com/android/server/location/GnssNavigationMessageProvider.java
+++ b/services/core/java/com/android/server/location/GnssNavigationMessageProvider.java
@@ -16,6 +16,7 @@
package com.android.server.location;
+import android.content.Context;
import android.location.GnssNavigationMessage;
import android.location.IGnssNavigationMessageListener;
import android.os.Handler;
@@ -39,13 +40,14 @@
private final GnssNavigationMessageProviderNative mNative;
private boolean mCollectionStarted;
- protected GnssNavigationMessageProvider(Handler handler) {
- this(handler, new GnssNavigationMessageProviderNative());
+ protected GnssNavigationMessageProvider(Context context, Handler handler) {
+ this(context, handler, new GnssNavigationMessageProviderNative());
}
@VisibleForTesting
- GnssNavigationMessageProvider(Handler handler, GnssNavigationMessageProviderNative aNative) {
- super(handler, TAG);
+ GnssNavigationMessageProvider(Context context, Handler handler,
+ GnssNavigationMessageProviderNative aNative) {
+ super(context, handler, TAG);
mNative = aNative;
}
@@ -84,15 +86,10 @@
}
public void onNavigationMessageAvailable(final GnssNavigationMessage event) {
- ListenerOperation<IGnssNavigationMessageListener> operation =
- new ListenerOperation<IGnssNavigationMessageListener>() {
- @Override
- public void execute(IGnssNavigationMessageListener listener)
- throws RemoteException {
- listener.onGnssNavigationMessageReceived(event);
- }
- };
- foreach(operation);
+ foreach((IGnssNavigationMessageListener listener, int uid, String packageName) -> {
+ listener.onGnssNavigationMessageReceived(event);
+ }
+ );
}
public void onCapabilitiesUpdated(boolean isGnssNavigationMessageSupported) {
@@ -138,7 +135,8 @@
}
@Override
- public void execute(IGnssNavigationMessageListener listener) throws RemoteException {
+ public void execute(IGnssNavigationMessageListener listener,
+ int uid, String packageName) throws RemoteException {
listener.onStatusChanged(mStatus);
}
}
diff --git a/services/core/java/com/android/server/location/GnssStatusListenerHelper.java b/services/core/java/com/android/server/location/GnssStatusListenerHelper.java
index 124220f..454dbdd 100644
--- a/services/core/java/com/android/server/location/GnssStatusListenerHelper.java
+++ b/services/core/java/com/android/server/location/GnssStatusListenerHelper.java
@@ -16,16 +16,20 @@
package com.android.server.location;
+import android.content.Context;
import android.location.IGnssStatusListener;
import android.os.Handler;
-import android.os.RemoteException;
+import android.util.Log;
/**
* Implementation of a handler for {@link IGnssStatusListener}.
*/
-abstract class GnssStatusListenerHelper extends RemoteListenerHelper<IGnssStatusListener> {
- protected GnssStatusListenerHelper(Handler handler) {
- super(handler, "GnssStatusListenerHelper");
+public abstract class GnssStatusListenerHelper extends RemoteListenerHelper<IGnssStatusListener> {
+ private static final String TAG = "GnssStatusListenerHelper";
+ private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
+
+ protected GnssStatusListenerHelper(Context context, Handler handler) {
+ super(context, handler, TAG);
setSupported(GnssLocationProvider.isSupported());
}
@@ -43,33 +47,22 @@
}
public void onStatusChanged(boolean isNavigating) {
- Operation operation;
if (isNavigating) {
- operation = new Operation() {
- @Override
- public void execute(IGnssStatusListener listener) throws RemoteException {
- listener.onGnssStarted();
- }
- };
+ foreach((IGnssStatusListener listener, int uid, String packageName) -> {
+ listener.onGnssStarted();
+ });
} else {
- operation = new Operation() {
- @Override
- public void execute(IGnssStatusListener listener) throws RemoteException {
- listener.onGnssStopped();
- }
- };
+ foreach((IGnssStatusListener listener, int uid, String packageName) -> {
+ listener.onGnssStopped();
+ });
}
- foreach(operation);
}
public void onFirstFix(final int timeToFirstFix) {
- Operation operation = new Operation() {
- @Override
- public void execute(IGnssStatusListener listener) throws RemoteException {
- listener.onFirstFix(timeToFirstFix);
- }
- };
- foreach(operation);
+ foreach((IGnssStatusListener listener, int uid, String packageName) -> {
+ listener.onFirstFix(timeToFirstFix);
+ }
+ );
}
public void onSvStatusChanged(
@@ -79,30 +72,23 @@
final float[] elevations,
final float[] azimuths,
final float[] carrierFreqs) {
- Operation operation = new Operation() {
- @Override
- public void execute(IGnssStatusListener listener) throws RemoteException {
- listener.onSvStatusChanged(
- svCount,
- prnWithFlags,
- cn0s,
- elevations,
- azimuths,
- carrierFreqs);
+ foreach((IGnssStatusListener listener, int uid, String packageName) -> {
+ if (!hasPermission(uid, packageName)) {
+ logPermissionDisabledEventNotReported(TAG, packageName, "GNSS status");
+ return;
}
- };
- foreach(operation);
+ listener.onSvStatusChanged(svCount, prnWithFlags, cn0s, elevations, azimuths,
+ carrierFreqs);
+ });
}
public void onNmeaReceived(final long timestamp, final String nmea) {
- Operation operation = new Operation() {
- @Override
- public void execute(IGnssStatusListener listener) throws RemoteException {
- listener.onNmeaReceived(timestamp, nmea);
+ foreach((IGnssStatusListener listener, int uid, String packageName) -> {
+ if (!hasPermission(uid, packageName)) {
+ logPermissionDisabledEventNotReported(TAG, packageName, "NMEA");
+ return;
}
- };
- foreach(operation);
+ listener.onNmeaReceived(timestamp, nmea);
+ });
}
-
- private interface Operation extends ListenerOperation<IGnssStatusListener> {}
}
diff --git a/services/core/java/com/android/server/location/RemoteListenerHelper.java b/services/core/java/com/android/server/location/RemoteListenerHelper.java
index fcdb9d1..37d43fc 100644
--- a/services/core/java/com/android/server/location/RemoteListenerHelper.java
+++ b/services/core/java/com/android/server/location/RemoteListenerHelper.java
@@ -17,6 +17,8 @@
package com.android.server.location;
import android.annotation.NonNull;
+import android.app.AppOpsManager;
+import android.content.Context;
import android.os.Handler;
import android.os.IBinder;
import android.os.IInterface;
@@ -46,6 +48,9 @@
private final Map<IBinder, LinkedListener> mListenerMap = new HashMap<>();
+ protected final Context mContext;
+ protected final AppOpsManager mAppOps;
+
private volatile boolean mIsRegistered; // must access only on handler thread, or read-only
private boolean mHasIsSupported;
@@ -53,10 +58,12 @@
private int mLastReportedResult = RESULT_UNKNOWN;
- protected RemoteListenerHelper(Handler handler, String name) {
+ protected RemoteListenerHelper(Context context, Handler handler, String name) {
Preconditions.checkNotNull(name);
mHandler = handler;
mTag = name;
+ mContext = context;
+ mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
}
// read-only access for a dump() thread assured via volatile
@@ -64,10 +71,10 @@
return mIsRegistered;
}
- public boolean addListener(@NonNull TListener listener) {
+ public boolean addListener(@NonNull TListener listener, int uid, String packageName) {
Preconditions.checkNotNull(listener, "Attempted to register a 'null' listener.");
IBinder binder = listener.asBinder();
- LinkedListener deathListener = new LinkedListener(listener);
+ LinkedListener deathListener = new LinkedListener(listener, uid, packageName);
synchronized (mListenerMap) {
if (mListenerMap.containsKey(binder)) {
// listener already added
@@ -102,7 +109,7 @@
// asynchronously in the future
return true;
}
- post(listener, getHandlerOperation(result));
+ post(deathListener, getHandlerOperation(result));
}
return true;
}
@@ -130,7 +137,7 @@
protected abstract ListenerOperation<TListener> getHandlerOperation(int result);
protected interface ListenerOperation<TListener extends IInterface> {
- void execute(TListener listener) throws RemoteException;
+ void execute(TListener listener, int uid, String packageName) throws RemoteException;
}
protected void foreach(ListenerOperation<TListener> operation) {
@@ -170,15 +177,28 @@
}
}
- private void foreachUnsafe(ListenerOperation<TListener> operation) {
- for (LinkedListener linkedListener : mListenerMap.values()) {
- post(linkedListener.getUnderlyingListener(), operation);
+ protected boolean hasPermission(int uid, String packageName) {
+ return mAppOps.noteOpNoThrow(AppOpsManager.OP_FINE_LOCATION, uid, packageName)
+ == AppOpsManager.MODE_ALLOWED;
+ }
+
+ protected void logPermissionDisabledEventNotReported(String tag, String packageName,
+ String event) {
+ if (Log.isLoggable(tag, Log.DEBUG)) {
+ Log.d(tag, "Location permission disabled. Skipping " + event + " reporting for app: "
+ + packageName);
}
}
- private void post(TListener listener, ListenerOperation<TListener> operation) {
+ private void foreachUnsafe(ListenerOperation<TListener> operation) {
+ for (LinkedListener linkedListener : mListenerMap.values()) {
+ post(linkedListener, operation);
+ }
+ }
+
+ private void post(LinkedListener linkedListener, ListenerOperation<TListener> operation) {
if (operation != null) {
- mHandler.post(new HandlerRunnable(listener, operation));
+ mHandler.post(new HandlerRunnable(linkedListener, operation));
}
}
@@ -193,13 +213,9 @@
}
if (!mIsRegistered) {
// post back a failure
- mHandler.post(new Runnable() {
- @Override
- public void run() {
- synchronized (mListenerMap) {
- ListenerOperation<TListener> operation = getHandlerOperation(registrationState);
- foreachUnsafe(operation);
- }
+ mHandler.post(() -> {
+ synchronized (mListenerMap) {
+ foreachUnsafe(getHandlerOperation(registrationState));
}
});
}
@@ -208,16 +224,14 @@
}
private void tryUnregister() {
- mHandler.post(new Runnable() {
- @Override
- public void run() {
- if (!mIsRegistered) {
- return;
+ mHandler.post(() -> {
+ if (!mIsRegistered) {
+ return;
+ }
+ unregisterFromService();
+ mIsRegistered = false;
}
- unregisterFromService();
- mIsRegistered = false;
- }
- });
+ );
}
private int calculateCurrentResultUnsafe() {
@@ -240,14 +254,13 @@
private class LinkedListener implements IBinder.DeathRecipient {
private final TListener mListener;
+ private final int mUid;
+ private final String mPackageName;
- public LinkedListener(@NonNull TListener listener) {
+ LinkedListener(@NonNull TListener listener, int uid, String packageName) {
mListener = listener;
- }
-
- @NonNull
- public TListener getUnderlyingListener() {
- return mListener;
+ mUid = uid;
+ mPackageName = packageName;
}
@Override
@@ -258,18 +271,19 @@
}
private class HandlerRunnable implements Runnable {
- private final TListener mListener;
+ private final LinkedListener mLinkedListener;
private final ListenerOperation<TListener> mOperation;
- public HandlerRunnable(TListener listener, ListenerOperation<TListener> operation) {
- mListener = listener;
+ HandlerRunnable(LinkedListener linkedListener, ListenerOperation<TListener> operation) {
+ mLinkedListener = linkedListener;
mOperation = operation;
}
@Override
public void run() {
try {
- mOperation.execute(mListener);
+ mOperation.execute(mLinkedListener.mListener, mLinkedListener.mUid,
+ mLinkedListener.mPackageName);
} catch (RemoteException e) {
Log.v(mTag, "Error in monitored listener.", e);
}