Home | History | Annotate | Download | only in managedprovisioning
      1 package 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 
     18 import android.annotation.Nullable;
     19 import android.app.Dialog;
     20 import android.app.DialogFragment;
     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.View.OnClickListener;
     27 import android.widget.Button;
     28 import android.widget.ImageView;
     29 import android.widget.TextView;
     30 
     31 import com.android.managedprovisioning.common.MdmPackageInfo;
     32 import com.android.managedprovisioning.common.Utils;
     33 import com.android.setupwizardlib.util.SystemBarHelper;
     34 
     35 /**
     36  * Displays information about an existing managed profile and asks the user if it should be deleted.
     37  *
     38  * <p>Expects parent component to implement {@link DeleteManagedProfileCallback} for user-response
     39  * handling.
     40  */
     41 public class DeleteManagedProfileDialog extends DialogFragment {
     42     private static final String KEY_USER_PROFILE_CALLBACK_ID = "user_profile_callback_id";
     43     private static final String KEY_MDM_PACKAGE_NAME = "mdm_package_name";
     44     private static final String KEY_PROFILE_OWNER_DOMAIN = "profile_owner_domain";
     45 
     46     private final Utils mUtils = new Utils();
     47 
     48     /**
     49      * @param managedProfileUserId user-id for the managed profile which will be passed back to the
     50      *     parent component in the {@link DeleteManagedProfileCallback#onRemoveProfileApproval(int)}
     51      *     call
     52      * @param mdmPackagename package name of the MDM application for the current managed profile, or
     53      *     null if the managed profile has no profile owner associated.
     54      * @param profileOwnerDomain domain name of the organization which owns the managed profile, or
     55      *     null if not known
     56      * @return initialized dialog
     57      */
     58     public static DeleteManagedProfileDialog newInstance(
     59             int managedProfileUserId, @Nullable ComponentName mdmPackagename,
     60             @Nullable String profileOwnerDomain) {
     61         Bundle args = new Bundle();
     62         args.putInt(KEY_USER_PROFILE_CALLBACK_ID, managedProfileUserId);
     63         // The device could be in a inconsistent state where it has a managed profile but no
     64         // associated profile owner package, for example after an unexpected reboot in the middle
     65         // of provisioning.
     66         if (mdmPackagename != null) {
     67             args.putString(KEY_MDM_PACKAGE_NAME, mdmPackagename.getPackageName());
     68         }
     69         args.putString(KEY_PROFILE_OWNER_DOMAIN, profileOwnerDomain);
     70 
     71         DeleteManagedProfileDialog dialog = new DeleteManagedProfileDialog();
     72         dialog.setArguments(args);
     73         return dialog;
     74     }
     75 
     76     @Override
     77     public Dialog onCreateDialog(Bundle savedInstanceState) {
     78         if (!(getActivity() instanceof DeleteManagedProfileCallback)) {
     79             throw new IllegalStateException("Calling activity must implement " +
     80                     "DeleteManagedProfileCallback, found: " + getActivity().getLocalClassName());
     81         }
     82 
     83         Bundle arguments = getArguments();
     84         final int callbackUserProfileId = arguments.getInt(KEY_USER_PROFILE_CALLBACK_ID);
     85         String mdmPackageName = arguments.getString(KEY_MDM_PACKAGE_NAME);
     86 
     87         String appLabel;
     88         Drawable appIcon;
     89         MdmPackageInfo mdmPackageInfo = null;
     90         if (mdmPackageName != null) {
     91             mdmPackageInfo = MdmPackageInfo.createFromPackageName(getActivity(), mdmPackageName);
     92         }
     93         if (mdmPackageInfo != null) {
     94             appLabel = mdmPackageInfo.appLabel;
     95             appIcon = mdmPackageInfo.packageIcon;
     96         } else {
     97             appLabel= getResources().getString(android.R.string.unknownName);
     98             appIcon = getActivity().getPackageManager().getDefaultActivityIcon();
     99         }
    100 
    101         final Dialog dialog = new Dialog(getActivity(), R.style.ManagedProvisioningDialogTheme);
    102         dialog.setTitle(R.string.delete_profile_title);
    103         dialog.setContentView(R.layout.delete_managed_profile_dialog);
    104         dialog.setCanceledOnTouchOutside(false);
    105         if (!mUtils.isUserSetupCompleted(getActivity())) {
    106             SystemBarHelper.hideSystemBars(dialog);
    107         }
    108 
    109         ImageView imageView = (ImageView) dialog.findViewById(
    110                 R.id.device_manager_icon_view);
    111         imageView.setImageDrawable(appIcon);
    112         imageView.setContentDescription(
    113                     getResources().getString(R.string.mdm_icon_label, appLabel));
    114 
    115         TextView deviceManagerName = (TextView) dialog.findViewById(
    116                 R.id.device_manager_name);
    117         deviceManagerName.setText(appLabel);
    118 
    119         Button positiveButton = (Button) dialog.findViewById(
    120                 R.id.delete_managed_profile_positive_button);
    121         positiveButton.setOnClickListener(new OnClickListener() {
    122                 @Override
    123                 public void onClick(View v) {
    124                     dialog.dismiss();
    125                     ((DeleteManagedProfileCallback) getActivity())
    126                             .onRemoveProfileApproval(callbackUserProfileId);
    127                 }
    128             });
    129 
    130         Button negativeButton = (Button) dialog.findViewById(
    131                 R.id.delete_managed_profile_negative_button);
    132         negativeButton.setOnClickListener(new OnClickListener() {
    133                 @Override
    134                 public void onClick(View v) {
    135                     dialog.dismiss();
    136                     ((DeleteManagedProfileCallback) getActivity()).onRemoveProfileCancel();
    137                 }
    138             });
    139 
    140         return dialog;
    141     }
    142 
    143     @Override
    144     public void onCancel(DialogInterface dialog) {
    145         dialog.dismiss();
    146         ((DeleteManagedProfileCallback) getActivity()).onRemoveProfileCancel();
    147     }
    148 
    149     /**
    150      * Callback interface for outcome of {@link DeleteManagedProfileDialog} presentation.
    151      */
    152     public interface DeleteManagedProfileCallback {
    153 
    154         /**
    155          * Invoked if the user hits the positive response (perform removal) button.
    156          *
    157          * @param managedProfileUserId user-id of the managed-profile that the dialog was presented
    158          *                             for
    159          */
    160         public abstract void onRemoveProfileApproval(int managedProfileUserId);
    161 
    162         /**
    163          * Invoked if the user hits the negative response (DO NOT perform removal) button, or the
    164          * dialog was otherwise dismissed.
    165          */
    166         public abstract void onRemoveProfileCancel();
    167     }
    168 }
    169