Home | History | Annotate | Download | only in connectivity
      1 /*
      2  * Copyright (C) 2014 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;
     18 
     19 import android.arch.lifecycle.ViewModelProviders;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.net.wifi.WifiConfiguration;
     23 import android.os.Bundle;
     24 import android.support.v4.app.Fragment;
     25 import android.support.v4.app.FragmentTransaction;
     26 
     27 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
     28 import com.android.settingslib.wifi.AccessPoint;
     29 import com.android.tv.settings.R;
     30 import com.android.tv.settings.connectivity.setup.AddStartState;
     31 import com.android.tv.settings.connectivity.setup.AdvancedWifiOptionsFlow;
     32 import com.android.tv.settings.connectivity.setup.ConnectAuthFailureState;
     33 import com.android.tv.settings.connectivity.setup.ConnectFailedState;
     34 import com.android.tv.settings.connectivity.setup.ConnectRejectedByApState;
     35 import com.android.tv.settings.connectivity.setup.ConnectState;
     36 import com.android.tv.settings.connectivity.setup.ConnectTimeOutState;
     37 import com.android.tv.settings.connectivity.setup.EnterPasswordState;
     38 import com.android.tv.settings.connectivity.setup.KnownNetworkState;
     39 import com.android.tv.settings.connectivity.setup.OptionsOrConnectState;
     40 import com.android.tv.settings.connectivity.setup.SuccessState;
     41 import com.android.tv.settings.connectivity.setup.UserChoiceInfo;
     42 import com.android.tv.settings.connectivity.util.State;
     43 import com.android.tv.settings.connectivity.util.StateMachine;
     44 import com.android.tv.settings.connectivity.util.WifiSecurityUtil;
     45 import com.android.tv.settings.core.instrumentation.InstrumentedActivity;
     46 
     47 /**
     48  * Add a wifi network where we already know the ssid/security; normal post-install settings.
     49  */
     50 public class WifiConnectionActivity extends InstrumentedActivity implements
     51         State.FragmentChangeListener {
     52     private static final String TAG = "WifiConnectionActivity";
     53 
     54     private static final String EXTRA_WIFI_SSID = "wifi_ssid";
     55     private static final String EXTRA_WIFI_SECURITY_NAME = "wifi_security_name";
     56 
     57     public static Intent createIntent(Context context, AccessPoint result, int security) {
     58         return new Intent(context, WifiConnectionActivity.class)
     59                 .putExtra(EXTRA_WIFI_SSID, result.getSsidStr())
     60                 .putExtra(EXTRA_WIFI_SECURITY_NAME, security);
     61     }
     62 
     63     public static Intent createIntent(Context context, AccessPoint result) {
     64         final int security = result.getSecurity();
     65         return createIntent(context, result, security);
     66     }
     67 
     68     public static Intent createIntent(Context context, WifiConfiguration configuration) {
     69         final int security = WifiSecurityUtil.getSecurity(configuration);
     70         final String ssid = configuration.getPrintableSsid();
     71         return new Intent(context, WifiConnectionActivity.class)
     72                 .putExtra(EXTRA_WIFI_SSID, ssid)
     73                 .putExtra(EXTRA_WIFI_SECURITY_NAME, security);
     74     }
     75 
     76     private WifiConfiguration mConfiguration;
     77     private int mWifiSecurity;
     78     private StateMachine mStateMachine;
     79     private State mConnectAuthFailureState;
     80     private State mConnectFailedState;
     81     private State mConnectRejectedByApState;
     82     private State mConnectState;
     83     private State mConnectTimeOutState;
     84     private State mEnterPasswordState;
     85     private State mKnownNetworkState;
     86     private State mSuccessState;
     87     private State mOptionsOrConnectState;
     88     private State mAddStartState;
     89     private State mFinishState;
     90 
     91     private final StateMachine.Callback mStateMachineCallback = new StateMachine.Callback() {
     92         @Override
     93         public void onFinish(int result) {
     94             setResult(result);
     95             finish();
     96         }
     97     };
     98 
     99     @Override
    100     protected void onCreate(Bundle savedInstanceState) {
    101         super.onCreate(savedInstanceState);
    102         setContentView(R.layout.wifi_container);
    103         mStateMachine = ViewModelProviders.of(this).get(StateMachine.class);
    104         mStateMachine.setCallback(mStateMachineCallback);
    105         mKnownNetworkState = new KnownNetworkState(this);
    106         mEnterPasswordState = new EnterPasswordState(this);
    107         mConnectState = new ConnectState(this);
    108         mConnectTimeOutState = new ConnectTimeOutState(this);
    109         mConnectRejectedByApState = new ConnectRejectedByApState(this);
    110         mConnectFailedState = new ConnectFailedState(this);
    111         mConnectAuthFailureState = new ConnectAuthFailureState(this);
    112         mSuccessState = new SuccessState(this);
    113         mOptionsOrConnectState = new OptionsOrConnectState(this);
    114         mAddStartState = new AddStartState(this);
    115         mFinishState = new FinishState(this);
    116 
    117         /* KnownNetwork */
    118         mStateMachine.addState(
    119                 mKnownNetworkState,
    120                 StateMachine.ADD_START,
    121                 mAddStartState);
    122         mStateMachine.addState(
    123                 mKnownNetworkState,
    124                 StateMachine.SELECT_WIFI,
    125                 mFinishState);
    126 
    127         /* Add Start */
    128         mStateMachine.addState(
    129                 mAddStartState,
    130                 StateMachine.PASSWORD,
    131                 mEnterPasswordState);
    132         mStateMachine.addState(
    133                 mAddStartState,
    134                 StateMachine.CONNECT,
    135                 mConnectState);
    136 
    137         /* Enter Password */
    138         mStateMachine.addState(
    139                 mEnterPasswordState,
    140                 StateMachine.OPTIONS_OR_CONNECT,
    141                 mOptionsOrConnectState);
    142 
    143         /* Option or Connect */
    144         mStateMachine.addState(
    145                 mOptionsOrConnectState,
    146                 StateMachine.CONNECT,
    147                 mConnectState);
    148 
    149         /* Connect */
    150         mStateMachine.addState(
    151                 mConnectState,
    152                 StateMachine.RESULT_REJECTED_BY_AP,
    153                 mConnectRejectedByApState);
    154         mStateMachine.addState(
    155                 mConnectState,
    156                 StateMachine.RESULT_UNKNOWN_ERROR,
    157                 mConnectFailedState);
    158         mStateMachine.addState(
    159                 mConnectState,
    160                 StateMachine.RESULT_TIMEOUT,
    161                 mConnectTimeOutState);
    162         mStateMachine.addState(
    163                 mConnectState,
    164                 StateMachine.RESULT_BAD_AUTH,
    165                 mConnectAuthFailureState);
    166         mStateMachine.addState(
    167                 mConnectState,
    168                 StateMachine.RESULT_SUCCESS,
    169                 mSuccessState);
    170 
    171         /* Connect Failed */
    172         mStateMachine.addState(
    173                 mConnectFailedState,
    174                 StateMachine.TRY_AGAIN,
    175                 mOptionsOrConnectState
    176         );
    177         mStateMachine.addState(
    178                 mConnectFailedState,
    179                 StateMachine.SELECT_WIFI,
    180                 mFinishState
    181         );
    182 
    183         /* Connect Timeout */
    184         mStateMachine.addState(
    185                 mConnectTimeOutState,
    186                 StateMachine.TRY_AGAIN,
    187                 mOptionsOrConnectState
    188         );
    189         mStateMachine.addState(
    190                 mConnectTimeOutState,
    191                 StateMachine.SELECT_WIFI,
    192                 mFinishState
    193         );
    194 
    195         /* Connect Rejected By AP */
    196         mStateMachine.addState(
    197                 mConnectRejectedByApState,
    198                 StateMachine.TRY_AGAIN,
    199                 mOptionsOrConnectState);
    200         mStateMachine.addState(
    201                 mConnectRejectedByApState,
    202                 StateMachine.SELECT_WIFI,
    203                 mFinishState);
    204 
    205         /*Connect Auth Failure */
    206         mStateMachine.addState(
    207                 mConnectAuthFailureState,
    208                 StateMachine.TRY_AGAIN,
    209                 mOptionsOrConnectState
    210         );
    211         mStateMachine.addState(
    212                 mConnectAuthFailureState,
    213                 StateMachine.SELECT_WIFI,
    214                 mFinishState
    215         );
    216 
    217         mWifiSecurity = getIntent().getIntExtra(EXTRA_WIFI_SECURITY_NAME, 0);
    218         mConfiguration = WifiConfigHelper.getConfiguration(
    219                 this, getIntent().getStringExtra(EXTRA_WIFI_SSID), mWifiSecurity);
    220 
    221         AdvancedWifiOptionsFlow.createFlow(
    222                 this, false, true, null,
    223                 mOptionsOrConnectState, mConnectState, AdvancedWifiOptionsFlow.START_DEFAULT_PAGE);
    224         UserChoiceInfo userChoiceInfo =
    225                     ViewModelProviders.of(this).get(UserChoiceInfo.class);
    226         userChoiceInfo.setWifiConfiguration(mConfiguration);
    227         userChoiceInfo.setWifiSecurity(mWifiSecurity);
    228         if (WifiConfigHelper.isNetworkSaved(mConfiguration)) {
    229             mStateMachine.setStartState(mKnownNetworkState);
    230         } else {
    231             mStateMachine.setStartState(mAddStartState);
    232         }
    233         mStateMachine.start(true);
    234     }
    235 
    236     @Override
    237     public void onBackPressed() {
    238         mStateMachine.back();
    239     }
    240 
    241     private void updateView(android.support.v4.app.Fragment fragment, boolean movingForward) {
    242         if (fragment != null) {
    243             FragmentTransaction updateTransaction = getSupportFragmentManager().beginTransaction();
    244             if (movingForward) {
    245                 updateTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    246             } else {
    247                 updateTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
    248             }
    249             updateTransaction.replace(R.id.wifi_container, fragment, TAG);
    250             updateTransaction.commit();
    251         }
    252     }
    253 
    254     @Override
    255     public void onFragmentChange(Fragment newFragment, boolean movingForward) {
    256         updateView(newFragment, movingForward);
    257     }
    258 
    259     @Override
    260     public int getMetricsCategory() {
    261         return MetricsEvent.SETTINGS_TV_WIFI_ADD_KNOWN_CATEGORY;
    262     }
    263 }
    264