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