blob: 525f254097e051074b14cdf24215464c3af3afaa [file] [log] [blame]
Jim Miller08fa40c2014-04-29 18:18:47 -07001/**
2 * Copyright (C) 2014 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
Jim Millerebbf2052015-03-31 17:24:34 -070017package android.hardware.fingerprint;
Jim Miller08fa40c2014-04-29 18:18:47 -070018
19import android.content.ContentResolver;
Jim Milleraf281ca2015-04-20 19:04:21 -070020import android.content.Context;
21import android.os.Vibrator;
Jim Miller08fa40c2014-04-29 18:18:47 -070022import android.provider.Settings;
Jim Millera75961472014-06-06 15:00:49 -070023import android.text.TextUtils;
Jim Miller08fa40c2014-04-29 18:18:47 -070024import android.util.Log;
25
Jim Millerba67aee2015-02-20 16:21:26 -080026import com.android.internal.util.ArrayUtils;
27
28import java.util.ArrayList;
Jim Miller08fa40c2014-04-29 18:18:47 -070029import java.util.Arrays;
Jim Millerba67aee2015-02-20 16:21:26 -080030import java.util.List;
Jim Miller08fa40c2014-04-29 18:18:47 -070031
Jim Miller777f5b22014-08-25 15:03:50 -070032/**
33 * Utility class for dealing with fingerprints and fingerprint settings.
34 * @hide
35 */
Jim Millera75961472014-06-06 15:00:49 -070036public
Jim Miller08fa40c2014-04-29 18:18:47 -070037class FingerprintUtils {
38 private static final boolean DEBUG = true;
39 private static final String TAG = "FingerprintUtils";
Jim Milleraf281ca2015-04-20 19:04:21 -070040 private static final long[] FP_ERROR_VIBRATE_PATTERN = new long[] {0, 30, 100, 30};
41 private static final long[] FP_SUCCESS_VIBRATE_PATTERN = new long[] {0, 30};
Jim Miller08fa40c2014-04-29 18:18:47 -070042
Jim Millerba67aee2015-02-20 16:21:26 -080043 private static int[] toIntArray(List<Integer> list) {
44 if (list == null) {
45 return null;
46 }
47 int[] arr = new int[list.size()];
48 int i = 0;
49 for (int elem : list) {
50 arr[i] = elem;
51 i++;
52 }
53 return arr;
54 }
55
Jim Miller08fa40c2014-04-29 18:18:47 -070056 public static int[] getFingerprintIdsForUser(ContentResolver res, int userId) {
57 String fingerIdsRaw = Settings.Secure.getStringForUser(res,
58 Settings.Secure.USER_FINGERPRINT_IDS, userId);
Jim Millerba67aee2015-02-20 16:21:26 -080059 ArrayList<Integer> tmp = new ArrayList<Integer>();
Jim Millera75961472014-06-06 15:00:49 -070060 if (!TextUtils.isEmpty(fingerIdsRaw)) {
61 String[] fingerStringIds = fingerIdsRaw.replace("[","").replace("]","").split(", ");
Jim Millerba67aee2015-02-20 16:21:26 -080062 int length = fingerStringIds.length;
63 for (int i = 0; i < length; i++) {
Jim Millera75961472014-06-06 15:00:49 -070064 try {
Jim Millerba67aee2015-02-20 16:21:26 -080065 tmp.add(Integer.decode(fingerStringIds[i]));
Jim Millera75961472014-06-06 15:00:49 -070066 } catch (NumberFormatException e) {
Jim Millerba67aee2015-02-20 16:21:26 -080067 if (DEBUG) Log.w(TAG, "Error parsing finger id: '" + fingerStringIds[i] + "'");
Jim Millera75961472014-06-06 15:00:49 -070068 }
Jim Miller08fa40c2014-04-29 18:18:47 -070069 }
70 }
Jim Millerba67aee2015-02-20 16:21:26 -080071 return toIntArray(tmp);
Jim Miller08fa40c2014-04-29 18:18:47 -070072 }
73
Jim Miller9f0753f2015-03-23 23:59:22 -070074 public static void addFingerprintIdForUser(ContentResolver res, int fingerId, int userId) {
Jim Millerba67aee2015-02-20 16:21:26 -080075 // FingerId 0 has special meaning.
76 if (fingerId == 0) {
77 Log.w(TAG, "Tried to add fingerId 0");
78 return;
79 }
80
Jim Miller08fa40c2014-04-29 18:18:47 -070081 int[] fingerIds = getFingerprintIdsForUser(res, userId);
82
Jim Miller08fa40c2014-04-29 18:18:47 -070083 // Don't allow dups
Jim Millerba67aee2015-02-20 16:21:26 -080084 if (ArrayUtils.contains(fingerIds, fingerId)) {
85 Log.w(TAG, "finger already added " + fingerId);
86 return;
Jim Miller08fa40c2014-04-29 18:18:47 -070087 }
88 int[] newList = Arrays.copyOf(fingerIds, fingerIds.length + 1);
89 newList[fingerIds.length] = fingerId;
90 Settings.Secure.putStringForUser(res, Settings.Secure.USER_FINGERPRINT_IDS,
91 Arrays.toString(newList), userId);
92 }
93
94 public static boolean removeFingerprintIdForUser(int fingerId, ContentResolver res, int userId)
95 {
96 // FingerId 0 has special meaning. The HAL layer is supposed to remove each finger one
97 // at a time and invoke notify() for each fingerId. If we get called with 0 here, it means
98 // something bad has happened.
Jim Millerba67aee2015-02-20 16:21:26 -080099 if (fingerId == 0) throw new IllegalArgumentException("fingerId can't be 0");
Jim Miller08fa40c2014-04-29 18:18:47 -0700100
Jim Millerba67aee2015-02-20 16:21:26 -0800101 final int[] fingerIds = getFingerprintIdsForUser(res, userId);
102 if (ArrayUtils.contains(fingerIds, fingerId)) {
103 final int[] result = ArrayUtils.removeInt(fingerIds, fingerId);
Jim Miller08fa40c2014-04-29 18:18:47 -0700104 Settings.Secure.putStringForUser(res, Settings.Secure.USER_FINGERPRINT_IDS,
Jim Millerba67aee2015-02-20 16:21:26 -0800105 Arrays.toString(result), userId);
Jim Miller08fa40c2014-04-29 18:18:47 -0700106 return true;
107 }
108 return false;
109 }
110
Jim Milleraf281ca2015-04-20 19:04:21 -0700111 public static void vibrateFingerprintError(Context context) {
112 Vibrator vibrator = context.getSystemService(Vibrator.class);
113 if (vibrator != null) {
114 vibrator.vibrate(FP_ERROR_VIBRATE_PATTERN, -1);
115 }
116 }
117
118 public static void vibrateFingerprintSuccess(Context context) {
119 Vibrator vibrator = context.getSystemService(Vibrator.class);
120 if (vibrator != null) {
121 vibrator.vibrate(FP_SUCCESS_VIBRATE_PATTERN, -1);
122 }
123 }
124
Jim Miller08fa40c2014-04-29 18:18:47 -0700125};
126