1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17 package com.android.settings.password; 18 19 import android.app.KeyguardManager; 20 import android.hardware.biometrics.BiometricConstants; 21 import android.hardware.biometrics.BiometricManager; 22 import android.hardware.biometrics.IBiometricConfirmDeviceCredentialCallback; 23 import android.os.Bundle; 24 import android.os.UserManager; 25 import android.util.Log; 26 import android.view.MenuItem; 27 import android.view.WindowManager; 28 import android.widget.LinearLayout; 29 30 import androidx.fragment.app.Fragment; 31 32 import com.android.settings.R; 33 import com.android.settings.SettingsActivity; 34 import com.android.settings.SetupWizardUtils; 35 import com.android.settings.Utils; 36 37 public abstract class ConfirmDeviceCredentialBaseActivity extends SettingsActivity { 38 39 private static final String STATE_IS_KEYGUARD_LOCKED = "STATE_IS_KEYGUARD_LOCKED"; 40 private static final String TAG = "ConfirmDeviceCredentialBaseActivity"; 41 42 enum ConfirmCredentialTheme { 43 NORMAL, 44 DARK, // TODO(yukl): Clean up DARK theme, as it should no longer be used 45 WORK 46 } 47 48 private boolean mRestoring; 49 private boolean mEnterAnimationPending; 50 private boolean mFirstTimeVisible = true; 51 private boolean mIsKeyguardLocked = false; 52 private ConfirmCredentialTheme mConfirmCredentialTheme; 53 private BiometricManager mBiometricManager; 54 55 // TODO(b/123378871): Remove when moved. 56 private final IBiometricConfirmDeviceCredentialCallback mCancelCallback 57 = new IBiometricConfirmDeviceCredentialCallback.Stub() { 58 @Override 59 public void cancel() { 60 finish(); 61 } 62 }; 63 isInternalActivity()64 private boolean isInternalActivity() { 65 return (this instanceof ConfirmLockPassword.InternalActivity) 66 || (this instanceof ConfirmLockPattern.InternalActivity); 67 } 68 69 @Override onCreate(Bundle savedState)70 protected void onCreate(Bundle savedState) { 71 final int credentialOwnerUserId; 72 try { 73 credentialOwnerUserId = Utils.getCredentialOwnerUserId(this, 74 Utils.getUserIdFromBundle(this, getIntent().getExtras(), isInternalActivity())); 75 } catch (SecurityException e) { 76 Log.e(TAG, "Invalid user Id supplied", e); 77 finish(); 78 return; 79 } 80 if (UserManager.get(this).isManagedProfile(credentialOwnerUserId)) { 81 setTheme(R.style.Theme_ConfirmDeviceCredentialsWork); 82 mConfirmCredentialTheme = ConfirmCredentialTheme.WORK; 83 } else if (getIntent().getBooleanExtra( 84 ConfirmDeviceCredentialBaseFragment.DARK_THEME, false)) { 85 setTheme(R.style.Theme_ConfirmDeviceCredentialsDark); 86 mConfirmCredentialTheme = ConfirmCredentialTheme.DARK; 87 } else { 88 setTheme(SetupWizardUtils.getTheme(getIntent())); 89 mConfirmCredentialTheme = ConfirmCredentialTheme.NORMAL; 90 } 91 super.onCreate(savedState); 92 93 mBiometricManager = getSystemService(BiometricManager.class); 94 mBiometricManager.registerCancellationCallback(mCancelCallback); 95 96 if (mConfirmCredentialTheme == ConfirmCredentialTheme.NORMAL) { 97 // Prevent the content parent from consuming the window insets because GlifLayout uses 98 // it to show the status bar background. 99 findViewById(R.id.content_parent).setFitsSystemWindows(false); 100 } 101 getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE); 102 mIsKeyguardLocked = savedState == null 103 ? getSystemService(KeyguardManager.class).isKeyguardLocked() 104 : savedState.getBoolean(STATE_IS_KEYGUARD_LOCKED, false); 105 // If the activity is launched, not due to config change, when keyguard is locked and the 106 // flag is set, assume it's launched on top of keyguard on purpose. 107 // TODO: Don't abuse SHOW_WHEN_LOCKED and don't check isKeyguardLocked. 108 // Set extra SHOW_WHEN_LOCKED and WindowManager FLAG_SHOW_WHEN_LOCKED only if it's 109 // truly on top of keyguard on purpose 110 if (mIsKeyguardLocked && getIntent().getBooleanExtra( 111 ConfirmDeviceCredentialBaseFragment.SHOW_WHEN_LOCKED, false)) { 112 getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); 113 } 114 CharSequence msg = getIntent().getStringExtra( 115 ConfirmDeviceCredentialBaseFragment.TITLE_TEXT); 116 setTitle(msg); 117 if (getActionBar() != null) { 118 getActionBar().setDisplayHomeAsUpEnabled(true); 119 getActionBar().setHomeButtonEnabled(true); 120 } 121 mRestoring = savedState != null; 122 } 123 124 @Override onSaveInstanceState(Bundle outState)125 public void onSaveInstanceState(Bundle outState) { 126 super.onSaveInstanceState(outState); 127 outState.putBoolean(STATE_IS_KEYGUARD_LOCKED, mIsKeyguardLocked); 128 } 129 130 @Override onOptionsItemSelected(MenuItem item)131 public boolean onOptionsItemSelected(MenuItem item) { 132 if (item.getItemId() == android.R.id.home) { 133 finish(); 134 return true; 135 } 136 return super.onOptionsItemSelected(item); 137 } 138 139 @Override onResume()140 public void onResume() { 141 super.onResume(); 142 if (!isChangingConfigurations() && !mRestoring 143 && mConfirmCredentialTheme == ConfirmCredentialTheme.DARK && mFirstTimeVisible) { 144 mFirstTimeVisible = false; 145 prepareEnterAnimation(); 146 mEnterAnimationPending = true; 147 } 148 } 149 getFragment()150 private ConfirmDeviceCredentialBaseFragment getFragment() { 151 Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.main_content); 152 if (fragment != null && fragment instanceof ConfirmDeviceCredentialBaseFragment) { 153 return (ConfirmDeviceCredentialBaseFragment) fragment; 154 } 155 return null; 156 } 157 158 @Override onEnterAnimationComplete()159 public void onEnterAnimationComplete() { 160 super.onEnterAnimationComplete(); 161 if (mEnterAnimationPending) { 162 startEnterAnimation(); 163 mEnterAnimationPending = false; 164 } 165 } 166 167 @Override onStop()168 public void onStop() { 169 super.onStop(); 170 // TODO(b/123378871): Remove when moved. 171 if (!isChangingConfigurations()) { 172 mBiometricManager.onConfirmDeviceCredentialError( 173 BiometricConstants.BIOMETRIC_ERROR_USER_CANCELED, 174 getString(com.android.internal.R.string.biometric_error_user_canceled)); 175 } 176 } 177 178 @Override finish()179 public void finish() { 180 super.finish(); 181 if (getIntent().getBooleanExtra( 182 ConfirmDeviceCredentialBaseFragment.USE_FADE_ANIMATION, false)) { 183 overridePendingTransition(0, R.anim.confirm_credential_biometric_transition_exit); 184 } 185 } 186 187 @Override isLaunchableInTaskModePinned()188 public boolean isLaunchableInTaskModePinned() { 189 return true; 190 } 191 prepareEnterAnimation()192 public void prepareEnterAnimation() { 193 getFragment().prepareEnterAnimation(); 194 } 195 startEnterAnimation()196 public void startEnterAnimation() { 197 getFragment().startEnterAnimation(); 198 } 199 getConfirmCredentialTheme()200 public ConfirmCredentialTheme getConfirmCredentialTheme() { 201 return mConfirmCredentialTheme; 202 } 203 } 204