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 EditIpSettingsActivity extends InstrumentedActivity implements
39         State.FragmentChangeListener {
40     private static final String TAG = "EditIpSettingsActivity";
41 
42     private static final int NETWORK_ID_ETHERNET = WifiConfiguration.INVALID_NETWORK_ID;
43     private static final String EXTRA_NETWORK_ID = "network_id";
44 
createIntent(Context context, int networkId)45     public static Intent createIntent(Context context, int networkId) {
46         return new Intent(context, EditIpSettingsActivity.class)
47                 .putExtra(EXTRA_NETWORK_ID, networkId);
48     }
49 
50     private State mSaveState;
51     private State mSaveSuccessState;
52     private State mSaveFailedState;
53     private StateMachine mStateMachine;
54     private final StateMachine.Callback mStateMachineCallback = result -> {
55         setResult(result);
56         finish();
57     };
58 
59     @Override
onCreate(Bundle savedInstanceState)60     protected void onCreate(Bundle savedInstanceState) {
61         super.onCreate(savedInstanceState);
62         setContentView(R.layout.wifi_container);
63         mStateMachine = ViewModelProviders.of(this).get(StateMachine.class);
64         mStateMachine.setCallback(mStateMachineCallback);
65         mSaveState = new SaveState(this);
66         mSaveSuccessState = new SaveSuccessState(this);
67         mSaveFailedState = new SaveFailedState(this);
68         int networkId = getIntent().getIntExtra(EXTRA_NETWORK_ID, NETWORK_ID_ETHERNET);
69         NetworkConfiguration netConfig;
70         if (networkId == NETWORK_ID_ETHERNET) {
71             netConfig = new EthernetConfig(this);
72             ((EthernetConfig) netConfig).load();
73         } else {
74             netConfig = new WifiConfig(this);
75             ((WifiConfig) netConfig).load(networkId);
76         }
77         EditSettingsInfo editSettingsInfo =
78                     ViewModelProviders.of(this).get(EditSettingsInfo.class);
79         editSettingsInfo.setNetworkConfiguration(netConfig);
80         AdvancedWifiOptionsFlow.createFlow(this, false, true, netConfig,
81                 null, mSaveState, AdvancedWifiOptionsFlow.START_IP_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 
95         mStateMachine.start(true);
96     }
97 
98 
99 
100     @Override
getMetricsCategory()101     public int getMetricsCategory() {
102         return MetricsProto.MetricsEvent.DIALOG_WIFI_AP_EDIT;
103     }
104 
105     @Override
onBackPressed()106     public void onBackPressed() {
107         mStateMachine.back();
108     }
109 
updateView(Fragment fragment, boolean movingForward)110     private void updateView(Fragment fragment, boolean movingForward) {
111         if (fragment != null) {
112             FragmentTransaction updateTransaction = getSupportFragmentManager().beginTransaction();
113             if (movingForward) {
114                 updateTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
115             } else {
116                 updateTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
117             }
118             updateTransaction.replace(R.id.wifi_container, fragment, TAG);
119             updateTransaction.commit();
120         }
121     }
122 
123     @Override
onFragmentChange(Fragment newFragment, boolean movingForward)124     public void onFragmentChange(Fragment newFragment, boolean movingForward) {
125         updateView(newFragment, movingForward);
126     }
127 }
128