Dianne Hackborn | feff652 | 2010-01-12 18:18:05 -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 | feff652 | 2010-01-12 18:18:05 -0800 | [diff] [blame] | 19 | import android.app.Activity; |
Makoto Onuki | 7cdaa52 | 2016-02-24 12:56:44 -0800 | [diff] [blame] | 20 | import android.app.AppGlobals; |
Amith Yamasani | a1d0184 | 2011-01-24 16:02:11 -0800 | [diff] [blame] | 21 | import android.app.ListFragment; |
Dianne Hackborn | 4037c7f | 2010-02-26 17:26:55 -0800 | [diff] [blame] | 22 | import android.app.admin.DeviceAdminInfo; |
| 23 | import android.app.admin.DeviceAdminReceiver; |
| 24 | import android.app.admin.DevicePolicyManager; |
Fyodor Kupolov | cfae855 | 2014-11-25 17:34:22 -0800 | [diff] [blame] | 25 | import android.content.BroadcastReceiver; |
Dianne Hackborn | feff652 | 2010-01-12 18:18:05 -0800 | [diff] [blame] | 26 | import android.content.ComponentName; |
| 27 | import android.content.Context; |
| 28 | import android.content.Intent; |
Fyodor Kupolov | cfae855 | 2014-11-25 17:34:22 -0800 | [diff] [blame] | 29 | import android.content.IntentFilter; |
Makoto Onuki | 7cdaa52 | 2016-02-24 12:56:44 -0800 | [diff] [blame] | 30 | import android.content.pm.ActivityInfo; |
| 31 | import android.content.pm.IPackageManager; |
Dianne Hackborn | feff652 | 2010-01-12 18:18:05 -0800 | [diff] [blame] | 32 | import android.content.pm.PackageManager; |
| 33 | import android.content.pm.ResolveInfo; |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 34 | import android.content.res.Resources; |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 35 | import android.graphics.drawable.Drawable; |
Dianne Hackborn | feff652 | 2010-01-12 18:18:05 -0800 | [diff] [blame] | 36 | import android.os.Bundle; |
Makoto Onuki | 7cdaa52 | 2016-02-24 12:56:44 -0800 | [diff] [blame] | 37 | import android.os.RemoteException; |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 38 | import android.os.UserHandle; |
| 39 | import android.os.UserManager; |
Dianne Hackborn | feff652 | 2010-01-12 18:18:05 -0800 | [diff] [blame] | 40 | import android.util.Log; |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 41 | import android.util.SparseArray; |
Dianne Hackborn | feff652 | 2010-01-12 18:18:05 -0800 | [diff] [blame] | 42 | import android.view.LayoutInflater; |
| 43 | import android.view.View; |
| 44 | import android.view.ViewGroup; |
| 45 | import android.widget.BaseAdapter; |
Dianne Hackborn | 61daf83 | 2010-01-27 16:22:05 -0800 | [diff] [blame] | 46 | import android.widget.CheckBox; |
Dianne Hackborn | feff652 | 2010-01-12 18:18:05 -0800 | [diff] [blame] | 47 | import android.widget.ImageView; |
| 48 | import android.widget.ListView; |
| 49 | import android.widget.TextView; |
| 50 | |
Tamas Berghammer | 265d3c2 | 2016-06-22 15:34:45 +0100 | [diff] [blame] | 51 | import com.android.internal.logging.nano.MetricsProto; |
Doris Ling | 03a3b51 | 2017-10-18 14:25:01 -0700 | [diff] [blame] | 52 | import com.android.settings.core.InstrumentedPreferenceFragment; |
Fan Zhang | 6507613 | 2016-08-08 10:25:13 -0700 | [diff] [blame] | 53 | import com.android.settings.core.instrumentation.Instrumentable; |
Fan Zhang | 45fb119 | 2016-09-20 09:05:27 -0700 | [diff] [blame] | 54 | import com.android.settings.core.instrumentation.VisibilityLoggerMixin; |
Fan Zhang | 6507613 | 2016-08-08 10:25:13 -0700 | [diff] [blame] | 55 | |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 56 | import org.xmlpull.v1.XmlPullParserException; |
| 57 | |
Dianne Hackborn | feff652 | 2010-01-12 18:18:05 -0800 | [diff] [blame] | 58 | import java.io.IOException; |
| 59 | import java.util.ArrayList; |
Fyodor Kupolov | cfae855 | 2014-11-25 17:34:22 -0800 | [diff] [blame] | 60 | import java.util.Collection; |
Alex Klyubin | d14258e | 2013-05-22 09:06:35 -0700 | [diff] [blame] | 61 | import java.util.Collections; |
Dianne Hackborn | feff652 | 2010-01-12 18:18:05 -0800 | [diff] [blame] | 62 | import java.util.List; |
| 63 | |
Fan Zhang | 6507613 | 2016-08-08 10:25:13 -0700 | [diff] [blame] | 64 | public class DeviceAdminSettings extends ListFragment implements Instrumentable { |
Dianne Hackborn | feff652 | 2010-01-12 18:18:05 -0800 | [diff] [blame] | 65 | static final String TAG = "DeviceAdminSettings"; |
Amith Yamasani | b8e0f60 | 2014-07-28 16:28:36 -0700 | [diff] [blame] | 66 | |
Fan Zhang | 6507613 | 2016-08-08 10:25:13 -0700 | [diff] [blame] | 67 | private final VisibilityLoggerMixin mVisibilityLoggerMixin = |
Fan Zhang | ea02415 | 2016-08-22 15:20:22 -0700 | [diff] [blame] | 68 | new VisibilityLoggerMixin(getMetricsCategory()); |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 69 | private DevicePolicyManager mDPM; |
| 70 | private UserManager mUm; |
Amith Yamasani | b8e0f60 | 2014-07-28 16:28:36 -0700 | [diff] [blame] | 71 | |
Kenny Guy | 7a35559 | 2015-12-22 12:11:41 +0000 | [diff] [blame] | 72 | private static class DeviceAdminListItem implements Comparable<DeviceAdminListItem> { |
| 73 | public DeviceAdminInfo info; |
| 74 | |
| 75 | // These aren't updated so they keep a stable sort order if user activates / de-activates |
| 76 | // an admin. |
| 77 | public String name; |
| 78 | public boolean active; |
| 79 | |
| 80 | public int compareTo(DeviceAdminListItem other) { |
| 81 | // Sort active admins first, then by name. |
| 82 | if (this.active != other.active) { |
| 83 | return this.active ? -1 : 1; |
| 84 | } |
| 85 | return this.name.compareTo(other.name); |
| 86 | } |
| 87 | } |
Fan Zhang | 45fb119 | 2016-09-20 09:05:27 -0700 | [diff] [blame] | 88 | |
| 89 | @Override |
| 90 | public void onAttach(Context context) { |
| 91 | super.onAttach(context); |
| 92 | mVisibilityLoggerMixin.onAttach(context); |
| 93 | } |
| 94 | |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 95 | /** |
| 96 | * Internal collection of device admin info objects for all profiles associated with the current |
| 97 | * user. |
| 98 | */ |
Kenny Guy | 7a35559 | 2015-12-22 12:11:41 +0000 | [diff] [blame] | 99 | private final ArrayList<DeviceAdminListItem> |
| 100 | mAdmins = new ArrayList<DeviceAdminListItem>(); |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 101 | |
| 102 | private String mDeviceOwnerPkg; |
| 103 | private SparseArray<ComponentName> mProfileOwnerComponents = new SparseArray<ComponentName>(); |
Dianne Hackborn | feff652 | 2010-01-12 18:18:05 -0800 | [diff] [blame] | 104 | |
Fyodor Kupolov | cfae855 | 2014-11-25 17:34:22 -0800 | [diff] [blame] | 105 | private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| 106 | @Override |
| 107 | public void onReceive(Context context, Intent intent) { |
| 108 | // Refresh the list, if state change has been received. It could be that checkboxes |
| 109 | // need to be updated |
| 110 | if (DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED.equals( |
| 111 | intent.getAction())) { |
| 112 | updateList(); |
| 113 | } |
| 114 | } |
| 115 | }; |
| 116 | |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 117 | @Override |
Fan Zhang | 6507613 | 2016-08-08 10:25:13 -0700 | [diff] [blame] | 118 | public int getMetricsCategory() { |
| 119 | return MetricsProto.MetricsEvent.DEVICE_ADMIN_SETTINGS; |
| 120 | } |
| 121 | |
| 122 | @Override |
Amith Yamasani | a1d0184 | 2011-01-24 16:02:11 -0800 | [diff] [blame] | 123 | public void onCreate(Bundle icicle) { |
| 124 | super.onCreate(icicle); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 129 | Bundle savedInstanceState) { |
| 130 | mDPM = (DevicePolicyManager) getActivity().getSystemService(Context.DEVICE_POLICY_SERVICE); |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 131 | mUm = (UserManager) getActivity().getSystemService(Context.USER_SERVICE); |
Amith Yamasani | a1d0184 | 2011-01-24 16:02:11 -0800 | [diff] [blame] | 132 | return inflater.inflate(R.layout.device_admin_settings, container, false); |
| 133 | } |
| 134 | |
| 135 | @Override |
Fabrice Di Meglio | 402c451 | 2014-07-28 18:55:29 -0700 | [diff] [blame] | 136 | public void onActivityCreated(Bundle savedInstanceState) { |
| 137 | super.onActivityCreated(savedInstanceState); |
Udam Saini | 0708d9e | 2016-03-28 16:35:13 -0700 | [diff] [blame] | 138 | setHasOptionsMenu(true); |
Fabrice Di Meglio | 402c451 | 2014-07-28 18:55:29 -0700 | [diff] [blame] | 139 | Utils.forceCustomPadding(getListView(), true /* additive padding */); |
Doris Ling | 03a3b51 | 2017-10-18 14:25:01 -0700 | [diff] [blame] | 140 | if (InstrumentedPreferenceFragment.usePreferenceScreenTitle()) { |
| 141 | getActivity().setTitle(R.string.manage_device_admin); |
| 142 | } |
Fabrice Di Meglio | 402c451 | 2014-07-28 18:55:29 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | @Override |
Amith Yamasani | a1d0184 | 2011-01-24 16:02:11 -0800 | [diff] [blame] | 146 | public void onResume() { |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 147 | super.onResume(); |
Fan Zhang | 6507613 | 2016-08-08 10:25:13 -0700 | [diff] [blame] | 148 | final Activity activity = getActivity(); |
Fan Zhang | ea02415 | 2016-08-22 15:20:22 -0700 | [diff] [blame] | 149 | mVisibilityLoggerMixin.onResume(); |
Fyodor Kupolov | cfae855 | 2014-11-25 17:34:22 -0800 | [diff] [blame] | 150 | IntentFilter filter = new IntentFilter(); |
| 151 | filter.addAction(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED); |
Fan Zhang | 6507613 | 2016-08-08 10:25:13 -0700 | [diff] [blame] | 152 | activity.registerReceiverAsUser( |
Fyodor Kupolov | cfae855 | 2014-11-25 17:34:22 -0800 | [diff] [blame] | 153 | mBroadcastReceiver, UserHandle.ALL, filter, null, null); |
Makoto Onuki | 4cfe39f | 2015-11-19 13:47:55 -0800 | [diff] [blame] | 154 | |
| 155 | final ComponentName deviceOwnerComponent = mDPM.getDeviceOwnerComponentOnAnyUser(); |
| 156 | mDeviceOwnerPkg = |
| 157 | deviceOwnerComponent != null ? deviceOwnerComponent.getPackageName() : null; |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 158 | mProfileOwnerComponents.clear(); |
| 159 | final List<UserHandle> profiles = mUm.getUserProfiles(); |
| 160 | final int profilesSize = profiles.size(); |
| 161 | for (int i = 0; i < profilesSize; ++i) { |
| 162 | final int profileId = profiles.get(i).getIdentifier(); |
| 163 | mProfileOwnerComponents.put(profileId, mDPM.getProfileOwnerAsUser(profileId)); |
| 164 | } |
Dianne Hackborn | 61daf83 | 2010-01-27 16:22:05 -0800 | [diff] [blame] | 165 | updateList(); |
Dianne Hackborn | 2842903 | 2010-01-25 18:56:17 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Fyodor Kupolov | cfae855 | 2014-11-25 17:34:22 -0800 | [diff] [blame] | 168 | @Override |
| 169 | public void onPause() { |
Fan Zhang | 6507613 | 2016-08-08 10:25:13 -0700 | [diff] [blame] | 170 | final Activity activity = getActivity(); |
| 171 | activity.unregisterReceiver(mBroadcastReceiver); |
Fan Zhang | ea02415 | 2016-08-22 15:20:22 -0700 | [diff] [blame] | 172 | mVisibilityLoggerMixin.onPause(); |
Fyodor Kupolov | cfae855 | 2014-11-25 17:34:22 -0800 | [diff] [blame] | 173 | super.onPause(); |
| 174 | } |
| 175 | |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 176 | /** |
| 177 | * Update the internal collection of available admins for all profiles associated with the |
| 178 | * current user. |
| 179 | */ |
Dianne Hackborn | 61daf83 | 2010-01-27 16:22:05 -0800 | [diff] [blame] | 180 | void updateList() { |
Kenny Guy | 7a35559 | 2015-12-22 12:11:41 +0000 | [diff] [blame] | 181 | mAdmins.clear(); |
Amith Yamasani | a1d0184 | 2011-01-24 16:02:11 -0800 | [diff] [blame] | 182 | |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 183 | final List<UserHandle> profiles = mUm.getUserProfiles(); |
| 184 | final int profilesSize = profiles.size(); |
| 185 | for (int i = 0; i < profilesSize; ++i) { |
| 186 | final int profileId = profiles.get(i).getIdentifier(); |
| 187 | updateAvailableAdminsForProfile(profileId); |
Dianne Hackborn | feff652 | 2010-01-12 18:18:05 -0800 | [diff] [blame] | 188 | } |
Kenny Guy | 7a35559 | 2015-12-22 12:11:41 +0000 | [diff] [blame] | 189 | Collections.sort(mAdmins); |
Amith Yamasani | b8e0f60 | 2014-07-28 16:28:36 -0700 | [diff] [blame] | 190 | |
Dianne Hackborn | 61daf83 | 2010-01-27 16:22:05 -0800 | [diff] [blame] | 191 | getListView().setAdapter(new PolicyListAdapter()); |
Dianne Hackborn | feff652 | 2010-01-12 18:18:05 -0800 | [diff] [blame] | 192 | } |
Amith Yamasani | a1d0184 | 2011-01-24 16:02:11 -0800 | [diff] [blame] | 193 | |
Dianne Hackborn | feff652 | 2010-01-12 18:18:05 -0800 | [diff] [blame] | 194 | @Override |
Amith Yamasani | a1d0184 | 2011-01-24 16:02:11 -0800 | [diff] [blame] | 195 | public void onListItemClick(ListView l, View v, int position, long id) { |
Zoltan Szatmary-Ban | d92897d | 2014-08-28 13:13:34 +0100 | [diff] [blame] | 196 | Object o = l.getAdapter().getItem(position); |
Zoltan Szatmary-Ban | d92897d | 2014-08-28 13:13:34 +0100 | [diff] [blame] | 197 | DeviceAdminInfo dpi = (DeviceAdminInfo) o; |
Kenny Guy | 7a35559 | 2015-12-22 12:11:41 +0000 | [diff] [blame] | 198 | final UserHandle user = new UserHandle(getUserId(dpi)); |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 199 | final Activity activity = getActivity(); |
Kenny Guy | 7a35559 | 2015-12-22 12:11:41 +0000 | [diff] [blame] | 200 | Intent intent = new Intent(activity, DeviceAdminAdd.class); |
| 201 | intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, dpi.getComponent()); |
| 202 | activity.startActivityAsUser(intent, user); |
Dianne Hackborn | feff652 | 2010-01-12 18:18:05 -0800 | [diff] [blame] | 203 | } |
Amith Yamasani | a1d0184 | 2011-01-24 16:02:11 -0800 | [diff] [blame] | 204 | |
Dianne Hackborn | feff652 | 2010-01-12 18:18:05 -0800 | [diff] [blame] | 205 | static class ViewHolder { |
| 206 | ImageView icon; |
| 207 | TextView name; |
Dianne Hackborn | 61daf83 | 2010-01-27 16:22:05 -0800 | [diff] [blame] | 208 | CheckBox checkbox; |
| 209 | TextView description; |
Dianne Hackborn | feff652 | 2010-01-12 18:18:05 -0800 | [diff] [blame] | 210 | } |
Amith Yamasani | b8e0f60 | 2014-07-28 16:28:36 -0700 | [diff] [blame] | 211 | |
Dianne Hackborn | feff652 | 2010-01-12 18:18:05 -0800 | [diff] [blame] | 212 | class PolicyListAdapter extends BaseAdapter { |
| 213 | final LayoutInflater mInflater; |
Amith Yamasani | b8e0f60 | 2014-07-28 16:28:36 -0700 | [diff] [blame] | 214 | |
Dianne Hackborn | feff652 | 2010-01-12 18:18:05 -0800 | [diff] [blame] | 215 | PolicyListAdapter() { |
Amith Yamasani | a1d0184 | 2011-01-24 16:02:11 -0800 | [diff] [blame] | 216 | mInflater = (LayoutInflater) |
| 217 | getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
Dianne Hackborn | feff652 | 2010-01-12 18:18:05 -0800 | [diff] [blame] | 218 | } |
| 219 | |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 220 | @Override |
Dianne Hackborn | feff652 | 2010-01-12 18:18:05 -0800 | [diff] [blame] | 221 | public boolean hasStableIds() { |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 222 | return false; |
Dianne Hackborn | feff652 | 2010-01-12 18:18:05 -0800 | [diff] [blame] | 223 | } |
Amith Yamasani | b8e0f60 | 2014-07-28 16:28:36 -0700 | [diff] [blame] | 224 | |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 225 | @Override |
Dianne Hackborn | feff652 | 2010-01-12 18:18:05 -0800 | [diff] [blame] | 226 | public int getCount() { |
Kenny Guy | 7a35559 | 2015-12-22 12:11:41 +0000 | [diff] [blame] | 227 | return mAdmins.size(); |
Dianne Hackborn | feff652 | 2010-01-12 18:18:05 -0800 | [diff] [blame] | 228 | } |
| 229 | |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 230 | /** |
| 231 | * The item for the given position in the list. |
| 232 | * |
Kenny Guy | 7a35559 | 2015-12-22 12:11:41 +0000 | [diff] [blame] | 233 | * @return DeviceAdminInfo object for actual device admins. |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 234 | */ |
| 235 | @Override |
Dianne Hackborn | feff652 | 2010-01-12 18:18:05 -0800 | [diff] [blame] | 236 | public Object getItem(int position) { |
Kenny Guy | 7a35559 | 2015-12-22 12:11:41 +0000 | [diff] [blame] | 237 | return ((DeviceAdminListItem) (mAdmins.get(position))).info; |
Dianne Hackborn | feff652 | 2010-01-12 18:18:05 -0800 | [diff] [blame] | 238 | } |
| 239 | |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 240 | @Override |
Dianne Hackborn | feff652 | 2010-01-12 18:18:05 -0800 | [diff] [blame] | 241 | public long getItemId(int position) { |
| 242 | return position; |
| 243 | } |
| 244 | |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 245 | @Override |
Dianne Hackborn | feff652 | 2010-01-12 18:18:05 -0800 | [diff] [blame] | 246 | public boolean areAllItemsEnabled() { |
| 247 | return false; |
| 248 | } |
| 249 | |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 250 | /** |
| 251 | * See {@link #getItemViewType} for the view types. |
| 252 | */ |
| 253 | @Override |
| 254 | public int getViewTypeCount() { |
Kenny Guy | 7a35559 | 2015-12-22 12:11:41 +0000 | [diff] [blame] | 255 | return 1; |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /** |
Kenny Guy | 7a35559 | 2015-12-22 12:11:41 +0000 | [diff] [blame] | 259 | * Returns 0 for all types. |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 260 | */ |
| 261 | @Override |
| 262 | public int getItemViewType(int position) { |
Kenny Guy | 7a35559 | 2015-12-22 12:11:41 +0000 | [diff] [blame] | 263 | return 0; |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | @Override |
Dianne Hackborn | feff652 | 2010-01-12 18:18:05 -0800 | [diff] [blame] | 267 | public boolean isEnabled(int position) { |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 268 | Object o = getItem(position); |
| 269 | return isEnabled(o); |
| 270 | } |
| 271 | |
| 272 | private boolean isEnabled(Object o) { |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 273 | DeviceAdminInfo info = (DeviceAdminInfo) o; |
Fyodor Kupolov | cfae855 | 2014-11-25 17:34:22 -0800 | [diff] [blame] | 274 | // Disable item if admin is being removed |
| 275 | if (isRemovingAdmin(info)) { |
| 276 | return false; |
| 277 | } |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 278 | return true; |
Dianne Hackborn | feff652 | 2010-01-12 18:18:05 -0800 | [diff] [blame] | 279 | } |
| 280 | |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 281 | @Override |
Dianne Hackborn | feff652 | 2010-01-12 18:18:05 -0800 | [diff] [blame] | 282 | public View getView(int position, View convertView, ViewGroup parent) { |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 283 | Object o = getItem(position); |
Kenny Guy | 7a35559 | 2015-12-22 12:11:41 +0000 | [diff] [blame] | 284 | if (convertView == null) { |
| 285 | convertView = newDeviceAdminView(parent); |
Dianne Hackborn | feff652 | 2010-01-12 18:18:05 -0800 | [diff] [blame] | 286 | } |
Kenny Guy | 7a35559 | 2015-12-22 12:11:41 +0000 | [diff] [blame] | 287 | bindView(convertView, (DeviceAdminInfo) o); |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 288 | return convertView; |
Dianne Hackborn | feff652 | 2010-01-12 18:18:05 -0800 | [diff] [blame] | 289 | } |
Amith Yamasani | b8e0f60 | 2014-07-28 16:28:36 -0700 | [diff] [blame] | 290 | |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 291 | private View newDeviceAdminView(ViewGroup parent) { |
Dianne Hackborn | feff652 | 2010-01-12 18:18:05 -0800 | [diff] [blame] | 292 | View v = mInflater.inflate(R.layout.device_admin_item, parent, false); |
| 293 | ViewHolder h = new ViewHolder(); |
Kenny Guy | 7a35559 | 2015-12-22 12:11:41 +0000 | [diff] [blame] | 294 | h.icon = (ImageView) v.findViewById(R.id.icon); |
| 295 | h.name = (TextView) v.findViewById(R.id.name); |
| 296 | h.checkbox = (CheckBox) v.findViewById(R.id.checkbox); |
| 297 | h.description = (TextView) v.findViewById(R.id.description); |
Dianne Hackborn | feff652 | 2010-01-12 18:18:05 -0800 | [diff] [blame] | 298 | v.setTag(h); |
| 299 | return v; |
| 300 | } |
Amith Yamasani | b8e0f60 | 2014-07-28 16:28:36 -0700 | [diff] [blame] | 301 | |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 302 | private void bindView(View view, DeviceAdminInfo item) { |
Amith Yamasani | a1d0184 | 2011-01-24 16:02:11 -0800 | [diff] [blame] | 303 | final Activity activity = getActivity(); |
Dianne Hackborn | feff652 | 2010-01-12 18:18:05 -0800 | [diff] [blame] | 304 | ViewHolder vh = (ViewHolder) view.getTag(); |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 305 | Drawable activityIcon = item.loadIcon(activity.getPackageManager()); |
Amith Yamasani | 30c50b1 | 2014-09-07 15:38:09 -0700 | [diff] [blame] | 306 | Drawable badgedIcon = activity.getPackageManager().getUserBadgedIcon( |
| 307 | activityIcon, new UserHandle(getUserId(item))); |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 308 | vh.icon.setImageDrawable(badgedIcon); |
Amith Yamasani | a1d0184 | 2011-01-24 16:02:11 -0800 | [diff] [blame] | 309 | vh.name.setText(item.loadLabel(activity.getPackageManager())); |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 310 | vh.checkbox.setChecked(isActiveAdmin(item)); |
| 311 | final boolean enabled = isEnabled(item); |
Dianne Hackborn | 61daf83 | 2010-01-27 16:22:05 -0800 | [diff] [blame] | 312 | try { |
Amith Yamasani | a1d0184 | 2011-01-24 16:02:11 -0800 | [diff] [blame] | 313 | vh.description.setText(item.loadDescription(activity.getPackageManager())); |
Dianne Hackborn | 61daf83 | 2010-01-27 16:22:05 -0800 | [diff] [blame] | 314 | } catch (Resources.NotFoundException e) { |
| 315 | } |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 316 | vh.checkbox.setEnabled(enabled); |
| 317 | vh.name.setEnabled(enabled); |
| 318 | vh.description.setEnabled(enabled); |
| 319 | vh.icon.setEnabled(enabled); |
Dianne Hackborn | feff652 | 2010-01-12 18:18:05 -0800 | [diff] [blame] | 320 | } |
| 321 | } |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 322 | |
| 323 | private boolean isDeviceOwner(DeviceAdminInfo item) { |
| 324 | return getUserId(item) == UserHandle.myUserId() |
| 325 | && item.getPackageName().equals(mDeviceOwnerPkg); |
| 326 | } |
| 327 | |
| 328 | private boolean isProfileOwner(DeviceAdminInfo item) { |
| 329 | ComponentName profileOwner = mProfileOwnerComponents.get(getUserId(item)); |
| 330 | return item.getComponent().equals(profileOwner); |
| 331 | } |
| 332 | |
| 333 | private boolean isActiveAdmin(DeviceAdminInfo item) { |
| 334 | return mDPM.isAdminActiveAsUser(item.getComponent(), getUserId(item)); |
| 335 | } |
| 336 | |
Fyodor Kupolov | cfae855 | 2014-11-25 17:34:22 -0800 | [diff] [blame] | 337 | private boolean isRemovingAdmin(DeviceAdminInfo item) { |
| 338 | return mDPM.isRemovingAdmin(item.getComponent(), getUserId(item)); |
| 339 | } |
| 340 | |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 341 | /** |
| 342 | * Add device admins to the internal collection that belong to a profile. |
| 343 | * |
| 344 | * @param profileId the profile identifier. |
| 345 | */ |
| 346 | private void updateAvailableAdminsForProfile(final int profileId) { |
| 347 | // We are adding the union of two sets 'A' and 'B' of device admins to mAvailableAdmins. |
| 348 | // Set 'A' is the set of active admins for the profile whereas set 'B' is the set of |
| 349 | // listeners to DeviceAdminReceiver.ACTION_DEVICE_ADMIN_ENABLED for the profile. |
| 350 | |
| 351 | // Add all of set 'A' to mAvailableAdmins. |
| 352 | List<ComponentName> activeAdminsListForProfile = mDPM.getActiveAdminsAsUser(profileId); |
| 353 | addActiveAdminsForProfile(activeAdminsListForProfile, profileId); |
| 354 | |
| 355 | // Collect set 'B' and add B-A to mAvailableAdmins. |
| 356 | addDeviceAdminBroadcastReceiversForProfile(activeAdminsListForProfile, profileId); |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Add a profile's device admins that are receivers of |
| 361 | * {@code DeviceAdminReceiver.ACTION_DEVICE_ADMIN_ENABLED} to the internal collection if they |
| 362 | * haven't been added yet. |
| 363 | * |
| 364 | * @param alreadyAddedComponents the set of active admin component names. Receivers of |
| 365 | * {@code DeviceAdminReceiver.ACTION_DEVICE_ADMIN_ENABLED} whose component is in this |
| 366 | * set are not added to the internal collection again. |
| 367 | * @param profileId the identifier of the profile |
| 368 | */ |
| 369 | private void addDeviceAdminBroadcastReceiversForProfile( |
| 370 | Collection<ComponentName> alreadyAddedComponents, final int profileId) { |
| 371 | final PackageManager pm = getActivity().getPackageManager(); |
Jeff Sharkey | df82840 | 2016-01-06 14:51:30 -0700 | [diff] [blame] | 372 | List<ResolveInfo> enabledForProfile = pm.queryBroadcastReceiversAsUser( |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 373 | new Intent(DeviceAdminReceiver.ACTION_DEVICE_ADMIN_ENABLED), |
| 374 | PackageManager.GET_META_DATA | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS, |
| 375 | profileId); |
| 376 | if (enabledForProfile == null) { |
| 377 | enabledForProfile = Collections.emptyList(); |
| 378 | } |
| 379 | final int n = enabledForProfile.size(); |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 380 | for (int i = 0; i < n; ++i) { |
| 381 | ResolveInfo resolveInfo = enabledForProfile.get(i); |
| 382 | ComponentName riComponentName = |
| 383 | new ComponentName(resolveInfo.activityInfo.packageName, |
| 384 | resolveInfo.activityInfo.name); |
| 385 | if (alreadyAddedComponents == null |
| 386 | || !alreadyAddedComponents.contains(riComponentName)) { |
Makoto Onuki | 7cdaa52 | 2016-02-24 12:56:44 -0800 | [diff] [blame] | 387 | DeviceAdminInfo deviceAdminInfo = createDeviceAdminInfo(resolveInfo.activityInfo); |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 388 | // add only visible ones (note: active admins are added regardless of visibility) |
| 389 | if (deviceAdminInfo != null && deviceAdminInfo.isVisible()) { |
Makoto Onuki | 9dbad57 | 2016-02-17 11:15:04 -0800 | [diff] [blame] | 390 | if (!deviceAdminInfo.getActivityInfo().applicationInfo.isInternal()) { |
| 391 | continue; |
| 392 | } |
Kenny Guy | 7a35559 | 2015-12-22 12:11:41 +0000 | [diff] [blame] | 393 | DeviceAdminListItem item = new DeviceAdminListItem(); |
| 394 | item.info = deviceAdminInfo; |
| 395 | item.name = deviceAdminInfo.loadLabel(pm).toString(); |
| 396 | // Active ones already added. |
| 397 | item.active = false; |
| 398 | mAdmins.add(item); |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | } |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | /** |
| 405 | * Add a {@link DeviceAdminInfo} object to the internal collection of available admins for all |
| 406 | * active admin components associated with a profile. |
| 407 | * |
| 408 | * @param profileId a profile identifier. |
| 409 | */ |
| 410 | private void addActiveAdminsForProfile(final List<ComponentName> activeAdmins, |
| 411 | final int profileId) { |
| 412 | if (activeAdmins != null) { |
| 413 | final PackageManager packageManager = getActivity().getPackageManager(); |
Makoto Onuki | 7cdaa52 | 2016-02-24 12:56:44 -0800 | [diff] [blame] | 414 | final IPackageManager iPackageManager = AppGlobals.getPackageManager(); |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 415 | final int n = activeAdmins.size(); |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 416 | for (int i = 0; i < n; ++i) { |
Makoto Onuki | 7cdaa52 | 2016-02-24 12:56:44 -0800 | [diff] [blame] | 417 | final ComponentName activeAdmin = activeAdmins.get(i); |
| 418 | final ActivityInfo ai; |
| 419 | try { |
| 420 | ai = iPackageManager.getReceiverInfo(activeAdmin, |
| 421 | PackageManager.GET_META_DATA | |
| 422 | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS | |
Jeff Sharkey | 2079a38 | 2017-02-24 12:03:09 -0700 | [diff] [blame] | 423 | PackageManager.MATCH_DIRECT_BOOT_UNAWARE | |
| 424 | PackageManager.MATCH_DIRECT_BOOT_AWARE, profileId); |
Makoto Onuki | 7cdaa52 | 2016-02-24 12:56:44 -0800 | [diff] [blame] | 425 | } catch (RemoteException e) { |
| 426 | Log.w(TAG, "Unable to load component: " + activeAdmin); |
| 427 | continue; |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 428 | } |
Makoto Onuki | 7cdaa52 | 2016-02-24 12:56:44 -0800 | [diff] [blame] | 429 | final DeviceAdminInfo deviceAdminInfo = createDeviceAdminInfo(ai); |
| 430 | if (deviceAdminInfo == null) { |
| 431 | continue; |
| 432 | } |
| 433 | // Don't do the applicationInfo.isInternal() check here; if an active |
| 434 | // admin is already on SD card, just show it. |
| 435 | final DeviceAdminListItem item = new DeviceAdminListItem(); |
| 436 | item.info = deviceAdminInfo; |
| 437 | item.name = deviceAdminInfo.loadLabel(packageManager).toString(); |
| 438 | item.active = true; |
| 439 | mAdmins.add(item); |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 440 | } |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * Creates a device admin info object for the resolved intent that points to the component of |
| 446 | * the device admin. |
| 447 | * |
Makoto Onuki | 7cdaa52 | 2016-02-24 12:56:44 -0800 | [diff] [blame] | 448 | * @param ai ActivityInfo for the admin component. |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 449 | * @return new {@link DeviceAdminInfo} object or null if there was an error. |
| 450 | */ |
Makoto Onuki | 7cdaa52 | 2016-02-24 12:56:44 -0800 | [diff] [blame] | 451 | private DeviceAdminInfo createDeviceAdminInfo(ActivityInfo ai) { |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 452 | try { |
Makoto Onuki | 7cdaa52 | 2016-02-24 12:56:44 -0800 | [diff] [blame] | 453 | return new DeviceAdminInfo(getActivity(), ai); |
| 454 | } catch (XmlPullParserException|IOException e) { |
| 455 | Log.w(TAG, "Skipping " + ai, e); |
Zoltan Szatmary-Ban | f3d7209 | 2014-07-02 16:48:00 +0100 | [diff] [blame] | 456 | } |
| 457 | return null; |
| 458 | } |
| 459 | |
| 460 | /** |
| 461 | * Extracts the user id from a device admin info object. |
| 462 | * @param adminInfo the device administrator info. |
| 463 | * @return identifier of the user associated with the device admin. |
| 464 | */ |
| 465 | private int getUserId(DeviceAdminInfo adminInfo) { |
| 466 | return UserHandle.getUserId(adminInfo.getActivityInfo().applicationInfo.uid); |
| 467 | } |
Dianne Hackborn | feff652 | 2010-01-12 18:18:05 -0800 | [diff] [blame] | 468 | } |