Refinement of ContextParams behavior.

Since this parameters object is likely going to grow over time, we
should offer a way for callers to easily "inherit" all sub-parameters
by default, instead of making them copy the values manually.

This also gives a path for legacy apps to automatically preserve any
new sub-parameters that have been added in more modern API levels.

This change also relaxes the nullability annotations to match the
existing createAttributionContext() behavior, while also supporting
clearing of specific inherited values.

Bug: 181812281
Test: atest CtsContentTestCases:android.content.cts.ContextTest
Change-Id: Ic797e1aef578926b04ea0000ca3a1bf8684183b9
This commit is contained in:
Jeff Sharkey
2021-03-08 18:01:05 -07:00
parent 479d90cefd
commit 29e882b417
4 changed files with 41 additions and 7 deletions

View File

@@ -119,6 +119,37 @@ public final class ContextParams {
private String mReceiverAttributionTag;
private Set<String> mRenouncedPermissions;
/**
* Create a new builder.
* <p>
* This is valuable when you are interested in having explicit control
* over every sub-parameter, and don't want to inherit any values from
* an existing Context.
* <p>
* Developers should strongly consider using
* {@link #Builder(ContextParams)} instead of this constructor, since
* that will will automatically inherit any new sub-parameters added in
* future platform releases.
*/
public Builder() {
}
/**
* Create a new builder that inherits all sub-parameters by default.
* <p>
* This is valuable when you are only interested in overriding specific
* sub-parameters, and want to preserve all other parameters. Setting a
* specific sub-parameter on the returned builder will override any
* inherited value.
*/
public Builder(@NonNull ContextParams params) {
Objects.requireNonNull(params);
mAttributionTag = params.mAttributionTag;
mReceiverPackage = params.mReceiverPackage;
mReceiverAttributionTag = params.mReceiverAttributionTag;
mRenouncedPermissions = params.mRenouncedPermissions;
}
/**
* Sets an attribution tag against which to track permission accesses.
*
@@ -126,7 +157,7 @@ public final class ContextParams {
* @return This builder.
*/
@NonNull
public Builder setAttributionTag(@NonNull String attributionTag) {
public Builder setAttributionTag(@Nullable String attributionTag) {
mAttributionTag = attributionTag;
return this;
}
@@ -140,7 +171,7 @@ public final class ContextParams {
* @return This builder.
*/
@NonNull
public Builder setReceiverPackage(@NonNull String packageName,
public Builder setReceiverPackage(@Nullable String packageName,
@Nullable String attributionTag) {
mReceiverPackage = packageName;
mReceiverAttributionTag = attributionTag;
@@ -169,7 +200,8 @@ public final class ContextParams {
*/
@SystemApi
@RequiresPermission(android.Manifest.permission.RENOUNCE_PERMISSIONS)
public @NonNull Builder setRenouncedPermissions(@NonNull Set<String> renouncedPermissions) {
public @NonNull Builder setRenouncedPermissions(
@Nullable Set<String> renouncedPermissions) {
if (renouncedPermissions != null) {
mRenouncedPermissions = Collections.unmodifiableSet(renouncedPermissions);
} else {