1/*
2 * Copyright 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.managedprovisioning.preprovisioning;
18
19import android.annotation.Nullable;
20import android.app.AlertDialog;
21import android.content.ComponentName;
22import android.content.DialogInterface;
23import android.graphics.drawable.Drawable;
24import android.os.Bundle;
25import android.view.View;
26import android.view.ViewGroup;
27import android.widget.ImageView;
28import android.widget.TextView;
29
30import com.android.managedprovisioning.R;
31import com.android.managedprovisioning.common.MdmPackageInfo;
32import com.android.managedprovisioning.common.SettingsFacade;
33import com.android.managedprovisioning.common.SimpleDialog;
34import com.android.setupwizardlib.util.SystemBarHelper;
35
36/**
37 * Displays information about an existing managed profile and asks the user if it should be deleted.
38 *
39 * <p>Expects parent component to implement {@link SimpleDialog.SimpleDialogListener} for
40 * user-response handling.
41 */
42public class DeleteManagedProfileDialog extends SimpleDialog {
43    private static final String KEY_USER_PROFILE_CALLBACK_ID = "user_profile_callback_id";
44    private static final String KEY_MDM_PACKAGE_NAME = "mdm_package_name";
45    private static final String KEY_PROFILE_OWNER_DOMAIN = "profile_owner_domain";
46
47    private final SettingsFacade mSettingsFacade = new SettingsFacade();
48
49    /**
50     * @param managedProfileUserId user-id for the managed profile
51     * @param mdmPackageName package name of the MDM application for the current managed profile,
52     * or null if the managed profile has no profile owner associated.
53     * @param profileOwnerDomain domain name of the organization which owns the managed profile, or
54     * null if not known
55     * @return initialized dialog
56     */
57    public static DeleteManagedProfileDialog newInstance(
58            int managedProfileUserId, @Nullable ComponentName mdmPackageName,
59            @Nullable String profileOwnerDomain) {
60
61        // TODO: this is a bit hacky; tidy up if time permits, e.g. by creating a CustomDialog class
62        Bundle args = new SimpleDialog.Builder()
63                .setTitle(R.string.delete_profile_title)
64                .setPositiveButtonMessage(R.string.delete_profile)
65                .setNegativeButtonMessage(R.string.cancel_delete_profile)
66                .setCancelable(false)
67                .build()
68                .getArguments();
69
70        args.putInt(KEY_USER_PROFILE_CALLBACK_ID, managedProfileUserId);
71
72        // The device could be in a inconsistent state where it has a managed profile but no
73        // associated profile owner package, for example after an unexpected reboot in the middle
74        // of provisioning.
75        if (mdmPackageName != null) {
76            args.putString(KEY_MDM_PACKAGE_NAME, mdmPackageName.getPackageName());
77        }
78        args.putString(KEY_PROFILE_OWNER_DOMAIN, profileOwnerDomain);
79
80        DeleteManagedProfileDialog dialog = new DeleteManagedProfileDialog();
81        dialog.setArguments(args);
82        return dialog;
83    }
84
85    @Override
86    public AlertDialog onCreateDialog(Bundle savedInstanceState) {
87        // TODO: this is a bit hacky; tidy up if time permits, e.g. by creating a CustomDialog class
88        AlertDialog dialog = super.onCreateDialog(savedInstanceState);
89        dialog.setView(createContentView());
90
91        if (!mSettingsFacade.isUserSetupCompleted(getActivity())) {
92            SystemBarHelper.hideSystemBars(dialog);
93        }
94
95        return dialog;
96    }
97
98    private View createContentView() {
99        View view = getActivity().getLayoutInflater().inflate(
100                R.layout.delete_managed_profile_dialog,
101                (ViewGroup) getActivity().findViewById(android.R.id.content), false);
102
103        String mdmPackageName = getArguments().getString(KEY_MDM_PACKAGE_NAME);
104        String appLabel;
105        Drawable appIcon;
106        MdmPackageInfo mdmPackageInfo = null;
107        if (mdmPackageName != null) {
108            mdmPackageInfo = MdmPackageInfo.createFromPackageName(getActivity(), mdmPackageName);
109        }
110        if (mdmPackageInfo != null) {
111            appLabel = mdmPackageInfo.appLabel;
112            appIcon = mdmPackageInfo.packageIcon;
113        } else {
114            appLabel = getResources().getString(android.R.string.unknownName);
115            appIcon = getActivity().getPackageManager().getDefaultActivityIcon();
116        }
117
118        ImageView imageView = view.findViewById(R.id.device_manager_icon_view);
119        imageView.setImageDrawable(appIcon);
120        imageView.setContentDescription(
121                getResources().getString(R.string.mdm_icon_label, appLabel));
122
123        TextView deviceManagerName = view.findViewById(R.id.device_manager_name);
124        deviceManagerName.setText(appLabel);
125
126        return view;
127    }
128
129    /**
130     * @return User id with which the dialog was instantiated
131     */
132    public int getUserId() {
133        return getArguments().getInt(KEY_USER_PROFILE_CALLBACK_ID);
134    }
135
136    @Override
137    public void onCancel(DialogInterface dialog) {
138        dialog.dismiss();
139        ((SimpleDialog.SimpleDialogListener) getActivity()).onNegativeButtonClick(
140                DeleteManagedProfileDialog.this);
141    }
142}