1 /*
2  * Copyright (C) 2017 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.tv.settings.connectivity.setup;
18 
19 import android.net.IpConfiguration;
20 import android.util.Log;
21 
22 import androidx.annotation.IntDef;
23 import androidx.fragment.app.FragmentActivity;
24 import androidx.lifecycle.ViewModelProviders;
25 
26 import com.android.tv.settings.connectivity.NetworkConfiguration;
27 import com.android.tv.settings.connectivity.util.State;
28 import com.android.tv.settings.connectivity.util.StateMachine;
29 
30 import java.lang.annotation.Retention;
31 import java.lang.annotation.RetentionPolicy;
32 
33 
34 /**
35  * Handles the flow of setting advanced options.
36  */
37 public class AdvancedWifiOptionsFlow {
38 
39     /** Flag that set advanced flow start with default page */
40     public static final int START_DEFAULT_PAGE = 0;
41     /** Flag that set advanced flow start with IP settings page */
42     public static final int START_IP_SETTINGS_PAGE = 1;
43     /** Flag that set advanced flow start with proxy settings page */
44     public static final int START_PROXY_SETTINGS_PAGE = 2;
45     private static final String TAG = "AdvancedWifiOptionsFlow";
46 
47     @IntDef({
48             START_DEFAULT_PAGE,
49             START_IP_SETTINGS_PAGE,
50             START_PROXY_SETTINGS_PAGE
51     })
52     @Retention(RetentionPolicy.SOURCE)
53     public @interface START_PAGE {
54     }
55 
56     /**
57      * Create a advanced flow.
58      *
59      * @param activity             activity that starts the advanced flow.
60      * @param askFirst             whether ask user to start advanced flow
61      * @param isSettingsFlow       whether advanced flow is started from settings flow
62      * @param initialConfiguration the previous {@link NetworkConfiguration} info.
63      * @param entranceState        The state that starts the advanced flow, null if there is none.
64      * @param exitState            The state where the advanced flow go after it ends.
65      * @param startPage            The page where the advanced flow starts with.
66      */
createFlow(FragmentActivity activity, boolean askFirst, boolean isSettingsFlow, NetworkConfiguration initialConfiguration, State entranceState, State exitState, @START_PAGE int startPage)67     public static void createFlow(FragmentActivity activity,
68             boolean askFirst,
69             boolean isSettingsFlow,
70             NetworkConfiguration initialConfiguration,
71             State entranceState,
72             State exitState,
73             @START_PAGE int startPage) {
74         StateMachine stateMachine = ViewModelProviders.of(activity).get(StateMachine.class);
75         AdvancedOptionsFlowInfo advancedOptionsFlowInfo = ViewModelProviders.of(activity).get(
76                 AdvancedOptionsFlowInfo.class);
77         advancedOptionsFlowInfo.setSettingsFlow(isSettingsFlow);
78         IpConfiguration ipConfiguration = (initialConfiguration != null)
79                 ? initialConfiguration.getIpConfiguration()
80                 : new IpConfiguration();
81         advancedOptionsFlowInfo.setIpConfiguration(ipConfiguration);
82         State advancedOptionsState = new AdvancedOptionsState(activity);
83         State proxySettingsState = new ProxySettingsState(activity);
84         State ipSettingsState = new IpSettingsState(activity);
85         State proxyHostNameState = new ProxyHostNameState(activity);
86         State proxyPortState = new ProxyPortState(activity);
87         State proxyBypassState = new ProxyBypassState(activity);
88         State proxySettingsInvalidState = new ProxySettingsInvalidState(activity);
89         State ipAddressState = new IpAddressState(activity);
90         State gatewayState = new GatewayState(activity);
91         State networkPrefixLengthState = new NetworkPrefixLengthState(activity);
92         State dns1State = new Dns1State(activity);
93         State dns2State = new Dns2State(activity);
94         State ipSettingsInvalidState = new IpSettingsInvalidState(activity);
95         State advancedFlowCompleteState = new AdvancedFlowCompleteState(activity);
96 
97         // Define the transitions between external states and internal states for advanced options
98         // flow.
99         State startState = null;
100         switch (startPage) {
101             case START_DEFAULT_PAGE :
102                 if (askFirst) {
103                     startState = advancedOptionsState;
104                 } else {
105                     startState = proxySettingsState;
106                 }
107                 break;
108             case START_IP_SETTINGS_PAGE :
109                 startState = ipSettingsState;
110                 break;
111             case START_PROXY_SETTINGS_PAGE :
112                 startState = proxySettingsState;
113                 break;
114             default:
115                 Log.wtf(TAG, "Got a wrong start state");
116                 break;
117         }
118 
119         /* Entrance */
120         if (entranceState != null) {
121             stateMachine.addState(
122                     entranceState,
123                     StateMachine.ENTER_ADVANCED_FLOW,
124                     startState
125             );
126         } else {
127             stateMachine.setStartState(startState);
128         }
129 
130         /* Exit */
131         stateMachine.addState(
132                 advancedFlowCompleteState,
133                 StateMachine.EXIT_ADVANCED_FLOW,
134                 exitState
135         );
136 
137         // Define the transitions between different states in advanced options flow.
138         /* Advanced Options */
139         stateMachine.addState(
140                 advancedOptionsState,
141                 StateMachine.ADVANCED_FLOW_COMPLETE,
142                 advancedFlowCompleteState
143         );
144         stateMachine.addState(
145                 advancedOptionsState,
146                 StateMachine.CONTINUE,
147                 proxySettingsState
148         );
149 
150         /* Proxy Settings */
151         stateMachine.addState(
152                 proxySettingsState,
153                 StateMachine.IP_SETTINGS,
154                 ipSettingsState
155         );
156         stateMachine.addState(
157                 proxySettingsState,
158                 StateMachine.ADVANCED_FLOW_COMPLETE,
159                 advancedFlowCompleteState
160         );
161         stateMachine.addState(
162                 proxySettingsState,
163                 StateMachine.PROXY_HOSTNAME,
164                 proxyHostNameState
165         );
166 
167         /* Proxy Hostname */
168         stateMachine.addState(
169                 proxyHostNameState,
170                 StateMachine.CONTINUE,
171                 proxyPortState
172         );
173 
174         /* Proxy Port */
175         stateMachine.addState(
176                 proxyPortState,
177                 StateMachine.CONTINUE,
178                 proxyBypassState
179         );
180 
181         /* Proxy Bypass */
182         stateMachine.addState(
183                 proxyBypassState,
184                 StateMachine.ADVANCED_FLOW_COMPLETE,
185                 advancedFlowCompleteState
186         );
187         stateMachine.addState(
188                 proxyBypassState,
189                 StateMachine.IP_SETTINGS,
190                 ipSettingsState
191         );
192         stateMachine.addState(
193                 proxyBypassState,
194                 StateMachine.PROXY_SETTINGS_INVALID,
195                 proxySettingsInvalidState
196         );
197 
198         /* Proxy Settings Invalid */
199         stateMachine.addState(
200                 proxySettingsInvalidState,
201                 StateMachine.CONTINUE,
202                 proxySettingsState
203         );
204 
205         /* Ip Settings */
206         stateMachine.addState(
207                 ipSettingsState,
208                 StateMachine.ADVANCED_FLOW_COMPLETE,
209                 advancedFlowCompleteState
210         );
211         stateMachine.addState(
212                 ipSettingsState,
213                 StateMachine.CONTINUE,
214                 ipAddressState
215         );
216 
217         /* Ip Address */
218         stateMachine.addState(
219                 ipAddressState,
220                 StateMachine.CONTINUE,
221                 gatewayState
222         );
223 
224         /* Gateway */
225         stateMachine.addState(
226                 gatewayState,
227                 StateMachine.CONTINUE,
228                 networkPrefixLengthState
229         );
230 
231         /* Network Prefix Length */
232         stateMachine.addState(
233                 networkPrefixLengthState,
234                 StateMachine.CONTINUE,
235                 dns1State
236         );
237 
238         /* Dns1 */
239         stateMachine.addState(
240                 dns1State,
241                 StateMachine.CONTINUE,
242                 dns2State
243         );
244 
245         /* Dns2 */
246         stateMachine.addState(
247                 dns2State,
248                 StateMachine.ADVANCED_FLOW_COMPLETE,
249                 advancedFlowCompleteState);
250         stateMachine.addState(
251                 dns2State,
252                 StateMachine.IP_SETTINGS_INVALID,
253                 ipSettingsInvalidState
254         );
255 
256         /* Ip Settings Invalid */
257         stateMachine.addState(
258                 ipSettingsInvalidState,
259                 StateMachine.CONTINUE,
260                 ipSettingsState
261         );
262     }
263 }
264