Home | History | Annotate | Download | only in settings
      1 /*
      2  * Copyright (C) 2015 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.AlertDialog;
     21 import android.app.AppGlobals;
     22 import android.app.admin.DevicePolicyManager;
     23 import android.content.ComponentName;
     24 import android.content.Context;
     25 import android.content.DialogInterface;
     26 import android.content.Intent;
     27 import android.content.pm.ActivityInfo;
     28 import android.graphics.drawable.Drawable;
     29 import android.os.Bundle;
     30 import android.os.Process;
     31 import android.os.RemoteException;
     32 import android.os.UserHandle;
     33 import android.os.UserManager;
     34 import android.util.Log;
     35 import android.view.LayoutInflater;
     36 import android.view.View;
     37 import android.widget.ImageView;
     38 import android.widget.TextView;
     39 
     40 import com.android.settingslib.RestrictedLockUtils;
     41 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
     42 
     43 import java.util.Objects;
     44 
     45 public class ShowAdminSupportDetailsDialog extends Activity
     46         implements DialogInterface.OnDismissListener {
     47 
     48     private static final String TAG = "AdminSupportDialog";
     49 
     50     private EnforcedAdmin mEnforcedAdmin;
     51     private View mDialogView;
     52     private String mRestriction = null;
     53 
     54     @Override
     55     protected void onCreate(Bundle savedInstanceState) {
     56         super.onCreate(savedInstanceState);
     57 
     58         mEnforcedAdmin = getAdminDetailsFromIntent(getIntent());
     59         mRestriction = getRestrictionFromIntent(getIntent());
     60 
     61         AlertDialog.Builder builder = new AlertDialog.Builder(this);
     62         mDialogView = LayoutInflater.from(builder.getContext()).inflate(
     63                 R.layout.admin_support_details_dialog, null);
     64         initializeDialogViews(mDialogView, mEnforcedAdmin.component, mEnforcedAdmin.userId,
     65                 mRestriction);
     66         builder.setOnDismissListener(this)
     67                 .setPositiveButton(R.string.okay, null)
     68                 .setView(mDialogView)
     69                 .show();
     70     }
     71 
     72     @Override
     73     public void onNewIntent(Intent intent) {
     74         super.onNewIntent(intent);
     75         EnforcedAdmin admin = getAdminDetailsFromIntent(intent);
     76         String restriction = getRestrictionFromIntent(intent);
     77         if (!mEnforcedAdmin.equals(admin) || !Objects.equals(mRestriction, restriction)) {
     78             mEnforcedAdmin = admin;
     79             mRestriction = restriction;
     80             initializeDialogViews(mDialogView, mEnforcedAdmin.component, mEnforcedAdmin.userId,
     81                     mRestriction);
     82         }
     83     }
     84 
     85     private EnforcedAdmin getAdminDetailsFromIntent(Intent intent) {
     86         EnforcedAdmin admin = new EnforcedAdmin(null, UserHandle.myUserId());
     87         if (intent == null) {
     88             return admin;
     89         }
     90         admin.component = intent.getParcelableExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN);
     91         admin.userId = intent.getIntExtra(Intent.EXTRA_USER_ID, UserHandle.myUserId());
     92         return admin;
     93     }
     94 
     95     private String getRestrictionFromIntent(Intent intent) {
     96         if (intent == null) return null;
     97         return intent.getStringExtra(DevicePolicyManager.EXTRA_RESTRICTION);
     98     }
     99 
    100     private void initializeDialogViews(View root, ComponentName admin, int userId,
    101                                                String restriction) {
    102         if (admin != null) {
    103             if (!RestrictedLockUtils.isAdminInCurrentUserOrProfile(this, admin)
    104                     || !RestrictedLockUtils.isCurrentUserOrProfile(this, userId)) {
    105                 admin = null;
    106             } else {
    107                 ActivityInfo ai = null;
    108                 try {
    109                     ai = AppGlobals.getPackageManager().getReceiverInfo(admin, 0 /* flags */,
    110                             userId);
    111                 } catch (RemoteException e) {
    112                     Log.w(TAG, "Missing reciever info", e);
    113                 }
    114                 if (ai != null) {
    115                     Drawable icon = ai.loadIcon(getPackageManager());
    116                     Drawable badgedIcon = getPackageManager().getUserBadgedIcon(
    117                             icon, new UserHandle(userId));
    118                     ((ImageView) root.findViewById(R.id.admin_support_icon)).setImageDrawable(
    119                             badgedIcon);
    120                 }
    121             }
    122         }
    123 
    124         setAdminSupportTitle(root, restriction);
    125         setAdminSupportDetails(this, root, new EnforcedAdmin(admin, userId), true);
    126     }
    127 
    128     private void setAdminSupportTitle(View root, String restriction) {
    129         final TextView titleView = (TextView) root.findViewById(R.id.admin_support_dialog_title);
    130         if (titleView == null) {
    131             return;
    132         }
    133         if (restriction == null) {
    134             titleView.setText(R.string.disabled_by_policy_title);
    135             return;
    136         }
    137         switch(restriction) {
    138             case UserManager.DISALLOW_ADJUST_VOLUME:
    139                 titleView.setText(R.string.disabled_by_policy_title_adjust_volume);
    140                 break;
    141             case UserManager.DISALLOW_OUTGOING_CALLS:
    142                 titleView.setText(R.string.disabled_by_policy_title_outgoing_calls);
    143                 break;
    144             case UserManager.DISALLOW_SMS:
    145                 titleView.setText(R.string.disabled_by_policy_title_sms);
    146                 break;
    147             case DevicePolicyManager.POLICY_DISABLE_CAMERA:
    148                 titleView.setText(R.string.disabled_by_policy_title_camera);
    149                 break;
    150             case DevicePolicyManager.POLICY_DISABLE_SCREEN_CAPTURE:
    151                 titleView.setText(R.string.disabled_by_policy_title_screen_capture);
    152                 break;
    153             default:
    154                 // Use general text if no specialized title applies
    155                 titleView.setText(R.string.disabled_by_policy_title);
    156         }
    157     }
    158 
    159     public static void setAdminSupportDetails(final Activity activity, View root,
    160             final EnforcedAdmin enforcedAdmin, final boolean finishActivity) {
    161         if (enforcedAdmin == null) {
    162             return;
    163         }
    164 
    165         if (enforcedAdmin.component != null) {
    166             DevicePolicyManager dpm = (DevicePolicyManager) activity.getSystemService(
    167                     Context.DEVICE_POLICY_SERVICE);
    168             if (!RestrictedLockUtils.isAdminInCurrentUserOrProfile(activity,
    169                     enforcedAdmin.component) || !RestrictedLockUtils.isCurrentUserOrProfile(
    170                     activity, enforcedAdmin.userId)) {
    171                 enforcedAdmin.component = null;
    172             } else {
    173                 if (enforcedAdmin.userId == UserHandle.USER_NULL) {
    174                     enforcedAdmin.userId = UserHandle.myUserId();
    175                 }
    176                 CharSequence supportMessage = null;
    177                 if (UserHandle.isSameApp(Process.myUid(), Process.SYSTEM_UID)) {
    178                     supportMessage = dpm.getShortSupportMessageForUser(
    179                             enforcedAdmin.component, enforcedAdmin.userId);
    180                 }
    181                 if (supportMessage != null) {
    182                     TextView textView = (TextView) root.findViewById(R.id.admin_support_msg);
    183                     textView.setText(supportMessage);
    184                 }
    185             }
    186         }
    187 
    188         root.findViewById(R.id.admins_policies_list).setOnClickListener(
    189                 new View.OnClickListener() {
    190                     @Override
    191                     public void onClick(View view) {
    192                         Intent intent = new Intent();
    193                         if (enforcedAdmin.component != null) {
    194                             intent.setClass(activity, DeviceAdminAdd.class);
    195                             intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
    196                                     enforcedAdmin.component);
    197                             intent.putExtra(DeviceAdminAdd.EXTRA_CALLED_FROM_SUPPORT_DIALOG, true);
    198                             // DeviceAdminAdd class may need to run as managed profile.
    199                             activity.startActivityAsUser(intent,
    200                                     new UserHandle(enforcedAdmin.userId));
    201                         } else {
    202                             intent.setClass(activity, Settings.DeviceAdminSettingsActivity.class);
    203                             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    204                             // Activity merges both managed profile and parent users
    205                             // admins so show as same user as this activity.
    206                             activity.startActivity(intent);
    207                         }
    208                         if (finishActivity) {
    209                             activity.finish();
    210                         }
    211                     }
    212                 });
    213     }
    214 
    215     @Override
    216     public void onDismiss(DialogInterface dialog) {
    217         finish();
    218     }
    219 }
    220