1 /*
2  * Copyright (C) 2016 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 package com.android.settings.vpn2;
17 
18 import android.app.Fragment;
19 import android.app.AlertDialog;
20 import android.app.Dialog;
21 import android.app.DialogFragment;
22 import android.content.DialogInterface;
23 import android.os.Bundle;
24 
25 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
26 import com.android.settings.R;
27 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
28 
29 public class ConfirmLockdownFragment extends InstrumentedDialogFragment
30         implements DialogInterface.OnClickListener {
31     public interface ConfirmLockdownListener {
onConfirmLockdown(Bundle options, boolean isEnabled, boolean isLockdown)32         public void onConfirmLockdown(Bundle options, boolean isEnabled, boolean isLockdown);
33     }
34 
35     private static final String TAG = "ConfirmLockdown";
36 
37     @Override
getMetricsCategory()38     public int getMetricsCategory() {
39         return MetricsEvent.DIALOG_VPN_REPLACE_EXISTING;
40     }
41 
42     private static final String ARG_REPLACING = "replacing";
43     private static final String ARG_ALWAYS_ON = "always_on";
44     private static final String ARG_LOCKDOWN_SRC = "lockdown_old";
45     private static final String ARG_LOCKDOWN_DST = "lockdown_new";
46     private static final String ARG_OPTIONS = "options";
47 
shouldShow(boolean replacing, boolean fromLockdown, boolean toLockdown)48     public static boolean shouldShow(boolean replacing, boolean fromLockdown, boolean toLockdown) {
49         // We only need to show this if we are:
50         //  - replacing an existing connection
51         //  - switching on always-on mode with lockdown enabled where it was not enabled before.
52         return replacing || (toLockdown && !fromLockdown);
53     }
54 
show(Fragment parent, boolean replacing, boolean alwaysOn, boolean fromLockdown, boolean toLockdown, Bundle options)55     public static void show(Fragment parent, boolean replacing, boolean alwaysOn,
56             boolean fromLockdown, boolean toLockdown, Bundle options) {
57         if (parent.getFragmentManager().findFragmentByTag(TAG) != null) {
58             // Already exists. Don't show it twice.
59             return;
60         }
61         final Bundle args = new Bundle();
62         args.putBoolean(ARG_REPLACING, replacing);
63         args.putBoolean(ARG_ALWAYS_ON, alwaysOn);
64         args.putBoolean(ARG_LOCKDOWN_SRC, fromLockdown);
65         args.putBoolean(ARG_LOCKDOWN_DST, toLockdown);
66         args.putParcelable(ARG_OPTIONS, options);
67 
68         final ConfirmLockdownFragment frag = new ConfirmLockdownFragment();
69         frag.setArguments(args);
70         frag.setTargetFragment(parent, 0);
71         frag.show(parent.getFragmentManager(), TAG);
72     }
73 
74     @Override
onCreateDialog(Bundle savedInstanceState)75     public Dialog onCreateDialog(Bundle savedInstanceState) {
76         final boolean replacing = getArguments().getBoolean(ARG_REPLACING);
77         final boolean alwaysOn = getArguments().getBoolean(ARG_ALWAYS_ON);
78         final boolean wasLockdown = getArguments().getBoolean(ARG_LOCKDOWN_SRC);
79         final boolean nowLockdown = getArguments().getBoolean(ARG_LOCKDOWN_DST);
80 
81         final int titleId = (nowLockdown ? R.string.vpn_require_connection_title :
82                 (replacing ? R.string.vpn_replace_vpn_title : R.string.vpn_set_vpn_title));
83         final int actionId =
84                 (replacing ? R.string.vpn_replace :
85                 (nowLockdown ? R.string.vpn_turn_on : R.string.okay));
86         final int messageId;
87         if (nowLockdown) {
88             messageId = replacing
89                     ? R.string.vpn_replace_always_on_vpn_enable_message
90                     : R.string.vpn_first_always_on_vpn_message;
91         } else {
92             messageId = wasLockdown
93                     ? R.string.vpn_replace_always_on_vpn_disable_message
94                     : R.string.vpn_replace_vpn_message;
95         }
96 
97         return new AlertDialog.Builder(getActivity())
98                 .setTitle(titleId)
99                 .setMessage(messageId)
100                 .setNegativeButton(R.string.cancel, null)
101                 .setPositiveButton(actionId, this)
102                 .create();
103     }
104 
105     @Override
onClick(DialogInterface dialog, int which)106     public void onClick(DialogInterface dialog, int which) {
107         if (getTargetFragment() instanceof ConfirmLockdownListener) {
108             ((ConfirmLockdownListener) getTargetFragment()).onConfirmLockdown(
109                     getArguments().getParcelable(ARG_OPTIONS),
110                     getArguments().getBoolean(ARG_ALWAYS_ON),
111                     getArguments().getBoolean(ARG_LOCKDOWN_DST));
112         }
113     }
114 }
115 
116