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.content.Context;
20 import android.content.Intent;
21 import android.net.wifi.WifiConfiguration;
22 import android.os.Bundle;
23 
24 import androidx.fragment.app.Fragment;
25 import androidx.fragment.app.FragmentTransaction;
26 import androidx.lifecycle.ViewModelProviders;
27 
28 import com.android.internal.logging.nano.MetricsProto;
29 import com.android.tv.settings.R;
30 import com.android.tv.settings.connectivity.setup.AdvancedWifiOptionsFlow;
31 import com.android.tv.settings.connectivity.util.State;
32 import com.android.tv.settings.connectivity.util.StateMachine;
33 import com.android.tv.settings.core.instrumentation.InstrumentedActivity;
34 
35 /**
36  * Allows the modification of advanced Wi-Fi settings
37  */
38 public class EditProxySettingsActivity extends InstrumentedActivity implements
39         State.FragmentChangeListener {
40 
41     private static final String TAG = "EditProxySettings";
42 
43     private static final int NETWORK_ID_ETHERNET = WifiConfiguration.INVALID_NETWORK_ID;
44     private static final String EXTRA_NETWORK_ID = "network_id";
45 
createIntent(Context context, int networkId)46     public static Intent createIntent(Context context, int networkId) {
47         return new Intent(context, EditProxySettingsActivity.class)
48                 .putExtra(EXTRA_NETWORK_ID, networkId);
49     }
50 
51     private State mSaveState;
52     private State mSaveSuccessState;
53     private State mSaveFailedState;
54     private StateMachine mStateMachine;
55     private final StateMachine.Callback mStateMachineCallback = result -> {
56         setResult(result);
57         finish();
58     };
59 
60     @Override
onCreate(Bundle savedInstanceState)61     protected void onCreate(Bundle savedInstanceState) {
62         super.onCreate(savedInstanceState);
63         setContentView(R.layout.wifi_container);
64         mStateMachine = ViewModelProviders.of(this).get(StateMachine.class);
65         mStateMachine.setCallback(mStateMachineCallback);
66         mSaveState = new SaveState(this);
67         mSaveSuccessState = new SaveSuccessState(this);
68         mSaveFailedState = new SaveFailedState(this);
69         int networkId = getIntent().getIntExtra(EXTRA_NETWORK_ID, NETWORK_ID_ETHERNET);
70         NetworkConfiguration netConfig;
71         if (networkId == NETWORK_ID_ETHERNET) {
72             netConfig = new EthernetConfig(this);
73             ((EthernetConfig) netConfig).load();
74         } else {
75             netConfig = new WifiConfig(this);
76             ((WifiConfig) netConfig).load(networkId);
77         }
78         EditSettingsInfo editSettingsInfo = ViewModelProviders.of(this).get(EditSettingsInfo.class);
79         editSettingsInfo.setNetworkConfiguration(netConfig);
80         AdvancedWifiOptionsFlow.createFlow(this, false, true, netConfig,
81                 null, mSaveState, AdvancedWifiOptionsFlow.START_PROXY_SETTINGS_PAGE);
82 
83         /* Save */
84         mStateMachine.addState(
85                 mSaveState,
86                 StateMachine.RESULT_SUCCESS,
87                 mSaveSuccessState
88         );
89         mStateMachine.addState(
90                 mSaveState,
91                 StateMachine.RESULT_FAILURE,
92                 mSaveFailedState
93         );
94         mStateMachine.start(true);
95     }
96 
97     @Override
getMetricsCategory()98     public int getMetricsCategory() {
99         return MetricsProto.MetricsEvent.DIALOG_WIFI_AP_EDIT;
100     }
101 
102     @Override
onBackPressed()103     public void onBackPressed() {
104         mStateMachine.back();
105     }
106 
updateView(Fragment fragment, boolean movingForward)107     private void updateView(Fragment fragment, boolean movingForward) {
108         if (fragment != null) {
109             FragmentTransaction updateTransaction = getSupportFragmentManager().beginTransaction();
110             if (movingForward) {
111                 updateTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
112             } else {
113                 updateTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
114             }
115             updateTransaction.replace(R.id.wifi_container, fragment, TAG);
116             updateTransaction.commit();
117         }
118     }
119 
120     @Override
onFragmentChange(Fragment newFragment, boolean movingForward)121     public void onFragmentChange(Fragment newFragment, boolean movingForward) {
122         updateView(newFragment, movingForward);
123     }
124 }
125