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.form.FormPage;
2165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
2265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.content.Context;
2365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.net.IpConfiguration.IpAssignment;
2465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.net.IpConfiguration.ProxySettings;
2565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.net.IpConfiguration;
2665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.net.LinkAddress;
2765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.net.NetworkUtils;
2865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.net.ProxyInfo;
2965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.net.RouteInfo;
3065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.net.StaticIpConfiguration;
3165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.net.wifi.WifiConfiguration;
3265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.os.Bundle;
3365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.text.TextUtils;
3465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
3565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport java.net.InetAddress;
3665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport java.net.Inet4Address;
3765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport java.util.Collection;
3865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport java.util.Iterator;
3965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
4065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/**
4165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Handles the form page flow of setting advanced options.
4265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */
4365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepublic class AdvancedWifiOptionsFlow {
4465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
4565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public interface PageHandler {
4665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        void addPage(WifiFormPageType formPageType);
4765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
4865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        void removePage(FormPage formPage);
4965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
5065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        boolean choiceChosen(FormPage formPage, int choiceResourceId);
5165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
5265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
5365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static final int RESULT_UNKNOWN_PAGE = 0;
5465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static final int RESULT_PAGE_HANDLED = 1;
5565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static final int RESULT_ALL_PAGES_COMPLETE = 2;
5665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
5765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private final Context mContext;
5865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private final PageHandler mPageHandler;
5965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private final boolean mAskFirst;
6065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private final NetworkConfiguration mInitialConfiguration;
6165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private FormPage mAdvancedOptionsPage;
6265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private FormPage mProxySettingsPage;
6365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private FormPage mIpSettingsPage;
6465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private FormPage mProxyHostnamePage;
6565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private FormPage mProxyPortPage;
6665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private FormPage mProxyBypassPage;
6765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private FormPage mIpAddressPage;
6865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private FormPage mGatewayPage;
6965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private FormPage mNetworkPrefixLengthPage;
7065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private FormPage mDns1Page;
7165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private FormPage mDns2Page;
7265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private IpConfiguration mIpConfiguration;
7365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private boolean mSettingsFlow;
7465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
7565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public AdvancedWifiOptionsFlow(Context context, PageHandler pageHandler,
7665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            NetworkConfiguration initialConfiguration) {
7765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mContext = context;
7865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mPageHandler = pageHandler;
7965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mAskFirst = false;
8065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mSettingsFlow = true;
8165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mInitialConfiguration = initialConfiguration;
8265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mIpConfiguration = (initialConfiguration != null) ?
8365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mInitialConfiguration.getIpConfiguration() :
8465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                new IpConfiguration();
8565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
8665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
8765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public AdvancedWifiOptionsFlow(Context context, PageHandler pageHandler, boolean askFirst,
8865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            NetworkConfiguration initialConfiguration) {
8965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mContext = context;
9065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mPageHandler = pageHandler;
9165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mAskFirst = askFirst;
9265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mSettingsFlow = false;
9365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mInitialConfiguration = initialConfiguration;
9465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mIpConfiguration = (initialConfiguration != null) ?
9565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mInitialConfiguration.getIpConfiguration() :
9665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                new IpConfiguration();
9765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
9865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
9965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public WifiFormPageType getInitialPage() {
10065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return (mAskFirst) ? WifiFormPageType.ADVANCED_OPTIONS : WifiFormPageType.PROXY_SETTINGS;
10165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
10265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
10365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public WifiFormPageType getInitialProxySettingsPage() {
10465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return WifiFormPageType.PROXY_SETTINGS;
10565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
10665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
10765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane   public WifiFormPageType getInitialIpSettingsPage() {
10865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return WifiFormPageType.IP_SETTINGS;
10965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
11065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
11165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    /**
11265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane     * @param formPageType the type of page that completed.
11365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane     * @param formPage the page that complete.
11465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane     * @return RESULT_PAGE_HANDLED if the page has been handled.
11565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane     *         RESULT_UNKNOWN_PAGE if the page is unrecognized and was ignored.
11665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane     *         RESULT_ALL_PAGES_COMPLETE if all pages have been completed.
11765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane     */
11865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public int handlePageComplete(WifiFormPageType formPageType, FormPage formPage) {
11965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
12065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        switch (formPageType) {
12165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            case ADVANCED_OPTIONS:
12265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mAdvancedOptionsPage = formPage;
12365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (mPageHandler.choiceChosen(formPage, R.string.wifi_action_advanced_no)) {
12465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    processProxySettings();
12565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    processIpSettings();
12665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    return RESULT_ALL_PAGES_COMPLETE;
12765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                } else {
12865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    mPageHandler.addPage(WifiFormPageType.PROXY_SETTINGS);
12965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
13065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                break;
13165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            case PROXY_SETTINGS:
13265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mProxySettingsPage = formPage;
13365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (mPageHandler.choiceChosen(formPage, R.string.wifi_action_proxy_none)) {
13465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    processProxySettings();
13565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    if (mSettingsFlow) {
13665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        return RESULT_ALL_PAGES_COMPLETE;
13765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    } else {
13865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        mPageHandler.addPage(WifiFormPageType.IP_SETTINGS);
13965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    }
14065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                } else {
14165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    mPageHandler.addPage(WifiFormPageType.PROXY_HOSTNAME);
14265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
14365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                break;
14465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            case PROXY_HOSTNAME:
14565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mProxyHostnamePage = formPage;
14665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mPageHandler.addPage(WifiFormPageType.PROXY_PORT);
14765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                break;
14865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            case PROXY_PORT:
14965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mProxyPortPage = formPage;
15065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mPageHandler.addPage(WifiFormPageType.PROXY_BYPASS);
15165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                break;
15265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            case PROXY_BYPASS:
15365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mProxyBypassPage = formPage;
15465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                int proxySettingsResult = processProxySettings();
15565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (proxySettingsResult == 0) {
15665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    if (mSettingsFlow) {
15765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        return RESULT_ALL_PAGES_COMPLETE;
15865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    } else {
15965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        mPageHandler.addPage(WifiFormPageType.IP_SETTINGS);
16065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    }
16165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                } else {
16265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    mPageHandler.addPage(WifiFormPageType.PROXY_SETTINGS_INVALID);
16365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
16465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                break;
16565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            case PROXY_SETTINGS_INVALID:
16665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mPageHandler.removePage(mProxySettingsPage);
16765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mPageHandler.removePage(mProxyHostnamePage);
16865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mPageHandler.removePage(mProxyPortPage);
16965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mPageHandler.removePage(mProxyBypassPage);
17065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mPageHandler.addPage(WifiFormPageType.PROXY_SETTINGS);
17165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                break;
17265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            case IP_SETTINGS:
17365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mIpSettingsPage = formPage;
17465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (mPageHandler.choiceChosen(formPage, R.string.wifi_action_dhcp)) {
17565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    processIpSettings();
17665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    return RESULT_ALL_PAGES_COMPLETE;
17765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                } else {
17865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    mPageHandler.addPage(WifiFormPageType.IP_ADDRESS);
17965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
18065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                break;
18165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            case IP_ADDRESS:
18265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mIpAddressPage = formPage;
18365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mPageHandler.addPage(WifiFormPageType.GATEWAY);
18465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                break;
18565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            case GATEWAY:
18665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mGatewayPage = formPage;
18765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mPageHandler.addPage(WifiFormPageType.NETWORK_PREFIX_LENGTH);
18865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                break;
18965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            case NETWORK_PREFIX_LENGTH:
19065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mNetworkPrefixLengthPage = formPage;
19165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mPageHandler.addPage(WifiFormPageType.DNS1);
19265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                break;
19365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            case DNS1:
19465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mDns1Page = formPage;
19565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mPageHandler.addPage(WifiFormPageType.DNS2);
19665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                break;
19765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            case DNS2:
19865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mDns2Page = formPage;
19965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                int ipSettingsResult = processIpSettings();
20065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (ipSettingsResult == 0) {
20165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    return RESULT_ALL_PAGES_COMPLETE;
20265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                } else {
20365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    mPageHandler.addPage(WifiFormPageType.IP_SETTINGS_INVALID);
20465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
20565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                break;
20665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            case IP_SETTINGS_INVALID:
20765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mPageHandler.removePage(mIpSettingsPage);
20865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mPageHandler.removePage(mIpAddressPage);
20965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mPageHandler.removePage(mGatewayPage);
21065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mPageHandler.removePage(mDns1Page);
21165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mPageHandler.removePage(mDns2Page);
21265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mPageHandler.addPage(WifiFormPageType.IP_SETTINGS);
21365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                break;
21465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            default:
21565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                return RESULT_UNKNOWN_PAGE;
21665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
21765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return RESULT_PAGE_HANDLED;
21865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
21965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
22065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public FormPage getPreviousPage(WifiFormPageType formPageType) {
22165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        switch (formPageType) {
22265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            case ADVANCED_OPTIONS:
22365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                return mAdvancedOptionsPage;
22465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            case PROXY_SETTINGS:
22565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (mProxySettingsPage == null && getInitialProxyInfo() != null) {
22665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    return createFormPage(R.string.wifi_action_proxy_manual);
22765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
22865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                return mProxySettingsPage;
22965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            case PROXY_HOSTNAME:
23065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (mProxyHostnamePage == null && getInitialProxyInfo() != null) {
23165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    return createFormPage(getInitialProxyInfo().getHost());
23265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
23365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                return mProxyHostnamePage;
23465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            case PROXY_PORT:
23565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (mProxyPortPage == null && getInitialProxyInfo() != null) {
23665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    return createFormPage(Integer.toString(getInitialProxyInfo().getPort()));
23765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
23865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                return mProxyPortPage;
23965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            case PROXY_BYPASS:
24065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (mProxyBypassPage == null && getInitialProxyInfo() != null) {
24165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    return createFormPage(getInitialProxyInfo().getExclusionListAsString());
24265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
24365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                return mProxyBypassPage;
24465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            case IP_SETTINGS:
24565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (mIpSettingsPage == null && getInitialLinkAddress() != null) {
24665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    return createFormPage(R.string.wifi_action_static);
24765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
24865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                return mIpSettingsPage;
24965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            case IP_ADDRESS:
25065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (mIpAddressPage == null && getInitialLinkAddress() != null) {
25165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    return createFormPage(getInitialLinkAddress().getAddress().getHostAddress());
25265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
25365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                return mIpAddressPage;
25465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            case GATEWAY:
25565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (mGatewayPage == null && getInitialGateway() != null) {
25665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    return createFormPage(getInitialGateway().getHostAddress());
25765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
25865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                return mGatewayPage;
25965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            case NETWORK_PREFIX_LENGTH:
26065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (mNetworkPrefixLengthPage == null && getInitialLinkAddress() != null) {
26165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    return createFormPage(
26265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                            Integer.toString(getInitialLinkAddress().getNetworkPrefixLength()));
26365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
26465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                return mNetworkPrefixLengthPage;
26565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            case DNS1:
26665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (mDns1Page == null && getInitialDns(0) != null) {
26765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    return createFormPage(getInitialDns(0).getHostAddress());
26865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
26965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                return mDns1Page;
27065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            case DNS2:
27165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (mDns2Page == null && getInitialDns(1) != null) {
27265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    return createFormPage(getInitialDns(1).getHostAddress());
27365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
27465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                return mDns2Page;
27565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            case IP_SETTINGS_INVALID:
27665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            case PROXY_SETTINGS_INVALID:
27765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            default:
27865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                return null;
27965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
28065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
28165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
28265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public boolean isEmptyTextAllowed(WifiFormPageType formPageType) {
28365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        switch (formPageType) {
28465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            case PROXY_BYPASS:
28565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            case DNS1:
28665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            case DNS2:
28765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            case GATEWAY:
28865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                return true;
28965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            default:
29065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                return false;
29165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
29265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
29365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
29465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private IpConfiguration getCurrentIpConfiguration() {
29565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return mIpConfiguration;
29665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
29765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
29865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public void updateConfiguration(WifiConfiguration configuration) {
29965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        configuration.setIpConfiguration(mIpConfiguration);
30065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
30165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
30265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public void updateConfiguration(NetworkConfiguration configuration) {
30365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        configuration.setIpConfiguration(mIpConfiguration);
30465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
30565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
30665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private InetAddress getInitialDns(int index) {
30765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        try {
30865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return mInitialConfiguration.getIpConfiguration().getStaticIpConfiguration()
30965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    .dnsServers.get(index);
31065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } catch (IndexOutOfBoundsException|NullPointerException e) {
31165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return null;
31265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
31365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
31465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
31565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private InetAddress getInitialGateway() {
31665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        try {
31765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return mInitialConfiguration.getIpConfiguration().getStaticIpConfiguration().gateway;
31865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } catch (NullPointerException e) {
31965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return null;
32065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
32165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
32265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
32365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private LinkAddress getInitialLinkAddress() {
32465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        try {
32565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return mInitialConfiguration.getIpConfiguration().getStaticIpConfiguration().ipAddress;
32665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } catch (NullPointerException e) {
32765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return null;
32865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
32965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
33065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
33165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private ProxyInfo getInitialProxyInfo() {
33265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        try {
33365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return mInitialConfiguration.getIpConfiguration().getHttpProxy();
33465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } catch (NullPointerException e) {
33565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return null;
33665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
33765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
33865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
33965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private FormPage createFormPage(int resultStringResourceId) {
34065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return createFormPage(mContext.getString(resultStringResourceId).toUpperCase());
34165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
34265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
34365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private FormPage createFormPage(String resultString) {
34465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        Bundle result = new Bundle();
34565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        result.putString(FormPage.DATA_KEY_SUMMARY_STRING, resultString);
34665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        FormPage formPage = FormPage.createTextInputForm(resultString);
34765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        formPage.complete(result);
34865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return formPage;
34965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
35065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
35165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private int processProxySettings() {
35265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        boolean hasProxySettings = (mAdvancedOptionsPage == null || !mPageHandler.choiceChosen(
35365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mAdvancedOptionsPage, R.string.wifi_action_advanced_no))
35465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                && !mPageHandler.choiceChosen(mProxySettingsPage, R.string.wifi_action_proxy_none);
35565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mIpConfiguration.setProxySettings(hasProxySettings ?
35665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                                          ProxySettings.STATIC : ProxySettings.NONE);
35765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (hasProxySettings) {
35865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            String host = mProxyHostnamePage.getDataSummary();
35965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            String portStr = mProxyPortPage.getDataSummary();
36065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            String exclusionList = mProxyBypassPage.getDataSummary();
36165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            int port = 0;
36265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            int result = 0;
36365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            try {
36465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                port = Integer.parseInt(portStr);
36565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                result = WifiConfigHelper.validate(host, portStr, exclusionList);
36665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            } catch (NumberFormatException e) {
36765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                result = R.string.proxy_error_invalid_port;
36865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
36965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (result == 0) {
37065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mIpConfiguration.setHttpProxy(new ProxyInfo(host, port, exclusionList));
37165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            } else {
37265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                return result;
37365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
37465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else {
37565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mIpConfiguration.setHttpProxy(null);
37665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
37765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
37865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return 0;
37965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
38065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
38165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private int processIpSettings() {
38265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        boolean hasIpSettings = (mAdvancedOptionsPage == null || !mPageHandler.choiceChosen(
38365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mAdvancedOptionsPage, R.string.wifi_action_advanced_no))
38465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                && !mPageHandler.choiceChosen(mIpSettingsPage, R.string.wifi_action_dhcp);
38565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mIpConfiguration.setIpAssignment(hasIpSettings ? IpAssignment.STATIC : IpAssignment.DHCP);
38665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
38765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (hasIpSettings) {
38865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            StaticIpConfiguration staticConfig = new StaticIpConfiguration();
38965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mIpConfiguration.setStaticIpConfiguration(staticConfig);
39065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
39165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            String ipAddr = mIpAddressPage.getDataSummary();
39265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (TextUtils.isEmpty(ipAddr))
39365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                return R.string.wifi_ip_settings_invalid_ip_address;
39465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
39565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            Inet4Address inetAddr = null;
39665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            try {
39765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                inetAddr = (Inet4Address) NetworkUtils.numericToInetAddress(ipAddr);
39865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            } catch (IllegalArgumentException|ClassCastException e) {
39965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                return R.string.wifi_ip_settings_invalid_ip_address;
40065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
40165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
40265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            int networkPrefixLength = -1;
40365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            try {
40465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                networkPrefixLength = Integer.parseInt(mNetworkPrefixLengthPage.getDataSummary());
40565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (networkPrefixLength < 0 || networkPrefixLength > 32) {
40665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    return R.string.wifi_ip_settings_invalid_network_prefix_length;
40765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
40865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                staticConfig.ipAddress = new LinkAddress(inetAddr, networkPrefixLength);
40965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            } catch (NumberFormatException e) {
41065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                return R.string.wifi_ip_settings_invalid_ip_address;
41165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
41265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
41365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            String gateway = mGatewayPage.getDataSummary();
41465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (!TextUtils.isEmpty(gateway)) {
41565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                try {
41665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    staticConfig.gateway =
41765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                            (Inet4Address) NetworkUtils.numericToInetAddress(gateway);
41865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                } catch (IllegalArgumentException|ClassCastException e) {
41965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    return R.string.wifi_ip_settings_invalid_gateway;
42065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
42165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
42265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
42365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            String dns1 = mDns1Page.getDataSummary();
42465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (!TextUtils.isEmpty(dns1)) {
42565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                try {
42665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    staticConfig.dnsServers.add(
42765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                            (Inet4Address) NetworkUtils.numericToInetAddress(dns1));
42865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                } catch (IllegalArgumentException|ClassCastException e) {
42965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    return R.string.wifi_ip_settings_invalid_dns;
43065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
43165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
43265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
43365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            String dns2 = mDns2Page.getDataSummary();
43465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (!TextUtils.isEmpty(dns2)) {
43565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                try {
43665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    staticConfig.dnsServers.add(
43765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                            (Inet4Address) NetworkUtils.numericToInetAddress(dns1));
43865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                } catch (IllegalArgumentException|ClassCastException e) {
43965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    return R.string.wifi_ip_settings_invalid_dns;
44065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
44165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
44265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else {
44365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mIpConfiguration.setStaticIpConfiguration(null);
44465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
44565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return 0;
44665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
44765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane}
448