Merge changes I52fcfd3f,I6de4abdc,I1e8944de,I9525bfe0,Ife09fef8, ... into main
* changes: Use carrier service changed callbacks when flag is on Delete slots as the listeners are unregistered. Have a full class (not inline) for privilege listener Update the carrier service UID when onCarrierServiceChanged Pass the modem count to registerCarrierPrivilegeListeners Reorder add/remove/register/unregister carrier listeners
This commit is contained in:
@@ -21,6 +21,7 @@ import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR;
|
|||||||
import static com.android.server.connectivity.ConnectivityFlags.CARRIER_SERVICE_CHANGED_USE_CALLBACK;
|
import static com.android.server.connectivity.ConnectivityFlags.CARRIER_SERVICE_CHANGED_USE_CALLBACK;
|
||||||
|
|
||||||
import android.annotation.NonNull;
|
import android.annotation.NonNull;
|
||||||
|
import android.annotation.Nullable;
|
||||||
import android.content.BroadcastReceiver;
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
@@ -72,9 +73,7 @@ public class CarrierPrivilegeAuthenticator {
|
|||||||
private final Object mLock = new Object();
|
private final Object mLock = new Object();
|
||||||
private final Handler mHandler;
|
private final Handler mHandler;
|
||||||
@NonNull
|
@NonNull
|
||||||
@GuardedBy("mLock")
|
private final List<PrivilegeListener> mCarrierPrivilegesChangedListeners = new ArrayList<>();
|
||||||
private final List<CarrierPrivilegesListenerShim> mCarrierPrivilegesChangedListeners =
|
|
||||||
new ArrayList<>();
|
|
||||||
private final boolean mUseCallbacksForServiceChanged;
|
private final boolean mUseCallbacksForServiceChanged;
|
||||||
|
|
||||||
public CarrierPrivilegeAuthenticator(@NonNull final Context c,
|
public CarrierPrivilegeAuthenticator(@NonNull final Context c,
|
||||||
@@ -119,32 +118,49 @@ public class CarrierPrivilegeAuthenticator {
|
|||||||
synchronized (mLock) {
|
synchronized (mLock) {
|
||||||
unregisterCarrierPrivilegesListeners();
|
unregisterCarrierPrivilegesListeners();
|
||||||
mModemCount = mTelephonyManager.getActiveModemCount();
|
mModemCount = mTelephonyManager.getActiveModemCount();
|
||||||
registerCarrierPrivilegesListeners();
|
registerCarrierPrivilegesListeners(mModemCount);
|
||||||
updateCarrierServiceUid();
|
if (!mUseCallbacksForServiceChanged) updateCarrierServiceUid();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void registerCarrierPrivilegesListeners() {
|
private class PrivilegeListener implements CarrierPrivilegesListenerShim {
|
||||||
final HandlerExecutor executor = new HandlerExecutor(mHandler);
|
public final int mLogicalSlot;
|
||||||
int modemCount;
|
PrivilegeListener(final int logicalSlot) {
|
||||||
synchronized (mLock) {
|
mLogicalSlot = logicalSlot;
|
||||||
modemCount = mModemCount;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override public void onCarrierPrivilegesChanged(
|
||||||
|
@NonNull List<String> privilegedPackageNames,
|
||||||
|
@NonNull int[] privilegedUids) {
|
||||||
|
if (mUseCallbacksForServiceChanged) return;
|
||||||
|
// Re-trigger the synchronous check (which is also very cheap due
|
||||||
|
// to caching in CarrierPrivilegesTracker). This allows consistency
|
||||||
|
// with the onSubscriptionsChangedListener and broadcasts.
|
||||||
|
updateCarrierServiceUid();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCarrierServiceChanged(@Nullable final String carrierServicePackageName,
|
||||||
|
final int carrierServiceUid) {
|
||||||
|
if (!mUseCallbacksForServiceChanged) {
|
||||||
|
// Re-trigger the synchronous check (which is also very cheap due
|
||||||
|
// to caching in CarrierPrivilegesTracker). This allows consistency
|
||||||
|
// with the onSubscriptionsChangedListener and broadcasts.
|
||||||
|
updateCarrierServiceUid();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
synchronized (mLock) {
|
||||||
|
mCarrierServiceUid.put(mLogicalSlot, carrierServiceUid);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void registerCarrierPrivilegesListeners(final int modemCount) {
|
||||||
|
final HandlerExecutor executor = new HandlerExecutor(mHandler);
|
||||||
try {
|
try {
|
||||||
for (int i = 0; i < modemCount; i++) {
|
for (int i = 0; i < modemCount; i++) {
|
||||||
CarrierPrivilegesListenerShim carrierPrivilegesListener =
|
PrivilegeListener carrierPrivilegesListener = new PrivilegeListener(i);
|
||||||
new CarrierPrivilegesListenerShim() {
|
addCarrierPrivilegesListener(executor, carrierPrivilegesListener);
|
||||||
@Override
|
|
||||||
public void onCarrierPrivilegesChanged(
|
|
||||||
@NonNull List<String> privilegedPackageNames,
|
|
||||||
@NonNull int[] privilegedUids) {
|
|
||||||
// Re-trigger the synchronous check (which is also very cheap due
|
|
||||||
// to caching in CarrierPrivilegesTracker). This allows consistency
|
|
||||||
// with the onSubscriptionsChangedListener and broadcasts.
|
|
||||||
updateCarrierServiceUid();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
addCarrierPrivilegesListener(i, executor, carrierPrivilegesListener);
|
|
||||||
mCarrierPrivilegesChangedListeners.add(carrierPrivilegesListener);
|
mCarrierPrivilegesChangedListeners.add(carrierPrivilegesListener);
|
||||||
}
|
}
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
@@ -152,24 +168,13 @@ public class CarrierPrivilegeAuthenticator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addCarrierPrivilegesListener(int logicalSlotIndex, Executor executor,
|
@GuardedBy("mLock")
|
||||||
CarrierPrivilegesListenerShim listener) {
|
private void unregisterCarrierPrivilegesListeners() {
|
||||||
try {
|
for (PrivilegeListener carrierPrivilegesListener : mCarrierPrivilegesChangedListeners) {
|
||||||
mTelephonyManagerShim.addCarrierPrivilegesListener(
|
removeCarrierPrivilegesListener(carrierPrivilegesListener);
|
||||||
logicalSlotIndex, executor, listener);
|
mCarrierServiceUid.delete(carrierPrivilegesListener.mLogicalSlot);
|
||||||
} catch (UnsupportedApiLevelException unsupportedApiLevelException) {
|
|
||||||
// Should not happen since CarrierPrivilegeAuthenticator is only used on T+
|
|
||||||
Log.e(TAG, "addCarrierPrivilegesListener API is not available");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void removeCarrierPrivilegesListener(CarrierPrivilegesListenerShim listener) {
|
|
||||||
try {
|
|
||||||
mTelephonyManagerShim.removeCarrierPrivilegesListener(listener);
|
|
||||||
} catch (UnsupportedApiLevelException unsupportedApiLevelException) {
|
|
||||||
// Should not happen since CarrierPrivilegeAuthenticator is only used on T+
|
|
||||||
Log.e(TAG, "removeCarrierPrivilegesListener API is not available");
|
|
||||||
}
|
}
|
||||||
|
mCarrierPrivilegesChangedListeners.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getCarrierServicePackageNameForLogicalSlot(int logicalSlotIndex) {
|
private String getCarrierServicePackageNameForLogicalSlot(int logicalSlotIndex) {
|
||||||
@@ -183,14 +188,6 @@ public class CarrierPrivilegeAuthenticator {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void unregisterCarrierPrivilegesListeners() {
|
|
||||||
for (CarrierPrivilegesListenerShim carrierPrivilegesListener :
|
|
||||||
mCarrierPrivilegesChangedListeners) {
|
|
||||||
removeCarrierPrivilegesListener(carrierPrivilegesListener);
|
|
||||||
}
|
|
||||||
mCarrierPrivilegesChangedListeners.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if a UID is the carrier service app of the subscription ID in the provided capabilities
|
* Check if a UID is the carrier service app of the subscription ID in the provided capabilities
|
||||||
*
|
*
|
||||||
@@ -273,4 +270,26 @@ public class CarrierPrivilegeAuthenticator {
|
|||||||
int getCarrierServicePackageUidForSlot(int slotId) {
|
int getCarrierServicePackageUidForSlot(int slotId) {
|
||||||
return getUidForPackage(getCarrierServicePackageNameForLogicalSlot(slotId));
|
return getUidForPackage(getCarrierServicePackageNameForLogicalSlot(slotId));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Helper methods to avoid having to deal with UnsupportedApiLevelException.
|
||||||
|
|
||||||
|
private void addCarrierPrivilegesListener(@NonNull final Executor executor,
|
||||||
|
@NonNull final PrivilegeListener listener) {
|
||||||
|
try {
|
||||||
|
mTelephonyManagerShim.addCarrierPrivilegesListener(listener.mLogicalSlot, executor,
|
||||||
|
listener);
|
||||||
|
} catch (UnsupportedApiLevelException unsupportedApiLevelException) {
|
||||||
|
// Should not happen since CarrierPrivilegeAuthenticator is only used on T+
|
||||||
|
Log.e(TAG, "addCarrierPrivilegesListener API is not available");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void removeCarrierPrivilegesListener(PrivilegeListener listener) {
|
||||||
|
try {
|
||||||
|
mTelephonyManagerShim.removeCarrierPrivilegesListener(listener);
|
||||||
|
} catch (UnsupportedApiLevelException unsupportedApiLevelException) {
|
||||||
|
// Should not happen since CarrierPrivilegeAuthenticator is only used on T+
|
||||||
|
Log.e(TAG, "removeCarrierPrivilegesListener API is not available");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -158,6 +158,8 @@ public class CarrierPrivilegeAuthenticatorTest {
|
|||||||
assertNotNull(initialListeners.get(1));
|
assertNotNull(initialListeners.get(1));
|
||||||
assertEquals(2, initialListeners.size());
|
assertEquals(2, initialListeners.size());
|
||||||
|
|
||||||
|
initialListeners.get(0).onCarrierServiceChanged(null, mCarrierConfigPkgUid);
|
||||||
|
|
||||||
final NetworkCapabilities.Builder ncBuilder = new NetworkCapabilities.Builder()
|
final NetworkCapabilities.Builder ncBuilder = new NetworkCapabilities.Builder()
|
||||||
.addTransportType(TRANSPORT_CELLULAR)
|
.addTransportType(TRANSPORT_CELLULAR)
|
||||||
.setNetworkSpecifier(new TelephonyNetworkSpecifier(0));
|
.setNetworkSpecifier(new TelephonyNetworkSpecifier(0));
|
||||||
@@ -194,6 +196,8 @@ public class CarrierPrivilegeAuthenticatorTest {
|
|||||||
assertNotNull(newListeners.get(0));
|
assertNotNull(newListeners.get(0));
|
||||||
assertEquals(1, newListeners.size());
|
assertEquals(1, newListeners.size());
|
||||||
|
|
||||||
|
newListeners.get(0).onCarrierServiceChanged(null, mCarrierConfigPkgUid);
|
||||||
|
|
||||||
final TelephonyNetworkSpecifier specifier = new TelephonyNetworkSpecifier(0);
|
final TelephonyNetworkSpecifier specifier = new TelephonyNetworkSpecifier(0);
|
||||||
final NetworkCapabilities nc = new NetworkCapabilities.Builder()
|
final NetworkCapabilities nc = new NetworkCapabilities.Builder()
|
||||||
.addTransportType(TRANSPORT_CELLULAR)
|
.addTransportType(TRANSPORT_CELLULAR)
|
||||||
@@ -219,6 +223,7 @@ public class CarrierPrivilegeAuthenticatorTest {
|
|||||||
applicationInfo.uid = mCarrierConfigPkgUid + 1;
|
applicationInfo.uid = mCarrierConfigPkgUid + 1;
|
||||||
doReturn(applicationInfo).when(mPackageManager).getApplicationInfo(eq(mTestPkg), anyInt());
|
doReturn(applicationInfo).when(mPackageManager).getApplicationInfo(eq(mTestPkg), anyInt());
|
||||||
listener.onCarrierPrivilegesChanged(Collections.emptyList(), new int[] {});
|
listener.onCarrierPrivilegesChanged(Collections.emptyList(), new int[] {});
|
||||||
|
listener.onCarrierServiceChanged(null, applicationInfo.uid);
|
||||||
|
|
||||||
assertFalse(mCarrierPrivilegeAuthenticator.hasCarrierPrivilegeForNetworkCapabilities(
|
assertFalse(mCarrierPrivilegeAuthenticator.hasCarrierPrivilegeForNetworkCapabilities(
|
||||||
mCarrierConfigPkgUid, nc));
|
mCarrierConfigPkgUid, nc));
|
||||||
@@ -228,6 +233,9 @@ public class CarrierPrivilegeAuthenticatorTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDefaultSubscription() throws Exception {
|
public void testDefaultSubscription() throws Exception {
|
||||||
|
final CarrierPrivilegesListenerShim listener = getCarrierPrivilegesListeners().get(0);
|
||||||
|
listener.onCarrierServiceChanged(null, mCarrierConfigPkgUid);
|
||||||
|
|
||||||
final NetworkCapabilities.Builder ncBuilder = new NetworkCapabilities.Builder();
|
final NetworkCapabilities.Builder ncBuilder = new NetworkCapabilities.Builder();
|
||||||
ncBuilder.addTransportType(TRANSPORT_CELLULAR);
|
ncBuilder.addTransportType(TRANSPORT_CELLULAR);
|
||||||
assertFalse(mCarrierPrivilegeAuthenticator.hasCarrierPrivilegeForNetworkCapabilities(
|
assertFalse(mCarrierPrivilegeAuthenticator.hasCarrierPrivilegeForNetworkCapabilities(
|
||||||
|
|||||||
Reference in New Issue
Block a user