1package com.android.managedprovisioning;
2/*
3 * Copyright 2015, The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18import android.annotation.Nullable;
19import android.app.Dialog;
20import android.app.DialogFragment;
21import android.content.ComponentName;
22import android.content.DialogInterface;
23import android.graphics.drawable.Drawable;
24import android.os.Bundle;
25import android.view.View;
26import android.view.View.OnClickListener;
27import android.widget.Button;
28import android.widget.ImageView;
29import android.widget.TextView;
30
31import com.android.managedprovisioning.Utils.MdmPackageInfo;
32
33/**
34 * Displays information about an existing managed profile and asks the user if it should be deleted.
35 *
36 * <p>Expects parent component to implement {@link DeleteManagedProfileCallback} for user-response
37 * handling.
38 */
39public class DeleteManagedProfileDialog extends DialogFragment {
40    private static final String KEY_USER_PROFILE_CALLBACK_ID = "user_profile_callback_id";
41    private static final String KEY_MDM_PACKAGE_NAME = "mdm_package_name";
42    private static final String KEY_PROFILE_OWNER_DOMAIN = "profile_owner_domain";
43
44    /**
45     * @param managedProfileUserId user-id for the managed profile which will be passed back to the
46     *     parent component in the {@link DeleteManagedProfileCallback#onRemoveProfileApproval(int)}
47     *     call
48     * @param mdmPackagename package name of the MDM application for the current managed profile, or
49     *     null if the managed profile has no profile owner associated.
50     * @param profileOwnerDomain domain name of the organization which owns the managed profile, or
51     *     null if not known
52     * @return initialized dialog
53     */
54    public static DeleteManagedProfileDialog newInstance(
55            int managedProfileUserId, @Nullable ComponentName mdmPackagename,
56            @Nullable String profileOwnerDomain) {
57        Bundle args = new Bundle();
58        args.putInt(KEY_USER_PROFILE_CALLBACK_ID, managedProfileUserId);
59        // The device could be in a inconsistent state where it has a managed profile but no
60        // associated profile owner package, for example after an unexpected reboot in the middle
61        // of provisioning.
62        if (mdmPackagename != null) {
63            args.putString(KEY_MDM_PACKAGE_NAME, mdmPackagename.getPackageName());
64        }
65        args.putString(KEY_PROFILE_OWNER_DOMAIN, profileOwnerDomain);
66
67        DeleteManagedProfileDialog dialog = new DeleteManagedProfileDialog();
68        dialog.setArguments(args);
69        return dialog;
70    }
71
72    @Override
73    public Dialog onCreateDialog(Bundle savedInstanceState) {
74        if (!(getActivity() instanceof DeleteManagedProfileCallback)) {
75            throw new IllegalStateException("Calling activity must implement " +
76                    "DeleteManagedProfileCallback, found: " + getActivity().getLocalClassName());
77        }
78
79        Bundle arguments = getArguments();
80        final int callbackUserProfileId = arguments.getInt(KEY_USER_PROFILE_CALLBACK_ID);
81        String mdmPackageName = arguments.getString(KEY_MDM_PACKAGE_NAME);
82
83        String appLabel;
84        Drawable appIcon;
85        MdmPackageInfo mdmPackageInfo = null;
86        if (mdmPackageName != null) {
87            mdmPackageInfo = Utils.getMdmPackageInfo(
88                    getActivity().getPackageManager(), mdmPackageName);
89        }
90        if (mdmPackageInfo != null) {
91            appLabel = mdmPackageInfo.getAppLabel();
92            appIcon = mdmPackageInfo.getPackageIcon();
93        } else {
94            appLabel= getResources().getString(android.R.string.unknownName);
95            appIcon = getActivity().getPackageManager().getDefaultActivityIcon();
96        }
97
98        final Dialog dialog = new Dialog(getActivity(), R.style.ManagedProvisioningDialogTheme);
99        dialog.setTitle(R.string.delete_profile_title);
100        dialog.setContentView(R.layout.delete_managed_profile_dialog);
101        dialog.setCanceledOnTouchOutside(false);
102
103        ImageView imageView = (ImageView) dialog.findViewById(
104                R.id.delete_managed_profile_mdm_icon_view);
105        imageView.setImageDrawable(appIcon);
106        imageView.setContentDescription(
107                    getResources().getString(R.string.mdm_icon_label, appLabel));
108
109        TextView deviceManagerName = (TextView) dialog.findViewById(
110                R.id.delete_managed_profile_device_manager_name);
111        deviceManagerName.setText(appLabel);
112
113        Button positiveButton = (Button) dialog.findViewById(
114                R.id.delete_managed_profile_positive_button);
115        positiveButton.setOnClickListener(new OnClickListener() {
116                @Override
117                public void onClick(View v) {
118                    dialog.dismiss();
119                    ((DeleteManagedProfileCallback) getActivity())
120                            .onRemoveProfileApproval(callbackUserProfileId);
121                }
122            });
123
124        Button negativeButton = (Button) dialog.findViewById(
125                R.id.delete_managed_profile_negative_button);
126        negativeButton.setOnClickListener(new OnClickListener() {
127                @Override
128                public void onClick(View v) {
129                    dialog.dismiss();
130                    ((DeleteManagedProfileCallback) getActivity()).onRemoveProfileCancel();
131                }
132            });
133
134        return dialog;
135    }
136
137    @Override
138    public void onCancel(DialogInterface dialog) {
139        dialog.dismiss();
140        ((DeleteManagedProfileCallback) getActivity()).onRemoveProfileCancel();
141    }
142
143    /**
144     * Callback interface for outcome of {@link DeleteManagedProfileDialog} presentation.
145     */
146    public interface DeleteManagedProfileCallback {
147
148        /**
149         * Invoked if the user hits the positive response (perform removal) button.
150         *
151         * @param managedProfileUserId user-id of the managed-profile that the dialog was presented
152         *                             for
153         */
154        public abstract void onRemoveProfileApproval(int managedProfileUserId);
155
156        /**
157         * Invoked if the user hits the negative response (DO NOT perform removal) button, or the
158         * dialog was otherwise dismissed.
159         */
160        public abstract void onRemoveProfileCancel();
161    }
162}
163