1b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee/*
2b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee * Copyright (C) 2016 The Android Open Source Project
3b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee *
4b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee * Licensed under the Apache License, Version 2.0 (the "License");
5b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee * you may not use this file except in compliance with the License.
6b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee * You may obtain a copy of the License at
7b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee *
8b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee *      http://www.apache.org/licenses/LICENSE-2.0
9b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee *
10b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee * Unless required by applicable law or agreed to in writing, software
11b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee * distributed under the License is distributed on an "AS IS" BASIS,
12b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee * See the License for the specific language governing permissions and
14b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee * limitations under the License.
15b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee */
16b6f787c4df8e111bdee75062c6a38f606c876858Robin Leepackage com.android.settings.vpn2;
17b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee
18b6f787c4df8e111bdee75062c6a38f606c876858Robin Leeimport android.app.Fragment;
19b6f787c4df8e111bdee75062c6a38f606c876858Robin Leeimport android.app.AlertDialog;
20b6f787c4df8e111bdee75062c6a38f606c876858Robin Leeimport android.app.Dialog;
21b6f787c4df8e111bdee75062c6a38f606c876858Robin Leeimport android.app.DialogFragment;
22b6f787c4df8e111bdee75062c6a38f606c876858Robin Leeimport android.content.DialogInterface;
23b6f787c4df8e111bdee75062c6a38f606c876858Robin Leeimport android.os.Bundle;
24b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee
25265d3c2a0c36251bf8a9f571d7239b6dd404d942Tamas Berghammerimport com.android.internal.logging.nano.MetricsProto.MetricsEvent;
26b6f787c4df8e111bdee75062c6a38f606c876858Robin Leeimport com.android.settings.R;
27b6f787c4df8e111bdee75062c6a38f606c876858Robin Leeimport com.android.settings.core.instrumentation.InstrumentedDialogFragment;
28b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee
29b6f787c4df8e111bdee75062c6a38f606c876858Robin Leepublic class ConfirmLockdownFragment extends InstrumentedDialogFragment
30b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee        implements DialogInterface.OnClickListener {
31b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee    public interface ConfirmLockdownListener {
32e7347dd8c02b67cb422765b947b398aa4dea9eb0Robin Lee        public void onConfirmLockdown(Bundle options, boolean isEnabled, boolean isLockdown);
33b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee    }
34b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee
35b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee    private static final String TAG = "ConfirmLockdown";
36b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee
37b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee    @Override
38b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee    public int getMetricsCategory() {
39b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee        return MetricsEvent.DIALOG_VPN_REPLACE_EXISTING;
40b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee    }
41b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee
42b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee    private static final String ARG_REPLACING = "replacing";
43e7347dd8c02b67cb422765b947b398aa4dea9eb0Robin Lee    private static final String ARG_ALWAYS_ON = "always_on";
44b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee    private static final String ARG_LOCKDOWN_SRC = "lockdown_old";
45b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee    private static final String ARG_LOCKDOWN_DST = "lockdown_new";
46b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee    private static final String ARG_OPTIONS = "options";
47b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee
48b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee    public static boolean shouldShow(boolean replacing, boolean fromLockdown, boolean toLockdown) {
49b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee        // We only need to show this if we are:
50b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee        //  - replacing an existing connection
51e7347dd8c02b67cb422765b947b398aa4dea9eb0Robin Lee        //  - switching on always-on mode with lockdown enabled where it was not enabled before.
52b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee        return replacing || (toLockdown && !fromLockdown);
53b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee    }
54b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee
55e7347dd8c02b67cb422765b947b398aa4dea9eb0Robin Lee    public static void show(Fragment parent, boolean replacing, boolean alwaysOn,
56b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee            boolean fromLockdown, boolean toLockdown, Bundle options) {
57b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee        if (parent.getFragmentManager().findFragmentByTag(TAG) != null) {
58b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee            // Already exists. Don't show it twice.
59b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee            return;
60b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee        }
61b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee        final Bundle args = new Bundle();
62b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee        args.putBoolean(ARG_REPLACING, replacing);
63e7347dd8c02b67cb422765b947b398aa4dea9eb0Robin Lee        args.putBoolean(ARG_ALWAYS_ON, alwaysOn);
64b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee        args.putBoolean(ARG_LOCKDOWN_SRC, fromLockdown);
65b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee        args.putBoolean(ARG_LOCKDOWN_DST, toLockdown);
66b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee        args.putParcelable(ARG_OPTIONS, options);
67b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee
68b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee        final ConfirmLockdownFragment frag = new ConfirmLockdownFragment();
69b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee        frag.setArguments(args);
70b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee        frag.setTargetFragment(parent, 0);
71b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee        frag.show(parent.getFragmentManager(), TAG);
72b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee    }
73b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee
74b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee    @Override
75b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee    public Dialog onCreateDialog(Bundle savedInstanceState) {
76b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee        final boolean replacing = getArguments().getBoolean(ARG_REPLACING);
77e7347dd8c02b67cb422765b947b398aa4dea9eb0Robin Lee        final boolean alwaysOn = getArguments().getBoolean(ARG_ALWAYS_ON);
78e7347dd8c02b67cb422765b947b398aa4dea9eb0Robin Lee        final boolean wasLockdown = getArguments().getBoolean(ARG_LOCKDOWN_SRC);
79e7347dd8c02b67cb422765b947b398aa4dea9eb0Robin Lee        final boolean nowLockdown = getArguments().getBoolean(ARG_LOCKDOWN_DST);
80b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee
817f96c4c84813c234bfad79a6fb0cd190feedbbbaRobin Lee        final int titleId = (nowLockdown ? R.string.vpn_require_connection_title :
827f96c4c84813c234bfad79a6fb0cd190feedbbbaRobin Lee                (replacing ? R.string.vpn_replace_vpn_title : R.string.vpn_set_vpn_title));
83b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee        final int actionId =
84b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee                (replacing ? R.string.vpn_replace :
85e7347dd8c02b67cb422765b947b398aa4dea9eb0Robin Lee                (nowLockdown ? R.string.vpn_turn_on : R.string.okay));
86b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee        final int messageId;
87e7347dd8c02b67cb422765b947b398aa4dea9eb0Robin Lee        if (nowLockdown) {
88b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee            messageId = replacing
89b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee                    ? R.string.vpn_replace_always_on_vpn_enable_message
90b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee                    : R.string.vpn_first_always_on_vpn_message;
91b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee        } else {
92e7347dd8c02b67cb422765b947b398aa4dea9eb0Robin Lee            messageId = wasLockdown
93b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee                    ? R.string.vpn_replace_always_on_vpn_disable_message
94b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee                    : R.string.vpn_replace_vpn_message;
95b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee        }
96b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee
97b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee        return new AlertDialog.Builder(getActivity())
98b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee                .setTitle(titleId)
99b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee                .setMessage(messageId)
100b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee                .setNegativeButton(R.string.cancel, null)
101b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee                .setPositiveButton(actionId, this)
102b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee                .create();
103b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee    }
104b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee
105b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee    @Override
106b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee    public void onClick(DialogInterface dialog, int which) {
107b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee        if (getTargetFragment() instanceof ConfirmLockdownListener) {
108b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee            ((ConfirmLockdownListener) getTargetFragment()).onConfirmLockdown(
109b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee                    getArguments().getParcelable(ARG_OPTIONS),
110e7347dd8c02b67cb422765b947b398aa4dea9eb0Robin Lee                    getArguments().getBoolean(ARG_ALWAYS_ON),
111b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee                    getArguments().getBoolean(ARG_LOCKDOWN_DST));
112b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee        }
113b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee    }
114b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee}
115b6f787c4df8e111bdee75062c6a38f606c876858Robin Lee
116