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
17package com.android.managedprovisioning;
18
19import android.app.Dialog;
20import android.app.DialogFragment;
21import android.content.DialogInterface;
22import android.content.Intent;
23import android.net.Uri;
24import android.os.Bundle;
25import android.view.View;
26import android.view.View.OnClickListener;
27import android.widget.TextView;
28import android.widget.Button;
29import android.widget.CheckBox;
30import android.widget.CompoundButton;
31import android.widget.CompoundButton.OnCheckedChangeListener;
32
33import com.android.managedprovisioning.common.Utils;
34import com.android.managedprovisioning.uiflows.WebActivity;
35import com.android.setupwizardlib.util.SystemBarHelper;
36
37/**
38 * Dialog used to notify the user that the admin will have full control over the profile/device.
39 * Custom runnables can be passed that are run on consent or cancel.
40 */
41public class UserConsentDialog extends DialogFragment {
42    private static final int PROFILE_OWNER = 1;
43    private static final int DEVICE_OWNER = 2;
44
45    private static final String LEARN_MORE_URL_PROFILE_OWNER =
46            "https://support.google.com/android/work/answer/6090512";
47    // TODO: replace by the final device owner learn more link.
48    private static final String LEARN_MORE_URL_DEVICE_OWNER =
49            "https://support.google.com/android/work/answer/6090512";
50
51    // Only urls starting with this base can be visisted in the device owner case.
52    private static final String LEARN_MORE_ALLOWED_BASE_URL =
53            "https://support.google.com/";
54
55    private static final String KEY_OWNER_TYPE = "owner_type";
56    private static final String KEY_SHOW_CONSENT_CHECKBOX = "consent_checkbox";
57
58    private final Utils mUtils = new Utils();
59
60    public static UserConsentDialog newProfileOwnerInstance() {
61        return newInstance(PROFILE_OWNER, false);
62    }
63
64    public static UserConsentDialog newDeviceOwnerInstance(boolean showConsentCheckbox) {
65        return newInstance(DEVICE_OWNER, showConsentCheckbox);
66    }
67
68    private static UserConsentDialog newInstance(int ownerType, boolean showConsentCheckbox) {
69        UserConsentDialog dialog = new UserConsentDialog();
70        Bundle args = new Bundle();
71        args.putInt(KEY_OWNER_TYPE, ownerType);
72        args.putBoolean(KEY_SHOW_CONSENT_CHECKBOX, showConsentCheckbox);
73        dialog.setArguments(args);
74        return dialog;
75    }
76
77    @Override
78    public Dialog onCreateDialog(Bundle savedInstanceState) {
79        int ownerType = getArguments().getInt(KEY_OWNER_TYPE);
80        if (ownerType != PROFILE_OWNER && ownerType != DEVICE_OWNER) {
81            throw new IllegalArgumentException("Illegal value for argument ownerType.");
82        }
83        boolean isProfileOwner = (ownerType == PROFILE_OWNER);
84
85        final Dialog dialog = new Dialog(getActivity(), R.style.ManagedProvisioningDialogTheme);
86        dialog.setContentView(R.layout.learn_more_dialog);
87        dialog.setCanceledOnTouchOutside(false);
88        if (!mUtils.isUserSetupCompleted(getActivity())) {
89            SystemBarHelper.hideSystemBars(dialog);
90        }
91
92        final TextView learnMoreMsg = (TextView) dialog.findViewById(R.id.learn_more_text1);
93        final TextView linkText = (TextView) dialog.findViewById(R.id.learn_more_link);
94        final TextView textFrpWarning = (TextView) dialog.findViewById(
95                R.id.learn_more_frp_warning);
96
97        initializeLearnMoreLink(linkText, isProfileOwner);
98        learnMoreMsg.setText(isProfileOwner ? R.string.admin_has_ability_to_monitor_profile
99                : R.string.admin_has_ability_to_monitor_device);
100
101        if (!isProfileOwner && mUtils.isFrpSupported(getActivity())) {
102            // For device owner, show a warning that FRP might not be fully active
103            textFrpWarning.setVisibility(View.VISIBLE);
104        }
105
106        final Button positiveButton = (Button) dialog.findViewById(R.id.positive_button);
107        positiveButton.setOnClickListener(new OnClickListener() {
108                @Override
109                public void onClick(View v) {
110                    dialog.dismiss();
111                    ((ConsentCallback) getActivity()).onDialogConsent();
112                }
113            });
114
115        final CheckBox consentCheckbox =
116                (CheckBox) dialog.findViewById(R.id.user_consent_checkbox);
117        if (getArguments().getBoolean(KEY_SHOW_CONSENT_CHECKBOX)) {
118            consentCheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
119                    @Override
120                    public void onCheckedChanged(CompoundButton cb, boolean isChecked) {
121                        positiveButton.setEnabled(isChecked);
122                    }
123                });
124            consentCheckbox.setVisibility(View.VISIBLE);
125            consentCheckbox.setChecked(false);
126            positiveButton.setEnabled(false);
127        }
128
129        final Button negativeButton = (Button) dialog.findViewById(R.id.negative_button);
130        negativeButton.setOnClickListener(new OnClickListener() {
131                @Override
132                public void onClick(View v) {
133                    dialog.dismiss();
134                    ((ConsentCallback) getActivity()).onDialogCancel();
135                }
136            });
137
138        return dialog;
139    }
140
141    private void initializeLearnMoreLink(TextView linkText, boolean isProfileOwner) {
142        if (!mUtils.isConnectedToNetwork(getActivity())) {
143            // If the device has currently no connectivity, don't show the "learn more" link.
144            linkText.setVisibility(View.GONE);
145        } else {
146            // Otherwise register a listener that starts a webview activity.
147            final String url = isProfileOwner ? LEARN_MORE_URL_PROFILE_OWNER
148                    : LEARN_MORE_URL_DEVICE_OWNER;
149            linkText.setOnClickListener(new OnClickListener() {
150                @Override
151                public void onClick(View v) {
152                    getActivity().startActivity(WebActivity.createIntent(getActivity(), url,
153                            LEARN_MORE_ALLOWED_BASE_URL));
154                }
155            });
156        }
157    }
158
159    @Override
160    public void onCancel(DialogInterface dialog) {
161        ((ConsentCallback) getActivity()).onDialogCancel();
162    }
163
164    public interface ConsentCallback {
165        public abstract void onDialogConsent();
166        public abstract void onDialogCancel();
167    }
168}
169