1/*
2 * Copyright (C) 2014 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.users;
18
19import android.app.AlertDialog;
20import android.app.Dialog;
21import android.content.Context;
22import android.content.DialogInterface;
23import android.content.pm.ApplicationInfo;
24import android.content.pm.PackageManager;
25import android.content.pm.UserInfo;
26import android.graphics.drawable.Drawable;
27import android.os.UserHandle;
28import android.os.UserManager;
29import android.view.LayoutInflater;
30import android.view.View;
31import android.widget.ImageView;
32import android.widget.TextView;
33
34import com.android.settings.R;
35import com.android.settings.Utils;
36
37/**
38 * Helper class for displaying dialogs related to user settings.
39 */
40public final class UserDialogs {
41
42    /**
43     * Creates a dialog to confirm with the user if it's ok to remove the user
44     * and delete all the data.
45     *
46     * @param context a Context object
47     * @param removingUserId The userId of the user to remove
48     * @param onConfirmListener Callback object for positive action
49     * @return the created Dialog
50     */
51    public static Dialog createRemoveDialog(Context context, int removingUserId,
52            DialogInterface.OnClickListener onConfirmListener) {
53        UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
54        UserInfo userInfo = um.getUserInfo(removingUserId);
55        AlertDialog.Builder builder = new AlertDialog.Builder(context)
56                .setPositiveButton(R.string.user_delete_button, onConfirmListener)
57                .setNegativeButton(android.R.string.cancel, null);
58        if (UserHandle.myUserId() == removingUserId) {
59            builder.setTitle(R.string.user_confirm_remove_self_title);
60            builder.setMessage(R.string.user_confirm_remove_self_message);
61        } else if (userInfo.isRestricted()) {
62            builder.setTitle(R.string.user_profile_confirm_remove_title);
63            builder.setMessage(R.string.user_profile_confirm_remove_message);
64        } else if (userInfo.isManagedProfile()) {
65            builder.setTitle(R.string.work_profile_confirm_remove_title);
66            View view = createRemoveManagedUserDialogView(context, removingUserId);
67            if (view != null) {
68                builder.setView(view);
69            } else {
70                builder.setMessage(R.string.work_profile_confirm_remove_message);
71            }
72        } else {
73            builder.setTitle(R.string.user_confirm_remove_title);
74            builder.setMessage(R.string.user_confirm_remove_message);
75        }
76        return builder.create();
77    }
78
79    /**
80     * Creates a view to be used in the confirmation dialog for removing work profile.
81     */
82    private static View createRemoveManagedUserDialogView(Context context, int userId) {
83        PackageManager packageManager = context.getPackageManager();
84        ApplicationInfo mdmApplicationInfo = Utils.getAdminApplicationInfo(context, userId);
85        if (mdmApplicationInfo == null) {
86            return null;
87        }
88        LayoutInflater inflater =
89                (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
90
91        View view = inflater.inflate(R.layout.delete_managed_profile_dialog, null);
92        ImageView imageView =
93                (ImageView) view.findViewById(R.id.delete_managed_profile_mdm_icon_view);
94        Drawable badgedApplicationIcon = packageManager.getUserBadgedIcon(
95                packageManager.getApplicationIcon(mdmApplicationInfo), new UserHandle(userId));
96        imageView.setImageDrawable(badgedApplicationIcon);
97
98        CharSequence appLabel = packageManager.getApplicationLabel(mdmApplicationInfo);
99        CharSequence badgedAppLabel = packageManager.getUserBadgedLabel(appLabel,
100                new UserHandle(userId));
101        TextView textView =
102                (TextView) view.findViewById(R.id.delete_managed_profile_device_manager_name);
103        textView.setText(appLabel);
104        if (!appLabel.toString().contentEquals(badgedAppLabel)) {
105            textView.setContentDescription(badgedAppLabel);
106        }
107
108        return view;
109    }
110
111    /**
112     * Creates a dialog to confirm that the user is ok to enable phone calls and SMS.
113     *
114     * @param onConfirmListener Callback object for positive action
115     */
116    public static Dialog createEnablePhoneCallsAndSmsDialog(Context context,
117            DialogInterface.OnClickListener onConfirmListener) {
118        return new AlertDialog.Builder(context)
119                .setTitle(R.string.user_enable_calling_and_sms_confirm_title)
120                .setMessage(R.string.user_enable_calling_and_sms_confirm_message)
121                .setPositiveButton(R.string.okay, onConfirmListener)
122                .setNegativeButton(android.R.string.cancel, null)
123                .create();
124    }
125
126    /**
127     * Creates a dialog to confirm that the user is ok to enable phone calls (no SMS).
128     *
129     * @param onConfirmListener Callback object for positive action
130     */
131    public static Dialog createEnablePhoneCallsDialog(Context context,
132            DialogInterface.OnClickListener onConfirmListener) {
133        return new AlertDialog.Builder(context)
134                .setTitle(R.string.user_enable_calling_confirm_title)
135                .setMessage(R.string.user_enable_calling_confirm_message)
136                .setPositiveButton(R.string.okay, onConfirmListener)
137                .setNegativeButton(android.R.string.cancel, null)
138                .create();
139    }
140}
141