Merge "Replace Context @hide APIs" am: a1c0a06580 am: d5ee31aba5
Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1458063 Change-Id: Iea56458847be39665e7660f048eec88147be27c3
This commit is contained in:
@@ -1105,23 +1105,26 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
||||
intentFilter.addAction(Intent.ACTION_USER_ADDED);
|
||||
intentFilter.addAction(Intent.ACTION_USER_REMOVED);
|
||||
intentFilter.addAction(Intent.ACTION_USER_UNLOCKED);
|
||||
mContext.registerReceiverAsUser(
|
||||
|
||||
final Context userAllContext = mContext.createContextAsUser(UserHandle.ALL, 0 /* flags */);
|
||||
userAllContext.registerReceiver(
|
||||
mIntentReceiver,
|
||||
UserHandle.ALL,
|
||||
intentFilter,
|
||||
null /* broadcastPermission */,
|
||||
mHandler);
|
||||
mContext.registerReceiverAsUser(mUserPresentReceiver, UserHandle.SYSTEM,
|
||||
new IntentFilter(Intent.ACTION_USER_PRESENT), null, null);
|
||||
mContext.createContextAsUser(UserHandle.SYSTEM, 0 /* flags */).registerReceiver(
|
||||
mUserPresentReceiver,
|
||||
new IntentFilter(Intent.ACTION_USER_PRESENT),
|
||||
null /* broadcastPermission */,
|
||||
null /* scheduler */);
|
||||
|
||||
// Listen to package add and removal events for all users.
|
||||
intentFilter = new IntentFilter();
|
||||
intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
|
||||
intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
|
||||
intentFilter.addDataScheme("package");
|
||||
mContext.registerReceiverAsUser(
|
||||
userAllContext.registerReceiver(
|
||||
mIntentReceiver,
|
||||
UserHandle.ALL,
|
||||
intentFilter,
|
||||
null /* broadcastPermission */,
|
||||
mHandler);
|
||||
@@ -1129,8 +1132,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
||||
// Listen to lockdown VPN reset.
|
||||
intentFilter = new IntentFilter();
|
||||
intentFilter.addAction(LockdownVpnTracker.ACTION_LOCKDOWN_RESET);
|
||||
mContext.registerReceiverAsUser(
|
||||
mIntentReceiver, UserHandle.ALL, intentFilter, NETWORK_STACK, mHandler);
|
||||
userAllContext.registerReceiver(
|
||||
mIntentReceiver, intentFilter, NETWORK_STACK, mHandler);
|
||||
|
||||
try {
|
||||
mNMS.registerObserver(mDataActivityObserver);
|
||||
@@ -5259,7 +5262,9 @@ public class ConnectivityService extends IConnectivityManager.Stub
|
||||
// Try creating lockdown tracker, since user present usually means
|
||||
// unlocked keystore.
|
||||
updateLockdownVpn();
|
||||
mContext.unregisterReceiver(this);
|
||||
// Use the same context that registered receiver before to unregister it. Because use
|
||||
// different context to unregister receiver will cause exception.
|
||||
context.unregisterReceiver(this);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -95,7 +95,11 @@ public class MultipathPolicyTracker {
|
||||
|
||||
private static final boolean DBG = false;
|
||||
|
||||
// This context is for the current user.
|
||||
private final Context mContext;
|
||||
// This context is for all users, so register a BroadcastReceiver which can receive intents from
|
||||
// all users.
|
||||
private final Context mUserAllContext;
|
||||
private final Handler mHandler;
|
||||
private final Clock mClock;
|
||||
private final Dependencies mDeps;
|
||||
@@ -132,6 +136,7 @@ public class MultipathPolicyTracker {
|
||||
|
||||
public MultipathPolicyTracker(Context ctx, Handler handler, Dependencies deps) {
|
||||
mContext = ctx;
|
||||
mUserAllContext = ctx.createContextAsUser(UserHandle.ALL, 0 /* flags */);
|
||||
mHandler = handler;
|
||||
mClock = deps.getClock();
|
||||
mDeps = deps;
|
||||
@@ -155,8 +160,8 @@ public class MultipathPolicyTracker {
|
||||
|
||||
final IntentFilter intentFilter = new IntentFilter();
|
||||
intentFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
|
||||
mContext.registerReceiverAsUser(
|
||||
mConfigChangeReceiver, UserHandle.ALL, intentFilter, null, mHandler);
|
||||
mUserAllContext.registerReceiver(
|
||||
mConfigChangeReceiver, intentFilter, null /* broadcastPermission */, mHandler);
|
||||
}
|
||||
|
||||
public void shutdown() {
|
||||
@@ -167,7 +172,7 @@ public class MultipathPolicyTracker {
|
||||
}
|
||||
mMultipathTrackers.clear();
|
||||
mResolver.unregisterContentObserver(mSettingsObserver);
|
||||
mContext.unregisterReceiver(mConfigChangeReceiver);
|
||||
mUserAllContext.unregisterReceiver(mConfigChangeReceiver);
|
||||
}
|
||||
|
||||
// Called on an arbitrary binder thread.
|
||||
|
||||
@@ -865,7 +865,7 @@ public class Vpn {
|
||||
Intent serviceIntent = new Intent(VpnConfig.SERVICE_INTERFACE);
|
||||
serviceIntent.setPackage(alwaysOnPackage);
|
||||
try {
|
||||
return mContext.startServiceAsUser(serviceIntent, UserHandle.of(mUserId)) != null;
|
||||
return mUserIdContext.startService(serviceIntent) != null;
|
||||
} catch (RuntimeException e) {
|
||||
Log.e(TAG, "VpnService " + serviceIntent + " failed to start", e);
|
||||
return false;
|
||||
|
||||
@@ -34,6 +34,7 @@ import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.argThat;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
@@ -53,6 +54,7 @@ import android.net.NetworkTemplate;
|
||||
import android.net.StringNetworkSpecifier;
|
||||
import android.net.TelephonyNetworkSpecifier;
|
||||
import android.os.Handler;
|
||||
import android.os.UserHandle;
|
||||
import android.provider.Settings;
|
||||
import android.telephony.TelephonyManager;
|
||||
import android.test.mock.MockContentResolver;
|
||||
@@ -91,6 +93,7 @@ public class MultipathPolicyTrackerTest {
|
||||
private static final int POLICY_SNOOZED = -100;
|
||||
|
||||
@Mock private Context mContext;
|
||||
@Mock private Context mUserAllContext;
|
||||
@Mock private Resources mResources;
|
||||
@Mock private Handler mHandler;
|
||||
@Mock private MultipathPolicyTracker.Dependencies mDeps;
|
||||
@@ -127,8 +130,11 @@ public class MultipathPolicyTrackerTest {
|
||||
|
||||
when(mContext.getResources()).thenReturn(mResources);
|
||||
when(mContext.getApplicationInfo()).thenReturn(new ApplicationInfo());
|
||||
when(mContext.registerReceiverAsUser(mConfigChangeReceiverCaptor.capture(),
|
||||
any(), argThat(f -> f.hasAction(ACTION_CONFIGURATION_CHANGED)), any(), any()))
|
||||
doReturn(UserHandle.ALL.getIdentifier()).when(mUserAllContext).getUserId();
|
||||
when(mContext.createContextAsUser(eq(UserHandle.ALL), anyInt()))
|
||||
.thenReturn(mUserAllContext);
|
||||
when(mUserAllContext.registerReceiver(mConfigChangeReceiverCaptor.capture(),
|
||||
argThat(f -> f.hasAction(ACTION_CONFIGURATION_CHANGED)), any(), any()))
|
||||
.thenReturn(null);
|
||||
|
||||
when(mDeps.getClock()).thenReturn(mClock);
|
||||
|
||||
Reference in New Issue
Block a user