2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings;
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;
40 import com.android.settingslib.RestrictedLockUtils;
41 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
43 import java.util.Objects;
45 public class ShowAdminSupportDetailsDialog extends Activity
46 implements DialogInterface.OnDismissListener {
48 private static final String TAG = "AdminSupportDialog";
50 private EnforcedAdmin mEnforcedAdmin;
51 private View mDialogView;
52 private String mRestriction = null;
55 protected void onCreate(Bundle savedInstanceState) {
56 super.onCreate(savedInstanceState);
58 mEnforcedAdmin = getAdminDetailsFromIntent(getIntent());
59 mRestriction = getRestrictionFromIntent(getIntent());
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,
66 builder.setOnDismissListener(this)
67 .setPositiveButton(R.string.okay, null)
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,
85 private EnforcedAdmin getAdminDetailsFromIntent(Intent intent) {
86 EnforcedAdmin admin = new EnforcedAdmin(null, UserHandle.myUserId());
90 admin.component = intent.getParcelableExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN);
91 admin.userId = intent.getIntExtra(Intent.EXTRA_USER_ID, UserHandle.myUserId());
95 private String getRestrictionFromIntent(Intent intent) {
96 if (intent == null) return null;
97 return intent.getStringExtra(DevicePolicyManager.EXTRA_RESTRICTION);
100 private void initializeDialogViews(View root, ComponentName admin, int userId,
101 String restriction) {
103 if (!RestrictedLockUtils.isAdminInCurrentUserOrProfile(this, admin)
104 || !RestrictedLockUtils.isCurrentUserOrProfile(this, userId)) {
107 ActivityInfo ai = null;
109 ai = AppGlobals.getPackageManager().getReceiverInfo(admin, 0 /* flags */,
111 } catch (RemoteException e) {
112 Log.w(TAG, "Missing reciever info", e);
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(
124 setAdminSupportTitle(root, restriction);
125 setAdminSupportDetails(this, root, new EnforcedAdmin(admin, userId), true);
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) {
133 if (restriction == null) {
134 titleView.setText(R.string.disabled_by_policy_title);
137 switch(restriction) {
138 case UserManager.DISALLOW_ADJUST_VOLUME:
139 titleView.setText(R.string.disabled_by_policy_title_adjust_volume);
141 case UserManager.DISALLOW_OUTGOING_CALLS:
142 titleView.setText(R.string.disabled_by_policy_title_outgoing_calls);
144 case UserManager.DISALLOW_SMS:
145 titleView.setText(R.string.disabled_by_policy_title_sms);
147 case DevicePolicyManager.POLICY_DISABLE_CAMERA:
148 titleView.setText(R.string.disabled_by_policy_title_camera);
150 case DevicePolicyManager.POLICY_DISABLE_SCREEN_CAPTURE:
151 titleView.setText(R.string.disabled_by_policy_title_screen_capture);
154 // Use general text if no specialized title applies
155 titleView.setText(R.string.disabled_by_policy_title);
159 public static void setAdminSupportDetails(final Activity activity, View root,
160 final EnforcedAdmin enforcedAdmin, final boolean finishActivity) {
161 if (enforcedAdmin == null) {
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;
173 if (enforcedAdmin.userId == UserHandle.USER_NULL) {
174 enforcedAdmin.userId = UserHandle.myUserId();
176 CharSequence supportMessage = null;
177 if (UserHandle.isSameApp(Process.myUid(), Process.SYSTEM_UID)) {
178 supportMessage = dpm.getShortSupportMessageForUser(
179 enforcedAdmin.component, enforcedAdmin.userId);
181 if (supportMessage != null) {
182 TextView textView = (TextView) root.findViewById(R.id.admin_support_msg);
183 textView.setText(supportMessage);
188 root.findViewById(R.id.admins_policies_list).setOnClickListener(
189 new View.OnClickListener() {
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));
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);
208 if (finishActivity) {
216 public void onDismiss(DialogInterface dialog) {