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