@@ -252,10 +252,10 @@ public String toString() {
252252
253253 private PreciseDataConnectionState [] mPreciseDataConnectionState ;
254254
255- // Nothing here yet, but putting it here in case we want to add more in the future .
256- static final int ENFORCE_COARSE_LOCATION_PERMISSION_MASK = 0 ;
257-
258- static final int ENFORCE_FINE_LOCATION_PERMISSION_MASK =
255+ // Starting in Q, almost all cellular location requires FINE location enforcement .
256+ // Prior to Q, cellular was available with COARSE location enforcement. Bits in this
257+ // list will be checked for COARSE on apps targeting P or earlier and FINE on Q or later.
258+ static final int ENFORCE_LOCATION_PERMISSION_MASK =
259259 PhoneStateListener .LISTEN_CELL_LOCATION
260260 | PhoneStateListener .LISTEN_CELL_INFO ;
261261
@@ -293,7 +293,7 @@ public void handleMessage(Message msg) {
293293 + newDefaultSubId + " newDefaultPhoneId=" + newDefaultPhoneId );
294294 }
295295
296- //Due to possible risk condition,(notify call back using the new
296+ //Due to possible race condition,(notify call back using the new
297297 //defaultSubId comes before new defaultSubId update) we need to recall all
298298 //possible missed notify callback
299299 synchronized (mRecords ) {
@@ -715,11 +715,11 @@ private void listen(String callingPackage, IPhoneStateListener callback, int eve
715715 }
716716 if (validateEventsAndUserLocked (r , PhoneStateListener .LISTEN_CELL_LOCATION )) {
717717 try {
718- if (DBG_LOC ) log ("listen: mCellLocation = "
719- + mCellLocation [ phoneId ]);
720- if ( checkFineLocationAccess (r , Build .VERSION_CODES .Q )) {
721- r . callback . onCellLocationChanged (
722- new Bundle (mCellLocation [phoneId ]) );
718+ if (DBG_LOC ) log ("listen: mCellLocation= " + mCellLocation [ phoneId ]);
719+ if ( checkCoarseLocationAccess ( r , Build . VERSION_CODES . BASE )
720+ && checkFineLocationAccess (r , Build .VERSION_CODES .Q )) {
721+ // null will be translated to empty CellLocation object in client.
722+ r . callback . onCellLocationChanged (mCellLocation [phoneId ]);
723723 }
724724 } catch (RemoteException ex ) {
725725 remove (r .binder );
@@ -766,7 +766,8 @@ private void listen(String callingPackage, IPhoneStateListener callback, int eve
766766 try {
767767 if (DBG_LOC ) log ("listen: mCellInfo[" + phoneId + "] = "
768768 + mCellInfo .get (phoneId ));
769- if (checkFineLocationAccess (r , Build .VERSION_CODES .Q )) {
769+ if (checkCoarseLocationAccess (r , Build .VERSION_CODES .BASE )
770+ && checkFineLocationAccess (r , Build .VERSION_CODES .Q )) {
770771 r .callback .onCellInfoChanged (mCellInfo .get (phoneId ));
771772 }
772773 } catch (RemoteException ex ) {
@@ -1267,7 +1268,8 @@ public void notifyCellInfoForSubscriber(int subId, List<CellInfo> cellInfo) {
12671268 for (Record r : mRecords ) {
12681269 if (validateEventsAndUserLocked (r , PhoneStateListener .LISTEN_CELL_INFO ) &&
12691270 idMatch (r .subId , subId , phoneId ) &&
1270- checkFineLocationAccess (r , Build .VERSION_CODES .Q )) {
1271+ (checkCoarseLocationAccess (r , Build .VERSION_CODES .BASE )
1272+ && checkFineLocationAccess (r , Build .VERSION_CODES .Q ))) {
12711273 try {
12721274 if (DBG_LOC ) {
12731275 log ("notifyCellInfo: mCellInfo=" + cellInfo + " r=" + r );
@@ -1578,7 +1580,8 @@ public void notifyCellLocationForSubscriber(int subId, Bundle cellLocation) {
15781580 for (Record r : mRecords ) {
15791581 if (validateEventsAndUserLocked (r , PhoneStateListener .LISTEN_CELL_LOCATION ) &&
15801582 idMatch (r .subId , subId , phoneId ) &&
1581- checkFineLocationAccess (r , Build .VERSION_CODES .Q )) {
1583+ (checkCoarseLocationAccess (r , Build .VERSION_CODES .BASE )
1584+ && checkFineLocationAccess (r , Build .VERSION_CODES .Q ))) {
15821585 try {
15831586 if (DBG_LOC ) {
15841587 log ("notifyCellLocation: cellLocation=" + cellLocation
@@ -2247,19 +2250,13 @@ private boolean checkListenerPermission(
22472250 .setCallingPid (Binder .getCallingPid ())
22482251 .setCallingUid (Binder .getCallingUid ());
22492252
2250- boolean shouldCheckLocationPermissions = false ;
2251- if ((events & ENFORCE_COARSE_LOCATION_PERMISSION_MASK ) != 0 ) {
2252- locationQueryBuilder .setMinSdkVersionForCoarse (0 );
2253- shouldCheckLocationPermissions = true ;
2254- }
2255-
2256- if ((events & ENFORCE_FINE_LOCATION_PERMISSION_MASK ) != 0 ) {
2253+ if ((events & ENFORCE_LOCATION_PERMISSION_MASK ) != 0 ) {
22572254 // Everything that requires fine location started in Q. So far...
22582255 locationQueryBuilder .setMinSdkVersionForFine (Build .VERSION_CODES .Q );
2259- shouldCheckLocationPermissions = true ;
2260- }
2256+ // If we're enforcing fine starting in Q, we also want to enforce coarse even for
2257+ // older SDK versions.
2258+ locationQueryBuilder .setMinSdkVersionForCoarse (0 );
22612259
2262- if (shouldCheckLocationPermissions ) {
22632260 LocationAccessPolicy .LocationPermissionResult result =
22642261 LocationAccessPolicy .checkLocationPermission (
22652262 mContext , locationQueryBuilder .build ());
@@ -2422,8 +2419,16 @@ private void checkPossibleMissNotify(Record r, int phoneId) {
24222419 try {
24232420 if (VDBG ) log ("checkPossibleMissNotify: onServiceStateChanged state=" +
24242421 mServiceState [phoneId ]);
2425- r .callback .onServiceStateChanged (
2426- new ServiceState (mServiceState [phoneId ]));
2422+ ServiceState ss = new ServiceState (mServiceState [phoneId ]);
2423+ if (checkFineLocationAccess (r , Build .VERSION_CODES .Q )) {
2424+ r .callback .onServiceStateChanged (ss );
2425+ } else if (checkCoarseLocationAccess (r , Build .VERSION_CODES .Q )) {
2426+ r .callback .onServiceStateChanged (
2427+ ss .sanitizeLocationInfo (false ));
2428+ } else {
2429+ r .callback .onServiceStateChanged (
2430+ ss .sanitizeLocationInfo (true ));
2431+ }
24272432 } catch (RemoteException ex ) {
24282433 mRemoveList .add (r .binder );
24292434 }
@@ -2462,7 +2467,8 @@ private void checkPossibleMissNotify(Record r, int phoneId) {
24622467 log ("checkPossibleMissNotify: onCellInfoChanged[" + phoneId + "] = "
24632468 + mCellInfo .get (phoneId ));
24642469 }
2465- if (checkFineLocationAccess (r , Build .VERSION_CODES .Q )) {
2470+ if (checkCoarseLocationAccess (r , Build .VERSION_CODES .BASE )
2471+ && checkFineLocationAccess (r , Build .VERSION_CODES .Q )) {
24662472 r .callback .onCellInfoChanged (mCellInfo .get (phoneId ));
24672473 }
24682474 } catch (RemoteException ex ) {
@@ -2510,10 +2516,14 @@ private void checkPossibleMissNotify(Record r, int phoneId) {
25102516
25112517 if (validateEventsAndUserLocked (r , PhoneStateListener .LISTEN_CELL_LOCATION )) {
25122518 try {
2513- if (DBG_LOC ) log ("checkPossibleMissNotify: onCellLocationChanged mCellLocation = "
2514- + mCellLocation [phoneId ]);
2515- if (checkFineLocationAccess (r , Build .VERSION_CODES .Q )) {
2516- r .callback .onCellLocationChanged (new Bundle (mCellLocation [phoneId ]));
2519+ if (DBG_LOC ) {
2520+ log ("checkPossibleMissNotify: onCellLocationChanged mCellLocation= "
2521+ + mCellLocation [phoneId ]);
2522+ }
2523+ if (checkCoarseLocationAccess (r , Build .VERSION_CODES .BASE )
2524+ && checkFineLocationAccess (r , Build .VERSION_CODES .Q )) {
2525+ // null will be translated to empty CellLocation object in client.
2526+ r .callback .onCellLocationChanged (mCellLocation [phoneId ]);
25172527 }
25182528 } catch (RemoteException ex ) {
25192529 mRemoveList .add (r .binder );
0 commit comments