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.text.TextUtils;
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 Object sInstanceLock = new Object();
34     private static FingerprintUtils sInstance;
35 
36     @GuardedBy("this")
37     private final SparseArray<FingerprintsUserState> mUsers = new SparseArray<>();
38 
getInstance()39     public static FingerprintUtils getInstance() {
40         synchronized (sInstanceLock) {
41             if (sInstance == null) {
42                 sInstance = new FingerprintUtils();
43             }
44         }
45         return sInstance;
46     }
47 
FingerprintUtils()48     private FingerprintUtils() {
49     }
50 
getFingerprintsForUser(Context ctx, int userId)51     public List<Fingerprint> getFingerprintsForUser(Context ctx, int userId) {
52         return getStateForUser(ctx, userId).getFingerprints();
53     }
54 
addFingerprintForUser(Context ctx, int fingerId, int userId)55     public void addFingerprintForUser(Context ctx, int fingerId, int userId) {
56         getStateForUser(ctx, userId).addFingerprint(fingerId, userId);
57     }
58 
removeFingerprintIdForUser(Context ctx, int fingerId, int userId)59     public void removeFingerprintIdForUser(Context ctx, int fingerId, int userId) {
60         getStateForUser(ctx, userId).removeFingerprint(fingerId);
61     }
62 
renameFingerprintForUser(Context ctx, int fingerId, int userId, CharSequence name)63     public void renameFingerprintForUser(Context ctx, int fingerId, int userId, CharSequence name) {
64         if (TextUtils.isEmpty(name)) {
65             // Don't do the rename if it's empty
66             return;
67         }
68         getStateForUser(ctx, userId).renameFingerprint(fingerId, name);
69     }
70 
getStateForUser(Context ctx, int userId)71     private FingerprintsUserState getStateForUser(Context ctx, int userId) {
72         synchronized (this) {
73             FingerprintsUserState state = mUsers.get(userId);
74             if (state == null) {
75                 state = new FingerprintsUserState(ctx, userId);
76                 mUsers.put(userId, state);
77             }
78             return state;
79         }
80     }
81 }
82 
83