blob: 625369e7915316d3fad76d222898526f4c9be9c1 [file] [log] [blame]
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -08001/*
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
17package com.android.settings;
18
19import android.app.Activity;
Jiehua.Dai20108e22010-05-12 08:37:52 +020020import android.app.Dialog;
Daisuke Miyakawa21c1abc2010-09-12 15:42:56 -070021import android.app.admin.DevicePolicyManager;
Fan Zhang31b21002019-01-16 13:49:47 -080022import android.app.settings.SettingsEnums;
Oscar Montemayor05411892010-08-03 16:56:09 -070023import android.content.Context;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080024import android.content.Intent;
Robert Greenwalt52322a92010-10-13 14:07:03 -070025import android.net.ConnectivityManager;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080026import android.net.Proxy;
Jason Monke86790c2014-05-05 12:36:10 -040027import android.net.ProxyInfo;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080028import android.os.Bundle;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080029import android.text.Selection;
30import android.text.Spannable;
31import android.text.TextUtils;
32import android.util.Log;
Oscar Montemayor05411892010-08-03 16:56:09 -070033import android.view.LayoutInflater;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080034import android.view.View;
35import android.view.View.OnClickListener;
36import android.view.View.OnFocusChangeListener;
Daisuke Miyakawa21c1abc2010-09-12 15:42:56 -070037import android.view.ViewGroup;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080038import android.widget.Button;
39import android.widget.EditText;
40import android.widget.TextView;
41
Fan Zhang23f8d592018-08-28 15:11:40 -070042import androidx.appcompat.app.AlertDialog;
43
Jason Monk39b46742015-09-10 15:52:51 -040044import com.android.settings.SettingsPreferenceFragment.SettingsDialogFragment;
Doris Ling72489722017-11-16 11:03:40 -080045import com.android.settings.core.InstrumentedFragment;
Jason Monk39b46742015-09-10 15:52:51 -040046
Doris Ling72489722017-11-16 11:03:40 -080047public class ProxySelector extends InstrumentedFragment implements DialogCreatable {
Daisuke Miyakawa21c1abc2010-09-12 15:42:56 -070048 private static final String TAG = "ProxySelector";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080049
50 EditText mHostnameField;
51 EditText mPortField;
Oscar Montemayor05411892010-08-03 16:56:09 -070052 EditText mExclusionListField;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080053 Button mOKButton;
Daisuke Miyakawa21c1abc2010-09-12 15:42:56 -070054 Button mClearButton;
55 Button mDefaultButton;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080056
Jiehua.Dai20108e22010-05-12 08:37:52 +020057 private static final int ERROR_DIALOG_ID = 0;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080058
Daisuke Miyakawa21c1abc2010-09-12 15:42:56 -070059 private SettingsDialogFragment mDialogFragment;
60 private View mView;
61
62 @Override
Daisuke Miyakawa21c1abc2010-09-12 15:42:56 -070063 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 Greenwalt52322a92010-10-13 14:07:03 -070068 populateFields();
Daisuke Miyakawa21c1abc2010-09-12 15:42:56 -070069 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.Dai20108e22010-05-12 08:37:52 +020092 if (id == ERROR_DIALOG_ID) {
93 String hostname = mHostnameField.getText().toString().trim();
94 String portStr = mPortField.getText().toString().trim();
Oscar Montemayor05411892010-08-03 16:56:09 -070095 String exclList = mExclusionListField.getText().toString().trim();
Daisuke Miyakawa21c1abc2010-09-12 15:42:56 -070096 String msg = getActivity().getString(validate(hostname, portStr, exclList));
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080097
Daisuke Miyakawa21c1abc2010-09-12 15:42:56 -070098 return new AlertDialog.Builder(getActivity())
Jiehua.Dai20108e22010-05-12 08:37:52 +020099 .setTitle(R.string.proxy_error)
100 .setPositiveButton(R.string.proxy_error_dismiss, null)
101 .setMessage(msg)
102 .create();
103 }
Daisuke Miyakawa21c1abc2010-09-12 15:42:56 -0700104 return null;
Jiehua.Dai20108e22010-05-12 08:37:52 +0200105 }
106
Fan Zhangd65184f2016-09-19 17:45:24 -0700107 @Override
108 public int getDialogMetricsCategory(int dialogId) {
Fan Zhang31b21002019-01-16 13:49:47 -0800109 return SettingsEnums.DIALOG_PROXY_SELECTOR_ERROR;
Fan Zhangd65184f2016-09-19 17:45:24 -0700110 }
111
Daisuke Miyakawa21c1abc2010-09-12 15:42:56 -0700112 private void showDialog(int dialogId) {
113 if (mDialogFragment != null) {
114 Log.e(TAG, "Old dialog fragment not null!");
Jiehua.Dai20108e22010-05-12 08:37:52 +0200115 }
tmfangd5405cf2018-10-05 18:45:07 +0800116 mDialogFragment = SettingsDialogFragment.newInstance(this, dialogId);
tmfang27c84de2018-06-28 11:39:05 +0800117 mDialogFragment.show(getActivity().getSupportFragmentManager(), Integer.toString(dialogId));
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800118 }
119
Daisuke Miyakawa21c1abc2010-09-12 15:42:56 -0700120 private void initView(View view) {
121 mHostnameField = (EditText)view.findViewById(R.id.hostname);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800122 mHostnameField.setOnFocusChangeListener(mOnFocusChangeHandler);
123
Daisuke Miyakawa21c1abc2010-09-12 15:42:56 -0700124 mPortField = (EditText)view.findViewById(R.id.port);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800125 mPortField.setOnClickListener(mOKHandler);
126 mPortField.setOnFocusChangeListener(mOnFocusChangeHandler);
127
Daisuke Miyakawa21c1abc2010-09-12 15:42:56 -0700128 mExclusionListField = (EditText)view.findViewById(R.id.exclusionlist);
Oscar Montemayor05411892010-08-03 16:56:09 -0700129 mExclusionListField.setOnFocusChangeListener(mOnFocusChangeHandler);
130
Daisuke Miyakawa21c1abc2010-09-12 15:42:56 -0700131 mOKButton = (Button)view.findViewById(R.id.action);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800132 mOKButton.setOnClickListener(mOKHandler);
133
Daisuke Miyakawa21c1abc2010-09-12 15:42:56 -0700134 mClearButton = (Button)view.findViewById(R.id.clear);
135 mClearButton.setOnClickListener(mClearHandler);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800136
Daisuke Miyakawa21c1abc2010-09-12 15:42:56 -0700137 mDefaultButton = (Button)view.findViewById(R.id.defaultView);
138 mDefaultButton.setOnClickListener(mDefaultHandler);
139 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800140
Robert Greenwalt52322a92010-10-13 14:07:03 -0700141 void populateFields() {
Daisuke Miyakawa21c1abc2010-09-12 15:42:56 -0700142 final Activity activity = getActivity();
Robert Greenwalt52322a92010-10-13 14:07:03 -0700143 String hostname = "";
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800144 int port = -1;
Robert Greenwalt52322a92010-10-13 14:07:03 -0700145 String exclList = "";
146 // Use the last setting given by the user
147 ConnectivityManager cm =
148 (ConnectivityManager)getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
149
Jason Monke86790c2014-05-05 12:36:10 -0400150 ProxyInfo proxy = cm.getGlobalProxy();
Robert Greenwalt52322a92010-10-13 14:07:03 -0700151 if (proxy != null) {
152 hostname = proxy.getHost();
153 port = proxy.getPort();
Jason Monkd65928c2014-05-07 16:28:29 -0400154 exclList = proxy.getExclusionListAsString();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800155 }
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 Montemayor05411892010-08-03 16:56:09 -0700166 mExclusionListField.setText(exclList);
167
Daisuke Miyakawa21c1abc2010-09-12 15:42:56 -0700168 final Intent intent = activity.getIntent();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800169
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 Miyakawa21c1abc2010-09-12 15:42:56 -0700177 activity.setTitle(title);
Doris Ling72489722017-11-16 11:03:40 -0800178 } else {
179 activity.setTitle(R.string.proxy_settings_title);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800180 }
181 }
182
183 /**
184 * validate syntax of hostname and port entries
185 * @return 0 on success, string resource ID on failure
186 */
Irfan Sheriffc5361922010-09-22 16:09:35 -0700187 public static int validate(String hostname, String port, String exclList) {
Yuhao Zheng1f7a1902014-02-28 17:09:43 -0800188 switch (Proxy.validate(hostname, port, exclList)) {
189 case Proxy.PROXY_VALID:
190 return 0;
191 case Proxy.PROXY_HOSTNAME_EMPTY:
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800192 return R.string.proxy_error_empty_host_set_port;
Yuhao Zheng1f7a1902014-02-28 17:09:43 -0800193 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 Projectafc4ab22009-03-03 19:32:34 -0800198 return R.string.proxy_error_invalid_port;
Yuhao Zheng1f7a1902014-02-28 17:09:43 -0800199 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 Projectafc4ab22009-03-03 19:32:34 -0800205 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800206 }
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 Montemayor05411892010-08-03 16:56:09 -0700215 String exclList = mExclusionListField.getText().toString().trim();
Robert Greenwalt52322a92010-10-13 14:07:03 -0700216 int port = 0;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800217
Oscar Montemayor05411892010-08-03 16:56:09 -0700218 int result = validate(hostname, portStr, exclList);
Yuhao Zheng1f7a1902014-02-28 17:09:43 -0800219 if (result != 0) {
Jiehua.Dai20108e22010-05-12 08:37:52 +0200220 showDialog(ERROR_DIALOG_ID);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800221 return false;
222 }
223
224 if (portStr.length() > 0) {
225 try {
226 port = Integer.parseInt(portStr);
227 } catch (NumberFormatException ex) {
Robert Greenwalt52322a92010-10-13 14:07:03 -0700228 // should never happen - caught by validate above
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800229 return false;
230 }
231 }
Jason Monke86790c2014-05-05 12:36:10 -0400232 ProxyInfo p = new ProxyInfo(hostname, port, exclList);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800233 // 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 Projectafc4ab22009-03-03 19:32:34 -0800237 // 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 Greenwalt52322a92010-10-13 14:07:03 -0700239 ConnectivityManager cm =
240 (ConnectivityManager)getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800241
Robert Greenwalt52322a92010-10-13 14:07:03 -0700242 cm.setGlobalProxy(p);
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800243 return true;
244 }
245
246 OnClickListener mOKHandler = new OnClickListener() {
247 public void onClick(View v) {
248 if (saveToDb()) {
Daisuke Miyakawa21c1abc2010-09-12 15:42:56 -0700249 getActivity().onBackPressed();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800250 }
251 }
252 };
253
254 OnClickListener mClearHandler = new OnClickListener() {
255 public void onClick(View v) {
256 mHostnameField.setText("");
257 mPortField.setText("");
Oscar Montemayor05411892010-08-03 16:56:09 -0700258 mExclusionListField.setText("");
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800259 }
260 };
261
262 OnClickListener mDefaultHandler = new OnClickListener() {
263 public void onClick(View v) {
Oscar Montemayor05411892010-08-03 16:56:09 -0700264 // TODO: populate based on connection status
Robert Greenwalt52322a92010-10-13 14:07:03 -0700265 populateFields();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800266 }
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 Wren8a963ba2015-03-20 10:29:14 -0400277
278 @Override
Fan Zhang65076132016-08-08 10:25:13 -0700279 public int getMetricsCategory() {
Fan Zhang31b21002019-01-16 13:49:47 -0800280 return SettingsEnums.PROXY_SELECTOR;
Chris Wren8a963ba2015-03-20 10:29:14 -0400281 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800282}