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.fingerprint;
18
19import android.app.Activity;
20import android.app.KeyguardManager;
21import android.app.admin.DevicePolicyManager;
22import android.content.Intent;
23import android.os.Bundle;
24import android.os.UserHandle;
25import android.widget.Button;
26import android.widget.TextView;
27
28import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
29import com.android.internal.widget.LockPatternUtils;
30import com.android.settings.R;
31import com.android.settings.SetupWizardUtils;
32import com.android.settings.password.ChooseLockGeneric.ChooseLockGenericFragment;
33import com.android.settings.password.SetupChooseLockGeneric;
34import com.android.settings.password.SetupSkipDialog;
35import com.android.settings.password.StorageManagerWrapper;
36
37public class SetupFingerprintEnrollIntroduction extends FingerprintEnrollIntroduction {
38    private static final String KEY_LOCK_SCREEN_PRESENT = "wasLockScreenPresent";
39    private boolean mAlreadyHadLockScreenSetup = false;
40
41    @Override
42    protected void onCreate(Bundle savedInstanceState) {
43        super.onCreate(savedInstanceState);
44        if (savedInstanceState == null) {
45            mAlreadyHadLockScreenSetup = isKeyguardSecure();
46        } else {
47            mAlreadyHadLockScreenSetup = savedInstanceState.getBoolean(
48                    KEY_LOCK_SCREEN_PRESENT, false);
49        }
50    }
51
52    @Override
53    protected void onSaveInstanceState(Bundle outState) {
54        super.onSaveInstanceState(outState);
55        outState.putBoolean(KEY_LOCK_SCREEN_PRESENT, mAlreadyHadLockScreenSetup);
56    }
57
58    @Override
59    protected Intent getChooseLockIntent() {
60        Intent intent = new Intent(this, SetupChooseLockGeneric.class);
61
62        if (StorageManagerWrapper.isFileEncryptedNativeOrEmulated()) {
63            intent.putExtra(
64                    LockPatternUtils.PASSWORD_TYPE_KEY,
65                    DevicePolicyManager.PASSWORD_QUALITY_NUMERIC);
66            intent.putExtra(ChooseLockGenericFragment.EXTRA_SHOW_OPTIONS_BUTTON, true);
67        }
68        SetupWizardUtils.copySetupExtras(getIntent(), intent);
69        return intent;
70    }
71
72    @Override
73    protected Intent getFindSensorIntent() {
74        final Intent intent = new Intent(this, SetupFingerprintEnrollFindSensor.class);
75        SetupWizardUtils.copySetupExtras(getIntent(), intent);
76        return intent;
77    }
78
79    @Override
80    protected void initViews() {
81        super.initViews();
82
83        TextView description = (TextView) findViewById(R.id.description_text);
84        description.setText(
85                R.string.security_settings_fingerprint_enroll_introduction_message_setup);
86
87        Button nextButton = getNextButton();
88        nextButton.setText(
89                R.string.security_settings_fingerprint_enroll_introduction_continue_setup);
90
91        final Button cancelButton = (Button) findViewById(R.id.fingerprint_cancel_button);
92        cancelButton.setText(
93                R.string.security_settings_fingerprint_enroll_introduction_cancel_setup);
94    }
95
96    @Override
97    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
98        // if lock was already present, do not return intent data since it must have been
99        // reported in previous attempts
100        if (requestCode == FINGERPRINT_FIND_SENSOR_REQUEST && isKeyguardSecure()
101                && !mAlreadyHadLockScreenSetup) {
102            data = getMetricIntent(data);
103        }
104        super.onActivityResult(requestCode, resultCode, data);
105    }
106
107    private Intent getMetricIntent(Intent data) {
108        if (data == null) {
109            data = new Intent();
110        }
111        LockPatternUtils lockPatternUtils = new LockPatternUtils(this);
112        data.putExtra(SetupChooseLockGeneric.
113                SetupChooseLockGenericFragment.EXTRA_PASSWORD_QUALITY,
114                lockPatternUtils.getKeyguardStoredPasswordQuality(UserHandle.myUserId()));
115        return data;
116    }
117
118    @Override
119    protected void onCancelButtonClick() {
120        if (isKeyguardSecure()) {
121            // If the keyguard is already set up securely (maybe the user added a backup screen
122            // lock and skipped fingerprint), return RESULT_SKIP directly.
123            setResult(RESULT_SKIP, mAlreadyHadLockScreenSetup ? null : getMetricIntent(null));
124            finish();
125        } else {
126            setResult(SetupSkipDialog.RESULT_SKIP);
127            finish();
128        }
129    }
130
131    /**
132     * Propagate lock screen metrics if the user goes back from the fingerprint setup screen
133     * after having added lock screen to his device.
134     */
135    @Override
136    public void onBackPressed() {
137        if (!mAlreadyHadLockScreenSetup && isKeyguardSecure()) {
138            setResult(Activity.RESULT_CANCELED, getMetricIntent(null));
139        }
140        super.onBackPressed();
141    }
142
143    private boolean isKeyguardSecure() {
144        return getSystemService(KeyguardManager.class).isKeyguardSecure();
145    }
146
147    @Override
148    public int getMetricsCategory() {
149        return MetricsEvent.FINGERPRINT_ENROLL_INTRO_SETUP;
150    }
151}
152