Merge "Make LocationAccessQuery's builder safer to use" am: a863b0297b am: 524b0aaec6
Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1695246 Change-Id: I3f9226521f344345a0b1021748ee073bb38b020e
This commit is contained in:
@@ -2873,6 +2873,8 @@ public class TelephonyRegistry extends ITelephonyRegistry.Stub {
|
|||||||
// If we're enforcing fine starting in Q, we also want to enforce coarse even for
|
// If we're enforcing fine starting in Q, we also want to enforce coarse even for
|
||||||
// older SDK versions.
|
// older SDK versions.
|
||||||
locationQueryBuilder.setMinSdkVersionForCoarse(0);
|
locationQueryBuilder.setMinSdkVersionForCoarse(0);
|
||||||
|
locationQueryBuilder.setMinSdkVersionForCoarse(0);
|
||||||
|
locationQueryBuilder.setMinSdkVersionForEnforcement(0);
|
||||||
shouldCheckLocationPermissions = true;
|
shouldCheckLocationPermissions = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3001,6 +3003,14 @@ public class TelephonyRegistry extends ITelephonyRegistry.Stub {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean checkFineLocationAccess(Record r) {
|
||||||
|
return checkFineLocationAccess(r, Build.VERSION_CODES.BASE);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean checkCoarseLocationAccess(Record r) {
|
||||||
|
return checkCoarseLocationAccess(r, Build.VERSION_CODES.BASE);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Note -- this method should only be used at the site of a permission check if you need to
|
* Note -- this method should only be used at the site of a permission check if you need to
|
||||||
* explicitly allow apps below a certain SDK level access regardless of location permissions.
|
* explicitly allow apps below a certain SDK level access regardless of location permissions.
|
||||||
@@ -3016,6 +3026,8 @@ public class TelephonyRegistry extends ITelephonyRegistry.Stub {
|
|||||||
.setMethod("TelephonyRegistry push")
|
.setMethod("TelephonyRegistry push")
|
||||||
.setLogAsInfo(true) // we don't need to log an error every time we push
|
.setLogAsInfo(true) // we don't need to log an error every time we push
|
||||||
.setMinSdkVersionForFine(minSdk)
|
.setMinSdkVersionForFine(minSdk)
|
||||||
|
.setMinSdkVersionForCoarse(minSdk)
|
||||||
|
.setMinSdkVersionForEnforcement(minSdk)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
return Binder.withCleanCallingIdentity(() -> {
|
return Binder.withCleanCallingIdentity(() -> {
|
||||||
@@ -3040,6 +3052,8 @@ public class TelephonyRegistry extends ITelephonyRegistry.Stub {
|
|||||||
.setMethod("TelephonyRegistry push")
|
.setMethod("TelephonyRegistry push")
|
||||||
.setLogAsInfo(true) // we don't need to log an error every time we push
|
.setLogAsInfo(true) // we don't need to log an error every time we push
|
||||||
.setMinSdkVersionForCoarse(minSdk)
|
.setMinSdkVersionForCoarse(minSdk)
|
||||||
|
.setMinSdkVersionForFine(Integer.MAX_VALUE)
|
||||||
|
.setMinSdkVersionForEnforcement(minSdk)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
return Binder.withCleanCallingIdentity(() -> {
|
return Binder.withCleanCallingIdentity(() -> {
|
||||||
|
|||||||
@@ -86,8 +86,9 @@ public final class LocationAccessPolicy {
|
|||||||
private String mCallingFeatureId;
|
private String mCallingFeatureId;
|
||||||
private int mCallingUid;
|
private int mCallingUid;
|
||||||
private int mCallingPid;
|
private int mCallingPid;
|
||||||
private int mMinSdkVersionForCoarse = Integer.MAX_VALUE;
|
private int mMinSdkVersionForCoarse = -1;
|
||||||
private int mMinSdkVersionForFine = Integer.MAX_VALUE;
|
private int mMinSdkVersionForFine = -1;
|
||||||
|
private int mMinSdkVersionForEnforcement = -1;
|
||||||
private boolean mLogAsInfo = false;
|
private boolean mLogAsInfo = false;
|
||||||
private String mMethod;
|
private String mMethod;
|
||||||
|
|
||||||
@@ -125,7 +126,14 @@ public final class LocationAccessPolicy {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Apps that target at least this sdk version will be checked for coarse location
|
* Apps that target at least this sdk version will be checked for coarse location
|
||||||
* permission. Defaults to INT_MAX (which means don't check)
|
* permission. This method MUST be called before calling {@link #build()}. Otherwise, an
|
||||||
|
* {@link IllegalArgumentException} will be thrown.
|
||||||
|
*
|
||||||
|
* Additionally, if both the argument to this method and
|
||||||
|
* {@link #setMinSdkVersionForFine} are greater than {@link Build.VERSION_CODES#BASE},
|
||||||
|
* you must call {@link #setMinSdkVersionForEnforcement} with the min of the two to
|
||||||
|
* affirm that you do not want any location checks below a certain SDK version.
|
||||||
|
* Otherwise, {@link #build} will throw an {@link IllegalArgumentException}.
|
||||||
*/
|
*/
|
||||||
public Builder setMinSdkVersionForCoarse(
|
public Builder setMinSdkVersionForCoarse(
|
||||||
int minSdkVersionForCoarse) {
|
int minSdkVersionForCoarse) {
|
||||||
@@ -135,7 +143,14 @@ public final class LocationAccessPolicy {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Apps that target at least this sdk version will be checked for fine location
|
* Apps that target at least this sdk version will be checked for fine location
|
||||||
* permission. Defaults to INT_MAX (which means don't check)
|
* permission. This method MUST be called before calling {@link #build()}.
|
||||||
|
* Otherwise, an {@link IllegalArgumentException} will be thrown.
|
||||||
|
*
|
||||||
|
* Additionally, if both the argument to this method and
|
||||||
|
* {@link #setMinSdkVersionForCoarse} are greater than {@link Build.VERSION_CODES#BASE},
|
||||||
|
* you must call {@link #setMinSdkVersionForEnforcement} with the min of the two to
|
||||||
|
* affirm that you do not want any location checks below a certain SDK version.
|
||||||
|
* Otherwise, {@link #build} will throw an {@link IllegalArgumentException}.
|
||||||
*/
|
*/
|
||||||
public Builder setMinSdkVersionForFine(
|
public Builder setMinSdkVersionForFine(
|
||||||
int minSdkVersionForFine) {
|
int minSdkVersionForFine) {
|
||||||
@@ -143,6 +158,17 @@ public final class LocationAccessPolicy {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If both the argument to {@link #setMinSdkVersionForFine} and
|
||||||
|
* {@link #setMinSdkVersionForCoarse} are greater than {@link Build.VERSION_CODES#BASE},
|
||||||
|
* this method must be called with the min of the two to
|
||||||
|
* affirm that you do not want any location checks below a certain SDK version.
|
||||||
|
*/
|
||||||
|
public Builder setMinSdkVersionForEnforcement(int minSdkVersionForEnforcement) {
|
||||||
|
mMinSdkVersionForEnforcement = minSdkVersionForEnforcement;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Optional, for logging purposes only.
|
* Optional, for logging purposes only.
|
||||||
*/
|
*/
|
||||||
@@ -161,6 +187,26 @@ public final class LocationAccessPolicy {
|
|||||||
|
|
||||||
/** build LocationPermissionQuery */
|
/** build LocationPermissionQuery */
|
||||||
public LocationPermissionQuery build() {
|
public LocationPermissionQuery build() {
|
||||||
|
if (mMinSdkVersionForCoarse < 0 || mMinSdkVersionForFine < 0) {
|
||||||
|
throw new IllegalArgumentException("Must specify min sdk versions for"
|
||||||
|
+ " enforcement for both coarse and fine permissions");
|
||||||
|
}
|
||||||
|
if (mMinSdkVersionForFine > Build.VERSION_CODES.BASE
|
||||||
|
&& mMinSdkVersionForCoarse > Build.VERSION_CODES.BASE) {
|
||||||
|
if (mMinSdkVersionForEnforcement != Math.min(
|
||||||
|
mMinSdkVersionForCoarse, mMinSdkVersionForFine)) {
|
||||||
|
throw new IllegalArgumentException("setMinSdkVersionForEnforcement must be"
|
||||||
|
+ " called.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mMinSdkVersionForFine < mMinSdkVersionForCoarse) {
|
||||||
|
throw new IllegalArgumentException("Since fine location permission includes"
|
||||||
|
+ " access to coarse location, the min sdk level for enforcement of"
|
||||||
|
+ " the fine location permission must not be less than the min sdk"
|
||||||
|
+ " level for enforcement of the coarse location permission.");
|
||||||
|
}
|
||||||
|
|
||||||
return new LocationPermissionQuery(mCallingPackage, mCallingFeatureId,
|
return new LocationPermissionQuery(mCallingPackage, mCallingFeatureId,
|
||||||
mCallingUid, mCallingPid, mMinSdkVersionForCoarse, mMinSdkVersionForFine,
|
mCallingUid, mCallingPid, mMinSdkVersionForCoarse, mMinSdkVersionForFine,
|
||||||
mLogAsInfo, mMethod);
|
mLogAsInfo, mMethod);
|
||||||
|
|||||||
Reference in New Issue
Block a user