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; 18 19 import android.app.Activity; 20 import android.app.Dialog; 21 import android.app.DialogFragment; 22 import android.content.Context; 23 import android.content.DialogInterface; 24 import android.content.Intent; 25 import android.net.Uri; 26 import android.os.Bundle; 27 import android.view.View; 28 import android.view.View.OnClickListener; 29 import android.view.Window; 30 import android.view.WindowManager; 31 import android.widget.TextView; 32 import android.widget.Button; 33 34 /** 35 * Dialog used to notify the user that the admin will have full control over the profile/device. 36 * Custom runnables can be passed that are run on consent or cancel. 37 */ 38 public class UserConsentDialog extends DialogFragment { 39 public static final int PROFILE_OWNER = 1; 40 public static final int DEVICE_OWNER = 2; 41 42 public static final String LEARN_MORE_URL_PROFILE_OWNER = 43 "https://support.google.com/android/work/answer/6090512"; 44 45 private final Context mContext; 46 private final int mAdminMonitorsStringId; 47 private final Uri mLearnMoreUri; 48 private final Runnable mOnUserConsentRunnable; 49 private final Runnable mOnCancelRunnable; 50 51 public UserConsentDialog(final Context context, int ownerType, 52 final Runnable onUserConsentRunnable, final Runnable onCancelRunnable) { 53 super(); 54 mContext = context; 55 if (ownerType == PROFILE_OWNER) { 56 mAdminMonitorsStringId = R.string.admin_has_ability_to_monitor_profile; 57 mLearnMoreUri = Uri.parse(LEARN_MORE_URL_PROFILE_OWNER); 58 } else if (ownerType == DEVICE_OWNER) { 59 mAdminMonitorsStringId = R.string.admin_has_ability_to_monitor_device; 60 mLearnMoreUri = null; 61 } else { 62 throw new IllegalArgumentException("Illegal value for argument ownerType."); 63 } 64 mOnUserConsentRunnable = onUserConsentRunnable; 65 mOnCancelRunnable = onCancelRunnable; 66 } 67 68 @Override 69 public Dialog onCreateDialog(Bundle savedInstanceState) { 70 final Dialog dialog = new Dialog(mContext, R.style.ManagedProvisioningDialogTheme); 71 dialog.setContentView(R.layout.learn_more_dialog); 72 dialog.setCanceledOnTouchOutside(false); 73 74 TextView text1 = (TextView) dialog.findViewById(R.id.learn_more_text1); 75 text1.setText(mAdminMonitorsStringId); 76 77 final TextView linkText = (TextView) dialog.findViewById(R.id.learn_more_link); 78 if (mLearnMoreUri != null) { 79 linkText.setOnClickListener(new OnClickListener() { 80 @Override 81 public void onClick(View v) { 82 Intent browserIntent = new Intent(Intent.ACTION_VIEW, mLearnMoreUri); 83 mContext.startActivity(browserIntent); 84 } 85 }); 86 } else { 87 linkText.setVisibility(View.GONE); 88 } 89 90 Button positiveButton = (Button) dialog.findViewById(R.id.positive_button); 91 positiveButton.setOnClickListener(new OnClickListener() { 92 @Override 93 public void onClick(View v) { 94 dialog.dismiss(); 95 if (mOnUserConsentRunnable != null) { 96 mOnUserConsentRunnable.run(); 97 } 98 } 99 }); 100 101 Button negativeButton = (Button) dialog.findViewById(R.id.negative_button); 102 negativeButton.setOnClickListener(new OnClickListener() { 103 @Override 104 public void onClick(View v) { 105 dialog.dismiss(); 106 if (mOnCancelRunnable != null) { 107 mOnCancelRunnable.run(); 108 } 109 } 110 }); 111 112 return dialog; 113 } 114 115 @Override 116 public void onCancel(DialogInterface dialog) { 117 if (mOnCancelRunnable != null) { 118 mOnCancelRunnable.run(); 119 } 120 } 121 }