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
17package com.android.settings;
18
19import android.app.Fragment;
20import android.app.KeyguardManager;
21import android.os.Bundle;
22import android.view.MenuItem;
23import android.view.WindowManager;
24
25public abstract class ConfirmDeviceCredentialBaseActivity extends SettingsActivity {
26
27    private boolean mRestoring;
28    private boolean mDark;
29    private boolean mEnterAnimationPending;
30    private boolean mFirstTimeVisible = true;
31
32    @Override
33    protected void onCreate(Bundle savedState) {
34        if (getIntent().getBooleanExtra(ConfirmDeviceCredentialBaseFragment.DARK_THEME, false)) {
35            setTheme(R.style.Theme_ConfirmDeviceCredentialsDark);
36            mDark = true;
37        }
38        super.onCreate(savedState);
39        boolean deviceLocked = getSystemService(KeyguardManager.class).isKeyguardLocked();
40        if (deviceLocked && getIntent().getBooleanExtra(
41                ConfirmDeviceCredentialBaseFragment.SHOW_WHEN_LOCKED, false)) {
42            getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
43        }
44        CharSequence msg = getIntent().getStringExtra(
45                ConfirmDeviceCredentialBaseFragment.TITLE_TEXT);
46        setTitle(msg);
47        if (getActionBar() != null) {
48            getActionBar().setDisplayHomeAsUpEnabled(true);
49            getActionBar().setHomeButtonEnabled(true);
50        }
51        mRestoring = savedState != null;
52    }
53
54    @Override
55    public boolean onOptionsItemSelected(MenuItem item) {
56        if (item.getItemId() == android.R.id.home) {
57            finish();
58            return true;
59        }
60        return super.onOptionsItemSelected(item);
61    }
62
63    @Override
64    public void onResume() {
65        super.onResume();
66        if (!isChangingConfigurations() && !mRestoring && mDark && mFirstTimeVisible) {
67            mFirstTimeVisible = false;
68            prepareEnterAnimation();
69            mEnterAnimationPending = true;
70        }
71    }
72
73    private ConfirmDeviceCredentialBaseFragment getFragment() {
74        Fragment fragment = getFragmentManager().findFragmentById(R.id.main_content);
75        if (fragment != null && fragment instanceof ConfirmDeviceCredentialBaseFragment) {
76            return (ConfirmDeviceCredentialBaseFragment) fragment;
77        }
78        return null;
79    }
80
81    @Override
82    public void onEnterAnimationComplete() {
83        super.onEnterAnimationComplete();
84        if (mEnterAnimationPending) {
85            startEnterAnimation();
86            mEnterAnimationPending = false;
87        }
88    }
89
90    public void prepareEnterAnimation() {
91        getFragment().prepareEnterAnimation();
92    }
93
94    public void startEnterAnimation() {
95        getFragment().startEnterAnimation();
96    }
97}
98