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 82 if (ThemeHelper.shouldApplyGlifExpressiveStyle(getApplicationContext())) { 83 if (!ThemeHelper.trySetSuwTheme(this)) { 84 setTheme(ThemeHelper.getSuwDefaultTheme(getApplicationContext())); 85 ThemeHelper.trySetDynamicColor(this); 86 } 87 } else { 88 ThemeHelper.trySetDynamicColor(this); 89 } 90 super.onCreate(savedState); 91 92 if (mConfirmCredentialTheme == ConfirmCredentialTheme.NORMAL) { 93 // Prevent the content parent from consuming the window insets because GlifLayout uses 94 // it to show the status bar background. 95 findViewById(R.id.content_parent).setFitsSystemWindows(false); 96 } 97 getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE); 98 mIsKeyguardLocked = savedState == null 99 ? getSystemService(KeyguardManager.class).isKeyguardLocked() 100 : savedState.getBoolean(STATE_IS_KEYGUARD_LOCKED, false); 101 // If the activity is launched, not due to config change, when keyguard is locked and the 102 // flag is set, assume it's launched on top of keyguard on purpose. 103 // TODO: Don't abuse SHOW_WHEN_LOCKED and don't check isKeyguardLocked. 104 // Set extra SHOW_WHEN_LOCKED and WindowManager FLAG_SHOW_WHEN_LOCKED only if it's 105 // truly on top of keyguard on purpose 106 if (mIsKeyguardLocked && getIntent().getBooleanExtra( 107 ConfirmDeviceCredentialBaseFragment.SHOW_WHEN_LOCKED, false)) { 108 getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); 109 } 110 CharSequence msg = getIntent().getStringExtra( 111 ConfirmDeviceCredentialBaseFragment.TITLE_TEXT); 112 setTitle(msg); 113 if (getActionBar() != null) { 114 getActionBar().setDisplayHomeAsUpEnabled(true); 115 getActionBar().setHomeButtonEnabled(true); 116 } 117 mRestoring = savedState != null; 118 } 119 120 @Override onSaveInstanceState(Bundle outState)121 public void onSaveInstanceState(Bundle outState) { 122 super.onSaveInstanceState(outState); 123 outState.putBoolean(STATE_IS_KEYGUARD_LOCKED, mIsKeyguardLocked); 124 } 125 126 @Override onOptionsItemSelected(MenuItem item)127 public boolean onOptionsItemSelected(MenuItem item) { 128 if (item.getItemId() == android.R.id.home) { 129 finish(); 130 return true; 131 } 132 return super.onOptionsItemSelected(item); 133 } 134 135 @Override onResume()136 public void onResume() { 137 super.onResume(); 138 if (!isChangingConfigurations() && !mRestoring 139 && mConfirmCredentialTheme == ConfirmCredentialTheme.DARK && mFirstTimeVisible) { 140 mFirstTimeVisible = false; 141 prepareEnterAnimation(); 142 mEnterAnimationPending = true; 143 } 144 } 145 getFragment()146 private ConfirmDeviceCredentialBaseFragment getFragment() { 147 Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.main_content); 148 if (fragment != null && fragment instanceof ConfirmDeviceCredentialBaseFragment) { 149 return (ConfirmDeviceCredentialBaseFragment) fragment; 150 } 151 return null; 152 } 153 154 @Override onEnterAnimationComplete()155 public void onEnterAnimationComplete() { 156 super.onEnterAnimationComplete(); 157 if (mEnterAnimationPending) { 158 startEnterAnimation(); 159 mEnterAnimationPending = false; 160 } 161 } 162 163 @Override onStop()164 public void onStop() { 165 super.onStop(); 166 final boolean foregroundOnly = getIntent().getBooleanExtra( 167 ChooseLockSettingsHelper.EXTRA_KEY_FOREGROUND_ONLY, false); 168 if (!isChangingConfigurations() && foregroundOnly) { 169 finish(); 170 } 171 } 172 173 @Override onDestroy()174 public void onDestroy() { 175 super.onDestroy(); 176 // Force a garbage collection to remove remnant of user password shards from memory. 177 // Execute this with a slight delay to allow the activity lifecycle to complete and 178 // the instance to become gc-able. 179 new Handler(Looper.myLooper()).postDelayed(() -> { 180 System.gc(); 181 System.runFinalization(); 182 System.gc(); 183 }, 5000); 184 } 185 186 @Override finish()187 public void finish() { 188 super.finish(); 189 if (getIntent().getBooleanExtra( 190 ConfirmDeviceCredentialBaseFragment.USE_FADE_ANIMATION, false)) { 191 overridePendingTransition(0, R.anim.confirm_credential_biometric_transition_exit); 192 } 193 } 194 195 @Override isToolbarEnabled()196 protected boolean isToolbarEnabled() { 197 return false; 198 } 199 prepareEnterAnimation()200 public void prepareEnterAnimation() { 201 getFragment().prepareEnterAnimation(); 202 } 203 startEnterAnimation()204 public void startEnterAnimation() { 205 getFragment().startEnterAnimation(); 206 } 207 getConfirmCredentialTheme()208 public ConfirmCredentialTheme getConfirmCredentialTheme() { 209 return mConfirmCredentialTheme; 210 } 211 } 212