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
17package com.android.tv.settings.connectivity;
18
19import com.android.tv.settings.R;
20import com.android.tv.settings.connectivity.setup.SelectFromListWizardFragment;
21import com.android.tv.settings.connectivity.setup.SelectFromListWizardFragment.ListItem;
22import com.android.tv.settings.form.FormPage;
23import com.android.tv.settings.form.FormPageResultListener;
24
25import android.app.Fragment;
26import android.content.Context;
27import android.content.Intent;
28import android.net.wifi.WifiConfiguration;
29import android.net.wifi.WifiInfo;
30import android.net.wifi.WifiManager;
31import android.os.Bundle;
32import android.os.Parcel;
33import android.text.TextUtils;
34import android.util.Base64;
35import android.util.Log;
36
37import java.net.InetAddress;
38import java.util.ArrayList;
39
40/**
41 * Allows the modification of advanced Wi-Fi settings
42 */
43public class EditProxySettingsActivity extends WifiMultiPagedFormActivity
44        implements SaveWifiConfigurationFragment.Listener, TimedMessageWizardFragment.Listener {
45
46    private static final String TAG = "EditProxySettingsActivity";
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, EditProxySettingsActivity.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, -1);
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.getInitialProxySettingsPage());
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    @Override
97    protected boolean onPageComplete(WifiFormPageType formPageType, FormPage formPage) {
98
99        switch(formPageType) {
100            case SAVE:
101                switch (Integer.valueOf(formPage.getDataSummary())) {
102                    case SaveWifiConfigurationFragment.RESULT_FAILURE:
103                        addPage(WifiFormPageType.SAVE_FAILED);
104                        break;
105                    case SaveWifiConfigurationFragment.RESULT_SUCCESS:
106                        addPage(WifiFormPageType.SAVE_SUCCESS);
107                        break;
108                    default:
109                        break;
110                }
111                break;
112            case SAVE_FAILED:
113                break;
114            case SAVE_SUCCESS:
115                break;
116            default:
117                switch (mAdvancedWifiOptionsFlow.handlePageComplete(formPageType, formPage)) {
118                    case AdvancedWifiOptionsFlow.RESULT_UNKNOWN_PAGE:
119                        break;
120                    case AdvancedWifiOptionsFlow.RESULT_PAGE_HANDLED:
121                        break;
122                    case AdvancedWifiOptionsFlow.RESULT_ALL_PAGES_COMPLETE:
123                        save();
124                        break;
125                    default:
126                        break;
127                }
128                break;
129        }
130        return true;
131    }
132
133    @Override
134    protected void displayPage(FormPage formPage, FormPageResultListener listener,
135            boolean forward) {
136        WifiFormPageType formPageType = getFormPageType(formPage);
137        if (formPageType == WifiFormPageType.SAVE) {
138            mSavePage = formPage;
139            Fragment fragment = SaveWifiConfigurationFragment.newInstance(
140                    getString(formPageType.getTitleResourceId(), mConfiguration.getPrintableName()),
141                    mConfiguration);
142            displayFragment(fragment, forward);
143        } else if (formPageType == WifiFormPageType.SAVE_SUCCESS) {
144            mSuccessPage = formPage;
145            Fragment fragment = TimedMessageWizardFragment.newInstance(
146                    getString(formPageType.getTitleResourceId()));
147            displayFragment(fragment, forward);
148        } else {
149            displayPage(formPageType, mConfiguration.getPrintableName(), null, null,
150                    mAdvancedWifiOptionsFlow.getPreviousPage(formPageType), null,
151                    formPageType != WifiFormPageType.SAVE_SUCCESS, formPage, listener, forward,
152                    mAdvancedWifiOptionsFlow.isEmptyTextAllowed(formPageType));
153        }
154    }
155
156    private FormPage getPreviousPage(WifiFormPageType formPageType) {
157        return mAdvancedWifiOptionsFlow.getPreviousPage(formPageType);
158    }
159
160    private void save() {
161        mAdvancedWifiOptionsFlow.updateConfiguration(mConfiguration);
162        addPage(WifiFormPageType.SAVE);
163    }
164}
165