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 | 4249917 | 2010-10-15 18:45:07 -0700 | [diff] [blame] | 19 | import com.android.internal.os.storage.ExternalStorageFormatter; |
Dianne Hackborn | 2269d157 | 2010-02-24 19:54:22 -0800 | [diff] [blame] | 20 | import com.android.internal.util.FastXmlSerializer; |
Dianne Hackborn | 1afd1c9 | 2010-03-18 22:47:17 -0700 | [diff] [blame] | 21 | import com.android.internal.util.JournaledFile; |
Dianne Hackborn | 2269d157 | 2010-02-24 19:54:22 -0800 | [diff] [blame] | 22 | import com.android.internal.util.XmlUtils; |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 23 | import com.android.internal.widget.LockPatternUtils; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 24 | |
| 25 | import org.xmlpull.v1.XmlPullParser; |
| 26 | import org.xmlpull.v1.XmlPullParserException; |
| 27 | import org.xmlpull.v1.XmlSerializer; |
| 28 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 29 | import android.app.Activity; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 30 | import android.app.ActivityManagerNative; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 31 | import android.app.AlarmManager; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 32 | import android.app.AppGlobals; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 33 | import android.app.PendingIntent; |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 34 | import android.app.admin.DeviceAdminInfo; |
| 35 | import android.app.admin.DeviceAdminReceiver; |
| 36 | import android.app.admin.DevicePolicyManager; |
| 37 | import android.app.admin.IDevicePolicyManager; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 38 | import android.content.BroadcastReceiver; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 39 | import android.content.ComponentName; |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 40 | import android.content.ContentResolver; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 41 | import android.content.Context; |
| 42 | import android.content.Intent; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 43 | import android.content.IntentFilter; |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 44 | import android.content.pm.ApplicationInfo; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 45 | import android.content.pm.IPackageManager; |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 46 | import android.content.pm.PackageInfo; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 47 | import android.content.pm.PackageManager; |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 48 | import android.content.pm.Signature; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 49 | import android.content.pm.PackageManager.NameNotFoundException; |
Andy Stadler | 1f35d48 | 2010-11-19 15:39:41 -0800 | [diff] [blame] | 50 | import android.content.pm.ResolveInfo; |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 51 | import android.net.Uri; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 52 | import android.os.Binder; |
Adam Cohen | f752202 | 2012-10-03 20:03:18 -0700 | [diff] [blame] | 53 | import android.os.Bundle; |
Ben Komalo | ed48c8b | 2011-10-17 17:30:21 -0700 | [diff] [blame] | 54 | import android.os.Environment; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 55 | import android.os.Handler; |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 56 | import android.os.IBinder; |
| 57 | import android.os.IPowerManager; |
Dianne Hackborn | 4249917 | 2010-10-15 18:45:07 -0700 | [diff] [blame] | 58 | import android.os.PowerManager; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 59 | import android.os.Process; |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 60 | import android.os.RecoverySystem; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 61 | import android.os.RemoteCallback; |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 62 | import android.os.RemoteException; |
| 63 | import android.os.ServiceManager; |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 64 | import android.os.SystemClock; |
Andy Stadler | 0fe45de | 2011-01-20 16:35:09 -0800 | [diff] [blame] | 65 | import android.os.SystemProperties; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 66 | import android.os.UserHandle; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 67 | import android.os.UserManager; |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 68 | import android.provider.Settings; |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 69 | import android.util.AtomicFile; |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 70 | import android.util.PrintWriterPrinter; |
| 71 | import android.util.Printer; |
Andy Stadler | 1f35d48 | 2010-11-19 15:39:41 -0800 | [diff] [blame] | 72 | import android.util.Slog; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 73 | import android.util.SparseArray; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 74 | import android.util.Xml; |
Jim Miller | 93c518e | 2012-01-17 15:55:31 -0800 | [diff] [blame] | 75 | import android.view.IWindowManager; |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 76 | import android.view.WindowManagerPolicy; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 77 | |
| 78 | import java.io.File; |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 79 | import java.io.FileDescriptor; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 80 | import java.io.FileInputStream; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 81 | import java.io.FileNotFoundException; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 82 | import java.io.FileOutputStream; |
| 83 | import java.io.IOException; |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 84 | import java.io.PrintWriter; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 85 | import java.text.DateFormat; |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 86 | import java.util.ArrayList; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 87 | import java.util.Date; |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 88 | import java.util.HashMap; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 89 | import java.util.List; |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 90 | import java.util.Set; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 91 | |
| 92 | /** |
| 93 | * Implementation of the device policy APIs. |
| 94 | */ |
| 95 | public class DevicePolicyManagerService extends IDevicePolicyManager.Stub { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 96 | |
Jim Miller | 6b85768 | 2011-02-16 16:27:41 -0800 | [diff] [blame] | 97 | private static final String TAG = "DevicePolicyManagerService"; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 98 | |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 99 | private static final String DEVICE_POLICIES_XML = "device_policies.xml"; |
| 100 | |
Jim Miller | 6b85768 | 2011-02-16 16:27:41 -0800 | [diff] [blame] | 101 | private static final int REQUEST_EXPIRE_PASSWORD = 5571; |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 102 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 103 | private static final long MS_PER_DAY = 86400 * 1000; |
| 104 | |
| 105 | private static final long EXPIRATION_GRACE_PERIOD_MS = 5 * MS_PER_DAY; // 5 days, in ms |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 106 | |
| 107 | protected static final String ACTION_EXPIRED_PASSWORD_NOTIFICATION |
| 108 | = "com.android.server.ACTION_EXPIRED_PASSWORD_NOTIFICATION"; |
| 109 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 110 | private static final boolean DBG = false; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 111 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 112 | final Context mContext; |
Dianne Hackborn | 4249917 | 2010-10-15 18:45:07 -0700 | [diff] [blame] | 113 | final PowerManager.WakeLock mWakeLock; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 114 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 115 | IPowerManager mIPowerManager; |
Jim Miller | 93c518e | 2012-01-17 15:55:31 -0800 | [diff] [blame] | 116 | IWindowManager mIWindowManager; |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 117 | |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 118 | private DeviceOwner mDeviceOwner; |
| 119 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 120 | public static class DevicePolicyData { |
| 121 | int mActivePasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; |
| 122 | int mActivePasswordLength = 0; |
| 123 | int mActivePasswordUpperCase = 0; |
| 124 | int mActivePasswordLowerCase = 0; |
| 125 | int mActivePasswordLetters = 0; |
| 126 | int mActivePasswordNumeric = 0; |
| 127 | int mActivePasswordSymbols = 0; |
| 128 | int mActivePasswordNonLetter = 0; |
| 129 | int mFailedPasswordAttempts = 0; |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 130 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 131 | int mUserHandle;; |
| 132 | int mPasswordOwner = -1; |
| 133 | long mLastMaximumTimeToLock = -1; |
| 134 | |
| 135 | final HashMap<ComponentName, ActiveAdmin> mAdminMap |
| 136 | = new HashMap<ComponentName, ActiveAdmin>(); |
| 137 | final ArrayList<ActiveAdmin> mAdminList |
| 138 | = new ArrayList<ActiveAdmin>(); |
| 139 | |
| 140 | public DevicePolicyData(int userHandle) { |
| 141 | mUserHandle = userHandle; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | final SparseArray<DevicePolicyData> mUserData = new SparseArray<DevicePolicyData>(); |
| 146 | |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 147 | Handler mHandler = new Handler(); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 148 | |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 149 | BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 150 | @Override |
| 151 | public void onReceive(Context context, Intent intent) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 152 | final String action = intent.getAction(); |
| 153 | final int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, |
| 154 | getSendingUserId()); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 155 | if (Intent.ACTION_BOOT_COMPLETED.equals(action) |
| 156 | || ACTION_EXPIRED_PASSWORD_NOTIFICATION.equals(action)) { |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 157 | if (DBG) Slog.v(TAG, "Sending password expiration notifications for action " |
| 158 | + action + " for user " + userHandle); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 159 | mHandler.post(new Runnable() { |
| 160 | public void run() { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 161 | handlePasswordExpirationNotification(getUserData(userHandle)); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 162 | } |
| 163 | }); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 164 | } else if (Intent.ACTION_USER_REMOVED.equals(action)) { |
| 165 | removeUserData(userHandle); |
| 166 | } else if (Intent.ACTION_USER_STARTED.equals(action) |
| 167 | || Intent.ACTION_PACKAGE_CHANGED.equals(action) |
| 168 | || Intent.ACTION_PACKAGE_REMOVED.equals(action) |
| 169 | || Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) { |
| 170 | |
| 171 | if (Intent.ACTION_USER_STARTED.equals(action)) { |
| 172 | // Reset the policy data |
| 173 | synchronized (DevicePolicyManagerService.this) { |
| 174 | mUserData.remove(userHandle); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | handlePackagesChanged(userHandle); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | }; |
| 182 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 183 | static class ActiveAdmin { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 184 | final DeviceAdminInfo info; |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 185 | |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 186 | int passwordQuality = DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 187 | |
| 188 | static final int DEF_MINIMUM_PASSWORD_LENGTH = 0; |
| 189 | int minimumPasswordLength = DEF_MINIMUM_PASSWORD_LENGTH; |
| 190 | |
| 191 | static final int DEF_PASSWORD_HISTORY_LENGTH = 0; |
| 192 | int passwordHistoryLength = DEF_PASSWORD_HISTORY_LENGTH; |
| 193 | |
| 194 | static final int DEF_MINIMUM_PASSWORD_UPPER_CASE = 0; |
| 195 | int minimumPasswordUpperCase = DEF_MINIMUM_PASSWORD_UPPER_CASE; |
| 196 | |
| 197 | static final int DEF_MINIMUM_PASSWORD_LOWER_CASE = 0; |
| 198 | int minimumPasswordLowerCase = DEF_MINIMUM_PASSWORD_LOWER_CASE; |
| 199 | |
| 200 | static final int DEF_MINIMUM_PASSWORD_LETTERS = 1; |
Dianne Hackborn | 2a10840 | 2011-05-26 11:45:00 -0700 | [diff] [blame] | 201 | int minimumPasswordLetters = DEF_MINIMUM_PASSWORD_LETTERS; |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 202 | |
| 203 | static final int DEF_MINIMUM_PASSWORD_NUMERIC = 1; |
| 204 | int minimumPasswordNumeric = DEF_MINIMUM_PASSWORD_NUMERIC; |
| 205 | |
| 206 | static final int DEF_MINIMUM_PASSWORD_SYMBOLS = 1; |
| 207 | int minimumPasswordSymbols = DEF_MINIMUM_PASSWORD_SYMBOLS; |
| 208 | |
| 209 | static final int DEF_MINIMUM_PASSWORD_NON_LETTER = 0; |
| 210 | int minimumPasswordNonLetter = DEF_MINIMUM_PASSWORD_NON_LETTER; |
| 211 | |
| 212 | static final long DEF_MAXIMUM_TIME_TO_UNLOCK = 0; |
| 213 | long maximumTimeToUnlock = DEF_MAXIMUM_TIME_TO_UNLOCK; |
| 214 | |
| 215 | static final int DEF_MAXIMUM_FAILED_PASSWORDS_FOR_WIPE = 0; |
| 216 | int maximumFailedPasswordsForWipe = DEF_MAXIMUM_FAILED_PASSWORDS_FOR_WIPE; |
| 217 | |
| 218 | static final long DEF_PASSWORD_EXPIRATION_TIMEOUT = 0; |
| 219 | long passwordExpirationTimeout = DEF_PASSWORD_EXPIRATION_TIMEOUT; |
| 220 | |
| 221 | static final long DEF_PASSWORD_EXPIRATION_DATE = 0; |
| 222 | long passwordExpirationDate = DEF_PASSWORD_EXPIRATION_DATE; |
| 223 | |
Jim Miller | 48b9b0d | 2012-09-19 23:16:50 -0700 | [diff] [blame] | 224 | static final int DEF_KEYGUARD_FEATURES_DISABLED = 0; // none |
| 225 | int disabledKeyguardFeatures = DEF_KEYGUARD_FEATURES_DISABLED; |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 226 | |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 227 | boolean encryptionRequested = false; |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 228 | boolean disableCamera = false; |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 229 | |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 230 | // TODO: review implementation decisions with frameworks team |
| 231 | boolean specifiesGlobalProxy = false; |
| 232 | String globalProxySpec = null; |
| 233 | String globalProxyExclusionList = null; |
| 234 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 235 | ActiveAdmin(DeviceAdminInfo _info) { |
| 236 | info = _info; |
| 237 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 238 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 239 | int getUid() { return info.getActivityInfo().applicationInfo.uid; } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 240 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 241 | public UserHandle getUserHandle() { |
| 242 | return new UserHandle(UserHandle.getUserId(info.getActivityInfo().applicationInfo.uid)); |
| 243 | } |
| 244 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 245 | void writeToXml(XmlSerializer out) |
| 246 | throws IllegalArgumentException, IllegalStateException, IOException { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 247 | out.startTag(null, "policies"); |
| 248 | info.writePoliciesToXml(out); |
| 249 | out.endTag(null, "policies"); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 250 | if (passwordQuality != DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) { |
| 251 | out.startTag(null, "password-quality"); |
| 252 | out.attribute(null, "value", Integer.toString(passwordQuality)); |
| 253 | out.endTag(null, "password-quality"); |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 254 | if (minimumPasswordLength != DEF_MINIMUM_PASSWORD_LENGTH) { |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 255 | out.startTag(null, "min-password-length"); |
| 256 | out.attribute(null, "value", Integer.toString(minimumPasswordLength)); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 257 | out.endTag(null, "min-password-length"); |
| 258 | } |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 259 | if(passwordHistoryLength != DEF_PASSWORD_HISTORY_LENGTH) { |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 260 | out.startTag(null, "password-history-length"); |
| 261 | out.attribute(null, "value", Integer.toString(passwordHistoryLength)); |
| 262 | out.endTag(null, "password-history-length"); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 263 | } |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 264 | if (minimumPasswordUpperCase != DEF_MINIMUM_PASSWORD_UPPER_CASE) { |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 265 | out.startTag(null, "min-password-uppercase"); |
| 266 | out.attribute(null, "value", Integer.toString(minimumPasswordUpperCase)); |
| 267 | out.endTag(null, "min-password-uppercase"); |
| 268 | } |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 269 | if (minimumPasswordLowerCase != DEF_MINIMUM_PASSWORD_LOWER_CASE) { |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 270 | out.startTag(null, "min-password-lowercase"); |
| 271 | out.attribute(null, "value", Integer.toString(minimumPasswordLowerCase)); |
| 272 | out.endTag(null, "min-password-lowercase"); |
| 273 | } |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 274 | if (minimumPasswordLetters != DEF_MINIMUM_PASSWORD_LETTERS) { |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 275 | out.startTag(null, "min-password-letters"); |
| 276 | out.attribute(null, "value", Integer.toString(minimumPasswordLetters)); |
| 277 | out.endTag(null, "min-password-letters"); |
| 278 | } |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 279 | if (minimumPasswordNumeric != DEF_MINIMUM_PASSWORD_NUMERIC) { |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 280 | out.startTag(null, "min-password-numeric"); |
| 281 | out.attribute(null, "value", Integer.toString(minimumPasswordNumeric)); |
| 282 | out.endTag(null, "min-password-numeric"); |
| 283 | } |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 284 | if (minimumPasswordSymbols != DEF_MINIMUM_PASSWORD_SYMBOLS) { |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 285 | out.startTag(null, "min-password-symbols"); |
| 286 | out.attribute(null, "value", Integer.toString(minimumPasswordSymbols)); |
| 287 | out.endTag(null, "min-password-symbols"); |
| 288 | } |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 289 | if (minimumPasswordNonLetter > DEF_MINIMUM_PASSWORD_NON_LETTER) { |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 290 | out.startTag(null, "min-password-nonletter"); |
| 291 | out.attribute(null, "value", Integer.toString(minimumPasswordNonLetter)); |
| 292 | out.endTag(null, "min-password-nonletter"); |
| 293 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 294 | } |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 295 | if (maximumTimeToUnlock != DEF_MAXIMUM_TIME_TO_UNLOCK) { |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 296 | out.startTag(null, "max-time-to-unlock"); |
| 297 | out.attribute(null, "value", Long.toString(maximumTimeToUnlock)); |
| 298 | out.endTag(null, "max-time-to-unlock"); |
| 299 | } |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 300 | if (maximumFailedPasswordsForWipe != DEF_MAXIMUM_FAILED_PASSWORDS_FOR_WIPE) { |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 301 | out.startTag(null, "max-failed-password-wipe"); |
| 302 | out.attribute(null, "value", Integer.toString(maximumFailedPasswordsForWipe)); |
| 303 | out.endTag(null, "max-failed-password-wipe"); |
| 304 | } |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 305 | if (specifiesGlobalProxy) { |
| 306 | out.startTag(null, "specifies-global-proxy"); |
| 307 | out.attribute(null, "value", Boolean.toString(specifiesGlobalProxy)); |
| 308 | out.endTag(null, "specifies_global_proxy"); |
| 309 | if (globalProxySpec != null) { |
| 310 | out.startTag(null, "global-proxy-spec"); |
| 311 | out.attribute(null, "value", globalProxySpec); |
| 312 | out.endTag(null, "global-proxy-spec"); |
| 313 | } |
| 314 | if (globalProxyExclusionList != null) { |
| 315 | out.startTag(null, "global-proxy-exclusion-list"); |
| 316 | out.attribute(null, "value", globalProxyExclusionList); |
| 317 | out.endTag(null, "global-proxy-exclusion-list"); |
| 318 | } |
| 319 | } |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 320 | if (passwordExpirationTimeout != DEF_PASSWORD_EXPIRATION_TIMEOUT) { |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 321 | out.startTag(null, "password-expiration-timeout"); |
| 322 | out.attribute(null, "value", Long.toString(passwordExpirationTimeout)); |
| 323 | out.endTag(null, "password-expiration-timeout"); |
| 324 | } |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 325 | if (passwordExpirationDate != DEF_PASSWORD_EXPIRATION_DATE) { |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 326 | out.startTag(null, "password-expiration-date"); |
| 327 | out.attribute(null, "value", Long.toString(passwordExpirationDate)); |
| 328 | out.endTag(null, "password-expiration-date"); |
| 329 | } |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 330 | if (encryptionRequested) { |
| 331 | out.startTag(null, "encryption-requested"); |
| 332 | out.attribute(null, "value", Boolean.toString(encryptionRequested)); |
| 333 | out.endTag(null, "encryption-requested"); |
| 334 | } |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 335 | if (disableCamera) { |
| 336 | out.startTag(null, "disable-camera"); |
| 337 | out.attribute(null, "value", Boolean.toString(disableCamera)); |
| 338 | out.endTag(null, "disable-camera"); |
| 339 | } |
Jim Miller | 48b9b0d | 2012-09-19 23:16:50 -0700 | [diff] [blame] | 340 | if (disabledKeyguardFeatures != DEF_KEYGUARD_FEATURES_DISABLED) { |
| 341 | out.startTag(null, "disable-keyguard-features"); |
| 342 | out.attribute(null, "value", Integer.toString(disabledKeyguardFeatures)); |
| 343 | out.endTag(null, "disable-keyguard-features"); |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 344 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 345 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 346 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 347 | void readFromXml(XmlPullParser parser) |
| 348 | throws XmlPullParserException, IOException { |
| 349 | int outerDepth = parser.getDepth(); |
| 350 | int type; |
| 351 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 352 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 353 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 354 | continue; |
| 355 | } |
| 356 | String tag = parser.getName(); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 357 | if ("policies".equals(tag)) { |
| 358 | info.readPoliciesFromXml(parser); |
| 359 | } else if ("password-quality".equals(tag)) { |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 360 | passwordQuality = Integer.parseInt( |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 361 | parser.getAttributeValue(null, "value")); |
| 362 | } else if ("min-password-length".equals(tag)) { |
| 363 | minimumPasswordLength = Integer.parseInt( |
| 364 | parser.getAttributeValue(null, "value")); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 365 | } else if ("password-history-length".equals(tag)) { |
| 366 | passwordHistoryLength = Integer.parseInt( |
| 367 | parser.getAttributeValue(null, "value")); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 368 | } else if ("min-password-uppercase".equals(tag)) { |
| 369 | minimumPasswordUpperCase = Integer.parseInt( |
| 370 | parser.getAttributeValue(null, "value")); |
| 371 | } else if ("min-password-lowercase".equals(tag)) { |
| 372 | minimumPasswordLowerCase = Integer.parseInt( |
| 373 | parser.getAttributeValue(null, "value")); |
| 374 | } else if ("min-password-letters".equals(tag)) { |
| 375 | minimumPasswordLetters = Integer.parseInt( |
| 376 | parser.getAttributeValue(null, "value")); |
| 377 | } else if ("min-password-numeric".equals(tag)) { |
| 378 | minimumPasswordNumeric = Integer.parseInt( |
| 379 | parser.getAttributeValue(null, "value")); |
| 380 | } else if ("min-password-symbols".equals(tag)) { |
| 381 | minimumPasswordSymbols = Integer.parseInt( |
| 382 | parser.getAttributeValue(null, "value")); |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 383 | } else if ("min-password-nonletter".equals(tag)) { |
| 384 | minimumPasswordNonLetter = Integer.parseInt( |
| 385 | parser.getAttributeValue(null, "value")); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 386 | } else if ("max-time-to-unlock".equals(tag)) { |
| 387 | maximumTimeToUnlock = Long.parseLong( |
| 388 | parser.getAttributeValue(null, "value")); |
| 389 | } else if ("max-failed-password-wipe".equals(tag)) { |
| 390 | maximumFailedPasswordsForWipe = Integer.parseInt( |
| 391 | parser.getAttributeValue(null, "value")); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 392 | } else if ("specifies-global-proxy".equals(tag)) { |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 393 | specifiesGlobalProxy = Boolean.parseBoolean( |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 394 | parser.getAttributeValue(null, "value")); |
| 395 | } else if ("global-proxy-spec".equals(tag)) { |
| 396 | globalProxySpec = |
| 397 | parser.getAttributeValue(null, "value"); |
| 398 | } else if ("global-proxy-exclusion-list".equals(tag)) { |
| 399 | globalProxyExclusionList = |
| 400 | parser.getAttributeValue(null, "value"); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 401 | } else if ("password-expiration-timeout".equals(tag)) { |
| 402 | passwordExpirationTimeout = Long.parseLong( |
| 403 | parser.getAttributeValue(null, "value")); |
| 404 | } else if ("password-expiration-date".equals(tag)) { |
| 405 | passwordExpirationDate = Long.parseLong( |
| 406 | parser.getAttributeValue(null, "value")); |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 407 | } else if ("encryption-requested".equals(tag)) { |
| 408 | encryptionRequested = Boolean.parseBoolean( |
| 409 | parser.getAttributeValue(null, "value")); |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 410 | } else if ("disable-camera".equals(tag)) { |
| 411 | disableCamera = Boolean.parseBoolean( |
| 412 | parser.getAttributeValue(null, "value")); |
Amith Yamasani | 7077b3c | 2012-10-04 10:28:50 -0700 | [diff] [blame] | 413 | } else if ("disable-keyguard-features".equals(tag)) { |
| 414 | disabledKeyguardFeatures = Integer.parseInt( |
| 415 | parser.getAttributeValue(null, "value")); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 416 | } else { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 417 | Slog.w(TAG, "Unknown admin tag: " + tag); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 418 | } |
| 419 | XmlUtils.skipCurrentTag(parser); |
| 420 | } |
| 421 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 422 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 423 | void dump(String prefix, PrintWriter pw) { |
| 424 | pw.print(prefix); pw.print("uid="); pw.println(getUid()); |
| 425 | pw.print(prefix); pw.println("policies:"); |
| 426 | ArrayList<DeviceAdminInfo.PolicyInfo> pols = info.getUsedPolicies(); |
| 427 | if (pols != null) { |
| 428 | for (int i=0; i<pols.size(); i++) { |
| 429 | pw.print(prefix); pw.print(" "); pw.println(pols.get(i).tag); |
| 430 | } |
| 431 | } |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 432 | pw.print(prefix); pw.print("passwordQuality=0x"); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 433 | pw.println(Integer.toHexString(passwordQuality)); |
| 434 | pw.print(prefix); pw.print("minimumPasswordLength="); |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 435 | pw.println(minimumPasswordLength); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 436 | pw.print(prefix); pw.print("passwordHistoryLength="); |
| 437 | pw.println(passwordHistoryLength); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 438 | pw.print(prefix); pw.print("minimumPasswordUpperCase="); |
| 439 | pw.println(minimumPasswordUpperCase); |
| 440 | pw.print(prefix); pw.print("minimumPasswordLowerCase="); |
| 441 | pw.println(minimumPasswordLowerCase); |
| 442 | pw.print(prefix); pw.print("minimumPasswordLetters="); |
| 443 | pw.println(minimumPasswordLetters); |
| 444 | pw.print(prefix); pw.print("minimumPasswordNumeric="); |
| 445 | pw.println(minimumPasswordNumeric); |
| 446 | pw.print(prefix); pw.print("minimumPasswordSymbols="); |
| 447 | pw.println(minimumPasswordSymbols); |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 448 | pw.print(prefix); pw.print("minimumPasswordNonLetter="); |
| 449 | pw.println(minimumPasswordNonLetter); |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 450 | pw.print(prefix); pw.print("maximumTimeToUnlock="); |
| 451 | pw.println(maximumTimeToUnlock); |
| 452 | pw.print(prefix); pw.print("maximumFailedPasswordsForWipe="); |
| 453 | pw.println(maximumFailedPasswordsForWipe); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 454 | pw.print(prefix); pw.print("specifiesGlobalProxy="); |
| 455 | pw.println(specifiesGlobalProxy); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 456 | pw.print(prefix); pw.print("passwordExpirationTimeout="); |
| 457 | pw.println(passwordExpirationTimeout); |
| 458 | pw.print(prefix); pw.print("passwordExpirationDate="); |
| 459 | pw.println(passwordExpirationDate); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 460 | if (globalProxySpec != null) { |
| 461 | pw.print(prefix); pw.print("globalProxySpec="); |
| 462 | pw.println(globalProxySpec); |
| 463 | } |
| 464 | if (globalProxyExclusionList != null) { |
| 465 | pw.print(prefix); pw.print("globalProxyEclusionList="); |
| 466 | pw.println(globalProxyExclusionList); |
| 467 | } |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 468 | pw.print(prefix); pw.print("encryptionRequested="); |
| 469 | pw.println(encryptionRequested); |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 470 | pw.print(prefix); pw.print("disableCamera="); |
| 471 | pw.println(disableCamera); |
Amith Yamasani | 7077b3c | 2012-10-04 10:28:50 -0700 | [diff] [blame] | 472 | pw.print(prefix); pw.print("disabledKeyguardFeatures="); |
| 473 | pw.println(disabledKeyguardFeatures); |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 474 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 475 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 476 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 477 | private void handlePackagesChanged(int userHandle) { |
| 478 | boolean removed = false; |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 479 | if (DBG) Slog.d(TAG, "Handling package changes for user " + userHandle); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 480 | DevicePolicyData policy = getUserData(userHandle); |
| 481 | IPackageManager pm = AppGlobals.getPackageManager(); |
| 482 | for (int i = policy.mAdminList.size() - 1; i >= 0; i--) { |
| 483 | ActiveAdmin aa = policy.mAdminList.get(i); |
| 484 | try { |
| 485 | if (pm.getPackageInfo(aa.info.getPackageName(), 0, userHandle) == null |
| 486 | || pm.getReceiverInfo(aa.info.getComponent(), 0, userHandle) == null) { |
| 487 | removed = true; |
| 488 | policy.mAdminList.remove(i); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 489 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 490 | } catch (RemoteException re) { |
| 491 | // Shouldn't happen |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 492 | } |
| 493 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 494 | if (removed) { |
| 495 | validatePasswordOwnerLocked(policy); |
| 496 | syncDeviceCapabilitiesLocked(policy); |
| 497 | saveSettingsLocked(policy.mUserHandle); |
| 498 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 499 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 500 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 501 | /** |
| 502 | * Instantiates the service. |
| 503 | */ |
| 504 | public DevicePolicyManagerService(Context context) { |
| 505 | mContext = context; |
Dianne Hackborn | 4249917 | 2010-10-15 18:45:07 -0700 | [diff] [blame] | 506 | mWakeLock = ((PowerManager)context.getSystemService(Context.POWER_SERVICE)) |
| 507 | .newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "DPM"); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 508 | IntentFilter filter = new IntentFilter(); |
| 509 | filter.addAction(Intent.ACTION_BOOT_COMPLETED); |
| 510 | filter.addAction(ACTION_EXPIRED_PASSWORD_NOTIFICATION); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 511 | filter.addAction(Intent.ACTION_USER_REMOVED); |
| 512 | filter.addAction(Intent.ACTION_USER_STARTED); |
| 513 | context.registerReceiverAsUser(mReceiver, UserHandle.ALL, filter, null, mHandler); |
| 514 | filter = new IntentFilter(); |
| 515 | filter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
| 516 | filter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 517 | filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 518 | filter.addAction(Intent.ACTION_PACKAGE_ADDED); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 519 | filter.addDataScheme("package"); |
| 520 | context.registerReceiverAsUser(mReceiver, UserHandle.ALL, filter, null, mHandler); |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * Creates and loads the policy data from xml. |
| 525 | * @param userHandle the user for whom to load the policy data |
| 526 | * @return |
| 527 | */ |
| 528 | DevicePolicyData getUserData(int userHandle) { |
| 529 | synchronized (this) { |
| 530 | DevicePolicyData policy = mUserData.get(userHandle); |
| 531 | if (policy == null) { |
| 532 | policy = new DevicePolicyData(userHandle); |
| 533 | mUserData.append(userHandle, policy); |
| 534 | loadSettingsLocked(policy, userHandle); |
| 535 | } |
| 536 | return policy; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | void removeUserData(int userHandle) { |
| 541 | synchronized (this) { |
| 542 | if (userHandle == UserHandle.USER_OWNER) { |
| 543 | Slog.w(TAG, "Tried to remove device policy file for user 0! Ignoring."); |
| 544 | return; |
| 545 | } |
| 546 | DevicePolicyData policy = mUserData.get(userHandle); |
| 547 | if (policy != null) { |
| 548 | mUserData.remove(userHandle); |
| 549 | } |
| 550 | File policyFile = new File(Environment.getUserSystemDirectory(userHandle), |
| 551 | DEVICE_POLICIES_XML); |
| 552 | policyFile.delete(); |
| 553 | Slog.i(TAG, "Removed device policy file " + policyFile.getAbsolutePath()); |
| 554 | } |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 555 | } |
| 556 | |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 557 | void loadDeviceOwner() { |
| 558 | synchronized (this) { |
| 559 | if (DeviceOwner.isRegistered()) { |
| 560 | mDeviceOwner = new DeviceOwner(); |
| 561 | } |
| 562 | } |
| 563 | } |
| 564 | |
Andy Stadler | 043116a | 2010-11-29 17:43:32 -0800 | [diff] [blame] | 565 | /** |
| 566 | * Set an alarm for an upcoming event - expiration warning, expiration, or post-expiration |
| 567 | * reminders. Clears alarm if no expirations are configured. |
| 568 | */ |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 569 | protected void setExpirationAlarmCheckLocked(Context context, DevicePolicyData policy) { |
| 570 | final long expiration = getPasswordExpirationLocked(null, policy.mUserHandle); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 571 | final long now = System.currentTimeMillis(); |
| 572 | final long timeToExpire = expiration - now; |
| 573 | final long alarmTime; |
Andy Stadler | 043116a | 2010-11-29 17:43:32 -0800 | [diff] [blame] | 574 | if (expiration == 0) { |
| 575 | // No expirations are currently configured: Cancel alarm. |
| 576 | alarmTime = 0; |
| 577 | } else if (timeToExpire <= 0) { |
| 578 | // The password has already expired: Repeat every 24 hours. |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 579 | alarmTime = now + MS_PER_DAY; |
Andy Stadler | 043116a | 2010-11-29 17:43:32 -0800 | [diff] [blame] | 580 | } else { |
| 581 | // Selecting the next alarm time: Roll forward to the next 24 hour multiple before |
| 582 | // the expiration time. |
| 583 | long alarmInterval = timeToExpire % MS_PER_DAY; |
| 584 | if (alarmInterval == 0) { |
| 585 | alarmInterval = MS_PER_DAY; |
| 586 | } |
| 587 | alarmTime = now + alarmInterval; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 588 | } |
| 589 | |
Andy Stadler | 1f35d48 | 2010-11-19 15:39:41 -0800 | [diff] [blame] | 590 | long token = Binder.clearCallingIdentity(); |
| 591 | try { |
| 592 | AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 593 | PendingIntent pi = PendingIntent.getBroadcastAsUser(context, REQUEST_EXPIRE_PASSWORD, |
Andy Stadler | 1f35d48 | 2010-11-19 15:39:41 -0800 | [diff] [blame] | 594 | new Intent(ACTION_EXPIRED_PASSWORD_NOTIFICATION), |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 595 | PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT, |
| 596 | new UserHandle(policy.mUserHandle)); |
Andy Stadler | 1f35d48 | 2010-11-19 15:39:41 -0800 | [diff] [blame] | 597 | am.cancel(pi); |
Andy Stadler | 043116a | 2010-11-29 17:43:32 -0800 | [diff] [blame] | 598 | if (alarmTime != 0) { |
| 599 | am.set(AlarmManager.RTC, alarmTime, pi); |
| 600 | } |
Andy Stadler | 1f35d48 | 2010-11-19 15:39:41 -0800 | [diff] [blame] | 601 | } finally { |
| 602 | Binder.restoreCallingIdentity(token); |
| 603 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 604 | } |
| 605 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 606 | private IPowerManager getIPowerManager() { |
| 607 | if (mIPowerManager == null) { |
| 608 | IBinder b = ServiceManager.getService(Context.POWER_SERVICE); |
| 609 | mIPowerManager = IPowerManager.Stub.asInterface(b); |
| 610 | } |
| 611 | return mIPowerManager; |
| 612 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 613 | |
Jim Miller | 93c518e | 2012-01-17 15:55:31 -0800 | [diff] [blame] | 614 | private IWindowManager getWindowManager() { |
| 615 | if (mIWindowManager == null) { |
| 616 | IBinder b = ServiceManager.getService(Context.WINDOW_SERVICE); |
| 617 | mIWindowManager = IWindowManager.Stub.asInterface(b); |
| 618 | } |
| 619 | return mIWindowManager; |
| 620 | } |
| 621 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 622 | ActiveAdmin getActiveAdminUncheckedLocked(ComponentName who, int userHandle) { |
| 623 | ActiveAdmin admin = getUserData(userHandle).mAdminMap.get(who); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 624 | if (admin != null |
| 625 | && who.getPackageName().equals(admin.info.getActivityInfo().packageName) |
| 626 | && who.getClassName().equals(admin.info.getActivityInfo().name)) { |
| 627 | return admin; |
| 628 | } |
| 629 | return null; |
| 630 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 631 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 632 | ActiveAdmin getActiveAdminForCallerLocked(ComponentName who, int reqPolicy) |
| 633 | throws SecurityException { |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 634 | final int callingUid = Binder.getCallingUid(); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 635 | final int userHandle = UserHandle.getUserId(callingUid); |
| 636 | final DevicePolicyData policy = getUserData(userHandle); |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 637 | if (who != null) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 638 | ActiveAdmin admin = policy.mAdminMap.get(who); |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 639 | if (admin == null) { |
| 640 | throw new SecurityException("No active admin " + who); |
| 641 | } |
| 642 | if (admin.getUid() != callingUid) { |
| 643 | throw new SecurityException("Admin " + who + " is not owned by uid " |
| 644 | + Binder.getCallingUid()); |
| 645 | } |
| 646 | if (!admin.info.usesPolicy(reqPolicy)) { |
| 647 | throw new SecurityException("Admin " + admin.info.getComponent() |
| 648 | + " did not specify uses-policy for: " |
| 649 | + admin.info.getTagForPolicy(reqPolicy)); |
| 650 | } |
| 651 | return admin; |
| 652 | } else { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 653 | final int N = policy.mAdminList.size(); |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 654 | for (int i=0; i<N; i++) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 655 | ActiveAdmin admin = policy.mAdminList.get(i); |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 656 | if (admin.getUid() == callingUid && admin.info.usesPolicy(reqPolicy)) { |
| 657 | return admin; |
| 658 | } |
| 659 | } |
| 660 | throw new SecurityException("No active admin owned by uid " |
| 661 | + Binder.getCallingUid() + " for policy #" + reqPolicy); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 662 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 663 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 664 | |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 665 | void sendAdminCommandLocked(ActiveAdmin admin, String action) { |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 666 | sendAdminCommandLocked(admin, action, null); |
| 667 | } |
| 668 | |
| 669 | void sendAdminCommandLocked(ActiveAdmin admin, String action, BroadcastReceiver result) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 670 | Intent intent = new Intent(action); |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 671 | intent.setComponent(admin.info.getComponent()); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 672 | if (action.equals(DeviceAdminReceiver.ACTION_PASSWORD_EXPIRING)) { |
| 673 | intent.putExtra("expiration", admin.passwordExpirationDate); |
| 674 | } |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 675 | if (result != null) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 676 | mContext.sendOrderedBroadcastAsUser(intent, admin.getUserHandle(), |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 677 | null, result, mHandler, Activity.RESULT_OK, null, null); |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 678 | } else { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 679 | mContext.sendBroadcastAsUser(intent, UserHandle.OWNER); |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 680 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 681 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 682 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 683 | void sendAdminCommandLocked(String action, int reqPolicy, int userHandle) { |
| 684 | final DevicePolicyData policy = getUserData(userHandle); |
| 685 | final int count = policy.mAdminList.size(); |
| 686 | if (count > 0) { |
| 687 | for (int i = 0; i < count; i++) { |
| 688 | ActiveAdmin admin = policy.mAdminList.get(i); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 689 | if (admin.info.usesPolicy(reqPolicy)) { |
| 690 | sendAdminCommandLocked(admin, action); |
| 691 | } |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 692 | } |
Dianne Hackborn | 4141d03 | 2010-01-21 16:29:00 -0800 | [diff] [blame] | 693 | } |
| 694 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 695 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 696 | void removeActiveAdminLocked(final ComponentName adminReceiver, int userHandle) { |
| 697 | final ActiveAdmin admin = getActiveAdminUncheckedLocked(adminReceiver, userHandle); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 698 | if (admin != null) { |
| 699 | sendAdminCommandLocked(admin, |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 700 | DeviceAdminReceiver.ACTION_DEVICE_ADMIN_DISABLED, |
| 701 | new BroadcastReceiver() { |
| 702 | @Override |
| 703 | public void onReceive(Context context, Intent intent) { |
Dianne Hackborn | 2fe8fb2 | 2012-06-15 17:05:25 -0700 | [diff] [blame] | 704 | synchronized (DevicePolicyManagerService.this) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 705 | int userHandle = admin.getUserHandle().getIdentifier(); |
| 706 | DevicePolicyData policy = getUserData(userHandle); |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 707 | boolean doProxyCleanup = admin.info.usesPolicy( |
| 708 | DeviceAdminInfo.USES_POLICY_SETS_GLOBAL_PROXY); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 709 | policy.mAdminList.remove(admin); |
| 710 | policy.mAdminMap.remove(adminReceiver); |
| 711 | validatePasswordOwnerLocked(policy); |
| 712 | syncDeviceCapabilitiesLocked(policy); |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 713 | if (doProxyCleanup) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 714 | resetGlobalProxyLocked(getUserData(userHandle)); |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 715 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 716 | saveSettingsLocked(userHandle); |
| 717 | updateMaximumTimeToLockLocked(policy); |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 718 | } |
| 719 | } |
| 720 | }); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 721 | } |
| 722 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 723 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 724 | public DeviceAdminInfo findAdmin(ComponentName adminName, int userHandle) { |
| 725 | enforceCrossUserPermission(userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 726 | Intent resolveIntent = new Intent(); |
| 727 | resolveIntent.setComponent(adminName); |
| 728 | List<ResolveInfo> infos = mContext.getPackageManager().queryBroadcastReceivers( |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 729 | resolveIntent, |
| 730 | PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS, |
| 731 | userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 732 | if (infos == null || infos.size() <= 0) { |
| 733 | throw new IllegalArgumentException("Unknown admin: " + adminName); |
| 734 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 735 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 736 | try { |
| 737 | return new DeviceAdminInfo(mContext, infos.get(0)); |
| 738 | } catch (XmlPullParserException e) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 739 | Slog.w(TAG, "Bad device admin requested for user=" + userHandle + ": " + adminName, e); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 740 | return null; |
| 741 | } catch (IOException e) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 742 | Slog.w(TAG, "Bad device admin requested for user=" + userHandle + ": " + adminName, e); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 743 | return null; |
| 744 | } |
| 745 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 746 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 747 | private static JournaledFile makeJournaledFile(int userHandle) { |
| 748 | final String base = userHandle == 0 |
| 749 | ? "/data/system/" + DEVICE_POLICIES_XML |
| 750 | : new File(Environment.getUserSystemDirectory(userHandle), DEVICE_POLICIES_XML) |
| 751 | .getAbsolutePath(); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 752 | return new JournaledFile(new File(base), new File(base + ".tmp")); |
| 753 | } |
| 754 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 755 | private void saveSettingsLocked(int userHandle) { |
| 756 | DevicePolicyData policy = getUserData(userHandle); |
| 757 | JournaledFile journal = makeJournaledFile(userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 758 | FileOutputStream stream = null; |
| 759 | try { |
| 760 | stream = new FileOutputStream(journal.chooseForWrite(), false); |
| 761 | XmlSerializer out = new FastXmlSerializer(); |
| 762 | out.setOutput(stream, "utf-8"); |
| 763 | out.startDocument(null, true); |
| 764 | |
| 765 | out.startTag(null, "policies"); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 766 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 767 | final int N = policy.mAdminList.size(); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 768 | for (int i=0; i<N; i++) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 769 | ActiveAdmin ap = policy.mAdminList.get(i); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 770 | if (ap != null) { |
| 771 | out.startTag(null, "admin"); |
| 772 | out.attribute(null, "name", ap.info.getComponent().flattenToString()); |
| 773 | ap.writeToXml(out); |
| 774 | out.endTag(null, "admin"); |
| 775 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 776 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 777 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 778 | if (policy.mPasswordOwner >= 0) { |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 779 | out.startTag(null, "password-owner"); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 780 | out.attribute(null, "value", Integer.toString(policy.mPasswordOwner)); |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 781 | out.endTag(null, "password-owner"); |
| 782 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 783 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 784 | if (policy.mFailedPasswordAttempts != 0) { |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 785 | out.startTag(null, "failed-password-attempts"); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 786 | out.attribute(null, "value", Integer.toString(policy.mFailedPasswordAttempts)); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 787 | out.endTag(null, "failed-password-attempts"); |
| 788 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 789 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 790 | if (policy.mActivePasswordQuality != 0 || policy.mActivePasswordLength != 0 |
| 791 | || policy.mActivePasswordUpperCase != 0 || policy.mActivePasswordLowerCase != 0 |
| 792 | || policy.mActivePasswordLetters != 0 || policy.mActivePasswordNumeric != 0 |
| 793 | || policy.mActivePasswordSymbols != 0 || policy.mActivePasswordNonLetter != 0) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 794 | out.startTag(null, "active-password"); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 795 | out.attribute(null, "quality", Integer.toString(policy.mActivePasswordQuality)); |
| 796 | out.attribute(null, "length", Integer.toString(policy.mActivePasswordLength)); |
| 797 | out.attribute(null, "uppercase", Integer.toString(policy.mActivePasswordUpperCase)); |
| 798 | out.attribute(null, "lowercase", Integer.toString(policy.mActivePasswordLowerCase)); |
| 799 | out.attribute(null, "letters", Integer.toString(policy.mActivePasswordLetters)); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 800 | out.attribute(null, "numeric", Integer |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 801 | .toString(policy.mActivePasswordNumeric)); |
| 802 | out.attribute(null, "symbols", Integer.toString(policy.mActivePasswordSymbols)); |
| 803 | out.attribute(null, "nonletter", Integer.toString(policy.mActivePasswordNonLetter)); |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 804 | out.endTag(null, "active-password"); |
| 805 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 806 | |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 807 | out.endTag(null, "policies"); |
| 808 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 809 | out.endDocument(); |
| 810 | stream.close(); |
| 811 | journal.commit(); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 812 | sendChangedNotification(userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 813 | } catch (IOException e) { |
| 814 | try { |
| 815 | if (stream != null) { |
| 816 | stream.close(); |
| 817 | } |
| 818 | } catch (IOException ex) { |
| 819 | // Ignore |
| 820 | } |
| 821 | journal.rollback(); |
| 822 | } |
| 823 | } |
| 824 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 825 | private void sendChangedNotification(int userHandle) { |
Jim Miller | 284b62e | 2010-06-08 14:27:42 -0700 | [diff] [blame] | 826 | Intent intent = new Intent(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED); |
| 827 | intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
Amith Yamasani | b7a6859 | 2012-09-10 10:24:36 -0700 | [diff] [blame] | 828 | long ident = Binder.clearCallingIdentity(); |
| 829 | try { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 830 | mContext.sendBroadcastAsUser(intent, new UserHandle(userHandle)); |
Amith Yamasani | b7a6859 | 2012-09-10 10:24:36 -0700 | [diff] [blame] | 831 | } finally { |
| 832 | Binder.restoreCallingIdentity(ident); |
| 833 | } |
Jim Miller | 284b62e | 2010-06-08 14:27:42 -0700 | [diff] [blame] | 834 | } |
| 835 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 836 | private void loadSettingsLocked(DevicePolicyData policy, int userHandle) { |
| 837 | JournaledFile journal = makeJournaledFile(userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 838 | FileInputStream stream = null; |
| 839 | File file = journal.chooseForRead(); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 840 | try { |
| 841 | stream = new FileInputStream(file); |
| 842 | XmlPullParser parser = Xml.newPullParser(); |
| 843 | parser.setInput(stream, null); |
| 844 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 845 | int type; |
| 846 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 847 | && type != XmlPullParser.START_TAG) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 848 | } |
| 849 | String tag = parser.getName(); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 850 | if (!"policies".equals(tag)) { |
| 851 | throw new XmlPullParserException( |
| 852 | "Settings do not start with policies tag: found " + tag); |
| 853 | } |
| 854 | type = parser.next(); |
| 855 | int outerDepth = parser.getDepth(); |
| 856 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 857 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 858 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 859 | continue; |
| 860 | } |
| 861 | tag = parser.getName(); |
| 862 | if ("admin".equals(tag)) { |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 863 | String name = parser.getAttributeValue(null, "name"); |
| 864 | try { |
| 865 | DeviceAdminInfo dai = findAdmin( |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 866 | ComponentName.unflattenFromString(name), userHandle); |
| 867 | if (DBG && (UserHandle.getUserId(dai.getActivityInfo().applicationInfo.uid) |
| 868 | != userHandle)) { |
| 869 | Slog.w(TAG, "findAdmin returned an incorrect uid " |
| 870 | + dai.getActivityInfo().applicationInfo.uid + " for user " |
| 871 | + userHandle); |
| 872 | } |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 873 | if (dai != null) { |
| 874 | ActiveAdmin ap = new ActiveAdmin(dai); |
| 875 | ap.readFromXml(parser); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 876 | policy.mAdminMap.put(ap.info.getComponent(), ap); |
| 877 | policy.mAdminList.add(ap); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 878 | } |
| 879 | } catch (RuntimeException e) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 880 | Slog.w(TAG, "Failed loading admin " + name, e); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 881 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 882 | } else if ("failed-password-attempts".equals(tag)) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 883 | policy.mFailedPasswordAttempts = Integer.parseInt( |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 884 | parser.getAttributeValue(null, "value")); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 885 | XmlUtils.skipCurrentTag(parser); |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 886 | } else if ("password-owner".equals(tag)) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 887 | policy.mPasswordOwner = Integer.parseInt( |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 888 | parser.getAttributeValue(null, "value")); |
| 889 | XmlUtils.skipCurrentTag(parser); |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 890 | } else if ("active-password".equals(tag)) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 891 | policy.mActivePasswordQuality = Integer.parseInt( |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 892 | parser.getAttributeValue(null, "quality")); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 893 | policy.mActivePasswordLength = Integer.parseInt( |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 894 | parser.getAttributeValue(null, "length")); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 895 | policy.mActivePasswordUpperCase = Integer.parseInt( |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 896 | parser.getAttributeValue(null, "uppercase")); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 897 | policy.mActivePasswordLowerCase = Integer.parseInt( |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 898 | parser.getAttributeValue(null, "lowercase")); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 899 | policy.mActivePasswordLetters = Integer.parseInt( |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 900 | parser.getAttributeValue(null, "letters")); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 901 | policy.mActivePasswordNumeric = Integer.parseInt( |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 902 | parser.getAttributeValue(null, "numeric")); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 903 | policy.mActivePasswordSymbols = Integer.parseInt( |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 904 | parser.getAttributeValue(null, "symbols")); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 905 | policy.mActivePasswordNonLetter = Integer.parseInt( |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 906 | parser.getAttributeValue(null, "nonletter")); |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 907 | XmlUtils.skipCurrentTag(parser); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 908 | } else { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 909 | Slog.w(TAG, "Unknown tag: " + tag); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 910 | XmlUtils.skipCurrentTag(parser); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 911 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 912 | } |
| 913 | } catch (NullPointerException e) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 914 | Slog.w(TAG, "failed parsing " + file + " " + e); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 915 | } catch (NumberFormatException e) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 916 | Slog.w(TAG, "failed parsing " + file + " " + e); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 917 | } catch (XmlPullParserException e) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 918 | Slog.w(TAG, "failed parsing " + file + " " + e); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 919 | } catch (FileNotFoundException e) { |
| 920 | // 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] | 921 | } catch (IOException e) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 922 | Slog.w(TAG, "failed parsing " + file + " " + e); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 923 | } catch (IndexOutOfBoundsException e) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 924 | Slog.w(TAG, "failed parsing " + file + " " + e); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 925 | } |
| 926 | try { |
| 927 | if (stream != null) { |
| 928 | stream.close(); |
| 929 | } |
| 930 | } catch (IOException e) { |
| 931 | // Ignore |
| 932 | } |
| 933 | |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 934 | // Validate that what we stored for the password quality matches |
| 935 | // sufficiently what is currently set. Note that this is only |
| 936 | // a sanity check in case the two get out of sync; this should |
| 937 | // never normally happen. |
| 938 | LockPatternUtils utils = new LockPatternUtils(mContext); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 939 | if (utils.getActivePasswordQuality() < policy.mActivePasswordQuality) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 940 | Slog.w(TAG, "Active password quality 0x" |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 941 | + Integer.toHexString(policy.mActivePasswordQuality) |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 942 | + " does not match actual quality 0x" |
| 943 | + Integer.toHexString(utils.getActivePasswordQuality())); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 944 | policy.mActivePasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; |
| 945 | policy.mActivePasswordLength = 0; |
| 946 | policy.mActivePasswordUpperCase = 0; |
| 947 | policy.mActivePasswordLowerCase = 0; |
| 948 | policy.mActivePasswordLetters = 0; |
| 949 | policy.mActivePasswordNumeric = 0; |
| 950 | policy.mActivePasswordSymbols = 0; |
| 951 | policy.mActivePasswordNonLetter = 0; |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 952 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 953 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 954 | validatePasswordOwnerLocked(policy); |
| 955 | syncDeviceCapabilitiesLocked(policy); |
| 956 | updateMaximumTimeToLockLocked(policy); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 957 | } |
| 958 | |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 959 | static void validateQualityConstant(int quality) { |
| 960 | switch (quality) { |
| 961 | case DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED: |
Danielle Millett | de7a2f3 | 2011-12-21 17:02:32 -0500 | [diff] [blame] | 962 | case DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK: |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 963 | case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING: |
| 964 | case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC: |
| 965 | case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC: |
| 966 | case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC: |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 967 | case DevicePolicyManager.PASSWORD_QUALITY_COMPLEX: |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 968 | return; |
| 969 | } |
| 970 | throw new IllegalArgumentException("Invalid quality constant: 0x" |
| 971 | + Integer.toHexString(quality)); |
| 972 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 973 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 974 | void validatePasswordOwnerLocked(DevicePolicyData policy) { |
| 975 | if (policy.mPasswordOwner >= 0) { |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 976 | boolean haveOwner = false; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 977 | for (int i = policy.mAdminList.size() - 1; i >= 0; i--) { |
| 978 | if (policy.mAdminList.get(i).getUid() == policy.mPasswordOwner) { |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 979 | haveOwner = true; |
| 980 | break; |
| 981 | } |
| 982 | } |
| 983 | if (!haveOwner) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 984 | Slog.w(TAG, "Previous password owner " + policy.mPasswordOwner |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 985 | + " no longer active; disabling"); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 986 | policy.mPasswordOwner = -1; |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 987 | } |
| 988 | } |
| 989 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 990 | |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 991 | /** |
| 992 | * Pushes down policy information to the system for any policies related to general device |
| 993 | * capabilities that need to be enforced by lower level services (e.g. Camera services). |
| 994 | */ |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 995 | void syncDeviceCapabilitiesLocked(DevicePolicyData policy) { |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 996 | // Ensure the status of the camera is synced down to the system. Interested native services |
| 997 | // should monitor this value and act accordingly. |
| 998 | boolean systemState = SystemProperties.getBoolean(SYSTEM_PROP_DISABLE_CAMERA, false); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 999 | boolean cameraDisabled = getCameraDisabled(null, policy.mUserHandle); |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 1000 | if (cameraDisabled != systemState) { |
| 1001 | long token = Binder.clearCallingIdentity(); |
| 1002 | try { |
| 1003 | String value = cameraDisabled ? "1" : "0"; |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 1004 | if (DBG) Slog.v(TAG, "Change in camera state [" |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 1005 | + SYSTEM_PROP_DISABLE_CAMERA + "] = " + value); |
| 1006 | SystemProperties.set(SYSTEM_PROP_DISABLE_CAMERA, value); |
| 1007 | } finally { |
| 1008 | Binder.restoreCallingIdentity(token); |
| 1009 | } |
| 1010 | } |
| 1011 | } |
| 1012 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1013 | public void systemReady() { |
| 1014 | synchronized (this) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1015 | loadSettingsLocked(getUserData(UserHandle.USER_OWNER), UserHandle.USER_OWNER); |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 1016 | loadDeviceOwner(); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1017 | } |
| 1018 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1019 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1020 | private void handlePasswordExpirationNotification(DevicePolicyData policy) { |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1021 | synchronized (this) { |
| 1022 | final long now = System.currentTimeMillis(); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1023 | final int N = policy.mAdminList.size(); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1024 | if (N <= 0) { |
| 1025 | return; |
| 1026 | } |
| 1027 | for (int i=0; i < N; i++) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1028 | ActiveAdmin admin = policy.mAdminList.get(i); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1029 | if (admin.info.usesPolicy(DeviceAdminInfo.USES_POLICY_EXPIRE_PASSWORD) |
| 1030 | && admin.passwordExpirationTimeout > 0L |
| 1031 | && admin.passwordExpirationDate > 0L |
Andy Stadler | 043116a | 2010-11-29 17:43:32 -0800 | [diff] [blame] | 1032 | && now >= admin.passwordExpirationDate - EXPIRATION_GRACE_PERIOD_MS) { |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1033 | sendAdminCommandLocked(admin, DeviceAdminReceiver.ACTION_PASSWORD_EXPIRING); |
| 1034 | } |
| 1035 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1036 | setExpirationAlarmCheckLocked(mContext, policy); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1037 | } |
| 1038 | } |
| 1039 | |
Andy Stadler | c25f70a | 2010-12-08 15:56:45 -0800 | [diff] [blame] | 1040 | /** |
| 1041 | * @param adminReceiver The admin to add |
| 1042 | * @param refreshing true = update an active admin, no error |
| 1043 | */ |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1044 | public void setActiveAdmin(ComponentName adminReceiver, boolean refreshing, int userHandle) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1045 | mContext.enforceCallingOrSelfPermission( |
| 1046 | android.Manifest.permission.BIND_DEVICE_ADMIN, null); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1047 | enforceCrossUserPermission(userHandle); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1048 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1049 | DevicePolicyData policy = getUserData(userHandle); |
| 1050 | DeviceAdminInfo info = findAdmin(adminReceiver, userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1051 | if (info == null) { |
| 1052 | throw new IllegalArgumentException("Bad admin: " + adminReceiver); |
| 1053 | } |
| 1054 | synchronized (this) { |
| 1055 | long ident = Binder.clearCallingIdentity(); |
| 1056 | try { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1057 | if (!refreshing && getActiveAdminUncheckedLocked(adminReceiver, userHandle) != null) { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1058 | throw new IllegalArgumentException("Admin is already added"); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1059 | } |
Andy Stadler | c25f70a | 2010-12-08 15:56:45 -0800 | [diff] [blame] | 1060 | ActiveAdmin newAdmin = new ActiveAdmin(info); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1061 | policy.mAdminMap.put(adminReceiver, newAdmin); |
Andy Stadler | c25f70a | 2010-12-08 15:56:45 -0800 | [diff] [blame] | 1062 | int replaceIndex = -1; |
| 1063 | if (refreshing) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1064 | final int N = policy.mAdminList.size(); |
Andy Stadler | c25f70a | 2010-12-08 15:56:45 -0800 | [diff] [blame] | 1065 | for (int i=0; i < N; i++) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1066 | ActiveAdmin oldAdmin = policy.mAdminList.get(i); |
Andy Stadler | c25f70a | 2010-12-08 15:56:45 -0800 | [diff] [blame] | 1067 | if (oldAdmin.info.getComponent().equals(adminReceiver)) { |
| 1068 | replaceIndex = i; |
| 1069 | break; |
| 1070 | } |
| 1071 | } |
| 1072 | } |
| 1073 | if (replaceIndex == -1) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1074 | policy.mAdminList.add(newAdmin); |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 1075 | enableIfNecessary(info.getPackageName(), userHandle); |
Andy Stadler | c25f70a | 2010-12-08 15:56:45 -0800 | [diff] [blame] | 1076 | } else { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1077 | policy.mAdminList.set(replaceIndex, newAdmin); |
Andy Stadler | c25f70a | 2010-12-08 15:56:45 -0800 | [diff] [blame] | 1078 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1079 | saveSettingsLocked(userHandle); |
Andy Stadler | c25f70a | 2010-12-08 15:56:45 -0800 | [diff] [blame] | 1080 | sendAdminCommandLocked(newAdmin, DeviceAdminReceiver.ACTION_DEVICE_ADMIN_ENABLED); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1081 | } finally { |
| 1082 | Binder.restoreCallingIdentity(ident); |
| 1083 | } |
| 1084 | } |
| 1085 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1086 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1087 | public boolean isAdminActive(ComponentName adminReceiver, int userHandle) { |
| 1088 | enforceCrossUserPermission(userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1089 | synchronized (this) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1090 | return getActiveAdminUncheckedLocked(adminReceiver, userHandle) != null; |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1091 | } |
| 1092 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1093 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1094 | public boolean hasGrantedPolicy(ComponentName adminReceiver, int policyId, int userHandle) { |
| 1095 | enforceCrossUserPermission(userHandle); |
Andy Stadler | c25f70a | 2010-12-08 15:56:45 -0800 | [diff] [blame] | 1096 | synchronized (this) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1097 | ActiveAdmin administrator = getActiveAdminUncheckedLocked(adminReceiver, userHandle); |
Andy Stadler | c25f70a | 2010-12-08 15:56:45 -0800 | [diff] [blame] | 1098 | if (administrator == null) { |
| 1099 | throw new SecurityException("No active admin " + adminReceiver); |
| 1100 | } |
| 1101 | return administrator.info.usesPolicy(policyId); |
| 1102 | } |
| 1103 | } |
| 1104 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1105 | public List<ComponentName> getActiveAdmins(int userHandle) { |
| 1106 | enforceCrossUserPermission(userHandle); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1107 | synchronized (this) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1108 | DevicePolicyData policy = getUserData(userHandle); |
| 1109 | final int N = policy.mAdminList.size(); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1110 | if (N <= 0) { |
| 1111 | return null; |
| 1112 | } |
| 1113 | ArrayList<ComponentName> res = new ArrayList<ComponentName>(N); |
| 1114 | for (int i=0; i<N; i++) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1115 | res.add(policy.mAdminList.get(i).info.getComponent()); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1116 | } |
| 1117 | return res; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1118 | } |
| 1119 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1120 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1121 | public boolean packageHasActiveAdmins(String packageName, int userHandle) { |
| 1122 | enforceCrossUserPermission(userHandle); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1123 | synchronized (this) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1124 | DevicePolicyData policy = getUserData(userHandle); |
| 1125 | final int N = policy.mAdminList.size(); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1126 | for (int i=0; i<N; i++) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1127 | if (policy.mAdminList.get(i).info.getPackageName().equals(packageName)) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1128 | return true; |
| 1129 | } |
| 1130 | } |
| 1131 | return false; |
| 1132 | } |
| 1133 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1134 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1135 | public void removeActiveAdmin(ComponentName adminReceiver, int userHandle) { |
| 1136 | enforceCrossUserPermission(userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1137 | synchronized (this) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1138 | ActiveAdmin admin = getActiveAdminUncheckedLocked(adminReceiver, userHandle); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1139 | if (admin == null) { |
| 1140 | return; |
| 1141 | } |
| 1142 | if (admin.getUid() != Binder.getCallingUid()) { |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 1143 | // If trying to remove device owner, refuse when the caller is not the owner. |
| 1144 | if (mDeviceOwner != null |
| 1145 | && adminReceiver.getPackageName().equals(mDeviceOwner.getPackageName())) { |
| 1146 | return; |
| 1147 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1148 | mContext.enforceCallingOrSelfPermission( |
| 1149 | android.Manifest.permission.BIND_DEVICE_ADMIN, null); |
| 1150 | } |
| 1151 | long ident = Binder.clearCallingIdentity(); |
| 1152 | try { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1153 | removeActiveAdminLocked(adminReceiver, userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1154 | } finally { |
| 1155 | Binder.restoreCallingIdentity(ident); |
| 1156 | } |
| 1157 | } |
| 1158 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1159 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1160 | public void setPasswordQuality(ComponentName who, int quality, int userHandle) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1161 | validateQualityConstant(quality); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1162 | enforceCrossUserPermission(userHandle); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1163 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1164 | synchronized (this) { |
| 1165 | if (who == null) { |
| 1166 | throw new NullPointerException("ComponentName is null"); |
| 1167 | } |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 1168 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 1169 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1170 | if (ap.passwordQuality != quality) { |
| 1171 | ap.passwordQuality = quality; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1172 | saveSettingsLocked(userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1173 | } |
| 1174 | } |
| 1175 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1176 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1177 | public int getPasswordQuality(ComponentName who, int userHandle) { |
| 1178 | enforceCrossUserPermission(userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1179 | synchronized (this) { |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 1180 | int mode = DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1181 | DevicePolicyData policy = getUserData(userHandle); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1182 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1183 | if (who != null) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1184 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who, userHandle); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 1185 | return admin != null ? admin.passwordQuality : mode; |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1186 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1187 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1188 | final int N = policy.mAdminList.size(); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1189 | for (int i=0; i<N; i++) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1190 | ActiveAdmin admin = policy.mAdminList.get(i); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 1191 | if (mode < admin.passwordQuality) { |
| 1192 | mode = admin.passwordQuality; |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1193 | } |
| 1194 | } |
| 1195 | return mode; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1196 | } |
| 1197 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1198 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1199 | public void setPasswordMinimumLength(ComponentName who, int length, int userHandle) { |
| 1200 | enforceCrossUserPermission(userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1201 | synchronized (this) { |
| 1202 | if (who == null) { |
| 1203 | throw new NullPointerException("ComponentName is null"); |
| 1204 | } |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 1205 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 1206 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1207 | if (ap.minimumPasswordLength != length) { |
| 1208 | ap.minimumPasswordLength = length; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1209 | saveSettingsLocked(userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1210 | } |
| 1211 | } |
| 1212 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1213 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1214 | public int getPasswordMinimumLength(ComponentName who, int userHandle) { |
| 1215 | enforceCrossUserPermission(userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1216 | synchronized (this) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1217 | DevicePolicyData policy = getUserData(userHandle); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1218 | int length = 0; |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1219 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1220 | if (who != null) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1221 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who, userHandle); |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1222 | return admin != null ? admin.minimumPasswordLength : length; |
| 1223 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1224 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1225 | final int N = policy.mAdminList.size(); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1226 | for (int i=0; i<N; i++) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1227 | ActiveAdmin admin = policy.mAdminList.get(i); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1228 | if (length < admin.minimumPasswordLength) { |
| 1229 | length = admin.minimumPasswordLength; |
| 1230 | } |
| 1231 | } |
| 1232 | return length; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1233 | } |
| 1234 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1235 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1236 | public void setPasswordHistoryLength(ComponentName who, int length, int userHandle) { |
| 1237 | enforceCrossUserPermission(userHandle); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1238 | synchronized (this) { |
| 1239 | if (who == null) { |
| 1240 | throw new NullPointerException("ComponentName is null"); |
| 1241 | } |
| 1242 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 1243 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
| 1244 | if (ap.passwordHistoryLength != length) { |
| 1245 | ap.passwordHistoryLength = length; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1246 | saveSettingsLocked(userHandle); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1247 | } |
| 1248 | } |
| 1249 | } |
| 1250 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1251 | public int getPasswordHistoryLength(ComponentName who, int userHandle) { |
| 1252 | enforceCrossUserPermission(userHandle); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1253 | synchronized (this) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1254 | DevicePolicyData policy = getUserData(userHandle); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1255 | int length = 0; |
| 1256 | |
| 1257 | if (who != null) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1258 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who, userHandle); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1259 | return admin != null ? admin.passwordHistoryLength : length; |
| 1260 | } |
| 1261 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1262 | final int N = policy.mAdminList.size(); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1263 | for (int i = 0; i < N; i++) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1264 | ActiveAdmin admin = policy.mAdminList.get(i); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1265 | if (length < admin.passwordHistoryLength) { |
| 1266 | length = admin.passwordHistoryLength; |
| 1267 | } |
| 1268 | } |
| 1269 | return length; |
| 1270 | } |
| 1271 | } |
| 1272 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1273 | public void setPasswordExpirationTimeout(ComponentName who, long timeout, int userHandle) { |
| 1274 | enforceCrossUserPermission(userHandle); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1275 | synchronized (this) { |
| 1276 | if (who == null) { |
| 1277 | throw new NullPointerException("ComponentName is null"); |
| 1278 | } |
Andy Stadler | 1f35d48 | 2010-11-19 15:39:41 -0800 | [diff] [blame] | 1279 | if (timeout < 0) { |
| 1280 | throw new IllegalArgumentException("Timeout must be >= 0 ms"); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1281 | } |
| 1282 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 1283 | DeviceAdminInfo.USES_POLICY_EXPIRE_PASSWORD); |
| 1284 | // Calling this API automatically bumps the expiration date |
| 1285 | final long expiration = timeout > 0L ? (timeout + System.currentTimeMillis()) : 0L; |
| 1286 | ap.passwordExpirationDate = expiration; |
| 1287 | ap.passwordExpirationTimeout = timeout; |
| 1288 | if (timeout > 0L) { |
| 1289 | Slog.w(TAG, "setPasswordExpiration(): password will expire on " |
| 1290 | + DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT) |
| 1291 | .format(new Date(expiration))); |
| 1292 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1293 | saveSettingsLocked(userHandle); |
| 1294 | // in case this is the first one |
| 1295 | setExpirationAlarmCheckLocked(mContext, getUserData(userHandle)); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1296 | } |
| 1297 | } |
| 1298 | |
Andy Stadler | 043116a | 2010-11-29 17:43:32 -0800 | [diff] [blame] | 1299 | /** |
| 1300 | * Return a single admin's expiration cycle time, or the min of all cycle times. |
| 1301 | * Returns 0 if not configured. |
| 1302 | */ |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1303 | public long getPasswordExpirationTimeout(ComponentName who, int userHandle) { |
| 1304 | enforceCrossUserPermission(userHandle); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1305 | synchronized (this) { |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1306 | if (who != null) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1307 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who, userHandle); |
Andy Stadler | 043116a | 2010-11-29 17:43:32 -0800 | [diff] [blame] | 1308 | return admin != null ? admin.passwordExpirationTimeout : 0L; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1309 | } |
| 1310 | |
Andy Stadler | 043116a | 2010-11-29 17:43:32 -0800 | [diff] [blame] | 1311 | long timeout = 0L; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1312 | DevicePolicyData policy = getUserData(userHandle); |
| 1313 | final int N = policy.mAdminList.size(); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1314 | for (int i = 0; i < N; i++) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1315 | ActiveAdmin admin = policy.mAdminList.get(i); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1316 | if (timeout == 0L || (admin.passwordExpirationTimeout != 0L |
| 1317 | && timeout > admin.passwordExpirationTimeout)) { |
| 1318 | timeout = admin.passwordExpirationTimeout; |
| 1319 | } |
| 1320 | } |
| 1321 | return timeout; |
| 1322 | } |
| 1323 | } |
| 1324 | |
Andy Stadler | 043116a | 2010-11-29 17:43:32 -0800 | [diff] [blame] | 1325 | /** |
| 1326 | * Return a single admin's expiration date/time, or the min (soonest) for all admins. |
| 1327 | * Returns 0 if not configured. |
| 1328 | */ |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1329 | private long getPasswordExpirationLocked(ComponentName who, int userHandle) { |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1330 | if (who != null) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1331 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who, userHandle); |
Andy Stadler | 043116a | 2010-11-29 17:43:32 -0800 | [diff] [blame] | 1332 | return admin != null ? admin.passwordExpirationDate : 0L; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1333 | } |
| 1334 | |
Andy Stadler | 043116a | 2010-11-29 17:43:32 -0800 | [diff] [blame] | 1335 | long timeout = 0L; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1336 | DevicePolicyData policy = getUserData(userHandle); |
| 1337 | final int N = policy.mAdminList.size(); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1338 | for (int i = 0; i < N; i++) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1339 | ActiveAdmin admin = policy.mAdminList.get(i); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1340 | if (timeout == 0L || (admin.passwordExpirationDate != 0 |
| 1341 | && timeout > admin.passwordExpirationDate)) { |
| 1342 | timeout = admin.passwordExpirationDate; |
| 1343 | } |
| 1344 | } |
| 1345 | return timeout; |
| 1346 | } |
| 1347 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1348 | public long getPasswordExpiration(ComponentName who, int userHandle) { |
| 1349 | enforceCrossUserPermission(userHandle); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1350 | synchronized (this) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1351 | return getPasswordExpirationLocked(who, userHandle); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1352 | } |
| 1353 | } |
| 1354 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1355 | public void setPasswordMinimumUpperCase(ComponentName who, int length, int userHandle) { |
| 1356 | enforceCrossUserPermission(userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1357 | synchronized (this) { |
| 1358 | if (who == null) { |
| 1359 | throw new NullPointerException("ComponentName is null"); |
| 1360 | } |
| 1361 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 1362 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
| 1363 | if (ap.minimumPasswordUpperCase != length) { |
| 1364 | ap.minimumPasswordUpperCase = length; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1365 | saveSettingsLocked(userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1366 | } |
| 1367 | } |
| 1368 | } |
| 1369 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1370 | public int getPasswordMinimumUpperCase(ComponentName who, int userHandle) { |
| 1371 | enforceCrossUserPermission(userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1372 | synchronized (this) { |
| 1373 | int length = 0; |
| 1374 | |
| 1375 | if (who != null) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1376 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who, userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1377 | return admin != null ? admin.minimumPasswordUpperCase : length; |
| 1378 | } |
| 1379 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1380 | DevicePolicyData policy = getUserData(userHandle); |
| 1381 | final int N = policy.mAdminList.size(); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1382 | for (int i=0; i<N; i++) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1383 | ActiveAdmin admin = policy.mAdminList.get(i); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1384 | if (length < admin.minimumPasswordUpperCase) { |
| 1385 | length = admin.minimumPasswordUpperCase; |
| 1386 | } |
| 1387 | } |
| 1388 | return length; |
| 1389 | } |
| 1390 | } |
| 1391 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1392 | public void setPasswordMinimumLowerCase(ComponentName who, int length, int userHandle) { |
| 1393 | enforceCrossUserPermission(userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1394 | synchronized (this) { |
| 1395 | if (who == null) { |
| 1396 | throw new NullPointerException("ComponentName is null"); |
| 1397 | } |
| 1398 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 1399 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
| 1400 | if (ap.minimumPasswordLowerCase != length) { |
| 1401 | ap.minimumPasswordLowerCase = length; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1402 | saveSettingsLocked(userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1403 | } |
| 1404 | } |
| 1405 | } |
| 1406 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1407 | public int getPasswordMinimumLowerCase(ComponentName who, int userHandle) { |
| 1408 | enforceCrossUserPermission(userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1409 | synchronized (this) { |
| 1410 | int length = 0; |
| 1411 | |
| 1412 | if (who != null) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1413 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who, userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1414 | return admin != null ? admin.minimumPasswordLowerCase : length; |
| 1415 | } |
| 1416 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1417 | DevicePolicyData policy = getUserData(userHandle); |
| 1418 | final int N = policy.mAdminList.size(); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1419 | for (int i=0; i<N; i++) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1420 | ActiveAdmin admin = policy.mAdminList.get(i); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1421 | if (length < admin.minimumPasswordLowerCase) { |
| 1422 | length = admin.minimumPasswordLowerCase; |
| 1423 | } |
| 1424 | } |
| 1425 | return length; |
| 1426 | } |
| 1427 | } |
| 1428 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1429 | public void setPasswordMinimumLetters(ComponentName who, int length, int userHandle) { |
| 1430 | enforceCrossUserPermission(userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1431 | synchronized (this) { |
| 1432 | if (who == null) { |
| 1433 | throw new NullPointerException("ComponentName is null"); |
| 1434 | } |
| 1435 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 1436 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
| 1437 | if (ap.minimumPasswordLetters != length) { |
| 1438 | ap.minimumPasswordLetters = length; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1439 | saveSettingsLocked(userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1440 | } |
| 1441 | } |
| 1442 | } |
| 1443 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1444 | public int getPasswordMinimumLetters(ComponentName who, int userHandle) { |
| 1445 | enforceCrossUserPermission(userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1446 | synchronized (this) { |
| 1447 | int length = 0; |
| 1448 | |
| 1449 | if (who != null) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1450 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who, userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1451 | return admin != null ? admin.minimumPasswordLetters : length; |
| 1452 | } |
| 1453 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1454 | DevicePolicyData policy = getUserData(userHandle); |
| 1455 | final int N = policy.mAdminList.size(); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1456 | for (int i=0; i<N; i++) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1457 | ActiveAdmin admin = policy.mAdminList.get(i); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1458 | if (length < admin.minimumPasswordLetters) { |
| 1459 | length = admin.minimumPasswordLetters; |
| 1460 | } |
| 1461 | } |
| 1462 | return length; |
| 1463 | } |
| 1464 | } |
| 1465 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1466 | public void setPasswordMinimumNumeric(ComponentName who, int length, int userHandle) { |
| 1467 | enforceCrossUserPermission(userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1468 | synchronized (this) { |
| 1469 | if (who == null) { |
| 1470 | throw new NullPointerException("ComponentName is null"); |
| 1471 | } |
| 1472 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 1473 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
| 1474 | if (ap.minimumPasswordNumeric != length) { |
| 1475 | ap.minimumPasswordNumeric = length; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1476 | saveSettingsLocked(userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1477 | } |
| 1478 | } |
| 1479 | } |
| 1480 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1481 | public int getPasswordMinimumNumeric(ComponentName who, int userHandle) { |
| 1482 | enforceCrossUserPermission(userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1483 | synchronized (this) { |
| 1484 | int length = 0; |
| 1485 | |
| 1486 | if (who != null) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1487 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who, userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1488 | return admin != null ? admin.minimumPasswordNumeric : length; |
| 1489 | } |
| 1490 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1491 | DevicePolicyData policy = getUserData(userHandle); |
| 1492 | final int N = policy.mAdminList.size(); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1493 | for (int i = 0; i < N; i++) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1494 | ActiveAdmin admin = policy.mAdminList.get(i); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1495 | if (length < admin.minimumPasswordNumeric) { |
| 1496 | length = admin.minimumPasswordNumeric; |
| 1497 | } |
| 1498 | } |
| 1499 | return length; |
| 1500 | } |
| 1501 | } |
| 1502 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1503 | public void setPasswordMinimumSymbols(ComponentName who, int length, int userHandle) { |
| 1504 | enforceCrossUserPermission(userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1505 | synchronized (this) { |
| 1506 | if (who == null) { |
| 1507 | throw new NullPointerException("ComponentName is null"); |
| 1508 | } |
| 1509 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 1510 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
| 1511 | if (ap.minimumPasswordSymbols != length) { |
| 1512 | ap.minimumPasswordSymbols = length; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1513 | saveSettingsLocked(userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1514 | } |
| 1515 | } |
| 1516 | } |
| 1517 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1518 | public int getPasswordMinimumSymbols(ComponentName who, int userHandle) { |
| 1519 | enforceCrossUserPermission(userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1520 | synchronized (this) { |
| 1521 | int length = 0; |
| 1522 | |
| 1523 | if (who != null) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1524 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who, userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1525 | return admin != null ? admin.minimumPasswordSymbols : length; |
| 1526 | } |
| 1527 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1528 | DevicePolicyData policy = getUserData(userHandle); |
| 1529 | final int N = policy.mAdminList.size(); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1530 | for (int i=0; i<N; i++) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1531 | ActiveAdmin admin = policy.mAdminList.get(i); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1532 | if (length < admin.minimumPasswordSymbols) { |
| 1533 | length = admin.minimumPasswordSymbols; |
| 1534 | } |
| 1535 | } |
| 1536 | return length; |
| 1537 | } |
| 1538 | } |
| 1539 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1540 | public void setPasswordMinimumNonLetter(ComponentName who, int length, int userHandle) { |
| 1541 | enforceCrossUserPermission(userHandle); |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 1542 | synchronized (this) { |
| 1543 | if (who == null) { |
| 1544 | throw new NullPointerException("ComponentName is null"); |
| 1545 | } |
| 1546 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 1547 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
| 1548 | if (ap.minimumPasswordNonLetter != length) { |
| 1549 | ap.minimumPasswordNonLetter = length; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1550 | saveSettingsLocked(userHandle); |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 1551 | } |
| 1552 | } |
| 1553 | } |
| 1554 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1555 | public int getPasswordMinimumNonLetter(ComponentName who, int userHandle) { |
| 1556 | enforceCrossUserPermission(userHandle); |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 1557 | synchronized (this) { |
| 1558 | int length = 0; |
| 1559 | |
| 1560 | if (who != null) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1561 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who, userHandle); |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 1562 | return admin != null ? admin.minimumPasswordNonLetter : length; |
| 1563 | } |
| 1564 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1565 | DevicePolicyData policy = getUserData(userHandle); |
| 1566 | final int N = policy.mAdminList.size(); |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 1567 | for (int i=0; i<N; i++) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1568 | ActiveAdmin admin = policy.mAdminList.get(i); |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 1569 | if (length < admin.minimumPasswordNonLetter) { |
| 1570 | length = admin.minimumPasswordNonLetter; |
| 1571 | } |
| 1572 | } |
| 1573 | return length; |
| 1574 | } |
| 1575 | } |
| 1576 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1577 | public boolean isActivePasswordSufficient(int userHandle) { |
| 1578 | enforceCrossUserPermission(userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1579 | synchronized (this) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1580 | DevicePolicyData policy = getUserData(userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1581 | // This API can only be called by an active device admin, |
| 1582 | // so try to retrieve it to check that the caller is one. |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 1583 | getActiveAdminForCallerLocked(null, |
| 1584 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1585 | if (policy.mActivePasswordQuality < getPasswordQuality(null, userHandle) |
| 1586 | || policy.mActivePasswordLength < getPasswordMinimumLength(null, userHandle)) { |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1587 | return false; |
| 1588 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1589 | if (policy.mActivePasswordQuality != DevicePolicyManager.PASSWORD_QUALITY_COMPLEX) { |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1590 | return true; |
| 1591 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1592 | return policy.mActivePasswordUpperCase >= getPasswordMinimumUpperCase(null, userHandle) |
| 1593 | && policy.mActivePasswordLowerCase >= getPasswordMinimumLowerCase(null, userHandle) |
| 1594 | && policy.mActivePasswordLetters >= getPasswordMinimumLetters(null, userHandle) |
| 1595 | && policy.mActivePasswordNumeric >= getPasswordMinimumNumeric(null, userHandle) |
| 1596 | && policy.mActivePasswordSymbols >= getPasswordMinimumSymbols(null, userHandle) |
| 1597 | && policy.mActivePasswordNonLetter >= getPasswordMinimumNonLetter(null, userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1598 | } |
| 1599 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1600 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1601 | public int getCurrentFailedPasswordAttempts(int userHandle) { |
| 1602 | enforceCrossUserPermission(userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1603 | synchronized (this) { |
| 1604 | // This API can only be called by an active device admin, |
| 1605 | // so try to retrieve it to check that the caller is one. |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 1606 | getActiveAdminForCallerLocked(null, |
| 1607 | DeviceAdminInfo.USES_POLICY_WATCH_LOGIN); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1608 | return getUserData(userHandle).mFailedPasswordAttempts; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1609 | } |
| 1610 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1611 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1612 | public void setMaximumFailedPasswordsForWipe(ComponentName who, int num, int userHandle) { |
| 1613 | enforceCrossUserPermission(userHandle); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1614 | synchronized (this) { |
| 1615 | // This API can only be called by an active device admin, |
| 1616 | // so try to retrieve it to check that the caller is one. |
| 1617 | getActiveAdminForCallerLocked(who, |
| 1618 | DeviceAdminInfo.USES_POLICY_WIPE_DATA); |
| 1619 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 1620 | DeviceAdminInfo.USES_POLICY_WATCH_LOGIN); |
| 1621 | if (ap.maximumFailedPasswordsForWipe != num) { |
| 1622 | ap.maximumFailedPasswordsForWipe = num; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1623 | saveSettingsLocked(userHandle); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1624 | } |
| 1625 | } |
| 1626 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1627 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1628 | public int getMaximumFailedPasswordsForWipe(ComponentName who, int userHandle) { |
| 1629 | enforceCrossUserPermission(userHandle); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1630 | synchronized (this) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1631 | DevicePolicyData policy = getUserData(userHandle); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1632 | int count = 0; |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1633 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1634 | if (who != null) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1635 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who, userHandle); |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1636 | return admin != null ? admin.maximumFailedPasswordsForWipe : count; |
| 1637 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1638 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1639 | final int N = policy.mAdminList.size(); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1640 | for (int i=0; i<N; i++) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1641 | ActiveAdmin admin = policy.mAdminList.get(i); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1642 | if (count == 0) { |
| 1643 | count = admin.maximumFailedPasswordsForWipe; |
| 1644 | } else if (admin.maximumFailedPasswordsForWipe != 0 |
| 1645 | && count > admin.maximumFailedPasswordsForWipe) { |
| 1646 | count = admin.maximumFailedPasswordsForWipe; |
| 1647 | } |
| 1648 | } |
| 1649 | return count; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1650 | } |
| 1651 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1652 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1653 | public boolean resetPassword(String password, int flags, int userHandle) { |
| 1654 | enforceCrossUserPermission(userHandle); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 1655 | int quality; |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 1656 | synchronized (this) { |
| 1657 | // This API can only be called by an active device admin, |
| 1658 | // so try to retrieve it to check that the caller is one. |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 1659 | getActiveAdminForCallerLocked(null, |
| 1660 | DeviceAdminInfo.USES_POLICY_RESET_PASSWORD); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1661 | quality = getPasswordQuality(null, userHandle); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 1662 | if (quality != DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1663 | int realQuality = LockPatternUtils.computePasswordQuality(password); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1664 | if (realQuality < quality |
| 1665 | && quality != DevicePolicyManager.PASSWORD_QUALITY_COMPLEX) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1666 | Slog.w(TAG, "resetPassword: password quality 0x" |
| 1667 | + Integer.toHexString(quality) |
| 1668 | + " does not meet required quality 0x" |
| 1669 | + Integer.toHexString(quality)); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 1670 | return false; |
| 1671 | } |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1672 | quality = Math.max(realQuality, quality); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 1673 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1674 | int length = getPasswordMinimumLength(null, userHandle); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 1675 | if (password.length() < length) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1676 | Slog.w(TAG, "resetPassword: password length " + password.length() |
| 1677 | + " does not meet required length " + length); |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 1678 | return false; |
| 1679 | } |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1680 | if (quality == DevicePolicyManager.PASSWORD_QUALITY_COMPLEX) { |
| 1681 | int letters = 0; |
| 1682 | int uppercase = 0; |
| 1683 | int lowercase = 0; |
| 1684 | int numbers = 0; |
| 1685 | int symbols = 0; |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 1686 | int nonletter = 0; |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1687 | for (int i = 0; i < password.length(); i++) { |
| 1688 | char c = password.charAt(i); |
| 1689 | if (c >= 'A' && c <= 'Z') { |
| 1690 | letters++; |
| 1691 | uppercase++; |
| 1692 | } else if (c >= 'a' && c <= 'z') { |
| 1693 | letters++; |
| 1694 | lowercase++; |
| 1695 | } else if (c >= '0' && c <= '9') { |
| 1696 | numbers++; |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 1697 | nonletter++; |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1698 | } else { |
| 1699 | symbols++; |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 1700 | nonletter++; |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1701 | } |
| 1702 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1703 | int neededLetters = getPasswordMinimumLetters(null, userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1704 | if(letters < neededLetters) { |
| 1705 | Slog.w(TAG, "resetPassword: number of letters " + letters |
| 1706 | + " does not meet required number of letters " + neededLetters); |
| 1707 | return false; |
| 1708 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1709 | int neededNumbers = getPasswordMinimumNumeric(null, userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1710 | if (numbers < neededNumbers) { |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 1711 | Slog.w(TAG, "resetPassword: number of numerical digits " + numbers |
| 1712 | + " does not meet required number of numerical digits " |
| 1713 | + neededNumbers); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1714 | return false; |
| 1715 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1716 | int neededLowerCase = getPasswordMinimumLowerCase(null, userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1717 | if (lowercase < neededLowerCase) { |
| 1718 | Slog.w(TAG, "resetPassword: number of lowercase letters " + lowercase |
| 1719 | + " does not meet required number of lowercase letters " |
| 1720 | + neededLowerCase); |
| 1721 | return false; |
| 1722 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1723 | int neededUpperCase = getPasswordMinimumUpperCase(null, userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1724 | if (uppercase < neededUpperCase) { |
| 1725 | Slog.w(TAG, "resetPassword: number of uppercase letters " + uppercase |
| 1726 | + " does not meet required number of uppercase letters " |
| 1727 | + neededUpperCase); |
| 1728 | return false; |
| 1729 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1730 | int neededSymbols = getPasswordMinimumSymbols(null, userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1731 | if (symbols < neededSymbols) { |
| 1732 | Slog.w(TAG, "resetPassword: number of special symbols " + symbols |
| 1733 | + " does not meet required number of special symbols " + neededSymbols); |
| 1734 | return false; |
| 1735 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1736 | int neededNonLetter = getPasswordMinimumNonLetter(null, userHandle); |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 1737 | if (nonletter < neededNonLetter) { |
| 1738 | Slog.w(TAG, "resetPassword: number of non-letter characters " + nonletter |
| 1739 | + " does not meet required number of non-letter characters " |
| 1740 | + neededNonLetter); |
| 1741 | return false; |
| 1742 | } |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1743 | } |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 1744 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1745 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 1746 | int callingUid = Binder.getCallingUid(); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1747 | DevicePolicyData policy = getUserData(userHandle); |
| 1748 | if (policy.mPasswordOwner >= 0 && policy.mPasswordOwner != callingUid) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1749 | 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] | 1750 | return false; |
| 1751 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1752 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 1753 | // Don't do this with the lock held, because it is going to call |
| 1754 | // back in to the service. |
| 1755 | long ident = Binder.clearCallingIdentity(); |
| 1756 | try { |
| 1757 | LockPatternUtils utils = new LockPatternUtils(mContext); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1758 | utils.saveLockPassword(password, quality, false, userHandle); |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1759 | synchronized (this) { |
| 1760 | int newOwner = (flags&DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY) |
| 1761 | != 0 ? callingUid : -1; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1762 | if (policy.mPasswordOwner != newOwner) { |
| 1763 | policy.mPasswordOwner = newOwner; |
| 1764 | saveSettingsLocked(userHandle); |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1765 | } |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 1766 | } |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 1767 | } finally { |
| 1768 | Binder.restoreCallingIdentity(ident); |
| 1769 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1770 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 1771 | return true; |
| 1772 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1773 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1774 | public void setMaximumTimeToLock(ComponentName who, long timeMs, int userHandle) { |
| 1775 | enforceCrossUserPermission(userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1776 | synchronized (this) { |
| 1777 | if (who == null) { |
| 1778 | throw new NullPointerException("ComponentName is null"); |
| 1779 | } |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 1780 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
Dianne Hackborn | 315ada7 | 2010-02-11 12:14:08 -0800 | [diff] [blame] | 1781 | DeviceAdminInfo.USES_POLICY_FORCE_LOCK); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1782 | if (ap.maximumTimeToUnlock != timeMs) { |
| 1783 | ap.maximumTimeToUnlock = timeMs; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1784 | saveSettingsLocked(userHandle); |
| 1785 | updateMaximumTimeToLockLocked(getUserData(userHandle)); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1786 | } |
| 1787 | } |
| 1788 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1789 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1790 | void updateMaximumTimeToLockLocked(DevicePolicyData policy) { |
| 1791 | long timeMs = getMaximumTimeToLock(null, policy.mUserHandle); |
| 1792 | if (policy.mLastMaximumTimeToLock == timeMs) { |
Dianne Hackborn | 2fe8fb2 | 2012-06-15 17:05:25 -0700 | [diff] [blame] | 1793 | return; |
| 1794 | } |
| 1795 | |
| 1796 | long ident = Binder.clearCallingIdentity(); |
| 1797 | try { |
| 1798 | if (timeMs <= 0) { |
| 1799 | timeMs = Integer.MAX_VALUE; |
| 1800 | } else { |
| 1801 | // Make sure KEEP_SCREEN_ON is disabled, since that |
| 1802 | // would allow bypassing of the maximum time to lock. |
Christopher Tate | 62df6eb5 | 2012-09-07 15:00:54 -0700 | [diff] [blame] | 1803 | Settings.Global.putInt(mContext.getContentResolver(), |
| 1804 | Settings.Global.STAY_ON_WHILE_PLUGGED_IN, 0); |
Dianne Hackborn | 2fe8fb2 | 2012-06-15 17:05:25 -0700 | [diff] [blame] | 1805 | } |
| 1806 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1807 | policy.mLastMaximumTimeToLock = timeMs; |
Dianne Hackborn | 2fe8fb2 | 2012-06-15 17:05:25 -0700 | [diff] [blame] | 1808 | |
| 1809 | try { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 1810 | getIPowerManager().setMaximumScreenOffTimeoutFromDeviceAdmin((int)timeMs); |
Dianne Hackborn | 2fe8fb2 | 2012-06-15 17:05:25 -0700 | [diff] [blame] | 1811 | } catch (RemoteException e) { |
| 1812 | Slog.w(TAG, "Failure talking with power manager", e); |
| 1813 | } |
| 1814 | } finally { |
| 1815 | Binder.restoreCallingIdentity(ident); |
| 1816 | } |
| 1817 | } |
| 1818 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1819 | public long getMaximumTimeToLock(ComponentName who, int userHandle) { |
| 1820 | enforceCrossUserPermission(userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1821 | synchronized (this) { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1822 | long time = 0; |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1823 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1824 | if (who != null) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1825 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who, userHandle); |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1826 | return admin != null ? admin.maximumTimeToUnlock : time; |
| 1827 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1828 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1829 | DevicePolicyData policy = getUserData(userHandle); |
| 1830 | final int N = policy.mAdminList.size(); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1831 | for (int i=0; i<N; i++) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1832 | ActiveAdmin admin = policy.mAdminList.get(i); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1833 | if (time == 0) { |
| 1834 | time = admin.maximumTimeToUnlock; |
| 1835 | } else if (admin.maximumTimeToUnlock != 0 |
| 1836 | && time > admin.maximumTimeToUnlock) { |
| 1837 | time = admin.maximumTimeToUnlock; |
| 1838 | } |
| 1839 | } |
| 1840 | return time; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1841 | } |
| 1842 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1843 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 1844 | public void lockNow() { |
| 1845 | synchronized (this) { |
| 1846 | // This API can only be called by an active device admin, |
| 1847 | // so try to retrieve it to check that the caller is one. |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 1848 | getActiveAdminForCallerLocked(null, |
| 1849 | DeviceAdminInfo.USES_POLICY_FORCE_LOCK); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1850 | lockNowUnchecked(); |
| 1851 | } |
| 1852 | } |
| 1853 | |
| 1854 | private void lockNowUnchecked() { |
| 1855 | long ident = Binder.clearCallingIdentity(); |
| 1856 | try { |
| 1857 | // Power off the display |
| 1858 | getIPowerManager().goToSleep(SystemClock.uptimeMillis(), |
| 1859 | PowerManager.GO_TO_SLEEP_REASON_DEVICE_ADMIN); |
| 1860 | // Ensure the device is locked |
Adam Cohen | f752202 | 2012-10-03 20:03:18 -0700 | [diff] [blame] | 1861 | getWindowManager().lockNow(null); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1862 | } catch (RemoteException e) { |
| 1863 | } finally { |
| 1864 | Binder.restoreCallingIdentity(ident); |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 1865 | } |
| 1866 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1867 | |
Ben Komalo | ed48c8b | 2011-10-17 17:30:21 -0700 | [diff] [blame] | 1868 | private boolean isExtStorageEncrypted() { |
| 1869 | String state = SystemProperties.get("vold.decrypt"); |
| 1870 | return !"".equals(state); |
| 1871 | } |
| 1872 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1873 | void wipeDataLocked(int flags) { |
Ben Komalo | ed48c8b | 2011-10-17 17:30:21 -0700 | [diff] [blame] | 1874 | // If the SD card is encrypted and non-removable, we have to force a wipe. |
| 1875 | boolean forceExtWipe = !Environment.isExternalStorageRemovable() && isExtStorageEncrypted(); |
| 1876 | boolean wipeExtRequested = (flags&DevicePolicyManager.WIPE_EXTERNAL_STORAGE) != 0; |
| 1877 | |
| 1878 | // Note: we can only do the wipe via ExternalStorageFormatter if the volume is not emulated. |
| 1879 | if ((forceExtWipe || wipeExtRequested) && !Environment.isExternalStorageEmulated()) { |
Dianne Hackborn | 4249917 | 2010-10-15 18:45:07 -0700 | [diff] [blame] | 1880 | Intent intent = new Intent(ExternalStorageFormatter.FORMAT_AND_FACTORY_RESET); |
Masanori Ogino | f535cb04 | 2012-02-15 19:25:50 +0900 | [diff] [blame] | 1881 | intent.putExtra(ExternalStorageFormatter.EXTRA_ALWAYS_RESET, true); |
Dianne Hackborn | 4249917 | 2010-10-15 18:45:07 -0700 | [diff] [blame] | 1882 | intent.setComponent(ExternalStorageFormatter.COMPONENT_NAME); |
| 1883 | mWakeLock.acquire(10000); |
| 1884 | mContext.startService(intent); |
| 1885 | } else { |
| 1886 | try { |
| 1887 | RecoverySystem.rebootWipeUserData(mContext); |
| 1888 | } catch (IOException e) { |
| 1889 | Slog.w(TAG, "Failed requesting data wipe", e); |
| 1890 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1891 | } |
| 1892 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1893 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1894 | public void wipeData(int flags, final int userHandle) { |
| 1895 | enforceCrossUserPermission(userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1896 | synchronized (this) { |
| 1897 | // This API can only be called by an active device admin, |
| 1898 | // so try to retrieve it to check that the caller is one. |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 1899 | getActiveAdminForCallerLocked(null, |
| 1900 | DeviceAdminInfo.USES_POLICY_WIPE_DATA); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1901 | long ident = Binder.clearCallingIdentity(); |
| 1902 | try { |
Amith Yamasani | 32f0742 | 2012-11-16 15:09:13 -0800 | [diff] [blame] | 1903 | wipeDeviceOrUserLocked(flags, userHandle); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1904 | } finally { |
| 1905 | Binder.restoreCallingIdentity(ident); |
| 1906 | } |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 1907 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1908 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1909 | |
Amith Yamasani | 32f0742 | 2012-11-16 15:09:13 -0800 | [diff] [blame] | 1910 | private void wipeDeviceOrUserLocked(int flags, final int userHandle) { |
| 1911 | if (userHandle == UserHandle.USER_OWNER) { |
| 1912 | wipeDataLocked(flags); |
| 1913 | } else { |
| 1914 | lockNowUnchecked(); |
| 1915 | mHandler.post(new Runnable() { |
| 1916 | public void run() { |
| 1917 | try { |
Amith Yamasani | e4cf734 | 2012-12-17 11:12:09 -0800 | [diff] [blame] | 1918 | ActivityManagerNative.getDefault().switchUser(UserHandle.USER_OWNER); |
Amith Yamasani | 32f0742 | 2012-11-16 15:09:13 -0800 | [diff] [blame] | 1919 | ((UserManager) mContext.getSystemService(Context.USER_SERVICE)) |
| 1920 | .removeUser(userHandle); |
| 1921 | } catch (RemoteException re) { |
| 1922 | // Shouldn't happen |
| 1923 | } |
| 1924 | } |
| 1925 | }); |
| 1926 | } |
| 1927 | } |
| 1928 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1929 | public void getRemoveWarning(ComponentName comp, final RemoteCallback result, int userHandle) { |
| 1930 | enforceCrossUserPermission(userHandle); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1931 | mContext.enforceCallingOrSelfPermission( |
| 1932 | android.Manifest.permission.BIND_DEVICE_ADMIN, null); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1933 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1934 | synchronized (this) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1935 | ActiveAdmin admin = getActiveAdminUncheckedLocked(comp, userHandle); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1936 | if (admin == null) { |
| 1937 | try { |
| 1938 | result.sendResult(null); |
| 1939 | } catch (RemoteException e) { |
| 1940 | } |
| 1941 | return; |
| 1942 | } |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 1943 | Intent intent = new Intent(DeviceAdminReceiver.ACTION_DEVICE_ADMIN_DISABLE_REQUESTED); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1944 | intent.setComponent(admin.info.getComponent()); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1945 | mContext.sendOrderedBroadcastAsUser(intent, new UserHandle(userHandle), |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1946 | null, new BroadcastReceiver() { |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1947 | @Override |
| 1948 | public void onReceive(Context context, Intent intent) { |
| 1949 | try { |
| 1950 | result.sendResult(getResultExtras(false)); |
| 1951 | } catch (RemoteException e) { |
| 1952 | } |
| 1953 | } |
| 1954 | }, null, Activity.RESULT_OK, null, null); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1955 | } |
| 1956 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1957 | |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1958 | public void setActivePasswordState(int quality, int length, int letters, int uppercase, |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1959 | int lowercase, int numbers, int symbols, int nonletter, int userHandle) { |
| 1960 | enforceCrossUserPermission(userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1961 | mContext.enforceCallingOrSelfPermission( |
| 1962 | android.Manifest.permission.BIND_DEVICE_ADMIN, null); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1963 | DevicePolicyData p = getUserData(userHandle); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1964 | |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1965 | validateQualityConstant(quality); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1966 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1967 | synchronized (this) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1968 | if (p.mActivePasswordQuality != quality || p.mActivePasswordLength != length |
| 1969 | || p.mFailedPasswordAttempts != 0 || p.mActivePasswordLetters != letters |
| 1970 | || p.mActivePasswordUpperCase != uppercase |
| 1971 | || p.mActivePasswordLowerCase != lowercase || p.mActivePasswordNumeric != numbers |
| 1972 | || p.mActivePasswordSymbols != symbols || p.mActivePasswordNonLetter != nonletter) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1973 | long ident = Binder.clearCallingIdentity(); |
| 1974 | try { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1975 | p.mActivePasswordQuality = quality; |
| 1976 | p.mActivePasswordLength = length; |
| 1977 | p.mActivePasswordLetters = letters; |
| 1978 | p.mActivePasswordLowerCase = lowercase; |
| 1979 | p.mActivePasswordUpperCase = uppercase; |
| 1980 | p.mActivePasswordNumeric = numbers; |
| 1981 | p.mActivePasswordSymbols = symbols; |
| 1982 | p.mActivePasswordNonLetter = nonletter; |
| 1983 | p.mFailedPasswordAttempts = 0; |
| 1984 | saveSettingsLocked(userHandle); |
| 1985 | updatePasswordExpirationsLocked(userHandle); |
| 1986 | setExpirationAlarmCheckLocked(mContext, p); |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 1987 | sendAdminCommandLocked(DeviceAdminReceiver.ACTION_PASSWORD_CHANGED, |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1988 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD, userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1989 | } finally { |
| 1990 | Binder.restoreCallingIdentity(ident); |
| 1991 | } |
| 1992 | } |
| 1993 | } |
| 1994 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1995 | |
Andy Stadler | 043116a | 2010-11-29 17:43:32 -0800 | [diff] [blame] | 1996 | /** |
| 1997 | * Called any time the device password is updated. Resets all password expiration clocks. |
| 1998 | */ |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1999 | private void updatePasswordExpirationsLocked(int userHandle) { |
| 2000 | DevicePolicyData policy = getUserData(userHandle); |
| 2001 | final int N = policy.mAdminList.size(); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 2002 | if (N > 0) { |
| 2003 | for (int i=0; i<N; i++) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2004 | ActiveAdmin admin = policy.mAdminList.get(i); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 2005 | if (admin.info.usesPolicy(DeviceAdminInfo.USES_POLICY_EXPIRE_PASSWORD)) { |
Andy Stadler | 043116a | 2010-11-29 17:43:32 -0800 | [diff] [blame] | 2006 | long timeout = admin.passwordExpirationTimeout; |
| 2007 | long expiration = timeout > 0L ? (timeout + System.currentTimeMillis()) : 0L; |
| 2008 | admin.passwordExpirationDate = expiration; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 2009 | } |
| 2010 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2011 | saveSettingsLocked(userHandle); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 2012 | } |
| 2013 | } |
| 2014 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2015 | public void reportFailedPasswordAttempt(int userHandle) { |
| 2016 | enforceCrossUserPermission(userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 2017 | mContext.enforceCallingOrSelfPermission( |
| 2018 | android.Manifest.permission.BIND_DEVICE_ADMIN, null); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 2019 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 2020 | synchronized (this) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2021 | DevicePolicyData policy = getUserData(userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 2022 | long ident = Binder.clearCallingIdentity(); |
| 2023 | try { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2024 | policy.mFailedPasswordAttempts++; |
| 2025 | saveSettingsLocked(userHandle); |
| 2026 | int max = getMaximumFailedPasswordsForWipe(null, userHandle); |
| 2027 | if (max > 0 && policy.mFailedPasswordAttempts >= max) { |
Amith Yamasani | 32f0742 | 2012-11-16 15:09:13 -0800 | [diff] [blame] | 2028 | wipeDeviceOrUserLocked(0, userHandle); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 2029 | } |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 2030 | sendAdminCommandLocked(DeviceAdminReceiver.ACTION_PASSWORD_FAILED, |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2031 | DeviceAdminInfo.USES_POLICY_WATCH_LOGIN, userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 2032 | } finally { |
| 2033 | Binder.restoreCallingIdentity(ident); |
| 2034 | } |
| 2035 | } |
| 2036 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 2037 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2038 | public void reportSuccessfulPasswordAttempt(int userHandle) { |
| 2039 | enforceCrossUserPermission(userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 2040 | mContext.enforceCallingOrSelfPermission( |
| 2041 | android.Manifest.permission.BIND_DEVICE_ADMIN, null); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 2042 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 2043 | synchronized (this) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2044 | DevicePolicyData policy = getUserData(userHandle); |
| 2045 | if (policy.mFailedPasswordAttempts != 0 || policy.mPasswordOwner >= 0) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 2046 | long ident = Binder.clearCallingIdentity(); |
| 2047 | try { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2048 | policy.mFailedPasswordAttempts = 0; |
| 2049 | policy.mPasswordOwner = -1; |
| 2050 | saveSettingsLocked(userHandle); |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 2051 | sendAdminCommandLocked(DeviceAdminReceiver.ACTION_PASSWORD_SUCCEEDED, |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2052 | DeviceAdminInfo.USES_POLICY_WATCH_LOGIN, userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 2053 | } finally { |
| 2054 | Binder.restoreCallingIdentity(ident); |
| 2055 | } |
| 2056 | } |
| 2057 | } |
| 2058 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 2059 | |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 2060 | public ComponentName setGlobalProxy(ComponentName who, String proxySpec, |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2061 | String exclusionList, int userHandle) { |
| 2062 | enforceCrossUserPermission(userHandle); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 2063 | synchronized(this) { |
| 2064 | if (who == null) { |
| 2065 | throw new NullPointerException("ComponentName is null"); |
| 2066 | } |
| 2067 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2068 | // Only check if owner has set global proxy. We don't allow other users to set it. |
| 2069 | DevicePolicyData policy = getUserData(UserHandle.USER_OWNER); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 2070 | ActiveAdmin admin = getActiveAdminForCallerLocked(who, |
| 2071 | DeviceAdminInfo.USES_POLICY_SETS_GLOBAL_PROXY); |
| 2072 | |
| 2073 | // Scan through active admins and find if anyone has already |
| 2074 | // set the global proxy. |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2075 | Set<ComponentName> compSet = policy.mAdminMap.keySet(); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 2076 | for (ComponentName component : compSet) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2077 | ActiveAdmin ap = policy.mAdminMap.get(component); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 2078 | if ((ap.specifiesGlobalProxy) && (!component.equals(who))) { |
| 2079 | // Another admin already sets the global proxy |
| 2080 | // Return it to the caller. |
| 2081 | return component; |
| 2082 | } |
| 2083 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2084 | |
| 2085 | // If the user is not the owner, don't set the global proxy. Fail silently. |
| 2086 | if (UserHandle.getCallingUserId() != UserHandle.USER_OWNER) { |
| 2087 | Slog.w(TAG, "Only the owner is allowed to set the global proxy. User " |
| 2088 | + userHandle + " is not permitted."); |
| 2089 | return null; |
| 2090 | } |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 2091 | if (proxySpec == null) { |
| 2092 | admin.specifiesGlobalProxy = false; |
| 2093 | admin.globalProxySpec = null; |
| 2094 | admin.globalProxyExclusionList = null; |
| 2095 | } else { |
| 2096 | |
| 2097 | admin.specifiesGlobalProxy = true; |
| 2098 | admin.globalProxySpec = proxySpec; |
| 2099 | admin.globalProxyExclusionList = exclusionList; |
| 2100 | } |
| 2101 | |
| 2102 | // Reset the global proxy accordingly |
| 2103 | // Do this using system permissions, as apps cannot write to secure settings |
| 2104 | long origId = Binder.clearCallingIdentity(); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2105 | resetGlobalProxyLocked(policy); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 2106 | Binder.restoreCallingIdentity(origId); |
| 2107 | return null; |
| 2108 | } |
| 2109 | } |
| 2110 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2111 | public ComponentName getGlobalProxyAdmin(int userHandle) { |
| 2112 | enforceCrossUserPermission(userHandle); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 2113 | synchronized(this) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2114 | DevicePolicyData policy = getUserData(UserHandle.USER_OWNER); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 2115 | // Scan through active admins and find if anyone has already |
| 2116 | // set the global proxy. |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2117 | final int N = policy.mAdminList.size(); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 2118 | for (int i = 0; i < N; i++) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2119 | ActiveAdmin ap = policy.mAdminList.get(i); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 2120 | if (ap.specifiesGlobalProxy) { |
| 2121 | // Device admin sets the global proxy |
| 2122 | // Return it to the caller. |
| 2123 | return ap.info.getComponent(); |
| 2124 | } |
| 2125 | } |
| 2126 | } |
| 2127 | // No device admin sets the global proxy. |
| 2128 | return null; |
| 2129 | } |
| 2130 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2131 | private void resetGlobalProxyLocked(DevicePolicyData policy) { |
| 2132 | final int N = policy.mAdminList.size(); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 2133 | for (int i = 0; i < N; i++) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2134 | ActiveAdmin ap = policy.mAdminList.get(i); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 2135 | if (ap.specifiesGlobalProxy) { |
Dianne Hackborn | 2fe8fb2 | 2012-06-15 17:05:25 -0700 | [diff] [blame] | 2136 | saveGlobalProxyLocked(ap.globalProxySpec, ap.globalProxyExclusionList); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 2137 | return; |
| 2138 | } |
| 2139 | } |
| 2140 | // No device admins defining global proxies - reset global proxy settings to none |
Dianne Hackborn | 2fe8fb2 | 2012-06-15 17:05:25 -0700 | [diff] [blame] | 2141 | saveGlobalProxyLocked(null, null); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 2142 | } |
| 2143 | |
Dianne Hackborn | 2fe8fb2 | 2012-06-15 17:05:25 -0700 | [diff] [blame] | 2144 | private void saveGlobalProxyLocked(String proxySpec, String exclusionList) { |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 2145 | if (exclusionList == null) { |
| 2146 | exclusionList = ""; |
| 2147 | } |
| 2148 | if (proxySpec == null) { |
| 2149 | proxySpec = ""; |
| 2150 | } |
| 2151 | // Remove white spaces |
| 2152 | proxySpec = proxySpec.trim(); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2153 | String data[] = proxySpec.split(":"); |
| 2154 | int proxyPort = 8080; |
| 2155 | if (data.length > 1) { |
| 2156 | try { |
| 2157 | proxyPort = Integer.parseInt(data[1]); |
| 2158 | } catch (NumberFormatException e) {} |
| 2159 | } |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 2160 | exclusionList = exclusionList.trim(); |
| 2161 | ContentResolver res = mContext.getContentResolver(); |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 2162 | Settings.Global.putString(res, Settings.Global.GLOBAL_HTTP_PROXY_HOST, data[0]); |
| 2163 | Settings.Global.putInt(res, Settings.Global.GLOBAL_HTTP_PROXY_PORT, proxyPort); |
| 2164 | Settings.Global.putString(res, Settings.Global.GLOBAL_HTTP_PROXY_EXCLUSION_LIST, |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2165 | exclusionList); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 2166 | } |
| 2167 | |
Andy Stadler | 7b0f8f0 | 2011-01-12 14:59:52 -0800 | [diff] [blame] | 2168 | /** |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 2169 | * Set the storage encryption request for a single admin. Returns the new total request |
| 2170 | * status (for all admins). |
Andy Stadler | 7b0f8f0 | 2011-01-12 14:59:52 -0800 | [diff] [blame] | 2171 | */ |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2172 | public int setStorageEncryption(ComponentName who, boolean encrypt, int userHandle) { |
| 2173 | enforceCrossUserPermission(userHandle); |
Andy Stadler | 7b0f8f0 | 2011-01-12 14:59:52 -0800 | [diff] [blame] | 2174 | synchronized (this) { |
| 2175 | // Check for permissions |
| 2176 | if (who == null) { |
| 2177 | throw new NullPointerException("ComponentName is null"); |
| 2178 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2179 | // Only owner can set storage encryption |
| 2180 | if (userHandle != UserHandle.USER_OWNER |
| 2181 | || UserHandle.getCallingUserId() != UserHandle.USER_OWNER) { |
| 2182 | Slog.w(TAG, "Only owner is allowed to set storage encryption. User " |
| 2183 | + UserHandle.getCallingUserId() + " is not permitted."); |
| 2184 | return 0; |
| 2185 | } |
| 2186 | |
Andy Stadler | 7b0f8f0 | 2011-01-12 14:59:52 -0800 | [diff] [blame] | 2187 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 2188 | DeviceAdminInfo.USES_ENCRYPTED_STORAGE); |
| 2189 | |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 2190 | // Quick exit: If the filesystem does not support encryption, we can exit early. |
| 2191 | if (!isEncryptionSupported()) { |
| 2192 | return DevicePolicyManager.ENCRYPTION_STATUS_UNSUPPORTED; |
| 2193 | } |
| 2194 | |
| 2195 | // (1) Record the value for the admin so it's sticky |
| 2196 | if (ap.encryptionRequested != encrypt) { |
| 2197 | ap.encryptionRequested = encrypt; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2198 | saveSettingsLocked(userHandle); |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 2199 | } |
| 2200 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2201 | DevicePolicyData policy = getUserData(UserHandle.USER_OWNER); |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 2202 | // (2) Compute "max" for all admins |
| 2203 | boolean newRequested = false; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2204 | final int N = policy.mAdminList.size(); |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 2205 | for (int i = 0; i < N; i++) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2206 | newRequested |= policy.mAdminList.get(i).encryptionRequested; |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 2207 | } |
| 2208 | |
| 2209 | // Notify OS of new request |
| 2210 | setEncryptionRequested(newRequested); |
| 2211 | |
| 2212 | // Return the new global request status |
| 2213 | return newRequested |
| 2214 | ? DevicePolicyManager.ENCRYPTION_STATUS_ACTIVE |
| 2215 | : DevicePolicyManager.ENCRYPTION_STATUS_INACTIVE; |
Andy Stadler | 7b0f8f0 | 2011-01-12 14:59:52 -0800 | [diff] [blame] | 2216 | } |
| 2217 | } |
| 2218 | |
| 2219 | /** |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 2220 | * Get the current storage encryption request status for a given admin, or aggregate of all |
| 2221 | * active admins. |
Andy Stadler | 7b0f8f0 | 2011-01-12 14:59:52 -0800 | [diff] [blame] | 2222 | */ |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2223 | public boolean getStorageEncryption(ComponentName who, int userHandle) { |
| 2224 | enforceCrossUserPermission(userHandle); |
Andy Stadler | 7b0f8f0 | 2011-01-12 14:59:52 -0800 | [diff] [blame] | 2225 | synchronized (this) { |
| 2226 | // Check for permissions if a particular caller is specified |
| 2227 | if (who != null) { |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 2228 | // When checking for a single caller, status is based on caller's request |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2229 | ActiveAdmin ap = getActiveAdminUncheckedLocked(who, userHandle); |
Andy Stadler | c994d69 | 2011-06-01 15:30:54 -0700 | [diff] [blame] | 2230 | return ap != null ? ap.encryptionRequested : false; |
Andy Stadler | 7b0f8f0 | 2011-01-12 14:59:52 -0800 | [diff] [blame] | 2231 | } |
| 2232 | |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 2233 | // If no particular caller is specified, return the aggregate set of requests. |
| 2234 | // This is short circuited by returning true on the first hit. |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2235 | DevicePolicyData policy = getUserData(userHandle); |
| 2236 | final int N = policy.mAdminList.size(); |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 2237 | for (int i = 0; i < N; i++) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2238 | if (policy.mAdminList.get(i).encryptionRequested) { |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 2239 | return true; |
| 2240 | } |
| 2241 | } |
| 2242 | return false; |
Andy Stadler | 7b0f8f0 | 2011-01-12 14:59:52 -0800 | [diff] [blame] | 2243 | } |
| 2244 | } |
| 2245 | |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 2246 | /** |
| 2247 | * Get the current encryption status of the device. |
| 2248 | */ |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2249 | public int getStorageEncryptionStatus(int userHandle) { |
| 2250 | enforceCrossUserPermission(userHandle); |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 2251 | return getEncryptionStatus(); |
| 2252 | } |
| 2253 | |
| 2254 | /** |
| 2255 | * Hook to low-levels: This should report if the filesystem supports encrypted storage. |
| 2256 | */ |
| 2257 | private boolean isEncryptionSupported() { |
| 2258 | // Note, this can be implemented as |
| 2259 | // return getEncryptionStatus() != DevicePolicyManager.ENCRYPTION_STATUS_UNSUPPORTED; |
| 2260 | // But is provided as a separate internal method if there's a faster way to do a |
| 2261 | // simple check for supported-or-not. |
| 2262 | return getEncryptionStatus() != DevicePolicyManager.ENCRYPTION_STATUS_UNSUPPORTED; |
| 2263 | } |
| 2264 | |
| 2265 | /** |
| 2266 | * Hook to low-levels: Reporting the current status of encryption. |
| 2267 | * @return A value such as {@link DevicePolicyManager#ENCRYPTION_STATUS_UNSUPPORTED} or |
| 2268 | * {@link DevicePolicyManager#ENCRYPTION_STATUS_INACTIVE} or |
| 2269 | * {@link DevicePolicyManager#ENCRYPTION_STATUS_ACTIVE}. |
| 2270 | */ |
| 2271 | private int getEncryptionStatus() { |
Andy Stadler | 0fe45de | 2011-01-20 16:35:09 -0800 | [diff] [blame] | 2272 | String status = SystemProperties.get("ro.crypto.state", "unsupported"); |
| 2273 | if ("encrypted".equalsIgnoreCase(status)) { |
| 2274 | return DevicePolicyManager.ENCRYPTION_STATUS_ACTIVE; |
| 2275 | } else if ("unencrypted".equalsIgnoreCase(status)) { |
| 2276 | return DevicePolicyManager.ENCRYPTION_STATUS_INACTIVE; |
| 2277 | } else { |
| 2278 | return DevicePolicyManager.ENCRYPTION_STATUS_UNSUPPORTED; |
| 2279 | } |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 2280 | } |
| 2281 | |
| 2282 | /** |
| 2283 | * Hook to low-levels: If needed, record the new admin setting for encryption. |
| 2284 | */ |
| 2285 | private void setEncryptionRequested(boolean encrypt) { |
| 2286 | } |
| 2287 | |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 2288 | /** |
| 2289 | * The system property used to share the state of the camera. The native camera service |
| 2290 | * is expected to read this property and act accordingly. |
| 2291 | */ |
| 2292 | public static final String SYSTEM_PROP_DISABLE_CAMERA = "sys.secpolicy.camera.disabled"; |
| 2293 | |
| 2294 | /** |
| 2295 | * Disables all device cameras according to the specified admin. |
| 2296 | */ |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2297 | public void setCameraDisabled(ComponentName who, boolean disabled, int userHandle) { |
| 2298 | enforceCrossUserPermission(userHandle); |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 2299 | synchronized (this) { |
| 2300 | if (who == null) { |
| 2301 | throw new NullPointerException("ComponentName is null"); |
| 2302 | } |
| 2303 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 2304 | DeviceAdminInfo.USES_POLICY_DISABLE_CAMERA); |
| 2305 | if (ap.disableCamera != disabled) { |
| 2306 | ap.disableCamera = disabled; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2307 | saveSettingsLocked(userHandle); |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 2308 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2309 | syncDeviceCapabilitiesLocked(getUserData(userHandle)); |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 2310 | } |
| 2311 | } |
| 2312 | |
| 2313 | /** |
| 2314 | * Gets whether or not all device cameras are disabled for a given admin, or disabled for any |
| 2315 | * active admins. |
| 2316 | */ |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2317 | public boolean getCameraDisabled(ComponentName who, int userHandle) { |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 2318 | synchronized (this) { |
| 2319 | if (who != null) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2320 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who, userHandle); |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 2321 | return (admin != null) ? admin.disableCamera : false; |
| 2322 | } |
| 2323 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2324 | DevicePolicyData policy = getUserData(userHandle); |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 2325 | // Determine whether or not the device camera is disabled for any active admins. |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2326 | final int N = policy.mAdminList.size(); |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 2327 | for (int i = 0; i < N; i++) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2328 | ActiveAdmin admin = policy.mAdminList.get(i); |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 2329 | if (admin.disableCamera) { |
| 2330 | return true; |
| 2331 | } |
| 2332 | } |
| 2333 | return false; |
| 2334 | } |
| 2335 | } |
| 2336 | |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 2337 | /** |
Jim Miller | 48b9b0d | 2012-09-19 23:16:50 -0700 | [diff] [blame] | 2338 | * Selectively disable keyguard features. |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 2339 | */ |
Jim Miller | 48b9b0d | 2012-09-19 23:16:50 -0700 | [diff] [blame] | 2340 | public void setKeyguardDisabledFeatures(ComponentName who, int which, int userHandle) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2341 | enforceCrossUserPermission(userHandle); |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 2342 | synchronized (this) { |
| 2343 | if (who == null) { |
| 2344 | throw new NullPointerException("ComponentName is null"); |
| 2345 | } |
| 2346 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
Jim Miller | 48b9b0d | 2012-09-19 23:16:50 -0700 | [diff] [blame] | 2347 | DeviceAdminInfo.USES_POLICY_DISABLE_KEYGUARD_FEATURES); |
| 2348 | if (ap.disabledKeyguardFeatures != which) { |
| 2349 | ap.disabledKeyguardFeatures = which; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2350 | saveSettingsLocked(userHandle); |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 2351 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2352 | syncDeviceCapabilitiesLocked(getUserData(userHandle)); |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 2353 | } |
| 2354 | } |
| 2355 | |
| 2356 | /** |
Jim Miller | 48b9b0d | 2012-09-19 23:16:50 -0700 | [diff] [blame] | 2357 | * Gets the disabled state for features in keyguard for the given admin, |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 2358 | * or the aggregate of all active admins if who is null. |
| 2359 | */ |
Jim Miller | 48b9b0d | 2012-09-19 23:16:50 -0700 | [diff] [blame] | 2360 | public int getKeyguardDisabledFeatures(ComponentName who, int userHandle) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2361 | enforceCrossUserPermission(userHandle); |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 2362 | synchronized (this) { |
| 2363 | if (who != null) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2364 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who, userHandle); |
Jim Miller | 48b9b0d | 2012-09-19 23:16:50 -0700 | [diff] [blame] | 2365 | return (admin != null) ? admin.disabledKeyguardFeatures : 0; |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 2366 | } |
| 2367 | |
Jim Miller | 48b9b0d | 2012-09-19 23:16:50 -0700 | [diff] [blame] | 2368 | // Determine which keyguard features are disabled for any active admins. |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2369 | DevicePolicyData policy = getUserData(userHandle); |
| 2370 | final int N = policy.mAdminList.size(); |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 2371 | int which = 0; |
| 2372 | for (int i = 0; i < N; i++) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2373 | ActiveAdmin admin = policy.mAdminList.get(i); |
Jim Miller | 48b9b0d | 2012-09-19 23:16:50 -0700 | [diff] [blame] | 2374 | which |= admin.disabledKeyguardFeatures; |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 2375 | } |
| 2376 | return which; |
| 2377 | } |
| 2378 | } |
| 2379 | |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 2380 | @Override |
| 2381 | public boolean setDeviceOwner(String packageName) { |
| 2382 | if (packageName == null |
| 2383 | || !DeviceOwner.isInstalled(packageName, mContext.getPackageManager())) { |
| 2384 | throw new IllegalArgumentException("Invalid package name " + packageName |
| 2385 | + " for device owner"); |
| 2386 | } |
| 2387 | synchronized (this) { |
| 2388 | if (mDeviceOwner == null && !isDeviceProvisioned()) { |
| 2389 | mDeviceOwner = new DeviceOwner(packageName); |
| 2390 | mDeviceOwner.writeOwnerFile(); |
| 2391 | return true; |
| 2392 | } else { |
| 2393 | throw new IllegalStateException("Trying to set device owner to " + packageName |
| 2394 | + ", owner=" + mDeviceOwner.getPackageName() |
| 2395 | + ", device_provisioned=" + isDeviceProvisioned()); |
| 2396 | } |
| 2397 | } |
| 2398 | } |
| 2399 | |
| 2400 | @Override |
| 2401 | public boolean isDeviceOwner(String packageName) { |
| 2402 | synchronized (this) { |
| 2403 | return mDeviceOwner != null |
| 2404 | && mDeviceOwner.getPackageName().equals(packageName); |
| 2405 | } |
| 2406 | } |
| 2407 | |
| 2408 | @Override |
| 2409 | public String getDeviceOwner() { |
| 2410 | synchronized (this) { |
| 2411 | if (mDeviceOwner != null) { |
| 2412 | return mDeviceOwner.getPackageName(); |
| 2413 | } |
| 2414 | } |
| 2415 | return null; |
| 2416 | } |
| 2417 | |
| 2418 | private boolean isDeviceProvisioned() { |
| 2419 | return Settings.Global.getInt(mContext.getContentResolver(), |
| 2420 | Settings.Global.DEVICE_PROVISIONED, 0) > 0; |
| 2421 | } |
| 2422 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2423 | private void enforceCrossUserPermission(int userHandle) { |
| 2424 | if (userHandle < 0) { |
| 2425 | throw new IllegalArgumentException("Invalid userId " + userHandle); |
| 2426 | } |
| 2427 | final int callingUid = Binder.getCallingUid(); |
| 2428 | if (userHandle == UserHandle.getUserId(callingUid)) return; |
| 2429 | if (callingUid != Process.SYSTEM_UID && callingUid != 0) { |
| 2430 | mContext.enforceCallingOrSelfPermission( |
| 2431 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, "Must be system or have" |
| 2432 | + " INTERACT_ACROSS_USERS_FULL permission"); |
| 2433 | } |
| 2434 | } |
| 2435 | |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 2436 | private void enableIfNecessary(String packageName, int userId) { |
| 2437 | try { |
| 2438 | IPackageManager ipm = AppGlobals.getPackageManager(); |
| 2439 | ApplicationInfo ai = ipm.getApplicationInfo(packageName, |
| 2440 | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS, |
| 2441 | userId); |
| 2442 | if (ai.enabledSetting |
| 2443 | == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) { |
| 2444 | ipm.setApplicationEnabledSetting(packageName, |
| 2445 | PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, |
| 2446 | PackageManager.DONT_KILL_APP, userId); |
| 2447 | } |
| 2448 | } catch (RemoteException e) { |
| 2449 | } |
| 2450 | } |
| 2451 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 2452 | @Override |
| 2453 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 2454 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 2455 | != PackageManager.PERMISSION_GRANTED) { |
| 2456 | |
| 2457 | pw.println("Permission Denial: can't dump DevicePolicyManagerService from from pid=" |
| 2458 | + Binder.getCallingPid() |
| 2459 | + ", uid=" + Binder.getCallingUid()); |
| 2460 | return; |
| 2461 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 2462 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 2463 | final Printer p = new PrintWriterPrinter(pw); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 2464 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 2465 | synchronized (this) { |
| 2466 | p.println("Current Device Policy Manager state:"); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 2467 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2468 | int userCount = mUserData.size(); |
| 2469 | for (int u = 0; u < userCount; u++) { |
| 2470 | DevicePolicyData policy = getUserData(mUserData.keyAt(u)); |
| 2471 | p.println(" Enabled Device Admins (User " + policy.mUserHandle + "):"); |
| 2472 | final int N = policy.mAdminList.size(); |
| 2473 | for (int i=0; i<N; i++) { |
| 2474 | ActiveAdmin ap = policy.mAdminList.get(i); |
| 2475 | if (ap != null) { |
| 2476 | pw.print(" "); pw.print(ap.info.getComponent().flattenToShortString()); |
| 2477 | pw.println(":"); |
| 2478 | ap.dump(" ", pw); |
| 2479 | } |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 2480 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 2481 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2482 | pw.println(" "); |
| 2483 | pw.print(" mPasswordOwner="); pw.println(policy.mPasswordOwner); |
| 2484 | } |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 2485 | } |
| 2486 | } |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 2487 | |
| 2488 | static class DeviceOwner { |
| 2489 | private static final String DEVICE_OWNER_XML = "device_owner.xml"; |
| 2490 | private static final String TAG_DEVICE_OWNER = "device-owner"; |
| 2491 | private static final String ATTR_PACKAGE = "package"; |
| 2492 | private String mPackageName; |
| 2493 | |
| 2494 | DeviceOwner() { |
| 2495 | readOwnerFile(); |
| 2496 | } |
| 2497 | |
| 2498 | DeviceOwner(String packageName) { |
| 2499 | this.mPackageName = packageName; |
| 2500 | } |
| 2501 | |
| 2502 | static boolean isRegistered() { |
| 2503 | return new File(Environment.getSystemSecureDirectory(), |
| 2504 | DEVICE_OWNER_XML).exists(); |
| 2505 | } |
| 2506 | |
| 2507 | String getPackageName() { |
| 2508 | return mPackageName; |
| 2509 | } |
| 2510 | |
| 2511 | static boolean isInstalled(String packageName, PackageManager pm) { |
| 2512 | try { |
| 2513 | PackageInfo pi; |
| 2514 | if ((pi = pm.getPackageInfo(packageName, 0)) != null) { |
| 2515 | if ((pi.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 2516 | return true; |
| 2517 | } |
| 2518 | } |
| 2519 | } catch (NameNotFoundException nnfe) { |
| 2520 | Slog.w(TAG, "Device Owner package " + packageName + " not installed."); |
| 2521 | } |
| 2522 | return false; |
| 2523 | } |
| 2524 | |
| 2525 | void readOwnerFile() { |
| 2526 | AtomicFile file = new AtomicFile(new File(Environment.getSystemSecureDirectory(), |
| 2527 | DEVICE_OWNER_XML)); |
| 2528 | try { |
| 2529 | FileInputStream input = file.openRead(); |
| 2530 | XmlPullParser parser = Xml.newPullParser(); |
| 2531 | parser.setInput(input, null); |
| 2532 | int type; |
| 2533 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 2534 | && type != XmlPullParser.START_TAG) { |
| 2535 | } |
| 2536 | String tag = parser.getName(); |
| 2537 | if (!TAG_DEVICE_OWNER.equals(tag)) { |
| 2538 | throw new XmlPullParserException( |
| 2539 | "Device Owner file does not start with device-owner tag: found " + tag); |
| 2540 | } |
| 2541 | mPackageName = parser.getAttributeValue(null, ATTR_PACKAGE); |
| 2542 | input.close(); |
| 2543 | } catch (XmlPullParserException xppe) { |
| 2544 | Slog.e(TAG, "Error parsing device-owner file\n" + xppe); |
| 2545 | } catch (IOException ioe) { |
| 2546 | Slog.e(TAG, "IO Exception when reading device-owner file\n" + ioe); |
| 2547 | } |
| 2548 | } |
| 2549 | |
| 2550 | void writeOwnerFile() { |
| 2551 | synchronized (this) { |
| 2552 | writeOwnerFileLocked(); |
| 2553 | } |
| 2554 | } |
| 2555 | |
| 2556 | private void writeOwnerFileLocked() { |
| 2557 | AtomicFile file = new AtomicFile(new File(Environment.getSystemSecureDirectory(), |
| 2558 | DEVICE_OWNER_XML)); |
| 2559 | try { |
| 2560 | FileOutputStream output = file.startWrite(); |
| 2561 | XmlSerializer out = new FastXmlSerializer(); |
| 2562 | out.setOutput(output, "utf-8"); |
| 2563 | out.startDocument(null, true); |
| 2564 | out.startTag(null, TAG_DEVICE_OWNER); |
| 2565 | out.attribute(null, ATTR_PACKAGE, mPackageName); |
| 2566 | out.endTag(null, TAG_DEVICE_OWNER); |
| 2567 | out.endDocument(); |
| 2568 | out.flush(); |
| 2569 | file.finishWrite(output); |
| 2570 | } catch (IOException ioe) { |
| 2571 | Slog.e(TAG, "IO Exception when writing device-owner file\n" + ioe); |
| 2572 | } |
| 2573 | } |
| 2574 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 2575 | } |