Inject system services instead of using context.getSystemService

BYPASS_INCLUSIVE_LANGUAGE_REASON=Method is more widely used and can't be
changed in the context of this CL
Fixes: 247475650
Test: atest KeyguardUpdateMonitorTest

Change-Id: I4b619dee6acac2df0f84c8948f159e237b8881a9
This commit is contained in:
Chandru
2022-09-28 10:14:50 +00:00
parent 10846703cd
commit 78138c2ae5
3 changed files with 103 additions and 99 deletions

View File

@@ -112,7 +112,6 @@ import android.os.Trace;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings;
import android.service.dreams.DreamService;
import android.service.dreams.IDreamManager;
import android.telephony.CarrierConfigManager;
import android.telephony.ServiceState;
@@ -285,6 +284,7 @@ public class KeyguardUpdateMonitor implements TrustManager.TrustListener, Dumpab
private final AuthController mAuthController;
private final UiEventLogger mUiEventLogger;
private final Set<Integer> mFaceAcquiredInfoIgnoreList;
private final PackageManager mPackageManager;
private int mStatusBarState;
private final StatusBarStateController.StateListener mStatusBarStateControllerListener =
new StatusBarStateController.StateListener() {
@@ -358,9 +358,9 @@ public class KeyguardUpdateMonitor implements TrustManager.TrustListener, Dumpab
private final IDreamManager mDreamManager;
private final TelephonyManager mTelephonyManager;
@Nullable
private FingerprintManager mFpm;
private final FingerprintManager mFpm;
@Nullable
private FaceManager mFaceManager;
private final FaceManager mFaceManager;
private final LockPatternUtils mLockPatternUtils;
private final boolean mWakeOnFingerprintAcquiredStart;
@@ -740,7 +740,7 @@ public class KeyguardUpdateMonitor implements TrustManager.TrustListener, Dumpab
* If the device is dreaming, awakens the device
*/
public void awakenFromDream() {
if (mIsDreaming && mDreamManager != null) {
if (mIsDreaming) {
try {
mDreamManager.awaken();
} catch (RemoteException e) {
@@ -1121,12 +1121,8 @@ public class KeyguardUpdateMonitor implements TrustManager.TrustListener, Dumpab
// Error is always the end of authentication lifecycle
mFaceCancelSignal = null;
boolean cameraPrivacyEnabled = false;
if (mSensorPrivacyManager != null) {
cameraPrivacyEnabled = mSensorPrivacyManager
.isSensorPrivacyEnabled(SensorPrivacyManager.TOGGLE_TYPE_SOFTWARE,
SensorPrivacyManager.Sensors.CAMERA);
}
boolean cameraPrivacyEnabled = mSensorPrivacyManager.isSensorPrivacyEnabled(
SensorPrivacyManager.TOGGLE_TYPE_SOFTWARE, SensorPrivacyManager.Sensors.CAMERA);
if (msgId == FaceManager.FACE_ERROR_CANCELED
&& mFaceRunningState == BIOMETRIC_STATE_CANCELLING_RESTARTING) {
@@ -1225,19 +1221,16 @@ public class KeyguardUpdateMonitor implements TrustManager.TrustListener, Dumpab
}
private boolean isFingerprintDisabled(int userId) {
final DevicePolicyManager dpm =
(DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
return dpm != null && (dpm.getKeyguardDisabledFeatures(null, userId)
& DevicePolicyManager.KEYGUARD_DISABLE_FINGERPRINT) != 0
return (mDevicePolicyManager.getKeyguardDisabledFeatures(null, userId)
& DevicePolicyManager.KEYGUARD_DISABLE_FINGERPRINT) != 0
|| isSimPinSecure();
}
private boolean isFaceDisabled(int userId) {
final DevicePolicyManager dpm =
(DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
// TODO(b/140035044)
return whitelistIpcs(() -> dpm != null && (dpm.getKeyguardDisabledFeatures(null, userId)
& DevicePolicyManager.KEYGUARD_DISABLE_FACE) != 0
return whitelistIpcs(() ->
(mDevicePolicyManager.getKeyguardDisabledFeatures(null, userId)
& DevicePolicyManager.KEYGUARD_DISABLE_FACE) != 0
|| isSimPinSecure());
}
@@ -1309,7 +1302,7 @@ public class KeyguardUpdateMonitor implements TrustManager.TrustListener, Dumpab
Intent intent =
new Intent(DevicePolicyManager.ACTION_BIND_SECONDARY_LOCKSCREEN_SERVICE)
.setPackage(supervisorComponent.getPackageName());
ResolveInfo resolveInfo = mContext.getPackageManager().resolveService(intent, 0);
ResolveInfo resolveInfo = mPackageManager.resolveService(intent, 0);
if (resolveInfo != null && resolveInfo.serviceInfo != null) {
Intent launchIntent =
new Intent().setComponent(resolveInfo.serviceInfo.getComponentName());
@@ -1911,9 +1904,20 @@ public class KeyguardUpdateMonitor implements TrustManager.TrustListener, Dumpab
UiEventLogger uiEventLogger,
// This has to be a provider because SessionTracker depends on KeyguardUpdateMonitor :(
Provider<SessionTracker> sessionTrackerProvider,
PowerManager powerManager) {
PowerManager powerManager,
TrustManager trustManager,
SubscriptionManager subscriptionManager,
UserManager userManager,
IDreamManager dreamManager,
DevicePolicyManager devicePolicyManager,
SensorPrivacyManager sensorPrivacyManager,
TelephonyManager telephonyManager,
PackageManager packageManager,
@Nullable FaceManager faceManager,
@Nullable FingerprintManager fingerprintManager,
@Nullable BiometricManager biometricManager) {
mContext = context;
mSubscriptionManager = SubscriptionManager.from(context);
mSubscriptionManager = subscriptionManager;
mTelephonyListenerManager = telephonyListenerManager;
mDeviceProvisioned = isDeviceProvisionedInSettingsDb();
mStrongAuthTracker = new StrongAuthTracker(context, this::notifyStrongAuthStateChanged);
@@ -1927,12 +1931,20 @@ public class KeyguardUpdateMonitor implements TrustManager.TrustListener, Dumpab
mLockPatternUtils = lockPatternUtils;
mAuthController = authController;
dumpManager.registerDumpable(getClass().getName(), this);
mSensorPrivacyManager = context.getSystemService(SensorPrivacyManager.class);
mSensorPrivacyManager = sensorPrivacyManager;
mActiveUnlockConfig = activeUnlockConfiguration;
mLogger = logger;
mUiEventLogger = uiEventLogger;
mSessionTrackerProvider = sessionTrackerProvider;
mPowerManager = powerManager;
mTrustManager = trustManager;
mUserManager = userManager;
mDreamManager = dreamManager;
mTelephonyManager = telephonyManager;
mDevicePolicyManager = devicePolicyManager;
mPackageManager = packageManager;
mFpm = fingerprintManager;
mFaceManager = faceManager;
mActiveUnlockConfig.setKeyguardUpdateMonitor(this);
mWakeOnFingerprintAcquiredStart = context.getResources()
.getBoolean(com.android.internal.R.bool.kg_wake_on_acquire_start);
@@ -2077,8 +2089,7 @@ public class KeyguardUpdateMonitor implements TrustManager.TrustListener, Dumpab
// listener now with the service state from the default sub.
mBackgroundExecutor.execute(() -> {
int subId = SubscriptionManager.getDefaultSubscriptionId();
ServiceState serviceState = mContext.getSystemService(TelephonyManager.class)
.getServiceStateForSubscriber(subId);
ServiceState serviceState = mTelephonyManager.getServiceStateForSubscriber(subId);
mHandler.sendMessage(
mHandler.obtainMessage(MSG_SERVICE_STATE_CHANGE, subId, 0, serviceState));
});
@@ -2100,25 +2111,20 @@ public class KeyguardUpdateMonitor implements TrustManager.TrustListener, Dumpab
e.rethrowAsRuntimeException();
}
mTrustManager = context.getSystemService(TrustManager.class);
mTrustManager.registerTrustListener(this);
setStrongAuthTracker(mStrongAuthTracker);
mDreamManager = IDreamManager.Stub.asInterface(
ServiceManager.getService(DreamService.DREAM_SERVICE));
if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)) {
mFpm = (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE);
if (mFpm != null) {
mFingerprintSensorProperties = mFpm.getSensorPropertiesInternal();
mFpm.addLockoutResetCallback(mFingerprintLockoutResetCallback);
}
if (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_FACE)) {
mFaceManager = (FaceManager) context.getSystemService(Context.FACE_SERVICE);
if (mFaceManager != null) {
mFaceSensorProperties = mFaceManager.getSensorPropertiesInternal();
mFaceManager.addLockoutResetCallback(mFaceLockoutResetCallback);
}
if (mFpm != null || mFaceManager != null) {
BiometricManager biometricManager = context.getSystemService(BiometricManager.class);
if (biometricManager != null) {
biometricManager.registerEnabledOnKeyguardCallback(mBiometricEnabledCallback);
}
@@ -2137,19 +2143,11 @@ public class KeyguardUpdateMonitor implements TrustManager.TrustListener, Dumpab
}
});
updateBiometricListeningState(BIOMETRIC_ACTION_UPDATE, FACE_AUTH_UPDATED_ON_KEYGUARD_INIT);
if (mFpm != null) {
mFpm.addLockoutResetCallback(mFingerprintLockoutResetCallback);
}
if (mFaceManager != null) {
mFaceManager.addLockoutResetCallback(mFaceLockoutResetCallback);
}
TaskStackChangeListeners.getInstance().registerTaskStackListener(mTaskStackListener);
mUserManager = context.getSystemService(UserManager.class);
mIsPrimaryUser = mUserManager.isPrimaryUser();
int user = ActivityManager.getCurrentUser();
mUserIsUnlocked.put(user, mUserManager.isUserUnlocked(user));
mDevicePolicyManager = context.getSystemService(DevicePolicyManager.class);
mLogoutEnabled = mDevicePolicyManager.isLogoutEnabled();
updateSecondaryLockscreenRequirement(user);
List<UserInfo> allUsers = mUserManager.getUsers();
@@ -2159,22 +2157,8 @@ public class KeyguardUpdateMonitor implements TrustManager.TrustListener, Dumpab
}
updateAirplaneModeState();
mTelephonyManager =
(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if (mTelephonyManager != null) {
mTelephonyListenerManager.addActiveDataSubscriptionIdListener(mPhoneStateListener);
// Set initial sim states values.
for (int slot = 0; slot < mTelephonyManager.getActiveModemCount(); slot++) {
int state = mTelephonyManager.getSimState(slot);
int[] subIds = mSubscriptionManager.getSubscriptionIds(slot);
if (subIds != null) {
for (int subId : subIds) {
mHandler.obtainMessage(MSG_SIM_STATE_CHANGE, subId, slot, state)
.sendToTarget();
}
}
}
}
mTelephonyListenerManager.addActiveDataSubscriptionIdListener(mPhoneStateListener);
initializeSimState();
mTimeFormatChangeObserver = new ContentObserver(mHandler) {
@Override
@@ -2191,6 +2175,20 @@ public class KeyguardUpdateMonitor implements TrustManager.TrustListener, Dumpab
false, mTimeFormatChangeObserver, UserHandle.USER_ALL);
}
private void initializeSimState() {
// Set initial sim states values.
for (int slot = 0; slot < mTelephonyManager.getActiveModemCount(); slot++) {
int state = mTelephonyManager.getSimState(slot);
int[] subIds = mSubscriptionManager.getSubscriptionIds(slot);
if (subIds != null) {
for (int subId : subIds) {
mHandler.obtainMessage(MSG_SIM_STATE_CHANGE, subId, slot, state)
.sendToTarget();
}
}
}
}
private void updateFaceEnrolled(int userId) {
mIsFaceEnrolled = whitelistIpcs(
() -> mFaceManager != null && mFaceManager.isHardwareDetected()
@@ -3190,7 +3188,7 @@ public class KeyguardUpdateMonitor implements TrustManager.TrustListener, Dumpab
return false;
}
Intent homeIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME);
ResolveInfo resolveInfo = mContext.getPackageManager().resolveActivityAsUser(homeIntent,
ResolveInfo resolveInfo = mPackageManager.resolveActivityAsUser(homeIntent,
0 /* flags */, getCurrentUser());
if (resolveInfo == null) {
@@ -3528,10 +3526,7 @@ public class KeyguardUpdateMonitor implements TrustManager.TrustListener, Dumpab
* @return true if and only if the state has changed for the specified {@code slotId}
*/
private boolean refreshSimState(int subId, int slotId) {
final TelephonyManager tele =
(TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
int state = (tele != null) ?
tele.getSimState(slotId) : TelephonyManager.SIM_STATE_UNKNOWN;
int state = mTelephonyManager.getSimState(slotId);
SimData data = mSimDatas.get(subId);
final boolean changed;
if (data == null) {
@@ -3674,13 +3669,8 @@ public class KeyguardUpdateMonitor implements TrustManager.TrustListener, Dumpab
* Unregister all listeners.
*/
public void destroy() {
// TODO: inject these dependencies:
TelephonyManager telephony =
(TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
if (telephony != null) {
mTelephonyListenerManager.removeActiveDataSubscriptionIdListener(mPhoneStateListener);
}
mStatusBarStateController.removeCallback(mStatusBarStateControllerListener);
mTelephonyListenerManager.removeActiveDataSubscriptionIdListener(mPhoneStateListener);
mSubscriptionManager.removeOnSubscriptionsChangedListener(mSubscriptionListener);
if (mDeviceProvisionedObserver != null) {

View File

@@ -46,6 +46,7 @@ import android.content.res.AssetManager;
import android.content.res.Resources;
import android.hardware.SensorManager;
import android.hardware.SensorPrivacyManager;
import android.hardware.biometrics.BiometricManager;
import android.hardware.camera2.CameraManager;
import android.hardware.devicestate.DeviceStateManager;
import android.hardware.display.AmbientDisplayConfiguration;
@@ -237,22 +238,39 @@ public class FrameworkServicesModule {
@Singleton
static IDreamManager provideIDreamManager() {
return IDreamManager.Stub.asInterface(
ServiceManager.checkService(DreamService.DREAM_SERVICE));
ServiceManager.getService(DreamService.DREAM_SERVICE));
}
@Provides
@Singleton
@Nullable
static FaceManager provideFaceManager(Context context) {
return context.getSystemService(FaceManager.class);
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_FACE)) {
return context.getSystemService(FaceManager.class);
}
return null;
}
@Provides
@Singleton
@Nullable
static FingerprintManager providesFingerprintManager(Context context) {
return context.getSystemService(FingerprintManager.class);
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)) {
return context.getSystemService(FingerprintManager.class);
}
return null;
}
/**
* @return null if both faceManager and fingerprintManager are null.
*/
@Provides
@Singleton
@Nullable
static BiometricManager providesBiometricManager(Context context,
@Nullable FaceManager faceManager, @Nullable FingerprintManager fingerprintManager) {
return faceManager == null && fingerprintManager == null ? null :
context.getSystemService(BiometricManager.class);
}
@Provides

View File

@@ -59,6 +59,7 @@ import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.content.pm.UserInfo;
import android.hardware.SensorPrivacyManager;
import android.hardware.biometrics.BiometricConstants;
import android.hardware.biometrics.BiometricManager;
import android.hardware.biometrics.BiometricSourceType;
@@ -79,13 +80,13 @@ import android.os.PowerManager;
import android.os.RemoteException;
import android.os.UserHandle;
import android.os.UserManager;
import android.service.dreams.IDreamManager;
import android.telephony.ServiceState;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.test.suitebuilder.annotation.SmallTest;
import android.testing.AndroidTestingRunner;
import android.testing.TestableContext;
import android.testing.TestableLooper;
import com.android.dx.mockito.inline.extended.ExtendedMockito;
@@ -170,6 +171,8 @@ public class KeyguardUpdateMonitorTest extends SysuiTestCase {
@Mock
private DevicePolicyManager mDevicePolicyManager;
@Mock
private IDreamManager mDreamManager;
@Mock
private KeyguardBypassController mKeyguardBypassController;
@Mock
private SubscriptionManager mSubscriptionManager;
@@ -178,6 +181,8 @@ public class KeyguardUpdateMonitorTest extends SysuiTestCase {
@Mock
private TelephonyManager mTelephonyManager;
@Mock
private SensorPrivacyManager mSensorPrivacyManager;
@Mock
private StatusBarStateController mStatusBarStateController;
@Mock
private AuthController mAuthController;
@@ -219,7 +224,6 @@ public class KeyguardUpdateMonitorTest extends SysuiTestCase {
private TestableLooper mTestableLooper;
private Handler mHandler;
private TestableKeyguardUpdateMonitor mKeyguardUpdateMonitor;
private TestableContext mSpiedContext;
private MockitoSession mMockitoSession;
private StatusBarStateController.StateListener mStatusBarStateListener;
private IBiometricEnabledOnKeyguardCallback mBiometricEnabledOnKeyguardCallback;
@@ -228,9 +232,6 @@ public class KeyguardUpdateMonitorTest extends SysuiTestCase {
@Before
public void setup() throws RemoteException {
MockitoAnnotations.initMocks(this);
mSpiedContext = spy(mContext);
when(mPackageManager.hasSystemFeature(anyString())).thenReturn(true);
when(mSpiedContext.getPackageManager()).thenReturn(mPackageManager);
when(mActivityService.getCurrentUser()).thenReturn(mCurrentUserInfo);
when(mActivityService.getCurrentUserId()).thenReturn(mCurrentUserId);
when(mFaceManager.isHardwareDetected()).thenReturn(true);
@@ -279,14 +280,6 @@ public class KeyguardUpdateMonitorTest extends SysuiTestCase {
.thenReturn(new ServiceState());
when(mLockPatternUtils.getLockSettings()).thenReturn(mLockSettings);
when(mAuthController.isUdfpsEnrolled(anyInt())).thenReturn(false);
mSpiedContext.addMockSystemService(TrustManager.class, mTrustManager);
mSpiedContext.addMockSystemService(FingerprintManager.class, mFingerprintManager);
mSpiedContext.addMockSystemService(BiometricManager.class, mBiometricManager);
mSpiedContext.addMockSystemService(FaceManager.class, mFaceManager);
mSpiedContext.addMockSystemService(UserManager.class, mUserManager);
mSpiedContext.addMockSystemService(DevicePolicyManager.class, mDevicePolicyManager);
mSpiedContext.addMockSystemService(SubscriptionManager.class, mSubscriptionManager);
mSpiedContext.addMockSystemService(TelephonyManager.class, mTelephonyManager);
mMockitoSession = ExtendedMockito.mockitoSession()
.spyStatic(SubscriptionManager.class)
@@ -301,7 +294,7 @@ public class KeyguardUpdateMonitorTest extends SysuiTestCase {
mTestableLooper = TestableLooper.get(this);
allowTestableLooperAsMainThread();
mKeyguardUpdateMonitor = new TestableKeyguardUpdateMonitor(mSpiedContext);
mKeyguardUpdateMonitor = new TestableKeyguardUpdateMonitor(mContext);
verify(mBiometricManager)
.registerEnabledOnKeyguardCallback(mBiometricEnabledCallbackArgCaptor.capture());
@@ -356,7 +349,7 @@ public class KeyguardUpdateMonitorTest extends SysuiTestCase {
when(mTelephonyManager.getSimState(anyInt())).thenReturn(state);
when(mSubscriptionManager.getSubscriptionIds(anyInt())).thenReturn(new int[]{subId});
KeyguardUpdateMonitor testKUM = new TestableKeyguardUpdateMonitor(mSpiedContext);
KeyguardUpdateMonitor testKUM = new TestableKeyguardUpdateMonitor(mContext);
mTestableLooper.processAllMessages();
@@ -1202,9 +1195,9 @@ public class KeyguardUpdateMonitorTest extends SysuiTestCase {
@Test
public void testShouldListenForFace_whenFaceManagerNotAvailable_returnsFalse() {
cleanupKeyguardUpdateMonitor();
mSpiedContext.addMockSystemService(FaceManager.class, null);
when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_FACE)).thenReturn(false);
mKeyguardUpdateMonitor = new TestableKeyguardUpdateMonitor(mSpiedContext);
mFaceManager = null;
mKeyguardUpdateMonitor = new TestableKeyguardUpdateMonitor(mContext);
assertThat(mKeyguardUpdateMonitor.shouldListenForFace()).isFalse();
}
@@ -1258,7 +1251,7 @@ public class KeyguardUpdateMonitorTest extends SysuiTestCase {
// This disables face auth
when(mUserManager.isPrimaryUser()).thenReturn(false);
mKeyguardUpdateMonitor =
new TestableKeyguardUpdateMonitor(mSpiedContext);
new TestableKeyguardUpdateMonitor(mContext);
// Face auth should run when the following is true.
keyguardNotGoingAway();
@@ -1527,15 +1520,16 @@ public class KeyguardUpdateMonitorTest extends SysuiTestCase {
verify(mHandler, times(1)).removeCallbacks(mKeyguardUpdateMonitor.mFpCancelNotReceived);
mKeyguardUpdateMonitor.dispatchStartedGoingToSleep(0 /* why */);
mTestableLooper.processAllMessages();
assertThat(mKeyguardUpdateMonitor.shouldListenForFingerprint(anyBoolean())).isEqualTo(true);
assertThat(mKeyguardUpdateMonitor.shouldListenForFingerprint(false)).isEqualTo(true);
assertThat(mKeyguardUpdateMonitor.shouldListenForFingerprint(true)).isEqualTo(true);
}
@Test
public void testFingerAcquired_wakesUpPowerManager() {
cleanupKeyguardUpdateMonitor();
mSpiedContext.getOrCreateTestableResources().addOverride(
mContext.getOrCreateTestableResources().addOverride(
com.android.internal.R.bool.kg_wake_on_acquire_start, true);
mKeyguardUpdateMonitor = new TestableKeyguardUpdateMonitor(mSpiedContext);
mKeyguardUpdateMonitor = new TestableKeyguardUpdateMonitor(mContext);
fingerprintAcquireStart();
verify(mPowerManager).wakeUp(anyLong(), anyInt(), anyString());
@@ -1544,9 +1538,9 @@ public class KeyguardUpdateMonitorTest extends SysuiTestCase {
@Test
public void testFingerAcquired_doesNotWakeUpPowerManager() {
cleanupKeyguardUpdateMonitor();
mSpiedContext.getOrCreateTestableResources().addOverride(
mContext.getOrCreateTestableResources().addOverride(
com.android.internal.R.bool.kg_wake_on_acquire_start, false);
mKeyguardUpdateMonitor = new TestableKeyguardUpdateMonitor(mSpiedContext);
mKeyguardUpdateMonitor = new TestableKeyguardUpdateMonitor(mContext);
fingerprintAcquireStart();
verify(mPowerManager, never()).wakeUp(anyLong(), anyInt(), anyString());
@@ -1716,7 +1710,9 @@ public class KeyguardUpdateMonitorTest extends SysuiTestCase {
mAuthController, mTelephonyListenerManager,
mInteractionJankMonitor, mLatencyTracker, mActiveUnlockConfig,
mKeyguardUpdateMonitorLogger, mUiEventLogger, () -> mSessionTracker,
mPowerManager);
mPowerManager, mTrustManager, mSubscriptionManager, mUserManager,
mDreamManager, mDevicePolicyManager, mSensorPrivacyManager, mTelephonyManager,
mPackageManager, mFaceManager, mFingerprintManager, mBiometricManager);
setStrongAuthTracker(KeyguardUpdateMonitorTest.this.mStrongAuthTracker);
}