Skip to content

Commit 1f1f597

Browse files
Victoria LeaseAndroid (Google) Code Review
authored andcommitted
Merge "Require ACCESS_FINE_LOCATION for Geofence use." into jb-mr1-dev
2 parents 45a0b90 + 4fab68b commit 1f1f597

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

location/java/android/location/LocationManager.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,12 @@ public void removeUpdates(PendingIntent intent) {
994994
* <p> Internally, this method uses both {@link #NETWORK_PROVIDER}
995995
* and {@link #GPS_PROVIDER}.
996996
*
997+
* <p>Before API version 17, this method could be used with
998+
* {@link android.Manifest.permission#ACCESS_FINE_LOCATION} or
999+
* {@link android.Manifest.permission#ACCESS_COARSE_LOCATION}.
1000+
* From API version 17 and onwards, this method requires
1001+
* {@link android.Manifest.permission#ACCESS_FINE_LOCATION} permission.
1002+
*
9971003
* @param latitude the latitude of the central point of the
9981004
* alert region
9991005
* @param longitude the longitude of the central point of the
@@ -1005,7 +1011,8 @@ public void removeUpdates(PendingIntent intent) {
10051011
* @param intent a PendingIntent that will be used to generate an Intent to
10061012
* fire when entry to or exit from the alert region is detected
10071013
*
1008-
* @throws SecurityException if no suitable permission is present
1014+
* @throws SecurityException if {@link android.Manifest.permission#ACCESS_FINE_LOCATION}
1015+
* permission is not present
10091016
*
10101017
* @deprecated Use {@link LocationRequest} and {@link Geofence} instead
10111018
*/
@@ -1055,7 +1062,8 @@ public void addProximityAlert(double latitude, double longitude, float radius, l
10551062
*
10561063
* @throws IllegalArgumentException if fence is null
10571064
* @throws IllegalArgumentException if intent is null
1058-
* @throws SecurityException if no suitable permission is present
1065+
* @throws SecurityException if {@link android.Manifest.permission#ACCESS_FINE_LOCATION}
1066+
* permission is not present
10591067
*/
10601068
public void addGeofence(LocationRequest request, Geofence fence, PendingIntent intent) {
10611069
checkPendingIntent(intent);
@@ -1071,11 +1079,18 @@ public void addGeofence(LocationRequest request, Geofence fence, PendingIntent i
10711079
/**
10721080
* Removes the proximity alert with the given PendingIntent.
10731081
*
1082+
* <p>Before API version 17, this method could be used with
1083+
* {@link android.Manifest.permission#ACCESS_FINE_LOCATION} or
1084+
* {@link android.Manifest.permission#ACCESS_COARSE_LOCATION}.
1085+
* From API version 17 and onwards, this method requires
1086+
* {@link android.Manifest.permission#ACCESS_FINE_LOCATION} permission.
1087+
*
10741088
* @param intent the PendingIntent that no longer needs to be notified of
10751089
* proximity alerts
10761090
*
10771091
* @throws IllegalArgumentException if intent is null
1078-
* @throws SecurityException if no suitable permission is present
1092+
* @throws SecurityException if {@link android.Manifest.permission#ACCESS_FINE_LOCATION}
1093+
* permission is not present
10791094
*
10801095
* @deprecated Use {@link LocationRequest} and {@link Geofence} instead
10811096
*/
@@ -1102,7 +1117,8 @@ public void removeProximityAlert(PendingIntent intent) {
11021117
*
11031118
* @throws IllegalArgumentException if fence is null
11041119
* @throws IllegalArgumentException if intent is null
1105-
* @throws SecurityException if no suitable permission is present
1120+
* @throws SecurityException if {@link android.Manifest.permission#ACCESS_FINE_LOCATION}
1121+
* permission is not present
11061122
*/
11071123
public void removeGeofence(Geofence fence, PendingIntent intent) {
11081124
checkPendingIntent(intent);
@@ -1122,7 +1138,8 @@ public void removeGeofence(Geofence fence, PendingIntent intent) {
11221138
* @param intent a pending intent previously passed to {@link #addGeofence}
11231139
*
11241140
* @throws IllegalArgumentException if intent is null
1125-
* @throws SecurityException if no suitable permission is present
1141+
* @throws SecurityException if {@link android.Manifest.permission#ACCESS_FINE_LOCATION}
1142+
* permission is not present
11261143
*/
11271144
public void removeAllGeofences(PendingIntent intent) {
11281145
checkPendingIntent(intent);

services/java/com/android/server/LocationManagerService.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,17 @@ private String checkPermission() {
588588
}
589589

590590
throw new SecurityException("Location requires either ACCESS_COARSE_LOCATION or" +
591-
"ACCESS_FINE_LOCATION permission");
591+
" ACCESS_FINE_LOCATION permission");
592+
}
593+
594+
/**
595+
* Throw SecurityException if caller lacks permission to use Geofences.
596+
*/
597+
private void checkGeofencePermission() {
598+
if (mContext.checkCallingOrSelfPermission(ACCESS_FINE_LOCATION) !=
599+
PackageManager.PERMISSION_GRANTED) {
600+
throw new SecurityException("Geofence usage requires ACCESS_FINE_LOCATION permission");
601+
}
592602
}
593603

594604
/**
@@ -1096,6 +1106,7 @@ public Location getLastLocation(LocationRequest request, String packageName) {
10961106
public void requestGeofence(LocationRequest request, Geofence geofence, PendingIntent intent,
10971107
String packageName) {
10981108
if (request == null) request = DEFAULT_LOCATION_REQUEST;
1109+
checkGeofencePermission();
10991110
checkPermissionAndRequest(request);
11001111
checkPendingIntent(intent);
11011112
checkPackageName(packageName);
@@ -1114,7 +1125,7 @@ public void requestGeofence(LocationRequest request, Geofence geofence, PendingI
11141125

11151126
@Override
11161127
public void removeGeofence(Geofence geofence, PendingIntent intent, String packageName) {
1117-
checkPermission();
1128+
checkGeofencePermission();
11181129
checkPendingIntent(intent);
11191130
checkPackageName(packageName);
11201131

0 commit comments

Comments
 (0)