19a42f2b64dada715248aec7c898f983375a395eeAlan Treadwaypackage com.android.managedprovisioning;
29a42f2b64dada715248aec7c898f983375a395eeAlan Treadway/*
39a42f2b64dada715248aec7c898f983375a395eeAlan Treadway * Copyright 2015, The Android Open Source Project
49a42f2b64dada715248aec7c898f983375a395eeAlan Treadway *
59a42f2b64dada715248aec7c898f983375a395eeAlan Treadway * Licensed under the Apache License, Version 2.0 (the "License");
69a42f2b64dada715248aec7c898f983375a395eeAlan Treadway * you may not use this file except in compliance with the License.
79a42f2b64dada715248aec7c898f983375a395eeAlan Treadway * You may obtain a copy of the License at
89a42f2b64dada715248aec7c898f983375a395eeAlan Treadway *
99a42f2b64dada715248aec7c898f983375a395eeAlan Treadway *      http://www.apache.org/licenses/LICENSE-2.0
109a42f2b64dada715248aec7c898f983375a395eeAlan Treadway *
119a42f2b64dada715248aec7c898f983375a395eeAlan Treadway * Unless required by applicable law or agreed to in writing, software
129a42f2b64dada715248aec7c898f983375a395eeAlan Treadway * distributed under the License is distributed on an "AS IS" BASIS,
139a42f2b64dada715248aec7c898f983375a395eeAlan Treadway * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
149a42f2b64dada715248aec7c898f983375a395eeAlan Treadway * See the License for the specific language governing permissions and
159a42f2b64dada715248aec7c898f983375a395eeAlan Treadway * limitations under the License.
169a42f2b64dada715248aec7c898f983375a395eeAlan Treadway */
179a42f2b64dada715248aec7c898f983375a395eeAlan Treadway
18f17cce06a596f159956f0dce52417886451cd395Rubin Xuimport android.annotation.Nullable;
199a42f2b64dada715248aec7c898f983375a395eeAlan Treadwayimport android.app.Dialog;
209a42f2b64dada715248aec7c898f983375a395eeAlan Treadwayimport android.app.DialogFragment;
219a42f2b64dada715248aec7c898f983375a395eeAlan Treadwayimport android.content.ComponentName;
229a42f2b64dada715248aec7c898f983375a395eeAlan Treadwayimport android.content.DialogInterface;
23f17cce06a596f159956f0dce52417886451cd395Rubin Xuimport android.graphics.drawable.Drawable;
249a42f2b64dada715248aec7c898f983375a395eeAlan Treadwayimport android.os.Bundle;
259a42f2b64dada715248aec7c898f983375a395eeAlan Treadwayimport android.view.View;
269a42f2b64dada715248aec7c898f983375a395eeAlan Treadwayimport android.view.View.OnClickListener;
279a42f2b64dada715248aec7c898f983375a395eeAlan Treadwayimport android.widget.Button;
289a42f2b64dada715248aec7c898f983375a395eeAlan Treadwayimport android.widget.ImageView;
299a42f2b64dada715248aec7c898f983375a395eeAlan Treadwayimport android.widget.TextView;
309a42f2b64dada715248aec7c898f983375a395eeAlan Treadway
319a42f2b64dada715248aec7c898f983375a395eeAlan Treadwayimport com.android.managedprovisioning.Utils.MdmPackageInfo;
329a42f2b64dada715248aec7c898f983375a395eeAlan Treadway
339a42f2b64dada715248aec7c898f983375a395eeAlan Treadway/**
349a42f2b64dada715248aec7c898f983375a395eeAlan Treadway * Displays information about an existing managed profile and asks the user if it should be deleted.
359a42f2b64dada715248aec7c898f983375a395eeAlan Treadway *
369a42f2b64dada715248aec7c898f983375a395eeAlan Treadway * <p>Expects parent component to implement {@link DeleteManagedProfileCallback} for user-response
379a42f2b64dada715248aec7c898f983375a395eeAlan Treadway * handling.
389a42f2b64dada715248aec7c898f983375a395eeAlan Treadway */
399a42f2b64dada715248aec7c898f983375a395eeAlan Treadwaypublic class DeleteManagedProfileDialog extends DialogFragment {
409a42f2b64dada715248aec7c898f983375a395eeAlan Treadway    private static final String KEY_USER_PROFILE_CALLBACK_ID = "user_profile_callback_id";
419a42f2b64dada715248aec7c898f983375a395eeAlan Treadway    private static final String KEY_MDM_PACKAGE_NAME = "mdm_package_name";
429a42f2b64dada715248aec7c898f983375a395eeAlan Treadway    private static final String KEY_PROFILE_OWNER_DOMAIN = "profile_owner_domain";
439a42f2b64dada715248aec7c898f983375a395eeAlan Treadway
449a42f2b64dada715248aec7c898f983375a395eeAlan Treadway    /**
459a42f2b64dada715248aec7c898f983375a395eeAlan Treadway     * @param managedProfileUserId user-id for the managed profile which will be passed back to the
469a42f2b64dada715248aec7c898f983375a395eeAlan Treadway     *     parent component in the {@link DeleteManagedProfileCallback#onRemoveProfileApproval(int)}
479a42f2b64dada715248aec7c898f983375a395eeAlan Treadway     *     call
48f17cce06a596f159956f0dce52417886451cd395Rubin Xu     * @param mdmPackagename package name of the MDM application for the current managed profile, or
49f17cce06a596f159956f0dce52417886451cd395Rubin Xu     *     null if the managed profile has no profile owner associated.
509a42f2b64dada715248aec7c898f983375a395eeAlan Treadway     * @param profileOwnerDomain domain name of the organization which owns the managed profile, or
519a42f2b64dada715248aec7c898f983375a395eeAlan Treadway     *     null if not known
529a42f2b64dada715248aec7c898f983375a395eeAlan Treadway     * @return initialized dialog
539a42f2b64dada715248aec7c898f983375a395eeAlan Treadway     */
549a42f2b64dada715248aec7c898f983375a395eeAlan Treadway    public static DeleteManagedProfileDialog newInstance(
55f17cce06a596f159956f0dce52417886451cd395Rubin Xu            int managedProfileUserId, @Nullable ComponentName mdmPackagename,
56f17cce06a596f159956f0dce52417886451cd395Rubin Xu            @Nullable String profileOwnerDomain) {
579a42f2b64dada715248aec7c898f983375a395eeAlan Treadway        Bundle args = new Bundle();
589a42f2b64dada715248aec7c898f983375a395eeAlan Treadway        args.putInt(KEY_USER_PROFILE_CALLBACK_ID, managedProfileUserId);
59f17cce06a596f159956f0dce52417886451cd395Rubin Xu        // The device could be in a inconsistent state where it has a managed profile but no
60f17cce06a596f159956f0dce52417886451cd395Rubin Xu        // associated profile owner package, for example after an unexpected reboot in the middle
61f17cce06a596f159956f0dce52417886451cd395Rubin Xu        // of provisioning.
62f17cce06a596f159956f0dce52417886451cd395Rubin Xu        if (mdmPackagename != null) {
63f17cce06a596f159956f0dce52417886451cd395Rubin Xu            args.putString(KEY_MDM_PACKAGE_NAME, mdmPackagename.getPackageName());
64f17cce06a596f159956f0dce52417886451cd395Rubin Xu        }
659a42f2b64dada715248aec7c898f983375a395eeAlan Treadway        args.putString(KEY_PROFILE_OWNER_DOMAIN, profileOwnerDomain);
669a42f2b64dada715248aec7c898f983375a395eeAlan Treadway
679a42f2b64dada715248aec7c898f983375a395eeAlan Treadway        DeleteManagedProfileDialog dialog = new DeleteManagedProfileDialog();
689a42f2b64dada715248aec7c898f983375a395eeAlan Treadway        dialog.setArguments(args);
699a42f2b64dada715248aec7c898f983375a395eeAlan Treadway        return dialog;
709a42f2b64dada715248aec7c898f983375a395eeAlan Treadway    }
719a42f2b64dada715248aec7c898f983375a395eeAlan Treadway
729a42f2b64dada715248aec7c898f983375a395eeAlan Treadway    @Override
739a42f2b64dada715248aec7c898f983375a395eeAlan Treadway    public Dialog onCreateDialog(Bundle savedInstanceState) {
749a42f2b64dada715248aec7c898f983375a395eeAlan Treadway        if (!(getActivity() instanceof DeleteManagedProfileCallback)) {
759a42f2b64dada715248aec7c898f983375a395eeAlan Treadway            throw new IllegalStateException("Calling activity must implement " +
769a42f2b64dada715248aec7c898f983375a395eeAlan Treadway                    "DeleteManagedProfileCallback, found: " + getActivity().getLocalClassName());
779a42f2b64dada715248aec7c898f983375a395eeAlan Treadway        }
789a42f2b64dada715248aec7c898f983375a395eeAlan Treadway
799a42f2b64dada715248aec7c898f983375a395eeAlan Treadway        Bundle arguments = getArguments();
809a42f2b64dada715248aec7c898f983375a395eeAlan Treadway        final int callbackUserProfileId = arguments.getInt(KEY_USER_PROFILE_CALLBACK_ID);
819a42f2b64dada715248aec7c898f983375a395eeAlan Treadway        String mdmPackageName = arguments.getString(KEY_MDM_PACKAGE_NAME);
829a42f2b64dada715248aec7c898f983375a395eeAlan Treadway
83f17cce06a596f159956f0dce52417886451cd395Rubin Xu        String appLabel;
84f17cce06a596f159956f0dce52417886451cd395Rubin Xu        Drawable appIcon;
85eb18ce020febefd5c683fea8ade52ad57299ccf0Rubin Xu        MdmPackageInfo mdmPackageInfo = null;
86f17cce06a596f159956f0dce52417886451cd395Rubin Xu        if (mdmPackageName != null) {
87eb18ce020febefd5c683fea8ade52ad57299ccf0Rubin Xu            mdmPackageInfo = Utils.getMdmPackageInfo(
88f17cce06a596f159956f0dce52417886451cd395Rubin Xu                    getActivity().getPackageManager(), mdmPackageName);
89eb18ce020febefd5c683fea8ade52ad57299ccf0Rubin Xu        }
90eb18ce020febefd5c683fea8ade52ad57299ccf0Rubin Xu        if (mdmPackageInfo != null) {
91f17cce06a596f159956f0dce52417886451cd395Rubin Xu            appLabel = mdmPackageInfo.getAppLabel();
92f17cce06a596f159956f0dce52417886451cd395Rubin Xu            appIcon = mdmPackageInfo.getPackageIcon();
93f17cce06a596f159956f0dce52417886451cd395Rubin Xu        } else {
94f17cce06a596f159956f0dce52417886451cd395Rubin Xu            appLabel= getResources().getString(android.R.string.unknownName);
95f17cce06a596f159956f0dce52417886451cd395Rubin Xu            appIcon = getActivity().getPackageManager().getDefaultActivityIcon();
96f17cce06a596f159956f0dce52417886451cd395Rubin Xu        }
979a42f2b64dada715248aec7c898f983375a395eeAlan Treadway
989a42f2b64dada715248aec7c898f983375a395eeAlan Treadway        final Dialog dialog = new Dialog(getActivity(), R.style.ManagedProvisioningDialogTheme);
999a42f2b64dada715248aec7c898f983375a395eeAlan Treadway        dialog.setTitle(R.string.delete_profile_title);
1009a42f2b64dada715248aec7c898f983375a395eeAlan Treadway        dialog.setContentView(R.layout.delete_managed_profile_dialog);
1019a42f2b64dada715248aec7c898f983375a395eeAlan Treadway        dialog.setCanceledOnTouchOutside(false);
1029a42f2b64dada715248aec7c898f983375a395eeAlan Treadway
1039a42f2b64dada715248aec7c898f983375a395eeAlan Treadway        ImageView imageView = (ImageView) dialog.findViewById(
1049a42f2b64dada715248aec7c898f983375a395eeAlan Treadway                R.id.delete_managed_profile_mdm_icon_view);
105f17cce06a596f159956f0dce52417886451cd395Rubin Xu        imageView.setImageDrawable(appIcon);
106bfe354d76a591e59997d79026002ee1a40e71811Sudheer Shanka        imageView.setContentDescription(
107bfe354d76a591e59997d79026002ee1a40e71811Sudheer Shanka                    getResources().getString(R.string.mdm_icon_label, appLabel));
1089a42f2b64dada715248aec7c898f983375a395eeAlan Treadway
1099a42f2b64dada715248aec7c898f983375a395eeAlan Treadway        TextView deviceManagerName = (TextView) dialog.findViewById(
1109a42f2b64dada715248aec7c898f983375a395eeAlan Treadway                R.id.delete_managed_profile_device_manager_name);
111bfe354d76a591e59997d79026002ee1a40e71811Sudheer Shanka        deviceManagerName.setText(appLabel);
1129a42f2b64dada715248aec7c898f983375a395eeAlan Treadway
1139a42f2b64dada715248aec7c898f983375a395eeAlan Treadway        Button positiveButton = (Button) dialog.findViewById(
1149a42f2b64dada715248aec7c898f983375a395eeAlan Treadway                R.id.delete_managed_profile_positive_button);
1159a42f2b64dada715248aec7c898f983375a395eeAlan Treadway        positiveButton.setOnClickListener(new OnClickListener() {
1169a42f2b64dada715248aec7c898f983375a395eeAlan Treadway                @Override
1179a42f2b64dada715248aec7c898f983375a395eeAlan Treadway                public void onClick(View v) {
1189a42f2b64dada715248aec7c898f983375a395eeAlan Treadway                    dialog.dismiss();
1199a42f2b64dada715248aec7c898f983375a395eeAlan Treadway                    ((DeleteManagedProfileCallback) getActivity())
1209a42f2b64dada715248aec7c898f983375a395eeAlan Treadway                            .onRemoveProfileApproval(callbackUserProfileId);
1219a42f2b64dada715248aec7c898f983375a395eeAlan Treadway                }
1229a42f2b64dada715248aec7c898f983375a395eeAlan Treadway            });
1239a42f2b64dada715248aec7c898f983375a395eeAlan Treadway
1249a42f2b64dada715248aec7c898f983375a395eeAlan Treadway        Button negativeButton = (Button) dialog.findViewById(
1259a42f2b64dada715248aec7c898f983375a395eeAlan Treadway                R.id.delete_managed_profile_negative_button);
1269a42f2b64dada715248aec7c898f983375a395eeAlan Treadway        negativeButton.setOnClickListener(new OnClickListener() {
1279a42f2b64dada715248aec7c898f983375a395eeAlan Treadway                @Override
1289a42f2b64dada715248aec7c898f983375a395eeAlan Treadway                public void onClick(View v) {
1299a42f2b64dada715248aec7c898f983375a395eeAlan Treadway                    dialog.dismiss();
1309a42f2b64dada715248aec7c898f983375a395eeAlan Treadway                    ((DeleteManagedProfileCallback) getActivity()).onRemoveProfileCancel();
1319a42f2b64dada715248aec7c898f983375a395eeAlan Treadway                }
1329a42f2b64dada715248aec7c898f983375a395eeAlan Treadway            });
1339a42f2b64dada715248aec7c898f983375a395eeAlan Treadway
1349a42f2b64dada715248aec7c898f983375a395eeAlan Treadway        return dialog;
1359a42f2b64dada715248aec7c898f983375a395eeAlan Treadway    }
1369a42f2b64dada715248aec7c898f983375a395eeAlan Treadway
1379a42f2b64dada715248aec7c898f983375a395eeAlan Treadway    @Override
1389a42f2b64dada715248aec7c898f983375a395eeAlan Treadway    public void onCancel(DialogInterface dialog) {
1399a42f2b64dada715248aec7c898f983375a395eeAlan Treadway        dialog.dismiss();
1409a42f2b64dada715248aec7c898f983375a395eeAlan Treadway        ((DeleteManagedProfileCallback) getActivity()).onRemoveProfileCancel();
1419a42f2b64dada715248aec7c898f983375a395eeAlan Treadway    }
1429a42f2b64dada715248aec7c898f983375a395eeAlan Treadway
1439a42f2b64dada715248aec7c898f983375a395eeAlan Treadway    /**
1449a42f2b64dada715248aec7c898f983375a395eeAlan Treadway     * Callback interface for outcome of {@link DeleteManagedProfileDialog} presentation.
1459a42f2b64dada715248aec7c898f983375a395eeAlan Treadway     */
1469a42f2b64dada715248aec7c898f983375a395eeAlan Treadway    public interface DeleteManagedProfileCallback {
1479a42f2b64dada715248aec7c898f983375a395eeAlan Treadway
1489a42f2b64dada715248aec7c898f983375a395eeAlan Treadway        /**
1499a42f2b64dada715248aec7c898f983375a395eeAlan Treadway         * Invoked if the user hits the positive response (perform removal) button.
1509a42f2b64dada715248aec7c898f983375a395eeAlan Treadway         *
1519a42f2b64dada715248aec7c898f983375a395eeAlan Treadway         * @param managedProfileUserId user-id of the managed-profile that the dialog was presented
1529a42f2b64dada715248aec7c898f983375a395eeAlan Treadway         *                             for
1539a42f2b64dada715248aec7c898f983375a395eeAlan Treadway         */
1549a42f2b64dada715248aec7c898f983375a395eeAlan Treadway        public abstract void onRemoveProfileApproval(int managedProfileUserId);
1559a42f2b64dada715248aec7c898f983375a395eeAlan Treadway
1569a42f2b64dada715248aec7c898f983375a395eeAlan Treadway        /**
1579a42f2b64dada715248aec7c898f983375a395eeAlan Treadway         * Invoked if the user hits the negative response (DO NOT perform removal) button, or the
1589a42f2b64dada715248aec7c898f983375a395eeAlan Treadway         * dialog was otherwise dismissed.
1599a42f2b64dada715248aec7c898f983375a395eeAlan Treadway         */
1609a42f2b64dada715248aec7c898f983375a395eeAlan Treadway        public abstract void onRemoveProfileCancel();
1619a42f2b64dada715248aec7c898f983375a395eeAlan Treadway    }
1629a42f2b64dada715248aec7c898f983375a395eeAlan Treadway}
163