Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.settings; |
| 18 | |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 19 | import android.app.Activity; |
Sudheer Shanka | acb1a61 | 2016-11-10 15:30:14 -0800 | [diff] [blame] | 20 | import android.app.ActivityManager; |
Dianne Hackborn | 61daf83 | 2010-01-27 16:22:05 -0800 | [diff] [blame] | 21 | import android.app.AlertDialog; |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 22 | import android.app.AppOpsManager; |
Dianne Hackborn | 61daf83 | 2010-01-27 16:22:05 -0800 | [diff] [blame] | 23 | import android.app.Dialog; |
Dianne Hackborn | 4037c7f | 2010-02-26 17:26:55 -0800 | [diff] [blame] | 24 | import android.app.admin.DeviceAdminInfo; |
| 25 | import android.app.admin.DeviceAdminReceiver; |
| 26 | import android.app.admin.DevicePolicyManager; |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 27 | import android.content.ComponentName; |
| 28 | import android.content.Context; |
Dianne Hackborn | 61daf83 | 2010-01-27 16:22:05 -0800 | [diff] [blame] | 29 | import android.content.DialogInterface; |
Dianne Hackborn | 39e64e7 | 2010-03-26 13:24:39 -0700 | [diff] [blame] | 30 | import android.content.Intent; |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 31 | import android.content.pm.ActivityInfo; |
Amith Yamasani | 080f8db | 2014-08-13 11:11:44 -0700 | [diff] [blame] | 32 | import android.content.pm.ApplicationInfo; |
| 33 | import android.content.pm.PackageInfo; |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 34 | import android.content.pm.PackageManager; |
Amith Yamasani | 080f8db | 2014-08-13 11:11:44 -0700 | [diff] [blame] | 35 | import android.content.pm.PackageManager.NameNotFoundException; |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 36 | import android.content.pm.ResolveInfo; |
Kenny Guy | 7a35559 | 2015-12-22 12:11:41 +0000 | [diff] [blame] | 37 | import android.content.pm.UserInfo; |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 38 | import android.content.res.Resources; |
Fyodor Kupolov | 1f43258 | 2015-09-18 18:22:24 -0700 | [diff] [blame] | 39 | import android.os.Binder; |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 40 | import android.os.Bundle; |
Dianne Hackborn | 61daf83 | 2010-01-27 16:22:05 -0800 | [diff] [blame] | 41 | import android.os.Handler; |
Suprabh Shukla | 05c8eb5 | 2017-05-22 15:20:06 -0700 | [diff] [blame] | 42 | import android.os.IBinder; |
Dianne Hackborn | 61daf83 | 2010-01-27 16:22:05 -0800 | [diff] [blame] | 43 | import android.os.RemoteCallback; |
Dianne Hackborn | 4f25e1d | 2011-03-02 22:39:49 -0800 | [diff] [blame] | 44 | import android.os.RemoteException; |
Amith Yamasani | b8e0f60 | 2014-07-28 16:28:36 -0700 | [diff] [blame] | 45 | import android.os.UserHandle; |
Xiaohui Chen | f9ee84d | 2015-10-21 11:25:35 -0700 | [diff] [blame] | 46 | import android.os.UserManager; |
Kenny Guy | 7a35559 | 2015-12-22 12:11:41 +0000 | [diff] [blame] | 47 | import android.text.TextUtils; |
Chris Palmer | 51794a0 | 2010-09-13 15:14:55 -0700 | [diff] [blame] | 48 | import android.text.TextUtils.TruncateAt; |
William Luh | 64e676a | 2013-11-22 15:42:40 -0800 | [diff] [blame] | 49 | import android.util.EventLog; |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 50 | import android.util.Log; |
Chris Palmer | 51794a0 | 2010-09-13 15:14:55 -0700 | [diff] [blame] | 51 | import android.view.Display; |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 52 | import android.view.View; |
| 53 | import android.view.ViewGroup; |
Fyodor Kupolov | 1c8600b | 2015-05-29 11:17:32 -0700 | [diff] [blame] | 54 | import android.view.ViewTreeObserver; |
Chris Palmer | 51794a0 | 2010-09-13 15:14:55 -0700 | [diff] [blame] | 55 | import android.view.WindowManager; |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 56 | import android.widget.AppSecurityPermissions; |
Dianne Hackborn | 61daf83 | 2010-01-27 16:22:05 -0800 | [diff] [blame] | 57 | import android.widget.Button; |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 58 | import android.widget.ImageView; |
| 59 | import android.widget.TextView; |
| 60 | |
Fyodor Kupolov | 2328946 | 2017-01-13 15:14:24 -0800 | [diff] [blame] | 61 | import com.android.internal.logging.nano.MetricsProto; |
Fyodor Kupolov | 2328946 | 2017-01-13 15:14:24 -0800 | [diff] [blame] | 62 | import com.android.settings.overlay.FeatureFactory; |
Kenny Guy | 7a35559 | 2015-12-22 12:11:41 +0000 | [diff] [blame] | 63 | import com.android.settings.users.UserDialogs; |
Nicolas Prevot | 4735488 | 2017-01-24 14:29:59 +0000 | [diff] [blame] | 64 | import com.android.settingslib.RestrictedLockUtils; |
| 65 | import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; |
Tony Mak | ee36bb9 | 2017-09-05 16:41:05 +0100 | [diff] [blame] | 66 | |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 67 | import org.xmlpull.v1.XmlPullParserException; |
| 68 | |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 69 | import java.io.IOException; |
| 70 | import java.util.ArrayList; |
Dianne Hackborn | 8274b67 | 2013-05-22 11:29:36 -0700 | [diff] [blame] | 71 | import java.util.List; |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 72 | |
| 73 | public class DeviceAdminAdd extends Activity { |
| 74 | static final String TAG = "DeviceAdminAdd"; |
William Luh | 64e676a | 2013-11-22 15:42:40 -0800 | [diff] [blame] | 75 | |
Dianne Hackborn | 61daf83 | 2010-01-27 16:22:05 -0800 | [diff] [blame] | 76 | static final int DIALOG_WARNING = 1; |
Chris Palmer | 51794a0 | 2010-09-13 15:14:55 -0700 | [diff] [blame] | 77 | |
| 78 | private static final int MAX_ADD_MSG_LINES_PORTRAIT = 5; |
| 79 | private static final int MAX_ADD_MSG_LINES_LANDSCAPE = 2; |
| 80 | private static final int MAX_ADD_MSG_LINES = 15; |
William Luh | 64e676a | 2013-11-22 15:42:40 -0800 | [diff] [blame] | 81 | |
Suprabh Shukla | 02a06ef | 2016-01-27 13:00:10 -0800 | [diff] [blame] | 82 | /** |
| 83 | * Optional key to map to the package name of the Device Admin. |
| 84 | * Currently only used when uninstalling an active device admin. |
| 85 | */ |
| 86 | public static final String EXTRA_DEVICE_ADMIN_PACKAGE_NAME = |
| 87 | "android.app.extra.DEVICE_ADMIN_PACKAGE_NAME"; |
| 88 | |
Sudheer Shanka | 17cbc99 | 2016-04-11 23:09:08 -0700 | [diff] [blame] | 89 | public static final String EXTRA_CALLED_FROM_SUPPORT_DIALOG = |
| 90 | "android.app.extra.CALLED_FROM_SUPPORT_DIALOG"; |
| 91 | |
Suprabh Shukla | 05c8eb5 | 2017-05-22 15:20:06 -0700 | [diff] [blame] | 92 | private final IBinder mToken = new Binder(); |
Dianne Hackborn | 61daf83 | 2010-01-27 16:22:05 -0800 | [diff] [blame] | 93 | Handler mHandler; |
William Luh | 64e676a | 2013-11-22 15:42:40 -0800 | [diff] [blame] | 94 | |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 95 | DevicePolicyManager mDPM; |
Dianne Hackborn | b763e34 | 2014-07-22 16:23:36 -0700 | [diff] [blame] | 96 | AppOpsManager mAppOps; |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 97 | DeviceAdminInfo mDeviceAdmin; |
Dianne Hackborn | b885afe | 2010-01-26 18:03:42 -0800 | [diff] [blame] | 98 | CharSequence mAddMsgText; |
Amith Yamasani | b8e0f60 | 2014-07-28 16:28:36 -0700 | [diff] [blame] | 99 | String mProfileOwnerName; |
William Luh | 64e676a | 2013-11-22 15:42:40 -0800 | [diff] [blame] | 100 | |
Dianne Hackborn | 61daf83 | 2010-01-27 16:22:05 -0800 | [diff] [blame] | 101 | ImageView mAdminIcon; |
| 102 | TextView mAdminName; |
| 103 | TextView mAdminDescription; |
Dianne Hackborn | b885afe | 2010-01-26 18:03:42 -0800 | [diff] [blame] | 104 | TextView mAddMsg; |
Amith Yamasani | b8e0f60 | 2014-07-28 16:28:36 -0700 | [diff] [blame] | 105 | TextView mProfileOwnerWarning; |
Amith Yamasani | a1d0184 | 2011-01-24 16:02:11 -0800 | [diff] [blame] | 106 | ImageView mAddMsgExpander; |
Chris Palmer | 51794a0 | 2010-09-13 15:14:55 -0700 | [diff] [blame] | 107 | boolean mAddMsgEllipsized = true; |
Dianne Hackborn | 61daf83 | 2010-01-27 16:22:05 -0800 | [diff] [blame] | 108 | TextView mAdminWarning; |
Kenny Guy | 7a35559 | 2015-12-22 12:11:41 +0000 | [diff] [blame] | 109 | TextView mSupportMessage; |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 110 | ViewGroup mAdminPolicies; |
Dianne Hackborn | 61daf83 | 2010-01-27 16:22:05 -0800 | [diff] [blame] | 111 | Button mActionButton; |
Makoto Onuki | c548848 | 2016-01-22 15:21:56 -0800 | [diff] [blame] | 112 | Button mUninstallButton; |
Dianne Hackborn | 97de6bc | 2010-03-03 17:18:13 -0800 | [diff] [blame] | 113 | Button mCancelButton; |
William Luh | 64e676a | 2013-11-22 15:42:40 -0800 | [diff] [blame] | 114 | |
Suprabh Shukla | 02a06ef | 2016-01-27 13:00:10 -0800 | [diff] [blame] | 115 | boolean mUninstalling = false; |
Dianne Hackborn | 61daf83 | 2010-01-27 16:22:05 -0800 | [diff] [blame] | 116 | boolean mAdding; |
Andy Stadler | 5cc0e41 | 2010-12-08 15:57:18 -0800 | [diff] [blame] | 117 | boolean mRefreshing; |
Dianne Hackborn | b763e34 | 2014-07-22 16:23:36 -0700 | [diff] [blame] | 118 | boolean mWaitingForRemoveMsg; |
Amith Yamasani | b8e0f60 | 2014-07-28 16:28:36 -0700 | [diff] [blame] | 119 | boolean mAddingProfileOwner; |
Fyodor Kupolov | 7dc56bd | 2015-02-24 09:26:53 -0800 | [diff] [blame] | 120 | boolean mAdminPoliciesInitialized; |
William Luh | 64e676a | 2013-11-22 15:42:40 -0800 | [diff] [blame] | 121 | |
Sudheer Shanka | 17cbc99 | 2016-04-11 23:09:08 -0700 | [diff] [blame] | 122 | boolean mIsCalledFromSupportDialog = false; |
| 123 | |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 124 | @Override |
| 125 | protected void onCreate(Bundle icicle) { |
| 126 | super.onCreate(icicle); |
| 127 | |
Dianne Hackborn | 61daf83 | 2010-01-27 16:22:05 -0800 | [diff] [blame] | 128 | mHandler = new Handler(getMainLooper()); |
William Luh | 64e676a | 2013-11-22 15:42:40 -0800 | [diff] [blame] | 129 | |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 130 | mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE); |
Dianne Hackborn | b763e34 | 2014-07-22 16:23:36 -0700 | [diff] [blame] | 131 | mAppOps = (AppOpsManager)getSystemService(Context.APP_OPS_SERVICE); |
Amith Yamasani | 080f8db | 2014-08-13 11:11:44 -0700 | [diff] [blame] | 132 | PackageManager packageManager = getPackageManager(); |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 133 | |
Dianne Hackborn | 39e64e7 | 2010-03-26 13:24:39 -0700 | [diff] [blame] | 134 | if ((getIntent().getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) { |
Andy Stadler | 5cc0e41 | 2010-12-08 15:57:18 -0800 | [diff] [blame] | 135 | Log.w(TAG, "Cannot start ADD_DEVICE_ADMIN as a new task"); |
Dianne Hackborn | 39e64e7 | 2010-03-26 13:24:39 -0700 | [diff] [blame] | 136 | finish(); |
| 137 | return; |
| 138 | } |
William Luh | 64e676a | 2013-11-22 15:42:40 -0800 | [diff] [blame] | 139 | |
Sudheer Shanka | 17cbc99 | 2016-04-11 23:09:08 -0700 | [diff] [blame] | 140 | mIsCalledFromSupportDialog = getIntent().getBooleanExtra( |
| 141 | EXTRA_CALLED_FROM_SUPPORT_DIALOG, false); |
Suprabh Shukla | 02a06ef | 2016-01-27 13:00:10 -0800 | [diff] [blame] | 142 | |
Amith Yamasani | b8e0f60 | 2014-07-28 16:28:36 -0700 | [diff] [blame] | 143 | String action = getIntent().getAction(); |
| 144 | ComponentName who = (ComponentName)getIntent().getParcelableExtra( |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 145 | DevicePolicyManager.EXTRA_DEVICE_ADMIN); |
Amith Yamasani | b8e0f60 | 2014-07-28 16:28:36 -0700 | [diff] [blame] | 146 | if (who == null) { |
Suprabh Shukla | 02a06ef | 2016-01-27 13:00:10 -0800 | [diff] [blame] | 147 | String packageName = getIntent().getStringExtra(EXTRA_DEVICE_ADMIN_PACKAGE_NAME); |
| 148 | for (ComponentName component : mDPM.getActiveAdmins()) { |
| 149 | if (component.getPackageName().equals(packageName)) { |
| 150 | who = component; |
| 151 | mUninstalling = true; |
| 152 | break; |
| 153 | } |
| 154 | } |
| 155 | if (who == null) { |
| 156 | Log.w(TAG, "No component specified in " + action); |
| 157 | finish(); |
| 158 | return; |
| 159 | } |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 160 | } |
Dianne Hackborn | 8274b67 | 2013-05-22 11:29:36 -0700 | [diff] [blame] | 161 | |
Amith Yamasani | b8e0f60 | 2014-07-28 16:28:36 -0700 | [diff] [blame] | 162 | if (action != null && action.equals(DevicePolicyManager.ACTION_SET_PROFILE_OWNER)) { |
Amith Yamasani | 080f8db | 2014-08-13 11:11:44 -0700 | [diff] [blame] | 163 | setResult(RESULT_CANCELED); |
| 164 | setFinishOnTouchOutside(true); |
Amith Yamasani | b8e0f60 | 2014-07-28 16:28:36 -0700 | [diff] [blame] | 165 | mAddingProfileOwner = true; |
| 166 | mProfileOwnerName = |
| 167 | getIntent().getStringExtra(DevicePolicyManager.EXTRA_PROFILE_OWNER_NAME); |
| 168 | String callingPackage = getCallingPackage(); |
| 169 | if (callingPackage == null || !callingPackage.equals(who.getPackageName())) { |
| 170 | Log.e(TAG, "Unknown or incorrect caller"); |
| 171 | finish(); |
| 172 | return; |
| 173 | } |
Amith Yamasani | 080f8db | 2014-08-13 11:11:44 -0700 | [diff] [blame] | 174 | try { |
| 175 | PackageInfo packageInfo = packageManager.getPackageInfo(callingPackage, 0); |
| 176 | if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { |
| 177 | Log.e(TAG, "Cannot set a non-system app as a profile owner"); |
| 178 | finish(); |
| 179 | return; |
| 180 | } |
| 181 | } catch (NameNotFoundException nnfe) { |
| 182 | Log.e(TAG, "Cannot find the package " + callingPackage); |
| 183 | finish(); |
| 184 | return; |
| 185 | } |
Amith Yamasani | b8e0f60 | 2014-07-28 16:28:36 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 188 | ActivityInfo ai; |
| 189 | try { |
Amith Yamasani | 080f8db | 2014-08-13 11:11:44 -0700 | [diff] [blame] | 190 | ai = packageManager.getReceiverInfo(who, PackageManager.GET_META_DATA); |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 191 | } catch (PackageManager.NameNotFoundException e) { |
Amith Yamasani | b8e0f60 | 2014-07-28 16:28:36 -0700 | [diff] [blame] | 192 | Log.w(TAG, "Unable to retrieve device policy " + who, e); |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 193 | finish(); |
| 194 | return; |
| 195 | } |
Dianne Hackborn | 8274b67 | 2013-05-22 11:29:36 -0700 | [diff] [blame] | 196 | |
Alex Klyubin | 29de9d5 | 2013-06-06 14:56:24 -0700 | [diff] [blame] | 197 | // When activating, make sure the given component name is actually a valid device admin. |
| 198 | // No need to check this when deactivating, because it is safe to deactivate an active |
| 199 | // invalid device admin. |
Amith Yamasani | b8e0f60 | 2014-07-28 16:28:36 -0700 | [diff] [blame] | 200 | if (!mDPM.isAdminActive(who)) { |
Amith Yamasani | 080f8db | 2014-08-13 11:11:44 -0700 | [diff] [blame] | 201 | List<ResolveInfo> avail = packageManager.queryBroadcastReceivers( |
Alex Klyubin | 29de9d5 | 2013-06-06 14:56:24 -0700 | [diff] [blame] | 202 | new Intent(DeviceAdminReceiver.ACTION_DEVICE_ADMIN_ENABLED), |
Alex Klyubin | 681b21f | 2013-06-06 15:45:23 -0700 | [diff] [blame] | 203 | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS); |
Alex Klyubin | 29de9d5 | 2013-06-06 14:56:24 -0700 | [diff] [blame] | 204 | int count = avail == null ? 0 : avail.size(); |
| 205 | boolean found = false; |
| 206 | for (int i=0; i<count; i++) { |
| 207 | ResolveInfo ri = avail.get(i); |
| 208 | if (ai.packageName.equals(ri.activityInfo.packageName) |
| 209 | && ai.name.equals(ri.activityInfo.name)) { |
| 210 | try { |
| 211 | // We didn't retrieve the meta data for all possible matches, so |
| 212 | // need to use the activity info of this specific one that was retrieved. |
| 213 | ri.activityInfo = ai; |
| 214 | DeviceAdminInfo dpi = new DeviceAdminInfo(this, ri); |
| 215 | found = true; |
| 216 | } catch (XmlPullParserException e) { |
| 217 | Log.w(TAG, "Bad " + ri.activityInfo, e); |
| 218 | } catch (IOException e) { |
| 219 | Log.w(TAG, "Bad " + ri.activityInfo, e); |
| 220 | } |
| 221 | break; |
Dianne Hackborn | 8274b67 | 2013-05-22 11:29:36 -0700 | [diff] [blame] | 222 | } |
Dianne Hackborn | 8274b67 | 2013-05-22 11:29:36 -0700 | [diff] [blame] | 223 | } |
Alex Klyubin | 29de9d5 | 2013-06-06 14:56:24 -0700 | [diff] [blame] | 224 | if (!found) { |
Amith Yamasani | b8e0f60 | 2014-07-28 16:28:36 -0700 | [diff] [blame] | 225 | Log.w(TAG, "Request to add invalid device admin: " + who); |
Alex Klyubin | 29de9d5 | 2013-06-06 14:56:24 -0700 | [diff] [blame] | 226 | finish(); |
| 227 | return; |
| 228 | } |
Dianne Hackborn | 8274b67 | 2013-05-22 11:29:36 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 231 | ResolveInfo ri = new ResolveInfo(); |
| 232 | ri.activityInfo = ai; |
| 233 | try { |
Andy Stadler | 5cc0e41 | 2010-12-08 15:57:18 -0800 | [diff] [blame] | 234 | mDeviceAdmin = new DeviceAdminInfo(this, ri); |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 235 | } catch (XmlPullParserException e) { |
Amith Yamasani | b8e0f60 | 2014-07-28 16:28:36 -0700 | [diff] [blame] | 236 | Log.w(TAG, "Unable to retrieve device policy " + who, e); |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 237 | finish(); |
| 238 | return; |
| 239 | } catch (IOException e) { |
Amith Yamasani | b8e0f60 | 2014-07-28 16:28:36 -0700 | [diff] [blame] | 240 | Log.w(TAG, "Unable to retrieve device policy " + who, e); |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 241 | finish(); |
| 242 | return; |
| 243 | } |
William Luh | 64e676a | 2013-11-22 15:42:40 -0800 | [diff] [blame] | 244 | |
Andy Stadler | 5cc0e41 | 2010-12-08 15:57:18 -0800 | [diff] [blame] | 245 | // This admin already exists, an we have two options at this point. If new policy |
| 246 | // bits are set, show the user the new list. If nothing has changed, simply return |
| 247 | // "OK" immediately. |
| 248 | if (DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN.equals(getIntent().getAction())) { |
| 249 | mRefreshing = false; |
Amith Yamasani | b8e0f60 | 2014-07-28 16:28:36 -0700 | [diff] [blame] | 250 | if (mDPM.isAdminActive(who)) { |
Makoto Onuki | d141856 | 2016-04-13 16:40:37 -0700 | [diff] [blame] | 251 | if (mDPM.isRemovingAdmin(who, android.os.Process.myUserHandle().getIdentifier())) { |
| 252 | Log.w(TAG, "Requested admin is already being removed: " + who); |
| 253 | finish(); |
| 254 | return; |
| 255 | } |
| 256 | |
Andy Stadler | 5cc0e41 | 2010-12-08 15:57:18 -0800 | [diff] [blame] | 257 | ArrayList<DeviceAdminInfo.PolicyInfo> newPolicies = mDeviceAdmin.getUsedPolicies(); |
| 258 | for (int i = 0; i < newPolicies.size(); i++) { |
| 259 | DeviceAdminInfo.PolicyInfo pi = newPolicies.get(i); |
Amith Yamasani | b8e0f60 | 2014-07-28 16:28:36 -0700 | [diff] [blame] | 260 | if (!mDPM.hasGrantedPolicy(who, pi.ident)) { |
Andy Stadler | 5cc0e41 | 2010-12-08 15:57:18 -0800 | [diff] [blame] | 261 | mRefreshing = true; |
| 262 | break; |
| 263 | } |
| 264 | } |
| 265 | if (!mRefreshing) { |
| 266 | // Nothing changed (or policies were removed) - return immediately |
| 267 | setResult(Activity.RESULT_OK); |
| 268 | finish(); |
| 269 | return; |
| 270 | } |
| 271 | } |
| 272 | } |
Amith Yamasani | b8e0f60 | 2014-07-28 16:28:36 -0700 | [diff] [blame] | 273 | |
| 274 | // If we're trying to add a profile owner and user setup hasn't completed yet, no |
| 275 | // need to prompt for permission. Just add and finish. |
| 276 | if (mAddingProfileOwner && !mDPM.hasUserSetupCompleted()) { |
| 277 | addAndFinish(); |
| 278 | return; |
| 279 | } |
| 280 | |
Andy Stadler | 5cc0e41 | 2010-12-08 15:57:18 -0800 | [diff] [blame] | 281 | mAddMsgText = getIntent().getCharSequenceExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION); |
Amith Yamasani | a1d0184 | 2011-01-24 16:02:11 -0800 | [diff] [blame] | 282 | |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 283 | setContentView(R.layout.device_admin_add); |
William Luh | 64e676a | 2013-11-22 15:42:40 -0800 | [diff] [blame] | 284 | |
Dianne Hackborn | 61daf83 | 2010-01-27 16:22:05 -0800 | [diff] [blame] | 285 | mAdminIcon = (ImageView)findViewById(R.id.admin_icon); |
| 286 | mAdminName = (TextView)findViewById(R.id.admin_name); |
| 287 | mAdminDescription = (TextView)findViewById(R.id.admin_description); |
Amith Yamasani | b8e0f60 | 2014-07-28 16:28:36 -0700 | [diff] [blame] | 288 | mProfileOwnerWarning = (TextView) findViewById(R.id.profile_owner_warning); |
Chris Palmer | 51794a0 | 2010-09-13 15:14:55 -0700 | [diff] [blame] | 289 | |
Dianne Hackborn | b885afe | 2010-01-26 18:03:42 -0800 | [diff] [blame] | 290 | mAddMsg = (TextView)findViewById(R.id.add_msg); |
Amith Yamasani | a1d0184 | 2011-01-24 16:02:11 -0800 | [diff] [blame] | 291 | mAddMsgExpander = (ImageView) findViewById(R.id.add_msg_expander); |
Fyodor Kupolov | 1c8600b | 2015-05-29 11:17:32 -0700 | [diff] [blame] | 292 | final View.OnClickListener onClickListener = new View.OnClickListener() { |
| 293 | @Override |
Chris Palmer | 51794a0 | 2010-09-13 15:14:55 -0700 | [diff] [blame] | 294 | public void onClick(View v) { |
Fyodor Kupolov | 1c8600b | 2015-05-29 11:17:32 -0700 | [diff] [blame] | 295 | toggleMessageEllipsis(mAddMsg); |
Chris Palmer | 51794a0 | 2010-09-13 15:14:55 -0700 | [diff] [blame] | 296 | } |
Fyodor Kupolov | 1c8600b | 2015-05-29 11:17:32 -0700 | [diff] [blame] | 297 | }; |
| 298 | mAddMsgExpander.setOnClickListener(onClickListener); |
| 299 | mAddMsg.setOnClickListener(onClickListener); |
| 300 | |
| 301 | // Determine whether the message can be collapsed - getLineCount() gives the correct |
| 302 | // number of lines only after a layout pass. |
| 303 | mAddMsg.getViewTreeObserver().addOnGlobalLayoutListener( |
| 304 | new ViewTreeObserver.OnGlobalLayoutListener() { |
| 305 | @Override |
| 306 | public void onGlobalLayout() { |
| 307 | final int maxLines = getEllipsizedLines(); |
| 308 | // hide the icon if number of visible lines does not exceed maxLines |
| 309 | boolean hideMsgExpander = mAddMsg.getLineCount() <= maxLines; |
| 310 | mAddMsgExpander.setVisibility(hideMsgExpander ? View.GONE : View.VISIBLE); |
| 311 | if (hideMsgExpander) { |
| 312 | mAddMsg.setOnClickListener(null); |
| 313 | ((View)mAddMsgExpander.getParent()).invalidate(); |
| 314 | } |
| 315 | mAddMsg.getViewTreeObserver().removeOnGlobalLayoutListener(this); |
| 316 | } |
| 317 | }); |
Chris Palmer | 51794a0 | 2010-09-13 15:14:55 -0700 | [diff] [blame] | 318 | |
| 319 | // toggleMessageEllipsis also handles initial layout: |
| 320 | toggleMessageEllipsis(mAddMsg); |
| 321 | |
Amith Yamasani | a1d0184 | 2011-01-24 16:02:11 -0800 | [diff] [blame] | 322 | mAdminWarning = (TextView) findViewById(R.id.admin_warning); |
| 323 | mAdminPolicies = (ViewGroup) findViewById(R.id.admin_policies); |
Kenny Guy | 7a35559 | 2015-12-22 12:11:41 +0000 | [diff] [blame] | 324 | mSupportMessage = (TextView) findViewById(R.id.admin_support_message); |
Makoto Onuki | c548848 | 2016-01-22 15:21:56 -0800 | [diff] [blame] | 325 | |
Amith Yamasani | a1d0184 | 2011-01-24 16:02:11 -0800 | [diff] [blame] | 326 | mCancelButton = (Button) findViewById(R.id.cancel_button); |
Stephan Huber | dc33321 | 2015-01-23 09:49:57 +0100 | [diff] [blame] | 327 | mCancelButton.setFilterTouchesWhenObscured(true); |
Dianne Hackborn | 97de6bc | 2010-03-03 17:18:13 -0800 | [diff] [blame] | 328 | mCancelButton.setOnClickListener(new View.OnClickListener() { |
| 329 | public void onClick(View v) { |
William Luh | 64e676a | 2013-11-22 15:42:40 -0800 | [diff] [blame] | 330 | EventLog.writeEvent(EventLogTags.EXP_DET_DEVICE_ADMIN_DECLINED_BY_USER, |
| 331 | mDeviceAdmin.getActivityInfo().applicationInfo.uid); |
Dianne Hackborn | 97de6bc | 2010-03-03 17:18:13 -0800 | [diff] [blame] | 332 | finish(); |
| 333 | } |
| 334 | }); |
Makoto Onuki | c548848 | 2016-01-22 15:21:56 -0800 | [diff] [blame] | 335 | |
| 336 | mUninstallButton = (Button) findViewById(R.id.uninstall_button); |
| 337 | mUninstallButton.setFilterTouchesWhenObscured(true); |
| 338 | mUninstallButton.setOnClickListener(new View.OnClickListener() { |
| 339 | public void onClick(View v) { |
| 340 | EventLog.writeEvent(EventLogTags.EXP_DET_DEVICE_ADMIN_UNINSTALLED_BY_USER, |
| 341 | mDeviceAdmin.getActivityInfo().applicationInfo.uid); |
Makoto Onuki | a31c725 | 2016-02-10 16:21:18 -0800 | [diff] [blame] | 342 | mDPM.uninstallPackageWithActiveAdmins(mDeviceAdmin.getPackageName()); |
| 343 | finish(); |
Makoto Onuki | c548848 | 2016-01-22 15:21:56 -0800 | [diff] [blame] | 344 | } |
| 345 | }); |
| 346 | |
Amith Yamasani | a1d0184 | 2011-01-24 16:02:11 -0800 | [diff] [blame] | 347 | mActionButton = (Button) findViewById(R.id.action_button); |
Nicolas Prevot | 4735488 | 2017-01-24 14:29:59 +0000 | [diff] [blame] | 348 | |
| 349 | final View restrictedAction = findViewById(R.id.restricted_action); |
| 350 | restrictedAction.setFilterTouchesWhenObscured(true); |
| 351 | restrictedAction.setOnClickListener(new View.OnClickListener() { |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 352 | public void onClick(View v) { |
Suprabh Shukla | 05c8eb5 | 2017-05-22 15:20:06 -0700 | [diff] [blame] | 353 | if (!mActionButton.isEnabled()) { |
Tony Mak | ee36bb9 | 2017-09-05 16:41:05 +0100 | [diff] [blame] | 354 | showPolicyTransparencyDialogIfRequired(); |
Suprabh Shukla | 05c8eb5 | 2017-05-22 15:20:06 -0700 | [diff] [blame] | 355 | return; |
| 356 | } |
Dianne Hackborn | 61daf83 | 2010-01-27 16:22:05 -0800 | [diff] [blame] | 357 | if (mAdding) { |
Amith Yamasani | b8e0f60 | 2014-07-28 16:28:36 -0700 | [diff] [blame] | 358 | addAndFinish(); |
Kenny Guy | 7a35559 | 2015-12-22 12:11:41 +0000 | [diff] [blame] | 359 | } else if (isManagedProfile(mDeviceAdmin) |
| 360 | && mDeviceAdmin.getComponent().equals(mDPM.getProfileOwner())) { |
| 361 | final int userId = UserHandle.myUserId(); |
| 362 | UserDialogs.createRemoveDialog(DeviceAdminAdd.this, userId, |
| 363 | new DialogInterface.OnClickListener() { |
| 364 | @Override |
| 365 | public void onClick(DialogInterface dialog, int which) { |
| 366 | UserManager um = UserManager.get(DeviceAdminAdd.this); |
| 367 | um.removeUser(userId); |
| 368 | finish(); |
| 369 | } |
| 370 | } |
Tony Mak | ee36bb9 | 2017-09-05 16:41:05 +0100 | [diff] [blame] | 371 | ).show(); |
Suprabh Shukla | 02a06ef | 2016-01-27 13:00:10 -0800 | [diff] [blame] | 372 | } else if (mUninstalling) { |
| 373 | mDPM.uninstallPackageWithActiveAdmins(mDeviceAdmin.getPackageName()); |
| 374 | finish(); |
Dianne Hackborn | b763e34 | 2014-07-22 16:23:36 -0700 | [diff] [blame] | 375 | } else if (!mWaitingForRemoveMsg) { |
Dianne Hackborn | 4f25e1d | 2011-03-02 22:39:49 -0800 | [diff] [blame] | 376 | try { |
| 377 | // Don't allow the admin to put a dialog up in front |
| 378 | // of us while we interact with the user. |
Sudheer Shanka | acb1a61 | 2016-11-10 15:30:14 -0800 | [diff] [blame] | 379 | ActivityManager.getService().stopAppSwitches(); |
Dianne Hackborn | 4f25e1d | 2011-03-02 22:39:49 -0800 | [diff] [blame] | 380 | } catch (RemoteException e) { |
| 381 | } |
Dianne Hackborn | b763e34 | 2014-07-22 16:23:36 -0700 | [diff] [blame] | 382 | mWaitingForRemoveMsg = true; |
Andrew Stadler | 4df6edb | 2010-02-05 01:00:13 -0800 | [diff] [blame] | 383 | mDPM.getRemoveWarning(mDeviceAdmin.getComponent(), |
Svet Ganov | c7f0287 | 2015-12-01 19:57:33 -0800 | [diff] [blame] | 384 | new RemoteCallback(new RemoteCallback.OnResultListener() { |
| 385 | @Override |
| 386 | public void onResult(Bundle result) { |
| 387 | CharSequence msg = result != null |
| 388 | ? result.getCharSequence( |
Dianne Hackborn | 5435511 | 2010-02-16 20:40:42 -0800 | [diff] [blame] | 389 | DeviceAdminReceiver.EXTRA_DISABLE_WARNING) |
Svet Ganov | c7f0287 | 2015-12-01 19:57:33 -0800 | [diff] [blame] | 390 | : null; |
| 391 | continueRemoveAction(msg); |
| 392 | } |
| 393 | }, mHandler)); |
Dianne Hackborn | b763e34 | 2014-07-22 16:23:36 -0700 | [diff] [blame] | 394 | // Don't want to wait too long. |
| 395 | getWindow().getDecorView().getHandler().postDelayed(new Runnable() { |
| 396 | @Override public void run() { |
| 397 | continueRemoveAction(null); |
| 398 | } |
| 399 | }, 2*1000); |
Andrew Stadler | 4df6edb | 2010-02-05 01:00:13 -0800 | [diff] [blame] | 400 | } |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 401 | } |
| 402 | }); |
| 403 | } |
William Luh | 64e676a | 2013-11-22 15:42:40 -0800 | [diff] [blame] | 404 | |
Tony Mak | ee36bb9 | 2017-09-05 16:41:05 +0100 | [diff] [blame] | 405 | /** |
| 406 | * Shows a dialog to explain why the button is disabled if required. |
| 407 | */ |
| 408 | private void showPolicyTransparencyDialogIfRequired() { |
| 409 | if (isManagedProfile(mDeviceAdmin) |
| 410 | && mDeviceAdmin.getComponent().equals(mDPM.getProfileOwner())) { |
| 411 | if (hasBaseCantRemoveProfileRestriction()) { |
| 412 | // If DISALLOW_REMOVE_MANAGED_PROFILE is set by the system, there's no |
| 413 | // point showing a dialog saying it's disabled by an admin. |
| 414 | return; |
| 415 | } |
| 416 | EnforcedAdmin enforcedAdmin = getAdminEnforcingCantRemoveProfile(); |
| 417 | if (enforcedAdmin != null) { |
| 418 | RestrictedLockUtils.sendShowAdminSupportDetailsIntent( |
| 419 | DeviceAdminAdd.this, |
| 420 | enforcedAdmin); |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
Amith Yamasani | b8e0f60 | 2014-07-28 16:28:36 -0700 | [diff] [blame] | 425 | void addAndFinish() { |
| 426 | try { |
Fyodor Kupolov | 2328946 | 2017-01-13 15:14:24 -0800 | [diff] [blame] | 427 | logSpecialPermissionChange(true, mDeviceAdmin.getComponent().getPackageName()); |
Amith Yamasani | b8e0f60 | 2014-07-28 16:28:36 -0700 | [diff] [blame] | 428 | mDPM.setActiveAdmin(mDeviceAdmin.getComponent(), mRefreshing); |
| 429 | EventLog.writeEvent(EventLogTags.EXP_DET_DEVICE_ADMIN_ACTIVATED_BY_USER, |
| 430 | mDeviceAdmin.getActivityInfo().applicationInfo.uid); |
| 431 | setResult(Activity.RESULT_OK); |
| 432 | } catch (RuntimeException e) { |
| 433 | // Something bad happened... could be that it was |
| 434 | // already set, though. |
| 435 | Log.w(TAG, "Exception trying to activate admin " |
| 436 | + mDeviceAdmin.getComponent(), e); |
| 437 | if (mDPM.isAdminActive(mDeviceAdmin.getComponent())) { |
| 438 | setResult(Activity.RESULT_OK); |
| 439 | } |
| 440 | } |
| 441 | if (mAddingProfileOwner) { |
| 442 | try { |
| 443 | mDPM.setProfileOwner(mDeviceAdmin.getComponent(), |
| 444 | mProfileOwnerName, UserHandle.myUserId()); |
| 445 | } catch (RuntimeException re) { |
| 446 | setResult(Activity.RESULT_CANCELED); |
| 447 | } |
| 448 | } |
| 449 | finish(); |
| 450 | } |
| 451 | |
Dianne Hackborn | b763e34 | 2014-07-22 16:23:36 -0700 | [diff] [blame] | 452 | void continueRemoveAction(CharSequence msg) { |
| 453 | if (!mWaitingForRemoveMsg) { |
| 454 | return; |
| 455 | } |
| 456 | mWaitingForRemoveMsg = false; |
| 457 | if (msg == null) { |
| 458 | try { |
Sudheer Shanka | acb1a61 | 2016-11-10 15:30:14 -0800 | [diff] [blame] | 459 | ActivityManager.getService().resumeAppSwitches(); |
Dianne Hackborn | b763e34 | 2014-07-22 16:23:36 -0700 | [diff] [blame] | 460 | } catch (RemoteException e) { |
| 461 | } |
Fyodor Kupolov | 2328946 | 2017-01-13 15:14:24 -0800 | [diff] [blame] | 462 | logSpecialPermissionChange(false, mDeviceAdmin.getComponent().getPackageName()); |
Dianne Hackborn | b763e34 | 2014-07-22 16:23:36 -0700 | [diff] [blame] | 463 | mDPM.removeActiveAdmin(mDeviceAdmin.getComponent()); |
| 464 | finish(); |
| 465 | } else { |
| 466 | try { |
| 467 | // Continue preventing anything from coming in front. |
Sudheer Shanka | acb1a61 | 2016-11-10 15:30:14 -0800 | [diff] [blame] | 468 | ActivityManager.getService().stopAppSwitches(); |
Dianne Hackborn | b763e34 | 2014-07-22 16:23:36 -0700 | [diff] [blame] | 469 | } catch (RemoteException e) { |
| 470 | } |
| 471 | Bundle args = new Bundle(); |
| 472 | args.putCharSequence( |
| 473 | DeviceAdminReceiver.EXTRA_DISABLE_WARNING, msg); |
| 474 | showDialog(DIALOG_WARNING, args); |
| 475 | } |
| 476 | } |
| 477 | |
Fyodor Kupolov | 2328946 | 2017-01-13 15:14:24 -0800 | [diff] [blame] | 478 | void logSpecialPermissionChange(boolean allow, String packageName) { |
| 479 | int logCategory = allow ? MetricsProto.MetricsEvent.APP_SPECIAL_PERMISSION_ADMIN_ALLOW : |
| 480 | MetricsProto.MetricsEvent.APP_SPECIAL_PERMISSION_ADMIN_DENY; |
| 481 | FeatureFactory.getFactory(this).getMetricsFeatureProvider().action(this, logCategory, packageName); |
| 482 | } |
| 483 | |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 484 | @Override |
| 485 | protected void onResume() { |
| 486 | super.onResume(); |
Suprabh Shukla | 05c8eb5 | 2017-05-22 15:20:06 -0700 | [diff] [blame] | 487 | mActionButton.setEnabled(true); |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 488 | updateInterface(); |
Suprabh Shukla | 05c8eb5 | 2017-05-22 15:20:06 -0700 | [diff] [blame] | 489 | // As long as we are running, don't let anyone overlay stuff on top of the screen. |
| 490 | mAppOps.setUserRestriction(AppOpsManager.OP_SYSTEM_ALERT_WINDOW, true, mToken); |
| 491 | mAppOps.setUserRestriction(AppOpsManager.OP_TOAST_WINDOW, true, mToken); |
| 492 | |
Dianne Hackborn | b763e34 | 2014-07-22 16:23:36 -0700 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | @Override |
| 496 | protected void onPause() { |
| 497 | super.onPause(); |
Suprabh Shukla | 05c8eb5 | 2017-05-22 15:20:06 -0700 | [diff] [blame] | 498 | // This just greys out the button. The actual listener is attached to R.id.restricted_action |
| 499 | mActionButton.setEnabled(false); |
| 500 | mAppOps.setUserRestriction(AppOpsManager.OP_SYSTEM_ALERT_WINDOW, false, mToken); |
| 501 | mAppOps.setUserRestriction(AppOpsManager.OP_TOAST_WINDOW, false, mToken); |
Dianne Hackborn | b763e34 | 2014-07-22 16:23:36 -0700 | [diff] [blame] | 502 | try { |
Sudheer Shanka | acb1a61 | 2016-11-10 15:30:14 -0800 | [diff] [blame] | 503 | ActivityManager.getService().resumeAppSwitches(); |
Dianne Hackborn | b763e34 | 2014-07-22 16:23:36 -0700 | [diff] [blame] | 504 | } catch (RemoteException e) { |
| 505 | } |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 506 | } |
William Luh | 64e676a | 2013-11-22 15:42:40 -0800 | [diff] [blame] | 507 | |
Dianne Hackborn | 61daf83 | 2010-01-27 16:22:05 -0800 | [diff] [blame] | 508 | @Override |
Sudheer Shanka | 17cbc99 | 2016-04-11 23:09:08 -0700 | [diff] [blame] | 509 | protected void onUserLeaveHint() { |
| 510 | super.onUserLeaveHint(); |
| 511 | // In case this is triggered from support dialog, finish this activity once the user leaves |
| 512 | // so that this won't appear as a background next time support dialog is triggered. This |
| 513 | // is because the support dialog activity and this belong to the same task and we can't |
| 514 | // start this in new activity since we need to know the calling package in this activity. |
| 515 | if (mIsCalledFromSupportDialog) { |
| 516 | finish(); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | @Override |
Dianne Hackborn | 61daf83 | 2010-01-27 16:22:05 -0800 | [diff] [blame] | 521 | protected Dialog onCreateDialog(int id, Bundle args) { |
| 522 | switch (id) { |
| 523 | case DIALOG_WARNING: { |
Dianne Hackborn | 5435511 | 2010-02-16 20:40:42 -0800 | [diff] [blame] | 524 | CharSequence msg = args.getCharSequence(DeviceAdminReceiver.EXTRA_DISABLE_WARNING); |
Fyodor Kupolov | 7dc56bd | 2015-02-24 09:26:53 -0800 | [diff] [blame] | 525 | AlertDialog.Builder builder = new AlertDialog.Builder(this); |
Dianne Hackborn | 61daf83 | 2010-01-27 16:22:05 -0800 | [diff] [blame] | 526 | builder.setMessage(msg); |
| 527 | builder.setPositiveButton(R.string.dlg_ok, |
| 528 | new DialogInterface.OnClickListener() { |
| 529 | public void onClick(DialogInterface dialog, int which) { |
Dianne Hackborn | b763e34 | 2014-07-22 16:23:36 -0700 | [diff] [blame] | 530 | try { |
Sudheer Shanka | acb1a61 | 2016-11-10 15:30:14 -0800 | [diff] [blame] | 531 | ActivityManager.getService().resumeAppSwitches(); |
Dianne Hackborn | b763e34 | 2014-07-22 16:23:36 -0700 | [diff] [blame] | 532 | } catch (RemoteException e) { |
| 533 | } |
Dianne Hackborn | 61daf83 | 2010-01-27 16:22:05 -0800 | [diff] [blame] | 534 | mDPM.removeActiveAdmin(mDeviceAdmin.getComponent()); |
| 535 | finish(); |
| 536 | } |
| 537 | }); |
| 538 | builder.setNegativeButton(R.string.dlg_cancel, null); |
| 539 | return builder.create(); |
| 540 | } |
| 541 | default: |
| 542 | return super.onCreateDialog(id, args); |
William Luh | 64e676a | 2013-11-22 15:42:40 -0800 | [diff] [blame] | 543 | |
Dianne Hackborn | 61daf83 | 2010-01-27 16:22:05 -0800 | [diff] [blame] | 544 | } |
| 545 | } |
William Luh | 64e676a | 2013-11-22 15:42:40 -0800 | [diff] [blame] | 546 | |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 547 | void updateInterface() { |
Nicolas Prevot | 4735488 | 2017-01-24 14:29:59 +0000 | [diff] [blame] | 548 | findViewById(R.id.restricted_icon).setVisibility(View.GONE); |
Dianne Hackborn | 61daf83 | 2010-01-27 16:22:05 -0800 | [diff] [blame] | 549 | mAdminIcon.setImageDrawable(mDeviceAdmin.loadIcon(getPackageManager())); |
| 550 | mAdminName.setText(mDeviceAdmin.loadLabel(getPackageManager())); |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 551 | try { |
Dianne Hackborn | 61daf83 | 2010-01-27 16:22:05 -0800 | [diff] [blame] | 552 | mAdminDescription.setText( |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 553 | mDeviceAdmin.loadDescription(getPackageManager())); |
Dianne Hackborn | 61daf83 | 2010-01-27 16:22:05 -0800 | [diff] [blame] | 554 | mAdminDescription.setVisibility(View.VISIBLE); |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 555 | } catch (Resources.NotFoundException e) { |
Dianne Hackborn | 61daf83 | 2010-01-27 16:22:05 -0800 | [diff] [blame] | 556 | mAdminDescription.setVisibility(View.GONE); |
Dianne Hackborn | b885afe | 2010-01-26 18:03:42 -0800 | [diff] [blame] | 557 | } |
Amith Yamasani | b8e0f60 | 2014-07-28 16:28:36 -0700 | [diff] [blame] | 558 | if (mAddingProfileOwner) { |
| 559 | mProfileOwnerWarning.setVisibility(View.VISIBLE); |
| 560 | } |
Dianne Hackborn | b885afe | 2010-01-26 18:03:42 -0800 | [diff] [blame] | 561 | if (mAddMsgText != null) { |
| 562 | mAddMsg.setText(mAddMsgText); |
| 563 | mAddMsg.setVisibility(View.VISIBLE); |
| 564 | } else { |
| 565 | mAddMsg.setVisibility(View.GONE); |
Amith Yamasani | a1d0184 | 2011-01-24 16:02:11 -0800 | [diff] [blame] | 566 | mAddMsgExpander.setVisibility(View.GONE); |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 567 | } |
Amith Yamasani | b8e0f60 | 2014-07-28 16:28:36 -0700 | [diff] [blame] | 568 | if (!mRefreshing && !mAddingProfileOwner |
| 569 | && mDPM.isAdminActive(mDeviceAdmin.getComponent())) { |
Dianne Hackborn | 61daf83 | 2010-01-27 16:22:05 -0800 | [diff] [blame] | 570 | mAdding = false; |
Kenny Guy | 7a35559 | 2015-12-22 12:11:41 +0000 | [diff] [blame] | 571 | final boolean isProfileOwner = |
| 572 | mDeviceAdmin.getComponent().equals(mDPM.getProfileOwner()); |
| 573 | final boolean isManagedProfile = isManagedProfile(mDeviceAdmin); |
| 574 | if (isProfileOwner && isManagedProfile) { |
| 575 | // Profile owner in a managed profile, user can remove profile to disable admin. |
| 576 | mAdminWarning.setText(R.string.admin_profile_owner_message); |
| 577 | mActionButton.setText(R.string.remove_managed_profile_label); |
Nicolas Prevot | 4735488 | 2017-01-24 14:29:59 +0000 | [diff] [blame] | 578 | |
| 579 | final EnforcedAdmin admin = getAdminEnforcingCantRemoveProfile(); |
| 580 | final boolean hasBaseRestriction = hasBaseCantRemoveProfileRestriction(); |
| 581 | if (admin != null && !hasBaseRestriction) { |
| 582 | findViewById(R.id.restricted_icon).setVisibility(View.VISIBLE); |
| 583 | } |
| 584 | mActionButton.setEnabled(admin == null && !hasBaseRestriction); |
Kenny Guy | 7a35559 | 2015-12-22 12:11:41 +0000 | [diff] [blame] | 585 | } else if (isProfileOwner || mDeviceAdmin.getComponent().equals( |
| 586 | mDPM.getDeviceOwnerComponentOnCallingUser())) { |
| 587 | // Profile owner in a user or device owner, user can't disable admin. |
| 588 | if (isProfileOwner) { |
| 589 | // Show profile owner in a user description. |
| 590 | mAdminWarning.setText(R.string.admin_profile_owner_user_message); |
| 591 | } else { |
| 592 | // Show device owner description. |
| 593 | mAdminWarning.setText(R.string.admin_device_owner_message); |
| 594 | } |
| 595 | mActionButton.setText(R.string.remove_device_admin); |
| 596 | mActionButton.setEnabled(false); |
| 597 | } else { |
| 598 | addDeviceAdminPolicies(false /* showDescription */); |
| 599 | mAdminWarning.setText(getString(R.string.device_admin_status, |
| 600 | mDeviceAdmin.getActivityInfo().applicationInfo.loadLabel( |
| 601 | getPackageManager()))); |
| 602 | setTitle(R.string.active_device_admin_msg); |
Suprabh Shukla | 02a06ef | 2016-01-27 13:00:10 -0800 | [diff] [blame] | 603 | if (mUninstalling) { |
| 604 | mActionButton.setText(R.string.remove_and_uninstall_device_admin); |
| 605 | } else { |
| 606 | mActionButton.setText(R.string.remove_device_admin); |
| 607 | } |
Kenny Guy | 7a35559 | 2015-12-22 12:11:41 +0000 | [diff] [blame] | 608 | } |
Sudheer Shanka | 4f0c7d3 | 2016-04-11 22:29:27 -0700 | [diff] [blame] | 609 | CharSequence supportMessage = mDPM.getLongSupportMessageForUser( |
Kenny Guy | 7a35559 | 2015-12-22 12:11:41 +0000 | [diff] [blame] | 610 | mDeviceAdmin.getComponent(), UserHandle.myUserId()); |
| 611 | if (!TextUtils.isEmpty(supportMessage)) { |
| 612 | mSupportMessage.setText(supportMessage); |
| 613 | mSupportMessage.setVisibility(View.VISIBLE); |
| 614 | } else { |
| 615 | mSupportMessage.setVisibility(View.GONE); |
| 616 | } |
Dianne Hackborn | 61daf83 | 2010-01-27 16:22:05 -0800 | [diff] [blame] | 617 | } else { |
Fyodor Kupolov | 7dc56bd | 2015-02-24 09:26:53 -0800 | [diff] [blame] | 618 | addDeviceAdminPolicies(true /* showDescription */); |
Dianne Hackborn | 61daf83 | 2010-01-27 16:22:05 -0800 | [diff] [blame] | 619 | mAdminWarning.setText(getString(R.string.device_admin_warning, |
| 620 | mDeviceAdmin.getActivityInfo().applicationInfo.loadLabel(getPackageManager()))); |
Amith Yamasani | b8e0f60 | 2014-07-28 16:28:36 -0700 | [diff] [blame] | 621 | if (mAddingProfileOwner) { |
| 622 | setTitle(getText(R.string.profile_owner_add_title)); |
| 623 | } else { |
| 624 | setTitle(getText(R.string.add_device_admin_msg)); |
| 625 | } |
Dianne Hackborn | 61daf83 | 2010-01-27 16:22:05 -0800 | [diff] [blame] | 626 | mActionButton.setText(getText(R.string.add_device_admin)); |
Makoto Onuki | c548848 | 2016-01-22 15:21:56 -0800 | [diff] [blame] | 627 | if (isAdminUninstallable()) { |
| 628 | mUninstallButton.setVisibility(View.VISIBLE); |
| 629 | } |
Kenny Guy | 7a35559 | 2015-12-22 12:11:41 +0000 | [diff] [blame] | 630 | mSupportMessage.setVisibility(View.GONE); |
Dianne Hackborn | 61daf83 | 2010-01-27 16:22:05 -0800 | [diff] [blame] | 631 | mAdding = true; |
| 632 | } |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 633 | } |
Chris Palmer | 51794a0 | 2010-09-13 15:14:55 -0700 | [diff] [blame] | 634 | |
Nicolas Prevot | 4735488 | 2017-01-24 14:29:59 +0000 | [diff] [blame] | 635 | private EnforcedAdmin getAdminEnforcingCantRemoveProfile() { |
| 636 | // Removing a managed profile is disallowed if DISALLOW_REMOVE_MANAGED_PROFILE |
| 637 | // is set in the parent rather than the user itself. |
| 638 | return RestrictedLockUtils.checkIfRestrictionEnforced(this, |
| 639 | UserManager.DISALLOW_REMOVE_MANAGED_PROFILE, getParentUserId()); |
| 640 | } |
| 641 | |
| 642 | private boolean hasBaseCantRemoveProfileRestriction() { |
| 643 | return RestrictedLockUtils.hasBaseUserRestriction(this, |
| 644 | UserManager.DISALLOW_REMOVE_MANAGED_PROFILE, getParentUserId()); |
| 645 | } |
| 646 | |
| 647 | private int getParentUserId() { |
| 648 | return UserManager.get(this).getProfileParent(UserHandle.myUserId()).id; |
| 649 | } |
| 650 | |
Fyodor Kupolov | 7dc56bd | 2015-02-24 09:26:53 -0800 | [diff] [blame] | 651 | private void addDeviceAdminPolicies(boolean showDescription) { |
| 652 | if (!mAdminPoliciesInitialized) { |
Xiaohui Chen | f9ee84d | 2015-10-21 11:25:35 -0700 | [diff] [blame] | 653 | boolean isAdminUser = UserManager.get(this).isAdminUser(); |
Fyodor Kupolov | 7dc56bd | 2015-02-24 09:26:53 -0800 | [diff] [blame] | 654 | for (DeviceAdminInfo.PolicyInfo pi : mDeviceAdmin.getUsedPolicies()) { |
Xiaohui Chen | f9ee84d | 2015-10-21 11:25:35 -0700 | [diff] [blame] | 655 | int descriptionId = isAdminUser ? pi.description : pi.descriptionForSecondaryUsers; |
| 656 | int labelId = isAdminUser ? pi.label : pi.labelForSecondaryUsers; |
Fyodor Kupolov | 7dc56bd | 2015-02-24 09:26:53 -0800 | [diff] [blame] | 657 | View view = AppSecurityPermissions.getPermissionItemView(this, getText(labelId), |
| 658 | showDescription ? getText(descriptionId) : "", true); |
| 659 | mAdminPolicies.addView(view); |
| 660 | } |
| 661 | mAdminPoliciesInitialized = true; |
| 662 | } |
| 663 | } |
Chris Palmer | 51794a0 | 2010-09-13 15:14:55 -0700 | [diff] [blame] | 664 | |
| 665 | void toggleMessageEllipsis(View v) { |
| 666 | TextView tv = (TextView) v; |
| 667 | |
| 668 | mAddMsgEllipsized = ! mAddMsgEllipsized; |
| 669 | tv.setEllipsize(mAddMsgEllipsized ? TruncateAt.END : null); |
| 670 | tv.setMaxLines(mAddMsgEllipsized ? getEllipsizedLines() : MAX_ADD_MSG_LINES); |
| 671 | |
Amith Yamasani | a1d0184 | 2011-01-24 16:02:11 -0800 | [diff] [blame] | 672 | mAddMsgExpander.setImageResource(mAddMsgEllipsized ? |
Chris Palmer | 51794a0 | 2010-09-13 15:14:55 -0700 | [diff] [blame] | 673 | com.android.internal.R.drawable.expander_ic_minimized : |
| 674 | com.android.internal.R.drawable.expander_ic_maximized); |
| 675 | } |
| 676 | |
| 677 | int getEllipsizedLines() { |
| 678 | Display d = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)) |
| 679 | .getDefaultDisplay(); |
| 680 | |
| 681 | return d.getHeight() > d.getWidth() ? |
| 682 | MAX_ADD_MSG_LINES_PORTRAIT : MAX_ADD_MSG_LINES_LANDSCAPE; |
| 683 | } |
| 684 | |
Kenny Guy | 7a35559 | 2015-12-22 12:11:41 +0000 | [diff] [blame] | 685 | /** |
| 686 | * @return true if adminInfo is running in a managed profile. |
| 687 | */ |
| 688 | private boolean isManagedProfile(DeviceAdminInfo adminInfo) { |
| 689 | UserManager um = UserManager.get(this); |
| 690 | UserInfo info = um.getUserInfo( |
| 691 | UserHandle.getUserId(adminInfo.getActivityInfo().applicationInfo.uid)); |
| 692 | return info != null ? info.isManagedProfile() : false; |
| 693 | } |
Makoto Onuki | c548848 | 2016-01-22 15:21:56 -0800 | [diff] [blame] | 694 | |
| 695 | private boolean isAdminUninstallable() { |
| 696 | // System apps can't be uninstalled. |
| 697 | return !mDeviceAdmin.getActivityInfo().applicationInfo.isSystemApp(); |
| 698 | } |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 699 | } |