summaryrefslogtreecommitdiff
path: root/Settings/src/com/android/tv/settings/connectivity/setup/AdvancedWifiOptionsFlow.java
blob: dffee7bebc85c1a95c1293a38f3710463105bacd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.tv.settings.connectivity.setup;

import android.net.IpConfiguration;
import android.util.Log;

import androidx.annotation.IntDef;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.ViewModelProviders;

import com.android.tv.settings.connectivity.NetworkConfiguration;
import com.android.tv.settings.connectivity.util.State;
import com.android.tv.settings.connectivity.util.StateMachine;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;


/**
 * Handles the flow of setting advanced options.
 */
public class AdvancedWifiOptionsFlow {

    /** Flag that set advanced flow start with default page */
    public static final int START_DEFAULT_PAGE = 0;
    /** Flag that set advanced flow start with IP settings page */
    public static final int START_IP_SETTINGS_PAGE = 1;
    /** Flag that set advanced flow start with proxy settings page */
    public static final int START_PROXY_SETTINGS_PAGE = 2;
    private static final String TAG = "AdvancedWifiOptionsFlow";

    @IntDef({
            START_DEFAULT_PAGE,
            START_IP_SETTINGS_PAGE,
            START_PROXY_SETTINGS_PAGE
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface START_PAGE {
    }

    /**
     * Create a advanced flow.
     *
     * @param activity             activity that starts the advanced flow.
     * @param askFirst             whether ask user to start advanced flow
     * @param isSettingsFlow       whether advanced flow is started from settings flow
     * @param initialConfiguration the previous {@link NetworkConfiguration} info.
     * @param entranceState        The state that starts the advanced flow, null if there is none.
     * @param exitState            The state where the advanced flow go after it ends.
     * @param startPage            The page where the advanced flow starts with.
     */
    public static void createFlow(FragmentActivity activity,
            boolean askFirst,
            boolean isSettingsFlow,
            NetworkConfiguration initialConfiguration,
            State entranceState,
            State exitState,
            @START_PAGE int startPage) {
        StateMachine stateMachine = ViewModelProviders.of(activity).get(StateMachine.class);
        AdvancedOptionsFlowInfo advancedOptionsFlowInfo = ViewModelProviders.of(activity).get(
                AdvancedOptionsFlowInfo.class);
        advancedOptionsFlowInfo.setSettingsFlow(isSettingsFlow);
        if (initialConfiguration != null) {
            advancedOptionsFlowInfo.setPrintableSsid(initialConfiguration.getPrintableName());
            advancedOptionsFlowInfo.setIpConfiguration(initialConfiguration.getIpConfiguration());
        } else {
            advancedOptionsFlowInfo.setPrintableSsid("");
            advancedOptionsFlowInfo.setIpConfiguration(new IpConfiguration());
        }
        State advancedOptionsState = new AdvancedOptionsState(activity);
        State proxySettingsState = new ProxySettingsState(activity);
        State ipSettingsState = new IpSettingsState(activity);
        State proxyHostNameState = new ProxyHostNameState(activity);
        State proxyPortState = new ProxyPortState(activity);
        State proxyBypassState = new ProxyBypassState(activity);
        State proxySettingsInvalidState = new ProxySettingsInvalidState(activity);
        State ipAddressState = new IpAddressState(activity);
        State gatewayState = new GatewayState(activity);
        State networkPrefixLengthState = new NetworkPrefixLengthState(activity);
        State dns1State = new Dns1State(activity);
        State dns2State = new Dns2State(activity);
        State ipSettingsInvalidState = new IpSettingsInvalidState(activity);
        State advancedFlowCompleteState = new AdvancedFlowCompleteState(activity);

        // Define the transitions between external states and internal states for advanced options
        // flow.
        State startState = null;
        switch (startPage) {
            case START_DEFAULT_PAGE :
                if (askFirst) {
                    startState = advancedOptionsState;
                } else {
                    startState = proxySettingsState;
                }
                break;
            case START_IP_SETTINGS_PAGE :
                startState = ipSettingsState;
                break;
            case START_PROXY_SETTINGS_PAGE :
                startState = proxySettingsState;
                break;
            default:
                Log.wtf(TAG, "Got a wrong start state");
                break;
        }

        /* Entrance */
        if (entranceState != null) {
            stateMachine.addState(
                    entranceState,
                    StateMachine.ENTER_ADVANCED_FLOW,
                    startState
            );
        } else {
            stateMachine.setStartState(startState);
        }

        /* Exit */
        stateMachine.addState(
                advancedFlowCompleteState,
                StateMachine.EXIT_ADVANCED_FLOW,
                exitState
        );

        // Define the transitions between different states in advanced options flow.
        /* Advanced Options */
        stateMachine.addState(
                advancedOptionsState,
                StateMachine.ADVANCED_FLOW_COMPLETE,
                advancedFlowCompleteState
        );
        stateMachine.addState(
                advancedOptionsState,
                StateMachine.CONTINUE,
                proxySettingsState
        );

        /* Proxy Settings */
        stateMachine.addState(
                proxySettingsState,
                StateMachine.IP_SETTINGS,
                ipSettingsState
        );
        stateMachine.addState(
                proxySettingsState,
                StateMachine.ADVANCED_FLOW_COMPLETE,
                advancedFlowCompleteState
        );
        stateMachine.addState(
                proxySettingsState,
                StateMachine.PROXY_HOSTNAME,
                proxyHostNameState
        );

        /* Proxy Hostname */
        stateMachine.addState(
                proxyHostNameState,
                StateMachine.CONTINUE,
                proxyPortState
        );

        /* Proxy Port */
        stateMachine.addState(
                proxyPortState,
                StateMachine.CONTINUE,
                proxyBypassState
        );

        /* Proxy Bypass */
        stateMachine.addState(
                proxyBypassState,
                StateMachine.ADVANCED_FLOW_COMPLETE,
                advancedFlowCompleteState
        );
        stateMachine.addState(
                proxyBypassState,
                StateMachine.IP_SETTINGS,
                ipSettingsState
        );
        stateMachine.addState(
                proxyBypassState,
                StateMachine.PROXY_SETTINGS_INVALID,
                proxySettingsInvalidState
        );

        /* Proxy Settings Invalid */
        stateMachine.addState(
                proxySettingsInvalidState,
                StateMachine.CONTINUE,
                proxySettingsState
        );

        /* Ip Settings */
        stateMachine.addState(
                ipSettingsState,
                StateMachine.ADVANCED_FLOW_COMPLETE,
                advancedFlowCompleteState
        );
        stateMachine.addState(
                ipSettingsState,
                StateMachine.CONTINUE,
                ipAddressState
        );

        /* Ip Address */
        stateMachine.addState(
                ipAddressState,
                StateMachine.CONTINUE,
                gatewayState
        );

        /* Gateway */
        stateMachine.addState(
                gatewayState,
                StateMachine.CONTINUE,
                networkPrefixLengthState
        );

        /* Network Prefix Length */
        stateMachine.addState(
                networkPrefixLengthState,
                StateMachine.CONTINUE,
                dns1State
        );

        /* Dns1 */
        stateMachine.addState(
                dns1State,
                StateMachine.CONTINUE,
                dns2State
        );

        /* Dns2 */
        stateMachine.addState(
                dns2State,
                StateMachine.ADVANCED_FLOW_COMPLETE,
                advancedFlowCompleteState);
        stateMachine.addState(
                dns2State,
                StateMachine.IP_SETTINGS_INVALID,
                ipSettingsInvalidState
        );

        /* Ip Settings Invalid */
        stateMachine.addState(
                ipSettingsInvalidState,
                StateMachine.CONTINUE,
                ipSettingsState
        );
    }
}