blob: 49dc8e4dac71bb9022df1763424bf356d79bcef6 [file] [log] [blame]
Jorim Jaggiee77ceb2015-05-12 15:00:12 -07001/**
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
17package com.android.server.fingerprint;
18
19import android.content.Context;
20import android.hardware.fingerprint.Fingerprint;
21import android.os.Vibrator;
Selim Cinekd9d53022015-08-13 16:17:13 -070022import android.text.TextUtils;
Jorim Jaggiee77ceb2015-05-12 15:00:12 -070023import android.util.SparseArray;
24
25import com.android.internal.annotations.GuardedBy;
26
27import java.util.List;
28
29/**
30 * Utility class for dealing with fingerprints and fingerprint settings.
31 */
32public 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
43 public static FingerprintUtils getInstance() {
44 synchronized (sInstanceLock) {
45 if (sInstance == null) {
46 sInstance = new FingerprintUtils();
47 }
48 }
49 return sInstance;
50 }
51
52 private FingerprintUtils() {
53 }
54
55 public List<Fingerprint> getFingerprintsForUser(Context ctx, int userId) {
56 return getStateForUser(ctx, userId).getFingerprints();
57 }
58
59 public void addFingerprintForUser(Context ctx, int fingerId, int userId) {
Jim Millered2ab1c2015-07-07 19:12:36 -070060 getStateForUser(ctx, userId).addFingerprint(fingerId, userId);
Jorim Jaggiee77ceb2015-05-12 15:00:12 -070061 }
62
63 public void removeFingerprintIdForUser(Context ctx, int fingerId, int userId) {
64 getStateForUser(ctx, userId).removeFingerprint(fingerId);
65 }
66
67 public void renameFingerprintForUser(Context ctx, int fingerId, int userId, CharSequence name) {
Selim Cinekd9d53022015-08-13 16:17:13 -070068 if (TextUtils.isEmpty(name)) {
69 // Don't do the rename if it's empty
70 return;
71 }
Jorim Jaggiee77ceb2015-05-12 15:00:12 -070072 getStateForUser(ctx, userId).renameFingerprint(fingerId, name);
73 }
74
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
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
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