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