Jorim Jaggi | ee77ceb | 2015-05-12 15:00:12 -0700 | [diff] [blame] | 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; |
Selim Cinek | d9d5302 | 2015-08-13 16:17:13 -0700 | [diff] [blame] | 21 | import android.text.TextUtils; |
Jorim Jaggi | ee77ceb | 2015-05-12 15:00:12 -0700 | [diff] [blame] | 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 | |
Jorim Jaggi | ee77ceb | 2015-05-12 15:00:12 -0700 | [diff] [blame] | 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 | |
| 39 | public static FingerprintUtils getInstance() { |
| 40 | synchronized (sInstanceLock) { |
| 41 | if (sInstance == null) { |
| 42 | sInstance = new FingerprintUtils(); |
| 43 | } |
| 44 | } |
| 45 | return sInstance; |
| 46 | } |
| 47 | |
| 48 | private FingerprintUtils() { |
| 49 | } |
| 50 | |
| 51 | public List<Fingerprint> getFingerprintsForUser(Context ctx, int userId) { |
| 52 | return getStateForUser(ctx, userId).getFingerprints(); |
| 53 | } |
| 54 | |
| 55 | public void addFingerprintForUser(Context ctx, int fingerId, int userId) { |
Jim Miller | ed2ab1c | 2015-07-07 19:12:36 -0700 | [diff] [blame] | 56 | getStateForUser(ctx, userId).addFingerprint(fingerId, userId); |
Jorim Jaggi | ee77ceb | 2015-05-12 15:00:12 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | public void removeFingerprintIdForUser(Context ctx, int fingerId, int userId) { |
| 60 | getStateForUser(ctx, userId).removeFingerprint(fingerId); |
| 61 | } |
| 62 | |
| 63 | public void renameFingerprintForUser(Context ctx, int fingerId, int userId, CharSequence name) { |
Selim Cinek | d9d5302 | 2015-08-13 16:17:13 -0700 | [diff] [blame] | 64 | if (TextUtils.isEmpty(name)) { |
| 65 | // Don't do the rename if it's empty |
| 66 | return; |
| 67 | } |
Jorim Jaggi | ee77ceb | 2015-05-12 15:00:12 -0700 | [diff] [blame] | 68 | getStateForUser(ctx, userId).renameFingerprint(fingerId, name); |
| 69 | } |
| 70 | |
Jorim Jaggi | ee77ceb | 2015-05-12 15:00:12 -0700 | [diff] [blame] | 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 | |