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