1 /*
2  * Copyright (C) 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.email.activity.setup;
18 
19 import android.app.AlertDialog;
20 import android.app.Dialog;
21 import android.app.DialogFragment;
22 import android.content.Context;
23 import android.content.DialogInterface;
24 import android.os.Bundle;
25 
26 import com.android.email.R;
27 
28 /**
29  * The "security required" error dialog.  This is presented whenever an exchange account
30  * reports that it will require security policy control, and provide the user with the
31  * opportunity to accept or deny this.
32  *
33  * If the user clicks OK, calls onSecurityRequiredDialogResultOk(true) which reports back
34  * to the target as if the settings check was "ok".  If the user clicks "cancel", calls
35  * onSecurityRequiredDialogResultOk(false) which simply closes the checker (this is the
36  * same as any other failed check.)
37  */
38 
39 public class SecurityRequiredDialogFragment extends DialogFragment {
40     public final static String TAG = "SecurityRequiredDialog";
41 
42     // Bundle keys for arguments
43     private final static String ARGS_HOST_NAME = "SecurityRequiredDialog.HostName";
44 
45     public interface Callback {
46 
47         /**
48          * Callback for the result of this dialog fragment
49          * @param ok True for OK pressed, false for cancel
50          */
onSecurityRequiredDialogResult(boolean ok)51         void onSecurityRequiredDialogResult(boolean ok);
52     }
53 
54     // Public no-args constructor needed for fragment re-instantiation
SecurityRequiredDialogFragment()55     public SecurityRequiredDialogFragment() {}
56 
newInstance(String hostName)57     public static SecurityRequiredDialogFragment newInstance(String hostName) {
58         final SecurityRequiredDialogFragment fragment = new SecurityRequiredDialogFragment();
59         final Bundle arguments = new Bundle(1);
60         arguments.putString(ARGS_HOST_NAME, hostName);
61         fragment.setArguments(arguments);
62         return fragment;
63     }
64 
65     @Override
onCreateDialog(Bundle savedInstanceState)66     public Dialog onCreateDialog(Bundle savedInstanceState) {
67         final Context context = getActivity();
68         final Bundle arguments = getArguments();
69         final String hostName = arguments.getString(ARGS_HOST_NAME);
70 
71         setCancelable(true);
72 
73         return new AlertDialog.Builder(context)
74                 .setIconAttribute(android.R.attr.alertDialogIcon)
75                 .setTitle(context.getString(R.string.account_setup_security_required_title))
76                 .setMessage(context.getString(
77                         R.string.account_setup_security_policies_required_fmt, hostName))
78                 .setPositiveButton(
79                         context.getString(android.R.string.ok),
80                         new DialogInterface.OnClickListener() {
81                             @Override
82                             public void onClick(DialogInterface dialog, int which) {
83                                 dismiss();
84                                 final Callback callback = (Callback) getActivity();
85                                 callback.onSecurityRequiredDialogResult(true);
86                             }
87                         })
88                 .setNegativeButton(
89                         context.getString(android.R.string.cancel),
90                         new DialogInterface.OnClickListener() {
91                             @Override
92                             public void onClick(DialogInterface dialog, int which) {
93                                 dialog.cancel();
94                             }
95                         })
96                 .create();
97     }
98 
99     @Override
100     public void onCancel(DialogInterface dialog) {
101         super.onCancel(dialog);
102         final Callback callback = (Callback) getActivity();
103         if (callback != null) {
104             callback.onSecurityRequiredDialogResult(false);
105         }
106     }
107 }
108