blob: 82dcd962f978f65cf2961342f5ed17e49d0966b0 [file] [log] [blame]
Andres Morales38a7ed02013-11-14 19:02:56 -08001/*
2 * Copyright (C) 2013 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 android.nfc;
18
19import static com.android.internal.util.Preconditions.checkNotNull;
20
21import android.annotation.Nullable;
22import android.app.ActivityManager;
23import android.content.Context;
24import android.os.RemoteException;
25import android.os.SystemProperties;
26import android.util.Log;
27
28import java.util.HashMap;
29
30/**
31 * Provides an interface to read and update NFC unlock settings.
32 * <p/>
33 * Allows system services (currently exclusively LockSettingsService) to
34 * register NFC tags to be used to unlock the device, as well as the ability
35 * to enable/disable the service entirely.
36 *
37 */
38public class NfcUnlock {
39
40 /**
41 * Action to unlock the device.
42 *
43 * @hide
44 */
45 public static final String ACTION_NFC_UNLOCK = "android.nfc.ACTION_NFC_UNLOCK";
46 /**
47 * Permission to unlock the device.
48 *
49 * @hide
50 */
51 public static final String NFC_UNLOCK_PERMISSION = "android.permission.NFC_UNLOCK";
52
53 /**
54 * Property to enable NFC Unlock
55 *
56 * @hide
57 */
58 public static final String PROPERTY = "ro.com.android.nfc.unlock";
59
60 private static final String TAG = "NfcUnlock";
61 private static HashMap<Context, NfcUnlock> sNfcUnlocks = new HashMap<Context, NfcUnlock>();
62
63 private final Context mContext;
64 private final boolean mEnabled;
65 private INfcUnlockSettings sService;
66
67 private NfcUnlock(Context context, INfcUnlockSettings service) {
68 this.mContext = checkNotNull(context);
69 this.sService = checkNotNull(service);
70 this.mEnabled = getPropertyEnabled();
71 }
72
73 /**
74 * Returns an instance of {@link NfcUnlock}.
75 */
76 public static synchronized NfcUnlock getInstance(NfcAdapter nfcAdapter) {
77 Context context = nfcAdapter.getContext();
78 if (context == null) {
79 Log.e(TAG, "NfcAdapter context is null");
80 throw new UnsupportedOperationException();
81 }
82
83 NfcUnlock manager = sNfcUnlocks.get(context);
84 if (manager == null) {
85 INfcUnlockSettings service = nfcAdapter.getNfcUnlockSettingsService();
86 manager = new NfcUnlock(context, service);
87 sNfcUnlocks.put(context, manager);
88 }
89
90 return manager;
91 }
92
93 /**
94 * Registers the given {@code tag} as an unlock tag.
95 *
96 * @return true if the tag was successfully registered.
97 * @hide
98 */
99 public boolean registerTag(Tag tag) {
100 enforcePropertyEnabled();
101
102 int currentUser = ActivityManager.getCurrentUser();
103
104 try {
105 return sService.registerTag(currentUser, tag);
106 } catch (RemoteException e) {
107 recoverService();
108 if (sService == null) {
109 Log.e(TAG, "Failed to recover NfcUnlockSettingsService");
110 return false;
111 }
112
113 try {
114 return sService.registerTag(currentUser, tag);
115 } catch (RemoteException ee) {
116 Log.e(TAG, "Failed to reach NfcUnlockSettingsService", ee);
117 return false;
118 }
119 }
120 }
121
122 /**
123 * Deregisters the given {@code tag} as an unlock tag.
124 *
125 * @return true if the tag was successfully deregistered.
126 * @hide
127 */
128 public boolean deregisterTag(long timestamp) {
129 enforcePropertyEnabled();
130 int currentUser = ActivityManager.getCurrentUser();
131
132 try {
133 return sService.deregisterTag(currentUser, timestamp);
134 } catch (RemoteException e) {
135 recoverService();
136 if (sService == null) {
137 Log.e(TAG, "Failed to recover NfcUnlockSettingsService");
138 return false;
139 }
140
141 try {
142 return sService.deregisterTag(currentUser, timestamp);
143 } catch (RemoteException ee) {
144 Log.e(TAG, "Failed to reach NfcUnlockSettingsService", ee);
145 return false;
146 }
147 }
148 }
149
150 /**
151 * Determines the enable state of the NFC unlock feature.
152 *
153 * @return true if NFC unlock is enabled.
154 */
155 public boolean getNfcUnlockEnabled() {
156 enforcePropertyEnabled();
157 int currentUser = ActivityManager.getCurrentUser();
158
159 try {
160 return sService.getNfcUnlockEnabled(currentUser);
161 } catch (RemoteException e) {
162 recoverService();
163 if (sService == null) {
164 Log.e(TAG, "Failed to recover NfcUnlockSettingsService");
165 return false;
166 }
167
168 try {
169 return sService.getNfcUnlockEnabled(currentUser);
170 } catch (RemoteException ee) {
171 Log.e(TAG, "Failed to reach NfcUnlockSettingsService", ee);
172 return false;
173 }
174 }
175 }
176
177 /**
178 * Set the enable state of the NFC unlock feature.
179 *
180 * @return true if the setting was successfully persisted.
181 * @hide
182 */
183 public boolean setNfcUnlockEnabled(boolean enabled) {
184 enforcePropertyEnabled();
185 int currentUser = ActivityManager.getCurrentUser();
186
187 try {
188 sService.setNfcUnlockEnabled(currentUser, enabled);
189 return true;
190 } catch (RemoteException e) {
191 recoverService();
192 if (sService == null) {
193 Log.e(TAG, "Failed to recover NfcUnlockSettingsService");
194 return false;
195 }
196
197 try {
198 sService.setNfcUnlockEnabled(currentUser, enabled);
199 return true;
200 } catch (RemoteException ee) {
201 Log.e(TAG, "Failed to reach NfcUnlockSettingsService", ee);
202 return false;
203 }
204
205 }
206 }
207
208 /**
209 * Returns a list of times (in millis since epoch) corresponding to when
210 * unlock tags were registered.
211 *
212 * @hide
213 */
214 @Nullable
215 public long[] getTagRegistryTimes() {
216 enforcePropertyEnabled();
217 int currentUser = ActivityManager.getCurrentUser();
218
219 try {
220 return sService.getTagRegistryTimes(currentUser);
221 } catch (RemoteException e) {
222 recoverService();
223 if (sService == null) {
224 Log.e(TAG, "Failed to recover NfcUnlockSettingsService");
225 return null;
226 }
227
228 try {
229 return sService.getTagRegistryTimes(currentUser);
230 } catch (RemoteException ee) {
231 Log.e(TAG, "Failed to reach NfcUnlockSettingsService", ee);
232 return null;
233 }
234 }
235 }
236
237 /**
238 * @hide
239 */
240 public static boolean getPropertyEnabled() {
241 return SystemProperties.get(PROPERTY).equals("ON");
242 }
243
244 private void recoverService() {
245 NfcAdapter adapter = NfcAdapter.getDefaultAdapter(mContext);
246 sService = adapter.getNfcUnlockSettingsService();
247 }
248
249
250 private void enforcePropertyEnabled() {
251 if (!mEnabled) {
252 throw new UnsupportedOperationException("NFC Unlock property is not enabled");
253 }
254 }
255}