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