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 | |
Amith Yamasani | 30f8eb4 | 2013-11-06 14:54:50 -0800 | [diff] [blame] | 17 | package com.android.server.devicepolicy; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 18 | |
Maggie Benthall | da51e68 | 2013-08-08 22:35:44 -0400 | [diff] [blame] | 19 | import static android.Manifest.permission.MANAGE_CA_CERTIFICATES; |
Paul Crowley | a7e87ac | 2014-11-18 13:50:19 +0000 | [diff] [blame] | 20 | import static android.app.admin.DevicePolicyManager.WIPE_EXTERNAL_STORAGE; |
| 21 | import static android.app.admin.DevicePolicyManager.WIPE_RESET_PROTECTION_DATA; |
Jessica Hummel | dd31a10 | 2014-09-16 11:56:50 +0100 | [diff] [blame] | 22 | import static android.content.pm.PackageManager.GET_UNINSTALLED_PACKAGES; |
Maggie Benthall | da51e68 | 2013-08-08 22:35:44 -0400 | [diff] [blame] | 23 | |
Kenny Guy | fa80a4f | 2014-08-20 19:40:59 +0100 | [diff] [blame] | 24 | import android.accessibilityservice.AccessibilityServiceInfo; |
Esteban Talavera | b5ef162 | 2014-09-08 17:49:01 +0100 | [diff] [blame] | 25 | import android.accounts.AccountManager; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 26 | import android.app.Activity; |
Sudheer Shanka | 6c780ac | 2016-06-08 17:13:24 -0700 | [diff] [blame] | 27 | import android.app.ActivityManager; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 28 | import android.app.ActivityManagerNative; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 29 | import android.app.AlarmManager; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 30 | import android.app.AppGlobals; |
Jason Monk | 03978a4 | 2014-06-10 15:05:30 -0400 | [diff] [blame] | 31 | import android.app.IActivityManager; |
Maggie Benthall | 0469f41 | 2013-09-05 15:30:26 -0400 | [diff] [blame] | 32 | import android.app.Notification; |
| 33 | import android.app.NotificationManager; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 34 | import android.app.PendingIntent; |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 35 | import android.app.admin.DeviceAdminInfo; |
| 36 | import android.app.admin.DeviceAdminReceiver; |
| 37 | import android.app.admin.DevicePolicyManager; |
Julia Reynolds | 2cb384f | 2014-08-13 15:15:55 -0400 | [diff] [blame] | 38 | import android.app.admin.DevicePolicyManagerInternal; |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 39 | import android.app.admin.IDevicePolicyManager; |
Zoltan Szatmary-Ban | 26ac6a6 | 2014-11-13 20:49:43 +0000 | [diff] [blame] | 40 | import android.app.backup.IBackupManager; |
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; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 49 | import android.content.pm.PackageManager; |
Kenny Guy | fa80a4f | 2014-08-20 19:40:59 +0100 | [diff] [blame] | 50 | import android.content.pm.PackageManager.NameNotFoundException; |
Andy Stadler | 1f35d48 | 2010-11-19 15:39:41 -0800 | [diff] [blame] | 51 | import android.content.pm.ResolveInfo; |
Maggie Benthall | 0469f41 | 2013-09-05 15:30:26 -0400 | [diff] [blame] | 52 | import android.content.pm.UserInfo; |
Bernhard Bauer | 26408cc | 2014-09-08 14:07:31 +0100 | [diff] [blame] | 53 | import android.database.ContentObserver; |
| 54 | import android.hardware.usb.UsbManager; |
Julia Reynolds | 4a21b25 | 2014-06-04 11:11:43 -0400 | [diff] [blame] | 55 | import android.media.AudioManager; |
| 56 | import android.media.IAudioService; |
Jason Monk | 03bc991 | 2014-05-13 09:44:57 -0400 | [diff] [blame] | 57 | import android.net.ConnectivityManager; |
Jason Monk | 54a9ebb | 2014-05-07 15:26:22 -0400 | [diff] [blame] | 58 | import android.net.ProxyInfo; |
Bernhard Bauer | 26408cc | 2014-09-08 14:07:31 +0100 | [diff] [blame] | 59 | import android.net.Uri; |
Robin Lee | d0130e8 | 2014-10-06 11:36:25 +0100 | [diff] [blame] | 60 | import android.os.AsyncTask; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 61 | import android.os.Binder; |
Robin Lee | 66e5d96 | 2014-04-09 16:44:21 +0100 | [diff] [blame] | 62 | import android.os.Bundle; |
Ben Komalo | ed48c8b | 2011-10-17 17:30:21 -0700 | [diff] [blame] | 63 | import android.os.Environment; |
Kenny Guy | 212037f | 2015-01-15 18:14:46 +0000 | [diff] [blame] | 64 | import android.os.FileUtils; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 65 | import android.os.Handler; |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 66 | import android.os.IBinder; |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 67 | import android.os.PersistableBundle; |
Dianne Hackborn | 4249917 | 2010-10-15 18:45:07 -0700 | [diff] [blame] | 68 | import android.os.PowerManager; |
Jeff Brown | 5ce1cb2 | 2014-11-06 19:05:33 -0800 | [diff] [blame] | 69 | import android.os.PowerManagerInternal; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 70 | import android.os.Process; |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 71 | import android.os.RecoverySystem; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 72 | import android.os.RemoteCallback; |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 73 | import android.os.RemoteException; |
| 74 | import android.os.ServiceManager; |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 75 | import android.os.SystemClock; |
Andy Stadler | 0fe45de | 2011-01-20 16:35:09 -0800 | [diff] [blame] | 76 | import android.os.SystemProperties; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 77 | import android.os.UserHandle; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 78 | import android.os.UserManager; |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 79 | import android.provider.Settings; |
Maggie Benthall | da51e68 | 2013-08-08 22:35:44 -0400 | [diff] [blame] | 80 | import android.security.Credentials; |
Bernhard Bauer | 26408cc | 2014-09-08 14:07:31 +0100 | [diff] [blame] | 81 | import android.security.IKeyChainService; |
Maggie Benthall | da51e68 | 2013-08-08 22:35:44 -0400 | [diff] [blame] | 82 | import android.security.KeyChain; |
| 83 | import android.security.KeyChain.KeyChainConnection; |
Adrian Roos | f8f56bc | 2014-11-20 23:55:34 +0100 | [diff] [blame] | 84 | import android.text.TextUtils; |
Paul Crowley | a7e87ac | 2014-11-18 13:50:19 +0000 | [diff] [blame] | 85 | import android.service.persistentdata.PersistentDataBlockManager; |
Maggie Benthall | da51e68 | 2013-08-08 22:35:44 -0400 | [diff] [blame] | 86 | import android.util.Log; |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 87 | import android.util.PrintWriterPrinter; |
| 88 | import android.util.Printer; |
Andy Stadler | 1f35d48 | 2010-11-19 15:39:41 -0800 | [diff] [blame] | 89 | import android.util.Slog; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 90 | import android.util.SparseArray; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 91 | import android.util.Xml; |
Kenny Guy | fa80a4f | 2014-08-20 19:40:59 +0100 | [diff] [blame] | 92 | import android.view.accessibility.AccessibilityManager; |
| 93 | import android.view.accessibility.IAccessibilityManager; |
| 94 | import android.view.inputmethod.InputMethodInfo; |
| 95 | import android.view.inputmethod.InputMethodManager; |
Jim Miller | 93c518e | 2012-01-17 15:55:31 -0800 | [diff] [blame] | 96 | import android.view.IWindowManager; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 97 | |
Julia Reynolds | 2cb384f | 2014-08-13 15:15:55 -0400 | [diff] [blame] | 98 | import com.android.internal.R; |
| 99 | import com.android.internal.os.storage.ExternalStorageFormatter; |
| 100 | import com.android.internal.util.FastXmlSerializer; |
| 101 | import com.android.internal.util.JournaledFile; |
| 102 | import com.android.internal.util.XmlUtils; |
| 103 | import com.android.internal.widget.LockPatternUtils; |
Julia Reynolds | 2cb384f | 2014-08-13 15:15:55 -0400 | [diff] [blame] | 104 | import com.android.server.LocalServices; |
| 105 | import com.android.server.SystemService; |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 106 | import com.android.server.devicepolicy.DevicePolicyManagerService.ActiveAdmin.TrustAgentInfo; |
Julia Reynolds | 2cb384f | 2014-08-13 15:15:55 -0400 | [diff] [blame] | 107 | |
Adam Connors | 776c555 | 2014-01-09 10:42:56 +0000 | [diff] [blame] | 108 | import org.xmlpull.v1.XmlPullParser; |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 109 | |
| 110 | import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT; |
| 111 | import static org.xmlpull.v1.XmlPullParser.END_TAG; |
| 112 | import static org.xmlpull.v1.XmlPullParser.TEXT; |
| 113 | |
Adam Connors | 776c555 | 2014-01-09 10:42:56 +0000 | [diff] [blame] | 114 | import org.xmlpull.v1.XmlPullParserException; |
| 115 | import org.xmlpull.v1.XmlSerializer; |
| 116 | |
Maggie Benthall | da51e68 | 2013-08-08 22:35:44 -0400 | [diff] [blame] | 117 | import java.io.ByteArrayInputStream; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 118 | import java.io.File; |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 119 | import java.io.FileDescriptor; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 120 | import java.io.FileInputStream; |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 121 | import java.io.FileNotFoundException; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 122 | import java.io.FileOutputStream; |
| 123 | import java.io.IOException; |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 124 | import java.io.PrintWriter; |
Maggie Benthall | da51e68 | 2013-08-08 22:35:44 -0400 | [diff] [blame] | 125 | import java.security.cert.CertificateException; |
| 126 | import java.security.cert.CertificateFactory; |
| 127 | import java.security.cert.X509Certificate; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 128 | import java.text.DateFormat; |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 129 | import java.util.ArrayList; |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 130 | import java.util.Collections; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 131 | import java.util.Date; |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 132 | import java.util.HashMap; |
Sander Alewijnse | 5c02db6 | 2014-05-07 10:46:57 +0100 | [diff] [blame] | 133 | import java.util.HashSet; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 134 | import java.util.List; |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 135 | import java.util.Map.Entry; |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 136 | import java.util.Set; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 137 | |
| 138 | /** |
| 139 | * Implementation of the device policy APIs. |
| 140 | */ |
| 141 | public class DevicePolicyManagerService extends IDevicePolicyManager.Stub { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 142 | |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 143 | private static final String LOG_TAG = "DevicePolicyManagerService"; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 144 | |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 145 | private static final String DEVICE_POLICIES_XML = "device_policies.xml"; |
| 146 | |
justinzhang | 511e0d8 | 2014-03-24 16:09:24 -0400 | [diff] [blame] | 147 | private static final String LOCK_TASK_COMPONENTS_XML = "lock-task-component"; |
| 148 | |
Jim Miller | 6b85768 | 2011-02-16 16:27:41 -0800 | [diff] [blame] | 149 | private static final int REQUEST_EXPIRE_PASSWORD = 5571; |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 150 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 151 | private static final long MS_PER_DAY = 86400 * 1000; |
| 152 | |
| 153 | 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] | 154 | |
| 155 | protected static final String ACTION_EXPIRED_PASSWORD_NOTIFICATION |
| 156 | = "com.android.server.ACTION_EXPIRED_PASSWORD_NOTIFICATION"; |
| 157 | |
Maggie Benthall | 0469f41 | 2013-09-05 15:30:26 -0400 | [diff] [blame] | 158 | private static final int MONITORING_CERT_NOTIFICATION_ID = R.string.ssl_ca_cert_warning; |
| 159 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 160 | private static final boolean DBG = false; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 161 | |
Amith Yamasani | f20d640 | 2014-05-24 15:34:37 -0700 | [diff] [blame] | 162 | private static final String ATTR_PERMISSION_PROVIDER = "permission-provider"; |
Amith Yamasani | ed239cb | 2014-07-16 17:26:17 -0700 | [diff] [blame] | 163 | private static final String ATTR_SETUP_COMPLETE = "setup-complete"; |
Amith Yamasani | f20d640 | 2014-05-24 15:34:37 -0700 | [diff] [blame] | 164 | |
Julia Reynolds | 2cb384f | 2014-08-13 15:15:55 -0400 | [diff] [blame] | 165 | private static final Set<String> DEVICE_OWNER_USER_RESTRICTIONS; |
| 166 | static { |
| 167 | DEVICE_OWNER_USER_RESTRICTIONS = new HashSet(); |
| 168 | DEVICE_OWNER_USER_RESTRICTIONS.add(UserManager.DISALLOW_USB_FILE_TRANSFER); |
| 169 | DEVICE_OWNER_USER_RESTRICTIONS.add(UserManager.DISALLOW_CONFIG_TETHERING); |
| 170 | DEVICE_OWNER_USER_RESTRICTIONS.add(UserManager.DISALLOW_FACTORY_RESET); |
| 171 | DEVICE_OWNER_USER_RESTRICTIONS.add(UserManager.DISALLOW_ADD_USER); |
| 172 | DEVICE_OWNER_USER_RESTRICTIONS.add(UserManager.DISALLOW_CONFIG_CELL_BROADCASTS); |
| 173 | DEVICE_OWNER_USER_RESTRICTIONS.add(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS); |
| 174 | DEVICE_OWNER_USER_RESTRICTIONS.add(UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA); |
| 175 | DEVICE_OWNER_USER_RESTRICTIONS.add(UserManager.DISALLOW_UNMUTE_MICROPHONE); |
| 176 | DEVICE_OWNER_USER_RESTRICTIONS.add(UserManager.DISALLOW_ADJUST_VOLUME); |
| 177 | DEVICE_OWNER_USER_RESTRICTIONS.add(UserManager.DISALLOW_SMS); |
| 178 | } |
| 179 | |
Julia Reynolds | 9ed66da | 2014-08-26 15:42:03 -0400 | [diff] [blame] | 180 | private static final Set<String> SECURE_SETTINGS_WHITELIST; |
Julia Reynolds | 82735bc | 2014-09-04 16:43:30 -0400 | [diff] [blame] | 181 | private static final Set<String> SECURE_SETTINGS_DEVICEOWNER_WHITELIST; |
Julia Reynolds | 9ed66da | 2014-08-26 15:42:03 -0400 | [diff] [blame] | 182 | private static final Set<String> GLOBAL_SETTINGS_WHITELIST; |
| 183 | static { |
| 184 | SECURE_SETTINGS_WHITELIST = new HashSet(); |
| 185 | SECURE_SETTINGS_WHITELIST.add(Settings.Secure.DEFAULT_INPUT_METHOD); |
| 186 | SECURE_SETTINGS_WHITELIST.add(Settings.Secure.SKIP_FIRST_USE_HINTS); |
Amith Yamasani | 52c39a1 | 2014-10-21 11:14:04 -0700 | [diff] [blame] | 187 | SECURE_SETTINGS_WHITELIST.add(Settings.Secure.INSTALL_NON_MARKET_APPS); |
Julia Reynolds | 9ed66da | 2014-08-26 15:42:03 -0400 | [diff] [blame] | 188 | |
Julia Reynolds | 82735bc | 2014-09-04 16:43:30 -0400 | [diff] [blame] | 189 | SECURE_SETTINGS_DEVICEOWNER_WHITELIST = new HashSet(); |
| 190 | SECURE_SETTINGS_DEVICEOWNER_WHITELIST.addAll(SECURE_SETTINGS_WHITELIST); |
| 191 | SECURE_SETTINGS_DEVICEOWNER_WHITELIST.add(Settings.Secure.LOCATION_MODE); |
| 192 | |
Julia Reynolds | 9ed66da | 2014-08-26 15:42:03 -0400 | [diff] [blame] | 193 | GLOBAL_SETTINGS_WHITELIST = new HashSet(); |
| 194 | GLOBAL_SETTINGS_WHITELIST.add(Settings.Global.ADB_ENABLED); |
| 195 | GLOBAL_SETTINGS_WHITELIST.add(Settings.Global.AUTO_TIME); |
| 196 | GLOBAL_SETTINGS_WHITELIST.add(Settings.Global.AUTO_TIME_ZONE); |
| 197 | GLOBAL_SETTINGS_WHITELIST.add(Settings.Global.BLUETOOTH_ON); |
| 198 | GLOBAL_SETTINGS_WHITELIST.add(Settings.Global.DATA_ROAMING); |
| 199 | GLOBAL_SETTINGS_WHITELIST.add(Settings.Global.DEVELOPMENT_SETTINGS_ENABLED); |
| 200 | GLOBAL_SETTINGS_WHITELIST.add(Settings.Global.MODE_RINGER); |
| 201 | GLOBAL_SETTINGS_WHITELIST.add(Settings.Global.NETWORK_PREFERENCE); |
| 202 | GLOBAL_SETTINGS_WHITELIST.add(Settings.Global.USB_MASS_STORAGE_ENABLED); |
| 203 | GLOBAL_SETTINGS_WHITELIST.add(Settings.Global.WIFI_ON); |
| 204 | GLOBAL_SETTINGS_WHITELIST.add(Settings.Global.WIFI_SLEEP_POLICY); |
| 205 | } |
| 206 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 207 | final Context mContext; |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 208 | final UserManager mUserManager; |
Dianne Hackborn | 4249917 | 2010-10-15 18:45:07 -0700 | [diff] [blame] | 209 | final PowerManager.WakeLock mWakeLock; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 210 | |
Svet Ganov | 6bd7025 | 2014-08-20 09:47:47 -0700 | [diff] [blame] | 211 | final LocalService mLocalService; |
| 212 | |
Jeff Brown | 5ce1cb2 | 2014-11-06 19:05:33 -0800 | [diff] [blame] | 213 | final PowerManager mPowerManager; |
| 214 | final PowerManagerInternal mPowerManagerInternal; |
| 215 | |
Jim Miller | 93c518e | 2012-01-17 15:55:31 -0800 | [diff] [blame] | 216 | IWindowManager mIWindowManager; |
Maggie Benthall | 0469f41 | 2013-09-05 15:30:26 -0400 | [diff] [blame] | 217 | NotificationManager mNotificationManager; |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 218 | |
Adam Connors | 776c555 | 2014-01-09 10:42:56 +0000 | [diff] [blame] | 219 | // Stores and loads state on device and profile owners. |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 220 | private DeviceOwner mDeviceOwner; |
| 221 | |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 222 | /** |
| 223 | * Whether or not device admin feature is supported. If it isn't return defaults for all |
| 224 | * public methods. |
| 225 | */ |
| 226 | private boolean mHasFeature; |
| 227 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 228 | public static final class Lifecycle extends SystemService { |
| 229 | private DevicePolicyManagerService mService; |
| 230 | |
| 231 | public Lifecycle(Context context) { |
| 232 | super(context); |
| 233 | mService = new DevicePolicyManagerService(context); |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public void onStart() { |
| 238 | publishBinderService(Context.DEVICE_POLICY_SERVICE, mService); |
| 239 | } |
| 240 | |
| 241 | @Override |
| 242 | public void onBootPhase(int phase) { |
| 243 | if (phase == PHASE_LOCK_SETTINGS_READY) { |
| 244 | mService.systemReady(); |
| 245 | } |
| 246 | } |
| 247 | } |
Amith Yamasani | ed239cb | 2014-07-16 17:26:17 -0700 | [diff] [blame] | 248 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 249 | public static class DevicePolicyData { |
| 250 | int mActivePasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; |
| 251 | int mActivePasswordLength = 0; |
| 252 | int mActivePasswordUpperCase = 0; |
| 253 | int mActivePasswordLowerCase = 0; |
| 254 | int mActivePasswordLetters = 0; |
| 255 | int mActivePasswordNumeric = 0; |
| 256 | int mActivePasswordSymbols = 0; |
| 257 | int mActivePasswordNonLetter = 0; |
| 258 | int mFailedPasswordAttempts = 0; |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 259 | |
Robin Lee | 5c921da | 2014-03-24 15:11:35 +0000 | [diff] [blame] | 260 | int mUserHandle; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 261 | int mPasswordOwner = -1; |
| 262 | long mLastMaximumTimeToLock = -1; |
Amith Yamasani | ed239cb | 2014-07-16 17:26:17 -0700 | [diff] [blame] | 263 | boolean mUserSetupComplete = false; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 264 | |
| 265 | final HashMap<ComponentName, ActiveAdmin> mAdminMap |
| 266 | = new HashMap<ComponentName, ActiveAdmin>(); |
| 267 | final ArrayList<ActiveAdmin> mAdminList |
| 268 | = new ArrayList<ActiveAdmin>(); |
Fyodor Kupolov | 96fb932 | 2014-12-01 15:08:09 -0800 | [diff] [blame] | 269 | final ArrayList<ComponentName> mRemovingAdmins |
| 270 | = new ArrayList<ComponentName>(); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 271 | |
justinzhang | 511e0d8 | 2014-03-24 16:09:24 -0400 | [diff] [blame] | 272 | // This is the list of component allowed to start lock task mode. |
Jason Monk | d7b8621 | 2014-06-16 13:15:38 -0400 | [diff] [blame] | 273 | final List<String> mLockTaskPackages = new ArrayList<String>(); |
justinzhang | 511e0d8 | 2014-03-24 16:09:24 -0400 | [diff] [blame] | 274 | |
Amith Yamasani | f20d640 | 2014-05-24 15:34:37 -0700 | [diff] [blame] | 275 | ComponentName mRestrictionsProvider; |
| 276 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 277 | public DevicePolicyData(int userHandle) { |
| 278 | mUserHandle = userHandle; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | final SparseArray<DevicePolicyData> mUserData = new SparseArray<DevicePolicyData>(); |
| 283 | |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 284 | Handler mHandler = new Handler(); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 285 | |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 286 | BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 287 | @Override |
| 288 | public void onReceive(Context context, Intent intent) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 289 | final String action = intent.getAction(); |
| 290 | final int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, |
| 291 | getSendingUserId()); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 292 | if (Intent.ACTION_BOOT_COMPLETED.equals(action) |
| 293 | || ACTION_EXPIRED_PASSWORD_NOTIFICATION.equals(action)) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 294 | if (DBG) Slog.v(LOG_TAG, "Sending password expiration notifications for action " |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 295 | + action + " for user " + userHandle); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 296 | mHandler.post(new Runnable() { |
| 297 | public void run() { |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 298 | handlePasswordExpirationNotification(userHandle); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 299 | } |
| 300 | }); |
Maggie Benthall | 0469f41 | 2013-09-05 15:30:26 -0400 | [diff] [blame] | 301 | } |
| 302 | if (Intent.ACTION_BOOT_COMPLETED.equals(action) |
| 303 | || KeyChain.ACTION_STORAGE_CHANGED.equals(action)) { |
Robin Lee | d0130e8 | 2014-10-06 11:36:25 +0100 | [diff] [blame] | 304 | new MonitoringCertNotificationTask().execute(intent); |
Maggie Benthall | 0469f41 | 2013-09-05 15:30:26 -0400 | [diff] [blame] | 305 | } |
| 306 | if (Intent.ACTION_USER_REMOVED.equals(action)) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 307 | removeUserData(userHandle); |
| 308 | } else if (Intent.ACTION_USER_STARTED.equals(action) |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 309 | || Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE.equals(action)) { |
| 310 | |
| 311 | if (Intent.ACTION_USER_STARTED.equals(action)) { |
| 312 | // Reset the policy data |
| 313 | synchronized (DevicePolicyManagerService.this) { |
| 314 | mUserData.remove(userHandle); |
| 315 | } |
| 316 | } |
Amith Yamasani | a0116af | 2014-12-04 14:49:24 -0800 | [diff] [blame] | 317 | handlePackagesChanged(null /* check all admins */, userHandle); |
| 318 | } else if (Intent.ACTION_PACKAGE_CHANGED.equals(action) |
| 319 | || (Intent.ACTION_PACKAGE_ADDED.equals(action) |
| 320 | && intent.getBooleanExtra(Intent.EXTRA_REPLACING, false))) { |
| 321 | handlePackagesChanged(intent.getData().getSchemeSpecificPart(), userHandle); |
| 322 | } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action) |
| 323 | && !intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { |
| 324 | handlePackagesChanged(intent.getData().getSchemeSpecificPart(), userHandle); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | }; |
| 328 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 329 | static class ActiveAdmin { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 330 | private static final String TAG_DISABLE_KEYGUARD_FEATURES = "disable-keyguard-features"; |
| 331 | private static final String TAG_DISABLE_CAMERA = "disable-camera"; |
Adam Connors | 210fe21 | 2014-07-17 15:41:43 +0100 | [diff] [blame] | 332 | private static final String TAG_DISABLE_CALLER_ID = "disable-caller-id"; |
Sander Alewijnse | d2a1eec | 2014-07-09 12:57:05 +0100 | [diff] [blame] | 333 | private static final String TAG_DISABLE_SCREEN_CAPTURE = "disable-screen-capture"; |
Sander Alewijnse | 5c02db6 | 2014-05-07 10:46:57 +0100 | [diff] [blame] | 334 | private static final String TAG_DISABLE_ACCOUNT_MANAGEMENT = "disable-account-management"; |
Sander Alewijnse | 0ced627 | 2014-08-26 11:18:26 +0100 | [diff] [blame] | 335 | private static final String TAG_REQUIRE_AUTO_TIME = "require_auto_time"; |
Sander Alewijnse | 5c02db6 | 2014-05-07 10:46:57 +0100 | [diff] [blame] | 336 | private static final String TAG_ACCOUNT_TYPE = "account-type"; |
Kenny Guy | fa80a4f | 2014-08-20 19:40:59 +0100 | [diff] [blame] | 337 | private static final String TAG_PERMITTED_ACCESSIBILITY_SERVICES |
| 338 | = "permitted-accessiblity-services"; |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 339 | private static final String TAG_ENCRYPTION_REQUESTED = "encryption-requested"; |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 340 | private static final String TAG_MANAGE_TRUST_AGENT_FEATURES = "manage-trust-agent-features"; |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 341 | private static final String TAG_TRUST_AGENT_COMPONENT_OPTIONS = "trust-agent-component-options"; |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 342 | private static final String TAG_TRUST_AGENT_COMPONENT = "component"; |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 343 | private static final String TAG_PASSWORD_EXPIRATION_DATE = "password-expiration-date"; |
| 344 | private static final String TAG_PASSWORD_EXPIRATION_TIMEOUT = "password-expiration-timeout"; |
| 345 | private static final String TAG_GLOBAL_PROXY_EXCLUSION_LIST = "global-proxy-exclusion-list"; |
| 346 | private static final String TAG_GLOBAL_PROXY_SPEC = "global-proxy-spec"; |
| 347 | private static final String TAG_SPECIFIES_GLOBAL_PROXY = "specifies-global-proxy"; |
Kenny Guy | fa80a4f | 2014-08-20 19:40:59 +0100 | [diff] [blame] | 348 | private static final String TAG_PERMITTED_IMES = "permitted-imes"; |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 349 | private static final String TAG_MAX_FAILED_PASSWORD_WIPE = "max-failed-password-wipe"; |
| 350 | private static final String TAG_MAX_TIME_TO_UNLOCK = "max-time-to-unlock"; |
| 351 | private static final String TAG_MIN_PASSWORD_NONLETTER = "min-password-nonletter"; |
| 352 | private static final String TAG_MIN_PASSWORD_SYMBOLS = "min-password-symbols"; |
| 353 | private static final String TAG_MIN_PASSWORD_NUMERIC = "min-password-numeric"; |
| 354 | private static final String TAG_MIN_PASSWORD_LETTERS = "min-password-letters"; |
| 355 | private static final String TAG_MIN_PASSWORD_LOWERCASE = "min-password-lowercase"; |
| 356 | private static final String TAG_MIN_PASSWORD_UPPERCASE = "min-password-uppercase"; |
| 357 | private static final String TAG_PASSWORD_HISTORY_LENGTH = "password-history-length"; |
| 358 | private static final String TAG_MIN_PASSWORD_LENGTH = "min-password-length"; |
| 359 | private static final String ATTR_VALUE = "value"; |
| 360 | private static final String TAG_PASSWORD_QUALITY = "password-quality"; |
| 361 | private static final String TAG_POLICIES = "policies"; |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 362 | private static final String TAG_CROSS_PROFILE_WIDGET_PROVIDERS = |
| 363 | "cross-profile-widget-providers"; |
| 364 | private static final String TAG_PROVIDER = "provider"; |
Kenny Guy | fa80a4f | 2014-08-20 19:40:59 +0100 | [diff] [blame] | 365 | private static final String TAG_PACKAGE_LIST_ITEM = "item"; |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 366 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 367 | final DeviceAdminInfo info; |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 368 | |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 369 | int passwordQuality = DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 370 | |
| 371 | static final int DEF_MINIMUM_PASSWORD_LENGTH = 0; |
| 372 | int minimumPasswordLength = DEF_MINIMUM_PASSWORD_LENGTH; |
| 373 | |
| 374 | static final int DEF_PASSWORD_HISTORY_LENGTH = 0; |
| 375 | int passwordHistoryLength = DEF_PASSWORD_HISTORY_LENGTH; |
| 376 | |
| 377 | static final int DEF_MINIMUM_PASSWORD_UPPER_CASE = 0; |
| 378 | int minimumPasswordUpperCase = DEF_MINIMUM_PASSWORD_UPPER_CASE; |
| 379 | |
| 380 | static final int DEF_MINIMUM_PASSWORD_LOWER_CASE = 0; |
| 381 | int minimumPasswordLowerCase = DEF_MINIMUM_PASSWORD_LOWER_CASE; |
| 382 | |
xusai | 5787785 | 2015-08-18 17:33:00 +0800 | [diff] [blame] | 383 | static final int DEF_MINIMUM_PASSWORD_LETTERS = 0; |
Dianne Hackborn | 2a10840 | 2011-05-26 11:45:00 -0700 | [diff] [blame] | 384 | int minimumPasswordLetters = DEF_MINIMUM_PASSWORD_LETTERS; |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 385 | |
xusai | 5787785 | 2015-08-18 17:33:00 +0800 | [diff] [blame] | 386 | static final int DEF_MINIMUM_PASSWORD_NUMERIC = 0; |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 387 | int minimumPasswordNumeric = DEF_MINIMUM_PASSWORD_NUMERIC; |
| 388 | |
xusai | 5787785 | 2015-08-18 17:33:00 +0800 | [diff] [blame] | 389 | static final int DEF_MINIMUM_PASSWORD_SYMBOLS = 0; |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 390 | int minimumPasswordSymbols = DEF_MINIMUM_PASSWORD_SYMBOLS; |
| 391 | |
| 392 | static final int DEF_MINIMUM_PASSWORD_NON_LETTER = 0; |
| 393 | int minimumPasswordNonLetter = DEF_MINIMUM_PASSWORD_NON_LETTER; |
| 394 | |
| 395 | static final long DEF_MAXIMUM_TIME_TO_UNLOCK = 0; |
| 396 | long maximumTimeToUnlock = DEF_MAXIMUM_TIME_TO_UNLOCK; |
| 397 | |
| 398 | static final int DEF_MAXIMUM_FAILED_PASSWORDS_FOR_WIPE = 0; |
| 399 | int maximumFailedPasswordsForWipe = DEF_MAXIMUM_FAILED_PASSWORDS_FOR_WIPE; |
| 400 | |
| 401 | static final long DEF_PASSWORD_EXPIRATION_TIMEOUT = 0; |
| 402 | long passwordExpirationTimeout = DEF_PASSWORD_EXPIRATION_TIMEOUT; |
| 403 | |
| 404 | static final long DEF_PASSWORD_EXPIRATION_DATE = 0; |
| 405 | long passwordExpirationDate = DEF_PASSWORD_EXPIRATION_DATE; |
| 406 | |
Jim Miller | 48b9b0d | 2012-09-19 23:16:50 -0700 | [diff] [blame] | 407 | static final int DEF_KEYGUARD_FEATURES_DISABLED = 0; // none |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 408 | |
Jim Miller | 48b9b0d | 2012-09-19 23:16:50 -0700 | [diff] [blame] | 409 | int disabledKeyguardFeatures = DEF_KEYGUARD_FEATURES_DISABLED; |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 410 | |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 411 | boolean encryptionRequested = false; |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 412 | boolean disableCamera = false; |
Adam Connors | 210fe21 | 2014-07-17 15:41:43 +0100 | [diff] [blame] | 413 | boolean disableCallerId = false; |
Sander Alewijnse | d2a1eec | 2014-07-09 12:57:05 +0100 | [diff] [blame] | 414 | boolean disableScreenCapture = false; // Can only be set by a device/profile owner. |
Sander Alewijnse | 0ced627 | 2014-08-26 11:18:26 +0100 | [diff] [blame] | 415 | boolean requireAutoTime = false; // Can only be set by a device owner. |
Sander Alewijnse | d2a1eec | 2014-07-09 12:57:05 +0100 | [diff] [blame] | 416 | |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 417 | static class TrustAgentInfo { |
| 418 | public PersistableBundle options; |
| 419 | TrustAgentInfo(PersistableBundle bundle) { |
| 420 | options = bundle; |
| 421 | } |
| 422 | } |
| 423 | |
Sander Alewijnse | 5c02db6 | 2014-05-07 10:46:57 +0100 | [diff] [blame] | 424 | Set<String> accountTypesWithManagementDisabled = new HashSet<String>(); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 425 | |
Kenny Guy | fa80a4f | 2014-08-20 19:40:59 +0100 | [diff] [blame] | 426 | // The list of permitted accessibility services package namesas set by a profile |
| 427 | // or device owner. Null means all accessibility services are allowed, empty means |
| 428 | // none except system services are allowed. |
| 429 | List<String> permittedAccessiblityServices; |
| 430 | |
| 431 | // The list of permitted input methods package names as set by a profile or device owner. |
| 432 | // Null means all input methods are allowed, empty means none except system imes are |
| 433 | // allowed. |
| 434 | List<String> permittedInputMethods; |
| 435 | |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 436 | // TODO: review implementation decisions with frameworks team |
| 437 | boolean specifiesGlobalProxy = false; |
| 438 | String globalProxySpec = null; |
| 439 | String globalProxyExclusionList = null; |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 440 | |
| 441 | HashMap<String, TrustAgentInfo> trustAgentInfos = new HashMap<String, TrustAgentInfo>(); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 442 | |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 443 | List<String> crossProfileWidgetProviders; |
| 444 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 445 | ActiveAdmin(DeviceAdminInfo _info) { |
| 446 | info = _info; |
| 447 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 448 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 449 | int getUid() { return info.getActivityInfo().applicationInfo.uid; } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 450 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 451 | public UserHandle getUserHandle() { |
| 452 | return new UserHandle(UserHandle.getUserId(info.getActivityInfo().applicationInfo.uid)); |
| 453 | } |
| 454 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 455 | void writeToXml(XmlSerializer out) |
| 456 | throws IllegalArgumentException, IllegalStateException, IOException { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 457 | out.startTag(null, TAG_POLICIES); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 458 | info.writePoliciesToXml(out); |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 459 | out.endTag(null, TAG_POLICIES); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 460 | if (passwordQuality != DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 461 | out.startTag(null, TAG_PASSWORD_QUALITY); |
| 462 | out.attribute(null, ATTR_VALUE, Integer.toString(passwordQuality)); |
| 463 | out.endTag(null, TAG_PASSWORD_QUALITY); |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 464 | if (minimumPasswordLength != DEF_MINIMUM_PASSWORD_LENGTH) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 465 | out.startTag(null, TAG_MIN_PASSWORD_LENGTH); |
| 466 | out.attribute(null, ATTR_VALUE, Integer.toString(minimumPasswordLength)); |
| 467 | out.endTag(null, TAG_MIN_PASSWORD_LENGTH); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 468 | } |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 469 | if(passwordHistoryLength != DEF_PASSWORD_HISTORY_LENGTH) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 470 | out.startTag(null, TAG_PASSWORD_HISTORY_LENGTH); |
| 471 | out.attribute(null, ATTR_VALUE, Integer.toString(passwordHistoryLength)); |
| 472 | out.endTag(null, TAG_PASSWORD_HISTORY_LENGTH); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 473 | } |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 474 | if (minimumPasswordUpperCase != DEF_MINIMUM_PASSWORD_UPPER_CASE) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 475 | out.startTag(null, TAG_MIN_PASSWORD_UPPERCASE); |
| 476 | out.attribute(null, ATTR_VALUE, Integer.toString(minimumPasswordUpperCase)); |
| 477 | out.endTag(null, TAG_MIN_PASSWORD_UPPERCASE); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 478 | } |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 479 | if (minimumPasswordLowerCase != DEF_MINIMUM_PASSWORD_LOWER_CASE) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 480 | out.startTag(null, TAG_MIN_PASSWORD_LOWERCASE); |
| 481 | out.attribute(null, ATTR_VALUE, Integer.toString(minimumPasswordLowerCase)); |
| 482 | out.endTag(null, TAG_MIN_PASSWORD_LOWERCASE); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 483 | } |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 484 | if (minimumPasswordLetters != DEF_MINIMUM_PASSWORD_LETTERS) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 485 | out.startTag(null, TAG_MIN_PASSWORD_LETTERS); |
| 486 | out.attribute(null, ATTR_VALUE, Integer.toString(minimumPasswordLetters)); |
| 487 | out.endTag(null, TAG_MIN_PASSWORD_LETTERS); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 488 | } |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 489 | if (minimumPasswordNumeric != DEF_MINIMUM_PASSWORD_NUMERIC) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 490 | out.startTag(null, TAG_MIN_PASSWORD_NUMERIC); |
| 491 | out.attribute(null, ATTR_VALUE, Integer.toString(minimumPasswordNumeric)); |
| 492 | out.endTag(null, TAG_MIN_PASSWORD_NUMERIC); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 493 | } |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 494 | if (minimumPasswordSymbols != DEF_MINIMUM_PASSWORD_SYMBOLS) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 495 | out.startTag(null, TAG_MIN_PASSWORD_SYMBOLS); |
| 496 | out.attribute(null, ATTR_VALUE, Integer.toString(minimumPasswordSymbols)); |
| 497 | out.endTag(null, TAG_MIN_PASSWORD_SYMBOLS); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 498 | } |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 499 | if (minimumPasswordNonLetter > DEF_MINIMUM_PASSWORD_NON_LETTER) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 500 | out.startTag(null, TAG_MIN_PASSWORD_NONLETTER); |
| 501 | out.attribute(null, ATTR_VALUE, Integer.toString(minimumPasswordNonLetter)); |
| 502 | out.endTag(null, TAG_MIN_PASSWORD_NONLETTER); |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 503 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 504 | } |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 505 | if (maximumTimeToUnlock != DEF_MAXIMUM_TIME_TO_UNLOCK) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 506 | out.startTag(null, TAG_MAX_TIME_TO_UNLOCK); |
| 507 | out.attribute(null, ATTR_VALUE, Long.toString(maximumTimeToUnlock)); |
| 508 | out.endTag(null, TAG_MAX_TIME_TO_UNLOCK); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 509 | } |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 510 | if (maximumFailedPasswordsForWipe != DEF_MAXIMUM_FAILED_PASSWORDS_FOR_WIPE) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 511 | out.startTag(null, TAG_MAX_FAILED_PASSWORD_WIPE); |
| 512 | out.attribute(null, ATTR_VALUE, Integer.toString(maximumFailedPasswordsForWipe)); |
| 513 | out.endTag(null, TAG_MAX_FAILED_PASSWORD_WIPE); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 514 | } |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 515 | if (specifiesGlobalProxy) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 516 | out.startTag(null, TAG_SPECIFIES_GLOBAL_PROXY); |
| 517 | out.attribute(null, ATTR_VALUE, Boolean.toString(specifiesGlobalProxy)); |
| 518 | out.endTag(null, TAG_SPECIFIES_GLOBAL_PROXY); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 519 | if (globalProxySpec != null) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 520 | out.startTag(null, TAG_GLOBAL_PROXY_SPEC); |
| 521 | out.attribute(null, ATTR_VALUE, globalProxySpec); |
| 522 | out.endTag(null, TAG_GLOBAL_PROXY_SPEC); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 523 | } |
| 524 | if (globalProxyExclusionList != null) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 525 | out.startTag(null, TAG_GLOBAL_PROXY_EXCLUSION_LIST); |
| 526 | out.attribute(null, ATTR_VALUE, globalProxyExclusionList); |
| 527 | out.endTag(null, TAG_GLOBAL_PROXY_EXCLUSION_LIST); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 528 | } |
| 529 | } |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 530 | if (passwordExpirationTimeout != DEF_PASSWORD_EXPIRATION_TIMEOUT) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 531 | out.startTag(null, TAG_PASSWORD_EXPIRATION_TIMEOUT); |
| 532 | out.attribute(null, ATTR_VALUE, Long.toString(passwordExpirationTimeout)); |
| 533 | out.endTag(null, TAG_PASSWORD_EXPIRATION_TIMEOUT); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 534 | } |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 535 | if (passwordExpirationDate != DEF_PASSWORD_EXPIRATION_DATE) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 536 | out.startTag(null, TAG_PASSWORD_EXPIRATION_DATE); |
| 537 | out.attribute(null, ATTR_VALUE, Long.toString(passwordExpirationDate)); |
| 538 | out.endTag(null, TAG_PASSWORD_EXPIRATION_DATE); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 539 | } |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 540 | if (encryptionRequested) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 541 | out.startTag(null, TAG_ENCRYPTION_REQUESTED); |
| 542 | out.attribute(null, ATTR_VALUE, Boolean.toString(encryptionRequested)); |
| 543 | out.endTag(null, TAG_ENCRYPTION_REQUESTED); |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 544 | } |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 545 | if (disableCamera) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 546 | out.startTag(null, TAG_DISABLE_CAMERA); |
| 547 | out.attribute(null, ATTR_VALUE, Boolean.toString(disableCamera)); |
| 548 | out.endTag(null, TAG_DISABLE_CAMERA); |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 549 | } |
Adam Connors | 210fe21 | 2014-07-17 15:41:43 +0100 | [diff] [blame] | 550 | if (disableCallerId) { |
| 551 | out.startTag(null, TAG_DISABLE_CALLER_ID); |
| 552 | out.attribute(null, ATTR_VALUE, Boolean.toString(disableCallerId)); |
| 553 | out.endTag(null, TAG_DISABLE_CALLER_ID); |
| 554 | } |
Sander Alewijnse | d2a1eec | 2014-07-09 12:57:05 +0100 | [diff] [blame] | 555 | if (disableScreenCapture) { |
| 556 | out.startTag(null, TAG_DISABLE_SCREEN_CAPTURE); |
| 557 | out.attribute(null, ATTR_VALUE, Boolean.toString(disableScreenCapture)); |
| 558 | out.endTag(null, TAG_DISABLE_SCREEN_CAPTURE); |
| 559 | } |
Sander Alewijnse | 0ced627 | 2014-08-26 11:18:26 +0100 | [diff] [blame] | 560 | if (requireAutoTime) { |
| 561 | out.startTag(null, TAG_REQUIRE_AUTO_TIME); |
| 562 | out.attribute(null, ATTR_VALUE, Boolean.toString(requireAutoTime)); |
| 563 | out.endTag(null, TAG_REQUIRE_AUTO_TIME); |
| 564 | } |
Jim Miller | 48b9b0d | 2012-09-19 23:16:50 -0700 | [diff] [blame] | 565 | if (disabledKeyguardFeatures != DEF_KEYGUARD_FEATURES_DISABLED) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 566 | out.startTag(null, TAG_DISABLE_KEYGUARD_FEATURES); |
| 567 | out.attribute(null, ATTR_VALUE, Integer.toString(disabledKeyguardFeatures)); |
| 568 | out.endTag(null, TAG_DISABLE_KEYGUARD_FEATURES); |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 569 | } |
Sander Alewijnse | 5c02db6 | 2014-05-07 10:46:57 +0100 | [diff] [blame] | 570 | if (!accountTypesWithManagementDisabled.isEmpty()) { |
| 571 | out.startTag(null, TAG_DISABLE_ACCOUNT_MANAGEMENT); |
| 572 | for (String ac : accountTypesWithManagementDisabled) { |
| 573 | out.startTag(null, TAG_ACCOUNT_TYPE); |
| 574 | out.attribute(null, ATTR_VALUE, ac); |
| 575 | out.endTag(null, TAG_ACCOUNT_TYPE); |
| 576 | } |
| 577 | out.endTag(null, TAG_DISABLE_ACCOUNT_MANAGEMENT); |
| 578 | } |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 579 | if (!trustAgentInfos.isEmpty()) { |
| 580 | Set<Entry<String, TrustAgentInfo>> set = trustAgentInfos.entrySet(); |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 581 | out.startTag(null, TAG_MANAGE_TRUST_AGENT_FEATURES); |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 582 | for (Entry<String, TrustAgentInfo> entry : set) { |
| 583 | TrustAgentInfo trustAgentInfo = entry.getValue(); |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 584 | out.startTag(null, TAG_TRUST_AGENT_COMPONENT); |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 585 | out.attribute(null, ATTR_VALUE, entry.getKey()); |
| 586 | if (trustAgentInfo.options != null) { |
| 587 | out.startTag(null, TAG_TRUST_AGENT_COMPONENT_OPTIONS); |
| 588 | try { |
| 589 | trustAgentInfo.options.saveToXml(out); |
| 590 | } catch (XmlPullParserException e) { |
| 591 | Log.e(LOG_TAG, "Failed to save TrustAgent options", e); |
| 592 | } |
| 593 | out.endTag(null, TAG_TRUST_AGENT_COMPONENT_OPTIONS); |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 594 | } |
| 595 | out.endTag(null, TAG_TRUST_AGENT_COMPONENT); |
| 596 | } |
| 597 | out.endTag(null, TAG_MANAGE_TRUST_AGENT_FEATURES); |
| 598 | } |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 599 | if (crossProfileWidgetProviders != null && !crossProfileWidgetProviders.isEmpty()) { |
| 600 | out.startTag(null, TAG_CROSS_PROFILE_WIDGET_PROVIDERS); |
| 601 | final int providerCount = crossProfileWidgetProviders.size(); |
| 602 | for (int i = 0; i < providerCount; i++) { |
| 603 | String provider = crossProfileWidgetProviders.get(i); |
| 604 | out.startTag(null, TAG_PROVIDER); |
| 605 | out.attribute(null, ATTR_VALUE, provider); |
| 606 | out.endTag(null, TAG_PROVIDER); |
| 607 | } |
| 608 | out.endTag(null, TAG_CROSS_PROFILE_WIDGET_PROVIDERS); |
| 609 | } |
Kenny Guy | fa80a4f | 2014-08-20 19:40:59 +0100 | [diff] [blame] | 610 | writePackageListToXml(out, TAG_PERMITTED_ACCESSIBILITY_SERVICES, |
| 611 | permittedAccessiblityServices); |
| 612 | writePackageListToXml(out, TAG_PERMITTED_IMES, permittedInputMethods); |
| 613 | } |
| 614 | |
| 615 | void writePackageListToXml(XmlSerializer out, String outerTag, |
| 616 | List<String> packageList) |
| 617 | throws IllegalArgumentException, IllegalStateException, IOException { |
| 618 | if (packageList == null) { |
| 619 | return; |
| 620 | } |
| 621 | |
| 622 | out.startTag(null, outerTag); |
| 623 | for (String packageName : packageList) { |
| 624 | out.startTag(null, TAG_PACKAGE_LIST_ITEM); |
| 625 | out.attribute(null, ATTR_VALUE, packageName); |
| 626 | out.endTag(null, TAG_PACKAGE_LIST_ITEM); |
| 627 | } |
| 628 | out.endTag(null, outerTag); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 629 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 630 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 631 | void readFromXml(XmlPullParser parser) |
| 632 | throws XmlPullParserException, IOException { |
| 633 | int outerDepth = parser.getDepth(); |
| 634 | int type; |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 635 | while ((type=parser.next()) != END_DOCUMENT |
| 636 | && (type != END_TAG || parser.getDepth() > outerDepth)) { |
| 637 | if (type == END_TAG || type == TEXT) { |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 638 | continue; |
| 639 | } |
| 640 | String tag = parser.getName(); |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 641 | if (TAG_POLICIES.equals(tag)) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 642 | info.readPoliciesFromXml(parser); |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 643 | } else if (TAG_PASSWORD_QUALITY.equals(tag)) { |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 644 | passwordQuality = Integer.parseInt( |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 645 | parser.getAttributeValue(null, ATTR_VALUE)); |
| 646 | } else if (TAG_MIN_PASSWORD_LENGTH.equals(tag)) { |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 647 | minimumPasswordLength = Integer.parseInt( |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 648 | parser.getAttributeValue(null, ATTR_VALUE)); |
| 649 | } else if (TAG_PASSWORD_HISTORY_LENGTH.equals(tag)) { |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 650 | passwordHistoryLength = Integer.parseInt( |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 651 | parser.getAttributeValue(null, ATTR_VALUE)); |
| 652 | } else if (TAG_MIN_PASSWORD_UPPERCASE.equals(tag)) { |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 653 | minimumPasswordUpperCase = Integer.parseInt( |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 654 | parser.getAttributeValue(null, ATTR_VALUE)); |
| 655 | } else if (TAG_MIN_PASSWORD_LOWERCASE.equals(tag)) { |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 656 | minimumPasswordLowerCase = Integer.parseInt( |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 657 | parser.getAttributeValue(null, ATTR_VALUE)); |
| 658 | } else if (TAG_MIN_PASSWORD_LETTERS.equals(tag)) { |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 659 | minimumPasswordLetters = Integer.parseInt( |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 660 | parser.getAttributeValue(null, ATTR_VALUE)); |
| 661 | } else if (TAG_MIN_PASSWORD_NUMERIC.equals(tag)) { |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 662 | minimumPasswordNumeric = Integer.parseInt( |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 663 | parser.getAttributeValue(null, ATTR_VALUE)); |
| 664 | } else if (TAG_MIN_PASSWORD_SYMBOLS.equals(tag)) { |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 665 | minimumPasswordSymbols = Integer.parseInt( |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 666 | parser.getAttributeValue(null, ATTR_VALUE)); |
| 667 | } else if (TAG_MIN_PASSWORD_NONLETTER.equals(tag)) { |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 668 | minimumPasswordNonLetter = Integer.parseInt( |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 669 | parser.getAttributeValue(null, ATTR_VALUE)); |
| 670 | } else if (TAG_MAX_TIME_TO_UNLOCK.equals(tag)) { |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 671 | maximumTimeToUnlock = Long.parseLong( |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 672 | parser.getAttributeValue(null, ATTR_VALUE)); |
| 673 | } else if (TAG_MAX_FAILED_PASSWORD_WIPE.equals(tag)) { |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 674 | maximumFailedPasswordsForWipe = Integer.parseInt( |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 675 | parser.getAttributeValue(null, ATTR_VALUE)); |
| 676 | } else if (TAG_SPECIFIES_GLOBAL_PROXY.equals(tag)) { |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 677 | specifiesGlobalProxy = Boolean.parseBoolean( |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 678 | parser.getAttributeValue(null, ATTR_VALUE)); |
| 679 | } else if (TAG_GLOBAL_PROXY_SPEC.equals(tag)) { |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 680 | globalProxySpec = |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 681 | parser.getAttributeValue(null, ATTR_VALUE); |
| 682 | } else if (TAG_GLOBAL_PROXY_EXCLUSION_LIST.equals(tag)) { |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 683 | globalProxyExclusionList = |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 684 | parser.getAttributeValue(null, ATTR_VALUE); |
| 685 | } else if (TAG_PASSWORD_EXPIRATION_TIMEOUT.equals(tag)) { |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 686 | passwordExpirationTimeout = Long.parseLong( |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 687 | parser.getAttributeValue(null, ATTR_VALUE)); |
| 688 | } else if (TAG_PASSWORD_EXPIRATION_DATE.equals(tag)) { |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 689 | passwordExpirationDate = Long.parseLong( |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 690 | parser.getAttributeValue(null, ATTR_VALUE)); |
| 691 | } else if (TAG_ENCRYPTION_REQUESTED.equals(tag)) { |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 692 | encryptionRequested = Boolean.parseBoolean( |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 693 | parser.getAttributeValue(null, ATTR_VALUE)); |
| 694 | } else if (TAG_DISABLE_CAMERA.equals(tag)) { |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 695 | disableCamera = Boolean.parseBoolean( |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 696 | parser.getAttributeValue(null, ATTR_VALUE)); |
Adam Connors | 210fe21 | 2014-07-17 15:41:43 +0100 | [diff] [blame] | 697 | } else if (TAG_DISABLE_CALLER_ID.equals(tag)) { |
| 698 | disableCallerId = Boolean.parseBoolean( |
| 699 | parser.getAttributeValue(null, ATTR_VALUE)); |
Sander Alewijnse | d2a1eec | 2014-07-09 12:57:05 +0100 | [diff] [blame] | 700 | } else if (TAG_DISABLE_SCREEN_CAPTURE.equals(tag)) { |
| 701 | disableScreenCapture = Boolean.parseBoolean( |
| 702 | parser.getAttributeValue(null, ATTR_VALUE)); |
Sander Alewijnse | 0ced627 | 2014-08-26 11:18:26 +0100 | [diff] [blame] | 703 | } else if (TAG_REQUIRE_AUTO_TIME.equals(tag)) { |
| 704 | requireAutoTime= Boolean.parseBoolean( |
| 705 | parser.getAttributeValue(null, ATTR_VALUE)); |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 706 | } else if (TAG_DISABLE_KEYGUARD_FEATURES.equals(tag)) { |
Amith Yamasani | 7077b3c | 2012-10-04 10:28:50 -0700 | [diff] [blame] | 707 | disabledKeyguardFeatures = Integer.parseInt( |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 708 | parser.getAttributeValue(null, ATTR_VALUE)); |
Sander Alewijnse | 5c02db6 | 2014-05-07 10:46:57 +0100 | [diff] [blame] | 709 | } else if (TAG_DISABLE_ACCOUNT_MANAGEMENT.equals(tag)) { |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 710 | accountTypesWithManagementDisabled = readDisableAccountInfo(parser, tag); |
| 711 | } else if (TAG_MANAGE_TRUST_AGENT_FEATURES.equals(tag)) { |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 712 | trustAgentInfos = getAllTrustAgentInfos(parser, tag); |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 713 | } else if (TAG_CROSS_PROFILE_WIDGET_PROVIDERS.equals(tag)) { |
| 714 | crossProfileWidgetProviders = getCrossProfileWidgetProviders(parser, tag); |
Kenny Guy | fa80a4f | 2014-08-20 19:40:59 +0100 | [diff] [blame] | 715 | } else if (TAG_PERMITTED_ACCESSIBILITY_SERVICES.equals(tag)) { |
| 716 | permittedAccessiblityServices = readPackageList(parser, tag); |
| 717 | } else if (TAG_PERMITTED_IMES.equals(tag)) { |
| 718 | permittedInputMethods = readPackageList(parser, tag); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 719 | } else { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 720 | Slog.w(LOG_TAG, "Unknown admin tag: " + tag); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 721 | } |
| 722 | XmlUtils.skipCurrentTag(parser); |
| 723 | } |
| 724 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 725 | |
Kenny Guy | fa80a4f | 2014-08-20 19:40:59 +0100 | [diff] [blame] | 726 | private List<String> readPackageList(XmlPullParser parser, |
| 727 | String tag) throws XmlPullParserException, IOException { |
| 728 | List<String> result = new ArrayList<String>(); |
| 729 | int outerDepth = parser.getDepth(); |
| 730 | int outerType; |
| 731 | while ((outerType=parser.next()) != XmlPullParser.END_DOCUMENT |
| 732 | && (outerType != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 733 | if (outerType == XmlPullParser.END_TAG || outerType == XmlPullParser.TEXT) { |
| 734 | continue; |
| 735 | } |
| 736 | String outerTag = parser.getName(); |
| 737 | if (TAG_PACKAGE_LIST_ITEM.equals(outerTag)) { |
| 738 | String packageName = parser.getAttributeValue(null, ATTR_VALUE); |
| 739 | if (packageName != null) { |
| 740 | result.add(packageName); |
| 741 | } else { |
| 742 | Slog.w(LOG_TAG, "Package name missing under " + outerTag); |
| 743 | } |
| 744 | } else { |
| 745 | Slog.w(LOG_TAG, "Unknown tag under " + tag + ": " + outerTag); |
| 746 | } |
| 747 | } |
| 748 | return result; |
| 749 | } |
| 750 | |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 751 | private Set<String> readDisableAccountInfo(XmlPullParser parser, String tag) |
| 752 | throws XmlPullParserException, IOException { |
| 753 | int outerDepthDAM = parser.getDepth(); |
| 754 | int typeDAM; |
| 755 | Set<String> result = new HashSet<String>(); |
| 756 | while ((typeDAM=parser.next()) != END_DOCUMENT |
| 757 | && (typeDAM != END_TAG || parser.getDepth() > outerDepthDAM)) { |
| 758 | if (typeDAM == END_TAG || typeDAM == TEXT) { |
| 759 | continue; |
| 760 | } |
| 761 | String tagDAM = parser.getName(); |
| 762 | if (TAG_ACCOUNT_TYPE.equals(tagDAM)) { |
| 763 | result.add(parser.getAttributeValue(null, ATTR_VALUE)); |
| 764 | } else { |
| 765 | Slog.w(LOG_TAG, "Unknown tag under " + tag + ": " + tagDAM); |
| 766 | } |
| 767 | } |
| 768 | return result; |
| 769 | } |
| 770 | |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 771 | private HashMap<String, TrustAgentInfo> getAllTrustAgentInfos( |
| 772 | XmlPullParser parser, String tag) throws XmlPullParserException, IOException { |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 773 | int outerDepthDAM = parser.getDepth(); |
| 774 | int typeDAM; |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 775 | HashMap<String, TrustAgentInfo> result = new HashMap<String, TrustAgentInfo>(); |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 776 | while ((typeDAM=parser.next()) != END_DOCUMENT |
| 777 | && (typeDAM != END_TAG || parser.getDepth() > outerDepthDAM)) { |
| 778 | if (typeDAM == END_TAG || typeDAM == TEXT) { |
| 779 | continue; |
| 780 | } |
| 781 | String tagDAM = parser.getName(); |
| 782 | if (TAG_TRUST_AGENT_COMPONENT.equals(tagDAM)) { |
| 783 | final String component = parser.getAttributeValue(null, ATTR_VALUE); |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 784 | final TrustAgentInfo trustAgentInfo = getTrustAgentInfo(parser, tag); |
| 785 | result.put(component, trustAgentInfo); |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 786 | } else { |
| 787 | Slog.w(LOG_TAG, "Unknown tag under " + tag + ": " + tagDAM); |
| 788 | } |
| 789 | } |
| 790 | return result; |
| 791 | } |
| 792 | |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 793 | private TrustAgentInfo getTrustAgentInfo(XmlPullParser parser, String tag) |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 794 | throws XmlPullParserException, IOException { |
| 795 | int outerDepthDAM = parser.getDepth(); |
| 796 | int typeDAM; |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 797 | TrustAgentInfo result = new TrustAgentInfo(null); |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 798 | while ((typeDAM=parser.next()) != END_DOCUMENT |
| 799 | && (typeDAM != END_TAG || parser.getDepth() > outerDepthDAM)) { |
| 800 | if (typeDAM == END_TAG || typeDAM == TEXT) { |
| 801 | continue; |
| 802 | } |
| 803 | String tagDAM = parser.getName(); |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 804 | if (TAG_TRUST_AGENT_COMPONENT_OPTIONS.equals(tagDAM)) { |
| 805 | PersistableBundle bundle = new PersistableBundle(); |
| 806 | bundle.restoreFromXml(parser); |
| 807 | result.options = bundle; |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 808 | } else { |
| 809 | Slog.w(LOG_TAG, "Unknown tag under " + tag + ": " + tagDAM); |
| 810 | } |
| 811 | } |
| 812 | return result; |
| 813 | } |
| 814 | |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 815 | private List<String> getCrossProfileWidgetProviders(XmlPullParser parser, String tag) |
| 816 | throws XmlPullParserException, IOException { |
| 817 | int outerDepthDAM = parser.getDepth(); |
| 818 | int typeDAM; |
| 819 | ArrayList<String> result = null; |
| 820 | while ((typeDAM=parser.next()) != END_DOCUMENT |
| 821 | && (typeDAM != END_TAG || parser.getDepth() > outerDepthDAM)) { |
| 822 | if (typeDAM == END_TAG || typeDAM == TEXT) { |
| 823 | continue; |
| 824 | } |
| 825 | String tagDAM = parser.getName(); |
| 826 | if (TAG_PROVIDER.equals(tagDAM)) { |
| 827 | final String provider = parser.getAttributeValue(null, ATTR_VALUE); |
| 828 | if (result == null) { |
| 829 | result = new ArrayList<>(); |
| 830 | } |
| 831 | result.add(provider); |
| 832 | } else { |
| 833 | Slog.w(LOG_TAG, "Unknown tag under " + tag + ": " + tagDAM); |
| 834 | } |
| 835 | } |
| 836 | return result; |
| 837 | } |
| 838 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 839 | void dump(String prefix, PrintWriter pw) { |
| 840 | pw.print(prefix); pw.print("uid="); pw.println(getUid()); |
| 841 | pw.print(prefix); pw.println("policies:"); |
| 842 | ArrayList<DeviceAdminInfo.PolicyInfo> pols = info.getUsedPolicies(); |
| 843 | if (pols != null) { |
| 844 | for (int i=0; i<pols.size(); i++) { |
| 845 | pw.print(prefix); pw.print(" "); pw.println(pols.get(i).tag); |
| 846 | } |
| 847 | } |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 848 | pw.print(prefix); pw.print("passwordQuality=0x"); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 849 | pw.println(Integer.toHexString(passwordQuality)); |
| 850 | pw.print(prefix); pw.print("minimumPasswordLength="); |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 851 | pw.println(minimumPasswordLength); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 852 | pw.print(prefix); pw.print("passwordHistoryLength="); |
| 853 | pw.println(passwordHistoryLength); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 854 | pw.print(prefix); pw.print("minimumPasswordUpperCase="); |
| 855 | pw.println(minimumPasswordUpperCase); |
| 856 | pw.print(prefix); pw.print("minimumPasswordLowerCase="); |
| 857 | pw.println(minimumPasswordLowerCase); |
| 858 | pw.print(prefix); pw.print("minimumPasswordLetters="); |
| 859 | pw.println(minimumPasswordLetters); |
| 860 | pw.print(prefix); pw.print("minimumPasswordNumeric="); |
| 861 | pw.println(minimumPasswordNumeric); |
| 862 | pw.print(prefix); pw.print("minimumPasswordSymbols="); |
| 863 | pw.println(minimumPasswordSymbols); |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 864 | pw.print(prefix); pw.print("minimumPasswordNonLetter="); |
| 865 | pw.println(minimumPasswordNonLetter); |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 866 | pw.print(prefix); pw.print("maximumTimeToUnlock="); |
| 867 | pw.println(maximumTimeToUnlock); |
| 868 | pw.print(prefix); pw.print("maximumFailedPasswordsForWipe="); |
| 869 | pw.println(maximumFailedPasswordsForWipe); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 870 | pw.print(prefix); pw.print("specifiesGlobalProxy="); |
| 871 | pw.println(specifiesGlobalProxy); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 872 | pw.print(prefix); pw.print("passwordExpirationTimeout="); |
| 873 | pw.println(passwordExpirationTimeout); |
| 874 | pw.print(prefix); pw.print("passwordExpirationDate="); |
| 875 | pw.println(passwordExpirationDate); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 876 | if (globalProxySpec != null) { |
| 877 | pw.print(prefix); pw.print("globalProxySpec="); |
| 878 | pw.println(globalProxySpec); |
| 879 | } |
| 880 | if (globalProxyExclusionList != null) { |
| 881 | pw.print(prefix); pw.print("globalProxyEclusionList="); |
| 882 | pw.println(globalProxyExclusionList); |
| 883 | } |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 884 | pw.print(prefix); pw.print("encryptionRequested="); |
| 885 | pw.println(encryptionRequested); |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 886 | pw.print(prefix); pw.print("disableCamera="); |
| 887 | pw.println(disableCamera); |
Adam Connors | 210fe21 | 2014-07-17 15:41:43 +0100 | [diff] [blame] | 888 | pw.print(prefix); pw.print("disableCallerId="); |
| 889 | pw.println(disableCallerId); |
Sander Alewijnse | d2a1eec | 2014-07-09 12:57:05 +0100 | [diff] [blame] | 890 | pw.print(prefix); pw.print("disableScreenCapture="); |
| 891 | pw.println(disableScreenCapture); |
Sander Alewijnse | 0ced627 | 2014-08-26 11:18:26 +0100 | [diff] [blame] | 892 | pw.print(prefix); pw.print("requireAutoTime="); |
| 893 | pw.println(requireAutoTime); |
Amith Yamasani | 7077b3c | 2012-10-04 10:28:50 -0700 | [diff] [blame] | 894 | pw.print(prefix); pw.print("disabledKeyguardFeatures="); |
| 895 | pw.println(disabledKeyguardFeatures); |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 896 | pw.print(prefix); pw.print("crossProfileWidgetProviders="); |
| 897 | pw.println(crossProfileWidgetProviders); |
Kenny Guy | fa80a4f | 2014-08-20 19:40:59 +0100 | [diff] [blame] | 898 | if (!(permittedAccessiblityServices == null)) { |
| 899 | pw.print(prefix); pw.print("permittedAccessibilityServices="); |
| 900 | pw.println(permittedAccessiblityServices.toString()); |
| 901 | } |
| 902 | if (!(permittedInputMethods == null)) { |
| 903 | pw.print(prefix); pw.print("permittedInputMethods="); |
| 904 | pw.println(permittedInputMethods.toString()); |
| 905 | } |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 906 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 907 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 908 | |
Amith Yamasani | a0116af | 2014-12-04 14:49:24 -0800 | [diff] [blame] | 909 | private void handlePackagesChanged(String packageName, int userHandle) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 910 | boolean removed = false; |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 911 | if (DBG) Slog.d(LOG_TAG, "Handling package changes for user " + userHandle); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 912 | DevicePolicyData policy = getUserData(userHandle); |
| 913 | IPackageManager pm = AppGlobals.getPackageManager(); |
Fyodor Kupolov | d01ff6d | 2014-11-12 10:46:50 -0800 | [diff] [blame] | 914 | synchronized (this) { |
| 915 | for (int i = policy.mAdminList.size() - 1; i >= 0; i--) { |
| 916 | ActiveAdmin aa = policy.mAdminList.get(i); |
| 917 | try { |
Amith Yamasani | a0116af | 2014-12-04 14:49:24 -0800 | [diff] [blame] | 918 | // If we're checking all packages or if the specific one we're checking matches, |
| 919 | // then check if the package and receiver still exist. |
| 920 | final String adminPackage = aa.info.getPackageName(); |
| 921 | if (packageName == null || packageName.equals(adminPackage)) { |
| 922 | if (pm.getPackageInfo(adminPackage, 0, userHandle) == null |
| 923 | || pm.getReceiverInfo(aa.info.getComponent(), 0, userHandle) |
| 924 | == null) { |
| 925 | removed = true; |
| 926 | policy.mAdminList.remove(i); |
| 927 | policy.mAdminMap.remove(aa.info.getComponent()); |
| 928 | } |
Fyodor Kupolov | d01ff6d | 2014-11-12 10:46:50 -0800 | [diff] [blame] | 929 | } |
| 930 | } catch (RemoteException re) { |
| 931 | // Shouldn't happen |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 932 | } |
| 933 | } |
Fyodor Kupolov | d01ff6d | 2014-11-12 10:46:50 -0800 | [diff] [blame] | 934 | if (removed) { |
| 935 | validatePasswordOwnerLocked(policy); |
| 936 | syncDeviceCapabilitiesLocked(policy); |
| 937 | saveSettingsLocked(policy.mUserHandle); |
| 938 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 939 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 940 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 941 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 942 | /** |
| 943 | * Instantiates the service. |
| 944 | */ |
| 945 | public DevicePolicyManagerService(Context context) { |
| 946 | mContext = context; |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 947 | mUserManager = UserManager.get(mContext); |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 948 | mHasFeature = context.getPackageManager().hasSystemFeature( |
| 949 | PackageManager.FEATURE_DEVICE_ADMIN); |
Jeff Brown | 5ce1cb2 | 2014-11-06 19:05:33 -0800 | [diff] [blame] | 950 | mPowerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE); |
| 951 | mPowerManagerInternal = LocalServices.getService(PowerManagerInternal.class); |
| 952 | mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "DPM"); |
Svet Ganov | 6bd7025 | 2014-08-20 09:47:47 -0700 | [diff] [blame] | 953 | mLocalService = new LocalService(); |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 954 | if (!mHasFeature) { |
| 955 | // Skip the rest of the initialization |
| 956 | return; |
| 957 | } |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 958 | IntentFilter filter = new IntentFilter(); |
| 959 | filter.addAction(Intent.ACTION_BOOT_COMPLETED); |
| 960 | filter.addAction(ACTION_EXPIRED_PASSWORD_NOTIFICATION); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 961 | filter.addAction(Intent.ACTION_USER_REMOVED); |
| 962 | filter.addAction(Intent.ACTION_USER_STARTED); |
Maggie Benthall | 0469f41 | 2013-09-05 15:30:26 -0400 | [diff] [blame] | 963 | filter.addAction(KeyChain.ACTION_STORAGE_CHANGED); |
Dianne Hackborn | d83a096 | 2014-05-02 16:28:33 -0700 | [diff] [blame] | 964 | filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 965 | context.registerReceiverAsUser(mReceiver, UserHandle.ALL, filter, null, mHandler); |
| 966 | filter = new IntentFilter(); |
| 967 | filter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
| 968 | filter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 969 | filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 970 | filter.addAction(Intent.ACTION_PACKAGE_ADDED); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 971 | filter.addDataScheme("package"); |
| 972 | context.registerReceiverAsUser(mReceiver, UserHandle.ALL, filter, null, mHandler); |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 973 | |
Svet Ganov | 6bd7025 | 2014-08-20 09:47:47 -0700 | [diff] [blame] | 974 | LocalServices.addService(DevicePolicyManagerInternal.class, mLocalService); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | /** |
| 978 | * Creates and loads the policy data from xml. |
| 979 | * @param userHandle the user for whom to load the policy data |
| 980 | * @return |
| 981 | */ |
| 982 | DevicePolicyData getUserData(int userHandle) { |
| 983 | synchronized (this) { |
| 984 | DevicePolicyData policy = mUserData.get(userHandle); |
| 985 | if (policy == null) { |
| 986 | policy = new DevicePolicyData(userHandle); |
| 987 | mUserData.append(userHandle, policy); |
| 988 | loadSettingsLocked(policy, userHandle); |
| 989 | } |
| 990 | return policy; |
| 991 | } |
| 992 | } |
| 993 | |
Benjamin Franz | fadb2b3 | 2015-01-14 18:22:42 +0000 | [diff] [blame] | 994 | /** |
| 995 | * Creates and loads the policy data from xml for data that is shared between |
| 996 | * various profiles of a user. In contrast to {@link #getUserData(int)} |
| 997 | * it allows access to data of users other than the calling user. |
| 998 | * |
| 999 | * This function should only be used for shared data, e.g. everything regarding |
| 1000 | * passwords and should be removed once multiple screen locks are present. |
| 1001 | * @param userHandle the user for whom to load the policy data |
| 1002 | * @return |
| 1003 | */ |
| 1004 | DevicePolicyData getUserDataUnchecked(int userHandle) { |
| 1005 | long ident = Binder.clearCallingIdentity(); |
| 1006 | try { |
| 1007 | return getUserData(userHandle); |
| 1008 | } finally { |
| 1009 | Binder.restoreCallingIdentity(ident); |
| 1010 | } |
| 1011 | } |
| 1012 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1013 | void removeUserData(int userHandle) { |
| 1014 | synchronized (this) { |
| 1015 | if (userHandle == UserHandle.USER_OWNER) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 1016 | Slog.w(LOG_TAG, "Tried to remove device policy file for user 0! Ignoring."); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1017 | return; |
| 1018 | } |
Amith Yamasani | 9348ac8 | 2014-02-10 13:43:18 -0800 | [diff] [blame] | 1019 | if (mDeviceOwner != null) { |
| 1020 | mDeviceOwner.removeProfileOwner(userHandle); |
| 1021 | mDeviceOwner.writeOwnerFile(); |
| 1022 | } |
Adam Connors | 776c555 | 2014-01-09 10:42:56 +0000 | [diff] [blame] | 1023 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1024 | DevicePolicyData policy = mUserData.get(userHandle); |
| 1025 | if (policy != null) { |
| 1026 | mUserData.remove(userHandle); |
| 1027 | } |
| 1028 | File policyFile = new File(Environment.getUserSystemDirectory(userHandle), |
| 1029 | DEVICE_POLICIES_XML); |
| 1030 | policyFile.delete(); |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 1031 | Slog.i(LOG_TAG, "Removed device policy file " + policyFile.getAbsolutePath()); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1032 | } |
Sander Alewijnse | a87863a | 2014-07-29 12:01:38 +0100 | [diff] [blame] | 1033 | updateScreenCaptureDisabledInWindowManager(userHandle, false /* default value */); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1034 | } |
| 1035 | |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 1036 | void loadDeviceOwner() { |
| 1037 | synchronized (this) { |
Adam Connors | 776c555 | 2014-01-09 10:42:56 +0000 | [diff] [blame] | 1038 | mDeviceOwner = DeviceOwner.load(); |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 1039 | } |
| 1040 | } |
| 1041 | |
Andy Stadler | 043116a | 2010-11-29 17:43:32 -0800 | [diff] [blame] | 1042 | /** |
| 1043 | * Set an alarm for an upcoming event - expiration warning, expiration, or post-expiration |
| 1044 | * reminders. Clears alarm if no expirations are configured. |
| 1045 | */ |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1046 | protected void setExpirationAlarmCheckLocked(Context context, DevicePolicyData policy) { |
| 1047 | final long expiration = getPasswordExpirationLocked(null, policy.mUserHandle); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1048 | final long now = System.currentTimeMillis(); |
| 1049 | final long timeToExpire = expiration - now; |
| 1050 | final long alarmTime; |
Andy Stadler | 043116a | 2010-11-29 17:43:32 -0800 | [diff] [blame] | 1051 | if (expiration == 0) { |
| 1052 | // No expirations are currently configured: Cancel alarm. |
| 1053 | alarmTime = 0; |
| 1054 | } else if (timeToExpire <= 0) { |
| 1055 | // The password has already expired: Repeat every 24 hours. |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1056 | alarmTime = now + MS_PER_DAY; |
Andy Stadler | 043116a | 2010-11-29 17:43:32 -0800 | [diff] [blame] | 1057 | } else { |
| 1058 | // Selecting the next alarm time: Roll forward to the next 24 hour multiple before |
| 1059 | // the expiration time. |
| 1060 | long alarmInterval = timeToExpire % MS_PER_DAY; |
| 1061 | if (alarmInterval == 0) { |
| 1062 | alarmInterval = MS_PER_DAY; |
| 1063 | } |
| 1064 | alarmTime = now + alarmInterval; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1065 | } |
| 1066 | |
Andy Stadler | 1f35d48 | 2010-11-19 15:39:41 -0800 | [diff] [blame] | 1067 | long token = Binder.clearCallingIdentity(); |
| 1068 | try { |
| 1069 | AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1070 | PendingIntent pi = PendingIntent.getBroadcastAsUser(context, REQUEST_EXPIRE_PASSWORD, |
Andy Stadler | 1f35d48 | 2010-11-19 15:39:41 -0800 | [diff] [blame] | 1071 | new Intent(ACTION_EXPIRED_PASSWORD_NOTIFICATION), |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1072 | PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT, |
| 1073 | new UserHandle(policy.mUserHandle)); |
Andy Stadler | 1f35d48 | 2010-11-19 15:39:41 -0800 | [diff] [blame] | 1074 | am.cancel(pi); |
Andy Stadler | 043116a | 2010-11-29 17:43:32 -0800 | [diff] [blame] | 1075 | if (alarmTime != 0) { |
| 1076 | am.set(AlarmManager.RTC, alarmTime, pi); |
| 1077 | } |
Andy Stadler | 1f35d48 | 2010-11-19 15:39:41 -0800 | [diff] [blame] | 1078 | } finally { |
| 1079 | Binder.restoreCallingIdentity(token); |
| 1080 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1081 | } |
| 1082 | |
Jim Miller | 93c518e | 2012-01-17 15:55:31 -0800 | [diff] [blame] | 1083 | private IWindowManager getWindowManager() { |
| 1084 | if (mIWindowManager == null) { |
| 1085 | IBinder b = ServiceManager.getService(Context.WINDOW_SERVICE); |
| 1086 | mIWindowManager = IWindowManager.Stub.asInterface(b); |
| 1087 | } |
| 1088 | return mIWindowManager; |
| 1089 | } |
| 1090 | |
Maggie Benthall | 0469f41 | 2013-09-05 15:30:26 -0400 | [diff] [blame] | 1091 | private NotificationManager getNotificationManager() { |
| 1092 | if (mNotificationManager == null) { |
| 1093 | mNotificationManager = |
| 1094 | (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
| 1095 | } |
| 1096 | return mNotificationManager; |
| 1097 | } |
| 1098 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1099 | ActiveAdmin getActiveAdminUncheckedLocked(ComponentName who, int userHandle) { |
| 1100 | ActiveAdmin admin = getUserData(userHandle).mAdminMap.get(who); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1101 | if (admin != null |
| 1102 | && who.getPackageName().equals(admin.info.getActivityInfo().packageName) |
| 1103 | && who.getClassName().equals(admin.info.getActivityInfo().name)) { |
| 1104 | return admin; |
| 1105 | } |
| 1106 | return null; |
| 1107 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1108 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1109 | ActiveAdmin getActiveAdminForCallerLocked(ComponentName who, int reqPolicy) |
| 1110 | throws SecurityException { |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1111 | final int callingUid = Binder.getCallingUid(); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1112 | final int userHandle = UserHandle.getUserId(callingUid); |
| 1113 | final DevicePolicyData policy = getUserData(userHandle); |
Robin Lee | 5c921da | 2014-03-24 15:11:35 +0000 | [diff] [blame] | 1114 | |
| 1115 | List<ActiveAdmin> candidates = new ArrayList<ActiveAdmin>(); |
| 1116 | |
| 1117 | // Build a list of admins for this uid matching the given ComponentName |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1118 | if (who != null) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1119 | ActiveAdmin admin = policy.mAdminMap.get(who); |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1120 | if (admin == null) { |
| 1121 | throw new SecurityException("No active admin " + who); |
| 1122 | } |
| 1123 | if (admin.getUid() != callingUid) { |
| 1124 | throw new SecurityException("Admin " + who + " is not owned by uid " |
| 1125 | + Binder.getCallingUid()); |
| 1126 | } |
Robin Lee | 5c921da | 2014-03-24 15:11:35 +0000 | [diff] [blame] | 1127 | candidates.add(admin); |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1128 | } else { |
Robin Lee | 5c921da | 2014-03-24 15:11:35 +0000 | [diff] [blame] | 1129 | for (ActiveAdmin admin : policy.mAdminList) { |
| 1130 | if (admin.getUid() == callingUid) { |
| 1131 | candidates.add(admin); |
| 1132 | } |
| 1133 | } |
| 1134 | } |
| 1135 | |
| 1136 | // Try to find an admin which can use reqPolicy |
| 1137 | for (ActiveAdmin admin : candidates) { |
| 1138 | boolean ownsDevice = isDeviceOwner(admin.info.getPackageName()); |
| 1139 | boolean ownsProfile = (getProfileOwner(userHandle) != null |
Amith Yamasani | bf3a946 | 2014-07-28 14:26:42 -0700 | [diff] [blame] | 1140 | && getProfileOwner(userHandle).getPackageName() |
| 1141 | .equals(admin.info.getPackageName())); |
Robin Lee | 5c921da | 2014-03-24 15:11:35 +0000 | [diff] [blame] | 1142 | |
| 1143 | if (reqPolicy == DeviceAdminInfo.USES_POLICY_DEVICE_OWNER) { |
| 1144 | if (ownsDevice) { |
| 1145 | return admin; |
| 1146 | } |
| 1147 | } else if (reqPolicy == DeviceAdminInfo.USES_POLICY_PROFILE_OWNER) { |
| 1148 | if (ownsDevice || ownsProfile) { |
| 1149 | return admin; |
| 1150 | } |
| 1151 | } else { |
| 1152 | if (admin.info.usesPolicy(reqPolicy)) { |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1153 | return admin; |
| 1154 | } |
| 1155 | } |
Robin Lee | 5c921da | 2014-03-24 15:11:35 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | if (who != null) { |
Robin Lee | 122c595 | 2014-06-11 17:39:45 +0100 | [diff] [blame] | 1159 | if (reqPolicy == DeviceAdminInfo.USES_POLICY_DEVICE_OWNER) { |
| 1160 | throw new SecurityException("Admin " + candidates.get(0).info.getComponent() |
| 1161 | + " does not own the device"); |
| 1162 | } |
| 1163 | if (reqPolicy == DeviceAdminInfo.USES_POLICY_PROFILE_OWNER) { |
| 1164 | throw new SecurityException("Admin " + candidates.get(0).info.getComponent() |
| 1165 | + " does not own the profile"); |
| 1166 | } |
Robin Lee | 5c921da | 2014-03-24 15:11:35 +0000 | [diff] [blame] | 1167 | throw new SecurityException("Admin " + candidates.get(0).info.getComponent() |
| 1168 | + " did not specify uses-policy for: " |
| 1169 | + candidates.get(0).info.getTagForPolicy(reqPolicy)); |
| 1170 | } else { |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1171 | throw new SecurityException("No active admin owned by uid " |
Robin Lee | 122c595 | 2014-06-11 17:39:45 +0100 | [diff] [blame] | 1172 | + Binder.getCallingUid() + " for policy #" + reqPolicy); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1173 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1174 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1175 | |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 1176 | void sendAdminCommandLocked(ActiveAdmin admin, String action) { |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 1177 | sendAdminCommandLocked(admin, action, null); |
| 1178 | } |
| 1179 | |
Jason Monk | 03978a4 | 2014-06-10 15:05:30 -0400 | [diff] [blame] | 1180 | void sendAdminCommandLocked(ActiveAdmin admin, String action, BroadcastReceiver result) { |
| 1181 | sendAdminCommandLocked(admin, action, null, result); |
| 1182 | } |
| 1183 | |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 1184 | /** |
| 1185 | * Send an update to one specific admin, get notified when that admin returns a result. |
| 1186 | */ |
Jason Monk | 03978a4 | 2014-06-10 15:05:30 -0400 | [diff] [blame] | 1187 | void sendAdminCommandLocked(ActiveAdmin admin, String action, Bundle adminExtras, |
| 1188 | BroadcastReceiver result) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1189 | Intent intent = new Intent(action); |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 1190 | intent.setComponent(admin.info.getComponent()); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1191 | if (action.equals(DeviceAdminReceiver.ACTION_PASSWORD_EXPIRING)) { |
| 1192 | intent.putExtra("expiration", admin.passwordExpirationDate); |
| 1193 | } |
Jason Monk | 03978a4 | 2014-06-10 15:05:30 -0400 | [diff] [blame] | 1194 | if (adminExtras != null) { |
| 1195 | intent.putExtras(adminExtras); |
| 1196 | } |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 1197 | if (result != null) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1198 | mContext.sendOrderedBroadcastAsUser(intent, admin.getUserHandle(), |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1199 | null, result, mHandler, Activity.RESULT_OK, null, null); |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 1200 | } else { |
Kenny Guy | 73a41d2 | 2014-06-05 16:51:24 +0100 | [diff] [blame] | 1201 | mContext.sendBroadcastAsUser(intent, admin.getUserHandle()); |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 1202 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1203 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1204 | |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 1205 | /** |
| 1206 | * Send an update to all admins of a user that enforce a specified policy. |
| 1207 | */ |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1208 | void sendAdminCommandLocked(String action, int reqPolicy, int userHandle) { |
| 1209 | final DevicePolicyData policy = getUserData(userHandle); |
| 1210 | final int count = policy.mAdminList.size(); |
| 1211 | if (count > 0) { |
| 1212 | for (int i = 0; i < count; i++) { |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 1213 | final ActiveAdmin admin = policy.mAdminList.get(i); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1214 | if (admin.info.usesPolicy(reqPolicy)) { |
| 1215 | sendAdminCommandLocked(admin, action); |
| 1216 | } |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 1217 | } |
Dianne Hackborn | 4141d03 | 2010-01-21 16:29:00 -0800 | [diff] [blame] | 1218 | } |
| 1219 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1220 | |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 1221 | /** |
| 1222 | * Send an update intent to all admins of a user and its profiles. Only send to admins that |
| 1223 | * enforce a specified policy. |
| 1224 | */ |
| 1225 | private void sendAdminCommandToSelfAndProfilesLocked(String action, int reqPolicy, |
| 1226 | int userHandle) { |
| 1227 | List<UserInfo> profiles = mUserManager.getProfiles(userHandle); |
| 1228 | for (UserInfo ui : profiles) { |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 1229 | int id = ui.id; |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 1230 | sendAdminCommandLocked(action, reqPolicy, id); |
| 1231 | } |
| 1232 | } |
| 1233 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1234 | void removeActiveAdminLocked(final ComponentName adminReceiver, int userHandle) { |
| 1235 | final ActiveAdmin admin = getActiveAdminUncheckedLocked(adminReceiver, userHandle); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1236 | if (admin != null) { |
Fyodor Kupolov | 96fb932 | 2014-12-01 15:08:09 -0800 | [diff] [blame] | 1237 | synchronized (this) { |
| 1238 | getUserData(userHandle).mRemovingAdmins.add(adminReceiver); |
| 1239 | } |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1240 | sendAdminCommandLocked(admin, |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 1241 | DeviceAdminReceiver.ACTION_DEVICE_ADMIN_DISABLED, |
| 1242 | new BroadcastReceiver() { |
| 1243 | @Override |
| 1244 | public void onReceive(Context context, Intent intent) { |
Dianne Hackborn | 2fe8fb2 | 2012-06-15 17:05:25 -0700 | [diff] [blame] | 1245 | synchronized (DevicePolicyManagerService.this) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1246 | int userHandle = admin.getUserHandle().getIdentifier(); |
| 1247 | DevicePolicyData policy = getUserData(userHandle); |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 1248 | boolean doProxyCleanup = admin.info.usesPolicy( |
| 1249 | DeviceAdminInfo.USES_POLICY_SETS_GLOBAL_PROXY); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1250 | policy.mAdminList.remove(admin); |
| 1251 | policy.mAdminMap.remove(adminReceiver); |
| 1252 | validatePasswordOwnerLocked(policy); |
| 1253 | syncDeviceCapabilitiesLocked(policy); |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 1254 | if (doProxyCleanup) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1255 | resetGlobalProxyLocked(getUserData(userHandle)); |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 1256 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1257 | saveSettingsLocked(userHandle); |
| 1258 | updateMaximumTimeToLockLocked(policy); |
Fyodor Kupolov | 96fb932 | 2014-12-01 15:08:09 -0800 | [diff] [blame] | 1259 | policy.mRemovingAdmins.remove(adminReceiver); |
Dianne Hackborn | d998acb | 2011-05-25 10:48:28 -0700 | [diff] [blame] | 1260 | } |
| 1261 | } |
Fyodor Kupolov | 96fb932 | 2014-12-01 15:08:09 -0800 | [diff] [blame] | 1262 | }); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1263 | } |
| 1264 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1265 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1266 | public DeviceAdminInfo findAdmin(ComponentName adminName, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 1267 | if (!mHasFeature) { |
| 1268 | return null; |
| 1269 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1270 | enforceCrossUserPermission(userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1271 | Intent resolveIntent = new Intent(); |
| 1272 | resolveIntent.setComponent(adminName); |
| 1273 | List<ResolveInfo> infos = mContext.getPackageManager().queryBroadcastReceivers( |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 1274 | resolveIntent, |
| 1275 | PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS, |
| 1276 | userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1277 | if (infos == null || infos.size() <= 0) { |
| 1278 | throw new IllegalArgumentException("Unknown admin: " + adminName); |
| 1279 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1280 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1281 | try { |
| 1282 | return new DeviceAdminInfo(mContext, infos.get(0)); |
| 1283 | } catch (XmlPullParserException e) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 1284 | Slog.w(LOG_TAG, "Bad device admin requested for user=" + userHandle + ": " + adminName, |
| 1285 | e); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1286 | return null; |
| 1287 | } catch (IOException e) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 1288 | Slog.w(LOG_TAG, "Bad device admin requested for user=" + userHandle + ": " + adminName, |
| 1289 | e); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1290 | return null; |
| 1291 | } |
| 1292 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1293 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1294 | private static JournaledFile makeJournaledFile(int userHandle) { |
| 1295 | final String base = userHandle == 0 |
| 1296 | ? "/data/system/" + DEVICE_POLICIES_XML |
| 1297 | : new File(Environment.getUserSystemDirectory(userHandle), DEVICE_POLICIES_XML) |
| 1298 | .getAbsolutePath(); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1299 | return new JournaledFile(new File(base), new File(base + ".tmp")); |
| 1300 | } |
| 1301 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1302 | private void saveSettingsLocked(int userHandle) { |
| 1303 | DevicePolicyData policy = getUserData(userHandle); |
| 1304 | JournaledFile journal = makeJournaledFile(userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1305 | FileOutputStream stream = null; |
| 1306 | try { |
| 1307 | stream = new FileOutputStream(journal.chooseForWrite(), false); |
| 1308 | XmlSerializer out = new FastXmlSerializer(); |
| 1309 | out.setOutput(stream, "utf-8"); |
| 1310 | out.startDocument(null, true); |
| 1311 | |
| 1312 | out.startTag(null, "policies"); |
Amith Yamasani | f20d640 | 2014-05-24 15:34:37 -0700 | [diff] [blame] | 1313 | if (policy.mRestrictionsProvider != null) { |
| 1314 | out.attribute(null, ATTR_PERMISSION_PROVIDER, |
| 1315 | policy.mRestrictionsProvider.flattenToString()); |
| 1316 | } |
Amith Yamasani | ed239cb | 2014-07-16 17:26:17 -0700 | [diff] [blame] | 1317 | if (policy.mUserSetupComplete) { |
| 1318 | out.attribute(null, ATTR_SETUP_COMPLETE, |
| 1319 | Boolean.toString(true)); |
| 1320 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1321 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1322 | final int N = policy.mAdminList.size(); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1323 | for (int i=0; i<N; i++) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1324 | ActiveAdmin ap = policy.mAdminList.get(i); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1325 | if (ap != null) { |
| 1326 | out.startTag(null, "admin"); |
| 1327 | out.attribute(null, "name", ap.info.getComponent().flattenToString()); |
| 1328 | ap.writeToXml(out); |
| 1329 | out.endTag(null, "admin"); |
| 1330 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1331 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1332 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1333 | if (policy.mPasswordOwner >= 0) { |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 1334 | out.startTag(null, "password-owner"); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1335 | out.attribute(null, "value", Integer.toString(policy.mPasswordOwner)); |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 1336 | out.endTag(null, "password-owner"); |
| 1337 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1338 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1339 | if (policy.mFailedPasswordAttempts != 0) { |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1340 | out.startTag(null, "failed-password-attempts"); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1341 | out.attribute(null, "value", Integer.toString(policy.mFailedPasswordAttempts)); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1342 | out.endTag(null, "failed-password-attempts"); |
| 1343 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1344 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1345 | if (policy.mActivePasswordQuality != 0 || policy.mActivePasswordLength != 0 |
| 1346 | || policy.mActivePasswordUpperCase != 0 || policy.mActivePasswordLowerCase != 0 |
| 1347 | || policy.mActivePasswordLetters != 0 || policy.mActivePasswordNumeric != 0 |
| 1348 | || policy.mActivePasswordSymbols != 0 || policy.mActivePasswordNonLetter != 0) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1349 | out.startTag(null, "active-password"); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1350 | out.attribute(null, "quality", Integer.toString(policy.mActivePasswordQuality)); |
| 1351 | out.attribute(null, "length", Integer.toString(policy.mActivePasswordLength)); |
| 1352 | out.attribute(null, "uppercase", Integer.toString(policy.mActivePasswordUpperCase)); |
| 1353 | out.attribute(null, "lowercase", Integer.toString(policy.mActivePasswordLowerCase)); |
| 1354 | out.attribute(null, "letters", Integer.toString(policy.mActivePasswordLetters)); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1355 | out.attribute(null, "numeric", Integer |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1356 | .toString(policy.mActivePasswordNumeric)); |
| 1357 | out.attribute(null, "symbols", Integer.toString(policy.mActivePasswordSymbols)); |
| 1358 | out.attribute(null, "nonletter", Integer.toString(policy.mActivePasswordNonLetter)); |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1359 | out.endTag(null, "active-password"); |
| 1360 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1361 | |
Jason Monk | d7b8621 | 2014-06-16 13:15:38 -0400 | [diff] [blame] | 1362 | for (int i=0; i<policy.mLockTaskPackages.size(); i++) { |
| 1363 | String component = policy.mLockTaskPackages.get(i); |
justinzhang | 511e0d8 | 2014-03-24 16:09:24 -0400 | [diff] [blame] | 1364 | out.startTag(null, LOCK_TASK_COMPONENTS_XML); |
Jason Monk | d7b8621 | 2014-06-16 13:15:38 -0400 | [diff] [blame] | 1365 | out.attribute(null, "name", component); |
justinzhang | 511e0d8 | 2014-03-24 16:09:24 -0400 | [diff] [blame] | 1366 | out.endTag(null, LOCK_TASK_COMPONENTS_XML); |
| 1367 | } |
| 1368 | |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1369 | out.endTag(null, "policies"); |
| 1370 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1371 | out.endDocument(); |
Kenny Guy | 212037f | 2015-01-15 18:14:46 +0000 | [diff] [blame] | 1372 | stream.flush(); |
| 1373 | FileUtils.sync(stream); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1374 | stream.close(); |
| 1375 | journal.commit(); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1376 | sendChangedNotification(userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1377 | } catch (IOException e) { |
| 1378 | try { |
| 1379 | if (stream != null) { |
| 1380 | stream.close(); |
| 1381 | } |
| 1382 | } catch (IOException ex) { |
| 1383 | // Ignore |
| 1384 | } |
| 1385 | journal.rollback(); |
| 1386 | } |
| 1387 | } |
| 1388 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1389 | private void sendChangedNotification(int userHandle) { |
Jim Miller | 284b62e | 2010-06-08 14:27:42 -0700 | [diff] [blame] | 1390 | Intent intent = new Intent(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED); |
| 1391 | intent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
Amith Yamasani | b7a6859 | 2012-09-10 10:24:36 -0700 | [diff] [blame] | 1392 | long ident = Binder.clearCallingIdentity(); |
| 1393 | try { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1394 | mContext.sendBroadcastAsUser(intent, new UserHandle(userHandle)); |
Amith Yamasani | b7a6859 | 2012-09-10 10:24:36 -0700 | [diff] [blame] | 1395 | } finally { |
| 1396 | Binder.restoreCallingIdentity(ident); |
| 1397 | } |
Jim Miller | 284b62e | 2010-06-08 14:27:42 -0700 | [diff] [blame] | 1398 | } |
| 1399 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1400 | private void loadSettingsLocked(DevicePolicyData policy, int userHandle) { |
| 1401 | JournaledFile journal = makeJournaledFile(userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1402 | FileInputStream stream = null; |
| 1403 | File file = journal.chooseForRead(); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1404 | try { |
| 1405 | stream = new FileInputStream(file); |
| 1406 | XmlPullParser parser = Xml.newPullParser(); |
| 1407 | parser.setInput(stream, null); |
| 1408 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1409 | int type; |
| 1410 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 1411 | && type != XmlPullParser.START_TAG) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1412 | } |
| 1413 | String tag = parser.getName(); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1414 | if (!"policies".equals(tag)) { |
| 1415 | throw new XmlPullParserException( |
| 1416 | "Settings do not start with policies tag: found " + tag); |
| 1417 | } |
Amith Yamasani | f20d640 | 2014-05-24 15:34:37 -0700 | [diff] [blame] | 1418 | |
| 1419 | // Extract the permission provider component name if available |
| 1420 | String permissionProvider = parser.getAttributeValue(null, ATTR_PERMISSION_PROVIDER); |
| 1421 | if (permissionProvider != null) { |
| 1422 | policy.mRestrictionsProvider = ComponentName.unflattenFromString(permissionProvider); |
| 1423 | } |
Amith Yamasani | ed239cb | 2014-07-16 17:26:17 -0700 | [diff] [blame] | 1424 | String userSetupComplete = parser.getAttributeValue(null, ATTR_SETUP_COMPLETE); |
| 1425 | if (userSetupComplete != null && Boolean.toString(true).equals(userSetupComplete)) { |
| 1426 | policy.mUserSetupComplete = true; |
| 1427 | } |
Amith Yamasani | f20d640 | 2014-05-24 15:34:37 -0700 | [diff] [blame] | 1428 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1429 | type = parser.next(); |
| 1430 | int outerDepth = parser.getDepth(); |
Jason Monk | d7b8621 | 2014-06-16 13:15:38 -0400 | [diff] [blame] | 1431 | policy.mLockTaskPackages.clear(); |
Robin Lee | 9c2be6c | 2014-07-22 16:47:40 +0100 | [diff] [blame] | 1432 | policy.mAdminList.clear(); |
| 1433 | policy.mAdminMap.clear(); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1434 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 1435 | && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 1436 | if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) { |
| 1437 | continue; |
| 1438 | } |
| 1439 | tag = parser.getName(); |
| 1440 | if ("admin".equals(tag)) { |
Dianne Hackborn | e83cefc | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 1441 | String name = parser.getAttributeValue(null, "name"); |
| 1442 | try { |
| 1443 | DeviceAdminInfo dai = findAdmin( |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1444 | ComponentName.unflattenFromString(name), userHandle); |
| 1445 | if (DBG && (UserHandle.getUserId(dai.getActivityInfo().applicationInfo.uid) |
| 1446 | != userHandle)) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 1447 | Slog.w(LOG_TAG, "findAdmin returned an incorrect uid " |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1448 | + dai.getActivityInfo().applicationInfo.uid + " for user " |
| 1449 | + userHandle); |
| 1450 | } |
Dianne Hackborn | e83cefc | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 1451 | if (dai != null) { |
| 1452 | ActiveAdmin ap = new ActiveAdmin(dai); |
| 1453 | ap.readFromXml(parser); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1454 | policy.mAdminMap.put(ap.info.getComponent(), ap); |
Dianne Hackborn | e83cefc | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 1455 | } |
| 1456 | } catch (RuntimeException e) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 1457 | Slog.w(LOG_TAG, "Failed loading admin " + name, e); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1458 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1459 | } else if ("failed-password-attempts".equals(tag)) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1460 | policy.mFailedPasswordAttempts = Integer.parseInt( |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1461 | parser.getAttributeValue(null, "value")); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1462 | XmlUtils.skipCurrentTag(parser); |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 1463 | } else if ("password-owner".equals(tag)) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1464 | policy.mPasswordOwner = Integer.parseInt( |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 1465 | parser.getAttributeValue(null, "value")); |
| 1466 | XmlUtils.skipCurrentTag(parser); |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1467 | } else if ("active-password".equals(tag)) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1468 | policy.mActivePasswordQuality = Integer.parseInt( |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1469 | parser.getAttributeValue(null, "quality")); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1470 | policy.mActivePasswordLength = Integer.parseInt( |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1471 | parser.getAttributeValue(null, "length")); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1472 | policy.mActivePasswordUpperCase = Integer.parseInt( |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1473 | parser.getAttributeValue(null, "uppercase")); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1474 | policy.mActivePasswordLowerCase = Integer.parseInt( |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1475 | parser.getAttributeValue(null, "lowercase")); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1476 | policy.mActivePasswordLetters = Integer.parseInt( |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1477 | parser.getAttributeValue(null, "letters")); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1478 | policy.mActivePasswordNumeric = Integer.parseInt( |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1479 | parser.getAttributeValue(null, "numeric")); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1480 | policy.mActivePasswordSymbols = Integer.parseInt( |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1481 | parser.getAttributeValue(null, "symbols")); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1482 | policy.mActivePasswordNonLetter = Integer.parseInt( |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 1483 | parser.getAttributeValue(null, "nonletter")); |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1484 | XmlUtils.skipCurrentTag(parser); |
justinzhang | 511e0d8 | 2014-03-24 16:09:24 -0400 | [diff] [blame] | 1485 | } else if (LOCK_TASK_COMPONENTS_XML.equals(tag)) { |
Jason Monk | d7b8621 | 2014-06-16 13:15:38 -0400 | [diff] [blame] | 1486 | policy.mLockTaskPackages.add(parser.getAttributeValue(null, "name")); |
justinzhang | 511e0d8 | 2014-03-24 16:09:24 -0400 | [diff] [blame] | 1487 | XmlUtils.skipCurrentTag(parser); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1488 | } else { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 1489 | Slog.w(LOG_TAG, "Unknown tag: " + tag); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1490 | XmlUtils.skipCurrentTag(parser); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 1491 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1492 | } |
| 1493 | } catch (NullPointerException e) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 1494 | Slog.w(LOG_TAG, "failed parsing " + file + " " + e); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1495 | } catch (NumberFormatException e) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 1496 | Slog.w(LOG_TAG, "failed parsing " + file + " " + e); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1497 | } catch (XmlPullParserException e) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 1498 | Slog.w(LOG_TAG, "failed parsing " + file + " " + e); |
Dianne Hackborn | cef65ee | 2010-09-30 18:27:22 -0700 | [diff] [blame] | 1499 | } catch (FileNotFoundException e) { |
| 1500 | // 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] | 1501 | } catch (IOException e) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 1502 | Slog.w(LOG_TAG, "failed parsing " + file + " " + e); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1503 | } catch (IndexOutOfBoundsException e) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 1504 | Slog.w(LOG_TAG, "failed parsing " + file + " " + e); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1505 | } |
| 1506 | try { |
| 1507 | if (stream != null) { |
| 1508 | stream.close(); |
| 1509 | } |
| 1510 | } catch (IOException e) { |
| 1511 | // Ignore |
| 1512 | } |
| 1513 | |
Robin Lee | 9c2be6c | 2014-07-22 16:47:40 +0100 | [diff] [blame] | 1514 | // Generate a list of admins from the admin map |
| 1515 | policy.mAdminList.addAll(policy.mAdminMap.values()); |
| 1516 | |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1517 | // Validate that what we stored for the password quality matches |
| 1518 | // sufficiently what is currently set. Note that this is only |
| 1519 | // a sanity check in case the two get out of sync; this should |
| 1520 | // never normally happen. |
| 1521 | LockPatternUtils utils = new LockPatternUtils(mContext); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1522 | if (utils.getActivePasswordQuality() < policy.mActivePasswordQuality) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 1523 | Slog.w(LOG_TAG, "Active password quality 0x" |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1524 | + Integer.toHexString(policy.mActivePasswordQuality) |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1525 | + " does not match actual quality 0x" |
| 1526 | + Integer.toHexString(utils.getActivePasswordQuality())); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1527 | policy.mActivePasswordQuality = DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; |
| 1528 | policy.mActivePasswordLength = 0; |
| 1529 | policy.mActivePasswordUpperCase = 0; |
| 1530 | policy.mActivePasswordLowerCase = 0; |
| 1531 | policy.mActivePasswordLetters = 0; |
| 1532 | policy.mActivePasswordNumeric = 0; |
| 1533 | policy.mActivePasswordSymbols = 0; |
| 1534 | policy.mActivePasswordNonLetter = 0; |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1535 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1536 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1537 | validatePasswordOwnerLocked(policy); |
| 1538 | syncDeviceCapabilitiesLocked(policy); |
| 1539 | updateMaximumTimeToLockLocked(policy); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1540 | } |
| 1541 | |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1542 | static void validateQualityConstant(int quality) { |
| 1543 | switch (quality) { |
| 1544 | case DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED: |
Danielle Millett | de7a2f3 | 2011-12-21 17:02:32 -0500 | [diff] [blame] | 1545 | case DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK: |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1546 | case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING: |
| 1547 | case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC: |
Jim Miller | 85516d0 | 2014-01-31 17:08:37 -0800 | [diff] [blame] | 1548 | case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX: |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1549 | case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC: |
| 1550 | case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC: |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 1551 | case DevicePolicyManager.PASSWORD_QUALITY_COMPLEX: |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1552 | return; |
| 1553 | } |
| 1554 | throw new IllegalArgumentException("Invalid quality constant: 0x" |
| 1555 | + Integer.toHexString(quality)); |
| 1556 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1557 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1558 | void validatePasswordOwnerLocked(DevicePolicyData policy) { |
| 1559 | if (policy.mPasswordOwner >= 0) { |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 1560 | boolean haveOwner = false; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1561 | for (int i = policy.mAdminList.size() - 1; i >= 0; i--) { |
| 1562 | if (policy.mAdminList.get(i).getUid() == policy.mPasswordOwner) { |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 1563 | haveOwner = true; |
| 1564 | break; |
| 1565 | } |
| 1566 | } |
| 1567 | if (!haveOwner) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 1568 | Slog.w(LOG_TAG, "Previous password owner " + policy.mPasswordOwner |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 1569 | + " no longer active; disabling"); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1570 | policy.mPasswordOwner = -1; |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 1571 | } |
| 1572 | } |
| 1573 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1574 | |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 1575 | /** |
| 1576 | * Pushes down policy information to the system for any policies related to general device |
| 1577 | * capabilities that need to be enforced by lower level services (e.g. Camera services). |
| 1578 | */ |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1579 | void syncDeviceCapabilitiesLocked(DevicePolicyData policy) { |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 1580 | // Ensure the status of the camera is synced down to the system. Interested native services |
| 1581 | // should monitor this value and act accordingly. |
| 1582 | boolean systemState = SystemProperties.getBoolean(SYSTEM_PROP_DISABLE_CAMERA, false); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1583 | boolean cameraDisabled = getCameraDisabled(null, policy.mUserHandle); |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 1584 | if (cameraDisabled != systemState) { |
| 1585 | long token = Binder.clearCallingIdentity(); |
| 1586 | try { |
| 1587 | String value = cameraDisabled ? "1" : "0"; |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 1588 | if (DBG) Slog.v(LOG_TAG, "Change in camera state [" |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 1589 | + SYSTEM_PROP_DISABLE_CAMERA + "] = " + value); |
| 1590 | SystemProperties.set(SYSTEM_PROP_DISABLE_CAMERA, value); |
| 1591 | } finally { |
| 1592 | Binder.restoreCallingIdentity(token); |
| 1593 | } |
| 1594 | } |
| 1595 | } |
| 1596 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1597 | public void systemReady() { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 1598 | if (!mHasFeature) { |
| 1599 | return; |
| 1600 | } |
Robin Lee | 9c2be6c | 2014-07-22 16:47:40 +0100 | [diff] [blame] | 1601 | getUserData(UserHandle.USER_OWNER); |
| 1602 | loadDeviceOwner(); |
Alexandra Gherghina | 22745f4 | 2014-06-04 12:03:08 +0100 | [diff] [blame] | 1603 | cleanUpOldUsers(); |
Amith Yamasani | ed239cb | 2014-07-16 17:26:17 -0700 | [diff] [blame] | 1604 | // Register an observer for watching for user setup complete. |
| 1605 | new SetupContentObserver(mHandler).register(mContext.getContentResolver()); |
| 1606 | // Initialize the user setup state, to handle the upgrade case. |
| 1607 | updateUserSetupComplete(); |
Sander Alewijnse | a87863a | 2014-07-29 12:01:38 +0100 | [diff] [blame] | 1608 | |
| 1609 | // Update the screen capture disabled cache in the window manager |
| 1610 | List<UserInfo> users = mUserManager.getUsers(true); |
| 1611 | final int N = users.size(); |
| 1612 | for (int i = 0; i < N; i++) { |
| 1613 | int userHandle = users.get(i).id; |
| 1614 | updateScreenCaptureDisabledInWindowManager(userHandle, |
| 1615 | getScreenCaptureDisabled(null, userHandle)); |
| 1616 | } |
| 1617 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1618 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1619 | |
Alexandra Gherghina | 22745f4 | 2014-06-04 12:03:08 +0100 | [diff] [blame] | 1620 | private void cleanUpOldUsers() { |
| 1621 | // This is needed in case the broadcast {@link Intent.ACTION_USER_REMOVED} was not handled |
| 1622 | // before reboot |
| 1623 | Set<Integer> usersWithProfileOwners; |
| 1624 | Set<Integer> usersWithData; |
| 1625 | synchronized(this) { |
| 1626 | usersWithProfileOwners = mDeviceOwner != null |
| 1627 | ? mDeviceOwner.getProfileOwnerKeys() : new HashSet<Integer>(); |
| 1628 | usersWithData = new HashSet<Integer>(); |
| 1629 | for (int i = 0; i < mUserData.size(); i++) { |
| 1630 | usersWithData.add(mUserData.keyAt(i)); |
| 1631 | } |
| 1632 | } |
| 1633 | List<UserInfo> allUsers = mUserManager.getUsers(); |
| 1634 | |
| 1635 | Set<Integer> deletedUsers = new HashSet<Integer>(); |
| 1636 | deletedUsers.addAll(usersWithProfileOwners); |
| 1637 | deletedUsers.addAll(usersWithData); |
| 1638 | for (UserInfo userInfo : allUsers) { |
| 1639 | deletedUsers.remove(userInfo.id); |
| 1640 | } |
| 1641 | for (Integer userId : deletedUsers) { |
| 1642 | removeUserData(userId); |
| 1643 | } |
| 1644 | } |
| 1645 | |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 1646 | private void handlePasswordExpirationNotification(int userHandle) { |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1647 | synchronized (this) { |
| 1648 | final long now = System.currentTimeMillis(); |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 1649 | |
| 1650 | List<UserInfo> profiles = mUserManager.getProfiles(userHandle); |
| 1651 | for (UserInfo ui : profiles) { |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 1652 | int profileUserHandle = ui.id; |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 1653 | final DevicePolicyData policy = getUserData(profileUserHandle); |
| 1654 | final int count = policy.mAdminList.size(); |
| 1655 | if (count > 0) { |
| 1656 | for (int i = 0; i < count; i++) { |
| 1657 | final ActiveAdmin admin = policy.mAdminList.get(i); |
| 1658 | if (admin.info.usesPolicy(DeviceAdminInfo.USES_POLICY_EXPIRE_PASSWORD) |
| 1659 | && admin.passwordExpirationTimeout > 0L |
| 1660 | && now >= admin.passwordExpirationDate - EXPIRATION_GRACE_PERIOD_MS |
| 1661 | && admin.passwordExpirationDate > 0L) { |
| 1662 | sendAdminCommandLocked(admin, |
| 1663 | DeviceAdminReceiver.ACTION_PASSWORD_EXPIRING); |
| 1664 | } |
| 1665 | } |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1666 | } |
| 1667 | } |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 1668 | setExpirationAlarmCheckLocked(mContext, getUserData(userHandle)); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 1669 | } |
| 1670 | } |
| 1671 | |
Robin Lee | d0130e8 | 2014-10-06 11:36:25 +0100 | [diff] [blame] | 1672 | private class MonitoringCertNotificationTask extends AsyncTask<Intent, Void, Void> { |
| 1673 | @Override |
| 1674 | protected Void doInBackground(Intent... params) { |
| 1675 | int userHandle = params[0].getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_ALL); |
Maggie Benthall | 0469f41 | 2013-09-05 15:30:26 -0400 | [diff] [blame] | 1676 | |
Robin Lee | d0130e8 | 2014-10-06 11:36:25 +0100 | [diff] [blame] | 1677 | if (userHandle == UserHandle.USER_ALL) { |
| 1678 | for (UserInfo userInfo : mUserManager.getUsers()) { |
| 1679 | manageNotification(userInfo.getUserHandle()); |
Maggie Benthall | 0469f41 | 2013-09-05 15:30:26 -0400 | [diff] [blame] | 1680 | } |
Robin Lee | d0130e8 | 2014-10-06 11:36:25 +0100 | [diff] [blame] | 1681 | } else { |
| 1682 | manageNotification(new UserHandle(userHandle)); |
Maggie Benthall | 0469f41 | 2013-09-05 15:30:26 -0400 | [diff] [blame] | 1683 | } |
Robin Lee | d0130e8 | 2014-10-06 11:36:25 +0100 | [diff] [blame] | 1684 | return null; |
Maggie Benthall | 0469f41 | 2013-09-05 15:30:26 -0400 | [diff] [blame] | 1685 | } |
| 1686 | |
Robin Lee | d0130e8 | 2014-10-06 11:36:25 +0100 | [diff] [blame] | 1687 | private void manageNotification(UserHandle userHandle) { |
| 1688 | if (!mUserManager.isUserRunning(userHandle)) { |
| 1689 | return; |
Maggie Benthall | 0469f41 | 2013-09-05 15:30:26 -0400 | [diff] [blame] | 1690 | } |
Robin Lee | d0130e8 | 2014-10-06 11:36:25 +0100 | [diff] [blame] | 1691 | |
Robin Lee | 80559f4 | 2014-11-25 13:40:49 +0000 | [diff] [blame] | 1692 | // Call out to KeyChain to check for user-added CAs |
Robin Lee | d0130e8 | 2014-10-06 11:36:25 +0100 | [diff] [blame] | 1693 | boolean hasCert = false; |
Robin Lee | d0130e8 | 2014-10-06 11:36:25 +0100 | [diff] [blame] | 1694 | try { |
| 1695 | KeyChainConnection kcs = KeyChain.bindAsUser(mContext, userHandle); |
| 1696 | try { |
| 1697 | if (!kcs.getService().getUserCaAliases().getList().isEmpty()) { |
| 1698 | hasCert = true; |
| 1699 | } |
| 1700 | } catch (RemoteException e) { |
| 1701 | Log.e(LOG_TAG, "Could not connect to KeyChain service", e); |
| 1702 | } finally { |
| 1703 | kcs.close(); |
| 1704 | } |
| 1705 | } catch (InterruptedException e) { |
| 1706 | Thread.currentThread().interrupt(); |
| 1707 | } catch (RuntimeException e) { |
| 1708 | Log.e(LOG_TAG, "Could not connect to KeyChain service", e); |
Robin Lee | d0130e8 | 2014-10-06 11:36:25 +0100 | [diff] [blame] | 1709 | } |
| 1710 | if (!hasCert) { |
| 1711 | getNotificationManager().cancelAsUser( |
| 1712 | null, MONITORING_CERT_NOTIFICATION_ID, userHandle); |
| 1713 | return; |
| 1714 | } |
| 1715 | |
Robin Lee | 80559f4 | 2014-11-25 13:40:49 +0000 | [diff] [blame] | 1716 | // Build and show a warning notification |
Robin Lee | d0130e8 | 2014-10-06 11:36:25 +0100 | [diff] [blame] | 1717 | int smallIconId; |
| 1718 | String contentText; |
| 1719 | final String ownerName = getDeviceOwnerName(); |
Robin Lee | 7f7ea82 | 2015-01-08 22:46:44 +0000 | [diff] [blame] | 1720 | if (isManagedProfile(userHandle.getIdentifier())) { |
| 1721 | contentText = mContext.getString(R.string.ssl_ca_cert_noti_by_administrator); |
| 1722 | smallIconId = R.drawable.stat_sys_certificate_info; |
| 1723 | } else if (ownerName != null) { |
Robin Lee | d0130e8 | 2014-10-06 11:36:25 +0100 | [diff] [blame] | 1724 | contentText = mContext.getString(R.string.ssl_ca_cert_noti_managed, ownerName); |
| 1725 | smallIconId = R.drawable.stat_sys_certificate_info; |
| 1726 | } else { |
| 1727 | contentText = mContext.getString(R.string.ssl_ca_cert_noti_by_unknown); |
| 1728 | smallIconId = android.R.drawable.stat_sys_warning; |
| 1729 | } |
| 1730 | |
| 1731 | Intent dialogIntent = new Intent(Settings.ACTION_MONITORING_CERT_INFO); |
| 1732 | dialogIntent.setFlags( |
| 1733 | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); |
| 1734 | dialogIntent.setPackage("com.android.settings"); |
| 1735 | PendingIntent notifyIntent = PendingIntent.getActivityAsUser(mContext, 0, |
| 1736 | dialogIntent, PendingIntent.FLAG_UPDATE_CURRENT, null, userHandle); |
| 1737 | |
| 1738 | final Context userContext; |
| 1739 | try { |
| 1740 | userContext = mContext.createPackageContextAsUser("android", 0, userHandle); |
| 1741 | } catch (PackageManager.NameNotFoundException e) { |
| 1742 | Log.e(LOG_TAG, "Create context as " + userHandle + " failed", e); |
| 1743 | return; |
| 1744 | } |
| 1745 | final Notification noti = new Notification.Builder(userContext) |
| 1746 | .setSmallIcon(smallIconId) |
| 1747 | .setContentTitle(mContext.getString(R.string.ssl_ca_cert_warning)) |
| 1748 | .setContentText(contentText) |
| 1749 | .setContentIntent(notifyIntent) |
Robin Lee | d0130e8 | 2014-10-06 11:36:25 +0100 | [diff] [blame] | 1750 | .setPriority(Notification.PRIORITY_HIGH) |
| 1751 | .setShowWhen(false) |
| 1752 | .setColor(mContext.getResources().getColor( |
| 1753 | com.android.internal.R.color.system_notification_accent_color)) |
| 1754 | .build(); |
| 1755 | |
| 1756 | getNotificationManager().notifyAsUser( |
| 1757 | null, MONITORING_CERT_NOTIFICATION_ID, noti, userHandle); |
Maggie Benthall | 0469f41 | 2013-09-05 15:30:26 -0400 | [diff] [blame] | 1758 | } |
| 1759 | } |
| 1760 | |
Andy Stadler | c25f70a | 2010-12-08 15:56:45 -0800 | [diff] [blame] | 1761 | /** |
| 1762 | * @param adminReceiver The admin to add |
| 1763 | * @param refreshing true = update an active admin, no error |
| 1764 | */ |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1765 | public void setActiveAdmin(ComponentName adminReceiver, boolean refreshing, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 1766 | if (!mHasFeature) { |
| 1767 | return; |
| 1768 | } |
Jason Monk | 03978a4 | 2014-06-10 15:05:30 -0400 | [diff] [blame] | 1769 | setActiveAdmin(adminReceiver, refreshing, userHandle, null); |
| 1770 | } |
| 1771 | |
| 1772 | private void setActiveAdmin(ComponentName adminReceiver, boolean refreshing, int userHandle, |
| 1773 | Bundle onEnableData) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1774 | mContext.enforceCallingOrSelfPermission( |
Amith Yamasani | a418cf2 | 2013-07-19 12:39:17 -0700 | [diff] [blame] | 1775 | android.Manifest.permission.MANAGE_DEVICE_ADMINS, null); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1776 | enforceCrossUserPermission(userHandle); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1777 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1778 | DevicePolicyData policy = getUserData(userHandle); |
| 1779 | DeviceAdminInfo info = findAdmin(adminReceiver, userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1780 | if (info == null) { |
| 1781 | throw new IllegalArgumentException("Bad admin: " + adminReceiver); |
| 1782 | } |
| 1783 | synchronized (this) { |
| 1784 | long ident = Binder.clearCallingIdentity(); |
| 1785 | try { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 1786 | if (!refreshing |
| 1787 | && getActiveAdminUncheckedLocked(adminReceiver, userHandle) != null) { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1788 | throw new IllegalArgumentException("Admin is already added"); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1789 | } |
Andy Stadler | c25f70a | 2010-12-08 15:56:45 -0800 | [diff] [blame] | 1790 | ActiveAdmin newAdmin = new ActiveAdmin(info); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1791 | policy.mAdminMap.put(adminReceiver, newAdmin); |
Andy Stadler | c25f70a | 2010-12-08 15:56:45 -0800 | [diff] [blame] | 1792 | int replaceIndex = -1; |
Robin Lee | 9c2be6c | 2014-07-22 16:47:40 +0100 | [diff] [blame] | 1793 | final int N = policy.mAdminList.size(); |
| 1794 | for (int i=0; i < N; i++) { |
| 1795 | ActiveAdmin oldAdmin = policy.mAdminList.get(i); |
| 1796 | if (oldAdmin.info.getComponent().equals(adminReceiver)) { |
| 1797 | replaceIndex = i; |
| 1798 | break; |
Andy Stadler | c25f70a | 2010-12-08 15:56:45 -0800 | [diff] [blame] | 1799 | } |
| 1800 | } |
| 1801 | if (replaceIndex == -1) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1802 | policy.mAdminList.add(newAdmin); |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 1803 | enableIfNecessary(info.getPackageName(), userHandle); |
Andy Stadler | c25f70a | 2010-12-08 15:56:45 -0800 | [diff] [blame] | 1804 | } else { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1805 | policy.mAdminList.set(replaceIndex, newAdmin); |
Andy Stadler | c25f70a | 2010-12-08 15:56:45 -0800 | [diff] [blame] | 1806 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1807 | saveSettingsLocked(userHandle); |
Jason Monk | 03978a4 | 2014-06-10 15:05:30 -0400 | [diff] [blame] | 1808 | sendAdminCommandLocked(newAdmin, DeviceAdminReceiver.ACTION_DEVICE_ADMIN_ENABLED, |
| 1809 | onEnableData, null); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1810 | } finally { |
| 1811 | Binder.restoreCallingIdentity(ident); |
| 1812 | } |
| 1813 | } |
| 1814 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1815 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1816 | public boolean isAdminActive(ComponentName adminReceiver, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 1817 | if (!mHasFeature) { |
| 1818 | return false; |
| 1819 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1820 | enforceCrossUserPermission(userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1821 | synchronized (this) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1822 | return getActiveAdminUncheckedLocked(adminReceiver, userHandle) != null; |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1823 | } |
| 1824 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1825 | |
Fyodor Kupolov | 96fb932 | 2014-12-01 15:08:09 -0800 | [diff] [blame] | 1826 | @Override |
| 1827 | public boolean isRemovingAdmin(ComponentName adminReceiver, int userHandle) { |
| 1828 | if (!mHasFeature) { |
| 1829 | return false; |
| 1830 | } |
| 1831 | enforceCrossUserPermission(userHandle); |
| 1832 | synchronized (this) { |
| 1833 | DevicePolicyData policyData = getUserData(userHandle); |
| 1834 | return policyData.mRemovingAdmins.contains(adminReceiver); |
| 1835 | } |
| 1836 | } |
| 1837 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1838 | public boolean hasGrantedPolicy(ComponentName adminReceiver, int policyId, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 1839 | if (!mHasFeature) { |
| 1840 | return false; |
| 1841 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1842 | enforceCrossUserPermission(userHandle); |
Andy Stadler | c25f70a | 2010-12-08 15:56:45 -0800 | [diff] [blame] | 1843 | synchronized (this) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1844 | ActiveAdmin administrator = getActiveAdminUncheckedLocked(adminReceiver, userHandle); |
Andy Stadler | c25f70a | 2010-12-08 15:56:45 -0800 | [diff] [blame] | 1845 | if (administrator == null) { |
| 1846 | throw new SecurityException("No active admin " + adminReceiver); |
| 1847 | } |
| 1848 | return administrator.info.usesPolicy(policyId); |
| 1849 | } |
| 1850 | } |
| 1851 | |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 1852 | @SuppressWarnings("unchecked") |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1853 | public List<ComponentName> getActiveAdmins(int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 1854 | if (!mHasFeature) { |
| 1855 | return Collections.EMPTY_LIST; |
| 1856 | } |
| 1857 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1858 | enforceCrossUserPermission(userHandle); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1859 | synchronized (this) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1860 | DevicePolicyData policy = getUserData(userHandle); |
| 1861 | final int N = policy.mAdminList.size(); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1862 | if (N <= 0) { |
| 1863 | return null; |
| 1864 | } |
| 1865 | ArrayList<ComponentName> res = new ArrayList<ComponentName>(N); |
| 1866 | for (int i=0; i<N; i++) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1867 | res.add(policy.mAdminList.get(i).info.getComponent()); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1868 | } |
| 1869 | return res; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1870 | } |
| 1871 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1872 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1873 | public boolean packageHasActiveAdmins(String packageName, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 1874 | if (!mHasFeature) { |
| 1875 | return false; |
| 1876 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1877 | enforceCrossUserPermission(userHandle); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1878 | synchronized (this) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1879 | DevicePolicyData policy = getUserData(userHandle); |
| 1880 | final int N = policy.mAdminList.size(); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1881 | for (int i=0; i<N; i++) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1882 | if (policy.mAdminList.get(i).info.getPackageName().equals(packageName)) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1883 | return true; |
| 1884 | } |
| 1885 | } |
| 1886 | return false; |
| 1887 | } |
| 1888 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1889 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1890 | public void removeActiveAdmin(ComponentName adminReceiver, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 1891 | if (!mHasFeature) { |
| 1892 | return; |
| 1893 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1894 | enforceCrossUserPermission(userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1895 | synchronized (this) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1896 | ActiveAdmin admin = getActiveAdminUncheckedLocked(adminReceiver, userHandle); |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1897 | if (admin == null) { |
| 1898 | return; |
| 1899 | } |
| 1900 | if (admin.getUid() != Binder.getCallingUid()) { |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 1901 | // If trying to remove device owner, refuse when the caller is not the owner. |
Robin Lee | 08fc72d | 2014-02-20 12:50:41 +0000 | [diff] [blame] | 1902 | if (isDeviceOwner(adminReceiver.getPackageName())) { |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 1903 | return; |
| 1904 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1905 | mContext.enforceCallingOrSelfPermission( |
Amith Yamasani | a418cf2 | 2013-07-19 12:39:17 -0700 | [diff] [blame] | 1906 | android.Manifest.permission.MANAGE_DEVICE_ADMINS, null); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1907 | } |
| 1908 | long ident = Binder.clearCallingIdentity(); |
| 1909 | try { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1910 | removeActiveAdminLocked(adminReceiver, userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1911 | } finally { |
| 1912 | Binder.restoreCallingIdentity(ident); |
| 1913 | } |
| 1914 | } |
| 1915 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1916 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1917 | public void setPasswordQuality(ComponentName who, int quality, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 1918 | if (!mHasFeature) { |
| 1919 | return; |
| 1920 | } |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1921 | validateQualityConstant(quality); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1922 | enforceCrossUserPermission(userHandle); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1923 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1924 | synchronized (this) { |
| 1925 | if (who == null) { |
| 1926 | throw new NullPointerException("ComponentName is null"); |
| 1927 | } |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 1928 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 1929 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 1930 | if (ap.passwordQuality != quality) { |
| 1931 | ap.passwordQuality = quality; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1932 | saveSettingsLocked(userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1933 | } |
| 1934 | } |
| 1935 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1936 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1937 | public int getPasswordQuality(ComponentName who, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 1938 | if (!mHasFeature) { |
| 1939 | return DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; |
| 1940 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1941 | enforceCrossUserPermission(userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1942 | synchronized (this) { |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 1943 | int mode = DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED; |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1944 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1945 | if (who != null) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1946 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who, userHandle); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 1947 | return admin != null ? admin.passwordQuality : mode; |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1948 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1949 | |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 1950 | // Return strictest policy for this user and profiles that are visible from this user. |
| 1951 | List<UserInfo> profiles = mUserManager.getProfiles(userHandle); |
| 1952 | for (UserInfo userInfo : profiles) { |
Benjamin Franz | fadb2b3 | 2015-01-14 18:22:42 +0000 | [diff] [blame] | 1953 | DevicePolicyData policy = getUserDataUnchecked(userInfo.id); |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 1954 | final int N = policy.mAdminList.size(); |
| 1955 | for (int i=0; i<N; i++) { |
| 1956 | ActiveAdmin admin = policy.mAdminList.get(i); |
| 1957 | if (mode < admin.passwordQuality) { |
| 1958 | mode = admin.passwordQuality; |
| 1959 | } |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1960 | } |
| 1961 | } |
| 1962 | return mode; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1963 | } |
| 1964 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1965 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1966 | public void setPasswordMinimumLength(ComponentName who, int length, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 1967 | if (!mHasFeature) { |
| 1968 | return; |
| 1969 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1970 | enforceCrossUserPermission(userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1971 | synchronized (this) { |
| 1972 | if (who == null) { |
| 1973 | throw new NullPointerException("ComponentName is null"); |
| 1974 | } |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 1975 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 1976 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1977 | if (ap.minimumPasswordLength != length) { |
| 1978 | ap.minimumPasswordLength = length; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1979 | saveSettingsLocked(userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1980 | } |
| 1981 | } |
| 1982 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1983 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1984 | public int getPasswordMinimumLength(ComponentName who, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 1985 | if (!mHasFeature) { |
| 1986 | return 0; |
| 1987 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1988 | enforceCrossUserPermission(userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1989 | synchronized (this) { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 1990 | int length = 0; |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1991 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1992 | if (who != null) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 1993 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who, userHandle); |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 1994 | return admin != null ? admin.minimumPasswordLength : length; |
| 1995 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 1996 | |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 1997 | // Return strictest policy for this user and profiles that are visible from this user. |
| 1998 | List<UserInfo> profiles = mUserManager.getProfiles(userHandle); |
| 1999 | for (UserInfo userInfo : profiles) { |
Benjamin Franz | fadb2b3 | 2015-01-14 18:22:42 +0000 | [diff] [blame] | 2000 | DevicePolicyData policy = getUserDataUnchecked(userInfo.id); |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 2001 | final int N = policy.mAdminList.size(); |
| 2002 | for (int i=0; i<N; i++) { |
| 2003 | ActiveAdmin admin = policy.mAdminList.get(i); |
| 2004 | if (length < admin.minimumPasswordLength) { |
| 2005 | length = admin.minimumPasswordLength; |
| 2006 | } |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 2007 | } |
| 2008 | } |
| 2009 | return length; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 2010 | } |
| 2011 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 2012 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2013 | public void setPasswordHistoryLength(ComponentName who, int length, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 2014 | if (!mHasFeature) { |
| 2015 | return; |
| 2016 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2017 | enforceCrossUserPermission(userHandle); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 2018 | synchronized (this) { |
| 2019 | if (who == null) { |
| 2020 | throw new NullPointerException("ComponentName is null"); |
| 2021 | } |
| 2022 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 2023 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
| 2024 | if (ap.passwordHistoryLength != length) { |
| 2025 | ap.passwordHistoryLength = length; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2026 | saveSettingsLocked(userHandle); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 2027 | } |
| 2028 | } |
| 2029 | } |
| 2030 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2031 | public int getPasswordHistoryLength(ComponentName who, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 2032 | if (!mHasFeature) { |
| 2033 | return 0; |
| 2034 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2035 | enforceCrossUserPermission(userHandle); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 2036 | synchronized (this) { |
| 2037 | int length = 0; |
| 2038 | |
| 2039 | if (who != null) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2040 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who, userHandle); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 2041 | return admin != null ? admin.passwordHistoryLength : length; |
| 2042 | } |
| 2043 | |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 2044 | // Return strictest policy for this user and profiles that are visible from this user. |
| 2045 | List<UserInfo> profiles = mUserManager.getProfiles(userHandle); |
| 2046 | for (UserInfo userInfo : profiles) { |
Benjamin Franz | fadb2b3 | 2015-01-14 18:22:42 +0000 | [diff] [blame] | 2047 | DevicePolicyData policy = getUserDataUnchecked(userInfo.id); |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 2048 | final int N = policy.mAdminList.size(); |
| 2049 | for (int i = 0; i < N; i++) { |
| 2050 | ActiveAdmin admin = policy.mAdminList.get(i); |
| 2051 | if (length < admin.passwordHistoryLength) { |
| 2052 | length = admin.passwordHistoryLength; |
| 2053 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 2054 | } |
| 2055 | } |
| 2056 | return length; |
| 2057 | } |
| 2058 | } |
| 2059 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2060 | public void setPasswordExpirationTimeout(ComponentName who, long timeout, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 2061 | if (!mHasFeature) { |
| 2062 | return; |
| 2063 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2064 | enforceCrossUserPermission(userHandle); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 2065 | synchronized (this) { |
| 2066 | if (who == null) { |
| 2067 | throw new NullPointerException("ComponentName is null"); |
| 2068 | } |
Andy Stadler | 1f35d48 | 2010-11-19 15:39:41 -0800 | [diff] [blame] | 2069 | if (timeout < 0) { |
| 2070 | throw new IllegalArgumentException("Timeout must be >= 0 ms"); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 2071 | } |
| 2072 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 2073 | DeviceAdminInfo.USES_POLICY_EXPIRE_PASSWORD); |
| 2074 | // Calling this API automatically bumps the expiration date |
| 2075 | final long expiration = timeout > 0L ? (timeout + System.currentTimeMillis()) : 0L; |
| 2076 | ap.passwordExpirationDate = expiration; |
| 2077 | ap.passwordExpirationTimeout = timeout; |
| 2078 | if (timeout > 0L) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 2079 | Slog.w(LOG_TAG, "setPasswordExpiration(): password will expire on " |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 2080 | + DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT) |
| 2081 | .format(new Date(expiration))); |
| 2082 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2083 | saveSettingsLocked(userHandle); |
| 2084 | // in case this is the first one |
| 2085 | setExpirationAlarmCheckLocked(mContext, getUserData(userHandle)); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 2086 | } |
| 2087 | } |
| 2088 | |
Andy Stadler | 043116a | 2010-11-29 17:43:32 -0800 | [diff] [blame] | 2089 | /** |
| 2090 | * Return a single admin's expiration cycle time, or the min of all cycle times. |
| 2091 | * Returns 0 if not configured. |
| 2092 | */ |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2093 | public long getPasswordExpirationTimeout(ComponentName who, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 2094 | if (!mHasFeature) { |
| 2095 | return 0L; |
| 2096 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2097 | enforceCrossUserPermission(userHandle); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 2098 | synchronized (this) { |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 2099 | long timeout = 0L; |
| 2100 | |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 2101 | if (who != null) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2102 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who, userHandle); |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 2103 | return admin != null ? admin.passwordExpirationTimeout : timeout; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 2104 | } |
| 2105 | |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 2106 | List<UserInfo> profiles = mUserManager.getProfiles(userHandle); |
| 2107 | for (UserInfo userInfo : profiles) { |
Benjamin Franz | fadb2b3 | 2015-01-14 18:22:42 +0000 | [diff] [blame] | 2108 | DevicePolicyData policy = getUserDataUnchecked(userInfo.id); |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 2109 | final int N = policy.mAdminList.size(); |
| 2110 | for (int i = 0; i < N; i++) { |
| 2111 | ActiveAdmin admin = policy.mAdminList.get(i); |
| 2112 | if (timeout == 0L || (admin.passwordExpirationTimeout != 0L |
| 2113 | && timeout > admin.passwordExpirationTimeout)) { |
| 2114 | timeout = admin.passwordExpirationTimeout; |
| 2115 | } |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 2116 | } |
| 2117 | } |
| 2118 | return timeout; |
| 2119 | } |
| 2120 | } |
| 2121 | |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 2122 | @Override |
| 2123 | public boolean addCrossProfileWidgetProvider(ComponentName admin, String packageName) { |
Svet Ganov | 6bd7025 | 2014-08-20 09:47:47 -0700 | [diff] [blame] | 2124 | final int userId = UserHandle.getCallingUserId(); |
| 2125 | List<String> changedProviders = null; |
| 2126 | |
| 2127 | synchronized (this) { |
| 2128 | ActiveAdmin activeAdmin = getActiveAdminForCallerLocked(admin, |
| 2129 | DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 2130 | if (activeAdmin.crossProfileWidgetProviders == null) { |
| 2131 | activeAdmin.crossProfileWidgetProviders = new ArrayList<>(); |
| 2132 | } |
| 2133 | List<String> providers = activeAdmin.crossProfileWidgetProviders; |
| 2134 | if (!providers.contains(packageName)) { |
| 2135 | providers.add(packageName); |
| 2136 | changedProviders = new ArrayList<>(providers); |
| 2137 | saveSettingsLocked(userId); |
| 2138 | } |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 2139 | } |
Svet Ganov | 6bd7025 | 2014-08-20 09:47:47 -0700 | [diff] [blame] | 2140 | |
| 2141 | if (changedProviders != null) { |
| 2142 | mLocalService.notifyCrossProfileProvidersChanged(userId, changedProviders); |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 2143 | return true; |
| 2144 | } |
Svet Ganov | 6bd7025 | 2014-08-20 09:47:47 -0700 | [diff] [blame] | 2145 | |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 2146 | return false; |
| 2147 | } |
| 2148 | |
| 2149 | @Override |
| 2150 | public boolean removeCrossProfileWidgetProvider(ComponentName admin, String packageName) { |
Svet Ganov | 6bd7025 | 2014-08-20 09:47:47 -0700 | [diff] [blame] | 2151 | final int userId = UserHandle.getCallingUserId(); |
| 2152 | List<String> changedProviders = null; |
| 2153 | |
| 2154 | synchronized (this) { |
| 2155 | ActiveAdmin activeAdmin = getActiveAdminForCallerLocked(admin, |
| 2156 | DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 2157 | if (activeAdmin.crossProfileWidgetProviders == null) { |
| 2158 | return false; |
| 2159 | } |
| 2160 | List<String> providers = activeAdmin.crossProfileWidgetProviders; |
| 2161 | if (providers.remove(packageName)) { |
| 2162 | changedProviders = new ArrayList<>(providers); |
| 2163 | saveSettingsLocked(userId); |
| 2164 | } |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 2165 | } |
Svet Ganov | 6bd7025 | 2014-08-20 09:47:47 -0700 | [diff] [blame] | 2166 | |
| 2167 | if (changedProviders != null) { |
| 2168 | mLocalService.notifyCrossProfileProvidersChanged(userId, changedProviders); |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 2169 | return true; |
| 2170 | } |
Svet Ganov | 6bd7025 | 2014-08-20 09:47:47 -0700 | [diff] [blame] | 2171 | |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 2172 | return false; |
| 2173 | } |
| 2174 | |
| 2175 | @Override |
| 2176 | public List<String> getCrossProfileWidgetProviders(ComponentName admin) { |
Svet Ganov | 6bd7025 | 2014-08-20 09:47:47 -0700 | [diff] [blame] | 2177 | synchronized (this) { |
| 2178 | ActiveAdmin activeAdmin = getActiveAdminForCallerLocked(admin, |
| 2179 | DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 2180 | if (activeAdmin.crossProfileWidgetProviders == null |
| 2181 | || activeAdmin.crossProfileWidgetProviders.isEmpty()) { |
| 2182 | return null; |
| 2183 | } |
| 2184 | if (Binder.getCallingUid() == Process.myUid()) { |
| 2185 | return new ArrayList<>(activeAdmin.crossProfileWidgetProviders); |
| 2186 | } else { |
| 2187 | return activeAdmin.crossProfileWidgetProviders; |
| 2188 | } |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 2189 | } |
| 2190 | } |
| 2191 | |
Andy Stadler | 043116a | 2010-11-29 17:43:32 -0800 | [diff] [blame] | 2192 | /** |
| 2193 | * Return a single admin's expiration date/time, or the min (soonest) for all admins. |
| 2194 | * Returns 0 if not configured. |
| 2195 | */ |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2196 | private long getPasswordExpirationLocked(ComponentName who, int userHandle) { |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 2197 | long timeout = 0L; |
| 2198 | |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 2199 | if (who != null) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2200 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who, userHandle); |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 2201 | return admin != null ? admin.passwordExpirationDate : timeout; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 2202 | } |
| 2203 | |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 2204 | List<UserInfo> profiles = mUserManager.getProfiles(userHandle); |
| 2205 | for (UserInfo userInfo : profiles) { |
Benjamin Franz | fadb2b3 | 2015-01-14 18:22:42 +0000 | [diff] [blame] | 2206 | DevicePolicyData policy = getUserDataUnchecked(userInfo.id); |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 2207 | final int N = policy.mAdminList.size(); |
| 2208 | for (int i = 0; i < N; i++) { |
| 2209 | ActiveAdmin admin = policy.mAdminList.get(i); |
| 2210 | if (timeout == 0L || (admin.passwordExpirationDate != 0 |
| 2211 | && timeout > admin.passwordExpirationDate)) { |
| 2212 | timeout = admin.passwordExpirationDate; |
| 2213 | } |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 2214 | } |
| 2215 | } |
| 2216 | return timeout; |
| 2217 | } |
| 2218 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2219 | public long getPasswordExpiration(ComponentName who, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 2220 | if (!mHasFeature) { |
| 2221 | return 0L; |
| 2222 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2223 | enforceCrossUserPermission(userHandle); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 2224 | synchronized (this) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2225 | return getPasswordExpirationLocked(who, userHandle); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 2226 | } |
| 2227 | } |
| 2228 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2229 | public void setPasswordMinimumUpperCase(ComponentName who, int length, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 2230 | if (!mHasFeature) { |
| 2231 | return; |
| 2232 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2233 | enforceCrossUserPermission(userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2234 | synchronized (this) { |
| 2235 | if (who == null) { |
| 2236 | throw new NullPointerException("ComponentName is null"); |
| 2237 | } |
| 2238 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 2239 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
| 2240 | if (ap.minimumPasswordUpperCase != length) { |
| 2241 | ap.minimumPasswordUpperCase = length; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2242 | saveSettingsLocked(userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2243 | } |
| 2244 | } |
| 2245 | } |
| 2246 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2247 | public int getPasswordMinimumUpperCase(ComponentName who, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 2248 | if (!mHasFeature) { |
| 2249 | return 0; |
| 2250 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2251 | enforceCrossUserPermission(userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2252 | synchronized (this) { |
| 2253 | int length = 0; |
| 2254 | |
| 2255 | if (who != null) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2256 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who, userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2257 | return admin != null ? admin.minimumPasswordUpperCase : length; |
| 2258 | } |
| 2259 | |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 2260 | // Return strictest policy for this user and profiles that are visible from this user. |
| 2261 | List<UserInfo> profiles = mUserManager.getProfiles(userHandle); |
| 2262 | for (UserInfo userInfo : profiles) { |
Benjamin Franz | fadb2b3 | 2015-01-14 18:22:42 +0000 | [diff] [blame] | 2263 | DevicePolicyData policy = getUserDataUnchecked(userInfo.id); |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 2264 | final int N = policy.mAdminList.size(); |
| 2265 | for (int i=0; i<N; i++) { |
| 2266 | ActiveAdmin admin = policy.mAdminList.get(i); |
| 2267 | if (length < admin.minimumPasswordUpperCase) { |
| 2268 | length = admin.minimumPasswordUpperCase; |
| 2269 | } |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2270 | } |
| 2271 | } |
| 2272 | return length; |
| 2273 | } |
| 2274 | } |
| 2275 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2276 | public void setPasswordMinimumLowerCase(ComponentName who, int length, int userHandle) { |
| 2277 | enforceCrossUserPermission(userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2278 | synchronized (this) { |
| 2279 | if (who == null) { |
| 2280 | throw new NullPointerException("ComponentName is null"); |
| 2281 | } |
| 2282 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 2283 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
| 2284 | if (ap.minimumPasswordLowerCase != length) { |
| 2285 | ap.minimumPasswordLowerCase = length; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2286 | saveSettingsLocked(userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2287 | } |
| 2288 | } |
| 2289 | } |
| 2290 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2291 | public int getPasswordMinimumLowerCase(ComponentName who, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 2292 | if (!mHasFeature) { |
| 2293 | return 0; |
| 2294 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2295 | enforceCrossUserPermission(userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2296 | synchronized (this) { |
| 2297 | int length = 0; |
| 2298 | |
| 2299 | if (who != null) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2300 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who, userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2301 | return admin != null ? admin.minimumPasswordLowerCase : length; |
| 2302 | } |
| 2303 | |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 2304 | // Return strictest policy for this user and profiles that are visible from this user. |
| 2305 | List<UserInfo> profiles = mUserManager.getProfiles(userHandle); |
| 2306 | for (UserInfo userInfo : profiles) { |
Benjamin Franz | fadb2b3 | 2015-01-14 18:22:42 +0000 | [diff] [blame] | 2307 | DevicePolicyData policy = getUserDataUnchecked(userInfo.id); |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 2308 | final int N = policy.mAdminList.size(); |
| 2309 | for (int i=0; i<N; i++) { |
| 2310 | ActiveAdmin admin = policy.mAdminList.get(i); |
| 2311 | if (length < admin.minimumPasswordLowerCase) { |
| 2312 | length = admin.minimumPasswordLowerCase; |
| 2313 | } |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2314 | } |
| 2315 | } |
| 2316 | return length; |
| 2317 | } |
| 2318 | } |
| 2319 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2320 | public void setPasswordMinimumLetters(ComponentName who, int length, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 2321 | if (!mHasFeature) { |
| 2322 | return; |
| 2323 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2324 | enforceCrossUserPermission(userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2325 | synchronized (this) { |
| 2326 | if (who == null) { |
| 2327 | throw new NullPointerException("ComponentName is null"); |
| 2328 | } |
| 2329 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 2330 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
| 2331 | if (ap.minimumPasswordLetters != length) { |
| 2332 | ap.minimumPasswordLetters = length; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2333 | saveSettingsLocked(userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2334 | } |
| 2335 | } |
| 2336 | } |
| 2337 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2338 | public int getPasswordMinimumLetters(ComponentName who, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 2339 | if (!mHasFeature) { |
| 2340 | return 0; |
| 2341 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2342 | enforceCrossUserPermission(userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2343 | synchronized (this) { |
| 2344 | int length = 0; |
| 2345 | |
| 2346 | if (who != null) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2347 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who, userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2348 | return admin != null ? admin.minimumPasswordLetters : length; |
| 2349 | } |
| 2350 | |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 2351 | // Return strictest policy for this user and profiles that are visible from this user. |
| 2352 | List<UserInfo> profiles = mUserManager.getProfiles(userHandle); |
| 2353 | for (UserInfo userInfo : profiles) { |
Benjamin Franz | fadb2b3 | 2015-01-14 18:22:42 +0000 | [diff] [blame] | 2354 | DevicePolicyData policy = getUserDataUnchecked(userInfo.id); |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 2355 | final int N = policy.mAdminList.size(); |
| 2356 | for (int i=0; i<N; i++) { |
| 2357 | ActiveAdmin admin = policy.mAdminList.get(i); |
| 2358 | if (length < admin.minimumPasswordLetters) { |
| 2359 | length = admin.minimumPasswordLetters; |
| 2360 | } |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2361 | } |
| 2362 | } |
| 2363 | return length; |
| 2364 | } |
| 2365 | } |
| 2366 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2367 | public void setPasswordMinimumNumeric(ComponentName who, int length, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 2368 | if (!mHasFeature) { |
| 2369 | return; |
| 2370 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2371 | enforceCrossUserPermission(userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2372 | synchronized (this) { |
| 2373 | if (who == null) { |
| 2374 | throw new NullPointerException("ComponentName is null"); |
| 2375 | } |
| 2376 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 2377 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
| 2378 | if (ap.minimumPasswordNumeric != length) { |
| 2379 | ap.minimumPasswordNumeric = length; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2380 | saveSettingsLocked(userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2381 | } |
| 2382 | } |
| 2383 | } |
| 2384 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2385 | public int getPasswordMinimumNumeric(ComponentName who, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 2386 | if (!mHasFeature) { |
| 2387 | return 0; |
| 2388 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2389 | enforceCrossUserPermission(userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2390 | synchronized (this) { |
| 2391 | int length = 0; |
| 2392 | |
| 2393 | if (who != null) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2394 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who, userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2395 | return admin != null ? admin.minimumPasswordNumeric : length; |
| 2396 | } |
| 2397 | |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 2398 | // Return strictest policy for this user and profiles that are visible from this user. |
| 2399 | List<UserInfo> profiles = mUserManager.getProfiles(userHandle); |
| 2400 | for (UserInfo userInfo : profiles) { |
Benjamin Franz | fadb2b3 | 2015-01-14 18:22:42 +0000 | [diff] [blame] | 2401 | DevicePolicyData policy = getUserDataUnchecked(userInfo.id); |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 2402 | final int N = policy.mAdminList.size(); |
| 2403 | for (int i = 0; i < N; i++) { |
| 2404 | ActiveAdmin admin = policy.mAdminList.get(i); |
| 2405 | if (length < admin.minimumPasswordNumeric) { |
| 2406 | length = admin.minimumPasswordNumeric; |
| 2407 | } |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2408 | } |
| 2409 | } |
| 2410 | return length; |
| 2411 | } |
| 2412 | } |
| 2413 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2414 | public void setPasswordMinimumSymbols(ComponentName who, int length, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 2415 | if (!mHasFeature) { |
| 2416 | return; |
| 2417 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2418 | enforceCrossUserPermission(userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2419 | synchronized (this) { |
| 2420 | if (who == null) { |
| 2421 | throw new NullPointerException("ComponentName is null"); |
| 2422 | } |
| 2423 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 2424 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
| 2425 | if (ap.minimumPasswordSymbols != length) { |
| 2426 | ap.minimumPasswordSymbols = length; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2427 | saveSettingsLocked(userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2428 | } |
| 2429 | } |
| 2430 | } |
| 2431 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2432 | public int getPasswordMinimumSymbols(ComponentName who, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 2433 | if (!mHasFeature) { |
| 2434 | return 0; |
| 2435 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2436 | enforceCrossUserPermission(userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2437 | synchronized (this) { |
| 2438 | int length = 0; |
| 2439 | |
| 2440 | if (who != null) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2441 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who, userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2442 | return admin != null ? admin.minimumPasswordSymbols : length; |
| 2443 | } |
| 2444 | |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 2445 | // Return strictest policy for this user and profiles that are visible from this user. |
| 2446 | List<UserInfo> profiles = mUserManager.getProfiles(userHandle); |
| 2447 | for (UserInfo userInfo : profiles) { |
Benjamin Franz | fadb2b3 | 2015-01-14 18:22:42 +0000 | [diff] [blame] | 2448 | DevicePolicyData policy = getUserDataUnchecked(userInfo.id); |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 2449 | final int N = policy.mAdminList.size(); |
| 2450 | for (int i=0; i<N; i++) { |
| 2451 | ActiveAdmin admin = policy.mAdminList.get(i); |
| 2452 | if (length < admin.minimumPasswordSymbols) { |
| 2453 | length = admin.minimumPasswordSymbols; |
| 2454 | } |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2455 | } |
| 2456 | } |
| 2457 | return length; |
| 2458 | } |
| 2459 | } |
| 2460 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2461 | public void setPasswordMinimumNonLetter(ComponentName who, int length, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 2462 | if (!mHasFeature) { |
| 2463 | return; |
| 2464 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2465 | enforceCrossUserPermission(userHandle); |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 2466 | synchronized (this) { |
| 2467 | if (who == null) { |
| 2468 | throw new NullPointerException("ComponentName is null"); |
| 2469 | } |
| 2470 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 2471 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
| 2472 | if (ap.minimumPasswordNonLetter != length) { |
| 2473 | ap.minimumPasswordNonLetter = length; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2474 | saveSettingsLocked(userHandle); |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 2475 | } |
| 2476 | } |
| 2477 | } |
| 2478 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2479 | public int getPasswordMinimumNonLetter(ComponentName who, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 2480 | if (!mHasFeature) { |
| 2481 | return 0; |
| 2482 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2483 | enforceCrossUserPermission(userHandle); |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 2484 | synchronized (this) { |
| 2485 | int length = 0; |
| 2486 | |
| 2487 | if (who != null) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2488 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who, userHandle); |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 2489 | return admin != null ? admin.minimumPasswordNonLetter : length; |
| 2490 | } |
| 2491 | |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 2492 | // Return strictest policy for this user and profiles that are visible from this user. |
| 2493 | List<UserInfo> profiles = mUserManager.getProfiles(userHandle); |
| 2494 | for (UserInfo userInfo : profiles) { |
Benjamin Franz | fadb2b3 | 2015-01-14 18:22:42 +0000 | [diff] [blame] | 2495 | DevicePolicyData policy = getUserDataUnchecked(userInfo.id); |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 2496 | final int N = policy.mAdminList.size(); |
| 2497 | for (int i=0; i<N; i++) { |
| 2498 | ActiveAdmin admin = policy.mAdminList.get(i); |
| 2499 | if (length < admin.minimumPasswordNonLetter) { |
| 2500 | length = admin.minimumPasswordNonLetter; |
| 2501 | } |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 2502 | } |
| 2503 | } |
| 2504 | return length; |
| 2505 | } |
| 2506 | } |
| 2507 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2508 | public boolean isActivePasswordSufficient(int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 2509 | if (!mHasFeature) { |
| 2510 | return true; |
| 2511 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2512 | enforceCrossUserPermission(userHandle); |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 2513 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 2514 | synchronized (this) { |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 2515 | |
| 2516 | // The active password is stored in the user that runs the launcher |
| 2517 | // If the user this is called from is part of a profile group, that is the parent |
| 2518 | // of the group. |
| 2519 | UserInfo parent = getProfileParent(userHandle); |
Benjamin Franz | fadb2b3 | 2015-01-14 18:22:42 +0000 | [diff] [blame] | 2520 | int id = (parent == null) ? userHandle : parent.id; |
| 2521 | DevicePolicyData policy = getUserDataUnchecked(id); |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 2522 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 2523 | // This API can only be called by an active device admin, |
| 2524 | // so try to retrieve it to check that the caller is one. |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 2525 | getActiveAdminForCallerLocked(null, |
| 2526 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2527 | if (policy.mActivePasswordQuality < getPasswordQuality(null, userHandle) |
| 2528 | || policy.mActivePasswordLength < getPasswordMinimumLength(null, userHandle)) { |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2529 | return false; |
| 2530 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2531 | if (policy.mActivePasswordQuality != DevicePolicyManager.PASSWORD_QUALITY_COMPLEX) { |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2532 | return true; |
| 2533 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2534 | return policy.mActivePasswordUpperCase >= getPasswordMinimumUpperCase(null, userHandle) |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 2535 | && policy.mActivePasswordLowerCase >= getPasswordMinimumLowerCase(null, userHandle) |
| 2536 | && policy.mActivePasswordLetters >= getPasswordMinimumLetters(null, userHandle) |
| 2537 | && policy.mActivePasswordNumeric >= getPasswordMinimumNumeric(null, userHandle) |
| 2538 | && policy.mActivePasswordSymbols >= getPasswordMinimumSymbols(null, userHandle) |
| 2539 | && policy.mActivePasswordNonLetter >= getPasswordMinimumNonLetter(null, userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 2540 | } |
| 2541 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 2542 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2543 | public int getCurrentFailedPasswordAttempts(int userHandle) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 2544 | synchronized (this) { |
| 2545 | // This API can only be called by an active device admin, |
| 2546 | // so try to retrieve it to check that the caller is one. |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 2547 | getActiveAdminForCallerLocked(null, |
| 2548 | DeviceAdminInfo.USES_POLICY_WATCH_LOGIN); |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 2549 | |
| 2550 | // The active password is stored in the parent. |
Benjamin Franz | fadb2b3 | 2015-01-14 18:22:42 +0000 | [diff] [blame] | 2551 | UserInfo parent = getProfileParent(userHandle); |
| 2552 | int id = (parent == null) ? userHandle : parent.id; |
| 2553 | DevicePolicyData policy = getUserDataUnchecked(id); |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 2554 | |
| 2555 | return policy.mFailedPasswordAttempts; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 2556 | } |
| 2557 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 2558 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2559 | public void setMaximumFailedPasswordsForWipe(ComponentName who, int num, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 2560 | if (!mHasFeature) { |
| 2561 | return; |
| 2562 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2563 | enforceCrossUserPermission(userHandle); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 2564 | synchronized (this) { |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 2565 | if (who == null) { |
| 2566 | throw new NullPointerException("ComponentName is null"); |
| 2567 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 2568 | // This API can only be called by an active device admin, |
| 2569 | // so try to retrieve it to check that the caller is one. |
| 2570 | getActiveAdminForCallerLocked(who, |
| 2571 | DeviceAdminInfo.USES_POLICY_WIPE_DATA); |
| 2572 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 2573 | DeviceAdminInfo.USES_POLICY_WATCH_LOGIN); |
| 2574 | if (ap.maximumFailedPasswordsForWipe != num) { |
| 2575 | ap.maximumFailedPasswordsForWipe = num; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2576 | saveSettingsLocked(userHandle); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 2577 | } |
| 2578 | } |
| 2579 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 2580 | |
Amith Yamasani | 3a3d212 | 2014-10-29 11:41:31 -0700 | [diff] [blame] | 2581 | @Override |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2582 | public int getMaximumFailedPasswordsForWipe(ComponentName who, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 2583 | if (!mHasFeature) { |
| 2584 | return 0; |
| 2585 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2586 | enforceCrossUserPermission(userHandle); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 2587 | synchronized (this) { |
Esteban Talavera | fe0f24c | 2014-08-06 16:20:56 +0100 | [diff] [blame] | 2588 | ActiveAdmin admin = (who != null) ? getActiveAdminUncheckedLocked(who, userHandle) |
| 2589 | : getAdminWithMinimumFailedPasswordsForWipeLocked(userHandle); |
| 2590 | return admin != null ? admin.maximumFailedPasswordsForWipe : 0; |
| 2591 | } |
| 2592 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 2593 | |
Amith Yamasani | 3a3d212 | 2014-10-29 11:41:31 -0700 | [diff] [blame] | 2594 | @Override |
| 2595 | public int getProfileWithMinimumFailedPasswordsForWipe(int userHandle) { |
| 2596 | if (!mHasFeature) { |
| 2597 | return UserHandle.USER_NULL; |
| 2598 | } |
| 2599 | enforceCrossUserPermission(userHandle); |
| 2600 | synchronized (this) { |
| 2601 | ActiveAdmin admin = getAdminWithMinimumFailedPasswordsForWipeLocked(userHandle); |
| 2602 | return admin != null ? admin.getUserHandle().getIdentifier() : UserHandle.USER_NULL; |
| 2603 | } |
| 2604 | } |
| 2605 | |
Esteban Talavera | fe0f24c | 2014-08-06 16:20:56 +0100 | [diff] [blame] | 2606 | /** |
| 2607 | * Returns the admin with the strictest policy on maximum failed passwords for this user and all |
| 2608 | * profiles that are visible from this user. If the policy for the primary and any other profile |
| 2609 | * are equal, it returns the admin for the primary profile. |
| 2610 | * Returns {@code null} if none of them have that policy set. |
| 2611 | */ |
| 2612 | private ActiveAdmin getAdminWithMinimumFailedPasswordsForWipeLocked(int userHandle) { |
| 2613 | int count = 0; |
| 2614 | ActiveAdmin strictestAdmin = null; |
| 2615 | for (UserInfo userInfo : mUserManager.getProfiles(userHandle)) { |
Benjamin Franz | fadb2b3 | 2015-01-14 18:22:42 +0000 | [diff] [blame] | 2616 | DevicePolicyData policy = getUserDataUnchecked(userInfo.id); |
Esteban Talavera | fe0f24c | 2014-08-06 16:20:56 +0100 | [diff] [blame] | 2617 | for (ActiveAdmin admin : policy.mAdminList) { |
| 2618 | if (admin.maximumFailedPasswordsForWipe == |
| 2619 | ActiveAdmin.DEF_MAXIMUM_FAILED_PASSWORDS_FOR_WIPE) { |
| 2620 | continue; // No max number of failed passwords policy set for this profile. |
| 2621 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 2622 | |
Esteban Talavera | fe0f24c | 2014-08-06 16:20:56 +0100 | [diff] [blame] | 2623 | // We always favor the primary profile if several profiles have the same value set. |
| 2624 | if (count == 0 || |
| 2625 | count > admin.maximumFailedPasswordsForWipe || |
| 2626 | (userInfo.isPrimary() && count >= admin.maximumFailedPasswordsForWipe)) { |
| 2627 | count = admin.maximumFailedPasswordsForWipe; |
| 2628 | strictestAdmin = admin; |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 2629 | } |
| 2630 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 2631 | } |
Esteban Talavera | fe0f24c | 2014-08-06 16:20:56 +0100 | [diff] [blame] | 2632 | return strictestAdmin; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 2633 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 2634 | |
Adrian Roos | f8f56bc | 2014-11-20 23:55:34 +0100 | [diff] [blame] | 2635 | public boolean resetPassword(String passwordOrNull, int flags, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 2636 | if (!mHasFeature) { |
| 2637 | return false; |
| 2638 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2639 | enforceCrossUserPermission(userHandle); |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 2640 | enforceNotManagedProfile(userHandle, "reset the password"); |
| 2641 | |
Adrian Roos | f8f56bc | 2014-11-20 23:55:34 +0100 | [diff] [blame] | 2642 | String password = passwordOrNull != null ? passwordOrNull : ""; |
| 2643 | |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 2644 | int quality; |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 2645 | synchronized (this) { |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 2646 | // This api can only be called by an active device admin, |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 2647 | // so try to retrieve it to check that the caller is one. |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 2648 | getActiveAdminForCallerLocked(null, |
| 2649 | DeviceAdminInfo.USES_POLICY_RESET_PASSWORD); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2650 | quality = getPasswordQuality(null, userHandle); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 2651 | if (quality != DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) { |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 2652 | int realQuality = LockPatternUtils.computePasswordQuality(password); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2653 | if (realQuality < quality |
| 2654 | && quality != DevicePolicyManager.PASSWORD_QUALITY_COMPLEX) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 2655 | Slog.w(LOG_TAG, "resetPassword: password quality 0x" |
joonyoung.cho | ad83c19 | 2013-04-18 13:51:08 +0900 | [diff] [blame] | 2656 | + Integer.toHexString(realQuality) |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 2657 | + " does not meet required quality 0x" |
| 2658 | + Integer.toHexString(quality)); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 2659 | return false; |
| 2660 | } |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2661 | quality = Math.max(realQuality, quality); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 2662 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2663 | int length = getPasswordMinimumLength(null, userHandle); |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 2664 | if (password.length() < length) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 2665 | Slog.w(LOG_TAG, "resetPassword: password length " + password.length() |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 2666 | + " does not meet required length " + length); |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 2667 | return false; |
| 2668 | } |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2669 | if (quality == DevicePolicyManager.PASSWORD_QUALITY_COMPLEX) { |
| 2670 | int letters = 0; |
| 2671 | int uppercase = 0; |
| 2672 | int lowercase = 0; |
| 2673 | int numbers = 0; |
| 2674 | int symbols = 0; |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 2675 | int nonletter = 0; |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2676 | for (int i = 0; i < password.length(); i++) { |
| 2677 | char c = password.charAt(i); |
| 2678 | if (c >= 'A' && c <= 'Z') { |
| 2679 | letters++; |
| 2680 | uppercase++; |
| 2681 | } else if (c >= 'a' && c <= 'z') { |
| 2682 | letters++; |
| 2683 | lowercase++; |
| 2684 | } else if (c >= '0' && c <= '9') { |
| 2685 | numbers++; |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 2686 | nonletter++; |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2687 | } else { |
| 2688 | symbols++; |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 2689 | nonletter++; |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2690 | } |
| 2691 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2692 | int neededLetters = getPasswordMinimumLetters(null, userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2693 | if(letters < neededLetters) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 2694 | Slog.w(LOG_TAG, "resetPassword: number of letters " + letters |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2695 | + " does not meet required number of letters " + neededLetters); |
| 2696 | return false; |
| 2697 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2698 | int neededNumbers = getPasswordMinimumNumeric(null, userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2699 | if (numbers < neededNumbers) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 2700 | Slog.w(LOG_TAG, "resetPassword: number of numerical digits " + numbers |
Dianne Hackborn | 40e9f29 | 2012-11-27 19:12:23 -0800 | [diff] [blame] | 2701 | + " does not meet required number of numerical digits " |
| 2702 | + neededNumbers); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2703 | return false; |
| 2704 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2705 | int neededLowerCase = getPasswordMinimumLowerCase(null, userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2706 | if (lowercase < neededLowerCase) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 2707 | Slog.w(LOG_TAG, "resetPassword: number of lowercase letters " + lowercase |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2708 | + " does not meet required number of lowercase letters " |
| 2709 | + neededLowerCase); |
| 2710 | return false; |
| 2711 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2712 | int neededUpperCase = getPasswordMinimumUpperCase(null, userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2713 | if (uppercase < neededUpperCase) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 2714 | Slog.w(LOG_TAG, "resetPassword: number of uppercase letters " + uppercase |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2715 | + " does not meet required number of uppercase letters " |
| 2716 | + neededUpperCase); |
| 2717 | return false; |
| 2718 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2719 | int neededSymbols = getPasswordMinimumSymbols(null, userHandle); |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2720 | if (symbols < neededSymbols) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 2721 | Slog.w(LOG_TAG, "resetPassword: number of special symbols " + symbols |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2722 | + " does not meet required number of special symbols " + neededSymbols); |
| 2723 | return false; |
| 2724 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2725 | int neededNonLetter = getPasswordMinimumNonLetter(null, userHandle); |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 2726 | if (nonletter < neededNonLetter) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 2727 | Slog.w(LOG_TAG, "resetPassword: number of non-letter characters " + nonletter |
Konstantin Lopyrev | c857740 | 2010-06-04 17:15:02 -0700 | [diff] [blame] | 2728 | + " does not meet required number of non-letter characters " |
| 2729 | + neededNonLetter); |
| 2730 | return false; |
| 2731 | } |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 2732 | } |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 2733 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 2734 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 2735 | int callingUid = Binder.getCallingUid(); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2736 | DevicePolicyData policy = getUserData(userHandle); |
| 2737 | if (policy.mPasswordOwner >= 0 && policy.mPasswordOwner != callingUid) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 2738 | Slog.w(LOG_TAG, "resetPassword: already set by another uid and not entered by user"); |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 2739 | return false; |
| 2740 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 2741 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 2742 | // Don't do this with the lock held, because it is going to call |
| 2743 | // back in to the service. |
| 2744 | long ident = Binder.clearCallingIdentity(); |
| 2745 | try { |
| 2746 | LockPatternUtils utils = new LockPatternUtils(mContext); |
Adrian Roos | f8f56bc | 2014-11-20 23:55:34 +0100 | [diff] [blame] | 2747 | if (!TextUtils.isEmpty(password)) { |
| 2748 | utils.saveLockPassword(password, quality, false, userHandle); |
| 2749 | } else { |
| 2750 | utils.clearLock(false, userHandle); |
| 2751 | } |
Adrian Roos | ef1de65 | 2014-06-25 23:29:55 +0200 | [diff] [blame] | 2752 | boolean requireEntry = (flags & DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY) != 0; |
| 2753 | if (requireEntry) { |
| 2754 | utils.requireCredentialEntry(UserHandle.USER_ALL); |
| 2755 | } |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 2756 | synchronized (this) { |
Adrian Roos | ef1de65 | 2014-06-25 23:29:55 +0200 | [diff] [blame] | 2757 | int newOwner = requireEntry ? callingUid : -1; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2758 | if (policy.mPasswordOwner != newOwner) { |
| 2759 | policy.mPasswordOwner = newOwner; |
| 2760 | saveSettingsLocked(userHandle); |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 2761 | } |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 2762 | } |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 2763 | } finally { |
| 2764 | Binder.restoreCallingIdentity(ident); |
| 2765 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 2766 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 2767 | return true; |
| 2768 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 2769 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2770 | public void setMaximumTimeToLock(ComponentName who, long timeMs, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 2771 | if (!mHasFeature) { |
| 2772 | return; |
| 2773 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2774 | enforceCrossUserPermission(userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 2775 | synchronized (this) { |
| 2776 | if (who == null) { |
| 2777 | throw new NullPointerException("ComponentName is null"); |
| 2778 | } |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 2779 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
Dianne Hackborn | 315ada7 | 2010-02-11 12:14:08 -0800 | [diff] [blame] | 2780 | DeviceAdminInfo.USES_POLICY_FORCE_LOCK); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 2781 | if (ap.maximumTimeToUnlock != timeMs) { |
| 2782 | ap.maximumTimeToUnlock = timeMs; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2783 | saveSettingsLocked(userHandle); |
| 2784 | updateMaximumTimeToLockLocked(getUserData(userHandle)); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 2785 | } |
| 2786 | } |
| 2787 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 2788 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2789 | void updateMaximumTimeToLockLocked(DevicePolicyData policy) { |
| 2790 | long timeMs = getMaximumTimeToLock(null, policy.mUserHandle); |
| 2791 | if (policy.mLastMaximumTimeToLock == timeMs) { |
Dianne Hackborn | 2fe8fb2 | 2012-06-15 17:05:25 -0700 | [diff] [blame] | 2792 | return; |
| 2793 | } |
| 2794 | |
| 2795 | long ident = Binder.clearCallingIdentity(); |
| 2796 | try { |
| 2797 | if (timeMs <= 0) { |
| 2798 | timeMs = Integer.MAX_VALUE; |
| 2799 | } else { |
| 2800 | // Make sure KEEP_SCREEN_ON is disabled, since that |
| 2801 | // would allow bypassing of the maximum time to lock. |
Christopher Tate | 62df6eb | 2012-09-07 15:00:54 -0700 | [diff] [blame] | 2802 | Settings.Global.putInt(mContext.getContentResolver(), |
| 2803 | Settings.Global.STAY_ON_WHILE_PLUGGED_IN, 0); |
Dianne Hackborn | 2fe8fb2 | 2012-06-15 17:05:25 -0700 | [diff] [blame] | 2804 | } |
| 2805 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2806 | policy.mLastMaximumTimeToLock = timeMs; |
Jeff Brown | 5ce1cb2 | 2014-11-06 19:05:33 -0800 | [diff] [blame] | 2807 | mPowerManagerInternal.setMaximumScreenOffTimeoutFromDeviceAdmin((int)timeMs); |
Dianne Hackborn | 2fe8fb2 | 2012-06-15 17:05:25 -0700 | [diff] [blame] | 2808 | } finally { |
| 2809 | Binder.restoreCallingIdentity(ident); |
| 2810 | } |
| 2811 | } |
| 2812 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2813 | public long getMaximumTimeToLock(ComponentName who, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 2814 | if (!mHasFeature) { |
| 2815 | return 0; |
| 2816 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2817 | enforceCrossUserPermission(userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 2818 | synchronized (this) { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 2819 | long time = 0; |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 2820 | |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 2821 | if (who != null) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2822 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who, userHandle); |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 2823 | return admin != null ? admin.maximumTimeToUnlock : time; |
| 2824 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 2825 | |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 2826 | // Return strictest policy for this user and profiles that are visible from this user. |
| 2827 | List<UserInfo> profiles = mUserManager.getProfiles(userHandle); |
| 2828 | for (UserInfo userInfo : profiles) { |
Benjamin Franz | fadb2b3 | 2015-01-14 18:22:42 +0000 | [diff] [blame] | 2829 | DevicePolicyData policy = getUserDataUnchecked(userInfo.id); |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 2830 | final int N = policy.mAdminList.size(); |
| 2831 | for (int i=0; i<N; i++) { |
| 2832 | ActiveAdmin admin = policy.mAdminList.get(i); |
| 2833 | if (time == 0) { |
| 2834 | time = admin.maximumTimeToUnlock; |
| 2835 | } else if (admin.maximumTimeToUnlock != 0 |
| 2836 | && time > admin.maximumTimeToUnlock) { |
| 2837 | time = admin.maximumTimeToUnlock; |
| 2838 | } |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 2839 | } |
| 2840 | } |
| 2841 | return time; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 2842 | } |
| 2843 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 2844 | |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 2845 | public void lockNow() { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 2846 | if (!mHasFeature) { |
| 2847 | return; |
| 2848 | } |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 2849 | synchronized (this) { |
| 2850 | // This API can only be called by an active device admin, |
| 2851 | // so try to retrieve it to check that the caller is one. |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 2852 | getActiveAdminForCallerLocked(null, |
| 2853 | DeviceAdminInfo.USES_POLICY_FORCE_LOCK); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2854 | lockNowUnchecked(); |
| 2855 | } |
| 2856 | } |
| 2857 | |
| 2858 | private void lockNowUnchecked() { |
| 2859 | long ident = Binder.clearCallingIdentity(); |
| 2860 | try { |
| 2861 | // Power off the display |
Jeff Brown | 5ce1cb2 | 2014-11-06 19:05:33 -0800 | [diff] [blame] | 2862 | mPowerManager.goToSleep(SystemClock.uptimeMillis(), |
Jeff Brown | 6d8fd27 | 2014-05-20 21:24:38 -0700 | [diff] [blame] | 2863 | PowerManager.GO_TO_SLEEP_REASON_DEVICE_ADMIN, 0); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2864 | // Ensure the device is locked |
Adrian Roos | ef1de65 | 2014-06-25 23:29:55 +0200 | [diff] [blame] | 2865 | new LockPatternUtils(mContext).requireCredentialEntry(UserHandle.USER_ALL); |
Adam Cohen | f752202 | 2012-10-03 20:03:18 -0700 | [diff] [blame] | 2866 | getWindowManager().lockNow(null); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 2867 | } catch (RemoteException e) { |
| 2868 | } finally { |
| 2869 | Binder.restoreCallingIdentity(ident); |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 2870 | } |
| 2871 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 2872 | |
Ben Komalo | ed48c8b | 2011-10-17 17:30:21 -0700 | [diff] [blame] | 2873 | private boolean isExtStorageEncrypted() { |
| 2874 | String state = SystemProperties.get("vold.decrypt"); |
| 2875 | return !"".equals(state); |
| 2876 | } |
| 2877 | |
Esteban Talavera | 808f6ef | 2014-08-28 17:15:54 +0100 | [diff] [blame] | 2878 | @Override |
| 2879 | public void enforceCanManageCaCerts(ComponentName who) { |
Robin Lee | 306fe08 | 2014-06-19 14:04:24 +0000 | [diff] [blame] | 2880 | if (who == null) { |
| 2881 | mContext.enforceCallingOrSelfPermission(MANAGE_CA_CERTIFICATES, null); |
| 2882 | } else { |
| 2883 | synchronized (this) { |
| 2884 | getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 2885 | } |
| 2886 | } |
Esteban Talavera | 808f6ef | 2014-08-28 17:15:54 +0100 | [diff] [blame] | 2887 | } |
| 2888 | |
| 2889 | @Override |
| 2890 | public boolean installCaCert(ComponentName admin, byte[] certBuffer) throws RemoteException { |
| 2891 | enforceCanManageCaCerts(admin); |
Robin Lee | 306fe08 | 2014-06-19 14:04:24 +0000 | [diff] [blame] | 2892 | |
Maggie Benthall | da51e68 | 2013-08-08 22:35:44 -0400 | [diff] [blame] | 2893 | byte[] pemCert; |
| 2894 | try { |
| 2895 | X509Certificate cert = parseCert(certBuffer); |
Robin Lee | 306fe08 | 2014-06-19 14:04:24 +0000 | [diff] [blame] | 2896 | pemCert = Credentials.convertToPem(cert); |
Maggie Benthall | da51e68 | 2013-08-08 22:35:44 -0400 | [diff] [blame] | 2897 | } catch (CertificateException ce) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 2898 | Log.e(LOG_TAG, "Problem converting cert", ce); |
Maggie Benthall | da51e68 | 2013-08-08 22:35:44 -0400 | [diff] [blame] | 2899 | return false; |
| 2900 | } catch (IOException ioe) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 2901 | Log.e(LOG_TAG, "Problem reading cert", ioe); |
Maggie Benthall | da51e68 | 2013-08-08 22:35:44 -0400 | [diff] [blame] | 2902 | return false; |
| 2903 | } |
Robin Lee | 306fe08 | 2014-06-19 14:04:24 +0000 | [diff] [blame] | 2904 | |
| 2905 | final UserHandle userHandle = new UserHandle(UserHandle.getCallingUserId()); |
| 2906 | final long id = Binder.clearCallingIdentity(); |
Maggie Benthall | da51e68 | 2013-08-08 22:35:44 -0400 | [diff] [blame] | 2907 | try { |
Robin Lee | 306fe08 | 2014-06-19 14:04:24 +0000 | [diff] [blame] | 2908 | final KeyChainConnection keyChainConnection = KeyChain.bindAsUser(mContext, userHandle); |
Maggie Benthall | da51e68 | 2013-08-08 22:35:44 -0400 | [diff] [blame] | 2909 | try { |
| 2910 | keyChainConnection.getService().installCaCertificate(pemCert); |
| 2911 | return true; |
Robin Lee | 306fe08 | 2014-06-19 14:04:24 +0000 | [diff] [blame] | 2912 | } catch (RemoteException e) { |
| 2913 | Log.e(LOG_TAG, "installCaCertsToKeyChain(): ", e); |
Maggie Benthall | da51e68 | 2013-08-08 22:35:44 -0400 | [diff] [blame] | 2914 | } finally { |
Robin Lee | 306fe08 | 2014-06-19 14:04:24 +0000 | [diff] [blame] | 2915 | keyChainConnection.close(); |
Maggie Benthall | da51e68 | 2013-08-08 22:35:44 -0400 | [diff] [blame] | 2916 | } |
| 2917 | } catch (InterruptedException e1) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 2918 | Log.w(LOG_TAG, "installCaCertsToKeyChain(): ", e1); |
Maggie Benthall | da51e68 | 2013-08-08 22:35:44 -0400 | [diff] [blame] | 2919 | Thread.currentThread().interrupt(); |
Robin Lee | 306fe08 | 2014-06-19 14:04:24 +0000 | [diff] [blame] | 2920 | } finally { |
| 2921 | Binder.restoreCallingIdentity(id); |
Maggie Benthall | da51e68 | 2013-08-08 22:35:44 -0400 | [diff] [blame] | 2922 | } |
| 2923 | return false; |
| 2924 | } |
| 2925 | |
Esteban Talavera | 808f6ef | 2014-08-28 17:15:54 +0100 | [diff] [blame] | 2926 | private static X509Certificate parseCert(byte[] certBuffer) throws CertificateException { |
Maggie Benthall | da51e68 | 2013-08-08 22:35:44 -0400 | [diff] [blame] | 2927 | CertificateFactory certFactory = CertificateFactory.getInstance("X.509"); |
| 2928 | return (X509Certificate) certFactory.generateCertificate(new ByteArrayInputStream( |
| 2929 | certBuffer)); |
| 2930 | } |
| 2931 | |
Esteban Talavera | 808f6ef | 2014-08-28 17:15:54 +0100 | [diff] [blame] | 2932 | @Override |
| 2933 | public void uninstallCaCert(ComponentName admin, String alias) { |
| 2934 | enforceCanManageCaCerts(admin); |
Robin Lee | 306fe08 | 2014-06-19 14:04:24 +0000 | [diff] [blame] | 2935 | |
| 2936 | final UserHandle userHandle = new UserHandle(UserHandle.getCallingUserId()); |
| 2937 | final long id = Binder.clearCallingIdentity(); |
Maggie Benthall | da51e68 | 2013-08-08 22:35:44 -0400 | [diff] [blame] | 2938 | try { |
Robin Lee | 306fe08 | 2014-06-19 14:04:24 +0000 | [diff] [blame] | 2939 | final KeyChainConnection keyChainConnection = KeyChain.bindAsUser(mContext, userHandle); |
Maggie Benthall | da51e68 | 2013-08-08 22:35:44 -0400 | [diff] [blame] | 2940 | try { |
Robin Lee | 306fe08 | 2014-06-19 14:04:24 +0000 | [diff] [blame] | 2941 | keyChainConnection.getService().deleteCaCertificate(alias); |
Maggie Benthall | da51e68 | 2013-08-08 22:35:44 -0400 | [diff] [blame] | 2942 | } catch (RemoteException e) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 2943 | Log.e(LOG_TAG, "from CaCertUninstaller: ", e); |
Maggie Benthall | da51e68 | 2013-08-08 22:35:44 -0400 | [diff] [blame] | 2944 | } finally { |
| 2945 | keyChainConnection.close(); |
Maggie Benthall | da51e68 | 2013-08-08 22:35:44 -0400 | [diff] [blame] | 2946 | } |
| 2947 | } catch (InterruptedException ie) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 2948 | Log.w(LOG_TAG, "CaCertUninstaller: ", ie); |
Maggie Benthall | da51e68 | 2013-08-08 22:35:44 -0400 | [diff] [blame] | 2949 | Thread.currentThread().interrupt(); |
Robin Lee | 306fe08 | 2014-06-19 14:04:24 +0000 | [diff] [blame] | 2950 | } finally { |
| 2951 | Binder.restoreCallingIdentity(id); |
Maggie Benthall | da51e68 | 2013-08-08 22:35:44 -0400 | [diff] [blame] | 2952 | } |
| 2953 | } |
| 2954 | |
Bernhard Bauer | 26408cc | 2014-09-08 14:07:31 +0100 | [diff] [blame] | 2955 | @Override |
| 2956 | public boolean installKeyPair(ComponentName who, byte[] privKey, byte[] cert, String alias) { |
| 2957 | if (who == null) { |
| 2958 | throw new NullPointerException("ComponentName is null"); |
| 2959 | } |
| 2960 | synchronized (this) { |
| 2961 | getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 2962 | } |
| 2963 | final UserHandle userHandle = new UserHandle(UserHandle.getCallingUserId()); |
| 2964 | final long id = Binder.clearCallingIdentity(); |
| 2965 | try { |
| 2966 | final KeyChainConnection keyChainConnection = KeyChain.bindAsUser(mContext, userHandle); |
| 2967 | try { |
| 2968 | IKeyChainService keyChain = keyChainConnection.getService(); |
| 2969 | return keyChain.installKeyPair(privKey, cert, alias); |
| 2970 | } catch (RemoteException e) { |
| 2971 | Log.e(LOG_TAG, "Installing certificate", e); |
| 2972 | } finally { |
| 2973 | keyChainConnection.close(); |
| 2974 | } |
| 2975 | } catch (InterruptedException e) { |
| 2976 | Log.w(LOG_TAG, "Interrupted while installing certificate", e); |
| 2977 | Thread.currentThread().interrupt(); |
| 2978 | } finally { |
| 2979 | Binder.restoreCallingIdentity(id); |
| 2980 | } |
| 2981 | return false; |
| 2982 | } |
| 2983 | |
Suprabh Shukla | 8900468 | 2017-01-30 20:52:04 -0800 | [diff] [blame] | 2984 | private void wipeDataNoLock(boolean wipeExtRequested, String reason) { |
Ben Komalo | ed48c8b | 2011-10-17 17:30:21 -0700 | [diff] [blame] | 2985 | // If the SD card is encrypted and non-removable, we have to force a wipe. |
| 2986 | boolean forceExtWipe = !Environment.isExternalStorageRemovable() && isExtStorageEncrypted(); |
Ben Komalo | ed48c8b | 2011-10-17 17:30:21 -0700 | [diff] [blame] | 2987 | |
| 2988 | // Note: we can only do the wipe via ExternalStorageFormatter if the volume is not emulated. |
| 2989 | if ((forceExtWipe || wipeExtRequested) && !Environment.isExternalStorageEmulated()) { |
Dianne Hackborn | 4249917 | 2010-10-15 18:45:07 -0700 | [diff] [blame] | 2990 | Intent intent = new Intent(ExternalStorageFormatter.FORMAT_AND_FACTORY_RESET); |
Masanori Ogino | f535cb0 | 2012-02-15 19:25:50 +0900 | [diff] [blame] | 2991 | intent.putExtra(ExternalStorageFormatter.EXTRA_ALWAYS_RESET, true); |
Jeff Sharkey | 004a4b2 | 2014-09-24 11:45:24 -0700 | [diff] [blame] | 2992 | intent.putExtra(Intent.EXTRA_REASON, reason); |
Dianne Hackborn | 4249917 | 2010-10-15 18:45:07 -0700 | [diff] [blame] | 2993 | intent.setComponent(ExternalStorageFormatter.COMPONENT_NAME); |
| 2994 | mWakeLock.acquire(10000); |
| 2995 | mContext.startService(intent); |
| 2996 | } else { |
| 2997 | try { |
Jeff Sharkey | 004a4b2 | 2014-09-24 11:45:24 -0700 | [diff] [blame] | 2998 | RecoverySystem.rebootWipeUserData(mContext, reason); |
Paul Crowley | a7e87ac | 2014-11-18 13:50:19 +0000 | [diff] [blame] | 2999 | } catch (IOException | SecurityException e) { |
Julia Reynolds | fe05380 | 2014-06-30 11:41:32 -0400 | [diff] [blame] | 3000 | Slog.w(LOG_TAG, "Failed requesting data wipe", e); |
Dianne Hackborn | 4249917 | 2010-10-15 18:45:07 -0700 | [diff] [blame] | 3001 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3002 | } |
| 3003 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 3004 | |
Jeff Sharkey | 004a4b2 | 2014-09-24 11:45:24 -0700 | [diff] [blame] | 3005 | @Override |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3006 | public void wipeData(int flags, final int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 3007 | if (!mHasFeature) { |
| 3008 | return; |
| 3009 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3010 | enforceCrossUserPermission(userHandle); |
Suprabh Shukla | 8900468 | 2017-01-30 20:52:04 -0800 | [diff] [blame] | 3011 | final String source; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 3012 | synchronized (this) { |
| 3013 | // This API can only be called by an active device admin, |
| 3014 | // so try to retrieve it to check that the caller is one. |
Jeff Sharkey | 004a4b2 | 2014-09-24 11:45:24 -0700 | [diff] [blame] | 3015 | final ActiveAdmin admin = getActiveAdminForCallerLocked(null, |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 3016 | DeviceAdminInfo.USES_POLICY_WIPE_DATA); |
Jeff Sharkey | 004a4b2 | 2014-09-24 11:45:24 -0700 | [diff] [blame] | 3017 | |
Paul Crowley | a7e87ac | 2014-11-18 13:50:19 +0000 | [diff] [blame] | 3018 | final ComponentName cname = admin.info.getComponent(); |
| 3019 | if (cname != null) { |
| 3020 | source = cname.flattenToShortString(); |
Jeff Sharkey | 004a4b2 | 2014-09-24 11:45:24 -0700 | [diff] [blame] | 3021 | } else { |
Paul Crowley | a7e87ac | 2014-11-18 13:50:19 +0000 | [diff] [blame] | 3022 | source = admin.info.getPackageName(); |
Jeff Sharkey | 004a4b2 | 2014-09-24 11:45:24 -0700 | [diff] [blame] | 3023 | } |
| 3024 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3025 | long ident = Binder.clearCallingIdentity(); |
| 3026 | try { |
Paul Crowley | a7e87ac | 2014-11-18 13:50:19 +0000 | [diff] [blame] | 3027 | if ((flags & WIPE_RESET_PROTECTION_DATA) != 0) { |
| 3028 | if (userHandle != UserHandle.USER_OWNER |
| 3029 | || !isDeviceOwner(admin.info.getPackageName())) { |
| 3030 | throw new SecurityException( |
| 3031 | "Only device owner admins can set WIPE_RESET_PROTECTION_DATA"); |
| 3032 | } |
| 3033 | PersistentDataBlockManager manager = (PersistentDataBlockManager) |
| 3034 | mContext.getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE); |
Paul Crowley | 4c939fd | 2015-03-25 11:43:42 +0000 | [diff] [blame] | 3035 | if (manager != null) { |
| 3036 | manager.wipe(); |
| 3037 | } |
Paul Crowley | a7e87ac | 2014-11-18 13:50:19 +0000 | [diff] [blame] | 3038 | } |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3039 | } finally { |
| 3040 | Binder.restoreCallingIdentity(ident); |
| 3041 | } |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 3042 | } |
Suprabh Shukla | 8900468 | 2017-01-30 20:52:04 -0800 | [diff] [blame] | 3043 | boolean wipeExtRequested = (flags & WIPE_EXTERNAL_STORAGE) != 0; |
| 3044 | wipeDeviceNoLock(wipeExtRequested, userHandle, |
| 3045 | "DevicePolicyManager.wipeData() from " + source); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3046 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 3047 | |
Suprabh Shukla | 8900468 | 2017-01-30 20:52:04 -0800 | [diff] [blame] | 3048 | private void wipeDeviceNoLock(boolean wipeExtRequested, final int userHandle, String reason) { |
| 3049 | final long iden = Binder.clearCallingIdentity(); |
| 3050 | try { |
| 3051 | if (userHandle == UserHandle.USER_OWNER) { |
| 3052 | wipeDataNoLock(wipeExtRequested, reason); |
| 3053 | } else { |
| 3054 | mHandler.post(new Runnable() { |
| 3055 | public void run() { |
| 3056 | try { |
| 3057 | IActivityManager am = ActivityManagerNative.getDefault(); |
| 3058 | if (am.getCurrentUser().id == userHandle) { |
| 3059 | am.switchUser(UserHandle.USER_OWNER); |
| 3060 | } |
| 3061 | if (!mUserManager.removeUser(userHandle)) { |
| 3062 | Slog.w(LOG_TAG, "Couldn't remove user " + userHandle); |
| 3063 | } |
| 3064 | } catch (RemoteException re) { |
| 3065 | // Shouldn't happen |
Kenny Guy | e9acb59 | 2015-02-04 17:55:42 +0000 | [diff] [blame] | 3066 | } |
Amith Yamasani | 32f0742 | 2012-11-16 15:09:13 -0800 | [diff] [blame] | 3067 | } |
Suprabh Shukla | 8900468 | 2017-01-30 20:52:04 -0800 | [diff] [blame] | 3068 | }); |
| 3069 | } |
| 3070 | } finally { |
| 3071 | Binder.restoreCallingIdentity(iden); |
Amith Yamasani | 32f0742 | 2012-11-16 15:09:13 -0800 | [diff] [blame] | 3072 | } |
| 3073 | } |
| 3074 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3075 | public void getRemoveWarning(ComponentName comp, final RemoteCallback result, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 3076 | if (!mHasFeature) { |
| 3077 | return; |
| 3078 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3079 | enforceCrossUserPermission(userHandle); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3080 | mContext.enforceCallingOrSelfPermission( |
| 3081 | android.Manifest.permission.BIND_DEVICE_ADMIN, null); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 3082 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3083 | synchronized (this) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3084 | ActiveAdmin admin = getActiveAdminUncheckedLocked(comp, userHandle); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3085 | if (admin == null) { |
| 3086 | try { |
| 3087 | result.sendResult(null); |
| 3088 | } catch (RemoteException e) { |
| 3089 | } |
| 3090 | return; |
| 3091 | } |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 3092 | Intent intent = new Intent(DeviceAdminReceiver.ACTION_DEVICE_ADMIN_DISABLE_REQUESTED); |
Dianne Hackborn | 8ddeee9 | 2014-07-22 16:12:59 -0700 | [diff] [blame] | 3093 | intent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3094 | intent.setComponent(admin.info.getComponent()); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3095 | mContext.sendOrderedBroadcastAsUser(intent, new UserHandle(userHandle), |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 3096 | null, new BroadcastReceiver() { |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3097 | @Override |
| 3098 | public void onReceive(Context context, Intent intent) { |
| 3099 | try { |
| 3100 | result.sendResult(getResultExtras(false)); |
| 3101 | } catch (RemoteException e) { |
| 3102 | } |
| 3103 | } |
| 3104 | }, null, Activity.RESULT_OK, null, null); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 3105 | } |
| 3106 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 3107 | |
Konstantin Lopyrev | a15dcfa | 2010-05-24 17:10:56 -0700 | [diff] [blame] | 3108 | public void setActivePasswordState(int quality, int length, int letters, int uppercase, |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3109 | int lowercase, int numbers, int symbols, int nonletter, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 3110 | if (!mHasFeature) { |
| 3111 | return; |
| 3112 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3113 | enforceCrossUserPermission(userHandle); |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 3114 | enforceNotManagedProfile(userHandle, "set the active password"); |
| 3115 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 3116 | mContext.enforceCallingOrSelfPermission( |
| 3117 | android.Manifest.permission.BIND_DEVICE_ADMIN, null); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3118 | DevicePolicyData p = getUserData(userHandle); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 3119 | |
Dianne Hackborn | 85f2c9c | 2010-03-22 11:12:48 -0700 | [diff] [blame] | 3120 | validateQualityConstant(quality); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 3121 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 3122 | synchronized (this) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3123 | if (p.mActivePasswordQuality != quality || p.mActivePasswordLength != length |
| 3124 | || p.mFailedPasswordAttempts != 0 || p.mActivePasswordLetters != letters |
| 3125 | || p.mActivePasswordUpperCase != uppercase |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 3126 | || p.mActivePasswordLowerCase != lowercase |
| 3127 | || p.mActivePasswordNumeric != numbers |
| 3128 | || p.mActivePasswordSymbols != symbols |
| 3129 | || p.mActivePasswordNonLetter != nonletter) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 3130 | long ident = Binder.clearCallingIdentity(); |
| 3131 | try { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3132 | p.mActivePasswordQuality = quality; |
| 3133 | p.mActivePasswordLength = length; |
| 3134 | p.mActivePasswordLetters = letters; |
| 3135 | p.mActivePasswordLowerCase = lowercase; |
| 3136 | p.mActivePasswordUpperCase = uppercase; |
| 3137 | p.mActivePasswordNumeric = numbers; |
| 3138 | p.mActivePasswordSymbols = symbols; |
| 3139 | p.mActivePasswordNonLetter = nonletter; |
| 3140 | p.mFailedPasswordAttempts = 0; |
| 3141 | saveSettingsLocked(userHandle); |
| 3142 | updatePasswordExpirationsLocked(userHandle); |
| 3143 | setExpirationAlarmCheckLocked(mContext, p); |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 3144 | sendAdminCommandToSelfAndProfilesLocked( |
| 3145 | DeviceAdminReceiver.ACTION_PASSWORD_CHANGED, |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3146 | DeviceAdminInfo.USES_POLICY_LIMIT_PASSWORD, userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 3147 | } finally { |
| 3148 | Binder.restoreCallingIdentity(ident); |
| 3149 | } |
| 3150 | } |
| 3151 | } |
| 3152 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 3153 | |
Andy Stadler | 043116a | 2010-11-29 17:43:32 -0800 | [diff] [blame] | 3154 | /** |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 3155 | * Called any time the device password is updated. Resets all password expiration clocks. |
Andy Stadler | 043116a | 2010-11-29 17:43:32 -0800 | [diff] [blame] | 3156 | */ |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3157 | private void updatePasswordExpirationsLocked(int userHandle) { |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 3158 | List<UserInfo> profiles = mUserManager.getProfiles(userHandle); |
| 3159 | for (UserInfo userInfo : profiles) { |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 3160 | int profileId = userInfo.id; |
Benjamin Franz | fadb2b3 | 2015-01-14 18:22:42 +0000 | [diff] [blame] | 3161 | DevicePolicyData policy = getUserDataUnchecked(profileId); |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 3162 | final int N = policy.mAdminList.size(); |
| 3163 | if (N > 0) { |
| 3164 | for (int i=0; i<N; i++) { |
| 3165 | ActiveAdmin admin = policy.mAdminList.get(i); |
| 3166 | if (admin.info.usesPolicy(DeviceAdminInfo.USES_POLICY_EXPIRE_PASSWORD)) { |
| 3167 | long timeout = admin.passwordExpirationTimeout; |
| 3168 | long expiration = timeout > 0L ? (timeout + System.currentTimeMillis()) : 0L; |
| 3169 | admin.passwordExpirationDate = expiration; |
| 3170 | } |
| 3171 | } |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 3172 | } |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 3173 | saveSettingsLocked(profileId); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 3174 | } |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 3175 | } |
| 3176 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3177 | public void reportFailedPasswordAttempt(int userHandle) { |
| 3178 | enforceCrossUserPermission(userHandle); |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 3179 | enforceNotManagedProfile(userHandle, "report failed password attempt"); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 3180 | mContext.enforceCallingOrSelfPermission( |
| 3181 | android.Manifest.permission.BIND_DEVICE_ADMIN, null); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 3182 | |
Craig Mautner | 66eb521 | 2014-09-04 12:56:39 -0700 | [diff] [blame] | 3183 | long ident = Binder.clearCallingIdentity(); |
| 3184 | try { |
| 3185 | boolean wipeData = false; |
| 3186 | int identifier = 0; |
| 3187 | synchronized (this) { |
| 3188 | DevicePolicyData policy = getUserData(userHandle); |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3189 | policy.mFailedPasswordAttempts++; |
| 3190 | saveSettingsLocked(userHandle); |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 3191 | if (mHasFeature) { |
Esteban Talavera | fe0f24c | 2014-08-06 16:20:56 +0100 | [diff] [blame] | 3192 | ActiveAdmin strictestAdmin = |
| 3193 | getAdminWithMinimumFailedPasswordsForWipeLocked(userHandle); |
Adrian Roos | b8b4774 | 2014-08-07 15:55:18 +0200 | [diff] [blame] | 3194 | int max = strictestAdmin != null |
| 3195 | ? strictestAdmin.maximumFailedPasswordsForWipe : 0; |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 3196 | if (max > 0 && policy.mFailedPasswordAttempts >= max) { |
Esteban Talavera | fe0f24c | 2014-08-06 16:20:56 +0100 | [diff] [blame] | 3197 | // Wipe the user/profile associated with the policy that was violated. This |
| 3198 | // is not necessarily calling user: if the policy that fired was from a |
| 3199 | // managed profile rather than the main user profile, we wipe former only. |
Craig Mautner | 66eb521 | 2014-09-04 12:56:39 -0700 | [diff] [blame] | 3200 | wipeData = true; |
| 3201 | identifier = strictestAdmin.getUserHandle().getIdentifier(); |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 3202 | } |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 3203 | sendAdminCommandToSelfAndProfilesLocked( |
| 3204 | DeviceAdminReceiver.ACTION_PASSWORD_FAILED, |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 3205 | DeviceAdminInfo.USES_POLICY_WATCH_LOGIN, userHandle); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 3206 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 3207 | } |
Craig Mautner | 66eb521 | 2014-09-04 12:56:39 -0700 | [diff] [blame] | 3208 | if (wipeData) { |
| 3209 | // Call without holding lock. |
Suprabh Shukla | 8900468 | 2017-01-30 20:52:04 -0800 | [diff] [blame] | 3210 | wipeDeviceNoLock(false, identifier, |
Paul Crowley | a7e87ac | 2014-11-18 13:50:19 +0000 | [diff] [blame] | 3211 | "reportFailedPasswordAttempt()"); |
Craig Mautner | 66eb521 | 2014-09-04 12:56:39 -0700 | [diff] [blame] | 3212 | } |
| 3213 | } finally { |
| 3214 | Binder.restoreCallingIdentity(ident); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 3215 | } |
| 3216 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 3217 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3218 | public void reportSuccessfulPasswordAttempt(int userHandle) { |
| 3219 | enforceCrossUserPermission(userHandle); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 3220 | mContext.enforceCallingOrSelfPermission( |
| 3221 | android.Manifest.permission.BIND_DEVICE_ADMIN, null); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 3222 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 3223 | synchronized (this) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3224 | DevicePolicyData policy = getUserData(userHandle); |
| 3225 | if (policy.mFailedPasswordAttempts != 0 || policy.mPasswordOwner >= 0) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 3226 | long ident = Binder.clearCallingIdentity(); |
| 3227 | try { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3228 | policy.mFailedPasswordAttempts = 0; |
| 3229 | policy.mPasswordOwner = -1; |
| 3230 | saveSettingsLocked(userHandle); |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 3231 | if (mHasFeature) { |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 3232 | sendAdminCommandToSelfAndProfilesLocked( |
| 3233 | DeviceAdminReceiver.ACTION_PASSWORD_SUCCEEDED, |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 3234 | DeviceAdminInfo.USES_POLICY_WATCH_LOGIN, userHandle); |
| 3235 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 3236 | } finally { |
| 3237 | Binder.restoreCallingIdentity(ident); |
| 3238 | } |
| 3239 | } |
| 3240 | } |
| 3241 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 3242 | |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 3243 | public ComponentName setGlobalProxy(ComponentName who, String proxySpec, |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3244 | String exclusionList, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 3245 | if (!mHasFeature) { |
| 3246 | return null; |
| 3247 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3248 | enforceCrossUserPermission(userHandle); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 3249 | synchronized(this) { |
| 3250 | if (who == null) { |
| 3251 | throw new NullPointerException("ComponentName is null"); |
| 3252 | } |
| 3253 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3254 | // Only check if owner has set global proxy. We don't allow other users to set it. |
| 3255 | DevicePolicyData policy = getUserData(UserHandle.USER_OWNER); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 3256 | ActiveAdmin admin = getActiveAdminForCallerLocked(who, |
| 3257 | DeviceAdminInfo.USES_POLICY_SETS_GLOBAL_PROXY); |
| 3258 | |
| 3259 | // Scan through active admins and find if anyone has already |
| 3260 | // set the global proxy. |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3261 | Set<ComponentName> compSet = policy.mAdminMap.keySet(); |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 3262 | for (ComponentName component : compSet) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3263 | ActiveAdmin ap = policy.mAdminMap.get(component); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 3264 | if ((ap.specifiesGlobalProxy) && (!component.equals(who))) { |
| 3265 | // Another admin already sets the global proxy |
| 3266 | // Return it to the caller. |
| 3267 | return component; |
| 3268 | } |
| 3269 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3270 | |
| 3271 | // If the user is not the owner, don't set the global proxy. Fail silently. |
| 3272 | if (UserHandle.getCallingUserId() != UserHandle.USER_OWNER) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 3273 | Slog.w(LOG_TAG, "Only the owner is allowed to set the global proxy. User " |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3274 | + userHandle + " is not permitted."); |
| 3275 | return null; |
| 3276 | } |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 3277 | if (proxySpec == null) { |
| 3278 | admin.specifiesGlobalProxy = false; |
| 3279 | admin.globalProxySpec = null; |
| 3280 | admin.globalProxyExclusionList = null; |
| 3281 | } else { |
| 3282 | |
| 3283 | admin.specifiesGlobalProxy = true; |
| 3284 | admin.globalProxySpec = proxySpec; |
| 3285 | admin.globalProxyExclusionList = exclusionList; |
| 3286 | } |
| 3287 | |
| 3288 | // Reset the global proxy accordingly |
| 3289 | // Do this using system permissions, as apps cannot write to secure settings |
| 3290 | long origId = Binder.clearCallingIdentity(); |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 3291 | try { |
| 3292 | resetGlobalProxyLocked(policy); |
| 3293 | } finally { |
| 3294 | Binder.restoreCallingIdentity(origId); |
| 3295 | } |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 3296 | return null; |
| 3297 | } |
| 3298 | } |
| 3299 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3300 | public ComponentName getGlobalProxyAdmin(int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 3301 | if (!mHasFeature) { |
| 3302 | return null; |
| 3303 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3304 | enforceCrossUserPermission(userHandle); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 3305 | synchronized(this) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3306 | DevicePolicyData policy = getUserData(UserHandle.USER_OWNER); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 3307 | // Scan through active admins and find if anyone has already |
| 3308 | // set the global proxy. |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3309 | final int N = policy.mAdminList.size(); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 3310 | for (int i = 0; i < N; i++) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3311 | ActiveAdmin ap = policy.mAdminList.get(i); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 3312 | if (ap.specifiesGlobalProxy) { |
| 3313 | // Device admin sets the global proxy |
| 3314 | // Return it to the caller. |
| 3315 | return ap.info.getComponent(); |
| 3316 | } |
| 3317 | } |
| 3318 | } |
| 3319 | // No device admin sets the global proxy. |
| 3320 | return null; |
| 3321 | } |
| 3322 | |
Jason Monk | 03bc991 | 2014-05-13 09:44:57 -0400 | [diff] [blame] | 3323 | public void setRecommendedGlobalProxy(ComponentName who, ProxyInfo proxyInfo) { |
| 3324 | synchronized (this) { |
| 3325 | getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_DEVICE_OWNER); |
| 3326 | } |
| 3327 | long token = Binder.clearCallingIdentity(); |
| 3328 | try { |
| 3329 | ConnectivityManager connectivityManager = (ConnectivityManager) |
| 3330 | mContext.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 3331 | connectivityManager.setGlobalProxy(proxyInfo); |
| 3332 | } finally { |
| 3333 | Binder.restoreCallingIdentity(token); |
| 3334 | } |
| 3335 | } |
| 3336 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3337 | private void resetGlobalProxyLocked(DevicePolicyData policy) { |
| 3338 | final int N = policy.mAdminList.size(); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 3339 | for (int i = 0; i < N; i++) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3340 | ActiveAdmin ap = policy.mAdminList.get(i); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 3341 | if (ap.specifiesGlobalProxy) { |
Dianne Hackborn | 2fe8fb2 | 2012-06-15 17:05:25 -0700 | [diff] [blame] | 3342 | saveGlobalProxyLocked(ap.globalProxySpec, ap.globalProxyExclusionList); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 3343 | return; |
| 3344 | } |
| 3345 | } |
| 3346 | // No device admins defining global proxies - reset global proxy settings to none |
Dianne Hackborn | 2fe8fb2 | 2012-06-15 17:05:25 -0700 | [diff] [blame] | 3347 | saveGlobalProxyLocked(null, null); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 3348 | } |
| 3349 | |
Dianne Hackborn | 2fe8fb2 | 2012-06-15 17:05:25 -0700 | [diff] [blame] | 3350 | private void saveGlobalProxyLocked(String proxySpec, String exclusionList) { |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 3351 | if (exclusionList == null) { |
| 3352 | exclusionList = ""; |
| 3353 | } |
| 3354 | if (proxySpec == null) { |
| 3355 | proxySpec = ""; |
| 3356 | } |
| 3357 | // Remove white spaces |
| 3358 | proxySpec = proxySpec.trim(); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3359 | String data[] = proxySpec.split(":"); |
| 3360 | int proxyPort = 8080; |
| 3361 | if (data.length > 1) { |
| 3362 | try { |
| 3363 | proxyPort = Integer.parseInt(data[1]); |
| 3364 | } catch (NumberFormatException e) {} |
| 3365 | } |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 3366 | exclusionList = exclusionList.trim(); |
| 3367 | ContentResolver res = mContext.getContentResolver(); |
Raj Mamadgi | 92d0249 | 2013-11-11 13:52:58 -0800 | [diff] [blame] | 3368 | |
Jason Monk | 54a9ebb | 2014-05-07 15:26:22 -0400 | [diff] [blame] | 3369 | ProxyInfo proxyProperties = new ProxyInfo(data[0], proxyPort, exclusionList); |
Raj Mamadgi | 92d0249 | 2013-11-11 13:52:58 -0800 | [diff] [blame] | 3370 | if (!proxyProperties.isValid()) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 3371 | Slog.e(LOG_TAG, "Invalid proxy properties, ignoring: " + proxyProperties.toString()); |
Raj Mamadgi | 92d0249 | 2013-11-11 13:52:58 -0800 | [diff] [blame] | 3372 | return; |
| 3373 | } |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 3374 | Settings.Global.putString(res, Settings.Global.GLOBAL_HTTP_PROXY_HOST, data[0]); |
| 3375 | Settings.Global.putInt(res, Settings.Global.GLOBAL_HTTP_PROXY_PORT, proxyPort); |
| 3376 | Settings.Global.putString(res, Settings.Global.GLOBAL_HTTP_PROXY_EXCLUSION_LIST, |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3377 | exclusionList); |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 3378 | } |
| 3379 | |
Andy Stadler | 7b0f8f0 | 2011-01-12 14:59:52 -0800 | [diff] [blame] | 3380 | /** |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 3381 | * Set the storage encryption request for a single admin. Returns the new total request |
| 3382 | * status (for all admins). |
Andy Stadler | 7b0f8f0 | 2011-01-12 14:59:52 -0800 | [diff] [blame] | 3383 | */ |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3384 | public int setStorageEncryption(ComponentName who, boolean encrypt, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 3385 | if (!mHasFeature) { |
| 3386 | return DevicePolicyManager.ENCRYPTION_STATUS_UNSUPPORTED; |
| 3387 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3388 | enforceCrossUserPermission(userHandle); |
Andy Stadler | 7b0f8f0 | 2011-01-12 14:59:52 -0800 | [diff] [blame] | 3389 | synchronized (this) { |
| 3390 | // Check for permissions |
| 3391 | if (who == null) { |
| 3392 | throw new NullPointerException("ComponentName is null"); |
| 3393 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3394 | // Only owner can set storage encryption |
| 3395 | if (userHandle != UserHandle.USER_OWNER |
| 3396 | || UserHandle.getCallingUserId() != UserHandle.USER_OWNER) { |
Amith Yamasani | ef38a10 | 2013-12-13 09:05:26 -0800 | [diff] [blame] | 3397 | Slog.w(LOG_TAG, "Only owner is allowed to set storage encryption. User " |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3398 | + UserHandle.getCallingUserId() + " is not permitted."); |
| 3399 | return 0; |
| 3400 | } |
| 3401 | |
Andy Stadler | 7b0f8f0 | 2011-01-12 14:59:52 -0800 | [diff] [blame] | 3402 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 3403 | DeviceAdminInfo.USES_ENCRYPTED_STORAGE); |
| 3404 | |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 3405 | // Quick exit: If the filesystem does not support encryption, we can exit early. |
| 3406 | if (!isEncryptionSupported()) { |
| 3407 | return DevicePolicyManager.ENCRYPTION_STATUS_UNSUPPORTED; |
| 3408 | } |
| 3409 | |
| 3410 | // (1) Record the value for the admin so it's sticky |
| 3411 | if (ap.encryptionRequested != encrypt) { |
| 3412 | ap.encryptionRequested = encrypt; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3413 | saveSettingsLocked(userHandle); |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 3414 | } |
| 3415 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3416 | DevicePolicyData policy = getUserData(UserHandle.USER_OWNER); |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 3417 | // (2) Compute "max" for all admins |
| 3418 | boolean newRequested = false; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3419 | final int N = policy.mAdminList.size(); |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 3420 | for (int i = 0; i < N; i++) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3421 | newRequested |= policy.mAdminList.get(i).encryptionRequested; |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 3422 | } |
| 3423 | |
| 3424 | // Notify OS of new request |
| 3425 | setEncryptionRequested(newRequested); |
| 3426 | |
| 3427 | // Return the new global request status |
| 3428 | return newRequested |
| 3429 | ? DevicePolicyManager.ENCRYPTION_STATUS_ACTIVE |
| 3430 | : DevicePolicyManager.ENCRYPTION_STATUS_INACTIVE; |
Andy Stadler | 7b0f8f0 | 2011-01-12 14:59:52 -0800 | [diff] [blame] | 3431 | } |
| 3432 | } |
| 3433 | |
| 3434 | /** |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 3435 | * Get the current storage encryption request status for a given admin, or aggregate of all |
| 3436 | * active admins. |
Andy Stadler | 7b0f8f0 | 2011-01-12 14:59:52 -0800 | [diff] [blame] | 3437 | */ |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3438 | public boolean getStorageEncryption(ComponentName who, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 3439 | if (!mHasFeature) { |
| 3440 | return false; |
| 3441 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3442 | enforceCrossUserPermission(userHandle); |
Andy Stadler | 7b0f8f0 | 2011-01-12 14:59:52 -0800 | [diff] [blame] | 3443 | synchronized (this) { |
| 3444 | // Check for permissions if a particular caller is specified |
| 3445 | if (who != null) { |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 3446 | // 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] | 3447 | ActiveAdmin ap = getActiveAdminUncheckedLocked(who, userHandle); |
Andy Stadler | c994d69 | 2011-06-01 15:30:54 -0700 | [diff] [blame] | 3448 | return ap != null ? ap.encryptionRequested : false; |
Andy Stadler | 7b0f8f0 | 2011-01-12 14:59:52 -0800 | [diff] [blame] | 3449 | } |
| 3450 | |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 3451 | // If no particular caller is specified, return the aggregate set of requests. |
| 3452 | // This is short circuited by returning true on the first hit. |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3453 | DevicePolicyData policy = getUserData(userHandle); |
| 3454 | final int N = policy.mAdminList.size(); |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 3455 | for (int i = 0; i < N; i++) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3456 | if (policy.mAdminList.get(i).encryptionRequested) { |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 3457 | return true; |
| 3458 | } |
| 3459 | } |
| 3460 | return false; |
Andy Stadler | 7b0f8f0 | 2011-01-12 14:59:52 -0800 | [diff] [blame] | 3461 | } |
| 3462 | } |
| 3463 | |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 3464 | /** |
| 3465 | * Get the current encryption status of the device. |
| 3466 | */ |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3467 | public int getStorageEncryptionStatus(int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 3468 | if (!mHasFeature) { |
| 3469 | // Ok to return current status. |
| 3470 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3471 | enforceCrossUserPermission(userHandle); |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 3472 | return getEncryptionStatus(); |
| 3473 | } |
| 3474 | |
| 3475 | /** |
| 3476 | * Hook to low-levels: This should report if the filesystem supports encrypted storage. |
| 3477 | */ |
| 3478 | private boolean isEncryptionSupported() { |
| 3479 | // Note, this can be implemented as |
| 3480 | // return getEncryptionStatus() != DevicePolicyManager.ENCRYPTION_STATUS_UNSUPPORTED; |
| 3481 | // But is provided as a separate internal method if there's a faster way to do a |
| 3482 | // simple check for supported-or-not. |
| 3483 | return getEncryptionStatus() != DevicePolicyManager.ENCRYPTION_STATUS_UNSUPPORTED; |
| 3484 | } |
| 3485 | |
| 3486 | /** |
| 3487 | * Hook to low-levels: Reporting the current status of encryption. |
| 3488 | * @return A value such as {@link DevicePolicyManager#ENCRYPTION_STATUS_UNSUPPORTED} or |
| 3489 | * {@link DevicePolicyManager#ENCRYPTION_STATUS_INACTIVE} or |
| 3490 | * {@link DevicePolicyManager#ENCRYPTION_STATUS_ACTIVE}. |
| 3491 | */ |
| 3492 | private int getEncryptionStatus() { |
Amir Samuelov | 1213e2f | 2014-10-14 13:15:30 +0300 | [diff] [blame] | 3493 | String pfeStatus = SystemProperties.get("vold.pfe", ""); |
| 3494 | if ("activated".equalsIgnoreCase(pfeStatus)) { |
| 3495 | return DevicePolicyManager.ENCRYPTION_STATUS_UNSUPPORTED; |
| 3496 | } |
| 3497 | |
Andy Stadler | 0fe45de | 2011-01-20 16:35:09 -0800 | [diff] [blame] | 3498 | String status = SystemProperties.get("ro.crypto.state", "unsupported"); |
| 3499 | if ("encrypted".equalsIgnoreCase(status)) { |
Amith Yamasani | cd410ba | 2014-10-17 11:16:58 -0700 | [diff] [blame] | 3500 | final long token = Binder.clearCallingIdentity(); |
| 3501 | try { |
| 3502 | return LockPatternUtils.isDeviceEncrypted() |
| 3503 | ? DevicePolicyManager.ENCRYPTION_STATUS_ACTIVE |
| 3504 | : DevicePolicyManager.ENCRYPTION_STATUS_INACTIVE; |
| 3505 | } finally { |
| 3506 | Binder.restoreCallingIdentity(token); |
| 3507 | } |
Andy Stadler | 0fe45de | 2011-01-20 16:35:09 -0800 | [diff] [blame] | 3508 | } else if ("unencrypted".equalsIgnoreCase(status)) { |
| 3509 | return DevicePolicyManager.ENCRYPTION_STATUS_INACTIVE; |
| 3510 | } else { |
| 3511 | return DevicePolicyManager.ENCRYPTION_STATUS_UNSUPPORTED; |
| 3512 | } |
Andy Stadler | 22dbfda | 2011-01-17 12:47:31 -0800 | [diff] [blame] | 3513 | } |
| 3514 | |
| 3515 | /** |
| 3516 | * Hook to low-levels: If needed, record the new admin setting for encryption. |
| 3517 | */ |
| 3518 | private void setEncryptionRequested(boolean encrypt) { |
| 3519 | } |
| 3520 | |
Sander Alewijnse | d2a1eec | 2014-07-09 12:57:05 +0100 | [diff] [blame] | 3521 | |
| 3522 | /** |
| 3523 | * Set whether the screen capture is disabled for the user managed by the specified admin. |
| 3524 | */ |
| 3525 | public void setScreenCaptureDisabled(ComponentName who, int userHandle, boolean disabled) { |
| 3526 | if (!mHasFeature) { |
| 3527 | return; |
| 3528 | } |
Sander Alewijnse | 0ced627 | 2014-08-26 11:18:26 +0100 | [diff] [blame] | 3529 | enforceCrossUserPermission(userHandle); |
Sander Alewijnse | d2a1eec | 2014-07-09 12:57:05 +0100 | [diff] [blame] | 3530 | synchronized (this) { |
| 3531 | if (who == null) { |
| 3532 | throw new NullPointerException("ComponentName is null"); |
| 3533 | } |
| 3534 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 3535 | DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 3536 | if (ap.disableScreenCapture != disabled) { |
| 3537 | ap.disableScreenCapture = disabled; |
| 3538 | saveSettingsLocked(userHandle); |
Sander Alewijnse | a87863a | 2014-07-29 12:01:38 +0100 | [diff] [blame] | 3539 | updateScreenCaptureDisabledInWindowManager(userHandle, disabled); |
Sander Alewijnse | d2a1eec | 2014-07-09 12:57:05 +0100 | [diff] [blame] | 3540 | } |
| 3541 | } |
| 3542 | } |
| 3543 | |
| 3544 | /** |
| 3545 | * Returns whether or not screen capture is disabled for a given admin, or disabled for any |
| 3546 | * active admin (if given admin is null). |
| 3547 | */ |
| 3548 | public boolean getScreenCaptureDisabled(ComponentName who, int userHandle) { |
| 3549 | if (!mHasFeature) { |
| 3550 | return false; |
| 3551 | } |
| 3552 | synchronized (this) { |
| 3553 | if (who != null) { |
| 3554 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who, userHandle); |
| 3555 | return (admin != null) ? admin.disableScreenCapture : false; |
| 3556 | } |
| 3557 | |
| 3558 | DevicePolicyData policy = getUserData(userHandle); |
| 3559 | final int N = policy.mAdminList.size(); |
| 3560 | for (int i = 0; i < N; i++) { |
| 3561 | ActiveAdmin admin = policy.mAdminList.get(i); |
| 3562 | if (admin.disableScreenCapture) { |
| 3563 | return true; |
| 3564 | } |
| 3565 | } |
| 3566 | return false; |
| 3567 | } |
| 3568 | } |
| 3569 | |
Sander Alewijnse | a87863a | 2014-07-29 12:01:38 +0100 | [diff] [blame] | 3570 | private void updateScreenCaptureDisabledInWindowManager(int userHandle, boolean disabled) { |
| 3571 | long ident = Binder.clearCallingIdentity(); |
| 3572 | try { |
| 3573 | getWindowManager().setScreenCaptureDisabled(userHandle, disabled); |
| 3574 | } catch (RemoteException e) { |
| 3575 | Log.w(LOG_TAG, "Unable to notify WindowManager.", e); |
| 3576 | } finally { |
| 3577 | Binder.restoreCallingIdentity(ident); |
| 3578 | } |
| 3579 | } |
| 3580 | |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 3581 | /** |
Sander Alewijnse | 0ced627 | 2014-08-26 11:18:26 +0100 | [diff] [blame] | 3582 | * Set whether auto time is required by the specified admin (must be device owner). |
| 3583 | */ |
| 3584 | public void setAutoTimeRequired(ComponentName who, int userHandle, boolean required) { |
| 3585 | if (!mHasFeature) { |
| 3586 | return; |
| 3587 | } |
| 3588 | enforceCrossUserPermission(userHandle); |
| 3589 | synchronized (this) { |
| 3590 | if (who == null) { |
| 3591 | throw new NullPointerException("ComponentName is null"); |
| 3592 | } |
| 3593 | ActiveAdmin admin = getActiveAdminForCallerLocked(who, |
| 3594 | DeviceAdminInfo.USES_POLICY_DEVICE_OWNER); |
| 3595 | if (admin.requireAutoTime != required) { |
| 3596 | admin.requireAutoTime = required; |
| 3597 | saveSettingsLocked(userHandle); |
| 3598 | } |
| 3599 | } |
| 3600 | |
| 3601 | // Turn AUTO_TIME on in settings if it is required |
| 3602 | if (required) { |
| 3603 | long ident = Binder.clearCallingIdentity(); |
| 3604 | try { |
| 3605 | Settings.Global.putInt(mContext.getContentResolver(), |
| 3606 | Settings.Global.AUTO_TIME, 1 /* AUTO_TIME on */); |
| 3607 | } finally { |
| 3608 | Binder.restoreCallingIdentity(ident); |
| 3609 | } |
| 3610 | } |
| 3611 | } |
| 3612 | |
| 3613 | /** |
| 3614 | * Returns whether or not auto time is required by the device owner. |
| 3615 | */ |
| 3616 | public boolean getAutoTimeRequired() { |
| 3617 | if (!mHasFeature) { |
| 3618 | return false; |
| 3619 | } |
| 3620 | synchronized (this) { |
| 3621 | ActiveAdmin deviceOwner = getDeviceOwnerAdmin(); |
| 3622 | return (deviceOwner != null) ? deviceOwner.requireAutoTime : false; |
| 3623 | } |
| 3624 | } |
| 3625 | |
| 3626 | /** |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 3627 | * The system property used to share the state of the camera. The native camera service |
| 3628 | * is expected to read this property and act accordingly. |
| 3629 | */ |
| 3630 | public static final String SYSTEM_PROP_DISABLE_CAMERA = "sys.secpolicy.camera.disabled"; |
| 3631 | |
| 3632 | /** |
| 3633 | * Disables all device cameras according to the specified admin. |
| 3634 | */ |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3635 | public void setCameraDisabled(ComponentName who, boolean disabled, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 3636 | if (!mHasFeature) { |
| 3637 | return; |
| 3638 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3639 | enforceCrossUserPermission(userHandle); |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 3640 | synchronized (this) { |
| 3641 | if (who == null) { |
| 3642 | throw new NullPointerException("ComponentName is null"); |
| 3643 | } |
| 3644 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 3645 | DeviceAdminInfo.USES_POLICY_DISABLE_CAMERA); |
| 3646 | if (ap.disableCamera != disabled) { |
| 3647 | ap.disableCamera = disabled; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3648 | saveSettingsLocked(userHandle); |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 3649 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3650 | syncDeviceCapabilitiesLocked(getUserData(userHandle)); |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 3651 | } |
| 3652 | } |
| 3653 | |
| 3654 | /** |
| 3655 | * Gets whether or not all device cameras are disabled for a given admin, or disabled for any |
| 3656 | * active admins. |
| 3657 | */ |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3658 | public boolean getCameraDisabled(ComponentName who, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 3659 | if (!mHasFeature) { |
| 3660 | return false; |
| 3661 | } |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 3662 | synchronized (this) { |
| 3663 | if (who != null) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3664 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who, userHandle); |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 3665 | return (admin != null) ? admin.disableCamera : false; |
| 3666 | } |
| 3667 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3668 | DevicePolicyData policy = getUserData(userHandle); |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 3669 | // 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] | 3670 | final int N = policy.mAdminList.size(); |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 3671 | for (int i = 0; i < N; i++) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3672 | ActiveAdmin admin = policy.mAdminList.get(i); |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 3673 | if (admin.disableCamera) { |
| 3674 | return true; |
| 3675 | } |
| 3676 | } |
| 3677 | return false; |
| 3678 | } |
| 3679 | } |
| 3680 | |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 3681 | /** |
Jim Miller | 48b9b0d | 2012-09-19 23:16:50 -0700 | [diff] [blame] | 3682 | * Selectively disable keyguard features. |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 3683 | */ |
Jim Miller | 48b9b0d | 2012-09-19 23:16:50 -0700 | [diff] [blame] | 3684 | public void setKeyguardDisabledFeatures(ComponentName who, int which, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 3685 | if (!mHasFeature) { |
| 3686 | return; |
| 3687 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3688 | enforceCrossUserPermission(userHandle); |
Robin Lee | a9ee672 | 2014-05-20 16:26:49 +0100 | [diff] [blame] | 3689 | enforceNotManagedProfile(userHandle, "disable keyguard features"); |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 3690 | synchronized (this) { |
| 3691 | if (who == null) { |
| 3692 | throw new NullPointerException("ComponentName is null"); |
| 3693 | } |
| 3694 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
Jim Miller | 48b9b0d | 2012-09-19 23:16:50 -0700 | [diff] [blame] | 3695 | DeviceAdminInfo.USES_POLICY_DISABLE_KEYGUARD_FEATURES); |
| 3696 | if (ap.disabledKeyguardFeatures != which) { |
| 3697 | ap.disabledKeyguardFeatures = which; |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3698 | saveSettingsLocked(userHandle); |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 3699 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3700 | syncDeviceCapabilitiesLocked(getUserData(userHandle)); |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 3701 | } |
| 3702 | } |
| 3703 | |
| 3704 | /** |
Jim Miller | 48b9b0d | 2012-09-19 23:16:50 -0700 | [diff] [blame] | 3705 | * Gets the disabled state for features in keyguard for the given admin, |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 3706 | * or the aggregate of all active admins if who is null. |
| 3707 | */ |
Jim Miller | 48b9b0d | 2012-09-19 23:16:50 -0700 | [diff] [blame] | 3708 | public int getKeyguardDisabledFeatures(ComponentName who, int userHandle) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 3709 | if (!mHasFeature) { |
| 3710 | return 0; |
| 3711 | } |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3712 | enforceCrossUserPermission(userHandle); |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 3713 | synchronized (this) { |
| 3714 | if (who != null) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3715 | ActiveAdmin admin = getActiveAdminUncheckedLocked(who, userHandle); |
Jim Miller | 48b9b0d | 2012-09-19 23:16:50 -0700 | [diff] [blame] | 3716 | return (admin != null) ? admin.disabledKeyguardFeatures : 0; |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 3717 | } |
| 3718 | |
Jim Miller | 48b9b0d | 2012-09-19 23:16:50 -0700 | [diff] [blame] | 3719 | // Determine which keyguard features are disabled for any active admins. |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3720 | DevicePolicyData policy = getUserData(userHandle); |
| 3721 | final int N = policy.mAdminList.size(); |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 3722 | int which = 0; |
| 3723 | for (int i = 0; i < N; i++) { |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 3724 | ActiveAdmin admin = policy.mAdminList.get(i); |
Jim Miller | 48b9b0d | 2012-09-19 23:16:50 -0700 | [diff] [blame] | 3725 | which |= admin.disabledKeyguardFeatures; |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 3726 | } |
| 3727 | return which; |
| 3728 | } |
| 3729 | } |
| 3730 | |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 3731 | @Override |
Geoffrey Borggaard | 334c7e3 | 2013-08-08 14:31:36 -0400 | [diff] [blame] | 3732 | public boolean setDeviceOwner(String packageName, String ownerName) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 3733 | if (!mHasFeature) { |
| 3734 | return false; |
| 3735 | } |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 3736 | if (packageName == null |
| 3737 | || !DeviceOwner.isInstalled(packageName, mContext.getPackageManager())) { |
| 3738 | throw new IllegalArgumentException("Invalid package name " + packageName |
| 3739 | + " for device owner"); |
| 3740 | } |
| 3741 | synchronized (this) { |
Esteban Talavera | b5ef162 | 2014-09-08 17:49:01 +0100 | [diff] [blame] | 3742 | if (!allowedToSetDeviceOwnerOnDevice()) { |
Adam Connors | 776c555 | 2014-01-09 10:42:56 +0000 | [diff] [blame] | 3743 | throw new IllegalStateException( |
| 3744 | "Trying to set device owner but device is already provisioned."); |
| 3745 | } |
| 3746 | |
| 3747 | if (mDeviceOwner != null && mDeviceOwner.hasDeviceOwner()) { |
| 3748 | throw new IllegalStateException( |
| 3749 | "Trying to set device owner but device owner is already set."); |
| 3750 | } |
| 3751 | |
Zoltan Szatmary-Ban | 26ac6a6 | 2014-11-13 20:49:43 +0000 | [diff] [blame] | 3752 | // Shutting down backup manager service permanently. |
| 3753 | long ident = Binder.clearCallingIdentity(); |
| 3754 | try { |
| 3755 | IBackupManager ibm = IBackupManager.Stub.asInterface( |
| 3756 | ServiceManager.getService(Context.BACKUP_SERVICE)); |
| 3757 | ibm.setBackupServiceActive(UserHandle.USER_OWNER, false); |
| 3758 | } catch (RemoteException e) { |
| 3759 | throw new IllegalStateException("Failed deactivating backup service.", e); |
| 3760 | } finally { |
| 3761 | Binder.restoreCallingIdentity(ident); |
| 3762 | } |
| 3763 | |
Adam Connors | 776c555 | 2014-01-09 10:42:56 +0000 | [diff] [blame] | 3764 | if (mDeviceOwner == null) { |
| 3765 | // Device owner is not set and does not exist, set it. |
| 3766 | mDeviceOwner = DeviceOwner.createWithDeviceOwner(packageName, ownerName); |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 3767 | mDeviceOwner.writeOwnerFile(); |
| 3768 | return true; |
| 3769 | } else { |
Adam Connors | 776c555 | 2014-01-09 10:42:56 +0000 | [diff] [blame] | 3770 | // Device owner is not set but a profile owner exists, update Device owner state. |
| 3771 | mDeviceOwner.setDeviceOwner(packageName, ownerName); |
| 3772 | mDeviceOwner.writeOwnerFile(); |
| 3773 | return true; |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 3774 | } |
| 3775 | } |
| 3776 | } |
| 3777 | |
| 3778 | @Override |
| 3779 | public boolean isDeviceOwner(String packageName) { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 3780 | if (!mHasFeature) { |
| 3781 | return false; |
| 3782 | } |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 3783 | synchronized (this) { |
| 3784 | return mDeviceOwner != null |
Robin Lee | 08fc72d | 2014-02-20 12:50:41 +0000 | [diff] [blame] | 3785 | && mDeviceOwner.hasDeviceOwner() |
Adam Connors | 776c555 | 2014-01-09 10:42:56 +0000 | [diff] [blame] | 3786 | && mDeviceOwner.getDeviceOwnerPackageName().equals(packageName); |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 3787 | } |
| 3788 | } |
| 3789 | |
| 3790 | @Override |
| 3791 | public String getDeviceOwner() { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 3792 | if (!mHasFeature) { |
| 3793 | return null; |
| 3794 | } |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 3795 | synchronized (this) { |
Alexandra Gherghina | a4fcb44 | 2014-04-24 17:55:30 +0100 | [diff] [blame] | 3796 | if (mDeviceOwner != null && mDeviceOwner.hasDeviceOwner()) { |
Adam Connors | 776c555 | 2014-01-09 10:42:56 +0000 | [diff] [blame] | 3797 | return mDeviceOwner.getDeviceOwnerPackageName(); |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 3798 | } |
| 3799 | } |
| 3800 | return null; |
| 3801 | } |
| 3802 | |
Geoffrey Borggaard | 334c7e3 | 2013-08-08 14:31:36 -0400 | [diff] [blame] | 3803 | @Override |
| 3804 | public String getDeviceOwnerName() { |
Amith Yamasani | 44a01b7 | 2013-09-16 10:44:57 -0700 | [diff] [blame] | 3805 | if (!mHasFeature) { |
| 3806 | return null; |
| 3807 | } |
Geoffrey Borggaard | 334c7e3 | 2013-08-08 14:31:36 -0400 | [diff] [blame] | 3808 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USERS, null); |
| 3809 | synchronized (this) { |
| 3810 | if (mDeviceOwner != null) { |
Adam Connors | 776c555 | 2014-01-09 10:42:56 +0000 | [diff] [blame] | 3811 | return mDeviceOwner.getDeviceOwnerName(); |
| 3812 | } |
| 3813 | } |
| 3814 | return null; |
| 3815 | } |
| 3816 | |
Sander Alewijnse | 0ced627 | 2014-08-26 11:18:26 +0100 | [diff] [blame] | 3817 | // Returns the active device owner or null if there is no device owner. |
| 3818 | private ActiveAdmin getDeviceOwnerAdmin() { |
| 3819 | String deviceOwnerPackageName = getDeviceOwner(); |
| 3820 | if (deviceOwnerPackageName == null) { |
| 3821 | return null; |
| 3822 | } |
| 3823 | |
| 3824 | DevicePolicyData policy = getUserData(UserHandle.USER_OWNER); |
| 3825 | final int n = policy.mAdminList.size(); |
| 3826 | for (int i = 0; i < n; i++) { |
| 3827 | ActiveAdmin admin = policy.mAdminList.get(i); |
| 3828 | if (deviceOwnerPackageName.equals(admin.info.getPackageName())) { |
| 3829 | return admin; |
| 3830 | } |
| 3831 | } |
| 3832 | return null; |
| 3833 | } |
| 3834 | |
Adam Connors | 776c555 | 2014-01-09 10:42:56 +0000 | [diff] [blame] | 3835 | @Override |
Jason Monk | b0dced8 | 2014-06-06 14:36:20 -0400 | [diff] [blame] | 3836 | public void clearDeviceOwner(String packageName) { |
| 3837 | if (packageName == null) { |
| 3838 | throw new NullPointerException("packageName is null"); |
| 3839 | } |
| 3840 | try { |
| 3841 | int uid = mContext.getPackageManager().getPackageUid(packageName, 0); |
| 3842 | if (uid != Binder.getCallingUid()) { |
| 3843 | throw new SecurityException("Invalid packageName"); |
| 3844 | } |
| 3845 | } catch (NameNotFoundException e) { |
| 3846 | throw new SecurityException(e); |
| 3847 | } |
| 3848 | if (!isDeviceOwner(packageName)) { |
| 3849 | throw new SecurityException("clearDeviceOwner can only be called by the device owner"); |
| 3850 | } |
| 3851 | synchronized (this) { |
| 3852 | long ident = Binder.clearCallingIdentity(); |
| 3853 | try { |
Julia Reynolds | 3d9eb78 | 2014-08-11 16:40:08 -0400 | [diff] [blame] | 3854 | clearUserRestrictions(new UserHandle(UserHandle.USER_OWNER)); |
Jason Monk | b0dced8 | 2014-06-06 14:36:20 -0400 | [diff] [blame] | 3855 | if (mDeviceOwner != null) { |
| 3856 | mDeviceOwner.clearDeviceOwner(); |
| 3857 | mDeviceOwner.writeOwnerFile(); |
| 3858 | } |
| 3859 | } finally { |
| 3860 | Binder.restoreCallingIdentity(ident); |
| 3861 | } |
| 3862 | } |
| 3863 | } |
| 3864 | |
| 3865 | @Override |
Amith Yamasani | bf3a946 | 2014-07-28 14:26:42 -0700 | [diff] [blame] | 3866 | public boolean setProfileOwner(ComponentName who, String ownerName, int userHandle) { |
Adam Connors | 776c555 | 2014-01-09 10:42:56 +0000 | [diff] [blame] | 3867 | if (!mHasFeature) { |
| 3868 | return false; |
| 3869 | } |
Sudheer Shanka | 6c780ac | 2016-06-08 17:13:24 -0700 | [diff] [blame] | 3870 | if (ActivityManager.checkComponentPermission(android.Manifest.permission.CREATE_USERS, |
| 3871 | Binder.getCallingUid(), -1, true) == PackageManager.PERMISSION_DENIED) { |
| 3872 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USERS, null); |
| 3873 | } |
Adam Connors | 661ec47 | 2014-02-11 13:59:46 +0000 | [diff] [blame] | 3874 | |
Amith Yamasani | 29ce85b | 2014-09-03 17:30:43 -0700 | [diff] [blame] | 3875 | UserInfo info = mUserManager.getUserInfo(userHandle); |
| 3876 | if (info == null) { |
Adam Connors | 661ec47 | 2014-02-11 13:59:46 +0000 | [diff] [blame] | 3877 | // User doesn't exist. |
| 3878 | throw new IllegalArgumentException( |
| 3879 | "Attempted to set profile owner for invalid userId: " + userHandle); |
| 3880 | } |
Amith Yamasani | 29ce85b | 2014-09-03 17:30:43 -0700 | [diff] [blame] | 3881 | if (info.isGuest()) { |
| 3882 | throw new IllegalStateException("Cannot set a profile owner on a guest"); |
| 3883 | } |
Adam Connors | 661ec47 | 2014-02-11 13:59:46 +0000 | [diff] [blame] | 3884 | |
Amith Yamasani | bf3a946 | 2014-07-28 14:26:42 -0700 | [diff] [blame] | 3885 | if (who == null |
| 3886 | || !DeviceOwner.isInstalledForUser(who.getPackageName(), userHandle)) { |
| 3887 | throw new IllegalArgumentException("Component " + who |
Adam Connors | 776c555 | 2014-01-09 10:42:56 +0000 | [diff] [blame] | 3888 | + " not installed for userId:" + userHandle); |
| 3889 | } |
| 3890 | synchronized (this) { |
Amith Yamasani | bf3a946 | 2014-07-28 14:26:42 -0700 | [diff] [blame] | 3891 | // Only SYSTEM_UID can override the userSetupComplete |
| 3892 | if (UserHandle.getAppId(Binder.getCallingUid()) != Process.SYSTEM_UID |
Amith Yamasani | d616a33 | 2014-09-12 09:05:19 -0700 | [diff] [blame] | 3893 | && hasUserSetupCompleted(userHandle)) { |
Adam Connors | 776c555 | 2014-01-09 10:42:56 +0000 | [diff] [blame] | 3894 | throw new IllegalStateException( |
| 3895 | "Trying to set profile owner but user is already set-up."); |
| 3896 | } |
Julia Reynolds | 401de17 | 2014-07-24 18:21:29 -0400 | [diff] [blame] | 3897 | |
Adam Connors | 776c555 | 2014-01-09 10:42:56 +0000 | [diff] [blame] | 3898 | if (mDeviceOwner == null) { |
| 3899 | // Device owner state does not exist, create it. |
Amith Yamasani | bf3a946 | 2014-07-28 14:26:42 -0700 | [diff] [blame] | 3900 | mDeviceOwner = DeviceOwner.createWithProfileOwner(who, ownerName, |
Adam Connors | 776c555 | 2014-01-09 10:42:56 +0000 | [diff] [blame] | 3901 | userHandle); |
| 3902 | mDeviceOwner.writeOwnerFile(); |
| 3903 | return true; |
| 3904 | } else { |
| 3905 | // Device owner already exists, update it. |
Amith Yamasani | bf3a946 | 2014-07-28 14:26:42 -0700 | [diff] [blame] | 3906 | mDeviceOwner.setProfileOwner(who, ownerName, userHandle); |
Adam Connors | 776c555 | 2014-01-09 10:42:56 +0000 | [diff] [blame] | 3907 | mDeviceOwner.writeOwnerFile(); |
| 3908 | return true; |
| 3909 | } |
| 3910 | } |
| 3911 | } |
| 3912 | |
| 3913 | @Override |
Amith Yamasani | bf3a946 | 2014-07-28 14:26:42 -0700 | [diff] [blame] | 3914 | public void clearProfileOwner(ComponentName who) { |
| 3915 | if (!mHasFeature) { |
| 3916 | return; |
| 3917 | } |
| 3918 | UserHandle callingUser = Binder.getCallingUserHandle(); |
| 3919 | // Check if this is the profile owner who is calling |
| 3920 | getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 3921 | synchronized (this) { |
| 3922 | long ident = Binder.clearCallingIdentity(); |
| 3923 | try { |
Julia Reynolds | 3d9eb78 | 2014-08-11 16:40:08 -0400 | [diff] [blame] | 3924 | clearUserRestrictions(callingUser); |
Amith Yamasani | bf3a946 | 2014-07-28 14:26:42 -0700 | [diff] [blame] | 3925 | if (mDeviceOwner != null) { |
| 3926 | mDeviceOwner.removeProfileOwner(callingUser.getIdentifier()); |
| 3927 | mDeviceOwner.writeOwnerFile(); |
| 3928 | } |
| 3929 | } finally { |
| 3930 | Binder.restoreCallingIdentity(ident); |
| 3931 | } |
| 3932 | } |
| 3933 | } |
| 3934 | |
Julia Reynolds | 3d9eb78 | 2014-08-11 16:40:08 -0400 | [diff] [blame] | 3935 | private void clearUserRestrictions(UserHandle userHandle) { |
| 3936 | AudioManager audioManager = |
| 3937 | (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); |
| 3938 | Bundle userRestrictions = mUserManager.getUserRestrictions(); |
| 3939 | mUserManager.setUserRestrictions(new Bundle(), userHandle); |
| 3940 | if (userRestrictions.getBoolean(UserManager.DISALLOW_ADJUST_VOLUME)) { |
| 3941 | audioManager.setMasterMute(false); |
| 3942 | } |
| 3943 | if (userRestrictions.getBoolean(UserManager.DISALLOW_UNMUTE_MICROPHONE)) { |
| 3944 | audioManager.setMicrophoneMute(false); |
| 3945 | } |
| 3946 | } |
| 3947 | |
Amith Yamasani | bf3a946 | 2014-07-28 14:26:42 -0700 | [diff] [blame] | 3948 | @Override |
| 3949 | public boolean hasUserSetupCompleted() { |
Amith Yamasani | d616a33 | 2014-09-12 09:05:19 -0700 | [diff] [blame] | 3950 | return hasUserSetupCompleted(UserHandle.getCallingUserId()); |
| 3951 | } |
| 3952 | |
| 3953 | private boolean hasUserSetupCompleted(int userHandle) { |
Amith Yamasani | bf3a946 | 2014-07-28 14:26:42 -0700 | [diff] [blame] | 3954 | if (!mHasFeature) { |
| 3955 | return true; |
| 3956 | } |
Amith Yamasani | d616a33 | 2014-09-12 09:05:19 -0700 | [diff] [blame] | 3957 | DevicePolicyData policy = getUserData(userHandle); |
Amith Yamasani | bf3a946 | 2014-07-28 14:26:42 -0700 | [diff] [blame] | 3958 | // If policy is null, return true, else check if the setup has completed. |
| 3959 | return policy == null || policy.mUserSetupComplete; |
| 3960 | } |
| 3961 | |
| 3962 | @Override |
Alexandra Gherghina | 512675b | 2014-04-02 11:23:54 +0100 | [diff] [blame] | 3963 | public void setProfileEnabled(ComponentName who) { |
| 3964 | if (!mHasFeature) { |
| 3965 | return; |
| 3966 | } |
Robin Lee | 65c2797 | 2014-08-28 16:56:15 +0100 | [diff] [blame] | 3967 | final int userHandle = UserHandle.getCallingUserId(); |
Alexandra Gherghina | 512675b | 2014-04-02 11:23:54 +0100 | [diff] [blame] | 3968 | synchronized (this) { |
| 3969 | // Check for permissions |
| 3970 | if (who == null) { |
| 3971 | throw new NullPointerException("ComponentName is null"); |
| 3972 | } |
| 3973 | // Check if this is the profile owner who is calling |
| 3974 | getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
Alexandra Gherghina | 385124d | 2014-04-03 13:37:39 +0100 | [diff] [blame] | 3975 | int userId = UserHandle.getCallingUserId(); |
Alexandra Gherghina | 512675b | 2014-04-02 11:23:54 +0100 | [diff] [blame] | 3976 | |
Alexandra Gherghina | 385124d | 2014-04-03 13:37:39 +0100 | [diff] [blame] | 3977 | long id = Binder.clearCallingIdentity(); |
Alexandra Gherghina | 512675b | 2014-04-02 11:23:54 +0100 | [diff] [blame] | 3978 | try { |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 3979 | mUserManager.setUserEnabled(userId); |
Alexandra Gherghina | 512675b | 2014-04-02 11:23:54 +0100 | [diff] [blame] | 3980 | Intent intent = new Intent(Intent.ACTION_MANAGED_PROFILE_ADDED); |
Robin Lee | 65c2797 | 2014-08-28 16:56:15 +0100 | [diff] [blame] | 3981 | intent.putExtra(Intent.EXTRA_USER, new UserHandle(userHandle)); |
Alexandra Gherghina | 9944c2f | 2014-04-15 10:40:33 +0100 | [diff] [blame] | 3982 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY | |
| 3983 | Intent.FLAG_RECEIVER_FOREGROUND); |
Kenny Guy | 73a41d2 | 2014-06-05 16:51:24 +0100 | [diff] [blame] | 3984 | // TODO This should send to parent of profile (which is always owner at the moment). |
Alexandra Gherghina | 512675b | 2014-04-02 11:23:54 +0100 | [diff] [blame] | 3985 | mContext.sendBroadcastAsUser(intent, UserHandle.OWNER); |
| 3986 | } finally { |
| 3987 | restoreCallingIdentity(id); |
| 3988 | } |
| 3989 | } |
| 3990 | } |
| 3991 | |
| 3992 | @Override |
Jessica Hummel | 1333ea1 | 2014-06-23 11:20:10 +0100 | [diff] [blame] | 3993 | public void setProfileName(ComponentName who, String profileName) { |
| 3994 | int userId = UserHandle.getCallingUserId(); |
| 3995 | |
| 3996 | if (who == null) { |
| 3997 | throw new NullPointerException("ComponentName is null"); |
| 3998 | } |
| 3999 | |
| 4000 | // Check if this is the profile owner (includes device owner). |
| 4001 | getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 4002 | |
| 4003 | long id = Binder.clearCallingIdentity(); |
| 4004 | try { |
| 4005 | mUserManager.setUserName(userId, profileName); |
| 4006 | } finally { |
| 4007 | restoreCallingIdentity(id); |
| 4008 | } |
| 4009 | } |
| 4010 | |
| 4011 | @Override |
Amith Yamasani | bf3a946 | 2014-07-28 14:26:42 -0700 | [diff] [blame] | 4012 | public ComponentName getProfileOwner(int userHandle) { |
Adam Connors | 776c555 | 2014-01-09 10:42:56 +0000 | [diff] [blame] | 4013 | if (!mHasFeature) { |
| 4014 | return null; |
| 4015 | } |
| 4016 | |
| 4017 | synchronized (this) { |
| 4018 | if (mDeviceOwner != null) { |
Amith Yamasani | bf3a946 | 2014-07-28 14:26:42 -0700 | [diff] [blame] | 4019 | return mDeviceOwner.getProfileOwnerComponent(userHandle); |
Adam Connors | 776c555 | 2014-01-09 10:42:56 +0000 | [diff] [blame] | 4020 | } |
| 4021 | } |
| 4022 | return null; |
| 4023 | } |
| 4024 | |
Adam Connors | 210fe21 | 2014-07-17 15:41:43 +0100 | [diff] [blame] | 4025 | // Returns the active profile owner for this user or null if the current user has no |
| 4026 | // profile owner. |
| 4027 | private ActiveAdmin getProfileOwnerAdmin(int userHandle) { |
Amith Yamasani | bf3a946 | 2014-07-28 14:26:42 -0700 | [diff] [blame] | 4028 | ComponentName profileOwner = |
| 4029 | mDeviceOwner != null ? mDeviceOwner.getProfileOwnerComponent(userHandle) : null; |
| 4030 | if (profileOwner == null) { |
Adam Connors | 210fe21 | 2014-07-17 15:41:43 +0100 | [diff] [blame] | 4031 | return null; |
| 4032 | } |
| 4033 | |
| 4034 | DevicePolicyData policy = getUserData(userHandle); |
| 4035 | final int n = policy.mAdminList.size(); |
| 4036 | for (int i = 0; i < n; i++) { |
| 4037 | ActiveAdmin admin = policy.mAdminList.get(i); |
Rubin Xu | d7892e7 | 2014-10-30 17:57:23 +0000 | [diff] [blame] | 4038 | if (profileOwner.equals(admin.info.getComponent())) { |
Adam Connors | 210fe21 | 2014-07-17 15:41:43 +0100 | [diff] [blame] | 4039 | return admin; |
| 4040 | } |
| 4041 | } |
| 4042 | return null; |
| 4043 | } |
| 4044 | |
Adam Connors | 776c555 | 2014-01-09 10:42:56 +0000 | [diff] [blame] | 4045 | @Override |
| 4046 | public String getProfileOwnerName(int userHandle) { |
| 4047 | if (!mHasFeature) { |
| 4048 | return null; |
| 4049 | } |
| 4050 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USERS, null); |
| 4051 | |
| 4052 | synchronized (this) { |
| 4053 | if (mDeviceOwner != null) { |
| 4054 | return mDeviceOwner.getProfileOwnerName(userHandle); |
Geoffrey Borggaard | 334c7e3 | 2013-08-08 14:31:36 -0400 | [diff] [blame] | 4055 | } |
| 4056 | } |
| 4057 | return null; |
| 4058 | } |
| 4059 | |
Esteban Talavera | b5ef162 | 2014-09-08 17:49:01 +0100 | [diff] [blame] | 4060 | /** |
| 4061 | * Device owner can only be set on an unprovisioned device, unless it was initiated by "adb", in |
| 4062 | * which case we allow it if no account is associated with the device. |
| 4063 | */ |
| 4064 | private boolean allowedToSetDeviceOwnerOnDevice() { |
| 4065 | int callingId = Binder.getCallingUid(); |
| 4066 | if (callingId == Process.SHELL_UID || callingId == Process.ROOT_UID) { |
| 4067 | return AccountManager.get(mContext).getAccounts().length == 0; |
| 4068 | } else { |
Amith Yamasani | d616a33 | 2014-09-12 09:05:19 -0700 | [diff] [blame] | 4069 | return !hasUserSetupCompleted(UserHandle.USER_OWNER); |
Esteban Talavera | b5ef162 | 2014-09-08 17:49:01 +0100 | [diff] [blame] | 4070 | } |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 4071 | } |
| 4072 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 4073 | private void enforceCrossUserPermission(int userHandle) { |
| 4074 | if (userHandle < 0) { |
| 4075 | throw new IllegalArgumentException("Invalid userId " + userHandle); |
| 4076 | } |
| 4077 | final int callingUid = Binder.getCallingUid(); |
| 4078 | if (userHandle == UserHandle.getUserId(callingUid)) return; |
| 4079 | if (callingUid != Process.SYSTEM_UID && callingUid != 0) { |
| 4080 | mContext.enforceCallingOrSelfPermission( |
| 4081 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, "Must be system or have" |
| 4082 | + " INTERACT_ACROSS_USERS_FULL permission"); |
| 4083 | } |
| 4084 | } |
| 4085 | |
Adam Connors | 210fe21 | 2014-07-17 15:41:43 +0100 | [diff] [blame] | 4086 | private void enforceSystemProcess(String message) { |
| 4087 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 4088 | throw new SecurityException(message); |
| 4089 | } |
| 4090 | } |
| 4091 | |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 4092 | private void enforceNotManagedProfile(int userHandle, String message) { |
| 4093 | if(isManagedProfile(userHandle)) { |
Robin Lee | a9e460a | 2014-05-21 15:04:19 +0100 | [diff] [blame] | 4094 | throw new SecurityException("You can not " + message + " for a managed profile. "); |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 4095 | } |
| 4096 | } |
| 4097 | |
| 4098 | private UserInfo getProfileParent(int userHandle) { |
| 4099 | long ident = Binder.clearCallingIdentity(); |
| 4100 | try { |
| 4101 | return mUserManager.getProfileParent(userHandle); |
| 4102 | } finally { |
| 4103 | Binder.restoreCallingIdentity(ident); |
| 4104 | } |
| 4105 | } |
| 4106 | |
| 4107 | private boolean isManagedProfile(int userHandle) { |
| 4108 | long ident = Binder.clearCallingIdentity(); |
| 4109 | try { |
| 4110 | return mUserManager.getUserInfo(userHandle).isManagedProfile(); |
| 4111 | } finally { |
| 4112 | Binder.restoreCallingIdentity(ident); |
| 4113 | } |
| 4114 | } |
| 4115 | |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 4116 | private void enableIfNecessary(String packageName, int userId) { |
| 4117 | try { |
| 4118 | IPackageManager ipm = AppGlobals.getPackageManager(); |
| 4119 | ApplicationInfo ai = ipm.getApplicationInfo(packageName, |
| 4120 | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS, |
| 4121 | userId); |
| 4122 | if (ai.enabledSetting |
| 4123 | == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) { |
| 4124 | ipm.setApplicationEnabledSetting(packageName, |
| 4125 | PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, |
Dianne Hackborn | 3fa3c28a | 2013-03-26 16:15:41 -0700 | [diff] [blame] | 4126 | PackageManager.DONT_KILL_APP, userId, "DevicePolicyManager"); |
Amith Yamasani | 71e6c69 | 2013-03-24 17:39:28 -0700 | [diff] [blame] | 4127 | } |
| 4128 | } catch (RemoteException e) { |
| 4129 | } |
| 4130 | } |
| 4131 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 4132 | @Override |
| 4133 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 4134 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 4135 | != PackageManager.PERMISSION_GRANTED) { |
| 4136 | |
| 4137 | pw.println("Permission Denial: can't dump DevicePolicyManagerService from from pid=" |
| 4138 | + Binder.getCallingPid() |
| 4139 | + ", uid=" + Binder.getCallingUid()); |
| 4140 | return; |
| 4141 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 4142 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 4143 | final Printer p = new PrintWriterPrinter(pw); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 4144 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 4145 | synchronized (this) { |
| 4146 | p.println("Current Device Policy Manager state:"); |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 4147 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 4148 | int userCount = mUserData.size(); |
| 4149 | for (int u = 0; u < userCount; u++) { |
| 4150 | DevicePolicyData policy = getUserData(mUserData.keyAt(u)); |
| 4151 | p.println(" Enabled Device Admins (User " + policy.mUserHandle + "):"); |
| 4152 | final int N = policy.mAdminList.size(); |
| 4153 | for (int i=0; i<N; i++) { |
| 4154 | ActiveAdmin ap = policy.mAdminList.get(i); |
| 4155 | if (ap != null) { |
| 4156 | pw.print(" "); pw.print(ap.info.getComponent().flattenToShortString()); |
| 4157 | pw.println(":"); |
| 4158 | ap.dump(" ", pw); |
| 4159 | } |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 4160 | } |
Fyodor Kupolov | 96fb932 | 2014-12-01 15:08:09 -0800 | [diff] [blame] | 4161 | if (!policy.mRemovingAdmins.isEmpty()) { |
| 4162 | p.println(" Removing Device Admins (User " + policy.mUserHandle + "): " |
| 4163 | + policy.mRemovingAdmins); |
| 4164 | } |
Konstantin Lopyrev | 3255823 | 2010-05-20 16:18:05 -0700 | [diff] [blame] | 4165 | |
Amith Yamasani | 599dd7c | 2012-09-14 23:20:08 -0700 | [diff] [blame] | 4166 | pw.println(" "); |
| 4167 | pw.print(" mPasswordOwner="); pw.println(policy.mPasswordOwner); |
| 4168 | } |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 4169 | } |
| 4170 | } |
Sander Alewijnse | f475ca3 | 2014-02-17 15:13:58 +0000 | [diff] [blame] | 4171 | |
Adam Connors | 010cfd4 | 2014-04-16 12:48:13 +0100 | [diff] [blame] | 4172 | @Override |
Robin Lee | 5c921da | 2014-03-24 15:11:35 +0000 | [diff] [blame] | 4173 | public void addPersistentPreferredActivity(ComponentName who, IntentFilter filter, |
Sander Alewijnse | f475ca3 | 2014-02-17 15:13:58 +0000 | [diff] [blame] | 4174 | ComponentName activity) { |
Robin Lee | 65c2797 | 2014-08-28 16:56:15 +0100 | [diff] [blame] | 4175 | final int userHandle = UserHandle.getCallingUserId(); |
| 4176 | |
Sander Alewijnse | f475ca3 | 2014-02-17 15:13:58 +0000 | [diff] [blame] | 4177 | synchronized (this) { |
Robin Lee | 5c921da | 2014-03-24 15:11:35 +0000 | [diff] [blame] | 4178 | if (who == null) { |
| 4179 | throw new NullPointerException("ComponentName is null"); |
| 4180 | } |
| 4181 | getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 4182 | |
| 4183 | IPackageManager pm = AppGlobals.getPackageManager(); |
| 4184 | long id = Binder.clearCallingIdentity(); |
| 4185 | try { |
Robin Lee | 65c2797 | 2014-08-28 16:56:15 +0100 | [diff] [blame] | 4186 | pm.addPersistentPreferredActivity(filter, activity, userHandle); |
Robin Lee | 5c921da | 2014-03-24 15:11:35 +0000 | [diff] [blame] | 4187 | } catch (RemoteException re) { |
| 4188 | // Shouldn't happen |
| 4189 | } finally { |
| 4190 | restoreCallingIdentity(id); |
Sander Alewijnse | f475ca3 | 2014-02-17 15:13:58 +0000 | [diff] [blame] | 4191 | } |
| 4192 | } |
| 4193 | } |
| 4194 | |
Adam Connors | 010cfd4 | 2014-04-16 12:48:13 +0100 | [diff] [blame] | 4195 | @Override |
Robin Lee | 5c921da | 2014-03-24 15:11:35 +0000 | [diff] [blame] | 4196 | public void clearPackagePersistentPreferredActivities(ComponentName who, String packageName) { |
Robin Lee | 65c2797 | 2014-08-28 16:56:15 +0100 | [diff] [blame] | 4197 | final int userHandle = UserHandle.getCallingUserId(); |
| 4198 | |
Sander Alewijnse | f475ca3 | 2014-02-17 15:13:58 +0000 | [diff] [blame] | 4199 | synchronized (this) { |
Robin Lee | 5c921da | 2014-03-24 15:11:35 +0000 | [diff] [blame] | 4200 | if (who == null) { |
| 4201 | throw new NullPointerException("ComponentName is null"); |
| 4202 | } |
| 4203 | getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 4204 | |
| 4205 | IPackageManager pm = AppGlobals.getPackageManager(); |
| 4206 | long id = Binder.clearCallingIdentity(); |
| 4207 | try { |
Robin Lee | 65c2797 | 2014-08-28 16:56:15 +0100 | [diff] [blame] | 4208 | pm.clearPackagePersistentPreferredActivities(packageName, userHandle); |
Robin Lee | 5c921da | 2014-03-24 15:11:35 +0000 | [diff] [blame] | 4209 | } catch (RemoteException re) { |
| 4210 | // Shouldn't happen |
| 4211 | } finally { |
| 4212 | restoreCallingIdentity(id); |
Sander Alewijnse | f475ca3 | 2014-02-17 15:13:58 +0000 | [diff] [blame] | 4213 | } |
| 4214 | } |
| 4215 | } |
Robin Lee | 66e5d96 | 2014-04-09 16:44:21 +0100 | [diff] [blame] | 4216 | |
| 4217 | @Override |
| 4218 | public void setApplicationRestrictions(ComponentName who, String packageName, Bundle settings) { |
| 4219 | final UserHandle userHandle = new UserHandle(UserHandle.getCallingUserId()); |
| 4220 | |
| 4221 | synchronized (this) { |
| 4222 | if (who == null) { |
| 4223 | throw new NullPointerException("ComponentName is null"); |
| 4224 | } |
| 4225 | getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 4226 | |
Robin Lee | 66e5d96 | 2014-04-09 16:44:21 +0100 | [diff] [blame] | 4227 | long id = Binder.clearCallingIdentity(); |
| 4228 | try { |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 4229 | mUserManager.setApplicationRestrictions(packageName, settings, userHandle); |
Robin Lee | 66e5d96 | 2014-04-09 16:44:21 +0100 | [diff] [blame] | 4230 | } finally { |
| 4231 | restoreCallingIdentity(id); |
| 4232 | } |
| 4233 | } |
| 4234 | } |
| 4235 | |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 4236 | public void setTrustAgentConfiguration(ComponentName admin, ComponentName agent, |
| 4237 | PersistableBundle args, int userHandle) { |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 4238 | if (!mHasFeature) { |
| 4239 | return; |
| 4240 | } |
| 4241 | enforceCrossUserPermission(userHandle); |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 4242 | enforceNotManagedProfile(userHandle, "set trust agent configuration"); |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 4243 | synchronized (this) { |
| 4244 | if (admin == null) { |
| 4245 | throw new NullPointerException("admin is null"); |
| 4246 | } |
| 4247 | if (agent == null) { |
| 4248 | throw new NullPointerException("agent is null"); |
| 4249 | } |
| 4250 | ActiveAdmin ap = getActiveAdminForCallerLocked(admin, |
| 4251 | DeviceAdminInfo.USES_POLICY_DISABLE_KEYGUARD_FEATURES); |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 4252 | ap.trustAgentInfos.put(agent.flattenToString(), new TrustAgentInfo(args)); |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 4253 | saveSettingsLocked(userHandle); |
| 4254 | syncDeviceCapabilitiesLocked(getUserData(userHandle)); |
| 4255 | } |
| 4256 | } |
| 4257 | |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 4258 | public List<PersistableBundle> getTrustAgentConfiguration(ComponentName admin, |
| 4259 | ComponentName agent, int userHandle) { |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 4260 | if (!mHasFeature) { |
| 4261 | return null; |
| 4262 | } |
| 4263 | enforceCrossUserPermission(userHandle); |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 4264 | if (agent == null) { |
| 4265 | throw new NullPointerException("agent is null"); |
| 4266 | } |
| 4267 | |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 4268 | synchronized (this) { |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 4269 | final String componentName = agent.flattenToString(); |
| 4270 | if (admin != null) { |
| 4271 | final ActiveAdmin ap = getActiveAdminUncheckedLocked(admin, userHandle); |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 4272 | if (ap == null) return null; |
| 4273 | TrustAgentInfo trustAgentInfo = ap.trustAgentInfos.get(componentName); |
| 4274 | if (trustAgentInfo == null || trustAgentInfo.options == null) return null; |
| 4275 | List<PersistableBundle> result = new ArrayList<PersistableBundle>(); |
| 4276 | result.add(trustAgentInfo.options); |
| 4277 | return result; |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 4278 | } |
| 4279 | |
| 4280 | // Return strictest policy for this user and profiles that are visible from this user. |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 4281 | final List<UserInfo> profiles = mUserManager.getProfiles(userHandle); |
| 4282 | List<PersistableBundle> result = null; |
| 4283 | |
| 4284 | // Search through all admins that use KEYGUARD_DISABLE_TRUST_AGENTS and keep track |
| 4285 | // of the options. If any admin doesn't have options, discard options for the rest |
| 4286 | // and return null. |
| 4287 | boolean allAdminsHaveOptions = true; |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 4288 | for (UserInfo userInfo : profiles) { |
Benjamin Franz | fadb2b3 | 2015-01-14 18:22:42 +0000 | [diff] [blame] | 4289 | DevicePolicyData policy = getUserDataUnchecked(userInfo.id); |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 4290 | final int N = policy.mAdminList.size(); |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 4291 | for (int i=0; i < N; i++) { |
| 4292 | final ActiveAdmin active = policy.mAdminList.get(i); |
| 4293 | final boolean disablesTrust = (active.disabledKeyguardFeatures |
| 4294 | & DevicePolicyManager.KEYGUARD_DISABLE_TRUST_AGENTS) != 0; |
| 4295 | final TrustAgentInfo info = active.trustAgentInfos.get(componentName); |
| 4296 | if (info != null && info.options != null && !info.options.isEmpty()) { |
| 4297 | if (disablesTrust) { |
| 4298 | if (result == null) { |
| 4299 | result = new ArrayList<PersistableBundle>(); |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 4300 | } |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 4301 | result.add(info.options); |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 4302 | } else { |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 4303 | Log.w(LOG_TAG, "Ignoring admin " + active.info |
| 4304 | + " because it has trust options but doesn't declare " |
| 4305 | + "KEYGUARD_DISABLE_TRUST_AGENTS"); |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 4306 | } |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 4307 | } else if (disablesTrust) { |
| 4308 | allAdminsHaveOptions = false; |
| 4309 | break; |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 4310 | } |
| 4311 | } |
| 4312 | } |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 4313 | return allAdminsHaveOptions ? result : null; |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 4314 | } |
| 4315 | } |
| 4316 | |
Amith Yamasani | f20d640 | 2014-05-24 15:34:37 -0700 | [diff] [blame] | 4317 | @Override |
| 4318 | public void setRestrictionsProvider(ComponentName who, ComponentName permissionProvider) { |
| 4319 | synchronized (this) { |
| 4320 | if (who == null) { |
| 4321 | throw new NullPointerException("ComponentName is null"); |
| 4322 | } |
| 4323 | getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 4324 | |
| 4325 | int userHandle = UserHandle.getCallingUserId(); |
| 4326 | DevicePolicyData userData = getUserData(userHandle); |
| 4327 | userData.mRestrictionsProvider = permissionProvider; |
| 4328 | saveSettingsLocked(userHandle); |
| 4329 | } |
| 4330 | } |
| 4331 | |
| 4332 | @Override |
| 4333 | public ComponentName getRestrictionsProvider(int userHandle) { |
| 4334 | synchronized (this) { |
| 4335 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 4336 | throw new SecurityException("Only the system can query the permission provider"); |
| 4337 | } |
| 4338 | DevicePolicyData userData = getUserData(userHandle); |
| 4339 | return userData != null ? userData.mRestrictionsProvider : null; |
| 4340 | } |
| 4341 | } |
| 4342 | |
Nicolas Prevot | 8194899 | 2014-05-16 18:25:26 +0100 | [diff] [blame] | 4343 | public void addCrossProfileIntentFilter(ComponentName who, IntentFilter filter, int flags) { |
Nicolas Prevot | 10fa67c | 2014-03-24 13:44:38 +0000 | [diff] [blame] | 4344 | int callingUserId = UserHandle.getCallingUserId(); |
| 4345 | synchronized (this) { |
| 4346 | if (who == null) { |
| 4347 | throw new NullPointerException("ComponentName is null"); |
| 4348 | } |
| 4349 | getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 4350 | |
| 4351 | IPackageManager pm = AppGlobals.getPackageManager(); |
| 4352 | long id = Binder.clearCallingIdentity(); |
| 4353 | try { |
Nicolas Prevot | 8194899 | 2014-05-16 18:25:26 +0100 | [diff] [blame] | 4354 | if ((flags & DevicePolicyManager.FLAG_PARENT_CAN_ACCESS_MANAGED) != 0) { |
Nicolas Prevot | 3f7777f | 2014-07-24 15:58:39 +0100 | [diff] [blame] | 4355 | pm.addCrossProfileIntentFilter(filter, who.getPackageName(), |
| 4356 | mContext.getUserId(), callingUserId, UserHandle.USER_OWNER, 0); |
Nicolas Prevot | 10fa67c | 2014-03-24 13:44:38 +0000 | [diff] [blame] | 4357 | } |
Nicolas Prevot | 8194899 | 2014-05-16 18:25:26 +0100 | [diff] [blame] | 4358 | if ((flags & DevicePolicyManager.FLAG_MANAGED_CAN_ACCESS_PARENT) != 0) { |
Nicolas Prevot | 3f7777f | 2014-07-24 15:58:39 +0100 | [diff] [blame] | 4359 | pm.addCrossProfileIntentFilter(filter, who.getPackageName(), |
| 4360 | mContext.getUserId(), UserHandle.USER_OWNER, callingUserId, 0); |
Nicolas Prevot | 10fa67c | 2014-03-24 13:44:38 +0000 | [diff] [blame] | 4361 | } |
| 4362 | } catch (RemoteException re) { |
| 4363 | // Shouldn't happen |
| 4364 | } finally { |
| 4365 | restoreCallingIdentity(id); |
| 4366 | } |
| 4367 | } |
| 4368 | } |
| 4369 | |
Nicolas Prevot | 8194899 | 2014-05-16 18:25:26 +0100 | [diff] [blame] | 4370 | public void clearCrossProfileIntentFilters(ComponentName who) { |
Nicolas Prevot | 10fa67c | 2014-03-24 13:44:38 +0000 | [diff] [blame] | 4371 | int callingUserId = UserHandle.getCallingUserId(); |
| 4372 | synchronized (this) { |
| 4373 | if (who == null) { |
| 4374 | throw new NullPointerException("ComponentName is null"); |
| 4375 | } |
| 4376 | getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 4377 | IPackageManager pm = AppGlobals.getPackageManager(); |
| 4378 | long id = Binder.clearCallingIdentity(); |
| 4379 | try { |
Nicolas Prevot | 3f7777f | 2014-07-24 15:58:39 +0100 | [diff] [blame] | 4380 | pm.clearCrossProfileIntentFilters(callingUserId, who.getPackageName(), |
| 4381 | callingUserId); |
Nicolas Prevot | 63798c5 | 2014-05-27 13:22:38 +0100 | [diff] [blame] | 4382 | // If we want to support multiple managed profiles, we will have to only remove |
| 4383 | // those that have callingUserId as their target. |
Nicolas Prevot | 3f7777f | 2014-07-24 15:58:39 +0100 | [diff] [blame] | 4384 | pm.clearCrossProfileIntentFilters(UserHandle.USER_OWNER, who.getPackageName(), |
| 4385 | callingUserId); |
Nicolas Prevot | 10fa67c | 2014-03-24 13:44:38 +0000 | [diff] [blame] | 4386 | } catch (RemoteException re) { |
| 4387 | // Shouldn't happen |
| 4388 | } finally { |
| 4389 | restoreCallingIdentity(id); |
| 4390 | } |
| 4391 | } |
| 4392 | } |
| 4393 | |
Kenny Guy | fa80a4f | 2014-08-20 19:40:59 +0100 | [diff] [blame] | 4394 | /** |
| 4395 | * @return true if all packages in enabledPackages are either in the list |
| 4396 | * permittedList or are a system app. |
| 4397 | */ |
| 4398 | private boolean checkPackagesInPermittedListOrSystem(List<String> enabledPackages, |
| 4399 | List<String> permittedList) { |
| 4400 | int userIdToCheck = UserHandle.getCallingUserId(); |
| 4401 | long id = Binder.clearCallingIdentity(); |
| 4402 | try { |
| 4403 | // If we have an enabled packages list for a managed profile the packages |
| 4404 | // we should check are installed for the parent user. |
| 4405 | UserInfo user = mUserManager.getUserInfo(userIdToCheck); |
| 4406 | if (user.isManagedProfile()) { |
| 4407 | userIdToCheck = user.profileGroupId; |
| 4408 | } |
| 4409 | |
| 4410 | IPackageManager pm = AppGlobals.getPackageManager(); |
| 4411 | for (String enabledPackage : enabledPackages) { |
| 4412 | boolean systemService = false; |
| 4413 | try { |
| 4414 | ApplicationInfo applicationInfo = pm.getApplicationInfo(enabledPackage, |
| 4415 | PackageManager.GET_UNINSTALLED_PACKAGES, userIdToCheck); |
| 4416 | systemService = (applicationInfo.flags |
| 4417 | & ApplicationInfo.FLAG_SYSTEM) != 0; |
| 4418 | } catch (RemoteException e) { |
| 4419 | Log.i(LOG_TAG, "Can't talk to package managed", e); |
| 4420 | } |
| 4421 | if (!systemService && !permittedList.contains(enabledPackage)) { |
| 4422 | return false; |
| 4423 | } |
| 4424 | } |
| 4425 | } finally { |
| 4426 | restoreCallingIdentity(id); |
| 4427 | } |
| 4428 | return true; |
| 4429 | } |
| 4430 | |
| 4431 | private AccessibilityManager getAccessibilityManagerForUser(int userId) { |
| 4432 | // Not using AccessibilityManager.getInstance because that guesses |
| 4433 | // at the user you require based on callingUid and caches for a given |
| 4434 | // process. |
| 4435 | IBinder iBinder = ServiceManager.getService(Context.ACCESSIBILITY_SERVICE); |
| 4436 | IAccessibilityManager service = iBinder == null |
| 4437 | ? null : IAccessibilityManager.Stub.asInterface(iBinder); |
| 4438 | return new AccessibilityManager(mContext, service, userId); |
| 4439 | } |
| 4440 | |
| 4441 | @Override |
| 4442 | public boolean setPermittedAccessibilityServices(ComponentName who, List packageList) { |
| 4443 | if (!mHasFeature) { |
| 4444 | return false; |
| 4445 | } |
| 4446 | if (who == null) { |
| 4447 | throw new NullPointerException("ComponentName is null"); |
| 4448 | } |
| 4449 | |
| 4450 | if (packageList != null) { |
| 4451 | int userId = UserHandle.getCallingUserId(); |
| 4452 | List<AccessibilityServiceInfo> enabledServices = null; |
| 4453 | long id = Binder.clearCallingIdentity(); |
| 4454 | try { |
| 4455 | UserInfo user = mUserManager.getUserInfo(userId); |
| 4456 | if (user.isManagedProfile()) { |
| 4457 | userId = user.profileGroupId; |
| 4458 | } |
| 4459 | AccessibilityManager accessibilityManager = getAccessibilityManagerForUser(userId); |
| 4460 | enabledServices = accessibilityManager.getEnabledAccessibilityServiceList( |
| 4461 | AccessibilityServiceInfo.FEEDBACK_ALL_MASK); |
| 4462 | } finally { |
| 4463 | restoreCallingIdentity(id); |
| 4464 | } |
| 4465 | |
| 4466 | if (enabledServices != null) { |
| 4467 | List<String> enabledPackages = new ArrayList<String>(); |
| 4468 | for (AccessibilityServiceInfo service : enabledServices) { |
| 4469 | enabledPackages.add(service.getResolveInfo().serviceInfo.packageName); |
| 4470 | } |
| 4471 | if (!checkPackagesInPermittedListOrSystem(enabledPackages, packageList)) { |
| 4472 | Slog.e(LOG_TAG, "Cannot set permitted accessibility services, " |
| 4473 | + "because it contains already enabled accesibility services."); |
| 4474 | return false; |
| 4475 | } |
| 4476 | } |
| 4477 | } |
| 4478 | |
| 4479 | synchronized (this) { |
| 4480 | ActiveAdmin admin = getActiveAdminForCallerLocked(who, |
| 4481 | DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 4482 | admin.permittedAccessiblityServices = packageList; |
| 4483 | saveSettingsLocked(UserHandle.getCallingUserId()); |
| 4484 | } |
| 4485 | return true; |
| 4486 | } |
| 4487 | |
| 4488 | @Override |
| 4489 | public List getPermittedAccessibilityServices(ComponentName who) { |
| 4490 | if (!mHasFeature) { |
| 4491 | return null; |
| 4492 | } |
| 4493 | |
| 4494 | if (who == null) { |
| 4495 | throw new NullPointerException("ComponentName is null"); |
| 4496 | } |
| 4497 | |
| 4498 | synchronized (this) { |
| 4499 | ActiveAdmin admin = getActiveAdminForCallerLocked(who, |
| 4500 | DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 4501 | return admin.permittedAccessiblityServices; |
| 4502 | } |
| 4503 | } |
| 4504 | |
| 4505 | @Override |
| 4506 | public List getPermittedAccessibilityServicesForUser(int userId) { |
| 4507 | if (!mHasFeature) { |
| 4508 | return null; |
| 4509 | } |
| 4510 | synchronized (this) { |
| 4511 | List<String> result = null; |
| 4512 | // If we have multiple profiles we return the intersection of the |
| 4513 | // permitted lists. This can happen in cases where we have a device |
| 4514 | // and profile owner. |
| 4515 | List<UserInfo> profiles = mUserManager.getProfiles(userId); |
| 4516 | final int PROFILES_SIZE = profiles.size(); |
| 4517 | for (int i = 0; i < PROFILES_SIZE; ++i) { |
| 4518 | // Just loop though all admins, only device or profiles |
| 4519 | // owners can have permitted lists set. |
Benjamin Franz | fadb2b3 | 2015-01-14 18:22:42 +0000 | [diff] [blame] | 4520 | DevicePolicyData policy = getUserDataUnchecked(profiles.get(i).id); |
Kenny Guy | fa80a4f | 2014-08-20 19:40:59 +0100 | [diff] [blame] | 4521 | final int N = policy.mAdminList.size(); |
| 4522 | for (int j = 0; j < N; j++) { |
| 4523 | ActiveAdmin admin = policy.mAdminList.get(j); |
| 4524 | List<String> fromAdmin = admin.permittedAccessiblityServices; |
| 4525 | if (fromAdmin != null) { |
| 4526 | if (result == null) { |
| 4527 | result = new ArrayList<String>(fromAdmin); |
| 4528 | } else { |
| 4529 | result.retainAll(fromAdmin); |
| 4530 | } |
| 4531 | } |
| 4532 | } |
| 4533 | } |
| 4534 | |
| 4535 | // If we have a permitted list add all system accessibility services. |
| 4536 | if (result != null) { |
| 4537 | long id = Binder.clearCallingIdentity(); |
| 4538 | try { |
| 4539 | UserInfo user = mUserManager.getUserInfo(userId); |
| 4540 | if (user.isManagedProfile()) { |
| 4541 | userId = user.profileGroupId; |
| 4542 | } |
| 4543 | AccessibilityManager accessibilityManager = |
| 4544 | getAccessibilityManagerForUser(userId); |
| 4545 | List<AccessibilityServiceInfo> installedServices = |
| 4546 | accessibilityManager.getInstalledAccessibilityServiceList(); |
| 4547 | |
| 4548 | IPackageManager pm = AppGlobals.getPackageManager(); |
| 4549 | if (installedServices != null) { |
| 4550 | for (AccessibilityServiceInfo service : installedServices) { |
| 4551 | String packageName = service.getResolveInfo().serviceInfo.packageName; |
| 4552 | try { |
| 4553 | ApplicationInfo applicationInfo = pm.getApplicationInfo(packageName, |
| 4554 | PackageManager.GET_UNINSTALLED_PACKAGES, userId); |
| 4555 | if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 4556 | result.add(packageName); |
| 4557 | } |
| 4558 | } catch (RemoteException e) { |
| 4559 | Log.i(LOG_TAG, "Accessibility service in missing package", e); |
| 4560 | } |
| 4561 | } |
| 4562 | } |
| 4563 | } finally { |
| 4564 | restoreCallingIdentity(id); |
| 4565 | } |
| 4566 | } |
| 4567 | |
| 4568 | return result; |
| 4569 | } |
| 4570 | } |
| 4571 | |
| 4572 | private boolean checkCallerIsCurrentUserOrProfile() { |
| 4573 | int callingUserId = UserHandle.getCallingUserId(); |
| 4574 | long token = Binder.clearCallingIdentity(); |
| 4575 | try { |
| 4576 | UserInfo currentUser; |
| 4577 | UserInfo callingUser = mUserManager.getUserInfo(callingUserId); |
| 4578 | try { |
| 4579 | currentUser = ActivityManagerNative.getDefault().getCurrentUser(); |
| 4580 | } catch (RemoteException e) { |
| 4581 | Slog.e(LOG_TAG, "Failed to talk to activity managed.", e); |
| 4582 | return false; |
| 4583 | } |
| 4584 | |
| 4585 | if (callingUser.isManagedProfile() && callingUser.profileGroupId != currentUser.id) { |
| 4586 | Slog.e(LOG_TAG, "Cannot set permitted input methods for managed profile " |
| 4587 | + "of a user that isn't the foreground user."); |
| 4588 | return false; |
| 4589 | } |
| 4590 | if (!callingUser.isManagedProfile() && callingUserId != currentUser.id ) { |
| 4591 | Slog.e(LOG_TAG, "Cannot set permitted input methods " |
| 4592 | + "of a user that isn't the foreground user."); |
| 4593 | return false; |
| 4594 | } |
| 4595 | } finally { |
| 4596 | Binder.restoreCallingIdentity(token); |
| 4597 | } |
| 4598 | return true; |
| 4599 | } |
| 4600 | |
| 4601 | @Override |
| 4602 | public boolean setPermittedInputMethods(ComponentName who, List packageList) { |
| 4603 | if (!mHasFeature) { |
| 4604 | return false; |
| 4605 | } |
| 4606 | if (who == null) { |
| 4607 | throw new NullPointerException("ComponentName is null"); |
| 4608 | } |
| 4609 | |
| 4610 | // TODO When InputMethodManager supports per user calls remove |
| 4611 | // this restriction. |
| 4612 | if (!checkCallerIsCurrentUserOrProfile()) { |
| 4613 | return false; |
| 4614 | } |
| 4615 | |
| 4616 | if (packageList != null) { |
| 4617 | // InputMethodManager fetches input methods for current user. |
| 4618 | // So this can only be set when calling user is the current user |
| 4619 | // or parent is current user in case of managed profiles. |
| 4620 | InputMethodManager inputMethodManager = (InputMethodManager) mContext |
| 4621 | .getSystemService(Context.INPUT_METHOD_SERVICE); |
| 4622 | List<InputMethodInfo> enabledImes = inputMethodManager.getEnabledInputMethodList(); |
| 4623 | |
| 4624 | if (enabledImes != null) { |
| 4625 | List<String> enabledPackages = new ArrayList<String>(); |
| 4626 | for (InputMethodInfo ime : enabledImes) { |
| 4627 | enabledPackages.add(ime.getPackageName()); |
| 4628 | } |
| 4629 | if (!checkPackagesInPermittedListOrSystem(enabledPackages, packageList)) { |
| 4630 | Slog.e(LOG_TAG, "Cannot set permitted input methods, " |
| 4631 | + "because it contains already enabled input method."); |
| 4632 | return false; |
| 4633 | } |
| 4634 | } |
| 4635 | } |
| 4636 | |
| 4637 | synchronized (this) { |
| 4638 | ActiveAdmin admin = getActiveAdminForCallerLocked(who, |
| 4639 | DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 4640 | admin.permittedInputMethods = packageList; |
| 4641 | saveSettingsLocked(UserHandle.getCallingUserId()); |
| 4642 | } |
| 4643 | return true; |
| 4644 | } |
| 4645 | |
| 4646 | @Override |
| 4647 | public List getPermittedInputMethods(ComponentName who) { |
| 4648 | if (!mHasFeature) { |
| 4649 | return null; |
| 4650 | } |
| 4651 | |
| 4652 | if (who == null) { |
| 4653 | throw new NullPointerException("ComponentName is null"); |
| 4654 | } |
| 4655 | |
| 4656 | synchronized (this) { |
| 4657 | ActiveAdmin admin = getActiveAdminForCallerLocked(who, |
| 4658 | DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 4659 | return admin.permittedInputMethods; |
| 4660 | } |
| 4661 | } |
| 4662 | |
| 4663 | @Override |
| 4664 | public List getPermittedInputMethodsForCurrentUser() { |
| 4665 | UserInfo currentUser; |
| 4666 | try { |
| 4667 | currentUser = ActivityManagerNative.getDefault().getCurrentUser(); |
| 4668 | } catch (RemoteException e) { |
| 4669 | Slog.e(LOG_TAG, "Failed to make remote calls to get current user", e); |
| 4670 | // Activity managed is dead, just allow all IMEs |
| 4671 | return null; |
| 4672 | } |
| 4673 | |
| 4674 | int userId = currentUser.id; |
| 4675 | synchronized (this) { |
| 4676 | List<String> result = null; |
| 4677 | // If we have multiple profiles we return the intersection of the |
| 4678 | // permitted lists. This can happen in cases where we have a device |
| 4679 | // and profile owner. |
| 4680 | List<UserInfo> profiles = mUserManager.getProfiles(userId); |
| 4681 | final int PROFILES_SIZE = profiles.size(); |
| 4682 | for (int i = 0; i < PROFILES_SIZE; ++i) { |
| 4683 | // Just loop though all admins, only device or profiles |
| 4684 | // owners can have permitted lists set. |
Benjamin Franz | fadb2b3 | 2015-01-14 18:22:42 +0000 | [diff] [blame] | 4685 | DevicePolicyData policy = getUserDataUnchecked(profiles.get(i).id); |
Kenny Guy | fa80a4f | 2014-08-20 19:40:59 +0100 | [diff] [blame] | 4686 | final int N = policy.mAdminList.size(); |
| 4687 | for (int j = 0; j < N; j++) { |
| 4688 | ActiveAdmin admin = policy.mAdminList.get(j); |
| 4689 | List<String> fromAdmin = admin.permittedInputMethods; |
| 4690 | if (fromAdmin != null) { |
| 4691 | if (result == null) { |
| 4692 | result = new ArrayList<String>(fromAdmin); |
| 4693 | } else { |
| 4694 | result.retainAll(fromAdmin); |
| 4695 | } |
| 4696 | } |
| 4697 | } |
| 4698 | } |
| 4699 | |
| 4700 | // If we have a permitted list add all system input methods. |
| 4701 | if (result != null) { |
| 4702 | InputMethodManager inputMethodManager = (InputMethodManager) mContext |
| 4703 | .getSystemService(Context.INPUT_METHOD_SERVICE); |
| 4704 | List<InputMethodInfo> imes = inputMethodManager.getInputMethodList(); |
| 4705 | long id = Binder.clearCallingIdentity(); |
| 4706 | try { |
| 4707 | IPackageManager pm = AppGlobals.getPackageManager(); |
| 4708 | if (imes != null) { |
| 4709 | for (InputMethodInfo ime : imes) { |
| 4710 | String packageName = ime.getPackageName(); |
| 4711 | try { |
| 4712 | ApplicationInfo applicationInfo = pm.getApplicationInfo( |
| 4713 | packageName, PackageManager.GET_UNINSTALLED_PACKAGES, |
| 4714 | userId); |
| 4715 | if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 4716 | result.add(packageName); |
| 4717 | } |
| 4718 | } catch (RemoteException e) { |
| 4719 | Log.i(LOG_TAG, "Input method for missing package", e); |
| 4720 | } |
| 4721 | } |
| 4722 | } |
| 4723 | } finally { |
| 4724 | restoreCallingIdentity(id); |
| 4725 | } |
| 4726 | } |
| 4727 | return result; |
| 4728 | } |
| 4729 | } |
| 4730 | |
Robin Lee | 66e5d96 | 2014-04-09 16:44:21 +0100 | [diff] [blame] | 4731 | @Override |
Julia Reynolds | 1e95839 | 2014-05-16 14:25:21 -0400 | [diff] [blame] | 4732 | public UserHandle createUser(ComponentName who, String name) { |
| 4733 | synchronized (this) { |
| 4734 | if (who == null) { |
| 4735 | throw new NullPointerException("ComponentName is null"); |
| 4736 | } |
| 4737 | getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_DEVICE_OWNER); |
| 4738 | |
| 4739 | long id = Binder.clearCallingIdentity(); |
| 4740 | try { |
| 4741 | UserInfo userInfo = mUserManager.createUser(name, 0 /* flags */); |
| 4742 | if (userInfo != null) { |
| 4743 | return userInfo.getUserHandle(); |
| 4744 | } |
| 4745 | return null; |
| 4746 | } finally { |
| 4747 | restoreCallingIdentity(id); |
| 4748 | } |
| 4749 | } |
| 4750 | } |
| 4751 | |
| 4752 | @Override |
Jason Monk | 03978a4 | 2014-06-10 15:05:30 -0400 | [diff] [blame] | 4753 | public UserHandle createAndInitializeUser(ComponentName who, String name, |
| 4754 | String ownerName, ComponentName profileOwnerComponent, Bundle adminExtras) { |
| 4755 | UserHandle user = createUser(who, name); |
Benjamin Franz | f87b174 | 2015-01-23 15:32:30 +0000 | [diff] [blame] | 4756 | if (user == null) { |
| 4757 | return null; |
| 4758 | } |
Jason Monk | 03978a4 | 2014-06-10 15:05:30 -0400 | [diff] [blame] | 4759 | long id = Binder.clearCallingIdentity(); |
| 4760 | try { |
| 4761 | String profileOwnerPkg = profileOwnerComponent.getPackageName(); |
| 4762 | final IPackageManager ipm = AppGlobals.getPackageManager(); |
| 4763 | IActivityManager activityManager = ActivityManagerNative.getDefault(); |
| 4764 | |
| 4765 | try { |
| 4766 | // Install the profile owner if not present. |
| 4767 | if (!ipm.isPackageAvailable(profileOwnerPkg, user.getIdentifier())) { |
| 4768 | ipm.installExistingPackageAsUser(profileOwnerPkg, user.getIdentifier()); |
| 4769 | } |
| 4770 | |
| 4771 | // Start user in background. |
| 4772 | activityManager.startUserInBackground(user.getIdentifier()); |
| 4773 | } catch (RemoteException e) { |
| 4774 | Slog.e(LOG_TAG, "Failed to make remote calls for configureUser", e); |
| 4775 | } |
| 4776 | |
| 4777 | setActiveAdmin(profileOwnerComponent, true, user.getIdentifier(), adminExtras); |
Amith Yamasani | bf3a946 | 2014-07-28 14:26:42 -0700 | [diff] [blame] | 4778 | setProfileOwner(profileOwnerComponent, ownerName, user.getIdentifier()); |
Jason Monk | 03978a4 | 2014-06-10 15:05:30 -0400 | [diff] [blame] | 4779 | return user; |
| 4780 | } finally { |
| 4781 | restoreCallingIdentity(id); |
| 4782 | } |
| 4783 | } |
| 4784 | |
| 4785 | @Override |
Julia Reynolds | 1e95839 | 2014-05-16 14:25:21 -0400 | [diff] [blame] | 4786 | public boolean removeUser(ComponentName who, UserHandle userHandle) { |
| 4787 | synchronized (this) { |
| 4788 | if (who == null) { |
| 4789 | throw new NullPointerException("ComponentName is null"); |
| 4790 | } |
| 4791 | getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_DEVICE_OWNER); |
| 4792 | |
| 4793 | long id = Binder.clearCallingIdentity(); |
| 4794 | try { |
| 4795 | return mUserManager.removeUser(userHandle.getIdentifier()); |
| 4796 | } finally { |
| 4797 | restoreCallingIdentity(id); |
| 4798 | } |
| 4799 | } |
| 4800 | } |
| 4801 | |
| 4802 | @Override |
Jason Monk | 582d911 | 2014-07-09 19:57:08 -0400 | [diff] [blame] | 4803 | public boolean switchUser(ComponentName who, UserHandle userHandle) { |
| 4804 | synchronized (this) { |
| 4805 | if (who == null) { |
| 4806 | throw new NullPointerException("ComponentName is null"); |
| 4807 | } |
| 4808 | getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_DEVICE_OWNER); |
| 4809 | |
| 4810 | long id = Binder.clearCallingIdentity(); |
| 4811 | try { |
| 4812 | int userId = UserHandle.USER_OWNER; |
| 4813 | if (userHandle != null) { |
| 4814 | userId = userHandle.getIdentifier(); |
| 4815 | } |
| 4816 | return ActivityManagerNative.getDefault().switchUser(userId); |
| 4817 | } catch (RemoteException e) { |
| 4818 | Log.e(LOG_TAG, "Couldn't switch user", e); |
| 4819 | return false; |
| 4820 | } finally { |
| 4821 | restoreCallingIdentity(id); |
| 4822 | } |
| 4823 | } |
| 4824 | } |
| 4825 | |
| 4826 | @Override |
Robin Lee | 66e5d96 | 2014-04-09 16:44:21 +0100 | [diff] [blame] | 4827 | public Bundle getApplicationRestrictions(ComponentName who, String packageName) { |
| 4828 | final UserHandle userHandle = new UserHandle(UserHandle.getCallingUserId()); |
| 4829 | |
| 4830 | synchronized (this) { |
| 4831 | if (who == null) { |
| 4832 | throw new NullPointerException("ComponentName is null"); |
| 4833 | } |
| 4834 | getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 4835 | |
Robin Lee | 66e5d96 | 2014-04-09 16:44:21 +0100 | [diff] [blame] | 4836 | long id = Binder.clearCallingIdentity(); |
| 4837 | try { |
Jessica Hummel | 91da58d | 2014-04-10 17:39:43 +0100 | [diff] [blame] | 4838 | return mUserManager.getApplicationRestrictions(packageName, userHandle); |
Robin Lee | 66e5d96 | 2014-04-09 16:44:21 +0100 | [diff] [blame] | 4839 | } finally { |
| 4840 | restoreCallingIdentity(id); |
| 4841 | } |
| 4842 | } |
| 4843 | } |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 4844 | |
| 4845 | @Override |
| 4846 | public void setUserRestriction(ComponentName who, String key, boolean enabled) { |
Amith Yamasani | 8cd28b5 | 2014-06-08 17:54:27 -0700 | [diff] [blame] | 4847 | final UserHandle user = new UserHandle(UserHandle.getCallingUserId()); |
| 4848 | final int userHandle = user.getIdentifier(); |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 4849 | synchronized (this) { |
| 4850 | if (who == null) { |
| 4851 | throw new NullPointerException("ComponentName is null"); |
| 4852 | } |
Julia Reynolds | 2cb384f | 2014-08-13 15:15:55 -0400 | [diff] [blame] | 4853 | ActiveAdmin activeAdmin = |
| 4854 | getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 4855 | boolean isDeviceOwner = isDeviceOwner(activeAdmin.info.getPackageName()); |
Amith Yamasani | c34dc7c | 2014-09-18 09:42:42 -0700 | [diff] [blame] | 4856 | if (!isDeviceOwner && userHandle != UserHandle.USER_OWNER |
| 4857 | && DEVICE_OWNER_USER_RESTRICTIONS.contains(key)) { |
Julia Reynolds | 2cb384f | 2014-08-13 15:15:55 -0400 | [diff] [blame] | 4858 | throw new SecurityException("Profile owners cannot set user restriction " + key); |
| 4859 | } |
Amith Yamasani | 8cd28b5 | 2014-06-08 17:54:27 -0700 | [diff] [blame] | 4860 | boolean alreadyRestricted = mUserManager.hasUserRestriction(key, user); |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 4861 | |
Julia Reynolds | b53453f | 2014-08-22 11:42:43 -0400 | [diff] [blame] | 4862 | IAudioService iAudioService = null; |
| 4863 | if (UserManager.DISALLOW_UNMUTE_MICROPHONE.equals(key) |
| 4864 | || UserManager.DISALLOW_ADJUST_VOLUME.equals(key)) { |
| 4865 | iAudioService = IAudioService.Stub.asInterface( |
| 4866 | ServiceManager.getService(Context.AUDIO_SERVICE)); |
| 4867 | } |
| 4868 | |
| 4869 | if (enabled && !alreadyRestricted) { |
| 4870 | try { |
| 4871 | if (UserManager.DISALLOW_UNMUTE_MICROPHONE.equals(key)) { |
| 4872 | iAudioService.setMicrophoneMute(true, who.getPackageName()); |
| 4873 | } else if (UserManager.DISALLOW_ADJUST_VOLUME.equals(key)) { |
| 4874 | iAudioService.setMasterMute(true, 0, who.getPackageName(), null); |
| 4875 | } |
| 4876 | } catch (RemoteException re) { |
| 4877 | Slog.e(LOG_TAG, "Failed to talk to AudioService.", re); |
| 4878 | } |
| 4879 | } |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 4880 | long id = Binder.clearCallingIdentity(); |
| 4881 | try { |
Julia Reynolds | 3d9eb78 | 2014-08-11 16:40:08 -0400 | [diff] [blame] | 4882 | if (enabled && !alreadyRestricted) { |
| 4883 | if (UserManager.DISALLOW_CONFIG_WIFI.equals(key)) { |
| 4884 | Settings.Secure.putIntForUser(mContext.getContentResolver(), |
| 4885 | Settings.Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 0, |
Amith Yamasani | 8cd28b5 | 2014-06-08 17:54:27 -0700 | [diff] [blame] | 4886 | userHandle); |
Julia Reynolds | 3d9eb78 | 2014-08-11 16:40:08 -0400 | [diff] [blame] | 4887 | } else if (UserManager.DISALLOW_USB_FILE_TRANSFER.equals(key)) { |
| 4888 | UsbManager manager = |
| 4889 | (UsbManager) mContext.getSystemService(Context.USB_SERVICE); |
| 4890 | manager.setCurrentFunction("none", false); |
| 4891 | } else if (UserManager.DISALLOW_SHARE_LOCATION.equals(key)) { |
| 4892 | Settings.Secure.putIntForUser(mContext.getContentResolver(), |
| 4893 | Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF, |
Amith Yamasani | 8cd28b5 | 2014-06-08 17:54:27 -0700 | [diff] [blame] | 4894 | userHandle); |
Julia Reynolds | 3d9eb78 | 2014-08-11 16:40:08 -0400 | [diff] [blame] | 4895 | Settings.Secure.putStringForUser(mContext.getContentResolver(), |
| 4896 | Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "", |
Amith Yamasani | 8cd28b5 | 2014-06-08 17:54:27 -0700 | [diff] [blame] | 4897 | userHandle); |
Julia Reynolds | 3d9eb78 | 2014-08-11 16:40:08 -0400 | [diff] [blame] | 4898 | } else if (UserManager.DISALLOW_DEBUGGING_FEATURES.equals(key)) { |
Amith Yamasani | 8cd28b5 | 2014-06-08 17:54:27 -0700 | [diff] [blame] | 4899 | // Only disable adb if changing for primary user, since it is global |
| 4900 | if (userHandle == UserHandle.USER_OWNER) { |
| 4901 | Settings.Global.putStringForUser(mContext.getContentResolver(), |
| 4902 | Settings.Global.ADB_ENABLED, "0", userHandle); |
| 4903 | } |
Julia Reynolds | 3d9eb78 | 2014-08-11 16:40:08 -0400 | [diff] [blame] | 4904 | } else if (UserManager.ENSURE_VERIFY_APPS.equals(key)) { |
| 4905 | Settings.Global.putStringForUser(mContext.getContentResolver(), |
| 4906 | Settings.Global.PACKAGE_VERIFIER_ENABLE, "1", |
Amith Yamasani | 8cd28b5 | 2014-06-08 17:54:27 -0700 | [diff] [blame] | 4907 | userHandle); |
Julia Reynolds | 3d9eb78 | 2014-08-11 16:40:08 -0400 | [diff] [blame] | 4908 | Settings.Global.putStringForUser(mContext.getContentResolver(), |
| 4909 | Settings.Global.PACKAGE_VERIFIER_INCLUDE_ADB, "1", |
Amith Yamasani | 8cd28b5 | 2014-06-08 17:54:27 -0700 | [diff] [blame] | 4910 | userHandle); |
Julia Reynolds | 3d9eb78 | 2014-08-11 16:40:08 -0400 | [diff] [blame] | 4911 | } else if (UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES.equals(key)) { |
| 4912 | Settings.Secure.putIntForUser(mContext.getContentResolver(), |
| 4913 | Settings.Secure.INSTALL_NON_MARKET_APPS, 0, |
Amith Yamasani | 8cd28b5 | 2014-06-08 17:54:27 -0700 | [diff] [blame] | 4914 | userHandle); |
Julia Reynolds | 3d9eb78 | 2014-08-11 16:40:08 -0400 | [diff] [blame] | 4915 | } |
| 4916 | } |
Amith Yamasani | 8cd28b5 | 2014-06-08 17:54:27 -0700 | [diff] [blame] | 4917 | mUserManager.setUserRestriction(key, enabled, user); |
Zoltan Szatmary-Ban | 871fa73 | 2015-01-16 12:36:29 +0000 | [diff] [blame] | 4918 | if (enabled != alreadyRestricted) { |
| 4919 | if (UserManager.DISALLOW_SHARE_LOCATION.equals(key)) { |
| 4920 | // Send out notifications however as some clients may want to reread the |
| 4921 | // value which actually changed due to a restriction having been applied. |
| 4922 | final String property = Settings.Secure.SYS_PROP_SETTING_VERSION; |
| 4923 | long version = SystemProperties.getLong(property, 0) + 1; |
| 4924 | SystemProperties.set(property, Long.toString(version)); |
| 4925 | |
| 4926 | final String name = Settings.Secure.LOCATION_PROVIDERS_ALLOWED; |
| 4927 | Uri url = Uri.withAppendedPath(Settings.Secure.CONTENT_URI, name); |
| 4928 | mContext.getContentResolver().notifyChange(url, null, true, userHandle); |
| 4929 | } |
| 4930 | } |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 4931 | } finally { |
| 4932 | restoreCallingIdentity(id); |
| 4933 | } |
Julia Reynolds | b53453f | 2014-08-22 11:42:43 -0400 | [diff] [blame] | 4934 | if (!enabled && alreadyRestricted) { |
| 4935 | try { |
| 4936 | if (UserManager.DISALLOW_UNMUTE_MICROPHONE.equals(key)) { |
| 4937 | iAudioService.setMicrophoneMute(false, who.getPackageName()); |
| 4938 | } else if (UserManager.DISALLOW_ADJUST_VOLUME.equals(key)) { |
| 4939 | iAudioService.setMasterMute(false, 0, who.getPackageName(), null); |
| 4940 | } |
| 4941 | } catch (RemoteException re) { |
| 4942 | Slog.e(LOG_TAG, "Failed to talk to AudioService.", re); |
| 4943 | } |
| 4944 | } |
Benjamin Franz | 0572054 | 2014-10-15 17:39:13 +0100 | [diff] [blame] | 4945 | sendChangedNotification(userHandle); |
Amith Yamasani | be46532 | 2014-04-24 13:45:17 -0700 | [diff] [blame] | 4946 | } |
| 4947 | } |
Adam Connors | 010cfd4 | 2014-04-16 12:48:13 +0100 | [diff] [blame] | 4948 | |
| 4949 | @Override |
Amith Yamasani | e5bcff6 | 2014-07-19 15:44:09 -0700 | [diff] [blame] | 4950 | public boolean setApplicationHidden(ComponentName who, String packageName, |
| 4951 | boolean hidden) { |
Julia Reynolds | 966881e | 2014-05-14 12:23:08 -0400 | [diff] [blame] | 4952 | int callingUserId = UserHandle.getCallingUserId(); |
| 4953 | synchronized (this) { |
| 4954 | if (who == null) { |
| 4955 | throw new NullPointerException("ComponentName is null"); |
| 4956 | } |
| 4957 | getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 4958 | |
| 4959 | long id = Binder.clearCallingIdentity(); |
| 4960 | try { |
| 4961 | IPackageManager pm = AppGlobals.getPackageManager(); |
Amith Yamasani | e5bcff6 | 2014-07-19 15:44:09 -0700 | [diff] [blame] | 4962 | return pm.setApplicationHiddenSettingAsUser(packageName, hidden, callingUserId); |
Julia Reynolds | 966881e | 2014-05-14 12:23:08 -0400 | [diff] [blame] | 4963 | } catch (RemoteException re) { |
| 4964 | // shouldn't happen |
Amith Yamasani | e5bcff6 | 2014-07-19 15:44:09 -0700 | [diff] [blame] | 4965 | Slog.e(LOG_TAG, "Failed to setApplicationHiddenSetting", re); |
Julia Reynolds | 966881e | 2014-05-14 12:23:08 -0400 | [diff] [blame] | 4966 | } finally { |
| 4967 | restoreCallingIdentity(id); |
| 4968 | } |
| 4969 | return false; |
| 4970 | } |
| 4971 | } |
| 4972 | |
| 4973 | @Override |
Amith Yamasani | e5bcff6 | 2014-07-19 15:44:09 -0700 | [diff] [blame] | 4974 | public boolean isApplicationHidden(ComponentName who, String packageName) { |
Julia Reynolds | 966881e | 2014-05-14 12:23:08 -0400 | [diff] [blame] | 4975 | int callingUserId = UserHandle.getCallingUserId(); |
| 4976 | synchronized (this) { |
| 4977 | if (who == null) { |
| 4978 | throw new NullPointerException("ComponentName is null"); |
| 4979 | } |
| 4980 | getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 4981 | |
| 4982 | long id = Binder.clearCallingIdentity(); |
| 4983 | try { |
| 4984 | IPackageManager pm = AppGlobals.getPackageManager(); |
Amith Yamasani | e5bcff6 | 2014-07-19 15:44:09 -0700 | [diff] [blame] | 4985 | return pm.getApplicationHiddenSettingAsUser(packageName, callingUserId); |
Julia Reynolds | 966881e | 2014-05-14 12:23:08 -0400 | [diff] [blame] | 4986 | } catch (RemoteException re) { |
| 4987 | // shouldn't happen |
Amith Yamasani | e5bcff6 | 2014-07-19 15:44:09 -0700 | [diff] [blame] | 4988 | Slog.e(LOG_TAG, "Failed to getApplicationHiddenSettingAsUser", re); |
Julia Reynolds | 966881e | 2014-05-14 12:23:08 -0400 | [diff] [blame] | 4989 | } finally { |
| 4990 | restoreCallingIdentity(id); |
| 4991 | } |
| 4992 | return false; |
| 4993 | } |
| 4994 | } |
| 4995 | |
| 4996 | @Override |
Adam Connors | 655be2a | 2014-07-14 09:01:25 +0000 | [diff] [blame] | 4997 | public void enableSystemApp(ComponentName who, String packageName) { |
| 4998 | synchronized (this) { |
| 4999 | if (who == null) { |
| 5000 | throw new NullPointerException("ComponentName is null"); |
| 5001 | } |
| 5002 | |
| 5003 | // This API can only be called by an active device admin, |
| 5004 | // so try to retrieve it to check that the caller is one. |
| 5005 | getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 5006 | |
| 5007 | int userId = UserHandle.getCallingUserId(); |
| 5008 | long id = Binder.clearCallingIdentity(); |
| 5009 | |
| 5010 | try { |
Adam Connors | 655be2a | 2014-07-14 09:01:25 +0000 | [diff] [blame] | 5011 | if (DBG) { |
| 5012 | Slog.v(LOG_TAG, "installing " + packageName + " for " |
| 5013 | + userId); |
| 5014 | } |
| 5015 | |
Jessica Hummel | dd31a10 | 2014-09-16 11:56:50 +0100 | [diff] [blame] | 5016 | UserManager um = UserManager.get(mContext); |
| 5017 | UserInfo primaryUser = um.getProfileParent(userId); |
| 5018 | |
| 5019 | // Call did not come from a managed profile |
| 5020 | if (primaryUser == null) { |
| 5021 | primaryUser = um.getUserInfo(userId); |
| 5022 | } |
| 5023 | |
Adam Connors | 655be2a | 2014-07-14 09:01:25 +0000 | [diff] [blame] | 5024 | IPackageManager pm = AppGlobals.getPackageManager(); |
| 5025 | if (!isSystemApp(pm, packageName, primaryUser.id)) { |
| 5026 | throw new IllegalArgumentException("Only system apps can be enabled this way."); |
| 5027 | } |
| 5028 | |
| 5029 | // Install the app. |
| 5030 | pm.installExistingPackageAsUser(packageName, userId); |
| 5031 | |
| 5032 | } catch (RemoteException re) { |
| 5033 | // shouldn't happen |
| 5034 | Slog.wtf(LOG_TAG, "Failed to install " + packageName, re); |
| 5035 | } finally { |
| 5036 | restoreCallingIdentity(id); |
| 5037 | } |
| 5038 | } |
| 5039 | } |
| 5040 | |
| 5041 | @Override |
| 5042 | public int enableSystemAppWithIntent(ComponentName who, Intent intent) { |
| 5043 | synchronized (this) { |
| 5044 | if (who == null) { |
| 5045 | throw new NullPointerException("ComponentName is null"); |
| 5046 | } |
| 5047 | |
| 5048 | // This API can only be called by an active device admin, |
| 5049 | // so try to retrieve it to check that the caller is one. |
| 5050 | getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 5051 | |
| 5052 | int userId = UserHandle.getCallingUserId(); |
| 5053 | long id = Binder.clearCallingIdentity(); |
| 5054 | |
| 5055 | try { |
| 5056 | UserManager um = UserManager.get(mContext); |
Adam Connors | 655be2a | 2014-07-14 09:01:25 +0000 | [diff] [blame] | 5057 | UserInfo primaryUser = um.getProfileParent(userId); |
| 5058 | |
Jessica Hummel | dd31a10 | 2014-09-16 11:56:50 +0100 | [diff] [blame] | 5059 | // Call did not come from a managed profile. |
| 5060 | if (primaryUser == null) { |
| 5061 | primaryUser = um.getUserInfo(userId); |
| 5062 | } |
| 5063 | |
Adam Connors | 655be2a | 2014-07-14 09:01:25 +0000 | [diff] [blame] | 5064 | IPackageManager pm = AppGlobals.getPackageManager(); |
| 5065 | List<ResolveInfo> activitiesToEnable = pm.queryIntentActivities(intent, |
| 5066 | intent.resolveTypeIfNeeded(mContext.getContentResolver()), |
| 5067 | 0, // no flags |
| 5068 | primaryUser.id); |
| 5069 | |
| 5070 | if (DBG) Slog.d(LOG_TAG, "Enabling system activities: " + activitiesToEnable); |
| 5071 | int numberOfAppsInstalled = 0; |
| 5072 | if (activitiesToEnable != null) { |
| 5073 | for (ResolveInfo info : activitiesToEnable) { |
| 5074 | if (info.activityInfo != null) { |
| 5075 | |
| 5076 | if (!isSystemApp(pm, info.activityInfo.packageName, primaryUser.id)) { |
| 5077 | throw new IllegalArgumentException( |
| 5078 | "Only system apps can be enabled this way."); |
| 5079 | } |
| 5080 | |
| 5081 | |
| 5082 | numberOfAppsInstalled++; |
| 5083 | pm.installExistingPackageAsUser(info.activityInfo.packageName, userId); |
| 5084 | } |
| 5085 | } |
| 5086 | } |
| 5087 | return numberOfAppsInstalled; |
| 5088 | } catch (RemoteException e) { |
| 5089 | // shouldn't happen |
| 5090 | Slog.wtf(LOG_TAG, "Failed to resolve intent for: " + intent); |
| 5091 | return 0; |
| 5092 | } finally { |
| 5093 | restoreCallingIdentity(id); |
| 5094 | } |
| 5095 | } |
| 5096 | } |
| 5097 | |
| 5098 | private boolean isSystemApp(IPackageManager pm, String packageName, int userId) |
| 5099 | throws RemoteException { |
Jessica Hummel | dd31a10 | 2014-09-16 11:56:50 +0100 | [diff] [blame] | 5100 | ApplicationInfo appInfo = pm.getApplicationInfo(packageName, GET_UNINSTALLED_PACKAGES, |
| 5101 | userId); |
Adam Connors | 655be2a | 2014-07-14 09:01:25 +0000 | [diff] [blame] | 5102 | return (appInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0; |
| 5103 | } |
| 5104 | |
| 5105 | @Override |
Sander Alewijnse | 5c02db6 | 2014-05-07 10:46:57 +0100 | [diff] [blame] | 5106 | public void setAccountManagementDisabled(ComponentName who, String accountType, |
| 5107 | boolean disabled) { |
| 5108 | if (!mHasFeature) { |
| 5109 | return; |
| 5110 | } |
| 5111 | synchronized (this) { |
| 5112 | if (who == null) { |
| 5113 | throw new NullPointerException("ComponentName is null"); |
| 5114 | } |
| 5115 | ActiveAdmin ap = getActiveAdminForCallerLocked(who, |
| 5116 | DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 5117 | if (disabled) { |
| 5118 | ap.accountTypesWithManagementDisabled.add(accountType); |
| 5119 | } else { |
| 5120 | ap.accountTypesWithManagementDisabled.remove(accountType); |
| 5121 | } |
| 5122 | saveSettingsLocked(UserHandle.getCallingUserId()); |
| 5123 | } |
| 5124 | } |
| 5125 | |
| 5126 | @Override |
| 5127 | public String[] getAccountTypesWithManagementDisabled() { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 5128 | return getAccountTypesWithManagementDisabledAsUser(UserHandle.getCallingUserId()); |
| 5129 | } |
| 5130 | |
| 5131 | @Override |
| 5132 | public String[] getAccountTypesWithManagementDisabledAsUser(int userId) { |
| 5133 | enforceCrossUserPermission(userId); |
Sander Alewijnse | 5c02db6 | 2014-05-07 10:46:57 +0100 | [diff] [blame] | 5134 | if (!mHasFeature) { |
| 5135 | return null; |
| 5136 | } |
| 5137 | synchronized (this) { |
Alexandra Gherghina | 999d394 | 2014-07-03 11:40:08 +0100 | [diff] [blame] | 5138 | DevicePolicyData policy = getUserData(userId); |
Sander Alewijnse | 5c02db6 | 2014-05-07 10:46:57 +0100 | [diff] [blame] | 5139 | final int N = policy.mAdminList.size(); |
| 5140 | HashSet<String> resultSet = new HashSet<String>(); |
| 5141 | for (int i = 0; i < N; i++) { |
| 5142 | ActiveAdmin admin = policy.mAdminList.get(i); |
| 5143 | resultSet.addAll(admin.accountTypesWithManagementDisabled); |
| 5144 | } |
| 5145 | return resultSet.toArray(new String[resultSet.size()]); |
| 5146 | } |
| 5147 | } |
justinzhang | 511e0d8 | 2014-03-24 16:09:24 -0400 | [diff] [blame] | 5148 | |
Kenny Guy | c13053b | 2014-05-29 14:17:17 +0100 | [diff] [blame] | 5149 | @Override |
Esteban Talavera | d5c5c13 | 2014-08-20 11:35:57 +0100 | [diff] [blame] | 5150 | public void setUninstallBlocked(ComponentName who, String packageName, |
| 5151 | boolean uninstallBlocked) { |
Kenny Guy | c13053b | 2014-05-29 14:17:17 +0100 | [diff] [blame] | 5152 | final int userId = UserHandle.getCallingUserId(); |
| 5153 | |
| 5154 | synchronized (this) { |
| 5155 | if (who == null) { |
| 5156 | throw new NullPointerException("ComponentName is null"); |
| 5157 | } |
| 5158 | getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 5159 | |
| 5160 | long id = Binder.clearCallingIdentity(); |
| 5161 | try { |
| 5162 | IPackageManager pm = AppGlobals.getPackageManager(); |
Esteban Talavera | d5c5c13 | 2014-08-20 11:35:57 +0100 | [diff] [blame] | 5163 | pm.setBlockUninstallForUser(packageName, uninstallBlocked, userId); |
Kenny Guy | c13053b | 2014-05-29 14:17:17 +0100 | [diff] [blame] | 5164 | } catch (RemoteException re) { |
| 5165 | // Shouldn't happen. |
| 5166 | Slog.e(LOG_TAG, "Failed to setBlockUninstallForUser", re); |
| 5167 | } finally { |
| 5168 | restoreCallingIdentity(id); |
| 5169 | } |
| 5170 | } |
| 5171 | } |
| 5172 | |
| 5173 | @Override |
Esteban Talavera | 729b2a6 | 2014-08-27 18:01:58 +0100 | [diff] [blame] | 5174 | public boolean isUninstallBlocked(ComponentName who, String packageName) { |
Rubin Xu | a97855b | 2014-11-07 05:41:00 +0000 | [diff] [blame] | 5175 | // This function should return true if and only if the package is blocked by |
| 5176 | // setUninstallBlocked(). It should still return false for other cases of blocks, such as |
| 5177 | // when the package is a system app, or when it is an active device admin. |
Kenny Guy | c13053b | 2014-05-29 14:17:17 +0100 | [diff] [blame] | 5178 | final int userId = UserHandle.getCallingUserId(); |
| 5179 | |
| 5180 | synchronized (this) { |
Rubin Xu | a97855b | 2014-11-07 05:41:00 +0000 | [diff] [blame] | 5181 | if (who != null) { |
| 5182 | getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
Kenny Guy | c13053b | 2014-05-29 14:17:17 +0100 | [diff] [blame] | 5183 | } |
Kenny Guy | c13053b | 2014-05-29 14:17:17 +0100 | [diff] [blame] | 5184 | |
| 5185 | long id = Binder.clearCallingIdentity(); |
| 5186 | try { |
| 5187 | IPackageManager pm = AppGlobals.getPackageManager(); |
| 5188 | return pm.getBlockUninstallForUser(packageName, userId); |
| 5189 | } catch (RemoteException re) { |
| 5190 | // Shouldn't happen. |
| 5191 | Slog.e(LOG_TAG, "Failed to getBlockUninstallForUser", re); |
| 5192 | } finally { |
| 5193 | restoreCallingIdentity(id); |
| 5194 | } |
| 5195 | } |
| 5196 | return false; |
| 5197 | } |
| 5198 | |
Adam Connors | 210fe21 | 2014-07-17 15:41:43 +0100 | [diff] [blame] | 5199 | @Override |
| 5200 | public void setCrossProfileCallerIdDisabled(ComponentName who, boolean disabled) { |
| 5201 | if (!mHasFeature) { |
| 5202 | return; |
| 5203 | } |
| 5204 | synchronized (this) { |
| 5205 | if (who == null) { |
| 5206 | throw new NullPointerException("ComponentName is null"); |
| 5207 | } |
| 5208 | ActiveAdmin admin = getActiveAdminForCallerLocked(who, |
| 5209 | DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 5210 | if (admin.disableCallerId != disabled) { |
| 5211 | admin.disableCallerId = disabled; |
| 5212 | saveSettingsLocked(UserHandle.getCallingUserId()); |
| 5213 | } |
| 5214 | } |
| 5215 | } |
| 5216 | |
| 5217 | @Override |
| 5218 | public boolean getCrossProfileCallerIdDisabled(ComponentName who) { |
| 5219 | if (!mHasFeature) { |
| 5220 | return false; |
| 5221 | } |
| 5222 | |
| 5223 | synchronized (this) { |
| 5224 | if (who == null) { |
| 5225 | throw new NullPointerException("ComponentName is null"); |
| 5226 | } |
| 5227 | |
| 5228 | ActiveAdmin admin = getActiveAdminForCallerLocked(who, |
| 5229 | DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 5230 | return admin.disableCallerId; |
| 5231 | } |
| 5232 | } |
| 5233 | |
| 5234 | @Override |
| 5235 | public boolean getCrossProfileCallerIdDisabledForUser(int userId) { |
Amith Yamasani | eee5094 | 2014-07-21 17:04:44 -0700 | [diff] [blame] | 5236 | // TODO: Should there be a check to make sure this relationship is within a profile group? |
| 5237 | //enforceSystemProcess("getCrossProfileCallerIdDisabled can only be called by system"); |
Adam Connors | 210fe21 | 2014-07-17 15:41:43 +0100 | [diff] [blame] | 5238 | synchronized (this) { |
| 5239 | ActiveAdmin admin = getProfileOwnerAdmin(userId); |
| 5240 | return (admin != null) ? admin.disableCallerId : false; |
| 5241 | } |
| 5242 | } |
| 5243 | |
justinzhang | 511e0d8 | 2014-03-24 16:09:24 -0400 | [diff] [blame] | 5244 | /** |
Jason Monk | d7b8621 | 2014-06-16 13:15:38 -0400 | [diff] [blame] | 5245 | * Sets which packages may enter lock task mode. |
justinzhang | 511e0d8 | 2014-03-24 16:09:24 -0400 | [diff] [blame] | 5246 | * |
Jason Monk | c5185f2 | 2014-06-24 11:12:42 -0400 | [diff] [blame] | 5247 | * This function can only be called by the device owner. |
justinzhang | 511e0d8 | 2014-03-24 16:09:24 -0400 | [diff] [blame] | 5248 | * @param components The list of components allowed to enter lock task mode. |
| 5249 | */ |
Jason Monk | 48aacba | 2014-08-13 16:29:08 -0400 | [diff] [blame] | 5250 | public void setLockTaskPackages(ComponentName who, String[] packages) throws SecurityException { |
Jason Monk | d7b8621 | 2014-06-16 13:15:38 -0400 | [diff] [blame] | 5251 | synchronized (this) { |
Jason Monk | 48aacba | 2014-08-13 16:29:08 -0400 | [diff] [blame] | 5252 | if (who == null) { |
| 5253 | throw new NullPointerException("ComponentName is null"); |
| 5254 | } |
| 5255 | getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_DEVICE_OWNER); |
justinzhang | 511e0d8 | 2014-03-24 16:09:24 -0400 | [diff] [blame] | 5256 | |
Jason Monk | 48aacba | 2014-08-13 16:29:08 -0400 | [diff] [blame] | 5257 | int userHandle = Binder.getCallingUserHandle().getIdentifier(); |
| 5258 | DevicePolicyData policy = getUserData(userHandle); |
| 5259 | policy.mLockTaskPackages.clear(); |
| 5260 | if (packages != null) { |
| 5261 | for (int j = 0; j < packages.length; j++) { |
| 5262 | String pkg = packages[j]; |
| 5263 | policy.mLockTaskPackages.add(pkg); |
Jason Monk | d7b8621 | 2014-06-16 13:15:38 -0400 | [diff] [blame] | 5264 | } |
justinzhang | 511e0d8 | 2014-03-24 16:09:24 -0400 | [diff] [blame] | 5265 | } |
Jason Monk | 48aacba | 2014-08-13 16:29:08 -0400 | [diff] [blame] | 5266 | |
| 5267 | // Store the settings persistently. |
| 5268 | saveSettingsLocked(userHandle); |
justinzhang | 511e0d8 | 2014-03-24 16:09:24 -0400 | [diff] [blame] | 5269 | } |
justinzhang | 511e0d8 | 2014-03-24 16:09:24 -0400 | [diff] [blame] | 5270 | } |
| 5271 | |
| 5272 | /** |
| 5273 | * This function returns the list of components allowed to start the task lock mode. |
| 5274 | */ |
Jason Monk | 48aacba | 2014-08-13 16:29:08 -0400 | [diff] [blame] | 5275 | public String[] getLockTaskPackages(ComponentName who) { |
Jason Monk | d7b8621 | 2014-06-16 13:15:38 -0400 | [diff] [blame] | 5276 | synchronized (this) { |
Jason Monk | 48aacba | 2014-08-13 16:29:08 -0400 | [diff] [blame] | 5277 | if (who == null) { |
| 5278 | throw new NullPointerException("ComponentName is null"); |
| 5279 | } |
| 5280 | getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_DEVICE_OWNER); |
| 5281 | |
| 5282 | int userHandle = Binder.getCallingUserHandle().getIdentifier(); |
Jason Monk | d7b8621 | 2014-06-16 13:15:38 -0400 | [diff] [blame] | 5283 | DevicePolicyData policy = getUserData(userHandle); |
| 5284 | return policy.mLockTaskPackages.toArray(new String[0]); |
| 5285 | } |
justinzhang | 511e0d8 | 2014-03-24 16:09:24 -0400 | [diff] [blame] | 5286 | } |
| 5287 | |
| 5288 | /** |
Jason Monk | d7b8621 | 2014-06-16 13:15:38 -0400 | [diff] [blame] | 5289 | * This function lets the caller know whether the given package is allowed to start the |
justinzhang | 511e0d8 | 2014-03-24 16:09:24 -0400 | [diff] [blame] | 5290 | * lock task mode. |
Jason Monk | d7b8621 | 2014-06-16 13:15:38 -0400 | [diff] [blame] | 5291 | * @param pkg The package to check |
justinzhang | 511e0d8 | 2014-03-24 16:09:24 -0400 | [diff] [blame] | 5292 | */ |
Jason Monk | d7b8621 | 2014-06-16 13:15:38 -0400 | [diff] [blame] | 5293 | public boolean isLockTaskPermitted(String pkg) { |
justinzhang | 511e0d8 | 2014-03-24 16:09:24 -0400 | [diff] [blame] | 5294 | // Get current user's devicepolicy |
| 5295 | int uid = Binder.getCallingUid(); |
| 5296 | int userHandle = UserHandle.getUserId(uid); |
| 5297 | DevicePolicyData policy = getUserData(userHandle); |
Jason Monk | d7b8621 | 2014-06-16 13:15:38 -0400 | [diff] [blame] | 5298 | synchronized (this) { |
| 5299 | for (int i = 0; i < policy.mLockTaskPackages.size(); i++) { |
| 5300 | String lockTaskPackage = policy.mLockTaskPackages.get(i); |
justinzhang | 511e0d8 | 2014-03-24 16:09:24 -0400 | [diff] [blame] | 5301 | |
Jason Monk | d7b8621 | 2014-06-16 13:15:38 -0400 | [diff] [blame] | 5302 | // If the given package equals one of the packages stored our list, |
| 5303 | // we allow this package to start lock task mode. |
| 5304 | if (lockTaskPackage.equals(pkg)) { |
| 5305 | return true; |
| 5306 | } |
justinzhang | 511e0d8 | 2014-03-24 16:09:24 -0400 | [diff] [blame] | 5307 | } |
| 5308 | } |
| 5309 | return false; |
| 5310 | } |
Julia Reynolds | da55165 | 2014-05-14 17:15:16 -0400 | [diff] [blame] | 5311 | |
| 5312 | @Override |
Jason Monk | 35c62a4 | 2014-06-17 10:24:47 -0400 | [diff] [blame] | 5313 | public void notifyLockTaskModeChanged(boolean isEnabled, String pkg, int userHandle) { |
| 5314 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 5315 | throw new SecurityException("notifyLockTaskModeChanged can only be called by system"); |
| 5316 | } |
| 5317 | synchronized (this) { |
| 5318 | final DevicePolicyData policy = getUserData(userHandle); |
| 5319 | Bundle adminExtras = new Bundle(); |
Jason Monk | 35c62a4 | 2014-06-17 10:24:47 -0400 | [diff] [blame] | 5320 | adminExtras.putString(DeviceAdminReceiver.EXTRA_LOCK_TASK_PACKAGE, pkg); |
| 5321 | for (ActiveAdmin admin : policy.mAdminList) { |
| 5322 | boolean ownsDevice = isDeviceOwner(admin.info.getPackageName()); |
| 5323 | boolean ownsProfile = (getProfileOwner(userHandle) != null |
| 5324 | && getProfileOwner(userHandle).equals(admin.info.getPackageName())); |
| 5325 | if (ownsDevice || ownsProfile) { |
Jason Monk | 48aacba | 2014-08-13 16:29:08 -0400 | [diff] [blame] | 5326 | if (isEnabled) { |
| 5327 | sendAdminCommandLocked(admin, DeviceAdminReceiver.ACTION_LOCK_TASK_ENTERING, |
| 5328 | adminExtras, null); |
| 5329 | } else { |
| 5330 | sendAdminCommandLocked(admin, DeviceAdminReceiver.ACTION_LOCK_TASK_EXITING); |
| 5331 | } |
Jason Monk | 35c62a4 | 2014-06-17 10:24:47 -0400 | [diff] [blame] | 5332 | } |
| 5333 | } |
| 5334 | } |
| 5335 | } |
| 5336 | |
| 5337 | @Override |
Julia Reynolds | da55165 | 2014-05-14 17:15:16 -0400 | [diff] [blame] | 5338 | public void setGlobalSetting(ComponentName who, String setting, String value) { |
| 5339 | final ContentResolver contentResolver = mContext.getContentResolver(); |
| 5340 | |
| 5341 | synchronized (this) { |
| 5342 | if (who == null) { |
| 5343 | throw new NullPointerException("ComponentName is null"); |
| 5344 | } |
| 5345 | getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_DEVICE_OWNER); |
| 5346 | |
Julia Reynolds | 9ed66da | 2014-08-26 15:42:03 -0400 | [diff] [blame] | 5347 | if (!GLOBAL_SETTINGS_WHITELIST.contains(setting)) { |
| 5348 | throw new SecurityException(String.format( |
| 5349 | "Permission denial: device owners cannot update %1$s", setting)); |
| 5350 | } |
| 5351 | |
Julia Reynolds | da55165 | 2014-05-14 17:15:16 -0400 | [diff] [blame] | 5352 | long id = Binder.clearCallingIdentity(); |
| 5353 | try { |
| 5354 | Settings.Global.putString(contentResolver, setting, value); |
| 5355 | } finally { |
| 5356 | restoreCallingIdentity(id); |
| 5357 | } |
| 5358 | } |
| 5359 | } |
| 5360 | |
| 5361 | @Override |
| 5362 | public void setSecureSetting(ComponentName who, String setting, String value) { |
| 5363 | int callingUserId = UserHandle.getCallingUserId(); |
| 5364 | final ContentResolver contentResolver = mContext.getContentResolver(); |
| 5365 | |
| 5366 | synchronized (this) { |
| 5367 | if (who == null) { |
| 5368 | throw new NullPointerException("ComponentName is null"); |
| 5369 | } |
Julia Reynolds | 82735bc | 2014-09-04 16:43:30 -0400 | [diff] [blame] | 5370 | ActiveAdmin activeAdmin = |
| 5371 | getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
Julia Reynolds | da55165 | 2014-05-14 17:15:16 -0400 | [diff] [blame] | 5372 | |
Julia Reynolds | 82735bc | 2014-09-04 16:43:30 -0400 | [diff] [blame] | 5373 | if (isDeviceOwner(activeAdmin.info.getPackageName())) { |
| 5374 | if (!SECURE_SETTINGS_DEVICEOWNER_WHITELIST.contains(setting)) { |
| 5375 | throw new SecurityException(String.format( |
| 5376 | "Permission denial: Device owners cannot update %1$s", setting)); |
| 5377 | } |
| 5378 | } else if (!SECURE_SETTINGS_WHITELIST.contains(setting)) { |
Julia Reynolds | 9ed66da | 2014-08-26 15:42:03 -0400 | [diff] [blame] | 5379 | throw new SecurityException(String.format( |
Julia Reynolds | 82735bc | 2014-09-04 16:43:30 -0400 | [diff] [blame] | 5380 | "Permission denial: Profile owners cannot update %1$s", setting)); |
Julia Reynolds | 9ed66da | 2014-08-26 15:42:03 -0400 | [diff] [blame] | 5381 | } |
| 5382 | |
Julia Reynolds | da55165 | 2014-05-14 17:15:16 -0400 | [diff] [blame] | 5383 | long id = Binder.clearCallingIdentity(); |
| 5384 | try { |
| 5385 | Settings.Secure.putStringForUser(contentResolver, setting, value, callingUserId); |
| 5386 | } finally { |
| 5387 | restoreCallingIdentity(id); |
| 5388 | } |
| 5389 | } |
| 5390 | } |
Julia Reynolds | 4a21b25 | 2014-06-04 11:11:43 -0400 | [diff] [blame] | 5391 | |
| 5392 | @Override |
| 5393 | public void setMasterVolumeMuted(ComponentName who, boolean on) { |
| 5394 | final ContentResolver contentResolver = mContext.getContentResolver(); |
| 5395 | |
| 5396 | synchronized (this) { |
| 5397 | if (who == null) { |
| 5398 | throw new NullPointerException("ComponentName is null"); |
| 5399 | } |
| 5400 | getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 5401 | |
| 5402 | IAudioService iAudioService = IAudioService.Stub.asInterface( |
| 5403 | ServiceManager.getService(Context.AUDIO_SERVICE)); |
| 5404 | try{ |
| 5405 | iAudioService.setMasterMute(on, 0, who.getPackageName(), null); |
| 5406 | } catch (RemoteException re) { |
| 5407 | Slog.e(LOG_TAG, "Failed to setMasterMute", re); |
| 5408 | } |
| 5409 | } |
| 5410 | } |
| 5411 | |
| 5412 | @Override |
| 5413 | public boolean isMasterVolumeMuted(ComponentName who) { |
| 5414 | final ContentResolver contentResolver = mContext.getContentResolver(); |
| 5415 | |
| 5416 | synchronized (this) { |
| 5417 | if (who == null) { |
| 5418 | throw new NullPointerException("ComponentName is null"); |
| 5419 | } |
| 5420 | getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER); |
| 5421 | |
| 5422 | AudioManager audioManager = |
| 5423 | (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); |
| 5424 | return audioManager.isMasterMute(); |
| 5425 | } |
| 5426 | } |
Amith Yamasani | ed239cb | 2014-07-16 17:26:17 -0700 | [diff] [blame] | 5427 | |
| 5428 | /** |
| 5429 | * We need to update the internal state of whether a user has completed setup once. After |
| 5430 | * that, we ignore any changes that reset the Settings.Secure.USER_SETUP_COMPLETE changes |
| 5431 | * as we don't trust any apps that might try to reset it. |
| 5432 | * <p> |
| 5433 | * Unfortunately, we don't know which user's setup state was changed, so we write all of |
| 5434 | * them. |
| 5435 | */ |
| 5436 | void updateUserSetupComplete() { |
| 5437 | List<UserInfo> users = mUserManager.getUsers(true); |
| 5438 | ContentResolver resolver = mContext.getContentResolver(); |
| 5439 | final int N = users.size(); |
| 5440 | for (int i = 0; i < N; i++) { |
| 5441 | int userHandle = users.get(i).id; |
| 5442 | if (Settings.Secure.getIntForUser(resolver, Settings.Secure.USER_SETUP_COMPLETE, 0, |
| 5443 | userHandle) != 0) { |
| 5444 | DevicePolicyData policy = getUserData(userHandle); |
Amith Yamasani | 0da218b | 2014-07-25 08:03:00 -0700 | [diff] [blame] | 5445 | if (!policy.mUserSetupComplete) { |
| 5446 | policy.mUserSetupComplete = true; |
| 5447 | synchronized (this) { |
| 5448 | saveSettingsLocked(userHandle); |
| 5449 | } |
Amith Yamasani | ed239cb | 2014-07-16 17:26:17 -0700 | [diff] [blame] | 5450 | } |
| 5451 | } |
| 5452 | } |
| 5453 | } |
| 5454 | |
| 5455 | private class SetupContentObserver extends ContentObserver { |
| 5456 | |
| 5457 | private final Uri mUserSetupComplete = Settings.Secure.getUriFor( |
| 5458 | Settings.Secure.USER_SETUP_COMPLETE); |
| 5459 | |
| 5460 | public SetupContentObserver(Handler handler) { |
| 5461 | super(handler); |
| 5462 | } |
| 5463 | |
| 5464 | void register(ContentResolver resolver) { |
| 5465 | resolver.registerContentObserver(mUserSetupComplete, false, this, UserHandle.USER_ALL); |
| 5466 | } |
| 5467 | |
| 5468 | @Override |
| 5469 | public void onChange(boolean selfChange, Uri uri) { |
| 5470 | if (mUserSetupComplete.equals(uri)) { |
| 5471 | updateUserSetupComplete(); |
| 5472 | } |
| 5473 | } |
| 5474 | } |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 5475 | |
| 5476 | private final class LocalService extends DevicePolicyManagerInternal { |
Svet Ganov | 6bd7025 | 2014-08-20 09:47:47 -0700 | [diff] [blame] | 5477 | private List<OnCrossProfileWidgetProvidersChangeListener> mWidgetProviderListeners; |
| 5478 | |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 5479 | @Override |
| 5480 | public List<String> getCrossProfileWidgetProviders(int profileId) { |
Svet Ganov | 6bd7025 | 2014-08-20 09:47:47 -0700 | [diff] [blame] | 5481 | synchronized (DevicePolicyManagerService.this) { |
Svetoslav | 28433db | 2014-10-14 15:53:14 -0700 | [diff] [blame] | 5482 | if (mDeviceOwner == null) { |
| 5483 | return Collections.emptyList(); |
| 5484 | } |
Svet Ganov | 6bd7025 | 2014-08-20 09:47:47 -0700 | [diff] [blame] | 5485 | ComponentName ownerComponent = mDeviceOwner.getProfileOwnerComponent(profileId); |
| 5486 | if (ownerComponent == null) { |
| 5487 | return Collections.emptyList(); |
| 5488 | } |
| 5489 | |
Benjamin Franz | fadb2b3 | 2015-01-14 18:22:42 +0000 | [diff] [blame] | 5490 | DevicePolicyData policy = getUserDataUnchecked(profileId); |
Svet Ganov | 6bd7025 | 2014-08-20 09:47:47 -0700 | [diff] [blame] | 5491 | ActiveAdmin admin = policy.mAdminMap.get(ownerComponent); |
| 5492 | |
| 5493 | if (admin == null || admin.crossProfileWidgetProviders == null |
| 5494 | || admin.crossProfileWidgetProviders.isEmpty()) { |
| 5495 | return Collections.emptyList(); |
| 5496 | } |
| 5497 | |
| 5498 | return admin.crossProfileWidgetProviders; |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 5499 | } |
Svet Ganov | 6bd7025 | 2014-08-20 09:47:47 -0700 | [diff] [blame] | 5500 | } |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 5501 | |
Svet Ganov | 6bd7025 | 2014-08-20 09:47:47 -0700 | [diff] [blame] | 5502 | @Override |
| 5503 | public void addOnCrossProfileWidgetProvidersChangeListener( |
| 5504 | OnCrossProfileWidgetProvidersChangeListener listener) { |
| 5505 | synchronized (DevicePolicyManagerService.this) { |
| 5506 | if (mWidgetProviderListeners == null) { |
| 5507 | mWidgetProviderListeners = new ArrayList<>(); |
| 5508 | } |
| 5509 | if (!mWidgetProviderListeners.contains(listener)) { |
| 5510 | mWidgetProviderListeners.add(listener); |
| 5511 | } |
Svetoslav | 318f91b | 2014-08-06 19:05:57 -0700 | [diff] [blame] | 5512 | } |
Svet Ganov | 6bd7025 | 2014-08-20 09:47:47 -0700 | [diff] [blame] | 5513 | } |
Svetoslav | 318f91b | 2014-08-06 19:05:57 -0700 | [diff] [blame] | 5514 | |
Svet Ganov | 6bd7025 | 2014-08-20 09:47:47 -0700 | [diff] [blame] | 5515 | private void notifyCrossProfileProvidersChanged(int userId, List<String> packages) { |
| 5516 | final List<OnCrossProfileWidgetProvidersChangeListener> listeners; |
| 5517 | synchronized (DevicePolicyManagerService.this) { |
| 5518 | listeners = new ArrayList<>(mWidgetProviderListeners); |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 5519 | } |
Svet Ganov | 6bd7025 | 2014-08-20 09:47:47 -0700 | [diff] [blame] | 5520 | final int listenerCount = listeners.size(); |
| 5521 | for (int i = 0; i < listenerCount; i++) { |
| 5522 | OnCrossProfileWidgetProvidersChangeListener listener = listeners.get(i); |
| 5523 | listener.onCrossProfileWidgetProvidersChanged(userId, packages); |
| 5524 | } |
Svetoslav | 976e8bd | 2014-07-16 15:12:03 -0700 | [diff] [blame] | 5525 | } |
| 5526 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 5527 | } |