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 
17 package com.android.managedprovisioning.preprovisioning;
18 
19 import android.annotation.Nullable;
20 import android.app.AlertDialog;
21 import android.content.ComponentName;
22 import android.content.DialogInterface;
23 import android.graphics.drawable.Drawable;
24 import android.os.Bundle;
25 import android.view.View;
26 import android.view.ViewGroup;
27 import android.widget.ImageView;
28 import android.widget.TextView;
29 
30 import com.android.managedprovisioning.R;
31 import com.android.managedprovisioning.common.MdmPackageInfo;
32 import com.android.managedprovisioning.common.SettingsFacade;
33 import com.android.managedprovisioning.common.SimpleDialog;
34 import 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  */
42 public 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      */
newInstance( int managedProfileUserId, @Nullable ComponentName mdmPackageName, @Nullable String profileOwnerDomain)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
onCreateDialog(Bundle savedInstanceState)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 
createContentView()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      */
getUserId()132     public int getUserId() {
133         return getArguments().getInt(KEY_USER_PROFILE_CALLBACK_ID);
134     }
135 
136     @Override
onCancel(DialogInterface dialog)137     public void onCancel(DialogInterface dialog) {
138         dialog.dismiss();
139         ((SimpleDialog.SimpleDialogListener) getActivity()).onNegativeButtonClick(
140                 DeleteManagedProfileDialog.this);
141     }
142 }