/* * Copyright (C) 2015 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.settings; import android.app.Activity; import android.app.AlertDialog; import android.app.admin.DevicePolicyManager; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.os.CancellationSignal; import android.os.Handler; import android.preference.Preference; import android.preference.Preference.OnPreferenceChangeListener; import android.preference.PreferenceGroup; import android.preference.PreferenceScreen; import android.hardware.fingerprint.Fingerprint; import android.hardware.fingerprint.FingerprintManager; import android.hardware.fingerprint.FingerprintManager.AuthenticationCallback; import android.hardware.fingerprint.FingerprintManager.RemovalCallback; import android.hardware.fingerprint.FingerprintManager.AuthenticationResult; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.Toast; import com.android.internal.logging.MetricsLogger; import com.android.settings.search.Indexable; import java.util.List; /** * Settings screen for fingerprints */ public class FingerprintSettings extends SettingsActivity { /** * Used by the FP settings wizard to indicate the wizard is * finished, and each activity in the wizard should finish. *
* Previously, each activity in the wizard would finish itself after
* starting the next activity. However, this leads to broken 'Back'
* behavior. So, now an activity does not finish itself until it gets this
* result.
*/
static final int RESULT_FINISHED = RESULT_FIRST_USER;
@Override
public Intent getIntent() {
Intent modIntent = new Intent(super.getIntent());
modIntent.putExtra(EXTRA_SHOW_FRAGMENT, FingerprintSettingsFragment.class.getName());
return modIntent;
}
@Override
protected boolean isValidFragment(String fragmentName) {
if (FingerprintSettingsFragment.class.getName().equals(fragmentName)) return true;
return false;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CharSequence msg = getText(R.string.security_settings_fingerprint_preference_title);
setTitle(msg);
}
public static class FingerprintSettingsFragment extends SettingsPreferenceFragment
implements OnPreferenceChangeListener, Indexable {
private static final int MAX_RETRY_ATTEMPTS = 5;
private static final String TAG = "FingerprintSettings";
private static final String KEY_FINGERPRINT_ITEM_PREFIX = "key_fingerprint_item";
private static final String KEY_USAGE_CATEGORY = "fingerprint_usage_category";
private static final String KEY_FINGERPRINT_ADD = "key_fingerprint_add";
private static final String KEY_MANAGE_CATEGORY = "fingerprint_manage_category";
private static final String KEY_FINGERPRINT_ENABLE_KEYGUARD_TOGGLE =
"fingerprint_enable_keyguard_toggle";
private static final String KEY_LAUNCHED_CONFIRM = "launched_confirm";
private static final int MSG_REFRESH_FINGERPRINT_TEMPLATES = 1000;
private static final int MSG_HIGHLIGHT_FINGERPRINT_ITEM = 1001;
private static final int CONFIRM_REQUEST = 101;
private static final int CHOOSE_LOCK_GENERIC_REQUEST = 102;
private static final int ADD_FINGERPRINT_REQUEST = 10;
private static final boolean ENABLE_USAGE_CATEGORY = false;
protected static final boolean DEBUG = true;
private FingerprintManager mFingerprintManager;
private EditText mDialogTextField;
private PreferenceGroup mManageCategory;
private CancellationSignal mFingerprintCancel;
private int mMaxFingerprintAttempts;
private byte[] mToken;
private boolean mLaunchedConfirm;
private AuthenticationCallback mAuthCallback = new AuthenticationCallback() {
@Override
public void onAuthenticationSucceeded(AuthenticationResult result) {
mHandler.obtainMessage(MSG_HIGHLIGHT_FINGERPRINT_ITEM,
result.getFingerprint().getFingerId(), 0).sendToTarget();
retryFingerprint(true);
}
public void onAuthenticationFailed() {
retryFingerprint(true);
};
@Override
public void onAuthenticationError(int errMsgId, CharSequence errString) {
// get activity will be null on a screen rotation
if (getActivity() != null) {
Toast.makeText(getActivity(), errString, Toast.LENGTH_SHORT);
if (errMsgId != FingerprintManager.FINGERPRINT_ERROR_CANCELED) {
retryFingerprint(false);
}
}
}
@Override
public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
Toast.makeText(getActivity(), helpString, Toast.LENGTH_SHORT);
}
};
private RemovalCallback mRemoveCallback = new RemovalCallback() {
@Override
public void onRemovalSucceeded(Fingerprint fingerprint) {
mHandler.obtainMessage(MSG_REFRESH_FINGERPRINT_TEMPLATES,
fingerprint.getFingerId(), 0).sendToTarget();
}
@Override
public void onRemovalError(Fingerprint fp, int errMsgId, CharSequence errString) {
Toast.makeText(getActivity(), errString, Toast.LENGTH_SHORT);
}
};
private final Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case MSG_REFRESH_FINGERPRINT_TEMPLATES:
removeFingerprintPreference(msg.arg1);
break;
case MSG_HIGHLIGHT_FINGERPRINT_ITEM:
highlightFingerprintItem(msg.arg1);
break;
}
};
};
private void stopFingerprint() {
if (mFingerprintCancel != null) {
mFingerprintCancel.cancel();
mFingerprintCancel = null;
}
}
private void retryFingerprint(boolean resetAttempts) {
if (resetAttempts) {
mMaxFingerprintAttempts = 0;
}
if (mMaxFingerprintAttempts < MAX_RETRY_ATTEMPTS) {
mFingerprintCancel = new CancellationSignal();
mFingerprintManager.authenticate(null, mFingerprintCancel, mAuthCallback, 0);
}
mMaxFingerprintAttempts++;
}
@Override
protected int getMetricsCategory() {
return MetricsLogger.FINGERPRINT;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
mToken = savedInstanceState.getByteArray(
ChooseLockSettingsHelper.EXTRA_KEY_CHALLENGE_TOKEN);
mLaunchedConfirm = savedInstanceState.getBoolean(
KEY_LAUNCHED_CONFIRM, false);
}
mFingerprintManager = (FingerprintManager) getActivity().getSystemService(
Context.FINGERPRINT_SERVICE);
// Need to authenticate a session token if none
if (mToken == null && mLaunchedConfirm == false) {
mLaunchedConfirm = true;
launchChooseOrConfirmLock();
}
}
protected void removeFingerprintPreference(int fingerprintId) {
String name = genKey(fingerprintId);
Preference prefToRemove = mManageCategory.findPreference(name);
if (prefToRemove != null) {
if (!mManageCategory.removePreference(prefToRemove)) {
Log.w(TAG, "Failed to remove preference with key " + name);
}
} else {
Log.w(TAG, "Can't find preference to remove: " + name);
}
}
private void highlightFingerprintItem(int fpId) {
String prefName = genKey(fpId);
Preference pref = mManageCategory.findPreference(prefName);
if (pref instanceof FingerprintPreference) {
final FingerprintPreference fpref = (FingerprintPreference) pref;
fpref.highlight();
} else {
Log.w(TAG, "Wrong pref " + (pref != null ? pref.getKey() : "null"));
}
}
/**
* Important!
*
* Don't forget to update the SecuritySearchIndexProvider if you are doing any change in the
* logic or adding/removing preferences here.
*/
private PreferenceScreen createPreferenceHierarchy() {
PreferenceScreen root = getPreferenceScreen();
if (root != null) {
root.removeAll();
}
addPreferencesFromResource(R.xml.security_settings_fingerprint);
root = getPreferenceScreen();
// Fingerprint items
mManageCategory = (PreferenceGroup) root.findPreference(KEY_MANAGE_CATEGORY);
if (mManageCategory != null) {
addFingerprintItemPreferences(mManageCategory);
}
// Fingerprint usage options
PreferenceGroup usageCategory = (PreferenceGroup) root.findPreference(
KEY_USAGE_CATEGORY);
if (usageCategory != null) {
Preference toggle = root.findPreference(KEY_FINGERPRINT_ENABLE_KEYGUARD_TOGGLE);
toggle.setOnPreferenceChangeListener(this);
if (!ENABLE_USAGE_CATEGORY) {
root.removePreference(usageCategory);
} else {
toggle.setOnPreferenceChangeListener(this);
}
}
return root;
}
private void addFingerprintItemPreferences(PreferenceGroup manageFingerprintCategory) {
manageFingerprintCategory.removeAll();
final List