blob: bcd247e0ca08cf9d4d25e949bed94f4ac136269f [file] [log] [blame]
Dianne Hackborn28429032010-01-25 18:56:17 -08001/*
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
17package com.android.settings;
18
Dianne Hackbornb763e342014-07-22 16:23:36 -070019import android.app.AppOpsManager;
Amith Yamasanib8e0f602014-07-28 16:28:36 -070020
Dianne Hackborn28429032010-01-25 18:56:17 -080021import org.xmlpull.v1.XmlPullParserException;
22
23import android.app.Activity;
Dianne Hackborn4f25e1d2011-03-02 22:39:49 -080024import android.app.ActivityManagerNative;
Dianne Hackborn61daf832010-01-27 16:22:05 -080025import android.app.AlertDialog;
Dianne Hackborn61daf832010-01-27 16:22:05 -080026import android.app.Dialog;
Dianne Hackborn4037c7f2010-02-26 17:26:55 -080027import android.app.admin.DeviceAdminInfo;
28import android.app.admin.DeviceAdminReceiver;
29import android.app.admin.DevicePolicyManager;
Dianne Hackborn28429032010-01-25 18:56:17 -080030import android.content.ComponentName;
31import android.content.Context;
Dianne Hackborn61daf832010-01-27 16:22:05 -080032import android.content.DialogInterface;
Dianne Hackborn39e64e72010-03-26 13:24:39 -070033import android.content.Intent;
Dianne Hackborn28429032010-01-25 18:56:17 -080034import android.content.pm.ActivityInfo;
Amith Yamasani080f8db2014-08-13 11:11:44 -070035import android.content.pm.ApplicationInfo;
36import android.content.pm.PackageInfo;
Dianne Hackborn28429032010-01-25 18:56:17 -080037import android.content.pm.PackageManager;
Amith Yamasani080f8db2014-08-13 11:11:44 -070038import android.content.pm.PackageManager.NameNotFoundException;
Dianne Hackborn28429032010-01-25 18:56:17 -080039import android.content.pm.ResolveInfo;
40import android.content.res.Resources;
41import android.os.Bundle;
Dianne Hackborn61daf832010-01-27 16:22:05 -080042import android.os.Handler;
43import android.os.RemoteCallback;
Dianne Hackborn4f25e1d2011-03-02 22:39:49 -080044import android.os.RemoteException;
Amith Yamasanib8e0f602014-07-28 16:28:36 -070045import android.os.UserHandle;
Chris Palmer51794a02010-09-13 15:14:55 -070046import android.text.TextUtils.TruncateAt;
William Luh64e676a2013-11-22 15:42:40 -080047import android.util.EventLog;
Dianne Hackborn28429032010-01-25 18:56:17 -080048import android.util.Log;
Chris Palmer51794a02010-09-13 15:14:55 -070049import android.view.Display;
Dianne Hackborn28429032010-01-25 18:56:17 -080050import android.view.View;
51import android.view.ViewGroup;
Fyodor Kupolov1c8600b2015-05-29 11:17:32 -070052import android.view.ViewTreeObserver;
Chris Palmer51794a02010-09-13 15:14:55 -070053import android.view.WindowManager;
Dianne Hackborn28429032010-01-25 18:56:17 -080054import android.widget.AppSecurityPermissions;
Dianne Hackborn61daf832010-01-27 16:22:05 -080055import android.widget.Button;
Dianne Hackborn28429032010-01-25 18:56:17 -080056import android.widget.ImageView;
57import android.widget.TextView;
58
59import java.io.IOException;
60import java.util.ArrayList;
Dianne Hackborn8274b672013-05-22 11:29:36 -070061import java.util.List;
Dianne Hackborn28429032010-01-25 18:56:17 -080062
63public class DeviceAdminAdd extends Activity {
64 static final String TAG = "DeviceAdminAdd";
William Luh64e676a2013-11-22 15:42:40 -080065
Dianne Hackborn61daf832010-01-27 16:22:05 -080066 static final int DIALOG_WARNING = 1;
Chris Palmer51794a02010-09-13 15:14:55 -070067
68 private static final int MAX_ADD_MSG_LINES_PORTRAIT = 5;
69 private static final int MAX_ADD_MSG_LINES_LANDSCAPE = 2;
70 private static final int MAX_ADD_MSG_LINES = 15;
William Luh64e676a2013-11-22 15:42:40 -080071
Dianne Hackborn61daf832010-01-27 16:22:05 -080072 Handler mHandler;
William Luh64e676a2013-11-22 15:42:40 -080073
Dianne Hackborn28429032010-01-25 18:56:17 -080074 DevicePolicyManager mDPM;
Dianne Hackbornb763e342014-07-22 16:23:36 -070075 AppOpsManager mAppOps;
Dianne Hackborn28429032010-01-25 18:56:17 -080076 DeviceAdminInfo mDeviceAdmin;
Dianne Hackbornb885afe2010-01-26 18:03:42 -080077 CharSequence mAddMsgText;
Amith Yamasanib8e0f602014-07-28 16:28:36 -070078 String mProfileOwnerName;
William Luh64e676a2013-11-22 15:42:40 -080079
Dianne Hackborn61daf832010-01-27 16:22:05 -080080 ImageView mAdminIcon;
81 TextView mAdminName;
82 TextView mAdminDescription;
Dianne Hackbornb885afe2010-01-26 18:03:42 -080083 TextView mAddMsg;
Amith Yamasanib8e0f602014-07-28 16:28:36 -070084 TextView mProfileOwnerWarning;
Amith Yamasania1d01842011-01-24 16:02:11 -080085 ImageView mAddMsgExpander;
Chris Palmer51794a02010-09-13 15:14:55 -070086 boolean mAddMsgEllipsized = true;
Dianne Hackborn61daf832010-01-27 16:22:05 -080087 TextView mAdminWarning;
Dianne Hackborn28429032010-01-25 18:56:17 -080088 ViewGroup mAdminPolicies;
Dianne Hackborn61daf832010-01-27 16:22:05 -080089 Button mActionButton;
Dianne Hackborn97de6bc2010-03-03 17:18:13 -080090 Button mCancelButton;
William Luh64e676a2013-11-22 15:42:40 -080091
Dianne Hackborn61daf832010-01-27 16:22:05 -080092 boolean mAdding;
Andy Stadler5cc0e412010-12-08 15:57:18 -080093 boolean mRefreshing;
Dianne Hackbornb763e342014-07-22 16:23:36 -070094 boolean mWaitingForRemoveMsg;
Amith Yamasanib8e0f602014-07-28 16:28:36 -070095 boolean mAddingProfileOwner;
Fyodor Kupolov7dc56bd2015-02-24 09:26:53 -080096 boolean mAdminPoliciesInitialized;
Dianne Hackbornb763e342014-07-22 16:23:36 -070097 int mCurSysAppOpMode;
98 int mCurToastAppOpMode;
William Luh64e676a2013-11-22 15:42:40 -080099
Dianne Hackborn28429032010-01-25 18:56:17 -0800100 @Override
101 protected void onCreate(Bundle icicle) {
102 super.onCreate(icicle);
103
Dianne Hackborn61daf832010-01-27 16:22:05 -0800104 mHandler = new Handler(getMainLooper());
William Luh64e676a2013-11-22 15:42:40 -0800105
Dianne Hackborn28429032010-01-25 18:56:17 -0800106 mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
Dianne Hackbornb763e342014-07-22 16:23:36 -0700107 mAppOps = (AppOpsManager)getSystemService(Context.APP_OPS_SERVICE);
Amith Yamasani080f8db2014-08-13 11:11:44 -0700108 PackageManager packageManager = getPackageManager();
Dianne Hackborn28429032010-01-25 18:56:17 -0800109
Dianne Hackborn39e64e72010-03-26 13:24:39 -0700110 if ((getIntent().getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
Andy Stadler5cc0e412010-12-08 15:57:18 -0800111 Log.w(TAG, "Cannot start ADD_DEVICE_ADMIN as a new task");
Dianne Hackborn39e64e72010-03-26 13:24:39 -0700112 finish();
113 return;
114 }
William Luh64e676a2013-11-22 15:42:40 -0800115
Amith Yamasanib8e0f602014-07-28 16:28:36 -0700116 String action = getIntent().getAction();
117 ComponentName who = (ComponentName)getIntent().getParcelableExtra(
Dianne Hackborn28429032010-01-25 18:56:17 -0800118 DevicePolicyManager.EXTRA_DEVICE_ADMIN);
Amith Yamasanib8e0f602014-07-28 16:28:36 -0700119 if (who == null) {
120 Log.w(TAG, "No component specified in " + action);
Dianne Hackborn28429032010-01-25 18:56:17 -0800121 finish();
122 return;
123 }
Dianne Hackborn8274b672013-05-22 11:29:36 -0700124
Amith Yamasanib8e0f602014-07-28 16:28:36 -0700125 if (action != null && action.equals(DevicePolicyManager.ACTION_SET_PROFILE_OWNER)) {
Amith Yamasani080f8db2014-08-13 11:11:44 -0700126 setResult(RESULT_CANCELED);
127 setFinishOnTouchOutside(true);
Amith Yamasanib8e0f602014-07-28 16:28:36 -0700128 mAddingProfileOwner = true;
129 mProfileOwnerName =
130 getIntent().getStringExtra(DevicePolicyManager.EXTRA_PROFILE_OWNER_NAME);
131 String callingPackage = getCallingPackage();
132 if (callingPackage == null || !callingPackage.equals(who.getPackageName())) {
133 Log.e(TAG, "Unknown or incorrect caller");
134 finish();
135 return;
136 }
Amith Yamasani080f8db2014-08-13 11:11:44 -0700137 try {
138 PackageInfo packageInfo = packageManager.getPackageInfo(callingPackage, 0);
139 if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
140 Log.e(TAG, "Cannot set a non-system app as a profile owner");
141 finish();
142 return;
143 }
144 } catch (NameNotFoundException nnfe) {
145 Log.e(TAG, "Cannot find the package " + callingPackage);
146 finish();
147 return;
148 }
Amith Yamasanib8e0f602014-07-28 16:28:36 -0700149 }
150
Dianne Hackborn28429032010-01-25 18:56:17 -0800151 ActivityInfo ai;
152 try {
Amith Yamasani080f8db2014-08-13 11:11:44 -0700153 ai = packageManager.getReceiverInfo(who, PackageManager.GET_META_DATA);
Dianne Hackborn28429032010-01-25 18:56:17 -0800154 } catch (PackageManager.NameNotFoundException e) {
Amith Yamasanib8e0f602014-07-28 16:28:36 -0700155 Log.w(TAG, "Unable to retrieve device policy " + who, e);
Dianne Hackborn28429032010-01-25 18:56:17 -0800156 finish();
157 return;
158 }
Dianne Hackborn8274b672013-05-22 11:29:36 -0700159
Alex Klyubin29de9d52013-06-06 14:56:24 -0700160 // When activating, make sure the given component name is actually a valid device admin.
161 // No need to check this when deactivating, because it is safe to deactivate an active
162 // invalid device admin.
Amith Yamasanib8e0f602014-07-28 16:28:36 -0700163 if (!mDPM.isAdminActive(who)) {
Amith Yamasani080f8db2014-08-13 11:11:44 -0700164 List<ResolveInfo> avail = packageManager.queryBroadcastReceivers(
Alex Klyubin29de9d52013-06-06 14:56:24 -0700165 new Intent(DeviceAdminReceiver.ACTION_DEVICE_ADMIN_ENABLED),
Alex Klyubin681b21f2013-06-06 15:45:23 -0700166 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS);
Alex Klyubin29de9d52013-06-06 14:56:24 -0700167 int count = avail == null ? 0 : avail.size();
168 boolean found = false;
169 for (int i=0; i<count; i++) {
170 ResolveInfo ri = avail.get(i);
171 if (ai.packageName.equals(ri.activityInfo.packageName)
172 && ai.name.equals(ri.activityInfo.name)) {
173 try {
174 // We didn't retrieve the meta data for all possible matches, so
175 // need to use the activity info of this specific one that was retrieved.
176 ri.activityInfo = ai;
177 DeviceAdminInfo dpi = new DeviceAdminInfo(this, ri);
178 found = true;
179 } catch (XmlPullParserException e) {
180 Log.w(TAG, "Bad " + ri.activityInfo, e);
181 } catch (IOException e) {
182 Log.w(TAG, "Bad " + ri.activityInfo, e);
183 }
184 break;
Dianne Hackborn8274b672013-05-22 11:29:36 -0700185 }
Dianne Hackborn8274b672013-05-22 11:29:36 -0700186 }
Alex Klyubin29de9d52013-06-06 14:56:24 -0700187 if (!found) {
Amith Yamasanib8e0f602014-07-28 16:28:36 -0700188 Log.w(TAG, "Request to add invalid device admin: " + who);
Alex Klyubin29de9d52013-06-06 14:56:24 -0700189 finish();
190 return;
191 }
Dianne Hackborn8274b672013-05-22 11:29:36 -0700192 }
193
Dianne Hackborn28429032010-01-25 18:56:17 -0800194 ResolveInfo ri = new ResolveInfo();
195 ri.activityInfo = ai;
196 try {
Andy Stadler5cc0e412010-12-08 15:57:18 -0800197 mDeviceAdmin = new DeviceAdminInfo(this, ri);
Dianne Hackborn28429032010-01-25 18:56:17 -0800198 } catch (XmlPullParserException e) {
Amith Yamasanib8e0f602014-07-28 16:28:36 -0700199 Log.w(TAG, "Unable to retrieve device policy " + who, e);
Dianne Hackborn28429032010-01-25 18:56:17 -0800200 finish();
201 return;
202 } catch (IOException e) {
Amith Yamasanib8e0f602014-07-28 16:28:36 -0700203 Log.w(TAG, "Unable to retrieve device policy " + who, e);
Dianne Hackborn28429032010-01-25 18:56:17 -0800204 finish();
205 return;
206 }
William Luh64e676a2013-11-22 15:42:40 -0800207
Andy Stadler5cc0e412010-12-08 15:57:18 -0800208 // This admin already exists, an we have two options at this point. If new policy
209 // bits are set, show the user the new list. If nothing has changed, simply return
210 // "OK" immediately.
211 if (DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN.equals(getIntent().getAction())) {
212 mRefreshing = false;
Amith Yamasanib8e0f602014-07-28 16:28:36 -0700213 if (mDPM.isAdminActive(who)) {
Andy Stadler5cc0e412010-12-08 15:57:18 -0800214 ArrayList<DeviceAdminInfo.PolicyInfo> newPolicies = mDeviceAdmin.getUsedPolicies();
215 for (int i = 0; i < newPolicies.size(); i++) {
216 DeviceAdminInfo.PolicyInfo pi = newPolicies.get(i);
Amith Yamasanib8e0f602014-07-28 16:28:36 -0700217 if (!mDPM.hasGrantedPolicy(who, pi.ident)) {
Andy Stadler5cc0e412010-12-08 15:57:18 -0800218 mRefreshing = true;
219 break;
220 }
221 }
222 if (!mRefreshing) {
223 // Nothing changed (or policies were removed) - return immediately
224 setResult(Activity.RESULT_OK);
225 finish();
226 return;
227 }
228 }
229 }
Amith Yamasanib8e0f602014-07-28 16:28:36 -0700230
231 // If we're trying to add a profile owner and user setup hasn't completed yet, no
232 // need to prompt for permission. Just add and finish.
233 if (mAddingProfileOwner && !mDPM.hasUserSetupCompleted()) {
234 addAndFinish();
235 return;
236 }
237
Andy Stadler5cc0e412010-12-08 15:57:18 -0800238 mAddMsgText = getIntent().getCharSequenceExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION);
Amith Yamasania1d01842011-01-24 16:02:11 -0800239
Dianne Hackborn28429032010-01-25 18:56:17 -0800240 setContentView(R.layout.device_admin_add);
William Luh64e676a2013-11-22 15:42:40 -0800241
Dianne Hackborn61daf832010-01-27 16:22:05 -0800242 mAdminIcon = (ImageView)findViewById(R.id.admin_icon);
243 mAdminName = (TextView)findViewById(R.id.admin_name);
244 mAdminDescription = (TextView)findViewById(R.id.admin_description);
Amith Yamasanib8e0f602014-07-28 16:28:36 -0700245 mProfileOwnerWarning = (TextView) findViewById(R.id.profile_owner_warning);
Chris Palmer51794a02010-09-13 15:14:55 -0700246
Dianne Hackbornb885afe2010-01-26 18:03:42 -0800247 mAddMsg = (TextView)findViewById(R.id.add_msg);
Amith Yamasania1d01842011-01-24 16:02:11 -0800248 mAddMsgExpander = (ImageView) findViewById(R.id.add_msg_expander);
Fyodor Kupolov1c8600b2015-05-29 11:17:32 -0700249 final View.OnClickListener onClickListener = new View.OnClickListener() {
250 @Override
Chris Palmer51794a02010-09-13 15:14:55 -0700251 public void onClick(View v) {
Fyodor Kupolov1c8600b2015-05-29 11:17:32 -0700252 toggleMessageEllipsis(mAddMsg);
Chris Palmer51794a02010-09-13 15:14:55 -0700253 }
Fyodor Kupolov1c8600b2015-05-29 11:17:32 -0700254 };
255 mAddMsgExpander.setOnClickListener(onClickListener);
256 mAddMsg.setOnClickListener(onClickListener);
257
258 // Determine whether the message can be collapsed - getLineCount() gives the correct
259 // number of lines only after a layout pass.
260 mAddMsg.getViewTreeObserver().addOnGlobalLayoutListener(
261 new ViewTreeObserver.OnGlobalLayoutListener() {
262 @Override
263 public void onGlobalLayout() {
264 final int maxLines = getEllipsizedLines();
265 // hide the icon if number of visible lines does not exceed maxLines
266 boolean hideMsgExpander = mAddMsg.getLineCount() <= maxLines;
267 mAddMsgExpander.setVisibility(hideMsgExpander ? View.GONE : View.VISIBLE);
268 if (hideMsgExpander) {
269 mAddMsg.setOnClickListener(null);
270 ((View)mAddMsgExpander.getParent()).invalidate();
271 }
272 mAddMsg.getViewTreeObserver().removeOnGlobalLayoutListener(this);
273 }
274 });
Chris Palmer51794a02010-09-13 15:14:55 -0700275
276 // toggleMessageEllipsis also handles initial layout:
277 toggleMessageEllipsis(mAddMsg);
278
Amith Yamasania1d01842011-01-24 16:02:11 -0800279 mAdminWarning = (TextView) findViewById(R.id.admin_warning);
280 mAdminPolicies = (ViewGroup) findViewById(R.id.admin_policies);
281 mCancelButton = (Button) findViewById(R.id.cancel_button);
Stephan Huberdc333212015-01-23 09:49:57 +0100282 mCancelButton.setFilterTouchesWhenObscured(true);
Dianne Hackborn97de6bc2010-03-03 17:18:13 -0800283 mCancelButton.setOnClickListener(new View.OnClickListener() {
284 public void onClick(View v) {
William Luh64e676a2013-11-22 15:42:40 -0800285 EventLog.writeEvent(EventLogTags.EXP_DET_DEVICE_ADMIN_DECLINED_BY_USER,
286 mDeviceAdmin.getActivityInfo().applicationInfo.uid);
Dianne Hackborn97de6bc2010-03-03 17:18:13 -0800287 finish();
288 }
289 });
Amith Yamasania1d01842011-01-24 16:02:11 -0800290 mActionButton = (Button) findViewById(R.id.action_button);
Stephan Huberdc333212015-01-23 09:49:57 +0100291 mActionButton.setFilterTouchesWhenObscured(true);
Dianne Hackborn61daf832010-01-27 16:22:05 -0800292 mActionButton.setOnClickListener(new View.OnClickListener() {
Dianne Hackborn28429032010-01-25 18:56:17 -0800293 public void onClick(View v) {
Dianne Hackborn61daf832010-01-27 16:22:05 -0800294 if (mAdding) {
Amith Yamasanib8e0f602014-07-28 16:28:36 -0700295 addAndFinish();
Dianne Hackbornb763e342014-07-22 16:23:36 -0700296 } else if (!mWaitingForRemoveMsg) {
Dianne Hackborn4f25e1d2011-03-02 22:39:49 -0800297 try {
298 // Don't allow the admin to put a dialog up in front
299 // of us while we interact with the user.
300 ActivityManagerNative.getDefault().stopAppSwitches();
301 } catch (RemoteException e) {
302 }
Dianne Hackbornb763e342014-07-22 16:23:36 -0700303 mWaitingForRemoveMsg = true;
Andrew Stadler4df6edb2010-02-05 01:00:13 -0800304 mDPM.getRemoveWarning(mDeviceAdmin.getComponent(),
305 new RemoteCallback(mHandler) {
306 @Override
307 protected void onResult(Bundle bundle) {
308 CharSequence msg = bundle != null
Dianne Hackborn54355112010-02-16 20:40:42 -0800309 ? bundle.getCharSequence(
310 DeviceAdminReceiver.EXTRA_DISABLE_WARNING)
Andrew Stadler4df6edb2010-02-05 01:00:13 -0800311 : null;
Dianne Hackbornb763e342014-07-22 16:23:36 -0700312 continueRemoveAction(msg);
Dianne Hackborn61daf832010-01-27 16:22:05 -0800313 }
Andrew Stadler4df6edb2010-02-05 01:00:13 -0800314 });
Dianne Hackbornb763e342014-07-22 16:23:36 -0700315 // Don't want to wait too long.
316 getWindow().getDecorView().getHandler().postDelayed(new Runnable() {
317 @Override public void run() {
318 continueRemoveAction(null);
319 }
320 }, 2*1000);
Andrew Stadler4df6edb2010-02-05 01:00:13 -0800321 }
Dianne Hackborn28429032010-01-25 18:56:17 -0800322 }
323 });
324 }
William Luh64e676a2013-11-22 15:42:40 -0800325
Amith Yamasanib8e0f602014-07-28 16:28:36 -0700326 void addAndFinish() {
327 try {
328 mDPM.setActiveAdmin(mDeviceAdmin.getComponent(), mRefreshing);
329 EventLog.writeEvent(EventLogTags.EXP_DET_DEVICE_ADMIN_ACTIVATED_BY_USER,
330 mDeviceAdmin.getActivityInfo().applicationInfo.uid);
331 setResult(Activity.RESULT_OK);
332 } catch (RuntimeException e) {
333 // Something bad happened... could be that it was
334 // already set, though.
335 Log.w(TAG, "Exception trying to activate admin "
336 + mDeviceAdmin.getComponent(), e);
337 if (mDPM.isAdminActive(mDeviceAdmin.getComponent())) {
338 setResult(Activity.RESULT_OK);
339 }
340 }
341 if (mAddingProfileOwner) {
342 try {
343 mDPM.setProfileOwner(mDeviceAdmin.getComponent(),
344 mProfileOwnerName, UserHandle.myUserId());
345 } catch (RuntimeException re) {
346 setResult(Activity.RESULT_CANCELED);
347 }
348 }
349 finish();
350 }
351
Dianne Hackbornb763e342014-07-22 16:23:36 -0700352 void continueRemoveAction(CharSequence msg) {
353 if (!mWaitingForRemoveMsg) {
354 return;
355 }
356 mWaitingForRemoveMsg = false;
357 if (msg == null) {
358 try {
359 ActivityManagerNative.getDefault().resumeAppSwitches();
360 } catch (RemoteException e) {
361 }
362 mDPM.removeActiveAdmin(mDeviceAdmin.getComponent());
363 finish();
364 } else {
365 try {
366 // Continue preventing anything from coming in front.
367 ActivityManagerNative.getDefault().stopAppSwitches();
368 } catch (RemoteException e) {
369 }
370 Bundle args = new Bundle();
371 args.putCharSequence(
372 DeviceAdminReceiver.EXTRA_DISABLE_WARNING, msg);
373 showDialog(DIALOG_WARNING, args);
374 }
375 }
376
Dianne Hackborn28429032010-01-25 18:56:17 -0800377 @Override
378 protected void onResume() {
379 super.onResume();
380 updateInterface();
Dianne Hackbornb763e342014-07-22 16:23:36 -0700381 // As long as we are running, don't let this admin overlay stuff on top of the screen.
382 final int uid = mDeviceAdmin.getActivityInfo().applicationInfo.uid;
383 final String pkg = mDeviceAdmin.getActivityInfo().applicationInfo.packageName;
384 mCurSysAppOpMode = mAppOps.checkOp(AppOpsManager.OP_SYSTEM_ALERT_WINDOW, uid, pkg);
385 mCurToastAppOpMode = mAppOps.checkOp(AppOpsManager.OP_TOAST_WINDOW, uid, pkg);
386 mAppOps.setMode(AppOpsManager.OP_SYSTEM_ALERT_WINDOW, uid, pkg, AppOpsManager.MODE_IGNORED);
387 mAppOps.setMode(AppOpsManager.OP_TOAST_WINDOW, uid, pkg, AppOpsManager.MODE_IGNORED);
388 }
389
390 @Override
391 protected void onPause() {
392 super.onPause();
393 // As long as we are running, don't let this admin overlay stuff on top of the screen.
394 final int uid = mDeviceAdmin.getActivityInfo().applicationInfo.uid;
395 final String pkg = mDeviceAdmin.getActivityInfo().applicationInfo.packageName;
396 mAppOps.setMode(AppOpsManager.OP_SYSTEM_ALERT_WINDOW, uid, pkg, mCurSysAppOpMode);
397 mAppOps.setMode(AppOpsManager.OP_TOAST_WINDOW, uid, pkg, mCurToastAppOpMode);
398 try {
399 ActivityManagerNative.getDefault().resumeAppSwitches();
400 } catch (RemoteException e) {
401 }
Dianne Hackborn28429032010-01-25 18:56:17 -0800402 }
William Luh64e676a2013-11-22 15:42:40 -0800403
Dianne Hackborn61daf832010-01-27 16:22:05 -0800404 @Override
405 protected Dialog onCreateDialog(int id, Bundle args) {
406 switch (id) {
407 case DIALOG_WARNING: {
Dianne Hackborn54355112010-02-16 20:40:42 -0800408 CharSequence msg = args.getCharSequence(DeviceAdminReceiver.EXTRA_DISABLE_WARNING);
Fyodor Kupolov7dc56bd2015-02-24 09:26:53 -0800409 AlertDialog.Builder builder = new AlertDialog.Builder(this);
Dianne Hackborn61daf832010-01-27 16:22:05 -0800410 builder.setMessage(msg);
411 builder.setPositiveButton(R.string.dlg_ok,
412 new DialogInterface.OnClickListener() {
413 public void onClick(DialogInterface dialog, int which) {
Dianne Hackbornb763e342014-07-22 16:23:36 -0700414 try {
415 ActivityManagerNative.getDefault().resumeAppSwitches();
416 } catch (RemoteException e) {
417 }
Dianne Hackborn61daf832010-01-27 16:22:05 -0800418 mDPM.removeActiveAdmin(mDeviceAdmin.getComponent());
419 finish();
420 }
421 });
422 builder.setNegativeButton(R.string.dlg_cancel, null);
423 return builder.create();
424 }
425 default:
426 return super.onCreateDialog(id, args);
William Luh64e676a2013-11-22 15:42:40 -0800427
Dianne Hackborn61daf832010-01-27 16:22:05 -0800428 }
429 }
William Luh64e676a2013-11-22 15:42:40 -0800430
Dianne Hackborn28429032010-01-25 18:56:17 -0800431 void updateInterface() {
Dianne Hackborn61daf832010-01-27 16:22:05 -0800432 mAdminIcon.setImageDrawable(mDeviceAdmin.loadIcon(getPackageManager()));
433 mAdminName.setText(mDeviceAdmin.loadLabel(getPackageManager()));
Dianne Hackborn28429032010-01-25 18:56:17 -0800434 try {
Dianne Hackborn61daf832010-01-27 16:22:05 -0800435 mAdminDescription.setText(
Dianne Hackborn28429032010-01-25 18:56:17 -0800436 mDeviceAdmin.loadDescription(getPackageManager()));
Dianne Hackborn61daf832010-01-27 16:22:05 -0800437 mAdminDescription.setVisibility(View.VISIBLE);
Dianne Hackborn28429032010-01-25 18:56:17 -0800438 } catch (Resources.NotFoundException e) {
Dianne Hackborn61daf832010-01-27 16:22:05 -0800439 mAdminDescription.setVisibility(View.GONE);
Dianne Hackbornb885afe2010-01-26 18:03:42 -0800440 }
Amith Yamasanib8e0f602014-07-28 16:28:36 -0700441 if (mAddingProfileOwner) {
442 mProfileOwnerWarning.setVisibility(View.VISIBLE);
443 }
Dianne Hackbornb885afe2010-01-26 18:03:42 -0800444 if (mAddMsgText != null) {
445 mAddMsg.setText(mAddMsgText);
446 mAddMsg.setVisibility(View.VISIBLE);
447 } else {
448 mAddMsg.setVisibility(View.GONE);
Amith Yamasania1d01842011-01-24 16:02:11 -0800449 mAddMsgExpander.setVisibility(View.GONE);
Dianne Hackborn28429032010-01-25 18:56:17 -0800450 }
Amith Yamasanib8e0f602014-07-28 16:28:36 -0700451 if (!mRefreshing && !mAddingProfileOwner
452 && mDPM.isAdminActive(mDeviceAdmin.getComponent())) {
Fyodor Kupolov7dc56bd2015-02-24 09:26:53 -0800453 addDeviceAdminPolicies(false /* showDescription */);
Dianne Hackborn61daf832010-01-27 16:22:05 -0800454 mAdminWarning.setText(getString(R.string.device_admin_status,
455 mDeviceAdmin.getActivityInfo().applicationInfo.loadLabel(getPackageManager())));
Amith Yamasania1d01842011-01-24 16:02:11 -0800456 setTitle(getText(R.string.active_device_admin_msg));
Dianne Hackborn61daf832010-01-27 16:22:05 -0800457 mActionButton.setText(getText(R.string.remove_device_admin));
458 mAdding = false;
459 } else {
Fyodor Kupolov7dc56bd2015-02-24 09:26:53 -0800460 addDeviceAdminPolicies(true /* showDescription */);
Dianne Hackborn61daf832010-01-27 16:22:05 -0800461 mAdminWarning.setText(getString(R.string.device_admin_warning,
462 mDeviceAdmin.getActivityInfo().applicationInfo.loadLabel(getPackageManager())));
Amith Yamasanib8e0f602014-07-28 16:28:36 -0700463 if (mAddingProfileOwner) {
464 setTitle(getText(R.string.profile_owner_add_title));
465 } else {
466 setTitle(getText(R.string.add_device_admin_msg));
467 }
Dianne Hackborn61daf832010-01-27 16:22:05 -0800468 mActionButton.setText(getText(R.string.add_device_admin));
469 mAdding = true;
470 }
Dianne Hackborn28429032010-01-25 18:56:17 -0800471 }
Chris Palmer51794a02010-09-13 15:14:55 -0700472
Fyodor Kupolov7dc56bd2015-02-24 09:26:53 -0800473 private void addDeviceAdminPolicies(boolean showDescription) {
474 if (!mAdminPoliciesInitialized) {
475 boolean isOwner = UserHandle.getCallingUserHandle().isOwner();
476 for (DeviceAdminInfo.PolicyInfo pi : mDeviceAdmin.getUsedPolicies()) {
477 int descriptionId = isOwner ? pi.description : pi.descriptionForSecondaryUsers;
478 int labelId = isOwner ? pi.label : pi.labelForSecondaryUsers;
479 View view = AppSecurityPermissions.getPermissionItemView(this, getText(labelId),
480 showDescription ? getText(descriptionId) : "", true);
481 mAdminPolicies.addView(view);
482 }
483 mAdminPoliciesInitialized = true;
484 }
485 }
Chris Palmer51794a02010-09-13 15:14:55 -0700486
487 void toggleMessageEllipsis(View v) {
488 TextView tv = (TextView) v;
489
490 mAddMsgEllipsized = ! mAddMsgEllipsized;
491 tv.setEllipsize(mAddMsgEllipsized ? TruncateAt.END : null);
492 tv.setMaxLines(mAddMsgEllipsized ? getEllipsizedLines() : MAX_ADD_MSG_LINES);
493
Amith Yamasania1d01842011-01-24 16:02:11 -0800494 mAddMsgExpander.setImageResource(mAddMsgEllipsized ?
Chris Palmer51794a02010-09-13 15:14:55 -0700495 com.android.internal.R.drawable.expander_ic_minimized :
496 com.android.internal.R.drawable.expander_ic_maximized);
497 }
498
499 int getEllipsizedLines() {
500 Display d = ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
501 .getDefaultDisplay();
502
503 return d.getHeight() > d.getWidth() ?
504 MAX_ADD_MSG_LINES_PORTRAIT : MAX_ADD_MSG_LINES_LANDSCAPE;
505 }
506
Dianne Hackborn28429032010-01-25 18:56:17 -0800507}