1 /**
2  * Copyright (C) 2015 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 com.android.server.fingerprint;
18 
19 import android.content.Context;
20 import android.hardware.fingerprint.Fingerprint;
21 import android.os.Vibrator;
22 import android.text.TextUtils;
23 import android.util.SparseArray;
24 
25 import com.android.internal.annotations.GuardedBy;
26 
27 import java.util.List;
28 
29 /**
30  * Utility class for dealing with fingerprints and fingerprint settings.
31  */
32 public class FingerprintUtils {
33 
34     private static final long[] FP_ERROR_VIBRATE_PATTERN = new long[] {0, 30, 100, 30};
35     private static final long[] FP_SUCCESS_VIBRATE_PATTERN = new long[] {0, 30};
36 
37     private static final Object sInstanceLock = new Object();
38     private static FingerprintUtils sInstance;
39 
40     @GuardedBy("this")
41     private final SparseArray<FingerprintsUserState> mUsers = new SparseArray<>();
42 
getInstance()43     public static FingerprintUtils getInstance() {
44         synchronized (sInstanceLock) {
45             if (sInstance == null) {
46                 sInstance = new FingerprintUtils();
47             }
48         }
49         return sInstance;
50     }
51 
FingerprintUtils()52     private FingerprintUtils() {
53     }
54 
getFingerprintsForUser(Context ctx, int userId)55     public List<Fingerprint> getFingerprintsForUser(Context ctx, int userId) {
56         return getStateForUser(ctx, userId).getFingerprints();
57     }
58 
addFingerprintForUser(Context ctx, int fingerId, int userId)59     public void addFingerprintForUser(Context ctx, int fingerId, int userId) {
60         getStateForUser(ctx, userId).addFingerprint(fingerId, userId);
61     }
62 
removeFingerprintIdForUser(Context ctx, int fingerId, int userId)63     public void removeFingerprintIdForUser(Context ctx, int fingerId, int userId) {
64         getStateForUser(ctx, userId).removeFingerprint(fingerId);
65     }
66 
renameFingerprintForUser(Context ctx, int fingerId, int userId, CharSequence name)67     public void renameFingerprintForUser(Context ctx, int fingerId, int userId, CharSequence name) {
68         if (TextUtils.isEmpty(name)) {
69             // Don't do the rename if it's empty
70             return;
71         }
72         getStateForUser(ctx, userId).renameFingerprint(fingerId, name);
73     }
74 
vibrateFingerprintError(Context context)75     public static void vibrateFingerprintError(Context context) {
76         Vibrator vibrator = context.getSystemService(Vibrator.class);
77         if (vibrator != null) {
78             vibrator.vibrate(FP_ERROR_VIBRATE_PATTERN, -1);
79         }
80     }
81 
vibrateFingerprintSuccess(Context context)82     public static void vibrateFingerprintSuccess(Context context) {
83         Vibrator vibrator = context.getSystemService(Vibrator.class);
84         if (vibrator != null) {
85             vibrator.vibrate(FP_SUCCESS_VIBRATE_PATTERN, -1);
86         }
87     }
88 
getStateForUser(Context ctx, int userId)89     private FingerprintsUserState getStateForUser(Context ctx, int userId) {
90         synchronized (this) {
91             FingerprintsUserState state = mUsers.get(userId);
92             if (state == null) {
93                 state = new FingerprintsUserState(ctx, userId);
94                 mUsers.put(userId, state);
95             }
96             return state;
97         }
98     }
99 }
100 
101