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; 28 import com.android.tv.settings.R; 29 import com.android.tv.settings.connectivity.setup.AdvancedWifiOptionsFlow; 30 import com.android.tv.settings.connectivity.util.State; 31 import com.android.tv.settings.connectivity.util.StateMachine; 32 import com.android.tv.settings.core.instrumentation.InstrumentedActivity; 33 34 /** 35 * Allows the modification of advanced Wi-Fi settings 36 */ 37 public class EditProxySettingsActivity extends InstrumentedActivity implements 38 State.FragmentChangeListener { 39 40 private static final String TAG = "EditProxySettings"; 41 42 private static final int NETWORK_ID_ETHERNET = WifiConfiguration.INVALID_NETWORK_ID; 43 private static final String EXTRA_NETWORK_ID = "network_id"; 44 45 public static Intent createIntent(Context context, int networkId) { 46 return new Intent(context, EditProxySettingsActivity.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 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 = ViewModelProviders.of(this).get(EditSettingsInfo.class); 78 editSettingsInfo.setNetworkConfiguration(netConfig); 79 AdvancedWifiOptionsFlow.createFlow(this, false, true, netConfig, 80 null, mSaveState, AdvancedWifiOptionsFlow.START_PROXY_SETTINGS_PAGE); 81 82 /* Save */ 83 mStateMachine.addState( 84 mSaveState, 85 StateMachine.RESULT_SUCCESS, 86 mSaveSuccessState 87 ); 88 mStateMachine.addState( 89 mSaveState, 90 StateMachine.RESULT_FAILURE, 91 mSaveFailedState 92 ); 93 mStateMachine.start(true); 94 } 95 96 @Override 97 public int getMetricsCategory() { 98 return MetricsProto.MetricsEvent.DIALOG_WIFI_AP_EDIT; 99 } 100 101 @Override 102 public void onBackPressed() { 103 mStateMachine.back(); 104 } 105 106 private void updateView(Fragment fragment, boolean movingForward) { 107 if (fragment != null) { 108 FragmentTransaction updateTransaction = getSupportFragmentManager().beginTransaction(); 109 if (movingForward) { 110 updateTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 111 } else { 112 updateTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE); 113 } 114 updateTransaction.replace(R.id.wifi_container, fragment, TAG); 115 updateTransaction.commit(); 116 } 117 } 118 119 @Override 120 public void onFragmentChange(Fragment newFragment, boolean movingForward) { 121 updateView(newFragment, movingForward); 122 } 123 } 124