Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 19 | import com.android.internal.content.PackageMonitor; |
Dianne Hackborn | 4249917 | 2010-10-15 18:45:07 -0700 | [diff] [blame] | 20 | import com.android.internal.os.storage.ExternalStorageFormatter; |
Dianne Hackborn | 2269d157 | 2010-02-24 19:54:22 -0800 | [diff] [blame] | 21 | import com.android.internal.util.FastXmlSerializer; |
Dianne Hackborn | 1afd1c9 | 2010-03-18 22:47:17 -0700 | [diff] [blame] | 22 | import com.android.internal.util.JournaledFile; |
Dianne Hackborn | 2269d157 | 2010-02-24 19:54:22 -0800 | [diff] [blame] | 23 | import com.android.internal.util.XmlUtils; |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 24 | import com.android.internal.widget.LockPatternUtils; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 25 | |
| 26 | import org.xmlpull.v1.XmlPullParser; |
| 27 | import org.xmlpull.v1.XmlPullParserException; |
| 28 | import org.xmlpull.v1.XmlSerializer; |
| 29 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 30 | import android.app.Activity; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 31 | import android.app.AlarmManager; |
| 32 | import android.app.PendingIntent; |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 33 | import android.app.admin.DeviceAdminInfo; |
| 34 | import android.app.admin.DeviceAdminReceiver; |
| 35 | import android.app.admin.DevicePolicyManager; |
| 36 | import android.app.admin.IDevicePolicyManager; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 37 | import android.content.BroadcastReceiver; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 38 | import android.content.ComponentName; |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 39 | import android.content.ContentResolver; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 40 | import android.content.Context; |
| 41 | import android.content.Intent; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 42 | import android.content.IntentFilter; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 43 | import android.content.pm.PackageManager; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 44 | import android.content.pm.PackageManager.NameNotFoundException; |
Andy Stadler | 1f35d48 | 2010-11-19 15:39:41 -0800 | [diff] [blame^] | 45 | import android.content.pm.ResolveInfo; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 46 | import android.os.Binder; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 47 | import android.os.Handler; |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 48 | import android.os.IBinder; |
| 49 | import android.os.IPowerManager; |
Dianne Hackborn | 4249917 | 2010-10-15 18:45:07 -0700 | [diff] [blame] | 50 | import android.os.PowerManager; |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 51 | import android.os.RecoverySystem; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 52 | import android.os.RemoteCallback; |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 53 | import android.os.RemoteException; |
| 54 | import android.os.ServiceManager; |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 55 | import android.os.SystemClock; |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 56 | import android.provider.Settings; |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 57 | import android.util.PrintWriterPrinter; |
| 58 | import android.util.Printer; |
Andy Stadler | 1f35d48 | 2010-11-19 15:39:41 -0800 | [diff] [blame^] | 59 | import android.util.Slog; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 60 | import android.util.Xml; |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 61 | import android.view.WindowManagerPolicy; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 62 | |
| 63 | import java.io.File; |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 64 | import java.io.FileDescriptor; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 65 | import java.io.FileInputStream; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 66 | import java.io.FileNotFoundException; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 67 | import java.io.FileOutputStream; |
| 68 | import java.io.IOException; |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 69 | import java.io.PrintWriter; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 70 | import java.text.DateFormat; |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 71 | import java.util.ArrayList; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 72 | import java.util.Date; |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 73 | import java.util.HashMap; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 74 | import java.util.List; |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 75 | import java.util.Set; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 76 | |
| 77 | /** |
| 78 | * Implementation of the device policy APIs. |
| 79 | */ |
| 80 | public class DevicePolicyManagerService extends IDevicePolicyManager.Stub { |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 81 | private static final int REQUEST_EXPIRE_PASSWORD = 5571; |
| 82 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 83 | static final String TAG = "DevicePolicyManagerService"; |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 84 | |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 85 | 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 Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 91 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 92 | final Context mContext; |
| 93 | final MyPackageMonitor mMonitor; |
Dianne Hackborn | 4249917 | 2010-10-15 18:45:07 -0700 | [diff] [blame] | 94 | final PowerManager.WakeLock mWakeLock; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 95 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 96 | IPowerManager mIPowerManager; |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 97 | |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 98 | int mActivePasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 99 | int mActivePasswordLength = 0; |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 100 | int mActivePasswordUpperCase = 0; |
| 101 | int mActivePasswordLowerCase = 0; |
| 102 | int mActivePasswordLetters = 0; |
| 103 | int mActivePasswordNumeric = 0; |
| 104 | int mActivePasswordSymbols = 0; |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 105 | int mActivePasswordNonLetter = 0; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 106 | int mFailedPasswordAttempts = 0; |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 107 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 108 | int mPasswordOwner = -1; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 109 | Handler mHandler = new Handler(); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 110 | |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 111 | final HashMap<ComponentName, ActiveAdmin> mAdminMap |
| 112 | = new HashMap<ComponentName, ActiveAdmin>(); |
| 113 | final ArrayList<ActiveAdmin> mAdminList |
| 114 | = new ArrayList<ActiveAdmin>(); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 115 | |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 116 | 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 Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 132 | static class ActiveAdmin { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 133 | final DeviceAdminInfo info; |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 134 | |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 135 | int passwordQuality = DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 136 | int minimumPasswordLength = 0; |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 137 | int passwordHistoryLength = 0; |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 138 | int minimumPasswordUpperCase = 0; |
| 139 | int minimumPasswordLowerCase = 0; |
| 140 | int minimumPasswordLetters = 1; |
| 141 | int minimumPasswordNumeric = 1; |
| 142 | int minimumPasswordSymbols = 1; |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 143 | int minimumPasswordNonLetter = 0; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 144 | long maximumTimeToUnlock = 0; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 145 | int maximumFailedPasswordsForWipe = 0; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 146 | long passwordExpirationTimeout = 0L; |
| 147 | long passwordExpirationDate = 0L; |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 148 | |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 149 | // TODO: review implementation decisions with frameworks team |
| 150 | boolean specifiesGlobalProxy = false; |
| 151 | String globalProxySpec = null; |
| 152 | String globalProxyExclusionList = null; |
| 153 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 154 | ActiveAdmin(DeviceAdminInfo _info) { |
| 155 | info = _info; |
| 156 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 157 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 158 | int getUid() { return info.getActivityInfo().applicationInfo.uid; } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 159 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 160 | void writeToXml(XmlSerializer out) |
| 161 | throws IllegalArgumentException, IllegalStateException, IOException { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 162 | out.startTag(null, "policies"); |
| 163 | info.writePoliciesToXml(out); |
| 164 | out.endTag(null, "policies"); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 165 | 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 Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 169 | if (minimumPasswordLength > 0) { |
| 170 | out.startTag(null, "min-password-length"); |
| 171 | out.attribute(null, "value", Integer.toString(minimumPasswordLength)); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 172 | 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 Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 178 | } |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 179 | 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 Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 204 | 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 Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 209 | } |
Dianne Hackborn | ab36acb | 2010-11-05 14:12:11 -0700 | [diff] [blame] | 210 | if (maximumTimeToUnlock != 0) { |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 211 | 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 Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 220 | 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 Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 235 | 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 Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 245 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 246 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 247 | 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 Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 257 | if ("policies".equals(tag)) { |
| 258 | info.readPoliciesFromXml(parser); |
| 259 | } else if ("password-quality".equals(tag)) { |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 260 | passwordQuality = Integer.parseInt( |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 261 | parser.getAttributeValue(null, "value")); |
| 262 | } else if ("min-password-length".equals(tag)) { |
| 263 | minimumPasswordLength = Integer.parseInt( |
| 264 | parser.getAttributeValue(null, "value")); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 265 | } else if ("password-history-length".equals(tag)) { |
| 266 | passwordHistoryLength = Integer.parseInt( |
| 267 | parser.getAttributeValue(null, "value")); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 268 | } 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 Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 283 | } else if ("min-password-nonletter".equals(tag)) { |
| 284 | minimumPasswordNonLetter = Integer.parseInt( |
| 285 | parser.getAttributeValue(null, "value")); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 286 | } 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 Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 292 | } 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 Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 301 | } 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 Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 307 | } else { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 308 | Slog.w(TAG, "Unknown admin tag: " + tag); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 309 | } |
| 310 | XmlUtils.skipCurrentTag(parser); |
| 311 | } |
| 312 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 313 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 314 | 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 Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 323 | pw.print(prefix); pw.print("passwordQuality=0x"); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 324 | pw.println(Integer.toHexString(passwordQuality)); |
| 325 | pw.print(prefix); pw.print("minimumPasswordLength="); |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 326 | pw.println(minimumPasswordLength); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 327 | pw.print(prefix); pw.print("passwordHistoryLength="); |
| 328 | pw.println(passwordHistoryLength); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 329 | 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 Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 339 | pw.print(prefix); pw.print("minimumPasswordNonLetter="); |
| 340 | pw.println(minimumPasswordNonLetter); |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 341 | pw.print(prefix); pw.print("maximumTimeToUnlock="); |
| 342 | pw.println(maximumTimeToUnlock); |
| 343 | pw.print(prefix); pw.print("maximumFailedPasswordsForWipe="); |
| 344 | pw.println(maximumFailedPasswordsForWipe); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 345 | pw.print(prefix); pw.print("specifiesGlobalProxy="); |
| 346 | pw.println(specifiesGlobalProxy); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 347 | pw.print(prefix); pw.print("passwordExpirationTimeout="); |
| 348 | pw.println(passwordExpirationTimeout); |
| 349 | pw.print(prefix); pw.print("passwordExpirationDate="); |
| 350 | pw.println(passwordExpirationDate); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 351 | 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 Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 359 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 360 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 361 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 362 | class MyPackageMonitor extends PackageMonitor { |
Andy Stadler | 1f35d48 | 2010-11-19 15:39:41 -0800 | [diff] [blame^] | 363 | @Override |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 364 | public void onSomePackagesChanged() { |
| 365 | synchronized (DevicePolicyManagerService.this) { |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 366 | boolean removed = false; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 367 | for (int i=mAdminList.size()-1; i>=0; i--) { |
| 368 | ActiveAdmin aa = mAdminList.get(i); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 369 | int change = isPackageDisappearing(aa.info.getPackageName()); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 370 | if (change == PACKAGE_PERMANENT_CHANGE |
| 371 | || change == PACKAGE_TEMPORARY_CHANGE) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 372 | Slog.w(TAG, "Admin unexpectedly uninstalled: " |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 373 | + aa.info.getComponent()); |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 374 | removed = true; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 375 | 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 Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 381 | Slog.w(TAG, "Admin package change removed component: " |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 382 | + aa.info.getComponent()); |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 383 | removed = true; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 384 | mAdminList.remove(i); |
| 385 | } |
| 386 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 387 | } |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 388 | if (removed) { |
| 389 | validatePasswordOwnerLocked(); |
| 390 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 391 | } |
| 392 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 393 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 394 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 395 | /** |
| 396 | * Instantiates the service. |
| 397 | */ |
| 398 | public DevicePolicyManagerService(Context context) { |
| 399 | mContext = context; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 400 | mMonitor = new MyPackageMonitor(); |
| 401 | mMonitor.register(context, true); |
Dianne Hackborn | 4249917 | 2010-10-15 18:45:07 -0700 | [diff] [blame] | 402 | mWakeLock = ((PowerManager)context.getSystemService(Context.POWER_SERVICE)) |
| 403 | .newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "DPM"); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 404 | 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 | |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 410 | protected void setExpirationAlarmCheckLocked(Context context) { |
| 411 | final long expiration = getPasswordExpirationLocked(null); |
| 412 | final long now = System.currentTimeMillis(); |
| 413 | final long timeToExpire = expiration - now; |
| 414 | final long alarmTime; |
| 415 | if (timeToExpire > 0L && timeToExpire < MS_PER_DAY) { |
| 416 | // Next expiration is less than a day, set alarm for exact expiration time |
| 417 | alarmTime = now + timeToExpire; |
| 418 | } else { |
| 419 | // Check again in 24 hours... |
| 420 | alarmTime = now + MS_PER_DAY; |
| 421 | } |
| 422 | |
Andy Stadler | 1f35d48 | 2010-11-19 15:39:41 -0800 | [diff] [blame^] | 423 | long token = Binder.clearCallingIdentity(); |
| 424 | try { |
| 425 | AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); |
| 426 | PendingIntent pi = PendingIntent.getBroadcast(context, REQUEST_EXPIRE_PASSWORD, |
| 427 | new Intent(ACTION_EXPIRED_PASSWORD_NOTIFICATION), |
| 428 | PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT); |
| 429 | am.cancel(pi); |
| 430 | am.set(AlarmManager.RTC, alarmTime, pi); |
| 431 | } finally { |
| 432 | Binder.restoreCallingIdentity(token); |
| 433 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 434 | } |
| 435 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 436 | private IPowerManager getIPowerManager() { |
| 437 | if (mIPowerManager == null) { |
| 438 | IBinder b = ServiceManager.getService(Context.POWER_SERVICE); |
| 439 | mIPowerManager = IPowerManager.Stub.asInterface(b); |
| 440 | } |
| 441 | return mIPowerManager; |
| 442 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 443 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 444 | ActiveAdmin getActiveAdminUncheckedLocked(ComponentName who) { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 445 | ActiveAdmin admin = mAdminMap.get(who); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 446 | if (admin != null |
| 447 | && who.getPackageName().equals(admin.info.getActivityInfo().packageName) |
| 448 | && who.getClassName().equals(admin.info.getActivityInfo().name)) { |
| 449 | return admin; |
| 450 | } |
| 451 | return null; |
| 452 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 453 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 454 | ActiveAdmin getActiveAdminForCallerLocked(ComponentName who, int reqPolicy) |
| 455 | throws SecurityException { |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 456 | final int callingUid = Binder.getCallingUid(); |
| 457 | if (who != null) { |
| 458 | ActiveAdmin admin = mAdminMap.get(who); |
| 459 | if (admin == null) { |
| 460 | throw new SecurityException("No active admin " + who); |
| 461 | } |
| 462 | if (admin.getUid() != callingUid) { |
| 463 | throw new SecurityException("Admin " + who + " is not owned by uid " |
| 464 | + Binder.getCallingUid()); |
| 465 | } |
| 466 | if (!admin.info.usesPolicy(reqPolicy)) { |
| 467 | throw new SecurityException("Admin " + admin.info.getComponent() |
| 468 | + " did not specify uses-policy for: " |
| 469 | + admin.info.getTagForPolicy(reqPolicy)); |
| 470 | } |
| 471 | return admin; |
| 472 | } else { |
| 473 | final int N = mAdminList.size(); |
| 474 | for (int i=0; i<N; i++) { |
| 475 | ActiveAdmin admin = mAdminList.get(i); |
| 476 | if (admin.getUid() == callingUid && admin.info.usesPolicy(reqPolicy)) { |
| 477 | return admin; |
| 478 | } |
| 479 | } |
| 480 | throw new SecurityException("No active admin owned by uid " |
| 481 | + Binder.getCallingUid() + " for policy #" + reqPolicy); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 482 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 483 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 484 | |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 485 | void sendAdminCommandLocked(ActiveAdmin admin, String action) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 486 | Intent intent = new Intent(action); |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 487 | intent.setComponent(admin.info.getComponent()); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 488 | if (action.equals(DeviceAdminReceiver.ACTION_PASSWORD_EXPIRING)) { |
| 489 | intent.putExtra("expiration", admin.passwordExpirationDate); |
| 490 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 491 | mContext.sendBroadcast(intent); |
| 492 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 493 | |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 494 | void sendAdminCommandLocked(String action, int reqPolicy) { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 495 | final int N = mAdminList.size(); |
| 496 | if (N > 0) { |
| 497 | for (int i=0; i<N; i++) { |
| 498 | ActiveAdmin admin = mAdminList.get(i); |
| 499 | if (admin.info.usesPolicy(reqPolicy)) { |
| 500 | sendAdminCommandLocked(admin, action); |
| 501 | } |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 502 | } |
Dianne Hackborn | 4141d03 | 2010-01-21 16:29:00 -0800 | [diff] [blame] | 503 | } |
| 504 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 505 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 506 | void removeActiveAdminLocked(ComponentName adminReceiver) { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 507 | ActiveAdmin admin = getActiveAdminUncheckedLocked(adminReceiver); |
| 508 | if (admin != null) { |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 509 | boolean doProxyCleanup = |
| 510 | admin.info.usesPolicy(DeviceAdminInfo.USES_POLICY_SETS_GLOBAL_PROXY); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 511 | sendAdminCommandLocked(admin, |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 512 | DeviceAdminReceiver.ACTION_DEVICE_ADMIN_DISABLED); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 513 | // XXX need to wait for it to complete. |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 514 | mAdminList.remove(admin); |
| 515 | mAdminMap.remove(adminReceiver); |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 516 | validatePasswordOwnerLocked(); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 517 | if (doProxyCleanup) { |
| 518 | resetGlobalProxy(); |
| 519 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 520 | } |
| 521 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 522 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 523 | public DeviceAdminInfo findAdmin(ComponentName adminName) { |
| 524 | Intent resolveIntent = new Intent(); |
| 525 | resolveIntent.setComponent(adminName); |
| 526 | List<ResolveInfo> infos = mContext.getPackageManager().queryBroadcastReceivers( |
| 527 | resolveIntent, PackageManager.GET_META_DATA); |
| 528 | if (infos == null || infos.size() <= 0) { |
| 529 | throw new IllegalArgumentException("Unknown admin: " + adminName); |
| 530 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 531 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 532 | try { |
| 533 | return new DeviceAdminInfo(mContext, infos.get(0)); |
| 534 | } catch (XmlPullParserException e) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 535 | Slog.w(TAG, "Bad device admin requested: " + adminName, e); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 536 | return null; |
| 537 | } catch (IOException e) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 538 | Slog.w(TAG, "Bad device admin requested: " + adminName, e); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 539 | return null; |
| 540 | } |
| 541 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 542 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 543 | private static JournaledFile makeJournaledFile() { |
| 544 | final String base = "/data/system/device_policies.xml"; |
| 545 | return new JournaledFile(new File(base), new File(base + ".tmp")); |
| 546 | } |
| 547 | |
| 548 | private void saveSettingsLocked() { |
| 549 | JournaledFile journal = makeJournaledFile(); |
| 550 | FileOutputStream stream = null; |
| 551 | try { |
| 552 | stream = new FileOutputStream(journal.chooseForWrite(), false); |
| 553 | XmlSerializer out = new FastXmlSerializer(); |
| 554 | out.setOutput(stream, "utf-8"); |
| 555 | out.startDocument(null, true); |
| 556 | |
| 557 | out.startTag(null, "policies"); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 558 | |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 559 | final int N = mAdminList.size(); |
| 560 | for (int i=0; i<N; i++) { |
| 561 | ActiveAdmin ap = mAdminList.get(i); |
| 562 | if (ap != null) { |
| 563 | out.startTag(null, "admin"); |
| 564 | out.attribute(null, "name", ap.info.getComponent().flattenToString()); |
| 565 | ap.writeToXml(out); |
| 566 | out.endTag(null, "admin"); |
| 567 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 568 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 569 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 570 | if (mPasswordOwner >= 0) { |
| 571 | out.startTag(null, "password-owner"); |
| 572 | out.attribute(null, "value", Integer.toString(mPasswordOwner)); |
| 573 | out.endTag(null, "password-owner"); |
| 574 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 575 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 576 | if (mFailedPasswordAttempts != 0) { |
| 577 | out.startTag(null, "failed-password-attempts"); |
| 578 | out.attribute(null, "value", Integer.toString(mFailedPasswordAttempts)); |
| 579 | out.endTag(null, "failed-password-attempts"); |
| 580 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 581 | |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 582 | if (mActivePasswordQuality != 0 || mActivePasswordLength != 0 |
| 583 | || mActivePasswordUpperCase != 0 || mActivePasswordLowerCase != 0 |
| 584 | || mActivePasswordLetters != 0 || mActivePasswordNumeric != 0 |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 585 | || mActivePasswordSymbols != 0 || mActivePasswordNonLetter != 0) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 586 | out.startTag(null, "active-password"); |
| 587 | out.attribute(null, "quality", Integer.toString(mActivePasswordQuality)); |
| 588 | out.attribute(null, "length", Integer.toString(mActivePasswordLength)); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 589 | out.attribute(null, "uppercase", Integer.toString(mActivePasswordUpperCase)); |
| 590 | out.attribute(null, "lowercase", Integer.toString(mActivePasswordLowerCase)); |
| 591 | out.attribute(null, "letters", Integer.toString(mActivePasswordLetters)); |
| 592 | out.attribute(null, "numeric", Integer |
| 593 | .toString(mActivePasswordNumeric)); |
| 594 | out.attribute(null, "symbols", Integer.toString(mActivePasswordSymbols)); |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 595 | out.attribute(null, "nonletter", Integer.toString(mActivePasswordNonLetter)); |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 596 | out.endTag(null, "active-password"); |
| 597 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 598 | |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 599 | out.endTag(null, "policies"); |
| 600 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 601 | out.endDocument(); |
| 602 | stream.close(); |
| 603 | journal.commit(); |
Jim Miller | 284b62e | 2010-06-08 14:27:42 -0700 | [diff] [blame] | 604 | sendChangedNotification(); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 605 | } catch (IOException e) { |
| 606 | try { |
| 607 | if (stream != null) { |
| 608 | stream.close(); |
| 609 | } |
| 610 | } catch (IOException ex) { |
| 611 | // Ignore |
| 612 | } |
| 613 | journal.rollback(); |
| 614 | } |
| 615 | } |
| 616 | |
Jim Miller | 284b62e | 2010-06-08 14:27:42 -0700 | [diff] [blame] | 617 | private void sendChangedNotification() { |
| 618 | Intent intent = new Intent(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED); |
| 619 | intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 620 | mContext.sendBroadcast(intent); |
| 621 | } |
| 622 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 623 | private void loadSettingsLocked() { |
| 624 | JournaledFile journal = makeJournaledFile(); |
| 625 | FileInputStream stream = null; |
| 626 | File file = journal.chooseForRead(); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 627 | try { |
| 628 | stream = new FileInputStream(file); |
| 629 | XmlPullParser parser = Xml.newPullParser(); |
| 630 | parser.setInput(stream, null); |
| 631 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 632 | int type; |
| 633 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 634 | && type != XmlPullParser.START_TAG) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 635 | } |
| 636 | String tag = parser.getName(); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 637 | if (!"policies".equals(tag)) { |
| 638 | throw new XmlPullParserException( |
| 639 | "Settings do not start with policies tag: found " + tag); |
| 640 | } |
| 641 | type = parser.next(); |
| 642 | int outerDepth = parser.getDepth(); |
| 643 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 644 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 645 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 646 | continue; |
| 647 | } |
| 648 | tag = parser.getName(); |
| 649 | if ("admin".equals(tag)) { |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 650 | String name = parser.getAttributeValue(null, "name"); |
| 651 | try { |
| 652 | DeviceAdminInfo dai = findAdmin( |
| 653 | ComponentName.unflattenFromString(name)); |
| 654 | if (dai != null) { |
| 655 | ActiveAdmin ap = new ActiveAdmin(dai); |
| 656 | ap.readFromXml(parser); |
| 657 | mAdminMap.put(ap.info.getComponent(), ap); |
| 658 | mAdminList.add(ap); |
| 659 | } |
| 660 | } catch (RuntimeException e) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 661 | Slog.w(TAG, "Failed loading admin " + name, e); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 662 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 663 | } else if ("failed-password-attempts".equals(tag)) { |
| 664 | mFailedPasswordAttempts = Integer.parseInt( |
| 665 | parser.getAttributeValue(null, "value")); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 666 | XmlUtils.skipCurrentTag(parser); |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 667 | } else if ("password-owner".equals(tag)) { |
| 668 | mPasswordOwner = Integer.parseInt( |
| 669 | parser.getAttributeValue(null, "value")); |
| 670 | XmlUtils.skipCurrentTag(parser); |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 671 | } else if ("active-password".equals(tag)) { |
| 672 | mActivePasswordQuality = Integer.parseInt( |
| 673 | parser.getAttributeValue(null, "quality")); |
| 674 | mActivePasswordLength = Integer.parseInt( |
| 675 | parser.getAttributeValue(null, "length")); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 676 | mActivePasswordUpperCase = Integer.parseInt( |
| 677 | parser.getAttributeValue(null, "uppercase")); |
| 678 | mActivePasswordLowerCase = Integer.parseInt( |
| 679 | parser.getAttributeValue(null, "lowercase")); |
| 680 | mActivePasswordLetters = Integer.parseInt( |
| 681 | parser.getAttributeValue(null, "letters")); |
| 682 | mActivePasswordNumeric = Integer.parseInt( |
| 683 | parser.getAttributeValue(null, "numeric")); |
| 684 | mActivePasswordSymbols = Integer.parseInt( |
| 685 | parser.getAttributeValue(null, "symbols")); |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 686 | mActivePasswordNonLetter = Integer.parseInt( |
| 687 | parser.getAttributeValue(null, "nonletter")); |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 688 | XmlUtils.skipCurrentTag(parser); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 689 | } else { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 690 | Slog.w(TAG, "Unknown tag: " + tag); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 691 | XmlUtils.skipCurrentTag(parser); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 692 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 693 | } |
| 694 | } catch (NullPointerException e) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 695 | Slog.w(TAG, "failed parsing " + file + " " + e); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 696 | } catch (NumberFormatException e) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 697 | Slog.w(TAG, "failed parsing " + file + " " + e); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 698 | } catch (XmlPullParserException e) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 699 | Slog.w(TAG, "failed parsing " + file + " " + e); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 700 | } catch (FileNotFoundException e) { |
| 701 | // Don't be noisy, this is normal if we haven't defined any policies. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 702 | } catch (IOException e) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 703 | Slog.w(TAG, "failed parsing " + file + " " + e); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 704 | } catch (IndexOutOfBoundsException e) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 705 | Slog.w(TAG, "failed parsing " + file + " " + e); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 706 | } |
| 707 | try { |
| 708 | if (stream != null) { |
| 709 | stream.close(); |
| 710 | } |
| 711 | } catch (IOException e) { |
| 712 | // Ignore |
| 713 | } |
| 714 | |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 715 | // Validate that what we stored for the password quality matches |
| 716 | // sufficiently what is currently set. Note that this is only |
| 717 | // a sanity check in case the two get out of sync; this should |
| 718 | // never normally happen. |
| 719 | LockPatternUtils utils = new LockPatternUtils(mContext); |
| 720 | if (utils.getActivePasswordQuality() < mActivePasswordQuality) { |
| 721 | Slog.w(TAG, "Active password quality 0x" |
| 722 | + Integer.toHexString(mActivePasswordQuality) |
| 723 | + " does not match actual quality 0x" |
| 724 | + Integer.toHexString(utils.getActivePasswordQuality())); |
| 725 | mActivePasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; |
| 726 | mActivePasswordLength = 0; |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 727 | mActivePasswordUpperCase = 0; |
| 728 | mActivePasswordLowerCase = 0; |
| 729 | mActivePasswordLetters = 0; |
| 730 | mActivePasswordNumeric = 0; |
| 731 | mActivePasswordSymbols = 0; |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 732 | mActivePasswordNonLetter = 0; |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 733 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 734 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 735 | validatePasswordOwnerLocked(); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 736 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 737 | long timeMs = getMaximumTimeToLock(null); |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 738 | if (timeMs <= 0) { |
| 739 | timeMs = Integer.MAX_VALUE; |
| 740 | } |
| 741 | try { |
| 742 | getIPowerManager().setMaximumScreenOffTimeount((int)timeMs); |
| 743 | } catch (RemoteException e) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 744 | Slog.w(TAG, "Failure talking with power manager", e); |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 745 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 746 | } |
| 747 | |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 748 | static void validateQualityConstant(int quality) { |
| 749 | switch (quality) { |
| 750 | case DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED: |
| 751 | case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING: |
| 752 | case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC: |
| 753 | case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC: |
| 754 | case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC: |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 755 | case DevicePolicyManager.PASSWORD_QUALITY_COMPLEX: |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 756 | return; |
| 757 | } |
| 758 | throw new IllegalArgumentException("Invalid quality constant: 0x" |
| 759 | + Integer.toHexString(quality)); |
| 760 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 761 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 762 | void validatePasswordOwnerLocked() { |
| 763 | if (mPasswordOwner >= 0) { |
| 764 | boolean haveOwner = false; |
| 765 | for (int i=mAdminList.size()-1; i>=0; i--) { |
| 766 | if (mAdminList.get(i).getUid() == mPasswordOwner) { |
| 767 | haveOwner = true; |
| 768 | break; |
| 769 | } |
| 770 | } |
| 771 | if (!haveOwner) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 772 | Slog.w(TAG, "Previous password owner " + mPasswordOwner |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 773 | + " no longer active; disabling"); |
| 774 | mPasswordOwner = -1; |
| 775 | } |
| 776 | } |
| 777 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 778 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 779 | public void systemReady() { |
| 780 | synchronized (this) { |
| 781 | loadSettingsLocked(); |
| 782 | } |
| 783 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 784 | |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 785 | private void handlePasswordExpirationNotification() { |
| 786 | synchronized (this) { |
| 787 | final long now = System.currentTimeMillis(); |
| 788 | final int N = mAdminList.size(); |
| 789 | if (N <= 0) { |
| 790 | return; |
| 791 | } |
| 792 | for (int i=0; i < N; i++) { |
| 793 | ActiveAdmin admin = mAdminList.get(i); |
| 794 | if (admin.info.usesPolicy(DeviceAdminInfo.USES_POLICY_EXPIRE_PASSWORD) |
| 795 | && admin.passwordExpirationTimeout > 0L |
| 796 | && admin.passwordExpirationDate > 0L |
| 797 | && now > admin.passwordExpirationDate - EXPIRATION_GRACE_PERIOD_MS) { |
| 798 | sendAdminCommandLocked(admin, DeviceAdminReceiver.ACTION_PASSWORD_EXPIRING); |
| 799 | } |
| 800 | } |
| 801 | setExpirationAlarmCheckLocked(mContext); |
| 802 | } |
| 803 | } |
| 804 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 805 | public void setActiveAdmin(ComponentName adminReceiver) { |
| 806 | mContext.enforceCallingOrSelfPermission( |
| 807 | android.Manifest.permission.BIND_DEVICE_ADMIN, null); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 808 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 809 | DeviceAdminInfo info = findAdmin(adminReceiver); |
| 810 | if (info == null) { |
| 811 | throw new IllegalArgumentException("Bad admin: " + adminReceiver); |
| 812 | } |
| 813 | synchronized (this) { |
| 814 | long ident = Binder.clearCallingIdentity(); |
| 815 | try { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 816 | if (getActiveAdminUncheckedLocked(adminReceiver) != null) { |
| 817 | throw new IllegalArgumentException("Admin is already added"); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 818 | } |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 819 | ActiveAdmin admin = new ActiveAdmin(info); |
| 820 | mAdminMap.put(adminReceiver, admin); |
| 821 | mAdminList.add(admin); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 822 | saveSettingsLocked(); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 823 | sendAdminCommandLocked(admin, |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 824 | DeviceAdminReceiver.ACTION_DEVICE_ADMIN_ENABLED); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 825 | } finally { |
| 826 | Binder.restoreCallingIdentity(ident); |
| 827 | } |
| 828 | } |
| 829 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 830 | |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 831 | public boolean isAdminActive(ComponentName adminReceiver) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 832 | synchronized (this) { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 833 | return getActiveAdminUncheckedLocked(adminReceiver) != null; |
| 834 | } |
| 835 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 836 | |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 837 | public List<ComponentName> getActiveAdmins() { |
| 838 | synchronized (this) { |
| 839 | final int N = mAdminList.size(); |
| 840 | if (N <= 0) { |
| 841 | return null; |
| 842 | } |
| 843 | ArrayList<ComponentName> res = new ArrayList<ComponentName>(N); |
| 844 | for (int i=0; i<N; i++) { |
| 845 | res.add(mAdminList.get(i).info.getComponent()); |
| 846 | } |
| 847 | return res; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 848 | } |
| 849 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 850 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 851 | public boolean packageHasActiveAdmins(String packageName) { |
| 852 | synchronized (this) { |
| 853 | final int N = mAdminList.size(); |
| 854 | for (int i=0; i<N; i++) { |
| 855 | if (mAdminList.get(i).info.getPackageName().equals(packageName)) { |
| 856 | return true; |
| 857 | } |
| 858 | } |
| 859 | return false; |
| 860 | } |
| 861 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 862 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 863 | public void removeActiveAdmin(ComponentName adminReceiver) { |
| 864 | synchronized (this) { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 865 | ActiveAdmin admin = getActiveAdminUncheckedLocked(adminReceiver); |
| 866 | if (admin == null) { |
| 867 | return; |
| 868 | } |
| 869 | if (admin.getUid() != Binder.getCallingUid()) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 870 | mContext.enforceCallingOrSelfPermission( |
| 871 | android.Manifest.permission.BIND_DEVICE_ADMIN, null); |
| 872 | } |
| 873 | long ident = Binder.clearCallingIdentity(); |
| 874 | try { |
| 875 | removeActiveAdminLocked(adminReceiver); |
| 876 | } finally { |
| 877 | Binder.restoreCallingIdentity(ident); |
| 878 | } |
| 879 | } |
| 880 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 881 | |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 882 | public void setPasswordQuality(ComponentName who, int quality) { |
| 883 | validateQualityConstant(quality); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 884 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 885 | synchronized (this) { |
| 886 | if (who == null) { |
| 887 | throw new NullPointerException("ComponentName is null"); |
| 888 | } |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 889 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 890 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 891 | if (ap.passwordQuality != quality) { |
| 892 | ap.passwordQuality = quality; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 893 | saveSettingsLocked(); |
| 894 | } |
| 895 | } |
| 896 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 897 | |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 898 | public int getPasswordQuality(ComponentName who) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 899 | synchronized (this) { |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 900 | int mode = DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 901 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 902 | if (who != null) { |
| 903 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 904 | return admin != null ? admin.passwordQuality : mode; |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 905 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 906 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 907 | final int N = mAdminList.size(); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 908 | for (int i=0; i<N; i++) { |
| 909 | ActiveAdmin admin = mAdminList.get(i); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 910 | if (mode < admin.passwordQuality) { |
| 911 | mode = admin.passwordQuality; |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 912 | } |
| 913 | } |
| 914 | return mode; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 915 | } |
| 916 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 917 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 918 | public void setPasswordMinimumLength(ComponentName who, int length) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 919 | synchronized (this) { |
| 920 | if (who == null) { |
| 921 | throw new NullPointerException("ComponentName is null"); |
| 922 | } |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 923 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 924 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 925 | if (ap.minimumPasswordLength != length) { |
| 926 | ap.minimumPasswordLength = length; |
| 927 | saveSettingsLocked(); |
| 928 | } |
| 929 | } |
| 930 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 931 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 932 | public int getPasswordMinimumLength(ComponentName who) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 933 | synchronized (this) { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 934 | int length = 0; |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 935 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 936 | if (who != null) { |
| 937 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who); |
| 938 | return admin != null ? admin.minimumPasswordLength : length; |
| 939 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 940 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 941 | final int N = mAdminList.size(); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 942 | for (int i=0; i<N; i++) { |
| 943 | ActiveAdmin admin = mAdminList.get(i); |
| 944 | if (length < admin.minimumPasswordLength) { |
| 945 | length = admin.minimumPasswordLength; |
| 946 | } |
| 947 | } |
| 948 | return length; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 949 | } |
| 950 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 951 | |
| 952 | public void setPasswordHistoryLength(ComponentName who, int length) { |
| 953 | synchronized (this) { |
| 954 | if (who == null) { |
| 955 | throw new NullPointerException("ComponentName is null"); |
| 956 | } |
| 957 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 958 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
| 959 | if (ap.passwordHistoryLength != length) { |
| 960 | ap.passwordHistoryLength = length; |
| 961 | saveSettingsLocked(); |
| 962 | } |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | public int getPasswordHistoryLength(ComponentName who) { |
| 967 | synchronized (this) { |
| 968 | int length = 0; |
| 969 | |
| 970 | if (who != null) { |
| 971 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who); |
| 972 | return admin != null ? admin.passwordHistoryLength : length; |
| 973 | } |
| 974 | |
| 975 | final int N = mAdminList.size(); |
| 976 | for (int i = 0; i < N; i++) { |
| 977 | ActiveAdmin admin = mAdminList.get(i); |
| 978 | if (length < admin.passwordHistoryLength) { |
| 979 | length = admin.passwordHistoryLength; |
| 980 | } |
| 981 | } |
| 982 | return length; |
| 983 | } |
| 984 | } |
| 985 | |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 986 | public void setPasswordExpirationTimeout(ComponentName who, long timeout) { |
| 987 | synchronized (this) { |
| 988 | if (who == null) { |
| 989 | throw new NullPointerException("ComponentName is null"); |
| 990 | } |
Andy Stadler | 1f35d48 | 2010-11-19 15:39:41 -0800 | [diff] [blame^] | 991 | if (timeout < 0) { |
| 992 | throw new IllegalArgumentException("Timeout must be >= 0 ms"); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 993 | } |
| 994 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 995 | DeviceAdminInfo.USES_POLICY_EXPIRE_PASSWORD); |
| 996 | // Calling this API automatically bumps the expiration date |
| 997 | final long expiration = timeout > 0L ? (timeout + System.currentTimeMillis()) : 0L; |
| 998 | ap.passwordExpirationDate = expiration; |
| 999 | ap.passwordExpirationTimeout = timeout; |
| 1000 | if (timeout > 0L) { |
| 1001 | Slog.w(TAG, "setPasswordExpiration(): password will expire on " |
| 1002 | + DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT) |
| 1003 | .format(new Date(expiration))); |
| 1004 | } |
| 1005 | saveSettingsLocked(); |
| 1006 | setExpirationAlarmCheckLocked(mContext); // in case this is the first one |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | public long getPasswordExpirationTimeout(ComponentName who) { |
| 1011 | synchronized (this) { |
| 1012 | long timeout = 0L; |
| 1013 | if (who != null) { |
| 1014 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who); |
| 1015 | return admin != null ? admin.passwordExpirationTimeout : timeout; |
| 1016 | } |
| 1017 | |
| 1018 | final int N = mAdminList.size(); |
| 1019 | for (int i = 0; i < N; i++) { |
| 1020 | ActiveAdmin admin = mAdminList.get(i); |
| 1021 | if (timeout == 0L || (admin.passwordExpirationTimeout != 0L |
| 1022 | && timeout > admin.passwordExpirationTimeout)) { |
| 1023 | timeout = admin.passwordExpirationTimeout; |
| 1024 | } |
| 1025 | } |
| 1026 | return timeout; |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | private long getPasswordExpirationLocked(ComponentName who) { |
| 1031 | long timeout = 0L; |
| 1032 | if (who != null) { |
| 1033 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who); |
| 1034 | return admin != null ? admin.passwordExpirationDate : timeout; |
| 1035 | } |
| 1036 | |
| 1037 | final int N = mAdminList.size(); |
| 1038 | for (int i = 0; i < N; i++) { |
| 1039 | ActiveAdmin admin = mAdminList.get(i); |
| 1040 | if (timeout == 0L || (admin.passwordExpirationDate != 0 |
| 1041 | && timeout > admin.passwordExpirationDate)) { |
| 1042 | timeout = admin.passwordExpirationDate; |
| 1043 | } |
| 1044 | } |
| 1045 | return timeout; |
| 1046 | } |
| 1047 | |
| 1048 | public long getPasswordExpiration(ComponentName who) { |
| 1049 | synchronized (this) { |
| 1050 | return getPasswordExpirationLocked(who); |
| 1051 | } |
| 1052 | } |
| 1053 | |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1054 | public void setPasswordMinimumUpperCase(ComponentName who, int length) { |
| 1055 | synchronized (this) { |
| 1056 | if (who == null) { |
| 1057 | throw new NullPointerException("ComponentName is null"); |
| 1058 | } |
| 1059 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 1060 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
| 1061 | if (ap.minimumPasswordUpperCase != length) { |
| 1062 | ap.minimumPasswordUpperCase = length; |
| 1063 | saveSettingsLocked(); |
| 1064 | } |
| 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | public int getPasswordMinimumUpperCase(ComponentName who) { |
| 1069 | synchronized (this) { |
| 1070 | int length = 0; |
| 1071 | |
| 1072 | if (who != null) { |
| 1073 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who); |
| 1074 | return admin != null ? admin.minimumPasswordUpperCase : length; |
| 1075 | } |
| 1076 | |
| 1077 | final int N = mAdminList.size(); |
| 1078 | for (int i=0; i<N; i++) { |
| 1079 | ActiveAdmin admin = mAdminList.get(i); |
| 1080 | if (length < admin.minimumPasswordUpperCase) { |
| 1081 | length = admin.minimumPasswordUpperCase; |
| 1082 | } |
| 1083 | } |
| 1084 | return length; |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | public void setPasswordMinimumLowerCase(ComponentName who, int length) { |
| 1089 | synchronized (this) { |
| 1090 | if (who == null) { |
| 1091 | throw new NullPointerException("ComponentName is null"); |
| 1092 | } |
| 1093 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 1094 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
| 1095 | if (ap.minimumPasswordLowerCase != length) { |
| 1096 | ap.minimumPasswordLowerCase = length; |
| 1097 | saveSettingsLocked(); |
| 1098 | } |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | public int getPasswordMinimumLowerCase(ComponentName who) { |
| 1103 | synchronized (this) { |
| 1104 | int length = 0; |
| 1105 | |
| 1106 | if (who != null) { |
| 1107 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who); |
| 1108 | return admin != null ? admin.minimumPasswordLowerCase : length; |
| 1109 | } |
| 1110 | |
| 1111 | final int N = mAdminList.size(); |
| 1112 | for (int i=0; i<N; i++) { |
| 1113 | ActiveAdmin admin = mAdminList.get(i); |
| 1114 | if (length < admin.minimumPasswordLowerCase) { |
| 1115 | length = admin.minimumPasswordLowerCase; |
| 1116 | } |
| 1117 | } |
| 1118 | return length; |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | public void setPasswordMinimumLetters(ComponentName who, int length) { |
| 1123 | synchronized (this) { |
| 1124 | if (who == null) { |
| 1125 | throw new NullPointerException("ComponentName is null"); |
| 1126 | } |
| 1127 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 1128 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
| 1129 | if (ap.minimumPasswordLetters != length) { |
| 1130 | ap.minimumPasswordLetters = length; |
| 1131 | saveSettingsLocked(); |
| 1132 | } |
| 1133 | } |
| 1134 | } |
| 1135 | |
| 1136 | public int getPasswordMinimumLetters(ComponentName who) { |
| 1137 | synchronized (this) { |
| 1138 | int length = 0; |
| 1139 | |
| 1140 | if (who != null) { |
| 1141 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who); |
| 1142 | return admin != null ? admin.minimumPasswordLetters : length; |
| 1143 | } |
| 1144 | |
| 1145 | final int N = mAdminList.size(); |
| 1146 | for (int i=0; i<N; i++) { |
| 1147 | ActiveAdmin admin = mAdminList.get(i); |
| 1148 | if (length < admin.minimumPasswordLetters) { |
| 1149 | length = admin.minimumPasswordLetters; |
| 1150 | } |
| 1151 | } |
| 1152 | return length; |
| 1153 | } |
| 1154 | } |
| 1155 | |
| 1156 | public void setPasswordMinimumNumeric(ComponentName who, int length) { |
| 1157 | synchronized (this) { |
| 1158 | if (who == null) { |
| 1159 | throw new NullPointerException("ComponentName is null"); |
| 1160 | } |
| 1161 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 1162 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
| 1163 | if (ap.minimumPasswordNumeric != length) { |
| 1164 | ap.minimumPasswordNumeric = length; |
| 1165 | saveSettingsLocked(); |
| 1166 | } |
| 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | public int getPasswordMinimumNumeric(ComponentName who) { |
| 1171 | synchronized (this) { |
| 1172 | int length = 0; |
| 1173 | |
| 1174 | if (who != null) { |
| 1175 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who); |
| 1176 | return admin != null ? admin.minimumPasswordNumeric : length; |
| 1177 | } |
| 1178 | |
| 1179 | final int N = mAdminList.size(); |
| 1180 | for (int i = 0; i < N; i++) { |
| 1181 | ActiveAdmin admin = mAdminList.get(i); |
| 1182 | if (length < admin.minimumPasswordNumeric) { |
| 1183 | length = admin.minimumPasswordNumeric; |
| 1184 | } |
| 1185 | } |
| 1186 | return length; |
| 1187 | } |
| 1188 | } |
| 1189 | |
| 1190 | public void setPasswordMinimumSymbols(ComponentName who, int length) { |
| 1191 | synchronized (this) { |
| 1192 | if (who == null) { |
| 1193 | throw new NullPointerException("ComponentName is null"); |
| 1194 | } |
| 1195 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 1196 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
| 1197 | if (ap.minimumPasswordSymbols != length) { |
| 1198 | ap.minimumPasswordSymbols = length; |
| 1199 | saveSettingsLocked(); |
| 1200 | } |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | public int getPasswordMinimumSymbols(ComponentName who) { |
| 1205 | synchronized (this) { |
| 1206 | int length = 0; |
| 1207 | |
| 1208 | if (who != null) { |
| 1209 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who); |
| 1210 | return admin != null ? admin.minimumPasswordSymbols : length; |
| 1211 | } |
| 1212 | |
| 1213 | final int N = mAdminList.size(); |
| 1214 | for (int i=0; i<N; i++) { |
| 1215 | ActiveAdmin admin = mAdminList.get(i); |
| 1216 | if (length < admin.minimumPasswordSymbols) { |
| 1217 | length = admin.minimumPasswordSymbols; |
| 1218 | } |
| 1219 | } |
| 1220 | return length; |
| 1221 | } |
| 1222 | } |
| 1223 | |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 1224 | public void setPasswordMinimumNonLetter(ComponentName who, int length) { |
| 1225 | synchronized (this) { |
| 1226 | if (who == null) { |
| 1227 | throw new NullPointerException("ComponentName is null"); |
| 1228 | } |
| 1229 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 1230 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
| 1231 | if (ap.minimumPasswordNonLetter != length) { |
| 1232 | ap.minimumPasswordNonLetter = length; |
| 1233 | saveSettingsLocked(); |
| 1234 | } |
| 1235 | } |
| 1236 | } |
| 1237 | |
| 1238 | public int getPasswordMinimumNonLetter(ComponentName who) { |
| 1239 | synchronized (this) { |
| 1240 | int length = 0; |
| 1241 | |
| 1242 | if (who != null) { |
| 1243 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who); |
| 1244 | return admin != null ? admin.minimumPasswordNonLetter : length; |
| 1245 | } |
| 1246 | |
| 1247 | final int N = mAdminList.size(); |
| 1248 | for (int i=0; i<N; i++) { |
| 1249 | ActiveAdmin admin = mAdminList.get(i); |
| 1250 | if (length < admin.minimumPasswordNonLetter) { |
| 1251 | length = admin.minimumPasswordNonLetter; |
| 1252 | } |
| 1253 | } |
| 1254 | return length; |
| 1255 | } |
| 1256 | } |
| 1257 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 1258 | public boolean isActivePasswordSufficient() { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1259 | synchronized (this) { |
| 1260 | // This API can only be called by an active device admin, |
| 1261 | // so try to retrieve it to check that the caller is one. |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 1262 | getActiveAdminForCallerLocked(null, |
| 1263 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1264 | if (mActivePasswordQuality < getPasswordQuality(null) |
| 1265 | || mActivePasswordLength < getPasswordMinimumLength(null)) { |
| 1266 | return false; |
| 1267 | } |
| 1268 | if(mActivePasswordQuality != DevicePolicyManager.PASSWORD_QUALITY_COMPLEX) { |
| 1269 | return true; |
| 1270 | } |
| 1271 | return mActivePasswordUpperCase >= getPasswordMinimumUpperCase(null) |
| 1272 | && mActivePasswordLowerCase >= getPasswordMinimumLowerCase(null) |
| 1273 | && mActivePasswordLetters >= getPasswordMinimumLetters(null) |
| 1274 | && mActivePasswordNumeric >= getPasswordMinimumNumeric(null) |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 1275 | && mActivePasswordSymbols >= getPasswordMinimumSymbols(null) |
| 1276 | && mActivePasswordNonLetter >= getPasswordMinimumNonLetter(null); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1277 | } |
| 1278 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1279 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1280 | public int getCurrentFailedPasswordAttempts() { |
| 1281 | 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 Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 1284 | getActiveAdminForCallerLocked(null, |
| 1285 | DeviceAdminInfo.USES_POLICY_WATCH_LOGIN); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1286 | return mFailedPasswordAttempts; |
| 1287 | } |
| 1288 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1289 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1290 | public void setMaximumFailedPasswordsForWipe(ComponentName who, int num) { |
| 1291 | synchronized (this) { |
| 1292 | // This API can only be called by an active device admin, |
| 1293 | // so try to retrieve it to check that the caller is one. |
| 1294 | getActiveAdminForCallerLocked(who, |
| 1295 | DeviceAdminInfo.USES_POLICY_WIPE_DATA); |
| 1296 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 1297 | DeviceAdminInfo.USES_POLICY_WATCH_LOGIN); |
| 1298 | if (ap.maximumFailedPasswordsForWipe != num) { |
| 1299 | ap.maximumFailedPasswordsForWipe = num; |
| 1300 | saveSettingsLocked(); |
| 1301 | } |
| 1302 | } |
| 1303 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1304 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1305 | public int getMaximumFailedPasswordsForWipe(ComponentName who) { |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1306 | synchronized (this) { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1307 | int count = 0; |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1308 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1309 | if (who != null) { |
| 1310 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who); |
| 1311 | return admin != null ? admin.maximumFailedPasswordsForWipe : count; |
| 1312 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1313 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1314 | final int N = mAdminList.size(); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1315 | for (int i=0; i<N; i++) { |
| 1316 | ActiveAdmin admin = mAdminList.get(i); |
| 1317 | if (count == 0) { |
| 1318 | count = admin.maximumFailedPasswordsForWipe; |
| 1319 | } else if (admin.maximumFailedPasswordsForWipe != 0 |
| 1320 | && count > admin.maximumFailedPasswordsForWipe) { |
| 1321 | count = admin.maximumFailedPasswordsForWipe; |
| 1322 | } |
| 1323 | } |
| 1324 | return count; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1325 | } |
| 1326 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1327 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 1328 | public boolean resetPassword(String password, int flags) { |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 1329 | int quality; |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 1330 | synchronized (this) { |
| 1331 | // This API can only be called by an active device admin, |
| 1332 | // so try to retrieve it to check that the caller is one. |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 1333 | getActiveAdminForCallerLocked(null, |
| 1334 | DeviceAdminInfo.USES_POLICY_RESET_PASSWORD); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 1335 | quality = getPasswordQuality(null); |
| 1336 | if (quality != DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1337 | int realQuality = LockPatternUtils.computePasswordQuality(password); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1338 | if (realQuality < quality |
| 1339 | && quality != DevicePolicyManager.PASSWORD_QUALITY_COMPLEX) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1340 | Slog.w(TAG, "resetPassword: password quality 0x" |
| 1341 | + Integer.toHexString(quality) |
| 1342 | + " does not meet required quality 0x" |
| 1343 | + Integer.toHexString(quality)); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 1344 | return false; |
| 1345 | } |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1346 | quality = Math.max(realQuality, quality); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 1347 | } |
| 1348 | int length = getPasswordMinimumLength(null); |
| 1349 | if (password.length() < length) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1350 | Slog.w(TAG, "resetPassword: password length " + password.length() |
| 1351 | + " does not meet required length " + length); |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 1352 | return false; |
| 1353 | } |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1354 | if (quality == DevicePolicyManager.PASSWORD_QUALITY_COMPLEX) { |
| 1355 | int letters = 0; |
| 1356 | int uppercase = 0; |
| 1357 | int lowercase = 0; |
| 1358 | int numbers = 0; |
| 1359 | int symbols = 0; |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 1360 | int nonletter = 0; |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1361 | for (int i = 0; i < password.length(); i++) { |
| 1362 | char c = password.charAt(i); |
| 1363 | if (c >= 'A' && c <= 'Z') { |
| 1364 | letters++; |
| 1365 | uppercase++; |
| 1366 | } else if (c >= 'a' && c <= 'z') { |
| 1367 | letters++; |
| 1368 | lowercase++; |
| 1369 | } else if (c >= '0' && c <= '9') { |
| 1370 | numbers++; |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 1371 | nonletter++; |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1372 | } else { |
| 1373 | symbols++; |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 1374 | nonletter++; |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1375 | } |
| 1376 | } |
| 1377 | int neededLetters = getPasswordMinimumLetters(null); |
| 1378 | if(letters < neededLetters) { |
| 1379 | Slog.w(TAG, "resetPassword: number of letters " + letters |
| 1380 | + " does not meet required number of letters " + neededLetters); |
| 1381 | return false; |
| 1382 | } |
| 1383 | int neededNumbers = getPasswordMinimumNumeric(null); |
| 1384 | if (numbers < neededNumbers) { |
| 1385 | Slog |
| 1386 | .w(TAG, "resetPassword: number of numerical digits " + numbers |
| 1387 | + " does not meet required number of numerical digits " |
| 1388 | + neededNumbers); |
| 1389 | return false; |
| 1390 | } |
| 1391 | int neededLowerCase = getPasswordMinimumLowerCase(null); |
| 1392 | if (lowercase < neededLowerCase) { |
| 1393 | Slog.w(TAG, "resetPassword: number of lowercase letters " + lowercase |
| 1394 | + " does not meet required number of lowercase letters " |
| 1395 | + neededLowerCase); |
| 1396 | return false; |
| 1397 | } |
| 1398 | int neededUpperCase = getPasswordMinimumUpperCase(null); |
| 1399 | if (uppercase < neededUpperCase) { |
| 1400 | Slog.w(TAG, "resetPassword: number of uppercase letters " + uppercase |
| 1401 | + " does not meet required number of uppercase letters " |
| 1402 | + neededUpperCase); |
| 1403 | return false; |
| 1404 | } |
| 1405 | int neededSymbols = getPasswordMinimumSymbols(null); |
| 1406 | if (symbols < neededSymbols) { |
| 1407 | Slog.w(TAG, "resetPassword: number of special symbols " + symbols |
| 1408 | + " does not meet required number of special symbols " + neededSymbols); |
| 1409 | return false; |
| 1410 | } |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 1411 | int neededNonLetter = getPasswordMinimumNonLetter(null); |
| 1412 | if (nonletter < neededNonLetter) { |
| 1413 | Slog.w(TAG, "resetPassword: number of non-letter characters " + nonletter |
| 1414 | + " does not meet required number of non-letter characters " |
| 1415 | + neededNonLetter); |
| 1416 | return false; |
| 1417 | } |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1418 | } |
| 1419 | |
| 1420 | LockPatternUtils utils = new LockPatternUtils(mContext); |
| 1421 | if(utils.checkPasswordHistory(password)) { |
| 1422 | Slog.w(TAG, "resetPassword: password is the same as one of the last " |
| 1423 | + getPasswordHistoryLength(null) + " passwords"); |
| 1424 | return false; |
| 1425 | } |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 1426 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1427 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 1428 | int callingUid = Binder.getCallingUid(); |
| 1429 | if (mPasswordOwner >= 0 && mPasswordOwner != callingUid) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1430 | Slog.w(TAG, "resetPassword: already set by another uid and not entered by user"); |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 1431 | return false; |
| 1432 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1433 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 1434 | // Don't do this with the lock held, because it is going to call |
| 1435 | // back in to the service. |
| 1436 | long ident = Binder.clearCallingIdentity(); |
| 1437 | try { |
| 1438 | LockPatternUtils utils = new LockPatternUtils(mContext); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 1439 | utils.saveLockPassword(password, quality); |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1440 | synchronized (this) { |
| 1441 | int newOwner = (flags&DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY) |
| 1442 | != 0 ? callingUid : -1; |
| 1443 | if (mPasswordOwner != newOwner) { |
| 1444 | mPasswordOwner = newOwner; |
| 1445 | saveSettingsLocked(); |
| 1446 | } |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 1447 | } |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 1448 | } finally { |
| 1449 | Binder.restoreCallingIdentity(ident); |
| 1450 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1451 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 1452 | return true; |
| 1453 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1454 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1455 | public void setMaximumTimeToLock(ComponentName who, long timeMs) { |
| 1456 | synchronized (this) { |
| 1457 | if (who == null) { |
| 1458 | throw new NullPointerException("ComponentName is null"); |
| 1459 | } |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 1460 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
Dianne Hackborn | 315ada7 | 2010-02-11 12:14:08 -0800 | [diff] [blame] | 1461 | DeviceAdminInfo.USES_POLICY_FORCE_LOCK); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1462 | if (ap.maximumTimeToUnlock != timeMs) { |
| 1463 | ap.maximumTimeToUnlock = timeMs; |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1464 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 1465 | long ident = Binder.clearCallingIdentity(); |
| 1466 | try { |
| 1467 | saveSettingsLocked(); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1468 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1469 | timeMs = getMaximumTimeToLock(null); |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 1470 | if (timeMs <= 0) { |
| 1471 | timeMs = Integer.MAX_VALUE; |
| 1472 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1473 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 1474 | try { |
| 1475 | getIPowerManager().setMaximumScreenOffTimeount((int)timeMs); |
| 1476 | } catch (RemoteException e) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1477 | Slog.w(TAG, "Failure talking with power manager", e); |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 1478 | } |
| 1479 | } finally { |
| 1480 | Binder.restoreCallingIdentity(ident); |
| 1481 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1482 | } |
| 1483 | } |
| 1484 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1485 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1486 | public long getMaximumTimeToLock(ComponentName who) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1487 | synchronized (this) { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1488 | long time = 0; |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1489 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1490 | if (who != null) { |
| 1491 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who); |
| 1492 | return admin != null ? admin.maximumTimeToUnlock : time; |
| 1493 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1494 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1495 | final int N = mAdminList.size(); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1496 | for (int i=0; i<N; i++) { |
| 1497 | ActiveAdmin admin = mAdminList.get(i); |
| 1498 | if (time == 0) { |
| 1499 | time = admin.maximumTimeToUnlock; |
| 1500 | } else if (admin.maximumTimeToUnlock != 0 |
| 1501 | && time > admin.maximumTimeToUnlock) { |
| 1502 | time = admin.maximumTimeToUnlock; |
| 1503 | } |
| 1504 | } |
| 1505 | return time; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1506 | } |
| 1507 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1508 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 1509 | public void lockNow() { |
| 1510 | synchronized (this) { |
| 1511 | // This API can only be called by an active device admin, |
| 1512 | // so try to retrieve it to check that the caller is one. |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 1513 | getActiveAdminForCallerLocked(null, |
| 1514 | DeviceAdminInfo.USES_POLICY_FORCE_LOCK); |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1515 | long ident = Binder.clearCallingIdentity(); |
| 1516 | try { |
| 1517 | mIPowerManager.goToSleepWithReason(SystemClock.uptimeMillis(), |
| 1518 | WindowManagerPolicy.OFF_BECAUSE_OF_ADMIN); |
| 1519 | } catch (RemoteException e) { |
| 1520 | } finally { |
| 1521 | Binder.restoreCallingIdentity(ident); |
| 1522 | } |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 1523 | } |
| 1524 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1525 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1526 | void wipeDataLocked(int flags) { |
Dianne Hackborn | 4249917 | 2010-10-15 18:45:07 -0700 | [diff] [blame] | 1527 | if ((flags&DevicePolicyManager.WIPE_EXTERNAL_STORAGE) != 0) { |
| 1528 | Intent intent = new Intent(ExternalStorageFormatter.FORMAT_AND_FACTORY_RESET); |
| 1529 | intent.setComponent(ExternalStorageFormatter.COMPONENT_NAME); |
| 1530 | mWakeLock.acquire(10000); |
| 1531 | mContext.startService(intent); |
| 1532 | } else { |
| 1533 | try { |
| 1534 | RecoverySystem.rebootWipeUserData(mContext); |
| 1535 | } catch (IOException e) { |
| 1536 | Slog.w(TAG, "Failed requesting data wipe", e); |
| 1537 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1538 | } |
| 1539 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1540 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1541 | public void wipeData(int flags) { |
| 1542 | synchronized (this) { |
| 1543 | // This API can only be called by an active device admin, |
| 1544 | // so try to retrieve it to check that the caller is one. |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 1545 | getActiveAdminForCallerLocked(null, |
| 1546 | DeviceAdminInfo.USES_POLICY_WIPE_DATA); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1547 | long ident = Binder.clearCallingIdentity(); |
| 1548 | try { |
| 1549 | wipeDataLocked(flags); |
| 1550 | } finally { |
| 1551 | Binder.restoreCallingIdentity(ident); |
| 1552 | } |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 1553 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1554 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1555 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1556 | public void getRemoveWarning(ComponentName comp, final RemoteCallback result) { |
| 1557 | mContext.enforceCallingOrSelfPermission( |
| 1558 | android.Manifest.permission.BIND_DEVICE_ADMIN, null); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1559 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1560 | synchronized (this) { |
| 1561 | ActiveAdmin admin = getActiveAdminUncheckedLocked(comp); |
| 1562 | if (admin == null) { |
| 1563 | try { |
| 1564 | result.sendResult(null); |
| 1565 | } catch (RemoteException e) { |
| 1566 | } |
| 1567 | return; |
| 1568 | } |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 1569 | Intent intent = new Intent(DeviceAdminReceiver.ACTION_DEVICE_ADMIN_DISABLE_REQUESTED); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1570 | intent.setComponent(admin.info.getComponent()); |
| 1571 | mContext.sendOrderedBroadcast(intent, null, new BroadcastReceiver() { |
| 1572 | @Override |
| 1573 | public void onReceive(Context context, Intent intent) { |
| 1574 | try { |
| 1575 | result.sendResult(getResultExtras(false)); |
| 1576 | } catch (RemoteException e) { |
| 1577 | } |
| 1578 | } |
| 1579 | }, null, Activity.RESULT_OK, null, null); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1580 | } |
| 1581 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1582 | |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1583 | public void setActivePasswordState(int quality, int length, int letters, int uppercase, |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 1584 | int lowercase, int numbers, int symbols, int nonletter) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1585 | mContext.enforceCallingOrSelfPermission( |
| 1586 | android.Manifest.permission.BIND_DEVICE_ADMIN, null); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1587 | |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1588 | validateQualityConstant(quality); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1589 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1590 | synchronized (this) { |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 1591 | if (mActivePasswordQuality != quality || mActivePasswordLength != length |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1592 | || mFailedPasswordAttempts != 0 || mActivePasswordLetters != letters |
| 1593 | || mActivePasswordUpperCase != uppercase |
| 1594 | || mActivePasswordLowerCase != lowercase || mActivePasswordNumeric != numbers |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 1595 | || mActivePasswordSymbols != symbols || mActivePasswordNonLetter != nonletter) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1596 | long ident = Binder.clearCallingIdentity(); |
| 1597 | try { |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 1598 | mActivePasswordQuality = quality; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1599 | mActivePasswordLength = length; |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1600 | mActivePasswordLetters = letters; |
| 1601 | mActivePasswordLowerCase = lowercase; |
| 1602 | mActivePasswordUpperCase = uppercase; |
| 1603 | mActivePasswordNumeric = numbers; |
| 1604 | mActivePasswordSymbols = symbols; |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 1605 | mActivePasswordNonLetter = nonletter; |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1606 | mFailedPasswordAttempts = 0; |
| 1607 | saveSettingsLocked(); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1608 | updatePasswordExpirationsLocked(); |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 1609 | sendAdminCommandLocked(DeviceAdminReceiver.ACTION_PASSWORD_CHANGED, |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 1610 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1611 | } finally { |
| 1612 | Binder.restoreCallingIdentity(ident); |
| 1613 | } |
| 1614 | } |
| 1615 | } |
| 1616 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1617 | |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1618 | private void updatePasswordExpirationsLocked() { |
| 1619 | final int N = mAdminList.size(); |
| 1620 | if (N > 0) { |
| 1621 | for (int i=0; i<N; i++) { |
| 1622 | ActiveAdmin admin = mAdminList.get(i); |
| 1623 | if (admin.info.usesPolicy(DeviceAdminInfo.USES_POLICY_EXPIRE_PASSWORD)) { |
| 1624 | admin.passwordExpirationDate = System.currentTimeMillis() |
| 1625 | + admin.passwordExpirationTimeout; |
| 1626 | } |
| 1627 | } |
| 1628 | saveSettingsLocked(); |
| 1629 | } |
| 1630 | } |
| 1631 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1632 | public void reportFailedPasswordAttempt() { |
| 1633 | mContext.enforceCallingOrSelfPermission( |
| 1634 | android.Manifest.permission.BIND_DEVICE_ADMIN, null); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1635 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1636 | synchronized (this) { |
| 1637 | long ident = Binder.clearCallingIdentity(); |
| 1638 | try { |
| 1639 | mFailedPasswordAttempts++; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1640 | saveSettingsLocked(); |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1641 | int max = getMaximumFailedPasswordsForWipe(null); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1642 | if (max > 0 && mFailedPasswordAttempts >= max) { |
| 1643 | wipeDataLocked(0); |
| 1644 | } |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 1645 | sendAdminCommandLocked(DeviceAdminReceiver.ACTION_PASSWORD_FAILED, |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 1646 | DeviceAdminInfo.USES_POLICY_WATCH_LOGIN); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1647 | } finally { |
| 1648 | Binder.restoreCallingIdentity(ident); |
| 1649 | } |
| 1650 | } |
| 1651 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1652 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1653 | public void reportSuccessfulPasswordAttempt() { |
| 1654 | mContext.enforceCallingOrSelfPermission( |
| 1655 | android.Manifest.permission.BIND_DEVICE_ADMIN, null); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1656 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1657 | synchronized (this) { |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 1658 | if (mFailedPasswordAttempts != 0 || mPasswordOwner >= 0) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1659 | long ident = Binder.clearCallingIdentity(); |
| 1660 | try { |
| 1661 | mFailedPasswordAttempts = 0; |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 1662 | mPasswordOwner = -1; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1663 | saveSettingsLocked(); |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 1664 | sendAdminCommandLocked(DeviceAdminReceiver.ACTION_PASSWORD_SUCCEEDED, |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 1665 | DeviceAdminInfo.USES_POLICY_WATCH_LOGIN); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1666 | } finally { |
| 1667 | Binder.restoreCallingIdentity(ident); |
| 1668 | } |
| 1669 | } |
| 1670 | } |
| 1671 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1672 | |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 1673 | public ComponentName setGlobalProxy(ComponentName who, String proxySpec, |
| 1674 | String exclusionList) { |
| 1675 | synchronized(this) { |
| 1676 | if (who == null) { |
| 1677 | throw new NullPointerException("ComponentName is null"); |
| 1678 | } |
| 1679 | |
| 1680 | ActiveAdmin admin = getActiveAdminForCallerLocked(who, |
| 1681 | DeviceAdminInfo.USES_POLICY_SETS_GLOBAL_PROXY); |
| 1682 | |
| 1683 | // Scan through active admins and find if anyone has already |
| 1684 | // set the global proxy. |
| 1685 | final int N = mAdminList.size(); |
| 1686 | Set<ComponentName> compSet = mAdminMap.keySet(); |
| 1687 | for (ComponentName component : compSet) { |
| 1688 | ActiveAdmin ap = mAdminMap.get(component); |
| 1689 | if ((ap.specifiesGlobalProxy) && (!component.equals(who))) { |
| 1690 | // Another admin already sets the global proxy |
| 1691 | // Return it to the caller. |
| 1692 | return component; |
| 1693 | } |
| 1694 | } |
| 1695 | if (proxySpec == null) { |
| 1696 | admin.specifiesGlobalProxy = false; |
| 1697 | admin.globalProxySpec = null; |
| 1698 | admin.globalProxyExclusionList = null; |
| 1699 | } else { |
| 1700 | |
| 1701 | admin.specifiesGlobalProxy = true; |
| 1702 | admin.globalProxySpec = proxySpec; |
| 1703 | admin.globalProxyExclusionList = exclusionList; |
| 1704 | } |
| 1705 | |
| 1706 | // Reset the global proxy accordingly |
| 1707 | // Do this using system permissions, as apps cannot write to secure settings |
| 1708 | long origId = Binder.clearCallingIdentity(); |
| 1709 | resetGlobalProxy(); |
| 1710 | Binder.restoreCallingIdentity(origId); |
| 1711 | return null; |
| 1712 | } |
| 1713 | } |
| 1714 | |
| 1715 | public ComponentName getGlobalProxyAdmin() { |
| 1716 | synchronized(this) { |
| 1717 | // Scan through active admins and find if anyone has already |
| 1718 | // set the global proxy. |
| 1719 | final int N = mAdminList.size(); |
| 1720 | for (int i = 0; i < N; i++) { |
| 1721 | ActiveAdmin ap = mAdminList.get(i); |
| 1722 | if (ap.specifiesGlobalProxy) { |
| 1723 | // Device admin sets the global proxy |
| 1724 | // Return it to the caller. |
| 1725 | return ap.info.getComponent(); |
| 1726 | } |
| 1727 | } |
| 1728 | } |
| 1729 | // No device admin sets the global proxy. |
| 1730 | return null; |
| 1731 | } |
| 1732 | |
| 1733 | private void resetGlobalProxy() { |
| 1734 | final int N = mAdminList.size(); |
| 1735 | for (int i = 0; i < N; i++) { |
| 1736 | ActiveAdmin ap = mAdminList.get(i); |
| 1737 | if (ap.specifiesGlobalProxy) { |
| 1738 | saveGlobalProxy(ap.globalProxySpec, ap.globalProxyExclusionList); |
| 1739 | return; |
| 1740 | } |
| 1741 | } |
| 1742 | // No device admins defining global proxies - reset global proxy settings to none |
| 1743 | saveGlobalProxy(null, null); |
| 1744 | } |
| 1745 | |
| 1746 | private void saveGlobalProxy(String proxySpec, String exclusionList) { |
| 1747 | if (exclusionList == null) { |
| 1748 | exclusionList = ""; |
| 1749 | } |
| 1750 | if (proxySpec == null) { |
| 1751 | proxySpec = ""; |
| 1752 | } |
| 1753 | // Remove white spaces |
| 1754 | proxySpec = proxySpec.trim(); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 1755 | String data[] = proxySpec.split(":"); |
| 1756 | int proxyPort = 8080; |
| 1757 | if (data.length > 1) { |
| 1758 | try { |
| 1759 | proxyPort = Integer.parseInt(data[1]); |
| 1760 | } catch (NumberFormatException e) {} |
| 1761 | } |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 1762 | exclusionList = exclusionList.trim(); |
| 1763 | ContentResolver res = mContext.getContentResolver(); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 1764 | Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_HOST, data[0]); |
| 1765 | Settings.Secure.putInt(res, Settings.Secure.GLOBAL_HTTP_PROXY_PORT, proxyPort); |
| 1766 | Settings.Secure.putString(res, Settings.Secure.GLOBAL_HTTP_PROXY_EXCLUSION_LIST, |
| 1767 | exclusionList); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 1768 | } |
| 1769 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 1770 | @Override |
| 1771 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1772 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 1773 | != PackageManager.PERMISSION_GRANTED) { |
| 1774 | |
| 1775 | pw.println("Permission Denial: can't dump DevicePolicyManagerService from from pid=" |
| 1776 | + Binder.getCallingPid() |
| 1777 | + ", uid=" + Binder.getCallingUid()); |
| 1778 | return; |
| 1779 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1780 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 1781 | final Printer p = new PrintWriterPrinter(pw); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1782 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 1783 | synchronized (this) { |
| 1784 | p.println("Current Device Policy Manager state:"); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1785 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 1786 | p.println(" Enabled Device Admins:"); |
| 1787 | final int N = mAdminList.size(); |
| 1788 | for (int i=0; i<N; i++) { |
| 1789 | ActiveAdmin ap = mAdminList.get(i); |
| 1790 | if (ap != null) { |
| 1791 | pw.print(" "); pw.print(ap.info.getComponent().flattenToShortString()); |
| 1792 | pw.println(":"); |
| 1793 | ap.dump(" ", pw); |
| 1794 | } |
| 1795 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1796 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 1797 | pw.println(" "); |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1798 | pw.print(" mActivePasswordQuality=0x"); |
| 1799 | pw.println(Integer.toHexString(mActivePasswordQuality)); |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 1800 | pw.print(" mActivePasswordLength="); pw.println(mActivePasswordLength); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1801 | pw.print(" mActivePasswordUpperCase="); pw.println(mActivePasswordUpperCase); |
| 1802 | pw.print(" mActivePasswordLowerCase="); pw.println(mActivePasswordLowerCase); |
| 1803 | pw.print(" mActivePasswordLetters="); pw.println(mActivePasswordLetters); |
| 1804 | pw.print(" mActivePasswordNumeric="); pw.println(mActivePasswordNumeric); |
| 1805 | pw.print(" mActivePasswordSymbols="); pw.println(mActivePasswordSymbols); |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 1806 | pw.print(" mActivePasswordNonLetter="); pw.println(mActivePasswordNonLetter); |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 1807 | pw.print(" mFailedPasswordAttempts="); pw.println(mFailedPasswordAttempts); |
| 1808 | pw.print(" mPasswordOwner="); pw.println(mPasswordOwner); |
| 1809 | } |
| 1810 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1811 | } |