The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.settings; |
| 18 | |
| 19 | import android.app.Activity; |
| 20 | import android.app.AlertDialog; |
Jiehua.Dai | 20108e2 | 2010-05-12 08:37:52 +0200 | [diff] [blame] | 21 | import android.app.Dialog; |
Daisuke Miyakawa | 21c1abc | 2010-09-12 15:42:56 -0700 | [diff] [blame] | 22 | import android.app.admin.DevicePolicyManager; |
Oscar Montemayor | 0541189 | 2010-08-03 16:56:09 -0700 | [diff] [blame] | 23 | import android.content.Context; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 24 | import android.content.Intent; |
Robert Greenwalt | 52322a9 | 2010-10-13 14:07:03 -0700 | [diff] [blame] | 25 | import android.net.ConnectivityManager; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 26 | import android.net.Proxy; |
Jason Monk | e86790c | 2014-05-05 12:36:10 -0400 | [diff] [blame] | 27 | import android.net.ProxyInfo; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 28 | import android.os.Bundle; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 29 | import android.text.Selection; |
| 30 | import android.text.Spannable; |
| 31 | import android.text.TextUtils; |
| 32 | import android.util.Log; |
Oscar Montemayor | 0541189 | 2010-08-03 16:56:09 -0700 | [diff] [blame] | 33 | import android.view.LayoutInflater; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 34 | import android.view.View; |
| 35 | import android.view.View.OnClickListener; |
| 36 | import android.view.View.OnFocusChangeListener; |
Daisuke Miyakawa | 21c1abc | 2010-09-12 15:42:56 -0700 | [diff] [blame] | 37 | import android.view.ViewGroup; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 38 | import android.widget.Button; |
| 39 | import android.widget.EditText; |
| 40 | import android.widget.TextView; |
| 41 | |
Tamas Berghammer | 265d3c2 | 2016-06-22 15:34:45 +0100 | [diff] [blame] | 42 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 43 | import com.android.settings.SettingsPreferenceFragment.SettingsDialogFragment; |
Fan Zhang | 2d0b344 | 2016-12-05 17:02:33 -0800 | [diff] [blame] | 44 | import com.android.settings.core.InstrumentedPreferenceFragment; |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 45 | |
Fan Zhang | 2d0b344 | 2016-12-05 17:02:33 -0800 | [diff] [blame] | 46 | public class ProxySelector extends InstrumentedPreferenceFragment implements DialogCreatable { |
Daisuke Miyakawa | 21c1abc | 2010-09-12 15:42:56 -0700 | [diff] [blame] | 47 | private static final String TAG = "ProxySelector"; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 48 | |
| 49 | EditText mHostnameField; |
| 50 | EditText mPortField; |
Oscar Montemayor | 0541189 | 2010-08-03 16:56:09 -0700 | [diff] [blame] | 51 | EditText mExclusionListField; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 52 | Button mOKButton; |
Daisuke Miyakawa | 21c1abc | 2010-09-12 15:42:56 -0700 | [diff] [blame] | 53 | Button mClearButton; |
| 54 | Button mDefaultButton; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 55 | |
Jiehua.Dai | 20108e2 | 2010-05-12 08:37:52 +0200 | [diff] [blame] | 56 | private static final int ERROR_DIALOG_ID = 0; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 57 | |
Daisuke Miyakawa | 21c1abc | 2010-09-12 15:42:56 -0700 | [diff] [blame] | 58 | private SettingsDialogFragment mDialogFragment; |
| 59 | private View mView; |
| 60 | |
| 61 | @Override |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 62 | public void onCreate(Bundle icicle) { |
| 63 | super.onCreate(icicle); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Jiehua.Dai | 20108e2 | 2010-05-12 08:37:52 +0200 | [diff] [blame] | 66 | @Override |
Daisuke Miyakawa | 21c1abc | 2010-09-12 15:42:56 -0700 | [diff] [blame] | 67 | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 68 | Bundle savedInstanceState) { |
| 69 | mView = inflater.inflate(R.layout.proxy, container, false); |
| 70 | initView(mView); |
| 71 | // TODO: Populate based on connection status |
Robert Greenwalt | 52322a9 | 2010-10-13 14:07:03 -0700 | [diff] [blame] | 72 | populateFields(); |
Daisuke Miyakawa | 21c1abc | 2010-09-12 15:42:56 -0700 | [diff] [blame] | 73 | return mView; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public void onActivityCreated(Bundle savedInstanceState) { |
| 78 | super.onActivityCreated(savedInstanceState); |
| 79 | final DevicePolicyManager dpm = |
| 80 | (DevicePolicyManager)getActivity().getSystemService(Context.DEVICE_POLICY_SERVICE); |
| 81 | |
| 82 | final boolean userSetGlobalProxy = (dpm.getGlobalProxyAdmin() == null); |
| 83 | // Disable UI if the Global Proxy is being controlled by a Device Admin |
| 84 | mHostnameField.setEnabled(userSetGlobalProxy); |
| 85 | mPortField.setEnabled(userSetGlobalProxy); |
| 86 | mExclusionListField.setEnabled(userSetGlobalProxy); |
| 87 | mOKButton.setEnabled(userSetGlobalProxy); |
| 88 | mClearButton.setEnabled(userSetGlobalProxy); |
| 89 | mDefaultButton.setEnabled(userSetGlobalProxy); |
| 90 | } |
| 91 | |
| 92 | // Dialog management |
| 93 | |
| 94 | @Override |
| 95 | public Dialog onCreateDialog(int id) { |
Jiehua.Dai | 20108e2 | 2010-05-12 08:37:52 +0200 | [diff] [blame] | 96 | if (id == ERROR_DIALOG_ID) { |
| 97 | String hostname = mHostnameField.getText().toString().trim(); |
| 98 | String portStr = mPortField.getText().toString().trim(); |
Oscar Montemayor | 0541189 | 2010-08-03 16:56:09 -0700 | [diff] [blame] | 99 | String exclList = mExclusionListField.getText().toString().trim(); |
Daisuke Miyakawa | 21c1abc | 2010-09-12 15:42:56 -0700 | [diff] [blame] | 100 | String msg = getActivity().getString(validate(hostname, portStr, exclList)); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 101 | |
Daisuke Miyakawa | 21c1abc | 2010-09-12 15:42:56 -0700 | [diff] [blame] | 102 | return new AlertDialog.Builder(getActivity()) |
Jiehua.Dai | 20108e2 | 2010-05-12 08:37:52 +0200 | [diff] [blame] | 103 | .setTitle(R.string.proxy_error) |
| 104 | .setPositiveButton(R.string.proxy_error_dismiss, null) |
| 105 | .setMessage(msg) |
| 106 | .create(); |
| 107 | } |
Daisuke Miyakawa | 21c1abc | 2010-09-12 15:42:56 -0700 | [diff] [blame] | 108 | return null; |
Jiehua.Dai | 20108e2 | 2010-05-12 08:37:52 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Fan Zhang | d65184f | 2016-09-19 17:45:24 -0700 | [diff] [blame] | 111 | @Override |
| 112 | public int getDialogMetricsCategory(int dialogId) { |
| 113 | return MetricsEvent.DIALOG_PROXY_SELECTOR_ERROR; |
| 114 | } |
| 115 | |
Daisuke Miyakawa | 21c1abc | 2010-09-12 15:42:56 -0700 | [diff] [blame] | 116 | private void showDialog(int dialogId) { |
| 117 | if (mDialogFragment != null) { |
| 118 | Log.e(TAG, "Old dialog fragment not null!"); |
Jiehua.Dai | 20108e2 | 2010-05-12 08:37:52 +0200 | [diff] [blame] | 119 | } |
Daisuke Miyakawa | 21c1abc | 2010-09-12 15:42:56 -0700 | [diff] [blame] | 120 | mDialogFragment = new SettingsDialogFragment(this, dialogId); |
| 121 | mDialogFragment.show(getActivity().getFragmentManager(), Integer.toString(dialogId)); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Daisuke Miyakawa | 21c1abc | 2010-09-12 15:42:56 -0700 | [diff] [blame] | 124 | private void initView(View view) { |
| 125 | mHostnameField = (EditText)view.findViewById(R.id.hostname); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 126 | mHostnameField.setOnFocusChangeListener(mOnFocusChangeHandler); |
| 127 | |
Daisuke Miyakawa | 21c1abc | 2010-09-12 15:42:56 -0700 | [diff] [blame] | 128 | mPortField = (EditText)view.findViewById(R.id.port); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 129 | mPortField.setOnClickListener(mOKHandler); |
| 130 | mPortField.setOnFocusChangeListener(mOnFocusChangeHandler); |
| 131 | |
Daisuke Miyakawa | 21c1abc | 2010-09-12 15:42:56 -0700 | [diff] [blame] | 132 | mExclusionListField = (EditText)view.findViewById(R.id.exclusionlist); |
Oscar Montemayor | 0541189 | 2010-08-03 16:56:09 -0700 | [diff] [blame] | 133 | mExclusionListField.setOnFocusChangeListener(mOnFocusChangeHandler); |
| 134 | |
Daisuke Miyakawa | 21c1abc | 2010-09-12 15:42:56 -0700 | [diff] [blame] | 135 | mOKButton = (Button)view.findViewById(R.id.action); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 136 | mOKButton.setOnClickListener(mOKHandler); |
| 137 | |
Daisuke Miyakawa | 21c1abc | 2010-09-12 15:42:56 -0700 | [diff] [blame] | 138 | mClearButton = (Button)view.findViewById(R.id.clear); |
| 139 | mClearButton.setOnClickListener(mClearHandler); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 140 | |
Daisuke Miyakawa | 21c1abc | 2010-09-12 15:42:56 -0700 | [diff] [blame] | 141 | mDefaultButton = (Button)view.findViewById(R.id.defaultView); |
| 142 | mDefaultButton.setOnClickListener(mDefaultHandler); |
| 143 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 144 | |
Robert Greenwalt | 52322a9 | 2010-10-13 14:07:03 -0700 | [diff] [blame] | 145 | void populateFields() { |
Daisuke Miyakawa | 21c1abc | 2010-09-12 15:42:56 -0700 | [diff] [blame] | 146 | final Activity activity = getActivity(); |
Robert Greenwalt | 52322a9 | 2010-10-13 14:07:03 -0700 | [diff] [blame] | 147 | String hostname = ""; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 148 | int port = -1; |
Robert Greenwalt | 52322a9 | 2010-10-13 14:07:03 -0700 | [diff] [blame] | 149 | String exclList = ""; |
| 150 | // Use the last setting given by the user |
| 151 | ConnectivityManager cm = |
| 152 | (ConnectivityManager)getActivity().getSystemService(Context.CONNECTIVITY_SERVICE); |
| 153 | |
Jason Monk | e86790c | 2014-05-05 12:36:10 -0400 | [diff] [blame] | 154 | ProxyInfo proxy = cm.getGlobalProxy(); |
Robert Greenwalt | 52322a9 | 2010-10-13 14:07:03 -0700 | [diff] [blame] | 155 | if (proxy != null) { |
| 156 | hostname = proxy.getHost(); |
| 157 | port = proxy.getPort(); |
Jason Monk | d65928c | 2014-05-07 16:28:29 -0400 | [diff] [blame] | 158 | exclList = proxy.getExclusionListAsString(); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | if (hostname == null) { |
| 162 | hostname = ""; |
| 163 | } |
| 164 | |
| 165 | mHostnameField.setText(hostname); |
| 166 | |
| 167 | String portStr = port == -1 ? "" : Integer.toString(port); |
| 168 | mPortField.setText(portStr); |
| 169 | |
Oscar Montemayor | 0541189 | 2010-08-03 16:56:09 -0700 | [diff] [blame] | 170 | mExclusionListField.setText(exclList); |
| 171 | |
Daisuke Miyakawa | 21c1abc | 2010-09-12 15:42:56 -0700 | [diff] [blame] | 172 | final Intent intent = activity.getIntent(); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 173 | |
| 174 | String buttonLabel = intent.getStringExtra("button-label"); |
| 175 | if (!TextUtils.isEmpty(buttonLabel)) { |
| 176 | mOKButton.setText(buttonLabel); |
| 177 | } |
| 178 | |
| 179 | String title = intent.getStringExtra("title"); |
| 180 | if (!TextUtils.isEmpty(title)) { |
Daisuke Miyakawa | 21c1abc | 2010-09-12 15:42:56 -0700 | [diff] [blame] | 181 | activity.setTitle(title); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * validate syntax of hostname and port entries |
| 187 | * @return 0 on success, string resource ID on failure |
| 188 | */ |
Irfan Sheriff | c536192 | 2010-09-22 16:09:35 -0700 | [diff] [blame] | 189 | public static int validate(String hostname, String port, String exclList) { |
Yuhao Zheng | 1f7a190 | 2014-02-28 17:09:43 -0800 | [diff] [blame] | 190 | switch (Proxy.validate(hostname, port, exclList)) { |
| 191 | case Proxy.PROXY_VALID: |
| 192 | return 0; |
| 193 | case Proxy.PROXY_HOSTNAME_EMPTY: |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 194 | return R.string.proxy_error_empty_host_set_port; |
Yuhao Zheng | 1f7a190 | 2014-02-28 17:09:43 -0800 | [diff] [blame] | 195 | case Proxy.PROXY_HOSTNAME_INVALID: |
| 196 | return R.string.proxy_error_invalid_host; |
| 197 | case Proxy.PROXY_PORT_EMPTY: |
| 198 | return R.string.proxy_error_empty_port; |
| 199 | case Proxy.PROXY_PORT_INVALID: |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 200 | return R.string.proxy_error_invalid_port; |
Yuhao Zheng | 1f7a190 | 2014-02-28 17:09:43 -0800 | [diff] [blame] | 201 | case Proxy.PROXY_EXCLLIST_INVALID: |
| 202 | return R.string.proxy_error_invalid_exclusion_list; |
| 203 | default: |
| 204 | // should neven happen |
| 205 | Log.e(TAG, "Unknown proxy settings error"); |
| 206 | return -1; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 207 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /** |
| 211 | * returns true on success, false if the user must correct something |
| 212 | */ |
| 213 | boolean saveToDb() { |
| 214 | |
| 215 | String hostname = mHostnameField.getText().toString().trim(); |
| 216 | String portStr = mPortField.getText().toString().trim(); |
Oscar Montemayor | 0541189 | 2010-08-03 16:56:09 -0700 | [diff] [blame] | 217 | String exclList = mExclusionListField.getText().toString().trim(); |
Robert Greenwalt | 52322a9 | 2010-10-13 14:07:03 -0700 | [diff] [blame] | 218 | int port = 0; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 219 | |
Oscar Montemayor | 0541189 | 2010-08-03 16:56:09 -0700 | [diff] [blame] | 220 | int result = validate(hostname, portStr, exclList); |
Yuhao Zheng | 1f7a190 | 2014-02-28 17:09:43 -0800 | [diff] [blame] | 221 | if (result != 0) { |
Jiehua.Dai | 20108e2 | 2010-05-12 08:37:52 +0200 | [diff] [blame] | 222 | showDialog(ERROR_DIALOG_ID); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 223 | return false; |
| 224 | } |
| 225 | |
| 226 | if (portStr.length() > 0) { |
| 227 | try { |
| 228 | port = Integer.parseInt(portStr); |
| 229 | } catch (NumberFormatException ex) { |
Robert Greenwalt | 52322a9 | 2010-10-13 14:07:03 -0700 | [diff] [blame] | 230 | // should never happen - caught by validate above |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 231 | return false; |
| 232 | } |
| 233 | } |
Jason Monk | e86790c | 2014-05-05 12:36:10 -0400 | [diff] [blame] | 234 | ProxyInfo p = new ProxyInfo(hostname, port, exclList); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 235 | // FIXME: The best solution would be to make a better UI that would |
| 236 | // disable editing of the text boxes if the user chooses to use the |
| 237 | // default settings. i.e. checking a box to always use the default |
| 238 | // carrier. http:/b/issue?id=756480 |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 239 | // FIXME: If the user types in a proxy that matches the default, should |
| 240 | // we keep that setting? Can be fixed with a new UI. |
Robert Greenwalt | 52322a9 | 2010-10-13 14:07:03 -0700 | [diff] [blame] | 241 | ConnectivityManager cm = |
| 242 | (ConnectivityManager)getActivity().getSystemService(Context.CONNECTIVITY_SERVICE); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 243 | |
Robert Greenwalt | 52322a9 | 2010-10-13 14:07:03 -0700 | [diff] [blame] | 244 | cm.setGlobalProxy(p); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 245 | return true; |
| 246 | } |
| 247 | |
| 248 | OnClickListener mOKHandler = new OnClickListener() { |
| 249 | public void onClick(View v) { |
| 250 | if (saveToDb()) { |
Daisuke Miyakawa | 21c1abc | 2010-09-12 15:42:56 -0700 | [diff] [blame] | 251 | getActivity().onBackPressed(); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | }; |
| 255 | |
| 256 | OnClickListener mClearHandler = new OnClickListener() { |
| 257 | public void onClick(View v) { |
| 258 | mHostnameField.setText(""); |
| 259 | mPortField.setText(""); |
Oscar Montemayor | 0541189 | 2010-08-03 16:56:09 -0700 | [diff] [blame] | 260 | mExclusionListField.setText(""); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 261 | } |
| 262 | }; |
| 263 | |
| 264 | OnClickListener mDefaultHandler = new OnClickListener() { |
| 265 | public void onClick(View v) { |
Oscar Montemayor | 0541189 | 2010-08-03 16:56:09 -0700 | [diff] [blame] | 266 | // TODO: populate based on connection status |
Robert Greenwalt | 52322a9 | 2010-10-13 14:07:03 -0700 | [diff] [blame] | 267 | populateFields(); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 268 | } |
| 269 | }; |
| 270 | |
| 271 | OnFocusChangeListener mOnFocusChangeHandler = new OnFocusChangeListener() { |
| 272 | public void onFocusChange(View v, boolean hasFocus) { |
| 273 | if (hasFocus) { |
| 274 | TextView textView = (TextView) v; |
| 275 | Selection.selectAll((Spannable) textView.getText()); |
| 276 | } |
| 277 | } |
| 278 | }; |
Chris Wren | 8a963ba | 2015-03-20 10:29:14 -0400 | [diff] [blame] | 279 | |
| 280 | @Override |
Fan Zhang | 6507613 | 2016-08-08 10:25:13 -0700 | [diff] [blame] | 281 | public int getMetricsCategory() { |
Chris Wren | 9d1bfd1 | 2016-01-26 18:04:01 -0500 | [diff] [blame] | 282 | return MetricsEvent.PROXY_SELECTOR; |
Chris Wren | 8a963ba | 2015-03-20 10:29:14 -0400 | [diff] [blame] | 283 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 284 | } |