blob: c1cd6f567fbfb7c8fdb94d7a82ceabd1a8c7e594 [file] [log] [blame]
Sudheer Shanka9159fe92015-11-23 12:07:05 +00001/*
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
17package com.android.settings;
18
19import android.app.Activity;
20import android.app.AlertDialog;
Kenny Guy7a355592015-12-22 12:11:41 +000021import android.app.AppGlobals;
Kenny Guy7a355592015-12-22 12:11:41 +000022import android.app.admin.DevicePolicyManager;
23import android.content.ComponentName;
Sudheer Shanka7dbbe132016-02-16 14:19:32 +000024import android.content.Context;
Sudheer Shanka9159fe92015-11-23 12:07:05 +000025import android.content.DialogInterface;
26import android.content.Intent;
Kenny Guy7a355592015-12-22 12:11:41 +000027import android.content.pm.ActivityInfo;
28import android.graphics.drawable.Drawable;
Sudheer Shanka9159fe92015-11-23 12:07:05 +000029import android.os.Bundle;
Sudheer Shanka7dbbe132016-02-16 14:19:32 +000030import android.os.Process;
Kenny Guy7a355592015-12-22 12:11:41 +000031import android.os.RemoteException;
32import android.os.UserHandle;
phweiss15931742017-03-08 19:55:48 +010033import android.os.UserManager;
Kenny Guy7a355592015-12-22 12:11:41 +000034import android.util.Log;
Sudheer Shanka9159fe92015-11-23 12:07:05 +000035import android.view.LayoutInflater;
36import android.view.View;
Kenny Guy7a355592015-12-22 12:11:41 +000037import android.widget.ImageView;
Sudheer Shanka9159fe92015-11-23 12:07:05 +000038import android.widget.TextView;
39
Sudheer Shanka8b72e562016-06-02 12:40:22 -070040import com.android.settingslib.RestrictedLockUtils;
41import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
Sudheer Shankafee4e3e2016-02-18 17:48:01 +000042
phweiss15931742017-03-08 19:55:48 +010043import java.util.Objects;
44
Sudheer Shanka9159fe92015-11-23 12:07:05 +000045public class ShowAdminSupportDetailsDialog extends Activity
46 implements DialogInterface.OnDismissListener {
47
Sudheer Shankafee4e3e2016-02-18 17:48:01 +000048 private static final String TAG = "AdminSupportDialog";
Kenny Guy7a355592015-12-22 12:11:41 +000049
Sudheer Shankafee4e3e2016-02-18 17:48:01 +000050 private EnforcedAdmin mEnforcedAdmin;
51 private View mDialogView;
phweiss15931742017-03-08 19:55:48 +010052 private String mRestriction = null;
Sudheer Shankafee4e3e2016-02-18 17:48:01 +000053
Sudheer Shanka9159fe92015-11-23 12:07:05 +000054 @Override
55 protected void onCreate(Bundle savedInstanceState) {
56 super.onCreate(savedInstanceState);
57
Sudheer Shankafee4e3e2016-02-18 17:48:01 +000058 mEnforcedAdmin = getAdminDetailsFromIntent(getIntent());
phweiss15931742017-03-08 19:55:48 +010059 mRestriction = getRestrictionFromIntent(getIntent());
Kenny Guy7a355592015-12-22 12:11:41 +000060
Nicolas Prevot637d24c2016-06-09 11:27:57 +010061 AlertDialog.Builder builder = new AlertDialog.Builder(this);
62 mDialogView = LayoutInflater.from(builder.getContext()).inflate(
Sudheer Shanka9159fe92015-11-23 12:07:05 +000063 R.layout.admin_support_details_dialog, null);
phweiss15931742017-03-08 19:55:48 +010064 initializeDialogViews(mDialogView, mEnforcedAdmin.component, mEnforcedAdmin.userId,
65 mRestriction);
Nicolas Prevot637d24c2016-06-09 11:27:57 +010066 builder.setOnDismissListener(this)
Sudheer Shanka9159fe92015-11-23 12:07:05 +000067 .setPositiveButton(R.string.okay, null)
Nicolas Prevot637d24c2016-06-09 11:27:57 +010068 .setView(mDialogView)
Sudheer Shanka9159fe92015-11-23 12:07:05 +000069 .show();
70 }
71
Sudheer Shankafee4e3e2016-02-18 17:48:01 +000072 @Override
73 public void onNewIntent(Intent intent) {
74 super.onNewIntent(intent);
75 EnforcedAdmin admin = getAdminDetailsFromIntent(intent);
phweiss15931742017-03-08 19:55:48 +010076 String restriction = getRestrictionFromIntent(intent);
77 if (!mEnforcedAdmin.equals(admin) || !Objects.equals(mRestriction, restriction)) {
Sudheer Shankafee4e3e2016-02-18 17:48:01 +000078 mEnforcedAdmin = admin;
phweiss15931742017-03-08 19:55:48 +010079 mRestriction = restriction;
80 initializeDialogViews(mDialogView, mEnforcedAdmin.component, mEnforcedAdmin.userId,
81 mRestriction);
Sudheer Shankafee4e3e2016-02-18 17:48:01 +000082 }
83 }
84
85 private EnforcedAdmin getAdminDetailsFromIntent(Intent intent) {
86 EnforcedAdmin admin = new EnforcedAdmin(null, UserHandle.myUserId());
87 if (intent == null) {
88 return admin;
89 }
Rubin Xu75a080b2016-07-28 14:35:08 +010090 admin.component = intent.getParcelableExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN);
91 admin.userId = intent.getIntExtra(Intent.EXTRA_USER_ID, UserHandle.myUserId());
Sudheer Shankafee4e3e2016-02-18 17:48:01 +000092 return admin;
93 }
94
phweiss15931742017-03-08 19:55:48 +010095 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) {
Kenny Guy7a355592015-12-22 12:11:41 +0000102 if (admin != null) {
Sudheer Shanka8b72e562016-06-02 12:40:22 -0700103 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 }
Kenny Guy7a355592015-12-22 12:11:41 +0000121 }
122 }
Sudheer Shanka9159fe92015-11-23 12:07:05 +0000123
phweiss15931742017-03-08 19:55:48 +0100124 setAdminSupportTitle(root, restriction);
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000125 setAdminSupportDetails(this, root, new EnforcedAdmin(admin, userId), true);
126 }
127
phweiss15931742017-03-08 19:55:48 +0100128 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
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000159 public static void setAdminSupportDetails(final Activity activity, View root,
160 final EnforcedAdmin enforcedAdmin, final boolean finishActivity) {
161 if (enforcedAdmin == null) {
162 return;
163 }
Sudheer Shanka8b72e562016-06-02 12:40:22 -0700164
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000165 if (enforcedAdmin.component != null) {
166 DevicePolicyManager dpm = (DevicePolicyManager) activity.getSystemService(
167 Context.DEVICE_POLICY_SERVICE);
Sudheer Shanka8b72e562016-06-02 12:40:22 -0700168 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 }
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000185 }
186 }
187
Sudheer Shanka9159fe92015-11-23 12:07:05 +0000188 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();
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000193 if (enforcedAdmin.component != null) {
194 intent.setClass(activity, DeviceAdminAdd.class);
195 intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
196 enforcedAdmin.component);
Sudheer Shanka17cbc992016-04-11 23:09:08 -0700197 intent.putExtra(DeviceAdminAdd.EXTRA_CALLED_FROM_SUPPORT_DIALOG, true);
Kenny Guy7a355592015-12-22 12:11:41 +0000198 // DeviceAdminAdd class may need to run as managed profile.
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000199 activity.startActivityAsUser(intent,
200 new UserHandle(enforcedAdmin.userId));
Kenny Guy7a355592015-12-22 12:11:41 +0000201 } else {
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000202 intent.setClass(activity, Settings.DeviceAdminSettingsActivity.class);
Sudheer Shanka65cabcd2016-05-31 11:50:18 -0700203 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Kenny Guy7a355592015-12-22 12:11:41 +0000204 // Activity merges both managed profile and parent users
205 // admins so show as same user as this activity.
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000206 activity.startActivity(intent);
Kenny Guy7a355592015-12-22 12:11:41 +0000207 }
Sudheer Shanka7dbbe132016-02-16 14:19:32 +0000208 if (finishActivity) {
209 activity.finish();
210 }
Sudheer Shanka9159fe92015-11-23 12:07:05 +0000211 }
212 });
213 }
214
215 @Override
216 public void onDismiss(DialogInterface dialog) {
217 finish();
218 }
Kenny Guy7a355592015-12-22 12:11:41 +0000219}