2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
17 package com.android.settings.fingerprint;
19 import android.app.admin.DevicePolicyManager;
20 import android.content.ActivityNotFoundException;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.graphics.Typeface;
24 import android.hardware.fingerprint.FingerprintManager;
25 import android.os.Bundle;
26 import android.os.UserHandle;
27 import android.os.UserManager;
28 import android.text.Annotation;
29 import android.text.SpannableString;
30 import android.text.SpannableStringBuilder;
31 import android.text.TextPaint;
32 import android.text.style.URLSpan;
33 import android.util.Log;
34 import android.view.View;
36 import com.android.internal.logging.MetricsProto.MetricsEvent;
37 import com.android.settings.ChooseLockGeneric;
38 import com.android.settings.ChooseLockSettingsHelper;
39 import com.android.settingslib.HelpUtils;
40 import com.android.settings.R;
41 import com.android.setupwizardlib.SetupWizardRecyclerLayout;
42 import com.android.setupwizardlib.items.IItem;
43 import com.android.setupwizardlib.items.Item;
44 import com.android.setupwizardlib.items.RecyclerItemAdapter;
47 * Onboarding activity for fingerprint enrollment.
49 public class FingerprintEnrollIntroduction extends FingerprintEnrollBase
50 implements RecyclerItemAdapter.OnItemSelectedListener {
52 protected static final int CHOOSE_LOCK_GENERIC_REQUEST = 1;
53 protected static final int FINGERPRINT_FIND_SENSOR_REQUEST = 2;
54 protected static final int LEARN_MORE_REQUEST = 3;
56 private UserManager mUserManager;
57 private boolean mHasPassword;
60 protected void onCreate(Bundle savedInstanceState) {
61 super.onCreate(savedInstanceState);
62 setContentView(R.layout.fingerprint_enroll_introduction);
63 setHeaderText(R.string.security_settings_fingerprint_enroll_introduction_title);
64 final SetupWizardRecyclerLayout layout =
65 (SetupWizardRecyclerLayout) findViewById(R.id.setup_wizard_layout);
66 mUserManager = UserManager.get(this);
67 final RecyclerItemAdapter adapter = (RecyclerItemAdapter) layout.getAdapter();
68 adapter.setOnItemSelectedListener(this);
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 // setupwizard library automatically sets the divider inset to
74 // R.dimen.suw_items_icon_divider_inset. We adjust this back to 0 as we do not want
75 // an inset within settings.
76 layout.setDividerInset(0);
77 updatePasswordQuality();
80 private void updatePasswordQuality() {
81 final int passwordQuality = new ChooseLockSettingsHelper(this).utils()
82 .getActivePasswordQuality(mUserManager.getCredentialOwnerProfile(mUserId));
83 mHasPassword = passwordQuality != DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED;
87 protected void onNextButtonClick() {
89 // No fingerprints registered, launch into enrollment wizard.
92 // Lock thingy is already set up, launch directly into find sensor step from wizard.
93 launchFindSensor(null);
97 private void launchChooseLock() {
98 Intent intent = getChooseLockIntent();
99 long challenge = getSystemService(FingerprintManager.class).preEnroll();
100 intent.putExtra(ChooseLockGeneric.ChooseLockGenericFragment.MINIMUM_QUALITY_KEY,
101 DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
102 intent.putExtra(ChooseLockGeneric.ChooseLockGenericFragment.HIDE_DISABLED_PREFS, true);
103 intent.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_HAS_CHALLENGE, true);
104 intent.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE, challenge);
105 intent.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_FOR_FINGERPRINT, true);
106 if (mUserId != UserHandle.USER_NULL) {
107 intent.putExtra(Intent.EXTRA_USER_ID, mUserId);
109 startActivityForResult(intent, CHOOSE_LOCK_GENERIC_REQUEST);
112 private void launchFindSensor(byte[] token) {
113 Intent intent = getFindSensorIntent();
115 intent.putExtra(ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE_TOKEN, token);
117 if (mUserId != UserHandle.USER_NULL) {
118 intent.putExtra(Intent.EXTRA_USER_ID, mUserId);
120 startActivityForResult(intent, FINGERPRINT_FIND_SENSOR_REQUEST);
123 protected Intent getChooseLockIntent() {
124 return new Intent(this, ChooseLockGeneric.class);
127 protected Intent getFindSensorIntent() {
128 Intent intent = new Intent(this, FingerprintEnrollFindSensor.class);
133 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
134 final boolean isResultFinished = resultCode == RESULT_FINISHED;
135 if (requestCode == FINGERPRINT_FIND_SENSOR_REQUEST) {
136 if (isResultFinished || resultCode == RESULT_SKIP) {
137 final int result = isResultFinished ? RESULT_OK : RESULT_SKIP;
138 setResult(result, data);
142 } else if (requestCode == CHOOSE_LOCK_GENERIC_REQUEST) {
143 if (isResultFinished) {
144 updatePasswordQuality();
145 byte[] token = data.getByteArrayExtra(
146 ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE_TOKEN);
147 launchFindSensor(token);
151 super.onActivityResult(requestCode, resultCode, data);
155 public void onItemSelected(IItem item) {
156 switch (((Item) item).getId()) {
157 case R.id.next_button:
160 case R.id.cancel_button:
161 onCancelButtonClick();
167 protected int getMetricsCategory() {
168 return MetricsEvent.FINGERPRINT_ENROLL_INTRO;
171 protected void onCancelButtonClick() {
175 private static class LearnMoreSpan extends URLSpan {
176 private static final String TAG = "LearnMoreSpan";
177 private static final Typeface TYPEFACE_MEDIUM =
178 Typeface.create("sans-serif-medium", Typeface.NORMAL);
180 private LearnMoreSpan(String url) {
185 public void onClick(View widget) {
186 Context ctx = widget.getContext();
187 Intent intent = HelpUtils.getHelpIntent(ctx, getURL(), ctx.getClass().getName());
189 // This needs to be startActivityForResult even though we do not care about the
190 // actual result because the help app needs to know about who invoked it.
191 widget.startActivityForResult(intent, LEARN_MORE_REQUEST);
192 } catch (ActivityNotFoundException e) {
193 Log.w(LearnMoreSpan.TAG,
194 "Actvity was not found for intent, " + intent.toString());
199 public void updateDrawState(TextPaint ds) {
200 super.updateDrawState(ds);
201 ds.setUnderlineText(false);
202 ds.setTypeface(TYPEFACE_MEDIUM);
205 public static CharSequence linkify(CharSequence rawText, String uri) {
206 SpannableString msg = new SpannableString(rawText);
207 Annotation[] spans = msg.getSpans(0, msg.length(), Annotation.class);
208 SpannableStringBuilder builder = new SpannableStringBuilder(msg);
209 for (Annotation annotation : spans) {
210 int start = msg.getSpanStart(annotation);
211 int end = msg.getSpanEnd(annotation);
212 LearnMoreSpan link = new LearnMoreSpan(uri);
213 builder.setSpan(link, start, end, msg.getSpanFlags(link));