blob: d2744128ab9fbc1524cffeed0b261847b8984d1f [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;
22import android.util.SparseArray;
23
24import com.android.internal.annotations.GuardedBy;
25
26import java.util.List;
27
28/**
29 * Utility class for dealing with fingerprints and fingerprint settings.
30 */
31public 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
42 public static FingerprintUtils getInstance() {
43 synchronized (sInstanceLock) {
44 if (sInstance == null) {
45 sInstance = new FingerprintUtils();
46 }
47 }
48 return sInstance;
49 }
50
51 private FingerprintUtils() {
52 }
53
54 public List<Fingerprint> getFingerprintsForUser(Context ctx, int userId) {
55 return getStateForUser(ctx, userId).getFingerprints();
56 }
57
58 public void addFingerprintForUser(Context ctx, int fingerId, int userId) {
Jim Millered2ab1c2015-07-07 19:12:36 -070059 getStateForUser(ctx, userId).addFingerprint(fingerId, userId);
Jorim Jaggiee77ceb2015-05-12 15:00:12 -070060 }
61
62 public void removeFingerprintIdForUser(Context ctx, int fingerId, int userId) {
63 getStateForUser(ctx, userId).removeFingerprint(fingerId);
64 }
65
66 public void renameFingerprintForUser(Context ctx, int fingerId, int userId, CharSequence name) {
67 getStateForUser(ctx, userId).renameFingerprint(fingerId, name);
68 }
69
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
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
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