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 com.android.tv.settings.R; 20 import com.android.tv.settings.connectivity.setup.SelectFromListWizardFragment; 21 import com.android.tv.settings.connectivity.setup.SelectFromListWizardFragment.ListItem; 22 import com.android.tv.settings.form.FormPage; 23 import com.android.tv.settings.form.FormPageResultListener; 24 25 import android.app.Fragment; 26 import android.content.Context; 27 import android.content.Intent; 28 import android.net.wifi.WifiConfiguration; 29 import android.net.wifi.WifiInfo; 30 import android.net.wifi.WifiManager; 31 import android.os.Bundle; 32 import android.os.Parcel; 33 import android.text.TextUtils; 34 import android.util.Base64; 35 import android.util.Log; 36 37 import java.net.InetAddress; 38 import java.util.ArrayList; 39 40 /** 41 * Allows the modification of advanced Wi-Fi settings 42 */ 43 public class EditIpSettingsActivity extends WifiMultiPagedFormActivity 44 implements SaveWifiConfigurationFragment.Listener, TimedMessageWizardFragment.Listener { 45 46 private static final String TAG = "EditIpSettingsActivity"; 47 48 public static final int NETWORK_ID_ETHERNET = WifiConfiguration.INVALID_NETWORK_ID; 49 private static final String EXTRA_NETWORK_ID = "network_id"; 50 51 public static Intent createIntent(Context context, int networkId) { 52 return new Intent(context, EditIpSettingsActivity.class) 53 .putExtra(EXTRA_NETWORK_ID, networkId); 54 } 55 56 private NetworkConfiguration mConfiguration; 57 private AdvancedWifiOptionsFlow mAdvancedWifiOptionsFlow; 58 private FormPage mSavePage; 59 private FormPage mSuccessPage; 60 61 @Override 62 protected void onCreate(Bundle savedInstanceState) { 63 int networkId = getIntent().getIntExtra(EXTRA_NETWORK_ID, NETWORK_ID_ETHERNET); 64 if (networkId == NETWORK_ID_ETHERNET) { 65 mConfiguration = NetworkConfigurationFactory.createNetworkConfiguration(this, 66 NetworkConfigurationFactory.TYPE_ETHERNET); 67 ((EthernetConfig) mConfiguration).load(); 68 } else { 69 mConfiguration = NetworkConfigurationFactory.createNetworkConfiguration(this, 70 NetworkConfigurationFactory.TYPE_WIFI); 71 ((WifiConfig) mConfiguration).load(networkId); 72 } 73 if (mConfiguration != null) { 74 mAdvancedWifiOptionsFlow = new AdvancedWifiOptionsFlow(this, this, mConfiguration); 75 addPage(mAdvancedWifiOptionsFlow.getInitialIpSettingsPage()); 76 } else { 77 Log.e(TAG, "Could not find existing configuration for network id: " + networkId); 78 } 79 super.onCreate(savedInstanceState); 80 } 81 82 @Override 83 public void onSaveWifiConfigurationCompleted(int reason) { 84 Bundle result = new Bundle(); 85 result.putString(FormPage.DATA_KEY_SUMMARY_STRING, Integer.toString(reason)); 86 onBundlePageResult(mSavePage, result); 87 } 88 89 @Override 90 public void onTimedMessageCompleted() { 91 Bundle result = new Bundle(); 92 result.putString(FormPage.DATA_KEY_SUMMARY_STRING, ""); 93 onBundlePageResult(mSuccessPage, result); 94 } 95 96 private void addResultWifiFormPage(int dataSummary) { 97 switch (dataSummary) { 98 case SaveWifiConfigurationFragment.RESULT_FAILURE: 99 addPage(WifiFormPageType.SAVE_FAILED); 100 break; 101 case SaveWifiConfigurationFragment.RESULT_SUCCESS: 102 addPage(WifiFormPageType.SAVE_SUCCESS); 103 break; 104 default: 105 break; 106 } 107 } 108 109 @Override 110 protected boolean onPageComplete(WifiFormPageType formPageType, FormPage formPage) { 111 switch(formPageType) { 112 case SAVE: 113 addResultWifiFormPage(Integer.valueOf(formPage.getDataSummary())); 114 break; 115 case SAVE_FAILED: 116 case SAVE_SUCCESS: 117 break; 118 default: 119 if (mAdvancedWifiOptionsFlow.handlePageComplete(formPageType, formPage) == 120 AdvancedWifiOptionsFlow.RESULT_ALL_PAGES_COMPLETE) { 121 save(); 122 } 123 break; 124 } 125 return true; 126 } 127 128 @Override 129 protected void displayPage(FormPage formPage, FormPageResultListener listener, 130 boolean forward) { 131 WifiFormPageType formPageType = getFormPageType(formPage); 132 if (formPageType == WifiFormPageType.SAVE) { 133 mSavePage = formPage; 134 Fragment fragment = SaveWifiConfigurationFragment.newInstance( 135 getString(formPageType.getTitleResourceId(), mConfiguration.getPrintableName()), 136 mConfiguration); 137 displayFragment(fragment, forward); 138 } else if (formPageType == WifiFormPageType.SAVE_SUCCESS) { 139 mSuccessPage = formPage; 140 Fragment fragment = TimedMessageWizardFragment.newInstance( 141 getString(formPageType.getTitleResourceId())); 142 displayFragment(fragment, forward); 143 } else { 144 displayPage(formPageType, mConfiguration.getPrintableName(), null, null, 145 mAdvancedWifiOptionsFlow.getPreviousPage(formPageType), null, 146 formPageType != WifiFormPageType.SAVE_SUCCESS, formPage, listener, forward, 147 mAdvancedWifiOptionsFlow.isEmptyTextAllowed(formPageType)); 148 } 149 } 150 151 private FormPage getPreviousPage(WifiFormPageType formPageType) { 152 return mAdvancedWifiOptionsFlow.getPreviousPage(formPageType); 153 } 154 155 private void save() { 156 mAdvancedWifiOptionsFlow.updateConfiguration(mConfiguration); 157 addPage(WifiFormPageType.SAVE); 158 } 159 } 160