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.os.Bundle; 21 import android.os.Handler; 22 import android.os.Looper; 23 import android.os.UserManager; 24 import android.util.Log; 25 import android.view.MenuItem; 26 import android.view.WindowManager; 27 28 import androidx.fragment.app.Fragment; 29 30 import com.android.settings.R; 31 import com.android.settings.SettingsActivity; 32 import com.android.settings.SetupWizardUtils; 33 import com.android.settings.Utils; 34 35 import com.google.android.setupdesign.util.ThemeHelper; 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 isInternalActivity()54 private boolean isInternalActivity() { 55 return (this instanceof ConfirmLockPassword.InternalActivity) 56 || (this instanceof ConfirmLockPattern.InternalActivity); 57 } 58 59 @Override onCreate(Bundle savedState)60 protected void onCreate(Bundle savedState) { 61 final int credentialOwnerUserId; 62 try { 63 credentialOwnerUserId = Utils.getCredentialOwnerUserId(this, 64 Utils.getUserIdFromBundle(this, getIntent().getExtras(), isInternalActivity())); 65 } catch (SecurityException e) { 66 Log.e(TAG, "Invalid user Id supplied", e); 67 finish(); 68 return; 69 } 70 if (UserManager.get(this).isManagedProfile(credentialOwnerUserId)) { 71 setTheme(SetupWizardUtils.getTheme(this, getIntent())); 72 mConfirmCredentialTheme = ConfirmCredentialTheme.WORK; 73 } else if (getIntent().getBooleanExtra( 74 ConfirmDeviceCredentialBaseFragment.DARK_THEME, false)) { 75 setTheme(R.style.Theme_ConfirmDeviceCredentialsDark); 76 mConfirmCredentialTheme = ConfirmCredentialTheme.DARK; 77 } else { 78 setTheme(SetupWizardUtils.getTheme(this, getIntent())); 79 mConfirmCredentialTheme = ConfirmCredentialTheme.NORMAL; 80 } 81 ThemeHelper.trySetDynamicColor(this); 82 super.onCreate(savedState); 83 84 if (mConfirmCredentialTheme == ConfirmCredentialTheme.NORMAL) { 85 // Prevent the content parent from consuming the window insets because GlifLayout uses 86 // it to show the status bar background. 87 findViewById(R.id.content_parent).setFitsSystemWindows(false); 88 } 89 getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE); 90 mIsKeyguardLocked = savedState == null 91 ? getSystemService(KeyguardManager.class).isKeyguardLocked() 92 : savedState.getBoolean(STATE_IS_KEYGUARD_LOCKED, false); 93 // If the activity is launched, not due to config change, when keyguard is locked and the 94 // flag is set, assume it's launched on top of keyguard on purpose. 95 // TODO: Don't abuse SHOW_WHEN_LOCKED and don't check isKeyguardLocked. 96 // Set extra SHOW_WHEN_LOCKED and WindowManager FLAG_SHOW_WHEN_LOCKED only if it's 97 // truly on top of keyguard on purpose 98 if (mIsKeyguardLocked && getIntent().getBooleanExtra( 99 ConfirmDeviceCredentialBaseFragment.SHOW_WHEN_LOCKED, false)) { 100 getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); 101 } 102 CharSequence msg = getIntent().getStringExtra( 103 ConfirmDeviceCredentialBaseFragment.TITLE_TEXT); 104 setTitle(msg); 105 if (getActionBar() != null) { 106 getActionBar().setDisplayHomeAsUpEnabled(true); 107 getActionBar().setHomeButtonEnabled(true); 108 } 109 mRestoring = savedState != null; 110 } 111 112 @Override onSaveInstanceState(Bundle outState)113 public void onSaveInstanceState(Bundle outState) { 114 super.onSaveInstanceState(outState); 115 outState.putBoolean(STATE_IS_KEYGUARD_LOCKED, mIsKeyguardLocked); 116 } 117 118 @Override onOptionsItemSelected(MenuItem item)119 public boolean onOptionsItemSelected(MenuItem item) { 120 if (item.getItemId() == android.R.id.home) { 121 finish(); 122 return true; 123 } 124 return super.onOptionsItemSelected(item); 125 } 126 127 @Override onResume()128 public void onResume() { 129 super.onResume(); 130 if (!isChangingConfigurations() && !mRestoring 131 && mConfirmCredentialTheme == ConfirmCredentialTheme.DARK && mFirstTimeVisible) { 132 mFirstTimeVisible = false; 133 prepareEnterAnimation(); 134 mEnterAnimationPending = true; 135 } 136 } 137 getFragment()138 private ConfirmDeviceCredentialBaseFragment getFragment() { 139 Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.main_content); 140 if (fragment != null && fragment instanceof ConfirmDeviceCredentialBaseFragment) { 141 return (ConfirmDeviceCredentialBaseFragment) fragment; 142 } 143 return null; 144 } 145 146 @Override onEnterAnimationComplete()147 public void onEnterAnimationComplete() { 148 super.onEnterAnimationComplete(); 149 if (mEnterAnimationPending) { 150 startEnterAnimation(); 151 mEnterAnimationPending = false; 152 } 153 } 154 155 @Override onStop()156 public void onStop() { 157 super.onStop(); 158 final boolean foregroundOnly = getIntent().getBooleanExtra( 159 ChooseLockSettingsHelper.EXTRA_KEY_FOREGROUND_ONLY, false); 160 if (!isChangingConfigurations() && foregroundOnly) { 161 finish(); 162 } 163 } 164 165 @Override onDestroy()166 public void onDestroy() { 167 super.onDestroy(); 168 // Force a garbage collection to remove remnant of user password shards from memory. 169 // Execute this with a slight delay to allow the activity lifecycle to complete and 170 // the instance to become gc-able. 171 new Handler(Looper.myLooper()).postDelayed(() -> { 172 System.gc(); 173 System.runFinalization(); 174 System.gc(); 175 }, 5000); 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 isToolbarEnabled()188 protected boolean isToolbarEnabled() { 189 return false; 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