blob: 17a3ab847f81ac0d2207b21562492a0cb6b75300 [file] [log] [blame]
Dianne Hackbornd6847842010-01-12 18:14:19 -08001/*
2 * Copyright (C) 2010 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;
18
19import com.android.common.FastXmlSerializer;
Dianne Hackborndf83afa2010-01-20 13:37:26 -080020import com.android.internal.widget.LockPatternUtils;
Dianne Hackbornd6847842010-01-12 18:14:19 -080021
22import org.xmlpull.v1.XmlPullParser;
23import org.xmlpull.v1.XmlPullParserException;
24import org.xmlpull.v1.XmlSerializer;
25
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080026import android.app.Activity;
Dianne Hackbornd6847842010-01-12 18:14:19 -080027import android.app.DeviceAdmin;
28import android.app.DeviceAdminInfo;
29import android.app.DevicePolicyManager;
30import android.app.IDevicePolicyManager;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080031import android.content.BroadcastReceiver;
Dianne Hackbornd6847842010-01-12 18:14:19 -080032import android.content.ComponentName;
33import android.content.Context;
34import android.content.Intent;
35import android.content.pm.PackageManager;
36import android.content.pm.ResolveInfo;
37import android.os.Binder;
Dianne Hackborndf83afa2010-01-20 13:37:26 -080038import android.os.IBinder;
39import android.os.IPowerManager;
Dianne Hackborndf83afa2010-01-20 13:37:26 -080040import android.os.RecoverySystem;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080041import android.os.RemoteCallback;
Dianne Hackborndf83afa2010-01-20 13:37:26 -080042import android.os.RemoteException;
43import android.os.ServiceManager;
Dianne Hackborn254cb442010-01-27 19:23:59 -080044import android.os.SystemClock;
Dianne Hackbornd6847842010-01-12 18:14:19 -080045import android.util.Log;
46import android.util.Xml;
Dianne Hackborn254cb442010-01-27 19:23:59 -080047import android.view.WindowManagerPolicy;
Dianne Hackbornd6847842010-01-12 18:14:19 -080048
49import java.io.File;
50import java.io.FileInputStream;
51import java.io.FileOutputStream;
52import java.io.IOException;
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -080053import java.util.ArrayList;
54import java.util.HashMap;
Dianne Hackbornd6847842010-01-12 18:14:19 -080055import java.util.List;
56
57/**
58 * Implementation of the device policy APIs.
59 */
60public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
61 private static final String TAG = "DevicePolicyManagerService";
62
63 private final Context mContext;
64
Dianne Hackborndf83afa2010-01-20 13:37:26 -080065 IPowerManager mIPowerManager;
66
Dianne Hackbornd6847842010-01-12 18:14:19 -080067 int mActivePasswordMode = DevicePolicyManager.PASSWORD_MODE_UNSPECIFIED;
68 int mActivePasswordLength = 0;
69 int mFailedPasswordAttempts = 0;
70
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -080071 final HashMap<ComponentName, ActiveAdmin> mAdminMap
72 = new HashMap<ComponentName, ActiveAdmin>();
73 final ArrayList<ActiveAdmin> mAdminList
74 = new ArrayList<ActiveAdmin>();
Dianne Hackbornd6847842010-01-12 18:14:19 -080075
76 static class ActiveAdmin {
Dianne Hackbornd6847842010-01-12 18:14:19 -080077 final DeviceAdminInfo info;
Dianne Hackbornd6847842010-01-12 18:14:19 -080078
79 int passwordMode = DevicePolicyManager.PASSWORD_MODE_UNSPECIFIED;
80 int minimumPasswordLength = 0;
81 long maximumTimeToUnlock = 0;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080082 int maximumFailedPasswordsForWipe = 0;
83
84 ActiveAdmin(DeviceAdminInfo _info) {
85 info = _info;
86 }
87
88 int getUid() { return info.getActivityInfo().applicationInfo.uid; }
89
90 void writeToXml(XmlSerializer out)
91 throws IllegalArgumentException, IllegalStateException, IOException {
92 if (passwordMode != DevicePolicyManager.PASSWORD_MODE_UNSPECIFIED) {
93 out.startTag(null, "password-mode");
94 out.attribute(null, "value", Integer.toString(passwordMode));
95 out.endTag(null, "password-mode");
96 if (minimumPasswordLength > 0) {
97 out.startTag(null, "min-password-length");
98 out.attribute(null, "value", Integer.toString(minimumPasswordLength));
99 out.endTag(null, "mn-password-length");
100 }
101 }
102 if (maximumTimeToUnlock != DevicePolicyManager.PASSWORD_MODE_UNSPECIFIED) {
103 out.startTag(null, "max-time-to-unlock");
104 out.attribute(null, "value", Long.toString(maximumTimeToUnlock));
105 out.endTag(null, "max-time-to-unlock");
106 }
107 if (maximumFailedPasswordsForWipe != 0) {
108 out.startTag(null, "max-failed-password-wipe");
109 out.attribute(null, "value", Integer.toString(maximumFailedPasswordsForWipe));
110 out.endTag(null, "max-failed-password-wipe");
111 }
112 }
113
114 void readFromXml(XmlPullParser parser)
115 throws XmlPullParserException, IOException {
116 int outerDepth = parser.getDepth();
117 int type;
118 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
119 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
120 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
121 continue;
122 }
123 String tag = parser.getName();
124 if ("password-mode".equals(tag)) {
125 passwordMode = Integer.parseInt(
126 parser.getAttributeValue(null, "value"));
127 } else if ("min-password-length".equals(tag)) {
128 minimumPasswordLength = Integer.parseInt(
129 parser.getAttributeValue(null, "value"));
130 } else if ("max-time-to-unlock".equals(tag)) {
131 maximumTimeToUnlock = Long.parseLong(
132 parser.getAttributeValue(null, "value"));
133 } else if ("max-failed-password-wipe".equals(tag)) {
134 maximumFailedPasswordsForWipe = Integer.parseInt(
135 parser.getAttributeValue(null, "value"));
136 }
137 }
138 }
Dianne Hackbornd6847842010-01-12 18:14:19 -0800139 }
140
141 /**
142 * Instantiates the service.
143 */
144 public DevicePolicyManagerService(Context context) {
145 mContext = context;
146 }
147
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800148 private IPowerManager getIPowerManager() {
149 if (mIPowerManager == null) {
150 IBinder b = ServiceManager.getService(Context.POWER_SERVICE);
151 mIPowerManager = IPowerManager.Stub.asInterface(b);
152 }
153 return mIPowerManager;
154 }
155
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800156 ActiveAdmin getActiveAdminUncheckedLocked(ComponentName who) {
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800157 ActiveAdmin admin = mAdminMap.get(who);
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800158 if (admin != null
159 && who.getPackageName().equals(admin.info.getActivityInfo().packageName)
160 && who.getClassName().equals(admin.info.getActivityInfo().name)) {
161 return admin;
162 }
163 return null;
164 }
165
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800166 ActiveAdmin getActiveAdminForCallerLocked(ComponentName who, int reqPolicy)
167 throws SecurityException {
Dianne Hackborn254cb442010-01-27 19:23:59 -0800168 final int callingUid = Binder.getCallingUid();
169 if (who != null) {
170 ActiveAdmin admin = mAdminMap.get(who);
171 if (admin == null) {
172 throw new SecurityException("No active admin " + who);
173 }
174 if (admin.getUid() != callingUid) {
175 throw new SecurityException("Admin " + who + " is not owned by uid "
176 + Binder.getCallingUid());
177 }
178 if (!admin.info.usesPolicy(reqPolicy)) {
179 throw new SecurityException("Admin " + admin.info.getComponent()
180 + " did not specify uses-policy for: "
181 + admin.info.getTagForPolicy(reqPolicy));
182 }
183 return admin;
184 } else {
185 final int N = mAdminList.size();
186 for (int i=0; i<N; i++) {
187 ActiveAdmin admin = mAdminList.get(i);
188 if (admin.getUid() == callingUid && admin.info.usesPolicy(reqPolicy)) {
189 return admin;
190 }
191 }
192 throw new SecurityException("No active admin owned by uid "
193 + Binder.getCallingUid() + " for policy #" + reqPolicy);
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800194 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800195 }
196
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800197 void sendAdminCommandLocked(ActiveAdmin admin, String action) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800198 Intent intent = new Intent(action);
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800199 intent.setComponent(admin.info.getComponent());
Dianne Hackbornd6847842010-01-12 18:14:19 -0800200 mContext.sendBroadcast(intent);
201 }
202
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800203 void sendAdminCommandLocked(String action, int reqPolicy) {
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800204 final int N = mAdminList.size();
205 if (N > 0) {
206 for (int i=0; i<N; i++) {
207 ActiveAdmin admin = mAdminList.get(i);
208 if (admin.info.usesPolicy(reqPolicy)) {
209 sendAdminCommandLocked(admin, action);
210 }
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800211 }
Dianne Hackborn4141d032010-01-21 16:29:00 -0800212 }
213 }
214
Dianne Hackbornd6847842010-01-12 18:14:19 -0800215 void removeActiveAdminLocked(ComponentName adminReceiver) {
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800216 ActiveAdmin admin = getActiveAdminUncheckedLocked(adminReceiver);
217 if (admin != null) {
218 sendAdminCommandLocked(admin,
Dianne Hackbornd6847842010-01-12 18:14:19 -0800219 DeviceAdmin.ACTION_DEVICE_ADMIN_DISABLED);
220 // XXX need to wait for it to complete.
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800221 mAdminList.remove(admin);
222 mAdminMap.remove(adminReceiver);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800223 }
224 }
225
226 public DeviceAdminInfo findAdmin(ComponentName adminName) {
227 Intent resolveIntent = new Intent();
228 resolveIntent.setComponent(adminName);
229 List<ResolveInfo> infos = mContext.getPackageManager().queryBroadcastReceivers(
230 resolveIntent, PackageManager.GET_META_DATA);
231 if (infos == null || infos.size() <= 0) {
232 throw new IllegalArgumentException("Unknown admin: " + adminName);
233 }
234
235 try {
236 return new DeviceAdminInfo(mContext, infos.get(0));
237 } catch (XmlPullParserException e) {
238 Log.w(TAG, "Bad device admin requested: " + adminName, e);
239 return null;
240 } catch (IOException e) {
241 Log.w(TAG, "Bad device admin requested: " + adminName, e);
242 return null;
243 }
244 }
245
246 private static JournaledFile makeJournaledFile() {
247 final String base = "/data/system/device_policies.xml";
248 return new JournaledFile(new File(base), new File(base + ".tmp"));
249 }
250
251 private void saveSettingsLocked() {
252 JournaledFile journal = makeJournaledFile();
253 FileOutputStream stream = null;
254 try {
255 stream = new FileOutputStream(journal.chooseForWrite(), false);
256 XmlSerializer out = new FastXmlSerializer();
257 out.setOutput(stream, "utf-8");
258 out.startDocument(null, true);
259
260 out.startTag(null, "policies");
261
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800262 final int N = mAdminList.size();
263 for (int i=0; i<N; i++) {
264 ActiveAdmin ap = mAdminList.get(i);
265 if (ap != null) {
266 out.startTag(null, "admin");
267 out.attribute(null, "name", ap.info.getComponent().flattenToString());
268 ap.writeToXml(out);
269 out.endTag(null, "admin");
270 }
Dianne Hackbornd6847842010-01-12 18:14:19 -0800271 }
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800272
Dianne Hackbornd6847842010-01-12 18:14:19 -0800273 out.endTag(null, "policies");
274
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800275 if (mFailedPasswordAttempts != 0) {
276 out.startTag(null, "failed-password-attempts");
277 out.attribute(null, "value", Integer.toString(mFailedPasswordAttempts));
278 out.endTag(null, "failed-password-attempts");
279 }
280
Dianne Hackbornd6847842010-01-12 18:14:19 -0800281 out.endDocument();
282 stream.close();
283 journal.commit();
284 } catch (IOException e) {
285 try {
286 if (stream != null) {
287 stream.close();
288 }
289 } catch (IOException ex) {
290 // Ignore
291 }
292 journal.rollback();
293 }
294 }
295
296 private void loadSettingsLocked() {
297 JournaledFile journal = makeJournaledFile();
298 FileInputStream stream = null;
299 File file = journal.chooseForRead();
Dianne Hackbornd6847842010-01-12 18:14:19 -0800300 try {
301 stream = new FileInputStream(file);
302 XmlPullParser parser = Xml.newPullParser();
303 parser.setInput(stream, null);
304
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800305 int type;
306 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
307 && type != XmlPullParser.START_TAG) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800308 }
309 String tag = parser.getName();
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800310 if (!"policies".equals(tag)) {
311 throw new XmlPullParserException(
312 "Settings do not start with policies tag: found " + tag);
313 }
314 type = parser.next();
315 int outerDepth = parser.getDepth();
316 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
317 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
318 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
319 continue;
320 }
321 tag = parser.getName();
322 if ("admin".equals(tag)) {
323 DeviceAdminInfo dai = findAdmin(
324 ComponentName.unflattenFromString(
325 parser.getAttributeValue(null, "name")));
326 if (dai != null) {
327 ActiveAdmin ap = new ActiveAdmin(dai);
328 ap.readFromXml(parser);
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800329 mAdminMap.put(ap.info.getComponent(), ap);
330 mAdminList.add(ap);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800331 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800332 } else if ("failed-password-attempts".equals(tag)) {
333 mFailedPasswordAttempts = Integer.parseInt(
334 parser.getAttributeValue(null, "value"));
335 }
Dianne Hackbornd6847842010-01-12 18:14:19 -0800336 }
337 } catch (NullPointerException e) {
338 Log.w(TAG, "failed parsing " + file + " " + e);
339 } catch (NumberFormatException e) {
340 Log.w(TAG, "failed parsing " + file + " " + e);
341 } catch (XmlPullParserException e) {
342 Log.w(TAG, "failed parsing " + file + " " + e);
343 } catch (IOException e) {
344 Log.w(TAG, "failed parsing " + file + " " + e);
345 } catch (IndexOutOfBoundsException e) {
346 Log.w(TAG, "failed parsing " + file + " " + e);
347 }
348 try {
349 if (stream != null) {
350 stream.close();
351 }
352 } catch (IOException e) {
353 // Ignore
354 }
355
Dianne Hackborn254cb442010-01-27 19:23:59 -0800356 long timeMs = getMaximumTimeToLock(null);
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800357 if (timeMs <= 0) {
358 timeMs = Integer.MAX_VALUE;
359 }
360 try {
361 getIPowerManager().setMaximumScreenOffTimeount((int)timeMs);
362 } catch (RemoteException e) {
363 Log.w(TAG, "Failure talking with power manager", e);
364 }
Dianne Hackbornd6847842010-01-12 18:14:19 -0800365 }
366
367 public void systemReady() {
368 synchronized (this) {
369 loadSettingsLocked();
370 }
371 }
372
373 public void setActiveAdmin(ComponentName adminReceiver) {
374 mContext.enforceCallingOrSelfPermission(
375 android.Manifest.permission.BIND_DEVICE_ADMIN, null);
376
377 DeviceAdminInfo info = findAdmin(adminReceiver);
378 if (info == null) {
379 throw new IllegalArgumentException("Bad admin: " + adminReceiver);
380 }
381 synchronized (this) {
382 long ident = Binder.clearCallingIdentity();
383 try {
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800384 if (getActiveAdminUncheckedLocked(adminReceiver) != null) {
385 throw new IllegalArgumentException("Admin is already added");
Dianne Hackbornd6847842010-01-12 18:14:19 -0800386 }
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800387 ActiveAdmin admin = new ActiveAdmin(info);
388 mAdminMap.put(adminReceiver, admin);
389 mAdminList.add(admin);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800390 saveSettingsLocked();
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800391 sendAdminCommandLocked(admin,
Dianne Hackbornd6847842010-01-12 18:14:19 -0800392 DeviceAdmin.ACTION_DEVICE_ADMIN_ENABLED);
393 } finally {
394 Binder.restoreCallingIdentity(ident);
395 }
396 }
397 }
398
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800399 public boolean isAdminActive(ComponentName adminReceiver) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800400 synchronized (this) {
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800401 return getActiveAdminUncheckedLocked(adminReceiver) != null;
402 }
403 }
404
405 public List<ComponentName> getActiveAdmins() {
406 synchronized (this) {
407 final int N = mAdminList.size();
408 if (N <= 0) {
409 return null;
410 }
411 ArrayList<ComponentName> res = new ArrayList<ComponentName>(N);
412 for (int i=0; i<N; i++) {
413 res.add(mAdminList.get(i).info.getComponent());
414 }
415 return res;
Dianne Hackbornd6847842010-01-12 18:14:19 -0800416 }
417 }
418
419 public void removeActiveAdmin(ComponentName adminReceiver) {
420 synchronized (this) {
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800421 ActiveAdmin admin = getActiveAdminUncheckedLocked(adminReceiver);
422 if (admin == null) {
423 return;
424 }
425 if (admin.getUid() != Binder.getCallingUid()) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800426 mContext.enforceCallingOrSelfPermission(
427 android.Manifest.permission.BIND_DEVICE_ADMIN, null);
428 }
429 long ident = Binder.clearCallingIdentity();
430 try {
431 removeActiveAdminLocked(adminReceiver);
432 } finally {
433 Binder.restoreCallingIdentity(ident);
434 }
435 }
436 }
437
438 public void setPasswordMode(ComponentName who, int mode) {
439 synchronized (this) {
440 if (who == null) {
441 throw new NullPointerException("ComponentName is null");
442 }
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800443 ActiveAdmin ap = getActiveAdminForCallerLocked(who,
444 DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800445 if (ap.passwordMode != mode) {
446 ap.passwordMode = mode;
447 saveSettingsLocked();
448 }
449 }
450 }
451
Dianne Hackborn254cb442010-01-27 19:23:59 -0800452 public int getPasswordMode(ComponentName who) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800453 synchronized (this) {
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800454 int mode = DevicePolicyManager.PASSWORD_MODE_UNSPECIFIED;
Dianne Hackborn254cb442010-01-27 19:23:59 -0800455
456 if (who != null) {
457 ActiveAdmin admin = getActiveAdminUncheckedLocked(who);
458 return admin != null ? admin.passwordMode : mode;
459 }
460
461 final int N = mAdminList.size();
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800462 for (int i=0; i<N; i++) {
463 ActiveAdmin admin = mAdminList.get(i);
464 if (mode < admin.passwordMode) {
465 mode = admin.passwordMode;
466 }
467 }
468 return mode;
Dianne Hackbornd6847842010-01-12 18:14:19 -0800469 }
470 }
471
Dianne Hackborn254cb442010-01-27 19:23:59 -0800472 public void setPasswordMinimumLength(ComponentName who, int length) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800473 synchronized (this) {
474 if (who == null) {
475 throw new NullPointerException("ComponentName is null");
476 }
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800477 ActiveAdmin ap = getActiveAdminForCallerLocked(who,
478 DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800479 if (ap.minimumPasswordLength != length) {
480 ap.minimumPasswordLength = length;
481 saveSettingsLocked();
482 }
483 }
484 }
485
Dianne Hackborn254cb442010-01-27 19:23:59 -0800486 public int getPasswordMinimumLength(ComponentName who) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800487 synchronized (this) {
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800488 int length = 0;
Dianne Hackborn254cb442010-01-27 19:23:59 -0800489
490 if (who != null) {
491 ActiveAdmin admin = getActiveAdminUncheckedLocked(who);
492 return admin != null ? admin.minimumPasswordLength : length;
493 }
494
495 final int N = mAdminList.size();
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800496 for (int i=0; i<N; i++) {
497 ActiveAdmin admin = mAdminList.get(i);
498 if (length < admin.minimumPasswordLength) {
499 length = admin.minimumPasswordLength;
500 }
501 }
502 return length;
Dianne Hackbornd6847842010-01-12 18:14:19 -0800503 }
504 }
505
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800506 public boolean isActivePasswordSufficient() {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800507 synchronized (this) {
508 // This API can only be called by an active device admin,
509 // so try to retrieve it to check that the caller is one.
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800510 getActiveAdminForCallerLocked(null,
511 DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD);
Dianne Hackborn254cb442010-01-27 19:23:59 -0800512 return mActivePasswordMode >= getPasswordMode(null)
513 && mActivePasswordLength >= getPasswordMinimumLength(null);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800514 }
515 }
516
517 public int getCurrentFailedPasswordAttempts() {
518 synchronized (this) {
519 // This API can only be called by an active device admin,
520 // so try to retrieve it to check that the caller is one.
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800521 getActiveAdminForCallerLocked(null,
522 DeviceAdminInfo.USES_POLICY_WATCH_LOGIN);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800523 return mFailedPasswordAttempts;
524 }
525 }
526
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800527 public void setMaximumFailedPasswordsForWipe(ComponentName who, int num) {
528 synchronized (this) {
529 // This API can only be called by an active device admin,
530 // so try to retrieve it to check that the caller is one.
531 getActiveAdminForCallerLocked(who,
532 DeviceAdminInfo.USES_POLICY_WIPE_DATA);
533 ActiveAdmin ap = getActiveAdminForCallerLocked(who,
534 DeviceAdminInfo.USES_POLICY_WATCH_LOGIN);
535 if (ap.maximumFailedPasswordsForWipe != num) {
536 ap.maximumFailedPasswordsForWipe = num;
537 saveSettingsLocked();
538 }
539 }
540 }
541
Dianne Hackborn254cb442010-01-27 19:23:59 -0800542 public int getMaximumFailedPasswordsForWipe(ComponentName who) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800543 synchronized (this) {
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800544 int count = 0;
Dianne Hackborn254cb442010-01-27 19:23:59 -0800545
546 if (who != null) {
547 ActiveAdmin admin = getActiveAdminUncheckedLocked(who);
548 return admin != null ? admin.maximumFailedPasswordsForWipe : count;
549 }
550
551 final int N = mAdminList.size();
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800552 for (int i=0; i<N; i++) {
553 ActiveAdmin admin = mAdminList.get(i);
554 if (count == 0) {
555 count = admin.maximumFailedPasswordsForWipe;
556 } else if (admin.maximumFailedPasswordsForWipe != 0
557 && count > admin.maximumFailedPasswordsForWipe) {
558 count = admin.maximumFailedPasswordsForWipe;
559 }
560 }
561 return count;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800562 }
563 }
564
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800565 public boolean resetPassword(String password) {
566 int mode;
567 synchronized (this) {
568 // This API can only be called by an active device admin,
569 // so try to retrieve it to check that the caller is one.
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800570 getActiveAdminForCallerLocked(null,
571 DeviceAdminInfo.USES_POLICY_RESET_PASSWORD);
Dianne Hackborn254cb442010-01-27 19:23:59 -0800572 mode = getPasswordMode(null);
573 if (password.length() < getPasswordMinimumLength(null)) {
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800574 return false;
575 }
576 }
577
578 // Don't do this with the lock held, because it is going to call
579 // back in to the service.
580 long ident = Binder.clearCallingIdentity();
581 try {
582 LockPatternUtils utils = new LockPatternUtils(mContext);
583 utils.saveLockPassword(password, mode);
584 } finally {
585 Binder.restoreCallingIdentity(ident);
586 }
587
588 return true;
589 }
590
Dianne Hackbornd6847842010-01-12 18:14:19 -0800591 public void setMaximumTimeToLock(ComponentName who, long timeMs) {
592 synchronized (this) {
593 if (who == null) {
594 throw new NullPointerException("ComponentName is null");
595 }
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800596 ActiveAdmin ap = getActiveAdminForCallerLocked(who,
597 DeviceAdminInfo.USES_POLICY_LIMIT_UNLOCK);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800598 if (ap.maximumTimeToUnlock != timeMs) {
599 ap.maximumTimeToUnlock = timeMs;
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800600
601 long ident = Binder.clearCallingIdentity();
602 try {
603 saveSettingsLocked();
Dianne Hackborn254cb442010-01-27 19:23:59 -0800604
605 timeMs = getMaximumTimeToLock(null);
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800606 if (timeMs <= 0) {
607 timeMs = Integer.MAX_VALUE;
608 }
Dianne Hackborn254cb442010-01-27 19:23:59 -0800609
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800610 try {
611 getIPowerManager().setMaximumScreenOffTimeount((int)timeMs);
612 } catch (RemoteException e) {
613 Log.w(TAG, "Failure talking with power manager", e);
614 }
615 } finally {
616 Binder.restoreCallingIdentity(ident);
617 }
Dianne Hackbornd6847842010-01-12 18:14:19 -0800618 }
619 }
620 }
621
Dianne Hackborn254cb442010-01-27 19:23:59 -0800622 public long getMaximumTimeToLock(ComponentName who) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800623 synchronized (this) {
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800624 long time = 0;
Dianne Hackborn254cb442010-01-27 19:23:59 -0800625
626 if (who != null) {
627 ActiveAdmin admin = getActiveAdminUncheckedLocked(who);
628 return admin != null ? admin.maximumTimeToUnlock : time;
629 }
630
631 final int N = mAdminList.size();
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800632 for (int i=0; i<N; i++) {
633 ActiveAdmin admin = mAdminList.get(i);
634 if (time == 0) {
635 time = admin.maximumTimeToUnlock;
636 } else if (admin.maximumTimeToUnlock != 0
637 && time > admin.maximumTimeToUnlock) {
638 time = admin.maximumTimeToUnlock;
639 }
640 }
641 return time;
Dianne Hackbornd6847842010-01-12 18:14:19 -0800642 }
643 }
644
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800645 public void lockNow() {
646 synchronized (this) {
647 // This API can only be called by an active device admin,
648 // so try to retrieve it to check that the caller is one.
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800649 getActiveAdminForCallerLocked(null,
650 DeviceAdminInfo.USES_POLICY_FORCE_LOCK);
Dianne Hackborn254cb442010-01-27 19:23:59 -0800651 long ident = Binder.clearCallingIdentity();
652 try {
653 mIPowerManager.goToSleepWithReason(SystemClock.uptimeMillis(),
654 WindowManagerPolicy.OFF_BECAUSE_OF_ADMIN);
655 } catch (RemoteException e) {
656 } finally {
657 Binder.restoreCallingIdentity(ident);
658 }
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800659 }
660 }
661
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800662 void wipeDataLocked(int flags) {
663 try {
664 RecoverySystem.rebootWipeUserData(mContext);
665 } catch (IOException e) {
666 Log.w(TAG, "Failed requesting data wipe", e);
667 }
668 }
669
Dianne Hackbornd6847842010-01-12 18:14:19 -0800670 public void wipeData(int flags) {
671 synchronized (this) {
672 // This API can only be called by an active device admin,
673 // so try to retrieve it to check that the caller is one.
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800674 getActiveAdminForCallerLocked(null,
675 DeviceAdminInfo.USES_POLICY_WIPE_DATA);
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800676 long ident = Binder.clearCallingIdentity();
677 try {
678 wipeDataLocked(flags);
679 } finally {
680 Binder.restoreCallingIdentity(ident);
681 }
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800682 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800683 }
684
685 public void getRemoveWarning(ComponentName comp, final RemoteCallback result) {
686 mContext.enforceCallingOrSelfPermission(
687 android.Manifest.permission.BIND_DEVICE_ADMIN, null);
688
689 synchronized (this) {
690 ActiveAdmin admin = getActiveAdminUncheckedLocked(comp);
691 if (admin == null) {
692 try {
693 result.sendResult(null);
694 } catch (RemoteException e) {
695 }
696 return;
697 }
698 Intent intent = new Intent(DeviceAdmin.ACTION_DEVICE_ADMIN_DISABLE_REQUESTED);
699 intent.setComponent(admin.info.getComponent());
700 mContext.sendOrderedBroadcast(intent, null, new BroadcastReceiver() {
701 @Override
702 public void onReceive(Context context, Intent intent) {
703 try {
704 result.sendResult(getResultExtras(false));
705 } catch (RemoteException e) {
706 }
707 }
708 }, null, Activity.RESULT_OK, null, null);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800709 }
710 }
711
712 public void setActivePasswordState(int mode, int length) {
713 mContext.enforceCallingOrSelfPermission(
714 android.Manifest.permission.BIND_DEVICE_ADMIN, null);
715
716 synchronized (this) {
717 if (mActivePasswordMode != mode || mActivePasswordLength != length
718 || mFailedPasswordAttempts != 0) {
719 long ident = Binder.clearCallingIdentity();
720 try {
721 mActivePasswordMode = mode;
722 mActivePasswordLength = length;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800723 if (mFailedPasswordAttempts != 0) {
724 mFailedPasswordAttempts = 0;
725 saveSettingsLocked();
726 }
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800727 sendAdminCommandLocked(DeviceAdmin.ACTION_PASSWORD_CHANGED,
728 DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800729 } finally {
730 Binder.restoreCallingIdentity(ident);
731 }
732 }
733 }
734 }
735
736 public void reportFailedPasswordAttempt() {
737 mContext.enforceCallingOrSelfPermission(
738 android.Manifest.permission.BIND_DEVICE_ADMIN, null);
739
740 synchronized (this) {
741 long ident = Binder.clearCallingIdentity();
742 try {
743 mFailedPasswordAttempts++;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800744 saveSettingsLocked();
Dianne Hackborn254cb442010-01-27 19:23:59 -0800745 int max = getMaximumFailedPasswordsForWipe(null);
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800746 if (max > 0 && mFailedPasswordAttempts >= max) {
747 wipeDataLocked(0);
748 }
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800749 sendAdminCommandLocked(DeviceAdmin.ACTION_PASSWORD_FAILED,
750 DeviceAdminInfo.USES_POLICY_WATCH_LOGIN);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800751 } finally {
752 Binder.restoreCallingIdentity(ident);
753 }
754 }
755 }
756
757 public void reportSuccessfulPasswordAttempt() {
758 mContext.enforceCallingOrSelfPermission(
759 android.Manifest.permission.BIND_DEVICE_ADMIN, null);
760
761 synchronized (this) {
762 if (mFailedPasswordAttempts != 0) {
763 long ident = Binder.clearCallingIdentity();
764 try {
765 mFailedPasswordAttempts = 0;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800766 saveSettingsLocked();
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800767 sendAdminCommandLocked(DeviceAdmin.ACTION_PASSWORD_SUCCEEDED,
768 DeviceAdminInfo.USES_POLICY_WATCH_LOGIN);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800769 } finally {
770 Binder.restoreCallingIdentity(ident);
771 }
772 }
773 }
774 }
775}