1 /* 2 * Copyright (C) 2018 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.enterprise; 18 19 import static android.app.admin.DevicePolicyResources.Strings.Settings.DISABLED_BY_IT_ADMIN_TITLE; 20 21 import android.annotation.NonNull; 22 import android.annotation.UserIdInt; 23 import android.app.Activity; 24 import android.app.admin.DevicePolicyManager; 25 import android.content.ComponentName; 26 import android.content.Context; 27 import android.os.Process; 28 import android.os.UserHandle; 29 import android.view.LayoutInflater; 30 import android.view.View; 31 import android.view.ViewGroup; 32 import android.widget.ImageView; 33 import android.widget.TextView; 34 35 import androidx.annotation.VisibleForTesting; 36 import androidx.appcompat.app.AlertDialog; 37 38 import com.android.settings.R; 39 import com.android.settingslib.RestrictedLockUtils; 40 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; 41 import com.android.settingslib.RestrictedLockUtilsInternal; 42 import com.android.settingslib.Utils; 43 import com.android.settingslib.enterprise.ActionDisabledByAdminController; 44 import com.android.settingslib.enterprise.ActionDisabledByAdminControllerFactory; 45 46 import java.util.Objects; 47 48 /** 49 * Helper class for {@link ActionDisabledByAdminDialog} which sets up the dialog. 50 */ 51 public final class ActionDisabledByAdminDialogHelper { 52 53 private static final String TAG = ActionDisabledByAdminDialogHelper.class.getName(); 54 @VisibleForTesting EnforcedAdmin mEnforcedAdmin; 55 private ViewGroup mDialogView; 56 private String mRestriction; 57 private final ActionDisabledByAdminController mActionDisabledByAdminController; 58 private final Activity mActivity; 59 ActionDisabledByAdminDialogHelper(Activity activity)60 public ActionDisabledByAdminDialogHelper(Activity activity) { 61 this(activity, null /* restriction */); 62 } 63 ActionDisabledByAdminDialogHelper(Activity activity, String restriction)64 public ActionDisabledByAdminDialogHelper(Activity activity, String restriction) { 65 mActivity = activity; 66 mDialogView = (ViewGroup) LayoutInflater.from(mActivity).inflate( 67 R.layout.support_details_dialog, null); 68 mActionDisabledByAdminController = ActionDisabledByAdminControllerFactory 69 .createInstance(mActivity, restriction, 70 new DeviceAdminStringProviderImpl(mActivity), 71 UserHandle.SYSTEM); 72 DevicePolicyManager devicePolicyManager = 73 mActivity.getSystemService(DevicePolicyManager.class); 74 75 TextView title = mDialogView.findViewById(R.id.admin_support_dialog_title); 76 title.setText(devicePolicyManager.getResources().getString(DISABLED_BY_IT_ADMIN_TITLE, 77 () -> mActivity.getString(R.string.disabled_by_policy_title))); 78 79 } 80 getEnforcementAdminUserId(@onNull EnforcedAdmin admin)81 private @UserIdInt int getEnforcementAdminUserId(@NonNull EnforcedAdmin admin) { 82 return admin.user == null ? UserHandle.USER_NULL : admin.user.getIdentifier(); 83 } 84 getEnforcementAdminUserId()85 private @UserIdInt int getEnforcementAdminUserId() { 86 return getEnforcementAdminUserId(mEnforcedAdmin); 87 } 88 prepareDialogBuilder(String restriction, EnforcedAdmin enforcedAdmin)89 public AlertDialog.Builder prepareDialogBuilder(String restriction, 90 EnforcedAdmin enforcedAdmin) { 91 AlertDialog.Builder builder = new AlertDialog.Builder(mActivity) 92 .setPositiveButton(R.string.suggestion_button_close, 93 mActionDisabledByAdminController 94 .getPositiveButtonListener(mActivity, enforcedAdmin)) 95 .setView(mDialogView); 96 prepareDialogBuilder(builder, restriction, enforcedAdmin); 97 return builder; 98 } 99 100 @VisibleForTesting prepareDialogBuilder(AlertDialog.Builder builder, String restriction, EnforcedAdmin enforcedAdmin)101 void prepareDialogBuilder(AlertDialog.Builder builder, String restriction, 102 EnforcedAdmin enforcedAdmin) { 103 mActionDisabledByAdminController.initialize( 104 new ActionDisabledLearnMoreButtonLauncherImpl(mActivity, builder)); 105 106 mEnforcedAdmin = enforcedAdmin; 107 mRestriction = restriction; 108 initializeDialogViews(mDialogView, mEnforcedAdmin, getEnforcementAdminUserId(), 109 mRestriction); 110 mActionDisabledByAdminController.setupLearnMoreButton(mActivity); 111 } 112 updateDialog(String restriction, EnforcedAdmin admin)113 public void updateDialog(String restriction, EnforcedAdmin admin) { 114 if (mEnforcedAdmin.equals(admin) && Objects.equals(mRestriction, restriction)) { 115 return; 116 } 117 mEnforcedAdmin = admin; 118 mRestriction = restriction; 119 initializeDialogViews(mDialogView, mEnforcedAdmin, getEnforcementAdminUserId(), 120 mRestriction); 121 } 122 initializeDialogViews(View root, EnforcedAdmin enforcedAdmin, int userId, String restriction)123 private void initializeDialogViews(View root, EnforcedAdmin enforcedAdmin, int userId, 124 String restriction) { 125 ComponentName admin = enforcedAdmin.component; 126 if (admin == null) { 127 return; 128 } 129 130 mActionDisabledByAdminController.updateEnforcedAdmin(enforcedAdmin, userId); 131 setAdminSupportIcon(root, admin, userId); 132 133 if (isNotCurrentUserOrProfile(admin, userId)) { 134 admin = null; 135 } 136 137 setAdminSupportTitle(root, restriction); 138 139 final UserHandle user; 140 if (userId == UserHandle.USER_NULL) { 141 user = null; 142 } else { 143 user = UserHandle.of(userId); 144 } 145 146 setAdminSupportDetails(mActivity, root, new EnforcedAdmin(admin, user)); 147 } 148 isNotCurrentUserOrProfile(ComponentName admin, int userId)149 private boolean isNotCurrentUserOrProfile(ComponentName admin, int userId) { 150 return !RestrictedLockUtilsInternal.isAdminInCurrentUserOrProfile(mActivity, admin) 151 || !RestrictedLockUtils.isCurrentUserOrProfile(mActivity, userId); 152 } 153 154 @VisibleForTesting setAdminSupportIcon(View root, ComponentName admin, int userId)155 void setAdminSupportIcon(View root, ComponentName admin, int userId) { 156 ImageView supportIconView = root.requireViewById(R.id.admin_support_icon); 157 supportIconView.setImageDrawable( 158 mActivity.getDrawable(R.drawable.ic_lock_closed)); 159 160 supportIconView.setImageTintList(Utils.getColorAccent(mActivity)); 161 } 162 163 @VisibleForTesting setAdminSupportTitle(View root, String restriction)164 void setAdminSupportTitle(View root, String restriction) { 165 final TextView titleView = root.findViewById(R.id.admin_support_dialog_title); 166 if (titleView == null) { 167 return; 168 } 169 titleView.setText(mActionDisabledByAdminController.getAdminSupportTitle(restriction)); 170 } 171 172 @VisibleForTesting setAdminSupportDetails(final Activity activity, final View root, final EnforcedAdmin enforcedAdmin)173 void setAdminSupportDetails(final Activity activity, final View root, 174 final EnforcedAdmin enforcedAdmin) { 175 if (enforcedAdmin == null || enforcedAdmin.component == null) { 176 return; 177 } 178 179 final DevicePolicyManager dpm = (DevicePolicyManager) activity.getSystemService( 180 Context.DEVICE_POLICY_SERVICE); 181 CharSequence supportMessage = null; 182 if (!RestrictedLockUtilsInternal.isAdminInCurrentUserOrProfile(activity, 183 enforcedAdmin.component) || !RestrictedLockUtils.isCurrentUserOrProfile( 184 activity, getEnforcementAdminUserId(enforcedAdmin))) { 185 enforcedAdmin.component = null; 186 } else { 187 if (enforcedAdmin.user == null) { 188 enforcedAdmin.user = UserHandle.of(UserHandle.myUserId()); 189 } 190 if (UserHandle.isSameApp(Process.myUid(), Process.SYSTEM_UID)) { 191 supportMessage = dpm.getShortSupportMessageForUser(enforcedAdmin.component, 192 getEnforcementAdminUserId(enforcedAdmin)); 193 } 194 } 195 final CharSequence supportContentString = 196 mActionDisabledByAdminController.getAdminSupportContentString( 197 mActivity, supportMessage); 198 final TextView textView = root.findViewById(R.id.admin_support_msg); 199 if (supportContentString != null) { 200 textView.setText(supportContentString); 201 } 202 } 203 } 204