165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/*
265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Copyright (C) 2014 The Android Open Source Project
365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Licensed under the Apache License, Version 2.0 (the "License");
565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * you may not use this file except in compliance with the License.
665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * You may obtain a copy of the License at
765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *      http://www.apache.org/licenses/LICENSE-2.0
965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
1065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Unless required by applicable law or agreed to in writing, software
1165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * distributed under the License is distributed on an "AS IS" BASIS,
1265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * See the License for the specific language governing permissions and
1465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * limitations under the License.
1565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */
1665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
1765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepackage com.android.tv.settings.connectivity;
1865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
1965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport com.android.tv.settings.R;
2065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport com.android.tv.settings.connectivity.setup.SelectFromListWizardFragment;
2165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport com.android.tv.settings.connectivity.setup.SelectFromListWizardFragment.ListItem;
2265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport com.android.tv.settings.form.FormPage;
2365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport com.android.tv.settings.form.FormPageResultListener;
2465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
2565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.app.Fragment;
2665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.content.Context;
2765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.content.Intent;
2865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.net.wifi.WifiConfiguration;
2965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.net.wifi.WifiInfo;
3065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.net.wifi.WifiManager;
3165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.os.Bundle;
3265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.os.Parcel;
3365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.text.TextUtils;
3465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.util.Base64;
3565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.util.Log;
3665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
3765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport java.net.InetAddress;
3865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport java.util.ArrayList;
3965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
4065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/**
4165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Allows the modification of advanced Wi-Fi settings
4265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */
4365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepublic class EditProxySettingsActivity extends WifiMultiPagedFormActivity
4465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        implements SaveWifiConfigurationFragment.Listener, TimedMessageWizardFragment.Listener {
4565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
4665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String TAG = "EditProxySettingsActivity";
4765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
4865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static final int NETWORK_ID_ETHERNET = WifiConfiguration.INVALID_NETWORK_ID;
4965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String EXTRA_NETWORK_ID = "network_id";
5065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
5165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static Intent createIntent(Context context, int networkId) {
5265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return new Intent(context, EditProxySettingsActivity.class)
5365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                .putExtra(EXTRA_NETWORK_ID, networkId);
5465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
5565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
5665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private NetworkConfiguration mConfiguration;
5765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private AdvancedWifiOptionsFlow mAdvancedWifiOptionsFlow;
5865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private FormPage mSavePage;
5965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private FormPage mSuccessPage;
6065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
6165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    @Override
6265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    protected void onCreate(Bundle savedInstanceState) {
6365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        int networkId = getIntent().getIntExtra(EXTRA_NETWORK_ID, -1);
6465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (networkId == NETWORK_ID_ETHERNET) {
6565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mConfiguration = NetworkConfigurationFactory.createNetworkConfiguration(this,
6665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    NetworkConfigurationFactory.TYPE_ETHERNET);
6765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            ((EthernetConfig) mConfiguration).load();
6865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else {
6965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mConfiguration = NetworkConfigurationFactory.createNetworkConfiguration(this,
7065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    NetworkConfigurationFactory.TYPE_WIFI);
7165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            ((WifiConfig) mConfiguration).load(networkId);
7265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
7365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (mConfiguration != null) {
7465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mAdvancedWifiOptionsFlow = new AdvancedWifiOptionsFlow(this, this, mConfiguration);
7565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            addPage(mAdvancedWifiOptionsFlow.getInitialProxySettingsPage());
7665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else {
7765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            Log.e(TAG, "Could not find existing configuration for network id: " + networkId);
7865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
7965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        super.onCreate(savedInstanceState);
8065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
8165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
8265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    @Override
8365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public void onSaveWifiConfigurationCompleted(int reason) {
8465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        Bundle result = new Bundle();
8565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        result.putString(FormPage.DATA_KEY_SUMMARY_STRING, Integer.toString(reason));
8665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        onBundlePageResult(mSavePage, result);
8765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
8865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
8965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    @Override
9065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public void onTimedMessageCompleted() {
9165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        Bundle result = new Bundle();
9265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        result.putString(FormPage.DATA_KEY_SUMMARY_STRING, "");
9365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        onBundlePageResult(mSuccessPage, result);
9465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
9565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
9665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    @Override
9765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    protected boolean onPageComplete(WifiFormPageType formPageType, FormPage formPage) {
9865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
9965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        switch(formPageType) {
10065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            case SAVE:
10165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                switch (Integer.valueOf(formPage.getDataSummary())) {
10265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    case SaveWifiConfigurationFragment.RESULT_FAILURE:
10365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        addPage(WifiFormPageType.SAVE_FAILED);
10465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        break;
10565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    case SaveWifiConfigurationFragment.RESULT_SUCCESS:
10665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        addPage(WifiFormPageType.SAVE_SUCCESS);
10765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        break;
10865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    default:
10965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        break;
11065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
11165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                break;
11265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            case SAVE_FAILED:
11365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                break;
11465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            case SAVE_SUCCESS:
11565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                break;
11665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            default:
11765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                switch (mAdvancedWifiOptionsFlow.handlePageComplete(formPageType, formPage)) {
11865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    case AdvancedWifiOptionsFlow.RESULT_UNKNOWN_PAGE:
11965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        break;
12065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    case AdvancedWifiOptionsFlow.RESULT_PAGE_HANDLED:
12165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        break;
12265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    case AdvancedWifiOptionsFlow.RESULT_ALL_PAGES_COMPLETE:
12365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        save();
12465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        break;
12565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    default:
12665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        break;
12765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
12865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                break;
12965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
13065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return true;
13165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
13265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
13365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    @Override
13465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    protected void displayPage(FormPage formPage, FormPageResultListener listener,
13565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            boolean forward) {
13665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        WifiFormPageType formPageType = getFormPageType(formPage);
13765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (formPageType == WifiFormPageType.SAVE) {
13865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mSavePage = formPage;
13965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            Fragment fragment = SaveWifiConfigurationFragment.newInstance(
14065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    getString(formPageType.getTitleResourceId(), mConfiguration.getPrintableName()),
14165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    mConfiguration);
14265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            displayFragment(fragment, forward);
14365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else if (formPageType == WifiFormPageType.SAVE_SUCCESS) {
14465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mSuccessPage = formPage;
14565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            Fragment fragment = TimedMessageWizardFragment.newInstance(
14665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    getString(formPageType.getTitleResourceId()));
14765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            displayFragment(fragment, forward);
14865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else {
14965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            displayPage(formPageType, mConfiguration.getPrintableName(), null, null,
15065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    mAdvancedWifiOptionsFlow.getPreviousPage(formPageType), null,
15165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    formPageType != WifiFormPageType.SAVE_SUCCESS, formPage, listener, forward,
15265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    mAdvancedWifiOptionsFlow.isEmptyTextAllowed(formPageType));
15365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
15465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
15565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
15665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private FormPage getPreviousPage(WifiFormPageType formPageType) {
15765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return mAdvancedWifiOptionsFlow.getPreviousPage(formPageType);
15865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
15965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
16065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private void save() {
16165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mAdvancedWifiOptionsFlow.updateConfiguration(mConfiguration);
16265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        addPage(WifiFormPageType.SAVE);
16365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
16465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane}
165