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 import android.widget.LinearLayout;
26 
27 public abstract class ConfirmDeviceCredentialBaseActivity extends SettingsActivity {
28 
29     private static final String STATE_IS_KEYGUARD_LOCKED = "STATE_IS_KEYGUARD_LOCKED";
30 
31     enum ConfirmCredentialTheme {
32         INTERNAL,
33         DARK,
34         WORK
35     }
36 
37     private boolean mRestoring;
38     private boolean mEnterAnimationPending;
39     private boolean mFirstTimeVisible = true;
40     private boolean mIsKeyguardLocked = false;
41     private ConfirmCredentialTheme mConfirmCredentialTheme;
42 
43     @Override
onCreate(Bundle savedState)44     protected void onCreate(Bundle savedState) {
45         int credentialOwnerUserId = Utils.getCredentialOwnerUserId(this,
46                 Utils.getUserIdFromBundle(this, getIntent().getExtras()));
47         if (UserManager.get(this).isManagedProfile(credentialOwnerUserId)) {
48             setTheme(R.style.Theme_ConfirmDeviceCredentialsWork);
49             mConfirmCredentialTheme = ConfirmCredentialTheme.WORK;
50         } else if (getIntent().getBooleanExtra(
51                 ConfirmDeviceCredentialBaseFragment.DARK_THEME, false)) {
52             setTheme(R.style.Theme_ConfirmDeviceCredentialsDark);
53             mConfirmCredentialTheme = ConfirmCredentialTheme.DARK;
54         } else {
55             setTheme(R.style.SetupWizardTheme_Light);
56             mConfirmCredentialTheme = ConfirmCredentialTheme.INTERNAL;
57         }
58         super.onCreate(savedState);
59 
60         if (mConfirmCredentialTheme == ConfirmCredentialTheme.INTERNAL) {
61             // Prevent the content parent from consuming the window insets because GlifLayout uses
62             // it to show the status bar background.
63             LinearLayout layout = (LinearLayout) findViewById(R.id.content_parent);
64             layout.setFitsSystemWindows(false);
65         }
66 
67         mIsKeyguardLocked = savedState == null
68                 ? getSystemService(KeyguardManager.class).isKeyguardLocked()
69                 : savedState.getBoolean(STATE_IS_KEYGUARD_LOCKED, false);
70         // If the activity is launched, not due to config change, when keyguard is locked and the
71         // flag is set, assume it's launched on top of keyguard on purpose.
72         // TODO: Don't abuse SHOW_WHEN_LOCKED and don't check isKeyguardLocked.
73         // Set extra SHOW_WHEN_LOCKED and WindowManager FLAG_SHOW_WHEN_LOCKED only if it's
74         // truly on top of keyguard on purpose
75         if (mIsKeyguardLocked && getIntent().getBooleanExtra(
76                 ConfirmDeviceCredentialBaseFragment.SHOW_WHEN_LOCKED, false)) {
77             getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
78         }
79         CharSequence msg = getIntent().getStringExtra(
80                 ConfirmDeviceCredentialBaseFragment.TITLE_TEXT);
81         setTitle(msg);
82         if (getActionBar() != null) {
83             getActionBar().setDisplayHomeAsUpEnabled(true);
84             getActionBar().setHomeButtonEnabled(true);
85         }
86         mRestoring = savedState != null;
87     }
88 
89     @Override
onSaveInstanceState(Bundle outState)90     public void onSaveInstanceState(Bundle outState) {
91         super.onSaveInstanceState(outState);
92         outState.putBoolean(STATE_IS_KEYGUARD_LOCKED, mIsKeyguardLocked);
93     }
94 
95     @Override
onOptionsItemSelected(MenuItem item)96     public boolean onOptionsItemSelected(MenuItem item) {
97         if (item.getItemId() == android.R.id.home) {
98             finish();
99             return true;
100         }
101         return super.onOptionsItemSelected(item);
102     }
103 
104     @Override
onResume()105     public void onResume() {
106         super.onResume();
107         if (!isChangingConfigurations() && !mRestoring
108                 && mConfirmCredentialTheme == ConfirmCredentialTheme.DARK && mFirstTimeVisible) {
109             mFirstTimeVisible = false;
110             prepareEnterAnimation();
111             mEnterAnimationPending = true;
112         }
113     }
114 
getFragment()115     private ConfirmDeviceCredentialBaseFragment getFragment() {
116         Fragment fragment = getFragmentManager().findFragmentById(R.id.main_content);
117         if (fragment != null && fragment instanceof ConfirmDeviceCredentialBaseFragment) {
118             return (ConfirmDeviceCredentialBaseFragment) fragment;
119         }
120         return null;
121     }
122 
123     @Override
onEnterAnimationComplete()124     public void onEnterAnimationComplete() {
125         super.onEnterAnimationComplete();
126         if (mEnterAnimationPending) {
127             startEnterAnimation();
128             mEnterAnimationPending = false;
129         }
130     }
131 
prepareEnterAnimation()132     public void prepareEnterAnimation() {
133         getFragment().prepareEnterAnimation();
134     }
135 
startEnterAnimation()136     public void startEnterAnimation() {
137         getFragment().startEnterAnimation();
138     }
139 
getConfirmCredentialTheme()140     public ConfirmCredentialTheme getConfirmCredentialTheme() {
141         return mConfirmCredentialTheme;
142     }
143 }
144