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 if (initialConfiguration != null) { 79 advancedOptionsFlowInfo.setPrintableSsid(initialConfiguration.getPrintableName()); 80 advancedOptionsFlowInfo.setIpConfiguration(initialConfiguration.getIpConfiguration()); 81 } else { 82 advancedOptionsFlowInfo.setPrintableSsid(""); 83 advancedOptionsFlowInfo.setIpConfiguration(new IpConfiguration()); 84 } 85 State advancedOptionsState = new AdvancedOptionsState(activity); 86 State proxySettingsState = new ProxySettingsState(activity); 87 State ipSettingsState = new IpSettingsState(activity); 88 State proxyHostNameState = new ProxyHostNameState(activity); 89 State proxyPortState = new ProxyPortState(activity); 90 State proxyBypassState = new ProxyBypassState(activity); 91 State proxySettingsInvalidState = new ProxySettingsInvalidState(activity); 92 State ipAddressState = new IpAddressState(activity); 93 State gatewayState = new GatewayState(activity); 94 State networkPrefixLengthState = new NetworkPrefixLengthState(activity); 95 State dns1State = new Dns1State(activity); 96 State dns2State = new Dns2State(activity); 97 State ipSettingsInvalidState = new IpSettingsInvalidState(activity); 98 State advancedFlowCompleteState = new AdvancedFlowCompleteState(activity); 99 100 // Define the transitions between external states and internal states for advanced options 101 // flow. 102 State startState = null; 103 switch (startPage) { 104 case START_DEFAULT_PAGE : 105 if (askFirst) { 106 startState = advancedOptionsState; 107 } else { 108 startState = proxySettingsState; 109 } 110 break; 111 case START_IP_SETTINGS_PAGE : 112 startState = ipSettingsState; 113 break; 114 case START_PROXY_SETTINGS_PAGE : 115 startState = proxySettingsState; 116 break; 117 default: 118 Log.wtf(TAG, "Got a wrong start state"); 119 break; 120 } 121 122 /* Entrance */ 123 if (entranceState != null) { 124 stateMachine.addState( 125 entranceState, 126 StateMachine.ENTER_ADVANCED_FLOW, 127 startState 128 ); 129 } else { 130 stateMachine.setStartState(startState); 131 } 132 133 /* Exit */ 134 stateMachine.addState( 135 advancedFlowCompleteState, 136 StateMachine.EXIT_ADVANCED_FLOW, 137 exitState 138 ); 139 140 // Define the transitions between different states in advanced options flow. 141 /* Advanced Options */ 142 stateMachine.addState( 143 advancedOptionsState, 144 StateMachine.ADVANCED_FLOW_COMPLETE, 145 advancedFlowCompleteState 146 ); 147 stateMachine.addState( 148 advancedOptionsState, 149 StateMachine.CONTINUE, 150 proxySettingsState 151 ); 152 153 /* Proxy Settings */ 154 stateMachine.addState( 155 proxySettingsState, 156 StateMachine.IP_SETTINGS, 157 ipSettingsState 158 ); 159 stateMachine.addState( 160 proxySettingsState, 161 StateMachine.ADVANCED_FLOW_COMPLETE, 162 advancedFlowCompleteState 163 ); 164 stateMachine.addState( 165 proxySettingsState, 166 StateMachine.PROXY_HOSTNAME, 167 proxyHostNameState 168 ); 169 170 /* Proxy Hostname */ 171 stateMachine.addState( 172 proxyHostNameState, 173 StateMachine.CONTINUE, 174 proxyPortState 175 ); 176 177 /* Proxy Port */ 178 stateMachine.addState( 179 proxyPortState, 180 StateMachine.CONTINUE, 181 proxyBypassState 182 ); 183 184 /* Proxy Bypass */ 185 stateMachine.addState( 186 proxyBypassState, 187 StateMachine.ADVANCED_FLOW_COMPLETE, 188 advancedFlowCompleteState 189 ); 190 stateMachine.addState( 191 proxyBypassState, 192 StateMachine.IP_SETTINGS, 193 ipSettingsState 194 ); 195 stateMachine.addState( 196 proxyBypassState, 197 StateMachine.PROXY_SETTINGS_INVALID, 198 proxySettingsInvalidState 199 ); 200 201 /* Proxy Settings Invalid */ 202 stateMachine.addState( 203 proxySettingsInvalidState, 204 StateMachine.CONTINUE, 205 proxySettingsState 206 ); 207 208 /* Ip Settings */ 209 stateMachine.addState( 210 ipSettingsState, 211 StateMachine.ADVANCED_FLOW_COMPLETE, 212 advancedFlowCompleteState 213 ); 214 stateMachine.addState( 215 ipSettingsState, 216 StateMachine.CONTINUE, 217 ipAddressState 218 ); 219 220 /* Ip Address */ 221 stateMachine.addState( 222 ipAddressState, 223 StateMachine.CONTINUE, 224 gatewayState 225 ); 226 227 /* Gateway */ 228 stateMachine.addState( 229 gatewayState, 230 StateMachine.CONTINUE, 231 networkPrefixLengthState 232 ); 233 234 /* Network Prefix Length */ 235 stateMachine.addState( 236 networkPrefixLengthState, 237 StateMachine.CONTINUE, 238 dns1State 239 ); 240 241 /* Dns1 */ 242 stateMachine.addState( 243 dns1State, 244 StateMachine.CONTINUE, 245 dns2State 246 ); 247 248 /* Dns2 */ 249 stateMachine.addState( 250 dns2State, 251 StateMachine.ADVANCED_FLOW_COMPLETE, 252 advancedFlowCompleteState); 253 stateMachine.addState( 254 dns2State, 255 StateMachine.IP_SETTINGS_INVALID, 256 ipSettingsInvalidState 257 ); 258 259 /* Ip Settings Invalid */ 260 stateMachine.addState( 261 ipSettingsInvalidState, 262 StateMachine.CONTINUE, 263 ipSettingsState 264 ); 265 } 266 } 267