1 /** 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 17 package android.service.fingerprint; 18 19 import android.content.ContentResolver; 20 import android.provider.Settings; 21 import android.text.TextUtils; 22 import android.util.Log; 23 24 import java.util.Arrays; 25 26 /** 27 * Utility class for dealing with fingerprints and fingerprint settings. 28 * @hide 29 */ 30 public 31 class FingerprintUtils { 32 private static final boolean DEBUG = true; 33 private static final String TAG = "FingerprintUtils"; 34 getFingerprintIdsForUser(ContentResolver res, int userId)35 public static int[] getFingerprintIdsForUser(ContentResolver res, int userId) { 36 String fingerIdsRaw = Settings.Secure.getStringForUser(res, 37 Settings.Secure.USER_FINGERPRINT_IDS, userId); 38 39 int result[] = {}; 40 if (!TextUtils.isEmpty(fingerIdsRaw)) { 41 String[] fingerStringIds = fingerIdsRaw.replace("[","").replace("]","").split(", "); 42 result = new int[fingerStringIds.length]; 43 for (int i = 0; i < result.length; i++) { 44 try { 45 result[i] = Integer.decode(fingerStringIds[i]); 46 } catch (NumberFormatException e) { 47 if (DEBUG) Log.d(TAG, "Error when parsing finger id " + fingerStringIds[i]); 48 } 49 } 50 } 51 return result; 52 } 53 addFingerprintIdForUser(int fingerId, ContentResolver res, int userId)54 public static void addFingerprintIdForUser(int fingerId, ContentResolver res, int userId) { 55 int[] fingerIds = getFingerprintIdsForUser(res, userId); 56 57 // FingerId 0 has special meaning. 58 if (fingerId == 0) return; 59 60 // Don't allow dups 61 for (int i = 0; i < fingerIds.length; i++) { 62 if (fingerIds[i] == fingerId) return; 63 } 64 int[] newList = Arrays.copyOf(fingerIds, fingerIds.length + 1); 65 newList[fingerIds.length] = fingerId; 66 Settings.Secure.putStringForUser(res, Settings.Secure.USER_FINGERPRINT_IDS, 67 Arrays.toString(newList), userId); 68 } 69 removeFingerprintIdForUser(int fingerId, ContentResolver res, int userId)70 public static boolean removeFingerprintIdForUser(int fingerId, ContentResolver res, int userId) 71 { 72 // FingerId 0 has special meaning. The HAL layer is supposed to remove each finger one 73 // at a time and invoke notify() for each fingerId. If we get called with 0 here, it means 74 // something bad has happened. 75 if (fingerId == 0) throw new IllegalStateException("Bad fingerId"); 76 77 int[] fingerIds = getFingerprintIdsForUser(res, userId); 78 int[] resultIds = Arrays.copyOf(fingerIds, fingerIds.length); 79 int resultCount = 0; 80 for (int i = 0; i < fingerIds.length; i++) { 81 if (fingerId != fingerIds[i]) { 82 resultIds[resultCount++] = fingerIds[i]; 83 } 84 } 85 if (resultCount > 0) { 86 Settings.Secure.putStringForUser(res, Settings.Secure.USER_FINGERPRINT_IDS, 87 Arrays.toString(Arrays.copyOf(resultIds, resultCount)), userId); 88 return true; 89 } 90 return false; 91 } 92 93 }; 94 95