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; 18 19 import android.app.Fragment; 20 import android.app.KeyguardManager; 21 import android.os.Bundle; 22 import android.os.UserManager; 23 import android.view.MenuItem; 24 import android.view.WindowManager; 25 26 public abstract class ConfirmDeviceCredentialBaseActivity extends SettingsActivity { 27 28 private static final String STATE_IS_KEYGUARD_LOCKED = "STATE_IS_KEYGUARD_LOCKED"; 29 30 private boolean mRestoring; 31 private boolean mDark; 32 private boolean mEnterAnimationPending; 33 private boolean mFirstTimeVisible = true; 34 private boolean mIsKeyguardLocked = false; 35 36 @Override onCreate(Bundle savedState)37 protected void onCreate(Bundle savedState) { 38 int credentialOwnerUserId = Utils.getCredentialOwnerUserId(this, 39 Utils.getUserIdFromBundle(this, getIntent().getExtras())); 40 if (Utils.isManagedProfile(UserManager.get(this), credentialOwnerUserId)) { 41 setTheme(R.style.Theme_ConfirmDeviceCredentialsWork); 42 } else if (getIntent().getBooleanExtra( 43 ConfirmDeviceCredentialBaseFragment.DARK_THEME, false)) { 44 setTheme(R.style.Theme_ConfirmDeviceCredentialsDark); 45 mDark = true; 46 } 47 super.onCreate(savedState); 48 mIsKeyguardLocked = savedState == null 49 ? getSystemService(KeyguardManager.class).isKeyguardLocked() 50 : savedState.getBoolean(STATE_IS_KEYGUARD_LOCKED, false); 51 // If the activity is launched, not due to config change, when keyguard is locked and the 52 // flag is set, assume it's launched on top of keyguard on purpose. 53 // TODO: Don't abuse SHOW_WHEN_LOCKED and don't check isKeyguardLocked. 54 // Set extra SHOW_WHEN_LOCKED and WindowManager FLAG_SHOW_WHEN_LOCKED only if it's 55 // truly on top of keyguard on purpose 56 if (mIsKeyguardLocked && getIntent().getBooleanExtra( 57 ConfirmDeviceCredentialBaseFragment.SHOW_WHEN_LOCKED, false)) { 58 getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); 59 } 60 CharSequence msg = getIntent().getStringExtra( 61 ConfirmDeviceCredentialBaseFragment.TITLE_TEXT); 62 setTitle(msg); 63 if (getActionBar() != null) { 64 getActionBar().setDisplayHomeAsUpEnabled(true); 65 getActionBar().setHomeButtonEnabled(true); 66 } 67 mRestoring = savedState != null; 68 } 69 70 @Override onSaveInstanceState(Bundle outState)71 public void onSaveInstanceState(Bundle outState) { 72 super.onSaveInstanceState(outState); 73 outState.putBoolean(STATE_IS_KEYGUARD_LOCKED, mIsKeyguardLocked); 74 } 75 76 @Override onOptionsItemSelected(MenuItem item)77 public boolean onOptionsItemSelected(MenuItem item) { 78 if (item.getItemId() == android.R.id.home) { 79 finish(); 80 return true; 81 } 82 return super.onOptionsItemSelected(item); 83 } 84 85 @Override onResume()86 public void onResume() { 87 super.onResume(); 88 if (!isChangingConfigurations() && !mRestoring && mDark && mFirstTimeVisible) { 89 mFirstTimeVisible = false; 90 prepareEnterAnimation(); 91 mEnterAnimationPending = true; 92 } 93 } 94 getFragment()95 private ConfirmDeviceCredentialBaseFragment getFragment() { 96 Fragment fragment = getFragmentManager().findFragmentById(R.id.main_content); 97 if (fragment != null && fragment instanceof ConfirmDeviceCredentialBaseFragment) { 98 return (ConfirmDeviceCredentialBaseFragment) fragment; 99 } 100 return null; 101 } 102 103 @Override onEnterAnimationComplete()104 public void onEnterAnimationComplete() { 105 super.onEnterAnimationComplete(); 106 if (mEnterAnimationPending) { 107 startEnterAnimation(); 108 mEnterAnimationPending = false; 109 } 110 } 111 prepareEnterAnimation()112 public void prepareEnterAnimation() { 113 getFragment().prepareEnterAnimation(); 114 } 115 startEnterAnimation()116 public void startEnterAnimation() { 117 getFragment().startEnterAnimation(); 118 } 119 } 120