Home | History | Annotate | Download | only in settings
      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 
     19 import android.app.Activity;
     20 import android.app.ActivityManager;
     21 import android.app.AlertDialog;
     22 import android.app.AppOpsManager;
     23 import android.app.Dialog;
     24 import android.app.admin.DeviceAdminInfo;
     25 import android.app.admin.DeviceAdminReceiver;
     26 import android.app.admin.DevicePolicyManager;
     27 import android.content.ComponentName;
     28 import android.content.Context;
     29 import android.content.DialogInterface;
     30 import android.content.Intent;
     31 import android.content.pm.ActivityInfo;
     32 import android.content.pm.ApplicationInfo;
     33 import android.content.pm.PackageInfo;
     34 import android.content.pm.PackageManager;
     35 import android.content.pm.PackageManager.NameNotFoundException;
     36 import android.content.pm.ResolveInfo;
     37 import android.content.pm.UserInfo;
     38 import android.content.res.Resources;
     39 import android.os.Binder;
     40 import android.os.Bundle;
     41 import android.os.Handler;
     42 import android.os.IBinder;
     43 import android.os.RemoteCallback;
     44 import android.os.RemoteException;
     45 import android.os.UserHandle;
     46 import android.os.UserManager;
     47 import android.text.TextUtils;
     48 import android.text.TextUtils.TruncateAt;
     49 import android.util.EventLog;
     50 import android.util.Log;
     51 import android.view.Display;
     52 import android.view.View;
     53 import android.view.ViewGroup;
     54 import android.view.ViewTreeObserver;
     55 import android.view.WindowManager;
     56 import android.widget.AppSecurityPermissions;
     57 import android.widget.Button;
     58 import android.widget.ImageView;
     59 import android.widget.TextView;
     60 
     61 import com.android.internal.logging.nano.MetricsProto;
     62 import com.android.settings.overlay.FeatureFactory;
     63 import com.android.settings.users.UserDialogs;
     64 import com.android.settingslib.RestrictedLockUtils;
     65 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
     66 
     67 import org.xmlpull.v1.XmlPullParserException;
     68 
     69 import java.io.IOException;
     70 import java.util.ArrayList;
     71 import java.util.List;
     72 
     73 public class DeviceAdminAdd extends Activity {
     74     static final String TAG = "DeviceAdminAdd";
     75 
     76     static final int DIALOG_WARNING = 1;
     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;
     81 
     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 
     89     public static final String EXTRA_CALLED_FROM_SUPPORT_DIALOG =
     90             "android.app.extra.CALLED_FROM_SUPPORT_DIALOG";
     91 
     92     private final IBinder mToken = new Binder();
     93     Handler mHandler;
     94 
     95     DevicePolicyManager mDPM;
     96     AppOpsManager mAppOps;
     97     DeviceAdminInfo mDeviceAdmin;
     98     CharSequence mAddMsgText;
     99     String mProfileOwnerName;
    100 
    101     ImageView mAdminIcon;
    102     TextView mAdminName;
    103     TextView mAdminDescription;
    104     TextView mAddMsg;
    105     TextView mProfileOwnerWarning;
    106     ImageView mAddMsgExpander;
    107     boolean mAddMsgEllipsized = true;
    108     TextView mAdminWarning;
    109     TextView mSupportMessage;
    110     ViewGroup mAdminPolicies;
    111     Button mActionButton;
    112     Button mUninstallButton;
    113     Button mCancelButton;
    114 
    115     boolean mUninstalling = false;
    116     boolean mAdding;
    117     boolean mRefreshing;
    118     boolean mWaitingForRemoveMsg;
    119     boolean mAddingProfileOwner;
    120     boolean mAdminPoliciesInitialized;
    121 
    122     boolean mIsCalledFromSupportDialog = false;
    123 
    124     @Override
    125     protected void onCreate(Bundle icicle) {
    126         super.onCreate(icicle);
    127 
    128         mHandler = new Handler(getMainLooper());
    129 
    130         mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
    131         mAppOps = (AppOpsManager)getSystemService(Context.APP_OPS_SERVICE);
    132         PackageManager packageManager = getPackageManager();
    133 
    134         if ((getIntent().getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
    135             Log.w(TAG, "Cannot start ADD_DEVICE_ADMIN as a new task");
    136             finish();
    137             return;
    138         }
    139 
    140         mIsCalledFromSupportDialog = getIntent().getBooleanExtra(
    141                 EXTRA_CALLED_FROM_SUPPORT_DIALOG, false);
    142 
    143         String action = getIntent().getAction();
    144         ComponentName who = (ComponentName)getIntent().getParcelableExtra(
    145                 DevicePolicyManager.EXTRA_DEVICE_ADMIN);
    146         if (who == null) {
    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             }
    160         }
    161 
    162         if (action != null && action.equals(DevicePolicyManager.ACTION_SET_PROFILE_OWNER)) {
    163             setResult(RESULT_CANCELED);
    164             setFinishOnTouchOutside(true);
    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             }
    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             }
    186         }
    187 
    188         ActivityInfo ai;
    189         try {
    190             ai = packageManager.getReceiverInfo(who, PackageManager.GET_META_DATA);
    191         } catch (PackageManager.NameNotFoundException e) {
    192             Log.w(TAG, "Unable to retrieve device policy " + who, e);
    193             finish();
    194             return;
    195         }
    196 
    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.
    200         if (!mDPM.isAdminActive(who)) {
    201             List<ResolveInfo> avail = packageManager.queryBroadcastReceivers(
    202                     new Intent(DeviceAdminReceiver.ACTION_DEVICE_ADMIN_ENABLED),
    203                     PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS);
    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;
    222                 }
    223             }
    224             if (!found) {
    225                 Log.w(TAG, "Request to add invalid device admin: " + who);
    226                 finish();
    227                 return;
    228             }
    229         }
    230 
    231         ResolveInfo ri = new ResolveInfo();
    232         ri.activityInfo = ai;
    233         try {
    234             mDeviceAdmin = new DeviceAdminInfo(this, ri);
    235         } catch (XmlPullParserException e) {
    236             Log.w(TAG, "Unable to retrieve device policy " + who, e);
    237             finish();
    238             return;
    239         } catch (IOException e) {
    240             Log.w(TAG, "Unable to retrieve device policy " + who, e);
    241             finish();
    242             return;
    243         }
    244 
    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;
    250             if (mDPM.isAdminActive(who)) {
    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 
    257                 ArrayList<DeviceAdminInfo.PolicyInfo> newPolicies = mDeviceAdmin.getUsedPolicies();
    258                 for (int i = 0; i < newPolicies.size(); i++) {
    259                     DeviceAdminInfo.PolicyInfo pi = newPolicies.get(i);
    260                     if (!mDPM.hasGrantedPolicy(who, pi.ident)) {
    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         }
    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 
    281         mAddMsgText = getIntent().getCharSequenceExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION);
    282 
    283         setContentView(R.layout.device_admin_add);
    284 
    285         mAdminIcon = (ImageView)findViewById(R.id.admin_icon);
    286         mAdminName = (TextView)findViewById(R.id.admin_name);
    287         mAdminDescription = (TextView)findViewById(R.id.admin_description);
    288         mProfileOwnerWarning = (TextView) findViewById(R.id.profile_owner_warning);
    289 
    290         mAddMsg = (TextView)findViewById(R.id.add_msg);
    291         mAddMsgExpander = (ImageView) findViewById(R.id.add_msg_expander);
    292         final View.OnClickListener onClickListener = new View.OnClickListener() {
    293             @Override
    294             public void onClick(View v) {
    295                 toggleMessageEllipsis(mAddMsg);
    296             }
    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                 });
    318 
    319         // toggleMessageEllipsis also handles initial layout:
    320         toggleMessageEllipsis(mAddMsg);
    321 
    322         mAdminWarning = (TextView) findViewById(R.id.admin_warning);
    323         mAdminPolicies = (ViewGroup) findViewById(R.id.admin_policies);
    324         mSupportMessage = (TextView) findViewById(R.id.admin_support_message);
    325 
    326         mCancelButton = (Button) findViewById(R.id.cancel_button);
    327         mCancelButton.setFilterTouchesWhenObscured(true);
    328         mCancelButton.setOnClickListener(new View.OnClickListener() {
    329             public void onClick(View v) {
    330                 EventLog.writeEvent(EventLogTags.EXP_DET_DEVICE_ADMIN_DECLINED_BY_USER,
    331                     mDeviceAdmin.getActivityInfo().applicationInfo.uid);
    332                 finish();
    333             }
    334         });
    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);
    342                 mDPM.uninstallPackageWithActiveAdmins(mDeviceAdmin.getPackageName());
    343                 finish();
    344             }
    345         });
    346 
    347         mActionButton = (Button) findViewById(R.id.action_button);
    348 
    349         final View restrictedAction = findViewById(R.id.restricted_action);
    350         restrictedAction.setFilterTouchesWhenObscured(true);
    351         restrictedAction.setOnClickListener(new View.OnClickListener() {
    352             public void onClick(View v) {
    353                 if (!mActionButton.isEnabled()) {
    354                     showPolicyTransparencyDialogIfRequired();
    355                     return;
    356                 }
    357                 if (mAdding) {
    358                     addAndFinish();
    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                             }
    371                     ).show();
    372                 } else if (mUninstalling) {
    373                     mDPM.uninstallPackageWithActiveAdmins(mDeviceAdmin.getPackageName());
    374                     finish();
    375                 } else if (!mWaitingForRemoveMsg) {
    376                     try {
    377                         // Don't allow the admin to put a dialog up in front
    378                         // of us while we interact with the user.
    379                         ActivityManager.getService().stopAppSwitches();
    380                     } catch (RemoteException e) {
    381                     }
    382                     mWaitingForRemoveMsg = true;
    383                     mDPM.getRemoveWarning(mDeviceAdmin.getComponent(),
    384                             new RemoteCallback(new RemoteCallback.OnResultListener() {
    385                                 @Override
    386                                 public void onResult(Bundle result) {
    387                                     CharSequence msg = result != null
    388                                             ? result.getCharSequence(
    389                                             DeviceAdminReceiver.EXTRA_DISABLE_WARNING)
    390                                             : null;
    391                                     continueRemoveAction(msg);
    392                                 }
    393                             }, mHandler));
    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);
    400                 }
    401             }
    402         });
    403     }
    404 
    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 
    425     void addAndFinish() {
    426         try {
    427             logSpecialPermissionChange(true, mDeviceAdmin.getComponent().getPackageName());
    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 
    452     void continueRemoveAction(CharSequence msg) {
    453         if (!mWaitingForRemoveMsg) {
    454             return;
    455         }
    456         mWaitingForRemoveMsg = false;
    457         if (msg == null) {
    458             try {
    459                 ActivityManager.getService().resumeAppSwitches();
    460             } catch (RemoteException e) {
    461             }
    462             logSpecialPermissionChange(false, mDeviceAdmin.getComponent().getPackageName());
    463             mDPM.removeActiveAdmin(mDeviceAdmin.getComponent());
    464             finish();
    465         } else {
    466             try {
    467                 // Continue preventing anything from coming in front.
    468                 ActivityManager.getService().stopAppSwitches();
    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 
    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 
    484     @Override
    485     protected void onResume() {
    486         super.onResume();
    487         mActionButton.setEnabled(true);
    488         updateInterface();
    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 
    493     }
    494 
    495     @Override
    496     protected void onPause() {
    497         super.onPause();
    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);
    502         try {
    503             ActivityManager.getService().resumeAppSwitches();
    504         } catch (RemoteException e) {
    505         }
    506     }
    507 
    508     @Override
    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
    521     protected Dialog onCreateDialog(int id, Bundle args) {
    522         switch (id) {
    523             case DIALOG_WARNING: {
    524                 CharSequence msg = args.getCharSequence(DeviceAdminReceiver.EXTRA_DISABLE_WARNING);
    525                 AlertDialog.Builder builder = new AlertDialog.Builder(this);
    526                 builder.setMessage(msg);
    527                 builder.setPositiveButton(R.string.dlg_ok,
    528                         new DialogInterface.OnClickListener() {
    529                     public void onClick(DialogInterface dialog, int which) {
    530                         try {
    531                             ActivityManager.getService().resumeAppSwitches();
    532                         } catch (RemoteException e) {
    533                         }
    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);
    543 
    544         }
    545     }
    546 
    547     void updateInterface() {
    548         findViewById(R.id.restricted_icon).setVisibility(View.GONE);
    549         mAdminIcon.setImageDrawable(mDeviceAdmin.loadIcon(getPackageManager()));
    550         mAdminName.setText(mDeviceAdmin.loadLabel(getPackageManager()));
    551         try {
    552             mAdminDescription.setText(
    553                     mDeviceAdmin.loadDescription(getPackageManager()));
    554             mAdminDescription.setVisibility(View.VISIBLE);
    555         } catch (Resources.NotFoundException e) {
    556             mAdminDescription.setVisibility(View.GONE);
    557         }
    558         if (mAddingProfileOwner) {
    559             mProfileOwnerWarning.setVisibility(View.VISIBLE);
    560         }
    561         if (mAddMsgText != null) {
    562             mAddMsg.setText(mAddMsgText);
    563             mAddMsg.setVisibility(View.VISIBLE);
    564         } else {
    565             mAddMsg.setVisibility(View.GONE);
    566             mAddMsgExpander.setVisibility(View.GONE);
    567         }
    568         if (!mRefreshing && !mAddingProfileOwner
    569                 && mDPM.isAdminActive(mDeviceAdmin.getComponent())) {
    570             mAdding = false;
    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);
    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);
    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);
    603                 if (mUninstalling) {
    604                     mActionButton.setText(R.string.remove_and_uninstall_device_admin);
    605                 } else {
    606                     mActionButton.setText(R.string.remove_device_admin);
    607                 }
    608             }
    609             CharSequence supportMessage = mDPM.getLongSupportMessageForUser(
    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             }
    617         } else {
    618             addDeviceAdminPolicies(true /* showDescription */);
    619             mAdminWarning.setText(getString(R.string.device_admin_warning,
    620                     mDeviceAdmin.getActivityInfo().applicationInfo.loadLabel(getPackageManager())));
    621             if (mAddingProfileOwner) {
    622                 setTitle(getText(R.string.profile_owner_add_title));
    623             } else {
    624                 setTitle(getText(R.string.add_device_admin_msg));
    625             }
    626             mActionButton.setText(getText(R.string.add_device_admin));
    627             if (isAdminUninstallable()) {
    628                 mUninstallButton.setVisibility(View.VISIBLE);
    629             }
    630             mSupportMessage.setVisibility(View.GONE);
    631             mAdding = true;
    632         }
    633     }
    634 
    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 
    651     private void addDeviceAdminPolicies(boolean showDescription) {
    652         if (!mAdminPoliciesInitialized) {
    653             boolean isAdminUser = UserManager.get(this).isAdminUser();
    654             for (DeviceAdminInfo.PolicyInfo pi : mDeviceAdmin.getUsedPolicies()) {
    655                 int descriptionId = isAdminUser ? pi.description : pi.descriptionForSecondaryUsers;
    656                 int labelId = isAdminUser ? pi.label : pi.labelForSecondaryUsers;
    657                 View view = AppSecurityPermissions.getPermissionItemView(this, getText(labelId),
    658                         showDescription ? getText(descriptionId) : "", true);
    659                 mAdminPolicies.addView(view);
    660             }
    661             mAdminPoliciesInitialized = true;
    662         }
    663     }
    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 
    672         mAddMsgExpander.setImageResource(mAddMsgEllipsized ?
    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 
    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     }
    694 
    695     private boolean isAdminUninstallable() {
    696         // System apps can't be uninstalled.
    697         return !mDeviceAdmin.getActivityInfo().applicationInfo.isSystemApp();
    698     }
    699 }
    700