Merge "Apply delivery group policies to SERVICE_STATE broadcast." into udc-dev

This commit is contained in:
Sudheer Shanka
2023-03-11 20:43:28 +00:00
committed by Android (Google) Code Review
4 changed files with 50 additions and 13 deletions

View File

@@ -1299,7 +1299,7 @@ class ContextImpl extends Context {
@Override @Override
public void sendBroadcastMultiplePermissions(Intent intent, String[] receiverPermissions, public void sendBroadcastMultiplePermissions(Intent intent, String[] receiverPermissions,
String[] excludedPermissions, String[] excludedPackages) { String[] excludedPermissions, String[] excludedPackages, BroadcastOptions options) {
warnIfCallingFromSystemProcess(); warnIfCallingFromSystemProcess();
String resolvedType = intent.resolveTypeIfNeeded(getContentResolver()); String resolvedType = intent.resolveTypeIfNeeded(getContentResolver());
try { try {
@@ -1307,7 +1307,8 @@ class ContextImpl extends Context {
ActivityManager.getService().broadcastIntentWithFeature( ActivityManager.getService().broadcastIntentWithFeature(
mMainThread.getApplicationThread(), getAttributionTag(), intent, resolvedType, mMainThread.getApplicationThread(), getAttributionTag(), intent, resolvedType,
null, Activity.RESULT_OK, null, null, receiverPermissions, excludedPermissions, null, Activity.RESULT_OK, null, null, receiverPermissions, excludedPermissions,
excludedPackages, AppOpsManager.OP_NONE, null, false, false, getUserId()); excludedPackages, AppOpsManager.OP_NONE,
options == null ? null : options.toBundle(), false, false, getUserId());
} catch (RemoteException e) { } catch (RemoteException e) {
throw e.rethrowFromSystemServer(); throw e.rethrowFromSystemServer();
} }

View File

@@ -2399,7 +2399,6 @@ public abstract class Context {
sendBroadcastMultiplePermissions(intent, receiverPermissions, excludedPermissions, null); sendBroadcastMultiplePermissions(intent, receiverPermissions, excludedPermissions, null);
} }
/** /**
* Like {@link #sendBroadcastMultiplePermissions(Intent, String[], String[])}, but also allows * Like {@link #sendBroadcastMultiplePermissions(Intent, String[], String[])}, but also allows
* specification of a list of excluded packages. * specification of a list of excluded packages.
@@ -2409,6 +2408,19 @@ public abstract class Context {
public void sendBroadcastMultiplePermissions(@NonNull Intent intent, public void sendBroadcastMultiplePermissions(@NonNull Intent intent,
@NonNull String[] receiverPermissions, @Nullable String[] excludedPermissions, @NonNull String[] receiverPermissions, @Nullable String[] excludedPermissions,
@Nullable String[] excludedPackages) { @Nullable String[] excludedPackages) {
sendBroadcastMultiplePermissions(intent, receiverPermissions, excludedPermissions,
excludedPackages, null);
}
/**
* Like {@link #sendBroadcastMultiplePermissions(Intent, String[], String[], String[])}, but
* also allows specification of options generated from {@link android.app.BroadcastOptions}.
*
* @hide
*/
public void sendBroadcastMultiplePermissions(@NonNull Intent intent,
@NonNull String[] receiverPermissions, @Nullable String[] excludedPermissions,
@Nullable String[] excludedPackages, @Nullable BroadcastOptions options) {
throw new RuntimeException("Not implemented. Must override in a subclass."); throw new RuntimeException("Not implemented. Must override in a subclass.");
} }

View File

@@ -24,6 +24,7 @@ import android.annotation.SuppressLint;
import android.annotation.SystemApi; import android.annotation.SystemApi;
import android.annotation.TestApi; import android.annotation.TestApi;
import android.annotation.UiContext; import android.annotation.UiContext;
import android.app.BroadcastOptions;
import android.app.IApplicationThread; import android.app.IApplicationThread;
import android.app.IServiceConnection; import android.app.IServiceConnection;
import android.app.compat.CompatChanges; import android.app.compat.CompatChanges;
@@ -519,9 +520,9 @@ public class ContextWrapper extends Context {
@Override @Override
public void sendBroadcastMultiplePermissions(@NonNull Intent intent, public void sendBroadcastMultiplePermissions(@NonNull Intent intent,
@NonNull String[] receiverPermissions, @Nullable String[] excludedPermissions, @NonNull String[] receiverPermissions, @Nullable String[] excludedPermissions,
@Nullable String[] excludedPackages) { @Nullable String[] excludedPackages, @Nullable BroadcastOptions options) {
mBase.sendBroadcastMultiplePermissions(intent, receiverPermissions, excludedPermissions, mBase.sendBroadcastMultiplePermissions(intent, receiverPermissions, excludedPermissions,
excludedPackages); excludedPackages, options);
} }
/** @hide */ /** @hide */

View File

@@ -28,6 +28,7 @@ import android.annotation.NonNull;
import android.annotation.Nullable; import android.annotation.Nullable;
import android.app.ActivityManager; import android.app.ActivityManager;
import android.app.AppOpsManager; import android.app.AppOpsManager;
import android.app.BroadcastOptions;
import android.app.compat.CompatChanges; import android.app.compat.CompatChanges;
import android.compat.annotation.ChangeId; import android.compat.annotation.ChangeId;
import android.compat.annotation.EnabledSince; import android.compat.annotation.EnabledSince;
@@ -3533,28 +3534,36 @@ public class TelephonyRegistry extends ITelephonyRegistry.Stub {
// - Sanitized ServiceState sent to all other apps with READ_PHONE_STATE // - Sanitized ServiceState sent to all other apps with READ_PHONE_STATE
// - Sanitized ServiceState sent to all other apps with READ_PRIVILEGED_PHONE_STATE but not // - Sanitized ServiceState sent to all other apps with READ_PRIVILEGED_PHONE_STATE but not
// READ_PHONE_STATE // READ_PHONE_STATE
BroadcastOptions options = createServiceStateBroadcastOptions(subId, phoneId);
if (LocationAccessPolicy.isLocationModeEnabled(mContext, mContext.getUserId())) { if (LocationAccessPolicy.isLocationModeEnabled(mContext, mContext.getUserId())) {
Intent fullIntent = createServiceStateIntent(state, subId, phoneId, false); Intent fullIntent = createServiceStateIntent(state, subId, phoneId, false);
mContext.createContextAsUser(UserHandle.ALL, 0).sendBroadcastMultiplePermissions( mContext.createContextAsUser(UserHandle.ALL, 0).sendBroadcastMultiplePermissions(
fullIntent, fullIntent,
new String[]{Manifest.permission.READ_PHONE_STATE, new String[]{Manifest.permission.READ_PHONE_STATE,
Manifest.permission.ACCESS_FINE_LOCATION}); Manifest.permission.ACCESS_FINE_LOCATION},
options);
mContext.createContextAsUser(UserHandle.ALL, 0).sendBroadcastMultiplePermissions( mContext.createContextAsUser(UserHandle.ALL, 0).sendBroadcastMultiplePermissions(
fullIntent, fullIntent,
new String[]{Manifest.permission.READ_PRIVILEGED_PHONE_STATE, new String[]{Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
Manifest.permission.ACCESS_FINE_LOCATION}, Manifest.permission.ACCESS_FINE_LOCATION},
new String[]{Manifest.permission.READ_PHONE_STATE}); new String[]{Manifest.permission.READ_PHONE_STATE},
null,
options);
Intent sanitizedIntent = createServiceStateIntent(state, subId, phoneId, true); Intent sanitizedIntent = createServiceStateIntent(state, subId, phoneId, true);
mContext.createContextAsUser(UserHandle.ALL, 0).sendBroadcastMultiplePermissions( mContext.createContextAsUser(UserHandle.ALL, 0).sendBroadcastMultiplePermissions(
sanitizedIntent, sanitizedIntent,
new String[]{Manifest.permission.READ_PHONE_STATE}, new String[]{Manifest.permission.READ_PHONE_STATE},
new String[]{Manifest.permission.ACCESS_FINE_LOCATION}); new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
null,
options);
mContext.createContextAsUser(UserHandle.ALL, 0).sendBroadcastMultiplePermissions( mContext.createContextAsUser(UserHandle.ALL, 0).sendBroadcastMultiplePermissions(
sanitizedIntent, sanitizedIntent,
new String[]{Manifest.permission.READ_PRIVILEGED_PHONE_STATE}, new String[]{Manifest.permission.READ_PRIVILEGED_PHONE_STATE},
new String[]{Manifest.permission.READ_PHONE_STATE, new String[]{Manifest.permission.READ_PHONE_STATE,
Manifest.permission.ACCESS_FINE_LOCATION}); Manifest.permission.ACCESS_FINE_LOCATION},
null,
options);
} else { } else {
String[] locationBypassPackages = Binder.withCleanCallingIdentity(() -> String[] locationBypassPackages = Binder.withCleanCallingIdentity(() ->
LocationAccessPolicy.getLocationBypassPackages(mContext)); LocationAccessPolicy.getLocationBypassPackages(mContext));
@@ -3563,11 +3572,14 @@ public class TelephonyRegistry extends ITelephonyRegistry.Stub {
fullIntent.setPackage(locationBypassPackage); fullIntent.setPackage(locationBypassPackage);
mContext.createContextAsUser(UserHandle.ALL, 0).sendBroadcastMultiplePermissions( mContext.createContextAsUser(UserHandle.ALL, 0).sendBroadcastMultiplePermissions(
fullIntent, fullIntent,
new String[]{Manifest.permission.READ_PHONE_STATE}); new String[]{Manifest.permission.READ_PHONE_STATE},
options);
mContext.createContextAsUser(UserHandle.ALL, 0).sendBroadcastMultiplePermissions( mContext.createContextAsUser(UserHandle.ALL, 0).sendBroadcastMultiplePermissions(
fullIntent, fullIntent,
new String[]{Manifest.permission.READ_PRIVILEGED_PHONE_STATE}, new String[]{Manifest.permission.READ_PRIVILEGED_PHONE_STATE},
new String[]{Manifest.permission.READ_PHONE_STATE}); new String[]{Manifest.permission.READ_PHONE_STATE},
null,
options);
} }
Intent sanitizedIntent = createServiceStateIntent(state, subId, phoneId, true); Intent sanitizedIntent = createServiceStateIntent(state, subId, phoneId, true);
@@ -3575,12 +3587,14 @@ public class TelephonyRegistry extends ITelephonyRegistry.Stub {
sanitizedIntent, sanitizedIntent,
new String[]{Manifest.permission.READ_PHONE_STATE}, new String[]{Manifest.permission.READ_PHONE_STATE},
new String[]{/* no excluded permissions */}, new String[]{/* no excluded permissions */},
locationBypassPackages); locationBypassPackages,
options);
mContext.createContextAsUser(UserHandle.ALL, 0).sendBroadcastMultiplePermissions( mContext.createContextAsUser(UserHandle.ALL, 0).sendBroadcastMultiplePermissions(
sanitizedIntent, sanitizedIntent,
new String[]{Manifest.permission.READ_PRIVILEGED_PHONE_STATE}, new String[]{Manifest.permission.READ_PRIVILEGED_PHONE_STATE},
new String[]{Manifest.permission.READ_PHONE_STATE}, new String[]{Manifest.permission.READ_PHONE_STATE},
locationBypassPackages); locationBypassPackages,
options);
} }
} }
@@ -3602,6 +3616,15 @@ public class TelephonyRegistry extends ITelephonyRegistry.Stub {
return intent; return intent;
} }
private BroadcastOptions createServiceStateBroadcastOptions(int subId, int phoneId) {
return new BroadcastOptions()
.setDeliveryGroupPolicy(BroadcastOptions.DELIVERY_GROUP_POLICY_MOST_RECENT)
// Use a combination of subId and phoneId as the key so that older broadcasts
// with same subId and phoneId will get discarded.
.setDeliveryGroupMatchingKey(Intent.ACTION_SERVICE_STATE, subId + "-" + phoneId)
.setDeferralPolicy(BroadcastOptions.DEFERRAL_POLICY_UNTIL_ACTIVE);
}
private void broadcastSignalStrengthChanged(SignalStrength signalStrength, int phoneId, private void broadcastSignalStrengthChanged(SignalStrength signalStrength, int phoneId,
int subId) { int subId) {
final long ident = Binder.clearCallingIdentity(); final long ident = Binder.clearCallingIdentity();