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