OSDN Git Service

Merge "Fix hard-coded colors for accessibility screens to work with night mode" into...
[android-x86/packages-apps-Settings.git] / src / com / android / settings / fingerprint / FingerprintEnrollIntroduction.java
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.fingerprint;
18
19 import android.app.Activity;
20 import android.app.admin.DevicePolicyManager;
21 import android.content.ActivityNotFoundException;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.hardware.fingerprint.FingerprintManager;
25 import android.graphics.Typeface;
26 import android.graphics.drawable.Drawable;
27 import android.os.Bundle;
28 import android.os.UserHandle;
29 import android.text.Annotation;
30 import android.text.SpannableString;
31 import android.text.SpannableStringBuilder;
32 import android.text.TextPaint;
33 import android.text.method.LinkMovementMethod;
34 import android.text.style.URLSpan;
35 import android.util.Log;
36 import android.view.View;
37 import android.widget.AdapterView;
38
39 import com.android.internal.logging.MetricsProto.MetricsEvent;
40 import com.android.settings.ChooseLockGeneric;
41 import com.android.settings.ChooseLockSettingsHelper;
42 import com.android.settings.HelpUtils;
43 import com.android.settings.R;
44 import com.android.setupwizardlib.SetupWizardItemsLayout;
45 import com.android.setupwizardlib.items.Item;
46 import com.android.setupwizardlib.items.ItemAdapter;
47
48 /**
49  * Onboarding activity for fingerprint enrollment.
50  */
51 public class FingerprintEnrollIntroduction extends FingerprintEnrollBase
52         implements AdapterView.OnItemClickListener {
53
54     protected static final int CHOOSE_LOCK_GENERIC_REQUEST = 1;
55     protected static final int FINGERPRINT_FIND_SENSOR_REQUEST = 2;
56     protected static final int LEARN_MORE_REQUEST = 3;
57
58     private boolean mHasPassword;
59
60     @Override
61     protected void onCreate(Bundle savedInstanceState) {
62         super.onCreate(savedInstanceState);
63         setContentView(R.layout.fingerprint_enroll_introduction);
64         setHeaderText(R.string.security_settings_fingerprint_enroll_introduction_title);
65         final SetupWizardItemsLayout layout =
66                 (SetupWizardItemsLayout) findViewById(R.id.setup_wizard_layout);
67         layout.getListView().setOnItemClickListener(this);
68         final ItemAdapter adapter = (ItemAdapter) layout.getAdapter();
69         Item item = (Item) adapter.findItemById(R.id.fingerprint_introduction_message);
70         item.setTitle(LearnMoreSpan.linkify(
71                 getText(R.string.security_settings_fingerprint_enroll_introduction_message),
72                 getString(R.string.help_url_fingerprint)));
73         updatePasswordQuality();
74     }
75
76     private void updatePasswordQuality() {
77         final int passwordQuality = new ChooseLockSettingsHelper(this).utils()
78                 .getActivePasswordQuality(mUserId);
79         mHasPassword = passwordQuality != DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED;
80     }
81
82     @Override
83     protected void onNextButtonClick() {
84         if (!mHasPassword) {
85             // No fingerprints registered, launch into enrollment wizard.
86             launchChooseLock();
87         } else {
88             // Lock thingy is already set up, launch directly into find sensor step from wizard.
89             launchFindSensor(null);
90         }
91     }
92
93     private void launchChooseLock() {
94         Intent intent = getChooseLockIntent();
95         long challenge = getSystemService(FingerprintManager.class).preEnroll();
96         intent.putExtra(ChooseLockGeneric.ChooseLockGenericFragment.MINIMUM_QUALITY_KEY,
97                 DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
98         intent.putExtra(ChooseLockGeneric.ChooseLockGenericFragment.HIDE_DISABLED_PREFS, true);
99         intent.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_HAS_CHALLENGE, true);
100         intent.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE, challenge);
101         intent.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_FOR_FINGERPRINT, true);
102         if (mUserId != UserHandle.USER_NULL) {
103             intent.putExtra(Intent.EXTRA_USER_ID, mUserId);
104         }
105         startActivityForResult(intent, CHOOSE_LOCK_GENERIC_REQUEST);
106     }
107
108     private void launchFindSensor(byte[] token) {
109         Intent intent = getFindSensorIntent();
110         if (token != null) {
111             intent.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE_TOKEN, token);
112         }
113         if (mUserId != UserHandle.USER_NULL) {
114             intent.putExtra(Intent.EXTRA_USER_ID, mUserId);
115         }
116         startActivityForResult(intent, FINGERPRINT_FIND_SENSOR_REQUEST);
117     }
118
119     protected Intent getChooseLockIntent() {
120         return new Intent(this, ChooseLockGeneric.class);
121     }
122
123     protected Intent getFindSensorIntent() {
124         Intent intent = new Intent(this, FingerprintEnrollFindSensor.class);
125         return intent;
126     }
127
128     @Override
129     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
130         final boolean isResultFinished = resultCode == RESULT_FINISHED;
131         if (requestCode == FINGERPRINT_FIND_SENSOR_REQUEST) {
132             if (isResultFinished || resultCode == RESULT_SKIP) {
133                 final int result = isResultFinished ? RESULT_OK : RESULT_SKIP;
134                 setResult(result, data);
135                 finish();
136                 return;
137             }
138         } else if (requestCode == CHOOSE_LOCK_GENERIC_REQUEST) {
139             if (isResultFinished) {
140                 updatePasswordQuality();
141                 byte[] token = data.getByteArrayExtra(
142                         ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE_TOKEN);
143                 launchFindSensor(token);
144                 return;
145             }
146         }
147         super.onActivityResult(requestCode, resultCode, data);
148     }
149
150     @Override
151     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
152         final Item item = (Item) parent.getItemAtPosition(position);
153         switch (item.getId()) {
154             case R.id.next_button:
155                 onNextButtonClick();
156                 break;
157             case R.id.cancel_button:
158                 onCancelButtonClick();
159                 break;
160         }
161     }
162
163     @Override
164     protected int getMetricsCategory() {
165         return MetricsEvent.FINGERPRINT_ENROLL_INTRO;
166     }
167
168     protected void onCancelButtonClick() {
169         finish();
170     }
171
172     private static class LearnMoreSpan extends URLSpan {
173         private static final String TAG = "LearnMoreSpan";
174         private static final Typeface TYPEFACE_MEDIUM =
175                 Typeface.create("sans-serif-medium", Typeface.NORMAL);
176
177         private LearnMoreSpan(String url) {
178             super(url);
179         }
180
181         @Override
182         public void onClick(View widget) {
183             Context ctx = widget.getContext();
184             Intent intent = HelpUtils.getHelpIntent(ctx, getURL(), ctx.getClass().getName());
185             try {
186                 // This needs to be startActivityForResult even though we do not care about the
187                 // actual result because the help app needs to know about who invoked it.
188                 widget.startActivityForResult(intent, LEARN_MORE_REQUEST);
189             } catch (ActivityNotFoundException e) {
190                 Log.w(LearnMoreSpan.TAG,
191                         "Actvity was not found for intent, " + intent.toString());
192             }
193         }
194
195         @Override
196         public void updateDrawState(TextPaint ds) {
197             super.updateDrawState(ds);
198             ds.setUnderlineText(false);
199             ds.setTypeface(TYPEFACE_MEDIUM);
200         }
201
202         public static CharSequence linkify(CharSequence rawText, String uri) {
203             SpannableString msg = new SpannableString(rawText);
204             Annotation[] spans = msg.getSpans(0, msg.length(), Annotation.class);
205             SpannableStringBuilder builder = new SpannableStringBuilder(msg);
206             for (Annotation annotation : spans) {
207                 int start = msg.getSpanStart(annotation);
208                 int end = msg.getSpanEnd(annotation);
209                 LearnMoreSpan link = new LearnMoreSpan(uri);
210                 builder.setSpan(link, start, end, msg.getSpanFlags(link));
211             }
212             return builder;
213         }
214     }
215 }