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