Merge "Replace Context @hide APIs"

This commit is contained in:
Paul Hu
2020-11-05 08:05:09 +00:00
committed by Gerrit Code Review
2 changed files with 22 additions and 11 deletions

View File

@@ -1105,23 +1105,26 @@ public class ConnectivityService extends IConnectivityManager.Stub
intentFilter.addAction(Intent.ACTION_USER_ADDED); intentFilter.addAction(Intent.ACTION_USER_ADDED);
intentFilter.addAction(Intent.ACTION_USER_REMOVED); intentFilter.addAction(Intent.ACTION_USER_REMOVED);
intentFilter.addAction(Intent.ACTION_USER_UNLOCKED); intentFilter.addAction(Intent.ACTION_USER_UNLOCKED);
mContext.registerReceiverAsUser(
final Context userAllContext = mContext.createContextAsUser(UserHandle.ALL, 0 /* flags */);
userAllContext.registerReceiver(
mIntentReceiver, mIntentReceiver,
UserHandle.ALL,
intentFilter, intentFilter,
null /* broadcastPermission */, null /* broadcastPermission */,
mHandler); mHandler);
mContext.registerReceiverAsUser(mUserPresentReceiver, UserHandle.SYSTEM, mContext.createContextAsUser(UserHandle.SYSTEM, 0 /* flags */).registerReceiver(
new IntentFilter(Intent.ACTION_USER_PRESENT), null, null); mUserPresentReceiver,
new IntentFilter(Intent.ACTION_USER_PRESENT),
null /* broadcastPermission */,
null /* scheduler */);
// Listen to package add and removal events for all users. // Listen to package add and removal events for all users.
intentFilter = new IntentFilter(); intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED); intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
intentFilter.addDataScheme("package"); intentFilter.addDataScheme("package");
mContext.registerReceiverAsUser( userAllContext.registerReceiver(
mIntentReceiver, mIntentReceiver,
UserHandle.ALL,
intentFilter, intentFilter,
null /* broadcastPermission */, null /* broadcastPermission */,
mHandler); mHandler);
@@ -1129,8 +1132,8 @@ public class ConnectivityService extends IConnectivityManager.Stub
// Listen to lockdown VPN reset. // Listen to lockdown VPN reset.
intentFilter = new IntentFilter(); intentFilter = new IntentFilter();
intentFilter.addAction(LockdownVpnTracker.ACTION_LOCKDOWN_RESET); intentFilter.addAction(LockdownVpnTracker.ACTION_LOCKDOWN_RESET);
mContext.registerReceiverAsUser( userAllContext.registerReceiver(
mIntentReceiver, UserHandle.ALL, intentFilter, NETWORK_STACK, mHandler); mIntentReceiver, intentFilter, NETWORK_STACK, mHandler);
try { try {
mNMS.registerObserver(mDataActivityObserver); mNMS.registerObserver(mDataActivityObserver);
@@ -5259,7 +5262,9 @@ public class ConnectivityService extends IConnectivityManager.Stub
// Try creating lockdown tracker, since user present usually means // Try creating lockdown tracker, since user present usually means
// unlocked keystore. // unlocked keystore.
updateLockdownVpn(); 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);
} }
}; };

View File

@@ -34,6 +34,7 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@@ -53,6 +54,7 @@ import android.net.NetworkTemplate;
import android.net.StringNetworkSpecifier; import android.net.StringNetworkSpecifier;
import android.net.TelephonyNetworkSpecifier; import android.net.TelephonyNetworkSpecifier;
import android.os.Handler; import android.os.Handler;
import android.os.UserHandle;
import android.provider.Settings; import android.provider.Settings;
import android.telephony.TelephonyManager; import android.telephony.TelephonyManager;
import android.test.mock.MockContentResolver; import android.test.mock.MockContentResolver;
@@ -91,6 +93,7 @@ public class MultipathPolicyTrackerTest {
private static final int POLICY_SNOOZED = -100; private static final int POLICY_SNOOZED = -100;
@Mock private Context mContext; @Mock private Context mContext;
@Mock private Context mUserAllContext;
@Mock private Resources mResources; @Mock private Resources mResources;
@Mock private Handler mHandler; @Mock private Handler mHandler;
@Mock private MultipathPolicyTracker.Dependencies mDeps; @Mock private MultipathPolicyTracker.Dependencies mDeps;
@@ -127,8 +130,11 @@ public class MultipathPolicyTrackerTest {
when(mContext.getResources()).thenReturn(mResources); when(mContext.getResources()).thenReturn(mResources);
when(mContext.getApplicationInfo()).thenReturn(new ApplicationInfo()); when(mContext.getApplicationInfo()).thenReturn(new ApplicationInfo());
when(mContext.registerReceiverAsUser(mConfigChangeReceiverCaptor.capture(), doReturn(UserHandle.ALL.getIdentifier()).when(mUserAllContext).getUserId();
any(), argThat(f -> f.hasAction(ACTION_CONFIGURATION_CHANGED)), any(), any())) 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); .thenReturn(null);
when(mDeps.getClock()).thenReturn(mClock); when(mDeps.getClock()).thenReturn(mClock);