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.tv.settings;
18 
19 import android.annotation.NonNull;
20 import android.annotation.UserIdInt;
21 import android.app.Activity;
22 import android.app.admin.DevicePolicyManager;
23 import android.content.ComponentName;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.content.res.ColorStateList;
27 import android.content.res.TypedArray;
28 import android.graphics.drawable.Drawable;
29 import android.os.Process;
30 import android.os.UserHandle;
31 import android.os.UserManager;
32 import android.util.IconDrawableFactory;
33 import android.view.LayoutInflater;
34 import android.view.View;
35 import android.view.ViewGroup;
36 import android.widget.ImageView;
37 import android.widget.TextView;
38 
39 import androidx.annotation.VisibleForTesting;
40 import android.app.AlertDialog;
41 
42 import com.android.tv.settings.R;
43 import com.android.settingslib.RestrictedLockUtils;
44 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
45 import com.android.settingslib.RestrictedLockUtilsInternal;
46 
47 import java.util.Objects;
48 
49 /**
50  * Helper class for {@link ActionDisabledByAdminDialog} which sets up the dialog.
51  */
52 public class ActionDisabledByAdminDialogHelper {
53 
54     private static final String TAG = ActionDisabledByAdminDialogHelper.class.getName();
55     @VisibleForTesting EnforcedAdmin mEnforcedAdmin;
56     private ViewGroup mDialogView;
57     private String mRestriction = null;
58     private Activity mActivity;
59 
ActionDisabledByAdminDialogHelper(Activity activity)60     public ActionDisabledByAdminDialogHelper(Activity activity) {
61         mActivity = activity;
62     }
63 
getEnforcementAdminUserId(@onNull EnforcedAdmin admin)64     private @UserIdInt int getEnforcementAdminUserId(@NonNull EnforcedAdmin admin) {
65         if (admin.user == null) {
66             return UserHandle.USER_NULL;
67         } else {
68             return admin.user.getIdentifier();
69         }
70     }
71 
getEnforcementAdminUserId()72     private @UserIdInt int getEnforcementAdminUserId() {
73         return getEnforcementAdminUserId(mEnforcedAdmin);
74     }
75 
prepareDialogBuilder(String restriction, EnforcedAdmin enforcedAdmin)76     public AlertDialog.Builder prepareDialogBuilder(String restriction,
77             EnforcedAdmin enforcedAdmin) {
78         mEnforcedAdmin = enforcedAdmin;
79         mRestriction = restriction;
80         final AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
81         mDialogView = (ViewGroup) LayoutInflater.from(builder.getContext()).inflate(
82                 R.layout.admin_support_details_dialog, null);
83         initializeDialogViews(mDialogView, mEnforcedAdmin.component, getEnforcementAdminUserId(),
84                 mRestriction);
85         builder.setPositiveButton(R.string.okay, null).setView(mDialogView);
86         return builder;
87     }
88 
updateDialog(String restriction, EnforcedAdmin admin)89     public void updateDialog(String restriction, EnforcedAdmin admin) {
90         if (mEnforcedAdmin.equals(admin) && Objects.equals(mRestriction, restriction)) {
91             return;
92         }
93         mEnforcedAdmin = admin;
94         mRestriction = restriction;
95         initializeDialogViews(mDialogView, mEnforcedAdmin.component, getEnforcementAdminUserId(),
96                 mRestriction);
97     }
98 
initializeDialogViews(View root, ComponentName admin, int userId, String restriction)99     private void initializeDialogViews(View root, ComponentName admin, int userId,
100             String restriction) {
101         if (admin == null) {
102             return;
103         }
104 
105         setAdminSupportTitle(root, restriction);
106 
107         final UserHandle user;
108         if (userId == UserHandle.USER_NULL) {
109             user = null;
110         } else {
111             user = UserHandle.of(userId);
112         }
113 
114         setAdminSupportDetails(mActivity, root, new EnforcedAdmin(admin, user));
115     }
116 
117     @VisibleForTesting
setAdminSupportTitle(View root, String restriction)118     void setAdminSupportTitle(View root, String restriction) {
119         final TextView titleView = root.findViewById(R.id.admin_support_dialog_title);
120         if (titleView == null) {
121             return;
122         }
123         if (restriction == null) {
124             titleView.setText(R.string.disabled_by_policy_title);
125             return;
126         }
127         switch (restriction) {
128             case UserManager.DISALLOW_ADJUST_VOLUME:
129                 titleView.setText(R.string.disabled_by_policy_title_adjust_volume);
130                 break;
131             case UserManager.DISALLOW_OUTGOING_CALLS:
132                 titleView.setText(R.string.disabled_by_policy_title_outgoing_calls);
133                 break;
134             case UserManager.DISALLOW_SMS:
135                 titleView.setText(R.string.disabled_by_policy_title_sms);
136                 break;
137             case DevicePolicyManager.POLICY_DISABLE_CAMERA:
138                 titleView.setText(R.string.disabled_by_policy_title_camera);
139                 break;
140             case DevicePolicyManager.POLICY_DISABLE_SCREEN_CAPTURE:
141                 titleView.setText(R.string.disabled_by_policy_title_screen_capture);
142                 break;
143             case DevicePolicyManager.POLICY_SUSPEND_PACKAGES:
144                 titleView.setText(R.string.disabled_by_policy_title_suspend_packages);
145                 break;
146             default:
147                 // Use general text if no specialized title applies
148                 titleView.setText(R.string.disabled_by_policy_title);
149         }
150     }
151 
152     @VisibleForTesting
setAdminSupportDetails(final Activity activity, final View root, final EnforcedAdmin enforcedAdmin)153     void setAdminSupportDetails(final Activity activity, final View root,
154             final EnforcedAdmin enforcedAdmin) {
155         if (enforcedAdmin == null || enforcedAdmin.component == null) {
156             return;
157         }
158 
159         final DevicePolicyManager dpm = (DevicePolicyManager) activity.getSystemService(
160                 Context.DEVICE_POLICY_SERVICE);
161         if (!RestrictedLockUtilsInternal.isAdminInCurrentUserOrProfile(activity,
162                 enforcedAdmin.component) || !RestrictedLockUtils.isCurrentUserOrProfile(
163                 activity, getEnforcementAdminUserId(enforcedAdmin))) {
164             enforcedAdmin.component = null;
165         } else {
166             if (enforcedAdmin.user == null) {
167                 enforcedAdmin.user = UserHandle.of(UserHandle.myUserId());
168             }
169             CharSequence supportMessage = null;
170             if (UserHandle.isSameApp(Process.myUid(), Process.SYSTEM_UID)) {
171                 supportMessage = dpm.getShortSupportMessageForUser(enforcedAdmin.component,
172                         getEnforcementAdminUserId(enforcedAdmin));
173             }
174             if (supportMessage != null) {
175                 final TextView textView = root.findViewById(R.id.admin_support_msg);
176                 textView.setText(supportMessage);
177             }
178         }
179     }
180 }
181