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 EditProxySettingsActivity extends WifiMultiPagedFormActivity
33         implements SaveWifiConfigurationFragment.Listener, TimedMessageWizardFragment.Listener {
34 
35     private static final String TAG = "EditProxySettings";
36 
37     public static final int NETWORK_ID_ETHERNET = WifiConfiguration.INVALID_NETWORK_ID;
38     private static final String EXTRA_NETWORK_ID = "network_id";
39 
createIntent(Context context, int networkId)40     public static Intent createIntent(Context context, int networkId) {
41         return new Intent(context, EditProxySettingsActivity.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
onCreate(Bundle savedInstanceState)51     protected void onCreate(Bundle savedInstanceState) {
52         int networkId = getIntent().getIntExtra(EXTRA_NETWORK_ID, -1);
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.getInitialProxySettingsPage());
65         } else {
66             Log.e(TAG, "Could not find existing configuration for network id: " + networkId);
67         }
68         super.onCreate(savedInstanceState);
69     }
70 
71     @Override
onSaveWifiConfigurationCompleted(int reason)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
onTimedMessageCompleted()79     public void onTimedMessageCompleted() {
80         Bundle result = new Bundle();
81         result.putString(FormPage.DATA_KEY_SUMMARY_STRING, "");
82         onBundlePageResult(mSuccessPage, result);
83     }
84 
85     @Override
onPageComplete(WifiFormPageType formPageType, FormPage formPage)86     protected boolean onPageComplete(WifiFormPageType formPageType, FormPage formPage) {
87 
88         switch(formPageType) {
89             case SAVE:
90                 switch (Integer.valueOf(formPage.getDataSummary())) {
91                     case SaveWifiConfigurationFragment.RESULT_FAILURE:
92                         addPage(WifiFormPageType.SAVE_FAILED);
93                         break;
94                     case SaveWifiConfigurationFragment.RESULT_SUCCESS:
95                         addPage(WifiFormPageType.SAVE_SUCCESS);
96                         break;
97                     default:
98                         break;
99                 }
100                 break;
101             case SAVE_FAILED:
102                 break;
103             case SAVE_SUCCESS:
104                 break;
105             default:
106                 switch (mAdvancedWifiOptionsFlow.handlePageComplete(formPageType, formPage)) {
107                     case AdvancedWifiOptionsFlow.RESULT_UNKNOWN_PAGE:
108                         break;
109                     case AdvancedWifiOptionsFlow.RESULT_PAGE_HANDLED:
110                         break;
111                     case AdvancedWifiOptionsFlow.RESULT_ALL_PAGES_COMPLETE:
112                         save();
113                         break;
114                     default:
115                         break;
116                 }
117                 break;
118         }
119         return true;
120     }
121 
122     @Override
displayPage(FormPage formPage, FormPageResultListener listener, boolean forward)123     protected void displayPage(FormPage formPage, FormPageResultListener listener,
124             boolean forward) {
125         WifiFormPageType formPageType = getFormPageType(formPage);
126         if (formPageType == WifiFormPageType.SAVE) {
127             mSavePage = formPage;
128             Fragment fragment = SaveWifiConfigurationFragment.newInstance(
129                     getString(formPageType.getTitleResourceId(), mConfiguration.getPrintableName()),
130                     mConfiguration);
131             displayFragment(fragment, forward);
132         } else if (formPageType == WifiFormPageType.SAVE_SUCCESS) {
133             mSuccessPage = formPage;
134             Fragment fragment = TimedMessageWizardFragment.newInstance(
135                     getString(formPageType.getTitleResourceId()));
136             displayFragment(fragment, forward);
137         } else {
138             displayPage(formPageType, mConfiguration.getPrintableName(), null, null,
139                     mAdvancedWifiOptionsFlow.getPreviousPage(formPageType), null,
140                     formPageType != WifiFormPageType.SAVE_SUCCESS, formPage, listener, forward,
141                     mAdvancedWifiOptionsFlow.isEmptyTextAllowed(formPageType));
142         }
143     }
144 
getPreviousPage(WifiFormPageType formPageType)145     private FormPage getPreviousPage(WifiFormPageType formPageType) {
146         return mAdvancedWifiOptionsFlow.getPreviousPage(formPageType);
147     }
148 
save()149     private void save() {
150         mAdvancedWifiOptionsFlow.updateConfiguration(mConfiguration);
151         addPage(WifiFormPageType.SAVE);
152     }
153 }
154