blob: 0dead1cd796cbd044da20a0719a49d006d2e30a4 [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
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080019import com.android.internal.content.PackageMonitor;
Dianne Hackborn42499172010-10-15 18:45:07 -070020import com.android.internal.os.storage.ExternalStorageFormatter;
Dianne Hackborn2269d1572010-02-24 19:54:22 -080021import com.android.internal.util.FastXmlSerializer;
Dianne Hackborn1afd1c92010-03-18 22:47:17 -070022import com.android.internal.util.JournaledFile;
Dianne Hackborn2269d1572010-02-24 19:54:22 -080023import com.android.internal.util.XmlUtils;
Dianne Hackborndf83afa2010-01-20 13:37:26 -080024import com.android.internal.widget.LockPatternUtils;
Dianne Hackbornd6847842010-01-12 18:14:19 -080025
26import org.xmlpull.v1.XmlPullParser;
27import org.xmlpull.v1.XmlPullParserException;
28import org.xmlpull.v1.XmlSerializer;
29
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080030import android.app.Activity;
Jim Millera4e28d12010-11-08 16:15:47 -080031import android.app.AlarmManager;
32import android.app.PendingIntent;
Dianne Hackborn87bba1e2010-02-26 17:25:54 -080033import android.app.admin.DeviceAdminInfo;
34import android.app.admin.DeviceAdminReceiver;
35import android.app.admin.DevicePolicyManager;
36import android.app.admin.IDevicePolicyManager;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080037import android.content.BroadcastReceiver;
Dianne Hackbornd6847842010-01-12 18:14:19 -080038import android.content.ComponentName;
Oscar Montemayor69238c62010-08-03 10:51:06 -070039import android.content.ContentResolver;
Dianne Hackbornd6847842010-01-12 18:14:19 -080040import android.content.Context;
41import android.content.Intent;
Jim Millera4e28d12010-11-08 16:15:47 -080042import android.content.IntentFilter;
Dianne Hackbornd6847842010-01-12 18:14:19 -080043import android.content.pm.PackageManager;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080044import android.content.pm.PackageManager.NameNotFoundException;
Andy Stadler1f35d482010-11-19 15:39:41 -080045import android.content.pm.ResolveInfo;
Dianne Hackbornd6847842010-01-12 18:14:19 -080046import android.os.Binder;
Jim Millera4e28d12010-11-08 16:15:47 -080047import android.os.Handler;
Dianne Hackborndf83afa2010-01-20 13:37:26 -080048import android.os.IBinder;
49import android.os.IPowerManager;
Dianne Hackborn42499172010-10-15 18:45:07 -070050import android.os.PowerManager;
Dianne Hackborndf83afa2010-01-20 13:37:26 -080051import android.os.RecoverySystem;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080052import android.os.RemoteCallback;
Dianne Hackborndf83afa2010-01-20 13:37:26 -080053import android.os.RemoteException;
54import android.os.ServiceManager;
Dianne Hackborn254cb442010-01-27 19:23:59 -080055import android.os.SystemClock;
Oscar Montemayor69238c62010-08-03 10:51:06 -070056import android.provider.Settings;
Dianne Hackborn87bba1e2010-02-26 17:25:54 -080057import android.util.PrintWriterPrinter;
58import android.util.Printer;
Andy Stadler1f35d482010-11-19 15:39:41 -080059import android.util.Slog;
Dianne Hackbornd6847842010-01-12 18:14:19 -080060import android.util.Xml;
Dianne Hackborn254cb442010-01-27 19:23:59 -080061import android.view.WindowManagerPolicy;
Dianne Hackbornd6847842010-01-12 18:14:19 -080062
63import java.io.File;
Dianne Hackborn87bba1e2010-02-26 17:25:54 -080064import java.io.FileDescriptor;
Dianne Hackbornd6847842010-01-12 18:14:19 -080065import java.io.FileInputStream;
Dianne Hackborncef65ee2010-09-30 18:27:22 -070066import java.io.FileNotFoundException;
Dianne Hackbornd6847842010-01-12 18:14:19 -080067import java.io.FileOutputStream;
68import java.io.IOException;
Dianne Hackborn87bba1e2010-02-26 17:25:54 -080069import java.io.PrintWriter;
Jim Millera4e28d12010-11-08 16:15:47 -080070import java.text.DateFormat;
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -080071import java.util.ArrayList;
Jim Millera4e28d12010-11-08 16:15:47 -080072import java.util.Date;
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -080073import java.util.HashMap;
Dianne Hackbornd6847842010-01-12 18:14:19 -080074import java.util.List;
Oscar Montemayor69238c62010-08-03 10:51:06 -070075import java.util.Set;
Dianne Hackbornd6847842010-01-12 18:14:19 -080076
77/**
78 * Implementation of the device policy APIs.
79 */
80public class DevicePolicyManagerService extends IDevicePolicyManager.Stub {
Jim Millera4e28d12010-11-08 16:15:47 -080081 private static final int REQUEST_EXPIRE_PASSWORD = 5571;
82
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080083 static final String TAG = "DevicePolicyManagerService";
Konstantin Lopyrev32558232010-05-20 16:18:05 -070084
Jim Millera4e28d12010-11-08 16:15:47 -080085 private static final long EXPIRATION_GRACE_PERIOD_MS = 5 * 86400 * 1000; // 5 days, in ms
86
87 protected static final String ACTION_EXPIRED_PASSWORD_NOTIFICATION
88 = "com.android.server.ACTION_EXPIRED_PASSWORD_NOTIFICATION";
89
90 private static final long MS_PER_DAY = 86400 * 1000;
Jim Millera4e28d12010-11-08 16:15:47 -080091
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080092 final Context mContext;
93 final MyPackageMonitor mMonitor;
Dianne Hackborn42499172010-10-15 18:45:07 -070094 final PowerManager.WakeLock mWakeLock;
Dianne Hackbornd6847842010-01-12 18:14:19 -080095
Dianne Hackborndf83afa2010-01-20 13:37:26 -080096 IPowerManager mIPowerManager;
Konstantin Lopyrev32558232010-05-20 16:18:05 -070097
Dianne Hackborn9327f4f2010-01-29 10:38:29 -080098 int mActivePasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED;
Dianne Hackbornd6847842010-01-12 18:14:19 -080099 int mActivePasswordLength = 0;
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700100 int mActivePasswordUpperCase = 0;
101 int mActivePasswordLowerCase = 0;
102 int mActivePasswordLetters = 0;
103 int mActivePasswordNumeric = 0;
104 int mActivePasswordSymbols = 0;
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700105 int mActivePasswordNonLetter = 0;
Dianne Hackbornd6847842010-01-12 18:14:19 -0800106 int mFailedPasswordAttempts = 0;
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700107
Dianne Hackborn87bba1e2010-02-26 17:25:54 -0800108 int mPasswordOwner = -1;
Jim Millera4e28d12010-11-08 16:15:47 -0800109 Handler mHandler = new Handler();
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700110
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800111 final HashMap<ComponentName, ActiveAdmin> mAdminMap
112 = new HashMap<ComponentName, ActiveAdmin>();
113 final ArrayList<ActiveAdmin> mAdminList
114 = new ArrayList<ActiveAdmin>();
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700115
Jim Millera4e28d12010-11-08 16:15:47 -0800116 BroadcastReceiver mReceiver = new BroadcastReceiver() {
117 @Override
118 public void onReceive(Context context, Intent intent) {
119 String action = intent.getAction();
120 if (Intent.ACTION_BOOT_COMPLETED.equals(action)
121 || ACTION_EXPIRED_PASSWORD_NOTIFICATION.equals(action)) {
122 Slog.v(TAG, "Sending password expiration notifications for action " + action);
123 mHandler.post(new Runnable() {
124 public void run() {
125 handlePasswordExpirationNotification();
126 }
127 });
128 }
129 }
130 };
131
Dianne Hackbornd6847842010-01-12 18:14:19 -0800132 static class ActiveAdmin {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800133 final DeviceAdminInfo info;
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700134
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800135 int passwordQuality = DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED;
Dianne Hackbornd6847842010-01-12 18:14:19 -0800136 int minimumPasswordLength = 0;
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700137 int passwordHistoryLength = 0;
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700138 int minimumPasswordUpperCase = 0;
139 int minimumPasswordLowerCase = 0;
140 int minimumPasswordLetters = 1;
141 int minimumPasswordNumeric = 1;
142 int minimumPasswordSymbols = 1;
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700143 int minimumPasswordNonLetter = 0;
Dianne Hackbornd6847842010-01-12 18:14:19 -0800144 long maximumTimeToUnlock = 0;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800145 int maximumFailedPasswordsForWipe = 0;
Jim Millera4e28d12010-11-08 16:15:47 -0800146 long passwordExpirationTimeout = 0L;
147 long passwordExpirationDate = 0L;
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700148
Oscar Montemayor69238c62010-08-03 10:51:06 -0700149 // TODO: review implementation decisions with frameworks team
150 boolean specifiesGlobalProxy = false;
151 String globalProxySpec = null;
152 String globalProxyExclusionList = null;
153
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800154 ActiveAdmin(DeviceAdminInfo _info) {
155 info = _info;
156 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700157
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800158 int getUid() { return info.getActivityInfo().applicationInfo.uid; }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700159
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800160 void writeToXml(XmlSerializer out)
161 throws IllegalArgumentException, IllegalStateException, IOException {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800162 out.startTag(null, "policies");
163 info.writePoliciesToXml(out);
164 out.endTag(null, "policies");
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800165 if (passwordQuality != DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) {
166 out.startTag(null, "password-quality");
167 out.attribute(null, "value", Integer.toString(passwordQuality));
168 out.endTag(null, "password-quality");
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800169 if (minimumPasswordLength > 0) {
170 out.startTag(null, "min-password-length");
171 out.attribute(null, "value", Integer.toString(minimumPasswordLength));
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700172 out.endTag(null, "min-password-length");
173 }
174 if(passwordHistoryLength > 0) {
175 out.startTag(null, "password-history-length");
176 out.attribute(null, "value", Integer.toString(passwordHistoryLength));
177 out.endTag(null, "password-history-length");
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800178 }
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700179 if (minimumPasswordUpperCase > 0) {
180 out.startTag(null, "min-password-uppercase");
181 out.attribute(null, "value", Integer.toString(minimumPasswordUpperCase));
182 out.endTag(null, "min-password-uppercase");
183 }
184 if (minimumPasswordLowerCase > 0) {
185 out.startTag(null, "min-password-lowercase");
186 out.attribute(null, "value", Integer.toString(minimumPasswordLowerCase));
187 out.endTag(null, "min-password-lowercase");
188 }
189 if (minimumPasswordLetters > 0) {
190 out.startTag(null, "min-password-letters");
191 out.attribute(null, "value", Integer.toString(minimumPasswordLetters));
192 out.endTag(null, "min-password-letters");
193 }
194 if (minimumPasswordNumeric > 0) {
195 out.startTag(null, "min-password-numeric");
196 out.attribute(null, "value", Integer.toString(minimumPasswordNumeric));
197 out.endTag(null, "min-password-numeric");
198 }
199 if (minimumPasswordSymbols > 0) {
200 out.startTag(null, "min-password-symbols");
201 out.attribute(null, "value", Integer.toString(minimumPasswordSymbols));
202 out.endTag(null, "min-password-symbols");
203 }
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700204 if (minimumPasswordNonLetter > 0) {
205 out.startTag(null, "min-password-nonletter");
206 out.attribute(null, "value", Integer.toString(minimumPasswordNonLetter));
207 out.endTag(null, "min-password-nonletter");
208 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800209 }
Dianne Hackbornab36acb2010-11-05 14:12:11 -0700210 if (maximumTimeToUnlock != 0) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800211 out.startTag(null, "max-time-to-unlock");
212 out.attribute(null, "value", Long.toString(maximumTimeToUnlock));
213 out.endTag(null, "max-time-to-unlock");
214 }
215 if (maximumFailedPasswordsForWipe != 0) {
216 out.startTag(null, "max-failed-password-wipe");
217 out.attribute(null, "value", Integer.toString(maximumFailedPasswordsForWipe));
218 out.endTag(null, "max-failed-password-wipe");
219 }
Oscar Montemayor69238c62010-08-03 10:51:06 -0700220 if (specifiesGlobalProxy) {
221 out.startTag(null, "specifies-global-proxy");
222 out.attribute(null, "value", Boolean.toString(specifiesGlobalProxy));
223 out.endTag(null, "specifies_global_proxy");
224 if (globalProxySpec != null) {
225 out.startTag(null, "global-proxy-spec");
226 out.attribute(null, "value", globalProxySpec);
227 out.endTag(null, "global-proxy-spec");
228 }
229 if (globalProxyExclusionList != null) {
230 out.startTag(null, "global-proxy-exclusion-list");
231 out.attribute(null, "value", globalProxyExclusionList);
232 out.endTag(null, "global-proxy-exclusion-list");
233 }
234 }
Jim Millera4e28d12010-11-08 16:15:47 -0800235 if (passwordExpirationTimeout != 0L) {
236 out.startTag(null, "password-expiration-timeout");
237 out.attribute(null, "value", Long.toString(passwordExpirationTimeout));
238 out.endTag(null, "password-expiration-timeout");
239 }
240 if (passwordExpirationDate != 0L) {
241 out.startTag(null, "password-expiration-date");
242 out.attribute(null, "value", Long.toString(passwordExpirationDate));
243 out.endTag(null, "password-expiration-date");
244 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800245 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700246
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800247 void readFromXml(XmlPullParser parser)
248 throws XmlPullParserException, IOException {
249 int outerDepth = parser.getDepth();
250 int type;
251 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
252 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
253 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
254 continue;
255 }
256 String tag = parser.getName();
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800257 if ("policies".equals(tag)) {
258 info.readPoliciesFromXml(parser);
259 } else if ("password-quality".equals(tag)) {
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800260 passwordQuality = Integer.parseInt(
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800261 parser.getAttributeValue(null, "value"));
262 } else if ("min-password-length".equals(tag)) {
263 minimumPasswordLength = Integer.parseInt(
264 parser.getAttributeValue(null, "value"));
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700265 } else if ("password-history-length".equals(tag)) {
266 passwordHistoryLength = Integer.parseInt(
267 parser.getAttributeValue(null, "value"));
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700268 } else if ("min-password-uppercase".equals(tag)) {
269 minimumPasswordUpperCase = Integer.parseInt(
270 parser.getAttributeValue(null, "value"));
271 } else if ("min-password-lowercase".equals(tag)) {
272 minimumPasswordLowerCase = Integer.parseInt(
273 parser.getAttributeValue(null, "value"));
274 } else if ("min-password-letters".equals(tag)) {
275 minimumPasswordLetters = Integer.parseInt(
276 parser.getAttributeValue(null, "value"));
277 } else if ("min-password-numeric".equals(tag)) {
278 minimumPasswordNumeric = Integer.parseInt(
279 parser.getAttributeValue(null, "value"));
280 } else if ("min-password-symbols".equals(tag)) {
281 minimumPasswordSymbols = Integer.parseInt(
282 parser.getAttributeValue(null, "value"));
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700283 } else if ("min-password-nonletter".equals(tag)) {
284 minimumPasswordNonLetter = Integer.parseInt(
285 parser.getAttributeValue(null, "value"));
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800286 } else if ("max-time-to-unlock".equals(tag)) {
287 maximumTimeToUnlock = Long.parseLong(
288 parser.getAttributeValue(null, "value"));
289 } else if ("max-failed-password-wipe".equals(tag)) {
290 maximumFailedPasswordsForWipe = Integer.parseInt(
291 parser.getAttributeValue(null, "value"));
Oscar Montemayor69238c62010-08-03 10:51:06 -0700292 } else if ("specifies-global-proxy".equals(tag)) {
293 specifiesGlobalProxy = Boolean.getBoolean(
294 parser.getAttributeValue(null, "value"));
295 } else if ("global-proxy-spec".equals(tag)) {
296 globalProxySpec =
297 parser.getAttributeValue(null, "value");
298 } else if ("global-proxy-exclusion-list".equals(tag)) {
299 globalProxyExclusionList =
300 parser.getAttributeValue(null, "value");
Jim Millera4e28d12010-11-08 16:15:47 -0800301 } else if ("password-expiration-timeout".equals(tag)) {
302 passwordExpirationTimeout = Long.parseLong(
303 parser.getAttributeValue(null, "value"));
304 } else if ("password-expiration-date".equals(tag)) {
305 passwordExpirationDate = Long.parseLong(
306 parser.getAttributeValue(null, "value"));
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800307 } else {
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700308 Slog.w(TAG, "Unknown admin tag: " + tag);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800309 }
310 XmlUtils.skipCurrentTag(parser);
311 }
312 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700313
Dianne Hackborn87bba1e2010-02-26 17:25:54 -0800314 void dump(String prefix, PrintWriter pw) {
315 pw.print(prefix); pw.print("uid="); pw.println(getUid());
316 pw.print(prefix); pw.println("policies:");
317 ArrayList<DeviceAdminInfo.PolicyInfo> pols = info.getUsedPolicies();
318 if (pols != null) {
319 for (int i=0; i<pols.size(); i++) {
320 pw.print(prefix); pw.print(" "); pw.println(pols.get(i).tag);
321 }
322 }
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700323 pw.print(prefix); pw.print("passwordQuality=0x");
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700324 pw.println(Integer.toHexString(passwordQuality));
325 pw.print(prefix); pw.print("minimumPasswordLength=");
Dianne Hackborn87bba1e2010-02-26 17:25:54 -0800326 pw.println(minimumPasswordLength);
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700327 pw.print(prefix); pw.print("passwordHistoryLength=");
328 pw.println(passwordHistoryLength);
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700329 pw.print(prefix); pw.print("minimumPasswordUpperCase=");
330 pw.println(minimumPasswordUpperCase);
331 pw.print(prefix); pw.print("minimumPasswordLowerCase=");
332 pw.println(minimumPasswordLowerCase);
333 pw.print(prefix); pw.print("minimumPasswordLetters=");
334 pw.println(minimumPasswordLetters);
335 pw.print(prefix); pw.print("minimumPasswordNumeric=");
336 pw.println(minimumPasswordNumeric);
337 pw.print(prefix); pw.print("minimumPasswordSymbols=");
338 pw.println(minimumPasswordSymbols);
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700339 pw.print(prefix); pw.print("minimumPasswordNonLetter=");
340 pw.println(minimumPasswordNonLetter);
Dianne Hackborn87bba1e2010-02-26 17:25:54 -0800341 pw.print(prefix); pw.print("maximumTimeToUnlock=");
342 pw.println(maximumTimeToUnlock);
343 pw.print(prefix); pw.print("maximumFailedPasswordsForWipe=");
344 pw.println(maximumFailedPasswordsForWipe);
Oscar Montemayor69238c62010-08-03 10:51:06 -0700345 pw.print(prefix); pw.print("specifiesGlobalProxy=");
346 pw.println(specifiesGlobalProxy);
Jim Millera4e28d12010-11-08 16:15:47 -0800347 pw.print(prefix); pw.print("passwordExpirationTimeout=");
348 pw.println(passwordExpirationTimeout);
349 pw.print(prefix); pw.print("passwordExpirationDate=");
350 pw.println(passwordExpirationDate);
Oscar Montemayor69238c62010-08-03 10:51:06 -0700351 if (globalProxySpec != null) {
352 pw.print(prefix); pw.print("globalProxySpec=");
353 pw.println(globalProxySpec);
354 }
355 if (globalProxyExclusionList != null) {
356 pw.print(prefix); pw.print("globalProxyEclusionList=");
357 pw.println(globalProxyExclusionList);
358 }
Dianne Hackborn87bba1e2010-02-26 17:25:54 -0800359 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800360 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700361
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800362 class MyPackageMonitor extends PackageMonitor {
Andy Stadler1f35d482010-11-19 15:39:41 -0800363 @Override
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800364 public void onSomePackagesChanged() {
365 synchronized (DevicePolicyManagerService.this) {
Dianne Hackborn87bba1e2010-02-26 17:25:54 -0800366 boolean removed = false;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800367 for (int i=mAdminList.size()-1; i>=0; i--) {
368 ActiveAdmin aa = mAdminList.get(i);
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700369 int change = isPackageDisappearing(aa.info.getPackageName());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800370 if (change == PACKAGE_PERMANENT_CHANGE
371 || change == PACKAGE_TEMPORARY_CHANGE) {
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700372 Slog.w(TAG, "Admin unexpectedly uninstalled: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800373 + aa.info.getComponent());
Dianne Hackborn87bba1e2010-02-26 17:25:54 -0800374 removed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800375 mAdminList.remove(i);
376 } else if (isPackageModified(aa.info.getPackageName())) {
377 try {
378 mContext.getPackageManager().getReceiverInfo(
379 aa.info.getComponent(), 0);
380 } catch (NameNotFoundException e) {
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700381 Slog.w(TAG, "Admin package change removed component: "
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800382 + aa.info.getComponent());
Dianne Hackborn87bba1e2010-02-26 17:25:54 -0800383 removed = true;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800384 mAdminList.remove(i);
385 }
386 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800387 }
Dianne Hackborn87bba1e2010-02-26 17:25:54 -0800388 if (removed) {
389 validatePasswordOwnerLocked();
390 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800391 }
392 }
Dianne Hackbornd6847842010-01-12 18:14:19 -0800393 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700394
Dianne Hackbornd6847842010-01-12 18:14:19 -0800395 /**
396 * Instantiates the service.
397 */
398 public DevicePolicyManagerService(Context context) {
399 mContext = context;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800400 mMonitor = new MyPackageMonitor();
401 mMonitor.register(context, true);
Dianne Hackborn42499172010-10-15 18:45:07 -0700402 mWakeLock = ((PowerManager)context.getSystemService(Context.POWER_SERVICE))
403 .newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "DPM");
Jim Millera4e28d12010-11-08 16:15:47 -0800404 IntentFilter filter = new IntentFilter();
405 filter.addAction(Intent.ACTION_BOOT_COMPLETED);
406 filter.addAction(ACTION_EXPIRED_PASSWORD_NOTIFICATION);
407 context.registerReceiver(mReceiver, filter);
408 }
409
Andy Stadler043116a2010-11-29 17:43:32 -0800410 /**
411 * Set an alarm for an upcoming event - expiration warning, expiration, or post-expiration
412 * reminders. Clears alarm if no expirations are configured.
413 */
Jim Millera4e28d12010-11-08 16:15:47 -0800414 protected void setExpirationAlarmCheckLocked(Context context) {
415 final long expiration = getPasswordExpirationLocked(null);
416 final long now = System.currentTimeMillis();
417 final long timeToExpire = expiration - now;
418 final long alarmTime;
Andy Stadler043116a2010-11-29 17:43:32 -0800419 if (expiration == 0) {
420 // No expirations are currently configured: Cancel alarm.
421 alarmTime = 0;
422 } else if (timeToExpire <= 0) {
423 // The password has already expired: Repeat every 24 hours.
Jim Millera4e28d12010-11-08 16:15:47 -0800424 alarmTime = now + MS_PER_DAY;
Andy Stadler043116a2010-11-29 17:43:32 -0800425 } else {
426 // Selecting the next alarm time: Roll forward to the next 24 hour multiple before
427 // the expiration time.
428 long alarmInterval = timeToExpire % MS_PER_DAY;
429 if (alarmInterval == 0) {
430 alarmInterval = MS_PER_DAY;
431 }
432 alarmTime = now + alarmInterval;
Jim Millera4e28d12010-11-08 16:15:47 -0800433 }
434
Andy Stadler1f35d482010-11-19 15:39:41 -0800435 long token = Binder.clearCallingIdentity();
436 try {
437 AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
438 PendingIntent pi = PendingIntent.getBroadcast(context, REQUEST_EXPIRE_PASSWORD,
439 new Intent(ACTION_EXPIRED_PASSWORD_NOTIFICATION),
440 PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);
441 am.cancel(pi);
Andy Stadler043116a2010-11-29 17:43:32 -0800442 if (alarmTime != 0) {
443 am.set(AlarmManager.RTC, alarmTime, pi);
444 }
Andy Stadler1f35d482010-11-19 15:39:41 -0800445 } finally {
446 Binder.restoreCallingIdentity(token);
447 }
Dianne Hackbornd6847842010-01-12 18:14:19 -0800448 }
449
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800450 private IPowerManager getIPowerManager() {
451 if (mIPowerManager == null) {
452 IBinder b = ServiceManager.getService(Context.POWER_SERVICE);
453 mIPowerManager = IPowerManager.Stub.asInterface(b);
454 }
455 return mIPowerManager;
456 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700457
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800458 ActiveAdmin getActiveAdminUncheckedLocked(ComponentName who) {
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800459 ActiveAdmin admin = mAdminMap.get(who);
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800460 if (admin != null
461 && who.getPackageName().equals(admin.info.getActivityInfo().packageName)
462 && who.getClassName().equals(admin.info.getActivityInfo().name)) {
463 return admin;
464 }
465 return null;
466 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700467
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800468 ActiveAdmin getActiveAdminForCallerLocked(ComponentName who, int reqPolicy)
469 throws SecurityException {
Dianne Hackborn254cb442010-01-27 19:23:59 -0800470 final int callingUid = Binder.getCallingUid();
471 if (who != null) {
472 ActiveAdmin admin = mAdminMap.get(who);
473 if (admin == null) {
474 throw new SecurityException("No active admin " + who);
475 }
476 if (admin.getUid() != callingUid) {
477 throw new SecurityException("Admin " + who + " is not owned by uid "
478 + Binder.getCallingUid());
479 }
480 if (!admin.info.usesPolicy(reqPolicy)) {
481 throw new SecurityException("Admin " + admin.info.getComponent()
482 + " did not specify uses-policy for: "
483 + admin.info.getTagForPolicy(reqPolicy));
484 }
485 return admin;
486 } else {
487 final int N = mAdminList.size();
488 for (int i=0; i<N; i++) {
489 ActiveAdmin admin = mAdminList.get(i);
490 if (admin.getUid() == callingUid && admin.info.usesPolicy(reqPolicy)) {
491 return admin;
492 }
493 }
494 throw new SecurityException("No active admin owned by uid "
495 + Binder.getCallingUid() + " for policy #" + reqPolicy);
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800496 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800497 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700498
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800499 void sendAdminCommandLocked(ActiveAdmin admin, String action) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800500 Intent intent = new Intent(action);
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800501 intent.setComponent(admin.info.getComponent());
Jim Millera4e28d12010-11-08 16:15:47 -0800502 if (action.equals(DeviceAdminReceiver.ACTION_PASSWORD_EXPIRING)) {
503 intent.putExtra("expiration", admin.passwordExpirationDate);
504 }
Dianne Hackbornd6847842010-01-12 18:14:19 -0800505 mContext.sendBroadcast(intent);
506 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700507
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800508 void sendAdminCommandLocked(String action, int reqPolicy) {
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800509 final int N = mAdminList.size();
510 if (N > 0) {
511 for (int i=0; i<N; i++) {
512 ActiveAdmin admin = mAdminList.get(i);
513 if (admin.info.usesPolicy(reqPolicy)) {
514 sendAdminCommandLocked(admin, action);
515 }
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800516 }
Dianne Hackborn4141d032010-01-21 16:29:00 -0800517 }
518 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700519
Dianne Hackbornd6847842010-01-12 18:14:19 -0800520 void removeActiveAdminLocked(ComponentName adminReceiver) {
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800521 ActiveAdmin admin = getActiveAdminUncheckedLocked(adminReceiver);
522 if (admin != null) {
Oscar Montemayor69238c62010-08-03 10:51:06 -0700523 boolean doProxyCleanup =
524 admin.info.usesPolicy(DeviceAdminInfo.USES_POLICY_SETS_GLOBAL_PROXY);
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800525 sendAdminCommandLocked(admin,
Dianne Hackbornef6b22f2010-02-16 20:38:49 -0800526 DeviceAdminReceiver.ACTION_DEVICE_ADMIN_DISABLED);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800527 // XXX need to wait for it to complete.
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800528 mAdminList.remove(admin);
529 mAdminMap.remove(adminReceiver);
Dianne Hackborn87bba1e2010-02-26 17:25:54 -0800530 validatePasswordOwnerLocked();
Oscar Montemayor69238c62010-08-03 10:51:06 -0700531 if (doProxyCleanup) {
532 resetGlobalProxy();
533 }
Dianne Hackbornd6847842010-01-12 18:14:19 -0800534 }
535 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700536
Dianne Hackbornd6847842010-01-12 18:14:19 -0800537 public DeviceAdminInfo findAdmin(ComponentName adminName) {
538 Intent resolveIntent = new Intent();
539 resolveIntent.setComponent(adminName);
540 List<ResolveInfo> infos = mContext.getPackageManager().queryBroadcastReceivers(
541 resolveIntent, PackageManager.GET_META_DATA);
542 if (infos == null || infos.size() <= 0) {
543 throw new IllegalArgumentException("Unknown admin: " + adminName);
544 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700545
Dianne Hackbornd6847842010-01-12 18:14:19 -0800546 try {
547 return new DeviceAdminInfo(mContext, infos.get(0));
548 } catch (XmlPullParserException e) {
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700549 Slog.w(TAG, "Bad device admin requested: " + adminName, e);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800550 return null;
551 } catch (IOException e) {
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700552 Slog.w(TAG, "Bad device admin requested: " + adminName, e);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800553 return null;
554 }
555 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700556
Dianne Hackbornd6847842010-01-12 18:14:19 -0800557 private static JournaledFile makeJournaledFile() {
558 final String base = "/data/system/device_policies.xml";
559 return new JournaledFile(new File(base), new File(base + ".tmp"));
560 }
561
562 private void saveSettingsLocked() {
563 JournaledFile journal = makeJournaledFile();
564 FileOutputStream stream = null;
565 try {
566 stream = new FileOutputStream(journal.chooseForWrite(), false);
567 XmlSerializer out = new FastXmlSerializer();
568 out.setOutput(stream, "utf-8");
569 out.startDocument(null, true);
570
571 out.startTag(null, "policies");
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700572
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800573 final int N = mAdminList.size();
574 for (int i=0; i<N; i++) {
575 ActiveAdmin ap = mAdminList.get(i);
576 if (ap != null) {
577 out.startTag(null, "admin");
578 out.attribute(null, "name", ap.info.getComponent().flattenToString());
579 ap.writeToXml(out);
580 out.endTag(null, "admin");
581 }
Dianne Hackbornd6847842010-01-12 18:14:19 -0800582 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700583
Dianne Hackborn87bba1e2010-02-26 17:25:54 -0800584 if (mPasswordOwner >= 0) {
585 out.startTag(null, "password-owner");
586 out.attribute(null, "value", Integer.toString(mPasswordOwner));
587 out.endTag(null, "password-owner");
588 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700589
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800590 if (mFailedPasswordAttempts != 0) {
591 out.startTag(null, "failed-password-attempts");
592 out.attribute(null, "value", Integer.toString(mFailedPasswordAttempts));
593 out.endTag(null, "failed-password-attempts");
594 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700595
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700596 if (mActivePasswordQuality != 0 || mActivePasswordLength != 0
597 || mActivePasswordUpperCase != 0 || mActivePasswordLowerCase != 0
598 || mActivePasswordLetters != 0 || mActivePasswordNumeric != 0
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700599 || mActivePasswordSymbols != 0 || mActivePasswordNonLetter != 0) {
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700600 out.startTag(null, "active-password");
601 out.attribute(null, "quality", Integer.toString(mActivePasswordQuality));
602 out.attribute(null, "length", Integer.toString(mActivePasswordLength));
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700603 out.attribute(null, "uppercase", Integer.toString(mActivePasswordUpperCase));
604 out.attribute(null, "lowercase", Integer.toString(mActivePasswordLowerCase));
605 out.attribute(null, "letters", Integer.toString(mActivePasswordLetters));
606 out.attribute(null, "numeric", Integer
607 .toString(mActivePasswordNumeric));
608 out.attribute(null, "symbols", Integer.toString(mActivePasswordSymbols));
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700609 out.attribute(null, "nonletter", Integer.toString(mActivePasswordNonLetter));
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700610 out.endTag(null, "active-password");
611 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700612
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700613 out.endTag(null, "policies");
614
Dianne Hackbornd6847842010-01-12 18:14:19 -0800615 out.endDocument();
616 stream.close();
617 journal.commit();
Jim Miller284b62e2010-06-08 14:27:42 -0700618 sendChangedNotification();
Dianne Hackbornd6847842010-01-12 18:14:19 -0800619 } catch (IOException e) {
620 try {
621 if (stream != null) {
622 stream.close();
623 }
624 } catch (IOException ex) {
625 // Ignore
626 }
627 journal.rollback();
628 }
629 }
630
Jim Miller284b62e2010-06-08 14:27:42 -0700631 private void sendChangedNotification() {
632 Intent intent = new Intent(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED);
633 intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
634 mContext.sendBroadcast(intent);
635 }
636
Dianne Hackbornd6847842010-01-12 18:14:19 -0800637 private void loadSettingsLocked() {
638 JournaledFile journal = makeJournaledFile();
639 FileInputStream stream = null;
640 File file = journal.chooseForRead();
Dianne Hackbornd6847842010-01-12 18:14:19 -0800641 try {
642 stream = new FileInputStream(file);
643 XmlPullParser parser = Xml.newPullParser();
644 parser.setInput(stream, null);
645
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800646 int type;
647 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
648 && type != XmlPullParser.START_TAG) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800649 }
650 String tag = parser.getName();
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800651 if (!"policies".equals(tag)) {
652 throw new XmlPullParserException(
653 "Settings do not start with policies tag: found " + tag);
654 }
655 type = parser.next();
656 int outerDepth = parser.getDepth();
657 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
658 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
659 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
660 continue;
661 }
662 tag = parser.getName();
663 if ("admin".equals(tag)) {
Dianne Hackborne83cefce2010-02-04 17:38:14 -0800664 String name = parser.getAttributeValue(null, "name");
665 try {
666 DeviceAdminInfo dai = findAdmin(
667 ComponentName.unflattenFromString(name));
668 if (dai != null) {
669 ActiveAdmin ap = new ActiveAdmin(dai);
670 ap.readFromXml(parser);
671 mAdminMap.put(ap.info.getComponent(), ap);
672 mAdminList.add(ap);
673 }
674 } catch (RuntimeException e) {
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700675 Slog.w(TAG, "Failed loading admin " + name, e);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800676 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800677 } else if ("failed-password-attempts".equals(tag)) {
678 mFailedPasswordAttempts = Integer.parseInt(
679 parser.getAttributeValue(null, "value"));
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800680 XmlUtils.skipCurrentTag(parser);
Dianne Hackborn87bba1e2010-02-26 17:25:54 -0800681 } else if ("password-owner".equals(tag)) {
682 mPasswordOwner = Integer.parseInt(
683 parser.getAttributeValue(null, "value"));
684 XmlUtils.skipCurrentTag(parser);
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700685 } else if ("active-password".equals(tag)) {
686 mActivePasswordQuality = Integer.parseInt(
687 parser.getAttributeValue(null, "quality"));
688 mActivePasswordLength = Integer.parseInt(
689 parser.getAttributeValue(null, "length"));
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700690 mActivePasswordUpperCase = Integer.parseInt(
691 parser.getAttributeValue(null, "uppercase"));
692 mActivePasswordLowerCase = Integer.parseInt(
693 parser.getAttributeValue(null, "lowercase"));
694 mActivePasswordLetters = Integer.parseInt(
695 parser.getAttributeValue(null, "letters"));
696 mActivePasswordNumeric = Integer.parseInt(
697 parser.getAttributeValue(null, "numeric"));
698 mActivePasswordSymbols = Integer.parseInt(
699 parser.getAttributeValue(null, "symbols"));
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700700 mActivePasswordNonLetter = Integer.parseInt(
701 parser.getAttributeValue(null, "nonletter"));
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700702 XmlUtils.skipCurrentTag(parser);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800703 } else {
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700704 Slog.w(TAG, "Unknown tag: " + tag);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800705 XmlUtils.skipCurrentTag(parser);
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800706 }
Dianne Hackbornd6847842010-01-12 18:14:19 -0800707 }
708 } catch (NullPointerException e) {
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700709 Slog.w(TAG, "failed parsing " + file + " " + e);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800710 } catch (NumberFormatException e) {
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700711 Slog.w(TAG, "failed parsing " + file + " " + e);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800712 } catch (XmlPullParserException e) {
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700713 Slog.w(TAG, "failed parsing " + file + " " + e);
Dianne Hackborncef65ee2010-09-30 18:27:22 -0700714 } catch (FileNotFoundException e) {
715 // Don't be noisy, this is normal if we haven't defined any policies.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800716 } catch (IOException e) {
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700717 Slog.w(TAG, "failed parsing " + file + " " + e);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800718 } catch (IndexOutOfBoundsException e) {
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700719 Slog.w(TAG, "failed parsing " + file + " " + e);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800720 }
721 try {
722 if (stream != null) {
723 stream.close();
724 }
725 } catch (IOException e) {
726 // Ignore
727 }
728
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700729 // Validate that what we stored for the password quality matches
730 // sufficiently what is currently set. Note that this is only
731 // a sanity check in case the two get out of sync; this should
732 // never normally happen.
733 LockPatternUtils utils = new LockPatternUtils(mContext);
734 if (utils.getActivePasswordQuality() < mActivePasswordQuality) {
735 Slog.w(TAG, "Active password quality 0x"
736 + Integer.toHexString(mActivePasswordQuality)
737 + " does not match actual quality 0x"
738 + Integer.toHexString(utils.getActivePasswordQuality()));
739 mActivePasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED;
740 mActivePasswordLength = 0;
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700741 mActivePasswordUpperCase = 0;
742 mActivePasswordLowerCase = 0;
743 mActivePasswordLetters = 0;
744 mActivePasswordNumeric = 0;
745 mActivePasswordSymbols = 0;
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700746 mActivePasswordNonLetter = 0;
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700747 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700748
Dianne Hackborn87bba1e2010-02-26 17:25:54 -0800749 validatePasswordOwnerLocked();
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700750
Dianne Hackborn254cb442010-01-27 19:23:59 -0800751 long timeMs = getMaximumTimeToLock(null);
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800752 if (timeMs <= 0) {
753 timeMs = Integer.MAX_VALUE;
754 }
755 try {
756 getIPowerManager().setMaximumScreenOffTimeount((int)timeMs);
757 } catch (RemoteException e) {
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700758 Slog.w(TAG, "Failure talking with power manager", e);
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800759 }
Dianne Hackbornd6847842010-01-12 18:14:19 -0800760 }
761
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700762 static void validateQualityConstant(int quality) {
763 switch (quality) {
764 case DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED:
765 case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING:
766 case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC:
767 case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC:
768 case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC:
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700769 case DevicePolicyManager.PASSWORD_QUALITY_COMPLEX:
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700770 return;
771 }
772 throw new IllegalArgumentException("Invalid quality constant: 0x"
773 + Integer.toHexString(quality));
774 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700775
Dianne Hackborn87bba1e2010-02-26 17:25:54 -0800776 void validatePasswordOwnerLocked() {
777 if (mPasswordOwner >= 0) {
778 boolean haveOwner = false;
779 for (int i=mAdminList.size()-1; i>=0; i--) {
780 if (mAdminList.get(i).getUid() == mPasswordOwner) {
781 haveOwner = true;
782 break;
783 }
784 }
785 if (!haveOwner) {
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700786 Slog.w(TAG, "Previous password owner " + mPasswordOwner
Dianne Hackborn87bba1e2010-02-26 17:25:54 -0800787 + " no longer active; disabling");
788 mPasswordOwner = -1;
789 }
790 }
791 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700792
Dianne Hackbornd6847842010-01-12 18:14:19 -0800793 public void systemReady() {
794 synchronized (this) {
795 loadSettingsLocked();
796 }
797 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700798
Jim Millera4e28d12010-11-08 16:15:47 -0800799 private void handlePasswordExpirationNotification() {
800 synchronized (this) {
801 final long now = System.currentTimeMillis();
802 final int N = mAdminList.size();
803 if (N <= 0) {
804 return;
805 }
806 for (int i=0; i < N; i++) {
807 ActiveAdmin admin = mAdminList.get(i);
808 if (admin.info.usesPolicy(DeviceAdminInfo.USES_POLICY_EXPIRE_PASSWORD)
809 && admin.passwordExpirationTimeout > 0L
810 && admin.passwordExpirationDate > 0L
Andy Stadler043116a2010-11-29 17:43:32 -0800811 && now >= admin.passwordExpirationDate - EXPIRATION_GRACE_PERIOD_MS) {
Jim Millera4e28d12010-11-08 16:15:47 -0800812 sendAdminCommandLocked(admin, DeviceAdminReceiver.ACTION_PASSWORD_EXPIRING);
813 }
814 }
815 setExpirationAlarmCheckLocked(mContext);
816 }
817 }
818
Dianne Hackbornd6847842010-01-12 18:14:19 -0800819 public void setActiveAdmin(ComponentName adminReceiver) {
820 mContext.enforceCallingOrSelfPermission(
821 android.Manifest.permission.BIND_DEVICE_ADMIN, null);
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700822
Dianne Hackbornd6847842010-01-12 18:14:19 -0800823 DeviceAdminInfo info = findAdmin(adminReceiver);
824 if (info == null) {
825 throw new IllegalArgumentException("Bad admin: " + adminReceiver);
826 }
827 synchronized (this) {
828 long ident = Binder.clearCallingIdentity();
829 try {
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800830 if (getActiveAdminUncheckedLocked(adminReceiver) != null) {
831 throw new IllegalArgumentException("Admin is already added");
Dianne Hackbornd6847842010-01-12 18:14:19 -0800832 }
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800833 ActiveAdmin admin = new ActiveAdmin(info);
834 mAdminMap.put(adminReceiver, admin);
835 mAdminList.add(admin);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800836 saveSettingsLocked();
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800837 sendAdminCommandLocked(admin,
Dianne Hackbornef6b22f2010-02-16 20:38:49 -0800838 DeviceAdminReceiver.ACTION_DEVICE_ADMIN_ENABLED);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800839 } finally {
840 Binder.restoreCallingIdentity(ident);
841 }
842 }
843 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700844
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800845 public boolean isAdminActive(ComponentName adminReceiver) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800846 synchronized (this) {
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800847 return getActiveAdminUncheckedLocked(adminReceiver) != null;
848 }
849 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700850
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800851 public List<ComponentName> getActiveAdmins() {
852 synchronized (this) {
853 final int N = mAdminList.size();
854 if (N <= 0) {
855 return null;
856 }
857 ArrayList<ComponentName> res = new ArrayList<ComponentName>(N);
858 for (int i=0; i<N; i++) {
859 res.add(mAdminList.get(i).info.getComponent());
860 }
861 return res;
Dianne Hackbornd6847842010-01-12 18:14:19 -0800862 }
863 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700864
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800865 public boolean packageHasActiveAdmins(String packageName) {
866 synchronized (this) {
867 final int N = mAdminList.size();
868 for (int i=0; i<N; i++) {
869 if (mAdminList.get(i).info.getPackageName().equals(packageName)) {
870 return true;
871 }
872 }
873 return false;
874 }
875 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700876
Dianne Hackbornd6847842010-01-12 18:14:19 -0800877 public void removeActiveAdmin(ComponentName adminReceiver) {
878 synchronized (this) {
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800879 ActiveAdmin admin = getActiveAdminUncheckedLocked(adminReceiver);
880 if (admin == null) {
881 return;
882 }
883 if (admin.getUid() != Binder.getCallingUid()) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800884 mContext.enforceCallingOrSelfPermission(
885 android.Manifest.permission.BIND_DEVICE_ADMIN, null);
886 }
887 long ident = Binder.clearCallingIdentity();
888 try {
889 removeActiveAdminLocked(adminReceiver);
890 } finally {
891 Binder.restoreCallingIdentity(ident);
892 }
893 }
894 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700895
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700896 public void setPasswordQuality(ComponentName who, int quality) {
897 validateQualityConstant(quality);
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700898
Dianne Hackbornd6847842010-01-12 18:14:19 -0800899 synchronized (this) {
900 if (who == null) {
901 throw new NullPointerException("ComponentName is null");
902 }
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800903 ActiveAdmin ap = getActiveAdminForCallerLocked(who,
904 DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD);
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700905 if (ap.passwordQuality != quality) {
906 ap.passwordQuality = quality;
Dianne Hackbornd6847842010-01-12 18:14:19 -0800907 saveSettingsLocked();
908 }
909 }
910 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700911
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800912 public int getPasswordQuality(ComponentName who) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800913 synchronized (this) {
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800914 int mode = DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED;
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700915
Dianne Hackborn254cb442010-01-27 19:23:59 -0800916 if (who != null) {
917 ActiveAdmin admin = getActiveAdminUncheckedLocked(who);
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800918 return admin != null ? admin.passwordQuality : mode;
Dianne Hackborn254cb442010-01-27 19:23:59 -0800919 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700920
Dianne Hackborn254cb442010-01-27 19:23:59 -0800921 final int N = mAdminList.size();
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800922 for (int i=0; i<N; i++) {
923 ActiveAdmin admin = mAdminList.get(i);
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800924 if (mode < admin.passwordQuality) {
925 mode = admin.passwordQuality;
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800926 }
927 }
928 return mode;
Dianne Hackbornd6847842010-01-12 18:14:19 -0800929 }
930 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700931
Dianne Hackborn254cb442010-01-27 19:23:59 -0800932 public void setPasswordMinimumLength(ComponentName who, int length) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800933 synchronized (this) {
934 if (who == null) {
935 throw new NullPointerException("ComponentName is null");
936 }
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800937 ActiveAdmin ap = getActiveAdminForCallerLocked(who,
938 DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800939 if (ap.minimumPasswordLength != length) {
940 ap.minimumPasswordLength = length;
941 saveSettingsLocked();
942 }
943 }
944 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700945
Dianne Hackborn254cb442010-01-27 19:23:59 -0800946 public int getPasswordMinimumLength(ComponentName who) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800947 synchronized (this) {
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800948 int length = 0;
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700949
Dianne Hackborn254cb442010-01-27 19:23:59 -0800950 if (who != null) {
951 ActiveAdmin admin = getActiveAdminUncheckedLocked(who);
952 return admin != null ? admin.minimumPasswordLength : length;
953 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700954
Dianne Hackborn254cb442010-01-27 19:23:59 -0800955 final int N = mAdminList.size();
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800956 for (int i=0; i<N; i++) {
957 ActiveAdmin admin = mAdminList.get(i);
958 if (length < admin.minimumPasswordLength) {
959 length = admin.minimumPasswordLength;
960 }
961 }
962 return length;
Dianne Hackbornd6847842010-01-12 18:14:19 -0800963 }
964 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700965
966 public void setPasswordHistoryLength(ComponentName who, int length) {
967 synchronized (this) {
968 if (who == null) {
969 throw new NullPointerException("ComponentName is null");
970 }
971 ActiveAdmin ap = getActiveAdminForCallerLocked(who,
972 DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD);
973 if (ap.passwordHistoryLength != length) {
974 ap.passwordHistoryLength = length;
975 saveSettingsLocked();
976 }
977 }
978 }
979
980 public int getPasswordHistoryLength(ComponentName who) {
981 synchronized (this) {
982 int length = 0;
983
984 if (who != null) {
985 ActiveAdmin admin = getActiveAdminUncheckedLocked(who);
986 return admin != null ? admin.passwordHistoryLength : length;
987 }
988
989 final int N = mAdminList.size();
990 for (int i = 0; i < N; i++) {
991 ActiveAdmin admin = mAdminList.get(i);
992 if (length < admin.passwordHistoryLength) {
993 length = admin.passwordHistoryLength;
994 }
995 }
996 return length;
997 }
998 }
999
Jim Millera4e28d12010-11-08 16:15:47 -08001000 public void setPasswordExpirationTimeout(ComponentName who, long timeout) {
1001 synchronized (this) {
1002 if (who == null) {
1003 throw new NullPointerException("ComponentName is null");
1004 }
Andy Stadler1f35d482010-11-19 15:39:41 -08001005 if (timeout < 0) {
1006 throw new IllegalArgumentException("Timeout must be >= 0 ms");
Jim Millera4e28d12010-11-08 16:15:47 -08001007 }
1008 ActiveAdmin ap = getActiveAdminForCallerLocked(who,
1009 DeviceAdminInfo.USES_POLICY_EXPIRE_PASSWORD);
1010 // Calling this API automatically bumps the expiration date
1011 final long expiration = timeout > 0L ? (timeout + System.currentTimeMillis()) : 0L;
1012 ap.passwordExpirationDate = expiration;
1013 ap.passwordExpirationTimeout = timeout;
1014 if (timeout > 0L) {
1015 Slog.w(TAG, "setPasswordExpiration(): password will expire on "
1016 + DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT)
1017 .format(new Date(expiration)));
1018 }
1019 saveSettingsLocked();
1020 setExpirationAlarmCheckLocked(mContext); // in case this is the first one
1021 }
1022 }
1023
Andy Stadler043116a2010-11-29 17:43:32 -08001024 /**
1025 * Return a single admin's expiration cycle time, or the min of all cycle times.
1026 * Returns 0 if not configured.
1027 */
Jim Millera4e28d12010-11-08 16:15:47 -08001028 public long getPasswordExpirationTimeout(ComponentName who) {
1029 synchronized (this) {
Jim Millera4e28d12010-11-08 16:15:47 -08001030 if (who != null) {
1031 ActiveAdmin admin = getActiveAdminUncheckedLocked(who);
Andy Stadler043116a2010-11-29 17:43:32 -08001032 return admin != null ? admin.passwordExpirationTimeout : 0L;
Jim Millera4e28d12010-11-08 16:15:47 -08001033 }
1034
Andy Stadler043116a2010-11-29 17:43:32 -08001035 long timeout = 0L;
Jim Millera4e28d12010-11-08 16:15:47 -08001036 final int N = mAdminList.size();
1037 for (int i = 0; i < N; i++) {
1038 ActiveAdmin admin = mAdminList.get(i);
1039 if (timeout == 0L || (admin.passwordExpirationTimeout != 0L
1040 && timeout > admin.passwordExpirationTimeout)) {
1041 timeout = admin.passwordExpirationTimeout;
1042 }
1043 }
1044 return timeout;
1045 }
1046 }
1047
Andy Stadler043116a2010-11-29 17:43:32 -08001048 /**
1049 * Return a single admin's expiration date/time, or the min (soonest) for all admins.
1050 * Returns 0 if not configured.
1051 */
Jim Millera4e28d12010-11-08 16:15:47 -08001052 private long getPasswordExpirationLocked(ComponentName who) {
Jim Millera4e28d12010-11-08 16:15:47 -08001053 if (who != null) {
1054 ActiveAdmin admin = getActiveAdminUncheckedLocked(who);
Andy Stadler043116a2010-11-29 17:43:32 -08001055 return admin != null ? admin.passwordExpirationDate : 0L;
Jim Millera4e28d12010-11-08 16:15:47 -08001056 }
1057
Andy Stadler043116a2010-11-29 17:43:32 -08001058 long timeout = 0L;
Jim Millera4e28d12010-11-08 16:15:47 -08001059 final int N = mAdminList.size();
1060 for (int i = 0; i < N; i++) {
1061 ActiveAdmin admin = mAdminList.get(i);
1062 if (timeout == 0L || (admin.passwordExpirationDate != 0
1063 && timeout > admin.passwordExpirationDate)) {
1064 timeout = admin.passwordExpirationDate;
1065 }
1066 }
1067 return timeout;
1068 }
1069
1070 public long getPasswordExpiration(ComponentName who) {
1071 synchronized (this) {
1072 return getPasswordExpirationLocked(who);
1073 }
1074 }
1075
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -07001076 public void setPasswordMinimumUpperCase(ComponentName who, int length) {
1077 synchronized (this) {
1078 if (who == null) {
1079 throw new NullPointerException("ComponentName is null");
1080 }
1081 ActiveAdmin ap = getActiveAdminForCallerLocked(who,
1082 DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD);
1083 if (ap.minimumPasswordUpperCase != length) {
1084 ap.minimumPasswordUpperCase = length;
1085 saveSettingsLocked();
1086 }
1087 }
1088 }
1089
1090 public int getPasswordMinimumUpperCase(ComponentName who) {
1091 synchronized (this) {
1092 int length = 0;
1093
1094 if (who != null) {
1095 ActiveAdmin admin = getActiveAdminUncheckedLocked(who);
1096 return admin != null ? admin.minimumPasswordUpperCase : length;
1097 }
1098
1099 final int N = mAdminList.size();
1100 for (int i=0; i<N; i++) {
1101 ActiveAdmin admin = mAdminList.get(i);
1102 if (length < admin.minimumPasswordUpperCase) {
1103 length = admin.minimumPasswordUpperCase;
1104 }
1105 }
1106 return length;
1107 }
1108 }
1109
1110 public void setPasswordMinimumLowerCase(ComponentName who, int length) {
1111 synchronized (this) {
1112 if (who == null) {
1113 throw new NullPointerException("ComponentName is null");
1114 }
1115 ActiveAdmin ap = getActiveAdminForCallerLocked(who,
1116 DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD);
1117 if (ap.minimumPasswordLowerCase != length) {
1118 ap.minimumPasswordLowerCase = length;
1119 saveSettingsLocked();
1120 }
1121 }
1122 }
1123
1124 public int getPasswordMinimumLowerCase(ComponentName who) {
1125 synchronized (this) {
1126 int length = 0;
1127
1128 if (who != null) {
1129 ActiveAdmin admin = getActiveAdminUncheckedLocked(who);
1130 return admin != null ? admin.minimumPasswordLowerCase : length;
1131 }
1132
1133 final int N = mAdminList.size();
1134 for (int i=0; i<N; i++) {
1135 ActiveAdmin admin = mAdminList.get(i);
1136 if (length < admin.minimumPasswordLowerCase) {
1137 length = admin.minimumPasswordLowerCase;
1138 }
1139 }
1140 return length;
1141 }
1142 }
1143
1144 public void setPasswordMinimumLetters(ComponentName who, int length) {
1145 synchronized (this) {
1146 if (who == null) {
1147 throw new NullPointerException("ComponentName is null");
1148 }
1149 ActiveAdmin ap = getActiveAdminForCallerLocked(who,
1150 DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD);
1151 if (ap.minimumPasswordLetters != length) {
1152 ap.minimumPasswordLetters = length;
1153 saveSettingsLocked();
1154 }
1155 }
1156 }
1157
1158 public int getPasswordMinimumLetters(ComponentName who) {
1159 synchronized (this) {
1160 int length = 0;
1161
1162 if (who != null) {
1163 ActiveAdmin admin = getActiveAdminUncheckedLocked(who);
1164 return admin != null ? admin.minimumPasswordLetters : length;
1165 }
1166
1167 final int N = mAdminList.size();
1168 for (int i=0; i<N; i++) {
1169 ActiveAdmin admin = mAdminList.get(i);
1170 if (length < admin.minimumPasswordLetters) {
1171 length = admin.minimumPasswordLetters;
1172 }
1173 }
1174 return length;
1175 }
1176 }
1177
1178 public void setPasswordMinimumNumeric(ComponentName who, int length) {
1179 synchronized (this) {
1180 if (who == null) {
1181 throw new NullPointerException("ComponentName is null");
1182 }
1183 ActiveAdmin ap = getActiveAdminForCallerLocked(who,
1184 DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD);
1185 if (ap.minimumPasswordNumeric != length) {
1186 ap.minimumPasswordNumeric = length;
1187 saveSettingsLocked();
1188 }
1189 }
1190 }
1191
1192 public int getPasswordMinimumNumeric(ComponentName who) {
1193 synchronized (this) {
1194 int length = 0;
1195
1196 if (who != null) {
1197 ActiveAdmin admin = getActiveAdminUncheckedLocked(who);
1198 return admin != null ? admin.minimumPasswordNumeric : length;
1199 }
1200
1201 final int N = mAdminList.size();
1202 for (int i = 0; i < N; i++) {
1203 ActiveAdmin admin = mAdminList.get(i);
1204 if (length < admin.minimumPasswordNumeric) {
1205 length = admin.minimumPasswordNumeric;
1206 }
1207 }
1208 return length;
1209 }
1210 }
1211
1212 public void setPasswordMinimumSymbols(ComponentName who, int length) {
1213 synchronized (this) {
1214 if (who == null) {
1215 throw new NullPointerException("ComponentName is null");
1216 }
1217 ActiveAdmin ap = getActiveAdminForCallerLocked(who,
1218 DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD);
1219 if (ap.minimumPasswordSymbols != length) {
1220 ap.minimumPasswordSymbols = length;
1221 saveSettingsLocked();
1222 }
1223 }
1224 }
1225
1226 public int getPasswordMinimumSymbols(ComponentName who) {
1227 synchronized (this) {
1228 int length = 0;
1229
1230 if (who != null) {
1231 ActiveAdmin admin = getActiveAdminUncheckedLocked(who);
1232 return admin != null ? admin.minimumPasswordSymbols : length;
1233 }
1234
1235 final int N = mAdminList.size();
1236 for (int i=0; i<N; i++) {
1237 ActiveAdmin admin = mAdminList.get(i);
1238 if (length < admin.minimumPasswordSymbols) {
1239 length = admin.minimumPasswordSymbols;
1240 }
1241 }
1242 return length;
1243 }
1244 }
1245
Konstantin Lopyrevc8577402010-06-04 17:15:02 -07001246 public void setPasswordMinimumNonLetter(ComponentName who, int length) {
1247 synchronized (this) {
1248 if (who == null) {
1249 throw new NullPointerException("ComponentName is null");
1250 }
1251 ActiveAdmin ap = getActiveAdminForCallerLocked(who,
1252 DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD);
1253 if (ap.minimumPasswordNonLetter != length) {
1254 ap.minimumPasswordNonLetter = length;
1255 saveSettingsLocked();
1256 }
1257 }
1258 }
1259
1260 public int getPasswordMinimumNonLetter(ComponentName who) {
1261 synchronized (this) {
1262 int length = 0;
1263
1264 if (who != null) {
1265 ActiveAdmin admin = getActiveAdminUncheckedLocked(who);
1266 return admin != null ? admin.minimumPasswordNonLetter : length;
1267 }
1268
1269 final int N = mAdminList.size();
1270 for (int i=0; i<N; i++) {
1271 ActiveAdmin admin = mAdminList.get(i);
1272 if (length < admin.minimumPasswordNonLetter) {
1273 length = admin.minimumPasswordNonLetter;
1274 }
1275 }
1276 return length;
1277 }
1278 }
1279
Dianne Hackborndf83afa2010-01-20 13:37:26 -08001280 public boolean isActivePasswordSufficient() {
Dianne Hackbornd6847842010-01-12 18:14:19 -08001281 synchronized (this) {
1282 // This API can only be called by an active device admin,
1283 // so try to retrieve it to check that the caller is one.
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08001284 getActiveAdminForCallerLocked(null,
1285 DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD);
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -07001286 if (mActivePasswordQuality < getPasswordQuality(null)
1287 || mActivePasswordLength < getPasswordMinimumLength(null)) {
1288 return false;
1289 }
1290 if(mActivePasswordQuality != DevicePolicyManager.PASSWORD_QUALITY_COMPLEX) {
1291 return true;
1292 }
1293 return mActivePasswordUpperCase >= getPasswordMinimumUpperCase(null)
1294 && mActivePasswordLowerCase >= getPasswordMinimumLowerCase(null)
1295 && mActivePasswordLetters >= getPasswordMinimumLetters(null)
1296 && mActivePasswordNumeric >= getPasswordMinimumNumeric(null)
Konstantin Lopyrevc8577402010-06-04 17:15:02 -07001297 && mActivePasswordSymbols >= getPasswordMinimumSymbols(null)
1298 && mActivePasswordNonLetter >= getPasswordMinimumNonLetter(null);
Dianne Hackbornd6847842010-01-12 18:14:19 -08001299 }
1300 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001301
Dianne Hackbornd6847842010-01-12 18:14:19 -08001302 public int getCurrentFailedPasswordAttempts() {
1303 synchronized (this) {
1304 // This API can only be called by an active device admin,
1305 // so try to retrieve it to check that the caller is one.
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08001306 getActiveAdminForCallerLocked(null,
1307 DeviceAdminInfo.USES_POLICY_WATCH_LOGIN);
Dianne Hackbornd6847842010-01-12 18:14:19 -08001308 return mFailedPasswordAttempts;
1309 }
1310 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001311
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001312 public void setMaximumFailedPasswordsForWipe(ComponentName who, int num) {
1313 synchronized (this) {
1314 // This API can only be called by an active device admin,
1315 // so try to retrieve it to check that the caller is one.
1316 getActiveAdminForCallerLocked(who,
1317 DeviceAdminInfo.USES_POLICY_WIPE_DATA);
1318 ActiveAdmin ap = getActiveAdminForCallerLocked(who,
1319 DeviceAdminInfo.USES_POLICY_WATCH_LOGIN);
1320 if (ap.maximumFailedPasswordsForWipe != num) {
1321 ap.maximumFailedPasswordsForWipe = num;
1322 saveSettingsLocked();
1323 }
1324 }
1325 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001326
Dianne Hackborn254cb442010-01-27 19:23:59 -08001327 public int getMaximumFailedPasswordsForWipe(ComponentName who) {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001328 synchronized (this) {
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -08001329 int count = 0;
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001330
Dianne Hackborn254cb442010-01-27 19:23:59 -08001331 if (who != null) {
1332 ActiveAdmin admin = getActiveAdminUncheckedLocked(who);
1333 return admin != null ? admin.maximumFailedPasswordsForWipe : count;
1334 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001335
Dianne Hackborn254cb442010-01-27 19:23:59 -08001336 final int N = mAdminList.size();
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -08001337 for (int i=0; i<N; i++) {
1338 ActiveAdmin admin = mAdminList.get(i);
1339 if (count == 0) {
1340 count = admin.maximumFailedPasswordsForWipe;
1341 } else if (admin.maximumFailedPasswordsForWipe != 0
1342 && count > admin.maximumFailedPasswordsForWipe) {
1343 count = admin.maximumFailedPasswordsForWipe;
1344 }
1345 }
1346 return count;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001347 }
1348 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001349
Dianne Hackborn87bba1e2010-02-26 17:25:54 -08001350 public boolean resetPassword(String password, int flags) {
Dianne Hackborn9327f4f2010-01-29 10:38:29 -08001351 int quality;
Dianne Hackborndf83afa2010-01-20 13:37:26 -08001352 synchronized (this) {
1353 // This API can only be called by an active device admin,
1354 // so try to retrieve it to check that the caller is one.
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08001355 getActiveAdminForCallerLocked(null,
1356 DeviceAdminInfo.USES_POLICY_RESET_PASSWORD);
Dianne Hackborn9327f4f2010-01-29 10:38:29 -08001357 quality = getPasswordQuality(null);
1358 if (quality != DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) {
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -07001359 int realQuality = LockPatternUtils.computePasswordQuality(password);
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -07001360 if (realQuality < quality
1361 && quality != DevicePolicyManager.PASSWORD_QUALITY_COMPLEX) {
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -07001362 Slog.w(TAG, "resetPassword: password quality 0x"
1363 + Integer.toHexString(quality)
1364 + " does not meet required quality 0x"
1365 + Integer.toHexString(quality));
Dianne Hackborn9327f4f2010-01-29 10:38:29 -08001366 return false;
1367 }
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -07001368 quality = Math.max(realQuality, quality);
Dianne Hackborn9327f4f2010-01-29 10:38:29 -08001369 }
1370 int length = getPasswordMinimumLength(null);
1371 if (password.length() < length) {
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -07001372 Slog.w(TAG, "resetPassword: password length " + password.length()
1373 + " does not meet required length " + length);
Dianne Hackborndf83afa2010-01-20 13:37:26 -08001374 return false;
1375 }
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -07001376 if (quality == DevicePolicyManager.PASSWORD_QUALITY_COMPLEX) {
1377 int letters = 0;
1378 int uppercase = 0;
1379 int lowercase = 0;
1380 int numbers = 0;
1381 int symbols = 0;
Konstantin Lopyrevc8577402010-06-04 17:15:02 -07001382 int nonletter = 0;
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -07001383 for (int i = 0; i < password.length(); i++) {
1384 char c = password.charAt(i);
1385 if (c >= 'A' && c <= 'Z') {
1386 letters++;
1387 uppercase++;
1388 } else if (c >= 'a' && c <= 'z') {
1389 letters++;
1390 lowercase++;
1391 } else if (c >= '0' && c <= '9') {
1392 numbers++;
Konstantin Lopyrevc8577402010-06-04 17:15:02 -07001393 nonletter++;
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -07001394 } else {
1395 symbols++;
Konstantin Lopyrevc8577402010-06-04 17:15:02 -07001396 nonletter++;
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -07001397 }
1398 }
1399 int neededLetters = getPasswordMinimumLetters(null);
1400 if(letters < neededLetters) {
1401 Slog.w(TAG, "resetPassword: number of letters " + letters
1402 + " does not meet required number of letters " + neededLetters);
1403 return false;
1404 }
1405 int neededNumbers = getPasswordMinimumNumeric(null);
1406 if (numbers < neededNumbers) {
1407 Slog
1408 .w(TAG, "resetPassword: number of numerical digits " + numbers
1409 + " does not meet required number of numerical digits "
1410 + neededNumbers);
1411 return false;
1412 }
1413 int neededLowerCase = getPasswordMinimumLowerCase(null);
1414 if (lowercase < neededLowerCase) {
1415 Slog.w(TAG, "resetPassword: number of lowercase letters " + lowercase
1416 + " does not meet required number of lowercase letters "
1417 + neededLowerCase);
1418 return false;
1419 }
1420 int neededUpperCase = getPasswordMinimumUpperCase(null);
1421 if (uppercase < neededUpperCase) {
1422 Slog.w(TAG, "resetPassword: number of uppercase letters " + uppercase
1423 + " does not meet required number of uppercase letters "
1424 + neededUpperCase);
1425 return false;
1426 }
1427 int neededSymbols = getPasswordMinimumSymbols(null);
1428 if (symbols < neededSymbols) {
1429 Slog.w(TAG, "resetPassword: number of special symbols " + symbols
1430 + " does not meet required number of special symbols " + neededSymbols);
1431 return false;
1432 }
Konstantin Lopyrevc8577402010-06-04 17:15:02 -07001433 int neededNonLetter = getPasswordMinimumNonLetter(null);
1434 if (nonletter < neededNonLetter) {
1435 Slog.w(TAG, "resetPassword: number of non-letter characters " + nonletter
1436 + " does not meet required number of non-letter characters "
1437 + neededNonLetter);
1438 return false;
1439 }
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -07001440 }
1441
1442 LockPatternUtils utils = new LockPatternUtils(mContext);
1443 if(utils.checkPasswordHistory(password)) {
1444 Slog.w(TAG, "resetPassword: password is the same as one of the last "
1445 + getPasswordHistoryLength(null) + " passwords");
1446 return false;
1447 }
Dianne Hackborndf83afa2010-01-20 13:37:26 -08001448 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001449
Dianne Hackborn87bba1e2010-02-26 17:25:54 -08001450 int callingUid = Binder.getCallingUid();
1451 if (mPasswordOwner >= 0 && mPasswordOwner != callingUid) {
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -07001452 Slog.w(TAG, "resetPassword: already set by another uid and not entered by user");
Dianne Hackborn87bba1e2010-02-26 17:25:54 -08001453 return false;
1454 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001455
Dianne Hackborndf83afa2010-01-20 13:37:26 -08001456 // Don't do this with the lock held, because it is going to call
1457 // back in to the service.
1458 long ident = Binder.clearCallingIdentity();
1459 try {
1460 LockPatternUtils utils = new LockPatternUtils(mContext);
Dianne Hackborn9327f4f2010-01-29 10:38:29 -08001461 utils.saveLockPassword(password, quality);
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -07001462 synchronized (this) {
1463 int newOwner = (flags&DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY)
1464 != 0 ? callingUid : -1;
1465 if (mPasswordOwner != newOwner) {
1466 mPasswordOwner = newOwner;
1467 saveSettingsLocked();
1468 }
Dianne Hackborn87bba1e2010-02-26 17:25:54 -08001469 }
Dianne Hackborndf83afa2010-01-20 13:37:26 -08001470 } finally {
1471 Binder.restoreCallingIdentity(ident);
1472 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001473
Dianne Hackborndf83afa2010-01-20 13:37:26 -08001474 return true;
1475 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001476
Dianne Hackbornd6847842010-01-12 18:14:19 -08001477 public void setMaximumTimeToLock(ComponentName who, long timeMs) {
1478 synchronized (this) {
1479 if (who == null) {
1480 throw new NullPointerException("ComponentName is null");
1481 }
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08001482 ActiveAdmin ap = getActiveAdminForCallerLocked(who,
Dianne Hackborn315ada72010-02-11 12:14:08 -08001483 DeviceAdminInfo.USES_POLICY_FORCE_LOCK);
Dianne Hackbornd6847842010-01-12 18:14:19 -08001484 if (ap.maximumTimeToUnlock != timeMs) {
1485 ap.maximumTimeToUnlock = timeMs;
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001486
Dianne Hackborndf83afa2010-01-20 13:37:26 -08001487 long ident = Binder.clearCallingIdentity();
1488 try {
1489 saveSettingsLocked();
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001490
Dianne Hackborn254cb442010-01-27 19:23:59 -08001491 timeMs = getMaximumTimeToLock(null);
Dianne Hackborndf83afa2010-01-20 13:37:26 -08001492 if (timeMs <= 0) {
1493 timeMs = Integer.MAX_VALUE;
1494 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001495
Dianne Hackborndf83afa2010-01-20 13:37:26 -08001496 try {
1497 getIPowerManager().setMaximumScreenOffTimeount((int)timeMs);
1498 } catch (RemoteException e) {
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -07001499 Slog.w(TAG, "Failure talking with power manager", e);
Dianne Hackborndf83afa2010-01-20 13:37:26 -08001500 }
1501 } finally {
1502 Binder.restoreCallingIdentity(ident);
1503 }
Dianne Hackbornd6847842010-01-12 18:14:19 -08001504 }
1505 }
1506 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001507
Dianne Hackborn254cb442010-01-27 19:23:59 -08001508 public long getMaximumTimeToLock(ComponentName who) {
Dianne Hackbornd6847842010-01-12 18:14:19 -08001509 synchronized (this) {
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -08001510 long time = 0;
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001511
Dianne Hackborn254cb442010-01-27 19:23:59 -08001512 if (who != null) {
1513 ActiveAdmin admin = getActiveAdminUncheckedLocked(who);
1514 return admin != null ? admin.maximumTimeToUnlock : time;
1515 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001516
Dianne Hackborn254cb442010-01-27 19:23:59 -08001517 final int N = mAdminList.size();
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -08001518 for (int i=0; i<N; i++) {
1519 ActiveAdmin admin = mAdminList.get(i);
1520 if (time == 0) {
1521 time = admin.maximumTimeToUnlock;
1522 } else if (admin.maximumTimeToUnlock != 0
1523 && time > admin.maximumTimeToUnlock) {
1524 time = admin.maximumTimeToUnlock;
1525 }
1526 }
1527 return time;
Dianne Hackbornd6847842010-01-12 18:14:19 -08001528 }
1529 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001530
Dianne Hackborndf83afa2010-01-20 13:37:26 -08001531 public void lockNow() {
1532 synchronized (this) {
1533 // This API can only be called by an active device admin,
1534 // so try to retrieve it to check that the caller is one.
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08001535 getActiveAdminForCallerLocked(null,
1536 DeviceAdminInfo.USES_POLICY_FORCE_LOCK);
Dianne Hackborn254cb442010-01-27 19:23:59 -08001537 long ident = Binder.clearCallingIdentity();
1538 try {
1539 mIPowerManager.goToSleepWithReason(SystemClock.uptimeMillis(),
1540 WindowManagerPolicy.OFF_BECAUSE_OF_ADMIN);
1541 } catch (RemoteException e) {
1542 } finally {
1543 Binder.restoreCallingIdentity(ident);
1544 }
Dianne Hackborndf83afa2010-01-20 13:37:26 -08001545 }
1546 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001547
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001548 void wipeDataLocked(int flags) {
Dianne Hackborn42499172010-10-15 18:45:07 -07001549 if ((flags&DevicePolicyManager.WIPE_EXTERNAL_STORAGE) != 0) {
1550 Intent intent = new Intent(ExternalStorageFormatter.FORMAT_AND_FACTORY_RESET);
1551 intent.setComponent(ExternalStorageFormatter.COMPONENT_NAME);
1552 mWakeLock.acquire(10000);
1553 mContext.startService(intent);
1554 } else {
1555 try {
1556 RecoverySystem.rebootWipeUserData(mContext);
1557 } catch (IOException e) {
1558 Slog.w(TAG, "Failed requesting data wipe", e);
1559 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001560 }
1561 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001562
Dianne Hackbornd6847842010-01-12 18:14:19 -08001563 public void wipeData(int flags) {
1564 synchronized (this) {
1565 // This API can only be called by an active device admin,
1566 // so try to retrieve it to check that the caller is one.
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08001567 getActiveAdminForCallerLocked(null,
1568 DeviceAdminInfo.USES_POLICY_WIPE_DATA);
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001569 long ident = Binder.clearCallingIdentity();
1570 try {
1571 wipeDataLocked(flags);
1572 } finally {
1573 Binder.restoreCallingIdentity(ident);
1574 }
Dianne Hackborndf83afa2010-01-20 13:37:26 -08001575 }
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001576 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001577
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001578 public void getRemoveWarning(ComponentName comp, final RemoteCallback result) {
1579 mContext.enforceCallingOrSelfPermission(
1580 android.Manifest.permission.BIND_DEVICE_ADMIN, null);
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001581
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001582 synchronized (this) {
1583 ActiveAdmin admin = getActiveAdminUncheckedLocked(comp);
1584 if (admin == null) {
1585 try {
1586 result.sendResult(null);
1587 } catch (RemoteException e) {
1588 }
1589 return;
1590 }
Dianne Hackbornef6b22f2010-02-16 20:38:49 -08001591 Intent intent = new Intent(DeviceAdminReceiver.ACTION_DEVICE_ADMIN_DISABLE_REQUESTED);
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001592 intent.setComponent(admin.info.getComponent());
1593 mContext.sendOrderedBroadcast(intent, null, new BroadcastReceiver() {
1594 @Override
1595 public void onReceive(Context context, Intent intent) {
1596 try {
1597 result.sendResult(getResultExtras(false));
1598 } catch (RemoteException e) {
1599 }
1600 }
1601 }, null, Activity.RESULT_OK, null, null);
Dianne Hackbornd6847842010-01-12 18:14:19 -08001602 }
1603 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001604
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -07001605 public void setActivePasswordState(int quality, int length, int letters, int uppercase,
Konstantin Lopyrevc8577402010-06-04 17:15:02 -07001606 int lowercase, int numbers, int symbols, int nonletter) {
Dianne Hackbornd6847842010-01-12 18:14:19 -08001607 mContext.enforceCallingOrSelfPermission(
1608 android.Manifest.permission.BIND_DEVICE_ADMIN, null);
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001609
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -07001610 validateQualityConstant(quality);
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001611
Dianne Hackbornd6847842010-01-12 18:14:19 -08001612 synchronized (this) {
Dianne Hackborn9327f4f2010-01-29 10:38:29 -08001613 if (mActivePasswordQuality != quality || mActivePasswordLength != length
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -07001614 || mFailedPasswordAttempts != 0 || mActivePasswordLetters != letters
1615 || mActivePasswordUpperCase != uppercase
1616 || mActivePasswordLowerCase != lowercase || mActivePasswordNumeric != numbers
Konstantin Lopyrevc8577402010-06-04 17:15:02 -07001617 || mActivePasswordSymbols != symbols || mActivePasswordNonLetter != nonletter) {
Dianne Hackbornd6847842010-01-12 18:14:19 -08001618 long ident = Binder.clearCallingIdentity();
1619 try {
Dianne Hackborn9327f4f2010-01-29 10:38:29 -08001620 mActivePasswordQuality = quality;
Dianne Hackbornd6847842010-01-12 18:14:19 -08001621 mActivePasswordLength = length;
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -07001622 mActivePasswordLetters = letters;
1623 mActivePasswordLowerCase = lowercase;
1624 mActivePasswordUpperCase = uppercase;
1625 mActivePasswordNumeric = numbers;
1626 mActivePasswordSymbols = symbols;
Konstantin Lopyrevc8577402010-06-04 17:15:02 -07001627 mActivePasswordNonLetter = nonletter;
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -07001628 mFailedPasswordAttempts = 0;
1629 saveSettingsLocked();
Jim Millera4e28d12010-11-08 16:15:47 -08001630 updatePasswordExpirationsLocked();
Andy Stadler043116a2010-11-29 17:43:32 -08001631 setExpirationAlarmCheckLocked(mContext);
Dianne Hackbornef6b22f2010-02-16 20:38:49 -08001632 sendAdminCommandLocked(DeviceAdminReceiver.ACTION_PASSWORD_CHANGED,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08001633 DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD);
Dianne Hackbornd6847842010-01-12 18:14:19 -08001634 } finally {
1635 Binder.restoreCallingIdentity(ident);
1636 }
1637 }
1638 }
1639 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001640
Andy Stadler043116a2010-11-29 17:43:32 -08001641 /**
1642 * Called any time the device password is updated. Resets all password expiration clocks.
1643 */
Jim Millera4e28d12010-11-08 16:15:47 -08001644 private void updatePasswordExpirationsLocked() {
1645 final int N = mAdminList.size();
1646 if (N > 0) {
1647 for (int i=0; i<N; i++) {
1648 ActiveAdmin admin = mAdminList.get(i);
1649 if (admin.info.usesPolicy(DeviceAdminInfo.USES_POLICY_EXPIRE_PASSWORD)) {
Andy Stadler043116a2010-11-29 17:43:32 -08001650 long timeout = admin.passwordExpirationTimeout;
1651 long expiration = timeout > 0L ? (timeout + System.currentTimeMillis()) : 0L;
1652 admin.passwordExpirationDate = expiration;
Jim Millera4e28d12010-11-08 16:15:47 -08001653 }
1654 }
1655 saveSettingsLocked();
1656 }
1657 }
1658
Dianne Hackbornd6847842010-01-12 18:14:19 -08001659 public void reportFailedPasswordAttempt() {
1660 mContext.enforceCallingOrSelfPermission(
1661 android.Manifest.permission.BIND_DEVICE_ADMIN, null);
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001662
Dianne Hackbornd6847842010-01-12 18:14:19 -08001663 synchronized (this) {
1664 long ident = Binder.clearCallingIdentity();
1665 try {
1666 mFailedPasswordAttempts++;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001667 saveSettingsLocked();
Dianne Hackborn254cb442010-01-27 19:23:59 -08001668 int max = getMaximumFailedPasswordsForWipe(null);
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001669 if (max > 0 && mFailedPasswordAttempts >= max) {
1670 wipeDataLocked(0);
1671 }
Dianne Hackbornef6b22f2010-02-16 20:38:49 -08001672 sendAdminCommandLocked(DeviceAdminReceiver.ACTION_PASSWORD_FAILED,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08001673 DeviceAdminInfo.USES_POLICY_WATCH_LOGIN);
Dianne Hackbornd6847842010-01-12 18:14:19 -08001674 } finally {
1675 Binder.restoreCallingIdentity(ident);
1676 }
1677 }
1678 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001679
Dianne Hackbornd6847842010-01-12 18:14:19 -08001680 public void reportSuccessfulPasswordAttempt() {
1681 mContext.enforceCallingOrSelfPermission(
1682 android.Manifest.permission.BIND_DEVICE_ADMIN, null);
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001683
Dianne Hackbornd6847842010-01-12 18:14:19 -08001684 synchronized (this) {
Dianne Hackborn87bba1e2010-02-26 17:25:54 -08001685 if (mFailedPasswordAttempts != 0 || mPasswordOwner >= 0) {
Dianne Hackbornd6847842010-01-12 18:14:19 -08001686 long ident = Binder.clearCallingIdentity();
1687 try {
1688 mFailedPasswordAttempts = 0;
Dianne Hackborn87bba1e2010-02-26 17:25:54 -08001689 mPasswordOwner = -1;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001690 saveSettingsLocked();
Dianne Hackbornef6b22f2010-02-16 20:38:49 -08001691 sendAdminCommandLocked(DeviceAdminReceiver.ACTION_PASSWORD_SUCCEEDED,
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08001692 DeviceAdminInfo.USES_POLICY_WATCH_LOGIN);
Dianne Hackbornd6847842010-01-12 18:14:19 -08001693 } finally {
1694 Binder.restoreCallingIdentity(ident);
1695 }
1696 }
1697 }
1698 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001699
Oscar Montemayor69238c62010-08-03 10:51:06 -07001700 public ComponentName setGlobalProxy(ComponentName who, String proxySpec,
1701 String exclusionList) {
1702 synchronized(this) {
1703 if (who == null) {
1704 throw new NullPointerException("ComponentName is null");
1705 }
1706
1707 ActiveAdmin admin = getActiveAdminForCallerLocked(who,
1708 DeviceAdminInfo.USES_POLICY_SETS_GLOBAL_PROXY);
1709
1710 // Scan through active admins and find if anyone has already
1711 // set the global proxy.
1712 final int N = mAdminList.size();
1713 Set<ComponentName> compSet = mAdminMap.keySet();
1714 for (ComponentName component : compSet) {
1715 ActiveAdmin ap = mAdminMap.get(component);
1716 if ((ap.specifiesGlobalProxy) && (!component.equals(who))) {
1717 // Another admin already sets the global proxy
1718 // Return it to the caller.
1719 return component;
1720 }
1721 }
1722 if (proxySpec == null) {
1723 admin.specifiesGlobalProxy = false;
1724 admin.globalProxySpec = null;
1725 admin.globalProxyExclusionList = null;
1726 } else {
1727
1728 admin.specifiesGlobalProxy = true;
1729 admin.globalProxySpec = proxySpec;
1730 admin.globalProxyExclusionList = exclusionList;
1731 }
1732
1733 // Reset the global proxy accordingly
1734 // Do this using system permissions, as apps cannot write to secure settings
1735 long origId = Binder.clearCallingIdentity();
1736 resetGlobalProxy();
1737 Binder.restoreCallingIdentity(origId);
1738 return null;
1739 }
1740 }
1741
1742 public ComponentName getGlobalProxyAdmin() {
1743 synchronized(this) {
1744 // Scan through active admins and find if anyone has already
1745 // set the global proxy.
1746 final int N = mAdminList.size();
1747 for (int i = 0; i < N; i++) {
1748 ActiveAdmin ap = mAdminList.get(i);
1749 if (ap.specifiesGlobalProxy) {
1750 // Device admin sets the global proxy
1751 // Return it to the caller.
1752 return ap.info.getComponent();
1753 }
1754 }
1755 }
1756 // No device admin sets the global proxy.
1757 return null;
1758 }
1759
1760 private void resetGlobalProxy() {
1761 final int N = mAdminList.size();
1762 for (int i = 0; i < N; i++) {
1763 ActiveAdmin ap = mAdminList.get(i);
1764 if (ap.specifiesGlobalProxy) {
1765 saveGlobalProxy(ap.globalProxySpec, ap.globalProxyExclusionList);
1766 return;
1767 }
1768 }
1769 // No device admins defining global proxies - reset global proxy settings to none
1770 saveGlobalProxy(null, null);
1771 }
1772
1773 private void saveGlobalProxy(String proxySpec, String exclusionList) {
1774 if (exclusionList == null) {
1775 exclusionList = "";
1776 }
1777 if (proxySpec == null) {
1778 proxySpec = "";
1779 }
1780 // Remove white spaces
1781 proxySpec = proxySpec.trim();
Robert Greenwalt434203a2010-10-11 16:00:27 -07001782 String data[] = proxySpec.split(":");
1783 int proxyPort = 8080;
1784 if (data.length > 1) {
1785 try {
1786 proxyPort = Integer.parseInt(data[1]);
1787 } catch (NumberFormatException e) {}
1788 }
Oscar Montemayor69238c62010-08-03 10:51:06 -07001789 exclusionList = exclusionList.trim();
1790 ContentResolver res = mContext.getContentResolver();
Robert Greenwalt434203a2010-10-11 16:00:27 -07001791 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, data[0]);
1792 Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, proxyPort);
1793 Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
1794 exclusionList);
Oscar Montemayor69238c62010-08-03 10:51:06 -07001795 }
1796
Dianne Hackborn87bba1e2010-02-26 17:25:54 -08001797 @Override
1798 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1799 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1800 != PackageManager.PERMISSION_GRANTED) {
1801
1802 pw.println("Permission Denial: can't dump DevicePolicyManagerService from from pid="
1803 + Binder.getCallingPid()
1804 + ", uid=" + Binder.getCallingUid());
1805 return;
1806 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001807
Dianne Hackborn87bba1e2010-02-26 17:25:54 -08001808 final Printer p = new PrintWriterPrinter(pw);
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001809
Dianne Hackborn87bba1e2010-02-26 17:25:54 -08001810 synchronized (this) {
1811 p.println("Current Device Policy Manager state:");
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001812
Dianne Hackborn87bba1e2010-02-26 17:25:54 -08001813 p.println(" Enabled Device Admins:");
1814 final int N = mAdminList.size();
1815 for (int i=0; i<N; i++) {
1816 ActiveAdmin ap = mAdminList.get(i);
1817 if (ap != null) {
1818 pw.print(" "); pw.print(ap.info.getComponent().flattenToShortString());
1819 pw.println(":");
1820 ap.dump(" ", pw);
1821 }
1822 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001823
Dianne Hackborn87bba1e2010-02-26 17:25:54 -08001824 pw.println(" ");
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -07001825 pw.print(" mActivePasswordQuality=0x");
1826 pw.println(Integer.toHexString(mActivePasswordQuality));
Dianne Hackborn87bba1e2010-02-26 17:25:54 -08001827 pw.print(" mActivePasswordLength="); pw.println(mActivePasswordLength);
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -07001828 pw.print(" mActivePasswordUpperCase="); pw.println(mActivePasswordUpperCase);
1829 pw.print(" mActivePasswordLowerCase="); pw.println(mActivePasswordLowerCase);
1830 pw.print(" mActivePasswordLetters="); pw.println(mActivePasswordLetters);
1831 pw.print(" mActivePasswordNumeric="); pw.println(mActivePasswordNumeric);
1832 pw.print(" mActivePasswordSymbols="); pw.println(mActivePasswordSymbols);
Konstantin Lopyrevc8577402010-06-04 17:15:02 -07001833 pw.print(" mActivePasswordNonLetter="); pw.println(mActivePasswordNonLetter);
Dianne Hackborn87bba1e2010-02-26 17:25:54 -08001834 pw.print(" mFailedPasswordAttempts="); pw.println(mFailedPasswordAttempts);
1835 pw.print(" mPasswordOwner="); pw.println(mPasswordOwner);
1836 }
1837 }
Dianne Hackbornd6847842010-01-12 18:14:19 -08001838}