blob: ae3d4a46deee814f78a97b28f84b4631c97b6c16 [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;
20import android.provider.Settings;
Jim Millera75961472014-06-06 15:00:49 -070021import android.text.TextUtils;
Jim Miller08fa40c2014-04-29 18:18:47 -070022import android.util.Log;
23
Jim Millerba67aee2015-02-20 16:21:26 -080024import com.android.internal.util.ArrayUtils;
25
26import java.util.ArrayList;
Jim Miller08fa40c2014-04-29 18:18:47 -070027import java.util.Arrays;
Jim Millerba67aee2015-02-20 16:21:26 -080028import java.util.List;
Jim Miller08fa40c2014-04-29 18:18:47 -070029
Jim Miller777f5b22014-08-25 15:03:50 -070030/**
31 * Utility class for dealing with fingerprints and fingerprint settings.
32 * @hide
33 */
Jim Millera75961472014-06-06 15:00:49 -070034public
Jim Miller08fa40c2014-04-29 18:18:47 -070035class FingerprintUtils {
36 private static final boolean DEBUG = true;
37 private static final String TAG = "FingerprintUtils";
38
Jim Millerba67aee2015-02-20 16:21:26 -080039 private static int[] toIntArray(List<Integer> list) {
40 if (list == null) {
41 return null;
42 }
43 int[] arr = new int[list.size()];
44 int i = 0;
45 for (int elem : list) {
46 arr[i] = elem;
47 i++;
48 }
49 return arr;
50 }
51
Jim Miller08fa40c2014-04-29 18:18:47 -070052 public static int[] getFingerprintIdsForUser(ContentResolver res, int userId) {
53 String fingerIdsRaw = Settings.Secure.getStringForUser(res,
54 Settings.Secure.USER_FINGERPRINT_IDS, userId);
Jim Millerba67aee2015-02-20 16:21:26 -080055 ArrayList<Integer> tmp = new ArrayList<Integer>();
Jim Millera75961472014-06-06 15:00:49 -070056 if (!TextUtils.isEmpty(fingerIdsRaw)) {
57 String[] fingerStringIds = fingerIdsRaw.replace("[","").replace("]","").split(", ");
Jim Millerba67aee2015-02-20 16:21:26 -080058 int length = fingerStringIds.length;
59 for (int i = 0; i < length; i++) {
Jim Millera75961472014-06-06 15:00:49 -070060 try {
Jim Millerba67aee2015-02-20 16:21:26 -080061 tmp.add(Integer.decode(fingerStringIds[i]));
Jim Millera75961472014-06-06 15:00:49 -070062 } catch (NumberFormatException e) {
Jim Millerba67aee2015-02-20 16:21:26 -080063 if (DEBUG) Log.w(TAG, "Error parsing finger id: '" + fingerStringIds[i] + "'");
Jim Millera75961472014-06-06 15:00:49 -070064 }
Jim Miller08fa40c2014-04-29 18:18:47 -070065 }
66 }
Jim Millerba67aee2015-02-20 16:21:26 -080067 return toIntArray(tmp);
Jim Miller08fa40c2014-04-29 18:18:47 -070068 }
69
Jim Miller9f0753f2015-03-23 23:59:22 -070070 public static void addFingerprintIdForUser(ContentResolver res, int fingerId, int userId) {
Jim Millerba67aee2015-02-20 16:21:26 -080071 // FingerId 0 has special meaning.
72 if (fingerId == 0) {
73 Log.w(TAG, "Tried to add fingerId 0");
74 return;
75 }
76
Jim Miller08fa40c2014-04-29 18:18:47 -070077 int[] fingerIds = getFingerprintIdsForUser(res, userId);
78
Jim Miller08fa40c2014-04-29 18:18:47 -070079 // Don't allow dups
Jim Millerba67aee2015-02-20 16:21:26 -080080 if (ArrayUtils.contains(fingerIds, fingerId)) {
81 Log.w(TAG, "finger already added " + fingerId);
82 return;
Jim Miller08fa40c2014-04-29 18:18:47 -070083 }
84 int[] newList = Arrays.copyOf(fingerIds, fingerIds.length + 1);
85 newList[fingerIds.length] = fingerId;
86 Settings.Secure.putStringForUser(res, Settings.Secure.USER_FINGERPRINT_IDS,
87 Arrays.toString(newList), userId);
88 }
89
90 public static boolean removeFingerprintIdForUser(int fingerId, ContentResolver res, int userId)
91 {
92 // FingerId 0 has special meaning. The HAL layer is supposed to remove each finger one
93 // at a time and invoke notify() for each fingerId. If we get called with 0 here, it means
94 // something bad has happened.
Jim Millerba67aee2015-02-20 16:21:26 -080095 if (fingerId == 0) throw new IllegalArgumentException("fingerId can't be 0");
Jim Miller08fa40c2014-04-29 18:18:47 -070096
Jim Millerba67aee2015-02-20 16:21:26 -080097 final int[] fingerIds = getFingerprintIdsForUser(res, userId);
98 if (ArrayUtils.contains(fingerIds, fingerId)) {
99 final int[] result = ArrayUtils.removeInt(fingerIds, fingerId);
Jim Miller08fa40c2014-04-29 18:18:47 -0700100 Settings.Secure.putStringForUser(res, Settings.Secure.USER_FINGERPRINT_IDS,
Jim Millerba67aee2015-02-20 16:21:26 -0800101 Arrays.toString(result), userId);
Jim Miller08fa40c2014-04-29 18:18:47 -0700102 return true;
103 }
104 return false;
105 }
106
107};
108