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