1afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project/*
2afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * Copyright (C) 2006 The Android Open Source Project
3afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project *
4afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
5afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * you may not use this file except in compliance with the License.
6afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * You may obtain a copy of the License at
7afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project *
8afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
9afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project *
10afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * Unless required by applicable law or agreed to in writing, software
11afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
12afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * See the License for the specific language governing permissions and
14afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * limitations under the License.
15afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project */
16afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
17afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectpackage com.android.settings;
18afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
19afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.app.Activity;
20afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.app.AlertDialog;
2120108e2ed6d7fdc889d85702f7cbda906db141cfJiehua.Daiimport android.app.Dialog;
2221c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawaimport android.app.admin.DevicePolicyManager;
2305411893af4afc4bf7b8530d787c5b9e0bbeed39Oscar Montemayorimport android.content.Context;
24afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.content.Intent;
2552322a9173fc167d3011017b396d1c7419fc280bRobert Greenwaltimport android.net.ConnectivityManager;
26afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.net.Proxy;
27e86790c8381aceb1d627c878fc41ab765c4d6874Jason Monkimport android.net.ProxyInfo;
28afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.os.Bundle;
29afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.text.Selection;
30afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.text.Spannable;
31afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.text.TextUtils;
32afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.util.Log;
3305411893af4afc4bf7b8530d787c5b9e0bbeed39Oscar Montemayorimport android.view.LayoutInflater;
34afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.view.View;
35afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.view.View.OnClickListener;
36afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.view.View.OnFocusChangeListener;
3721c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawaimport android.view.ViewGroup;
38afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.widget.Button;
39afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.widget.EditText;
40afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.widget.TextView;
41afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
429d1bfd1e8de6e46137a9571507c03526880d6a46Chris Wrenimport com.android.internal.logging.MetricsProto.MetricsEvent;
4339b467482d1bf256a111c757e9b7621c6f523271Jason Monkimport com.android.settings.SettingsPreferenceFragment.SettingsDialogFragment;
4439b467482d1bf256a111c757e9b7621c6f523271Jason Monk
458a963babe2e36b7a41f77b8d2598c97658196e58Chris Wrenpublic class ProxySelector extends InstrumentedFragment implements DialogCreatable {
4621c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa    private static final String TAG = "ProxySelector";
47afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
48afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    EditText    mHostnameField;
49afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    EditText    mPortField;
5005411893af4afc4bf7b8530d787c5b9e0bbeed39Oscar Montemayor    EditText    mExclusionListField;
51afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    Button      mOKButton;
5221c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa    Button      mClearButton;
5321c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa    Button      mDefaultButton;
54afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
5520108e2ed6d7fdc889d85702f7cbda906db141cfJiehua.Dai    private static final int ERROR_DIALOG_ID = 0;
56afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
5721c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa    private SettingsDialogFragment mDialogFragment;
5821c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa    private View mView;
5921c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa
6021c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa    @Override
61afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    public void onCreate(Bundle icicle) {
62afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        super.onCreate(icicle);
6321c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa    }
64afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
6521c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa    @Override
6621c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa    public View onCreateView(LayoutInflater inflater, ViewGroup container,
6721c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa            Bundle savedInstanceState) {
6821c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa        mView = inflater.inflate(R.layout.proxy, container, false);
6921c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa        initView(mView);
7005411893af4afc4bf7b8530d787c5b9e0bbeed39Oscar Montemayor        // TODO: Populate based on connection status
7152322a9173fc167d3011017b396d1c7419fc280bRobert Greenwalt        populateFields();
7221c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa        return mView;
73afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    }
74afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
7520108e2ed6d7fdc889d85702f7cbda906db141cfJiehua.Dai    @Override
7621c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa    public void onActivityCreated(Bundle savedInstanceState) {
7721c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa        super.onActivityCreated(savedInstanceState);
7821c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa        final DevicePolicyManager dpm =
7921c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa                (DevicePolicyManager)getActivity().getSystemService(Context.DEVICE_POLICY_SERVICE);
8021c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa
8121c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa        final boolean userSetGlobalProxy = (dpm.getGlobalProxyAdmin() == null);
8221c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa        // Disable UI if the Global Proxy is being controlled by a Device Admin
8321c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa        mHostnameField.setEnabled(userSetGlobalProxy);
8421c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa        mPortField.setEnabled(userSetGlobalProxy);
8521c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa        mExclusionListField.setEnabled(userSetGlobalProxy);
8621c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa        mOKButton.setEnabled(userSetGlobalProxy);
8721c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa        mClearButton.setEnabled(userSetGlobalProxy);
8821c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa        mDefaultButton.setEnabled(userSetGlobalProxy);
8921c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa    }
9021c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa
9121c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa    // Dialog management
9221c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa
9321c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa    @Override
9421c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa    public Dialog onCreateDialog(int id) {
9520108e2ed6d7fdc889d85702f7cbda906db141cfJiehua.Dai        if (id == ERROR_DIALOG_ID) {
9620108e2ed6d7fdc889d85702f7cbda906db141cfJiehua.Dai            String hostname = mHostnameField.getText().toString().trim();
9720108e2ed6d7fdc889d85702f7cbda906db141cfJiehua.Dai            String portStr = mPortField.getText().toString().trim();
9805411893af4afc4bf7b8530d787c5b9e0bbeed39Oscar Montemayor            String exclList = mExclusionListField.getText().toString().trim();
9921c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa            String msg = getActivity().getString(validate(hostname, portStr, exclList));
10020108e2ed6d7fdc889d85702f7cbda906db141cfJiehua.Dai
10121c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa            return new AlertDialog.Builder(getActivity())
10220108e2ed6d7fdc889d85702f7cbda906db141cfJiehua.Dai                    .setTitle(R.string.proxy_error)
10320108e2ed6d7fdc889d85702f7cbda906db141cfJiehua.Dai                    .setPositiveButton(R.string.proxy_error_dismiss, null)
10420108e2ed6d7fdc889d85702f7cbda906db141cfJiehua.Dai                    .setMessage(msg)
10520108e2ed6d7fdc889d85702f7cbda906db141cfJiehua.Dai                    .create();
10620108e2ed6d7fdc889d85702f7cbda906db141cfJiehua.Dai        }
10721c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa        return null;
10820108e2ed6d7fdc889d85702f7cbda906db141cfJiehua.Dai    }
109afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
11021c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa    private void showDialog(int dialogId) {
11121c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa        if (mDialogFragment != null) {
11221c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa            Log.e(TAG, "Old dialog fragment not null!");
11320108e2ed6d7fdc889d85702f7cbda906db141cfJiehua.Dai        }
11421c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa        mDialogFragment = new SettingsDialogFragment(this, dialogId);
11521c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa        mDialogFragment.show(getActivity().getFragmentManager(), Integer.toString(dialogId));
116afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    }
117afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
11821c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa    private void initView(View view) {
11921c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa        mHostnameField = (EditText)view.findViewById(R.id.hostname);
120afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        mHostnameField.setOnFocusChangeListener(mOnFocusChangeHandler);
121afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
12221c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa        mPortField = (EditText)view.findViewById(R.id.port);
123afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        mPortField.setOnClickListener(mOKHandler);
124afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        mPortField.setOnFocusChangeListener(mOnFocusChangeHandler);
125afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
12621c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa        mExclusionListField = (EditText)view.findViewById(R.id.exclusionlist);
12705411893af4afc4bf7b8530d787c5b9e0bbeed39Oscar Montemayor        mExclusionListField.setOnFocusChangeListener(mOnFocusChangeHandler);
12805411893af4afc4bf7b8530d787c5b9e0bbeed39Oscar Montemayor
12921c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa        mOKButton = (Button)view.findViewById(R.id.action);
130afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        mOKButton.setOnClickListener(mOKHandler);
131afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
13221c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa        mClearButton = (Button)view.findViewById(R.id.clear);
13321c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa        mClearButton.setOnClickListener(mClearHandler);
134afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
13521c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa        mDefaultButton = (Button)view.findViewById(R.id.defaultView);
13621c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa        mDefaultButton.setOnClickListener(mDefaultHandler);
13721c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa    }
138afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
13952322a9173fc167d3011017b396d1c7419fc280bRobert Greenwalt    void populateFields() {
14021c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa        final Activity activity = getActivity();
14152322a9173fc167d3011017b396d1c7419fc280bRobert Greenwalt        String hostname = "";
142afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        int port = -1;
14352322a9173fc167d3011017b396d1c7419fc280bRobert Greenwalt        String exclList = "";
14452322a9173fc167d3011017b396d1c7419fc280bRobert Greenwalt        // Use the last setting given by the user
14552322a9173fc167d3011017b396d1c7419fc280bRobert Greenwalt        ConnectivityManager cm =
14652322a9173fc167d3011017b396d1c7419fc280bRobert Greenwalt                (ConnectivityManager)getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
14752322a9173fc167d3011017b396d1c7419fc280bRobert Greenwalt
148e86790c8381aceb1d627c878fc41ab765c4d6874Jason Monk        ProxyInfo proxy = cm.getGlobalProxy();
14952322a9173fc167d3011017b396d1c7419fc280bRobert Greenwalt        if (proxy != null) {
15052322a9173fc167d3011017b396d1c7419fc280bRobert Greenwalt            hostname = proxy.getHost();
15152322a9173fc167d3011017b396d1c7419fc280bRobert Greenwalt            port = proxy.getPort();
152d65928c558e79d3e7e0682b3d99610204fb4479fJason Monk            exclList = proxy.getExclusionListAsString();
153afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
154afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
155afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        if (hostname == null) {
156afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            hostname = "";
157afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
158afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
159afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        mHostnameField.setText(hostname);
160afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
161afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        String portStr = port == -1 ? "" : Integer.toString(port);
162afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        mPortField.setText(portStr);
163afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
16405411893af4afc4bf7b8530d787c5b9e0bbeed39Oscar Montemayor        mExclusionListField.setText(exclList);
16505411893af4afc4bf7b8530d787c5b9e0bbeed39Oscar Montemayor
16621c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa        final Intent intent = activity.getIntent();
167afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
168afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        String buttonLabel = intent.getStringExtra("button-label");
169afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        if (!TextUtils.isEmpty(buttonLabel)) {
170afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            mOKButton.setText(buttonLabel);
171afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
172afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
173afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        String title = intent.getStringExtra("title");
174afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        if (!TextUtils.isEmpty(title)) {
17521c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa            activity.setTitle(title);
176afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
177afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    }
178afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
179afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    /**
180afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project     * validate syntax of hostname and port entries
181afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project     * @return 0 on success, string resource ID on failure
182afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project     */
183c5361927f42572c8e10867ec6f8ffaa2cacff733Irfan Sheriff    public static int validate(String hostname, String port, String exclList) {
1841f7a190fd70c73a6c119b1c30dc4e4c034d8f343Yuhao Zheng        switch (Proxy.validate(hostname, port, exclList)) {
1851f7a190fd70c73a6c119b1c30dc4e4c034d8f343Yuhao Zheng            case Proxy.PROXY_VALID:
1861f7a190fd70c73a6c119b1c30dc4e4c034d8f343Yuhao Zheng                return 0;
1871f7a190fd70c73a6c119b1c30dc4e4c034d8f343Yuhao Zheng            case Proxy.PROXY_HOSTNAME_EMPTY:
188afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                return R.string.proxy_error_empty_host_set_port;
1891f7a190fd70c73a6c119b1c30dc4e4c034d8f343Yuhao Zheng            case Proxy.PROXY_HOSTNAME_INVALID:
1901f7a190fd70c73a6c119b1c30dc4e4c034d8f343Yuhao Zheng                return R.string.proxy_error_invalid_host;
1911f7a190fd70c73a6c119b1c30dc4e4c034d8f343Yuhao Zheng            case Proxy.PROXY_PORT_EMPTY:
1921f7a190fd70c73a6c119b1c30dc4e4c034d8f343Yuhao Zheng                return R.string.proxy_error_empty_port;
1931f7a190fd70c73a6c119b1c30dc4e4c034d8f343Yuhao Zheng            case Proxy.PROXY_PORT_INVALID:
194afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                return R.string.proxy_error_invalid_port;
1951f7a190fd70c73a6c119b1c30dc4e4c034d8f343Yuhao Zheng            case Proxy.PROXY_EXCLLIST_INVALID:
1961f7a190fd70c73a6c119b1c30dc4e4c034d8f343Yuhao Zheng                return R.string.proxy_error_invalid_exclusion_list;
1971f7a190fd70c73a6c119b1c30dc4e4c034d8f343Yuhao Zheng            default:
1981f7a190fd70c73a6c119b1c30dc4e4c034d8f343Yuhao Zheng                // should neven happen
1991f7a190fd70c73a6c119b1c30dc4e4c034d8f343Yuhao Zheng                Log.e(TAG, "Unknown proxy settings error");
2001f7a190fd70c73a6c119b1c30dc4e4c034d8f343Yuhao Zheng                return -1;
201afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
202afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    }
203afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
204afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    /**
205afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project     * returns true on success, false if the user must correct something
206afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project     */
207afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    boolean saveToDb() {
208afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
209afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        String hostname = mHostnameField.getText().toString().trim();
210afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        String portStr = mPortField.getText().toString().trim();
21105411893af4afc4bf7b8530d787c5b9e0bbeed39Oscar Montemayor        String exclList = mExclusionListField.getText().toString().trim();
21252322a9173fc167d3011017b396d1c7419fc280bRobert Greenwalt        int port = 0;
213afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
21405411893af4afc4bf7b8530d787c5b9e0bbeed39Oscar Montemayor        int result = validate(hostname, portStr, exclList);
2151f7a190fd70c73a6c119b1c30dc4e4c034d8f343Yuhao Zheng        if (result != 0) {
21620108e2ed6d7fdc889d85702f7cbda906db141cfJiehua.Dai            showDialog(ERROR_DIALOG_ID);
217afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            return false;
218afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
219afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
220afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        if (portStr.length() > 0) {
221afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            try {
222afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                port = Integer.parseInt(portStr);
223afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            } catch (NumberFormatException ex) {
22452322a9173fc167d3011017b396d1c7419fc280bRobert Greenwalt                // should never happen - caught by validate above
225afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                return false;
226afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            }
227afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
228e86790c8381aceb1d627c878fc41ab765c4d6874Jason Monk        ProxyInfo p = new ProxyInfo(hostname, port, exclList);
229afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        // FIXME: The best solution would be to make a better UI that would
230afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        // disable editing of the text boxes if the user chooses to use the
231afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        // default settings. i.e. checking a box to always use the default
232afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        // carrier. http:/b/issue?id=756480
233afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        // FIXME: If the user types in a proxy that matches the default, should
234afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        // we keep that setting? Can be fixed with a new UI.
23552322a9173fc167d3011017b396d1c7419fc280bRobert Greenwalt        ConnectivityManager cm =
23652322a9173fc167d3011017b396d1c7419fc280bRobert Greenwalt                (ConnectivityManager)getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
237afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
23852322a9173fc167d3011017b396d1c7419fc280bRobert Greenwalt        cm.setGlobalProxy(p);
239afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        return true;
240afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    }
241afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
242afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    OnClickListener mOKHandler = new OnClickListener() {
243afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            public void onClick(View v) {
244afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                if (saveToDb()) {
24521c1abcef77b6db18590215b6f7bc8e6dfda28bfDaisuke Miyakawa                    getActivity().onBackPressed();
246afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                }
247afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            }
248afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        };
249afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
250afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    OnClickListener mClearHandler = new OnClickListener() {
251afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            public void onClick(View v) {
252afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                mHostnameField.setText("");
253afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                mPortField.setText("");
25405411893af4afc4bf7b8530d787c5b9e0bbeed39Oscar Montemayor                mExclusionListField.setText("");
255afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            }
256afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        };
257afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
258afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    OnClickListener mDefaultHandler = new OnClickListener() {
259afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            public void onClick(View v) {
26005411893af4afc4bf7b8530d787c5b9e0bbeed39Oscar Montemayor                // TODO: populate based on connection status
26152322a9173fc167d3011017b396d1c7419fc280bRobert Greenwalt                populateFields();
262afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            }
263afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        };
264afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
265afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    OnFocusChangeListener mOnFocusChangeHandler = new OnFocusChangeListener() {
266afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            public void onFocusChange(View v, boolean hasFocus) {
267afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                if (hasFocus) {
268afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                    TextView textView = (TextView) v;
269afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                    Selection.selectAll((Spannable) textView.getText());
270afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                }
271afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            }
272afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        };
2738a963babe2e36b7a41f77b8d2598c97658196e58Chris Wren
2748a963babe2e36b7a41f77b8d2598c97658196e58Chris Wren    @Override
2758a963babe2e36b7a41f77b8d2598c97658196e58Chris Wren    protected int getMetricsCategory() {
2769d1bfd1e8de6e46137a9571507c03526880d6a46Chris Wren        return MetricsEvent.PROXY_SELECTOR;
2778a963babe2e36b7a41f77b8d2598c97658196e58Chris Wren    }
278afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project}
279