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.os.Bundle;
20 
21 import androidx.fragment.app.Fragment;
22 import androidx.fragment.app.FragmentTransaction;
23 import androidx.lifecycle.ViewModelProviders;
24 
25 import com.android.internal.logging.nano.MetricsProto;
26 import com.android.tv.settings.R;
27 import com.android.tv.settings.connectivity.setup.AdvancedWifiOptionsFlow;
28 import com.android.tv.settings.connectivity.setup.ChooseSecurityState;
29 import com.android.tv.settings.connectivity.setup.ConnectAuthFailureState;
30 import com.android.tv.settings.connectivity.setup.ConnectFailedState;
31 import com.android.tv.settings.connectivity.setup.ConnectRejectedByApState;
32 import com.android.tv.settings.connectivity.setup.ConnectState;
33 import com.android.tv.settings.connectivity.setup.ConnectTimeOutState;
34 import com.android.tv.settings.connectivity.setup.EnterPasswordState;
35 import com.android.tv.settings.connectivity.setup.EnterSsidState;
36 import com.android.tv.settings.connectivity.setup.OptionsOrConnectState;
37 import com.android.tv.settings.connectivity.setup.SuccessState;
38 import com.android.tv.settings.connectivity.setup.UserChoiceInfo;
39 import com.android.tv.settings.connectivity.util.State;
40 import com.android.tv.settings.connectivity.util.StateMachine;
41 import com.android.tv.settings.core.instrumentation.InstrumentedActivity;
42 
43 /**
44  * Manual-style add wifi network (the kind you'd use for adding a hidden or out-of-range network.)
45  */
46 public class AddWifiNetworkActivity extends InstrumentedActivity
47             implements State.FragmentChangeListener {
48     private static final String TAG = "AddWifiNetworkActivity";
49     private State mChooseSecurityState;
50     private State mConnectAuthFailureState;
51     private State mConnectFailedState;
52     private State mConnectRejectedByApState;
53     private State mConnectState;
54     private State mConnectTimeOutState;
55     private State mEnterPasswordState;
56     private State mEnterSsidState;
57     private State mSuccessState;
58     private State mOptionsOrConnectState;
59     private State mEnterAdvancedFlowOrRetryState;
60     private State mFinishState;
61     private StateMachine mStateMachine;
62     private final StateMachine.Callback mStateMachineCallback = new StateMachine.Callback() {
63         @Override
64         public void onFinish(int result) {
65             setResult(result);
66             finish();
67         }
68     };
69 
70     @Override
onCreate(Bundle savedInstanceState)71     protected void onCreate(Bundle savedInstanceState) {
72         super.onCreate(savedInstanceState);
73         setContentView(R.layout.wifi_container);
74         mStateMachine = ViewModelProviders.of(this).get(StateMachine.class);
75         mStateMachine.setCallback(mStateMachineCallback);
76         UserChoiceInfo userChoiceInfo = ViewModelProviders.of(this).get(UserChoiceInfo.class);
77         userChoiceInfo.getWifiConfiguration().hiddenSSID = true;
78 
79         mEnterSsidState = new EnterSsidState(this);
80         mChooseSecurityState = new ChooseSecurityState(this);
81         mEnterPasswordState = new EnterPasswordState(this);
82         mConnectState = new ConnectState(this);
83         mSuccessState = new SuccessState(this);
84         mOptionsOrConnectState = new OptionsOrConnectState(this);
85         mConnectTimeOutState = new ConnectTimeOutState(this);
86         mConnectRejectedByApState = new ConnectRejectedByApState(this);
87         mConnectFailedState = new ConnectFailedState(this);
88         mConnectAuthFailureState = new ConnectAuthFailureState(this);
89         mFinishState = new FinishState(this);
90         mEnterAdvancedFlowOrRetryState = new EnterAdvancedFlowOrRetryState(this);
91 
92         AdvancedWifiOptionsFlow.createFlow(
93                 this, true, true, null, mOptionsOrConnectState,
94                 mConnectState, AdvancedWifiOptionsFlow.START_DEFAULT_PAGE);
95         AdvancedWifiOptionsFlow.createFlow(
96                 this, true, true, null, mEnterAdvancedFlowOrRetryState,
97                 mConnectState, AdvancedWifiOptionsFlow.START_DEFAULT_PAGE);
98 
99         /* Enter SSID */
100         mStateMachine.addState(
101                 mEnterSsidState,
102                 StateMachine.CONTINUE,
103                 mChooseSecurityState
104         );
105 
106         /* Choose security */
107         mStateMachine.addState(
108                 mChooseSecurityState,
109                 StateMachine.OPTIONS_OR_CONNECT,
110                 mOptionsOrConnectState
111         );
112         mStateMachine.addState(
113                 mChooseSecurityState,
114                 StateMachine.PASSWORD,
115                 mEnterPasswordState
116         );
117 
118         /* Enter Password */
119         mStateMachine.addState(
120                 mEnterPasswordState,
121                 StateMachine.OPTIONS_OR_CONNECT,
122                 mOptionsOrConnectState
123         );
124 
125         /* Options or Connect */
126         mStateMachine.addState(
127                 mOptionsOrConnectState,
128                 StateMachine.CONNECT,
129                 mConnectState
130         );
131 
132         /* Connect */
133         mStateMachine.addState(
134                 mConnectState,
135                 StateMachine.RESULT_REJECTED_BY_AP,
136                 mConnectRejectedByApState);
137         mStateMachine.addState(
138                 mConnectState,
139                 StateMachine.RESULT_UNKNOWN_ERROR,
140                 mConnectFailedState);
141         mStateMachine.addState(
142                 mConnectState,
143                 StateMachine.RESULT_TIMEOUT,
144                 mConnectTimeOutState);
145         mStateMachine.addState(
146                 mConnectState,
147                 StateMachine.RESULT_BAD_AUTH,
148                 mConnectAuthFailureState);
149         mStateMachine.addState(
150                 mConnectState,
151                 StateMachine.RESULT_SUCCESS,
152                 mSuccessState);
153 
154         /* Connect Failed */
155         mStateMachine.addState(
156                 mConnectFailedState,
157                 StateMachine.TRY_AGAIN,
158                 mOptionsOrConnectState
159         );
160 
161         mStateMachine.addState(
162                 mConnectFailedState,
163                 StateMachine.SELECT_WIFI,
164                 mFinishState
165         );
166 
167         /* Connect Timeout */
168         mStateMachine.addState(
169                 mConnectTimeOutState,
170                 StateMachine.TRY_AGAIN,
171                 mOptionsOrConnectState
172         );
173         mStateMachine.addState(
174                 mConnectTimeOutState,
175                 StateMachine.SELECT_WIFI,
176                 mFinishState
177         );
178 
179         /* Connect Rejected By AP */
180         mStateMachine.addState(
181                 mConnectRejectedByApState,
182                 StateMachine.TRY_AGAIN,
183                 mOptionsOrConnectState);
184         mStateMachine.addState(
185                 mConnectRejectedByApState,
186                 StateMachine.SELECT_WIFI,
187                 mFinishState);
188 
189         /* Connect Auth Failure */
190         mStateMachine.addState(
191                 mConnectAuthFailureState,
192                 StateMachine.TRY_AGAIN,
193                 mEnterAdvancedFlowOrRetryState);
194         mStateMachine.addState(
195                 mConnectRejectedByApState,
196                 StateMachine.SELECT_WIFI,
197                 mFinishState);
198 
199         /* Enter Advanced Flow or Retry */
200         mStateMachine.addState(
201                 mEnterAdvancedFlowOrRetryState,
202                 StateMachine.CONTINUE,
203                 mEnterSsidState);
204         mStateMachine.setStartState(mEnterSsidState);
205         mStateMachine.start(true);
206     }
207 
208     @Override
getMetricsCategory()209     public int getMetricsCategory() {
210         // do not log visibility.
211         return MetricsProto.MetricsEvent.ACTION_WIFI_ADD_NETWORK;
212     }
213 
214     @Override
onBackPressed()215     public void onBackPressed() {
216         mStateMachine.back();
217     }
218 
updateView(Fragment fragment, boolean movingForward)219     private void updateView(Fragment fragment, boolean movingForward) {
220         if (fragment != null) {
221             FragmentTransaction updateTransaction = getSupportFragmentManager().beginTransaction();
222             if (movingForward) {
223                 updateTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
224             } else {
225                 updateTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
226             }
227             updateTransaction.replace(R.id.wifi_container, fragment, TAG);
228             updateTransaction.commit();
229         }
230     }
231 
232     @Override
onFragmentChange(Fragment newFragment, boolean movingForward)233     public void onFragmentChange(Fragment newFragment, boolean movingForward) {
234         updateView(newFragment, movingForward);
235     }
236 }
237