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.Dialog; 21 import android.app.admin.DevicePolicyManager; 22 import android.app.settings.SettingsEnums; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.net.ConnectivityManager; 26 import android.net.Proxy; 27 import android.net.ProxyInfo; 28 import android.os.Bundle; 29 import android.text.Selection; 30 import android.text.Spannable; 31 import android.text.TextUtils; 32 import android.util.Log; 33 import android.view.LayoutInflater; 34 import android.view.View; 35 import android.view.View.OnClickListener; 36 import android.view.View.OnFocusChangeListener; 37 import android.view.ViewGroup; 38 import android.widget.Button; 39 import android.widget.EditText; 40 import android.widget.TextView; 41 42 import androidx.appcompat.app.AlertDialog; 43 44 import com.android.settings.SettingsPreferenceFragment.SettingsDialogFragment; 45 import com.android.settings.core.InstrumentedFragment; 46 47 public class ProxySelector extends InstrumentedFragment implements DialogCreatable { 48 private static final String TAG = "ProxySelector"; 49 50 EditText mHostnameField; 51 EditText mPortField; 52 EditText mExclusionListField; 53 Button mOKButton; 54 Button mClearButton; 55 Button mDefaultButton; 56 57 private static final int ERROR_DIALOG_ID = 0; 58 59 private SettingsDialogFragment mDialogFragment; 60 private View mView; 61 62 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)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 68 populateFields(); 69 return mView; 70 } 71 72 @Override onActivityCreated(Bundle savedInstanceState)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 onCreateDialog(int id)91 public Dialog onCreateDialog(int id) { 92 if (id == ERROR_DIALOG_ID) { 93 String hostname = mHostnameField.getText().toString().trim(); 94 String portStr = mPortField.getText().toString().trim(); 95 String exclList = mExclusionListField.getText().toString().trim(); 96 String msg = getActivity().getString(validate(hostname, portStr, exclList)); 97 98 return new AlertDialog.Builder(getActivity()) 99 .setTitle(R.string.proxy_error) 100 .setPositiveButton(R.string.proxy_error_dismiss, null) 101 .setMessage(msg) 102 .create(); 103 } 104 return null; 105 } 106 107 @Override getDialogMetricsCategory(int dialogId)108 public int getDialogMetricsCategory(int dialogId) { 109 return SettingsEnums.DIALOG_PROXY_SELECTOR_ERROR; 110 } 111 showDialog(int dialogId)112 private void showDialog(int dialogId) { 113 if (mDialogFragment != null) { 114 Log.e(TAG, "Old dialog fragment not null!"); 115 } 116 mDialogFragment = SettingsDialogFragment.newInstance(this, dialogId); 117 mDialogFragment.show(getActivity().getSupportFragmentManager(), Integer.toString(dialogId)); 118 } 119 initView(View view)120 private void initView(View view) { 121 mHostnameField = (EditText)view.findViewById(R.id.hostname); 122 mHostnameField.setOnFocusChangeListener(mOnFocusChangeHandler); 123 124 mPortField = (EditText)view.findViewById(R.id.port); 125 mPortField.setOnClickListener(mOKHandler); 126 mPortField.setOnFocusChangeListener(mOnFocusChangeHandler); 127 128 mExclusionListField = (EditText)view.findViewById(R.id.exclusionlist); 129 mExclusionListField.setOnFocusChangeListener(mOnFocusChangeHandler); 130 131 mOKButton = (Button)view.findViewById(R.id.action); 132 mOKButton.setOnClickListener(mOKHandler); 133 134 mClearButton = (Button)view.findViewById(R.id.clear); 135 mClearButton.setOnClickListener(mClearHandler); 136 137 mDefaultButton = (Button)view.findViewById(R.id.defaultView); 138 mDefaultButton.setOnClickListener(mDefaultHandler); 139 } 140 populateFields()141 void populateFields() { 142 final Activity activity = getActivity(); 143 String hostname = ""; 144 int port = -1; 145 String exclList = ""; 146 // Use the last setting given by the user 147 ConnectivityManager cm = 148 (ConnectivityManager)getActivity().getSystemService(Context.CONNECTIVITY_SERVICE); 149 150 ProxyInfo proxy = cm.getGlobalProxy(); 151 if (proxy != null) { 152 hostname = proxy.getHost(); 153 port = proxy.getPort(); 154 exclList = proxy.getExclusionListAsString(); 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 166 mExclusionListField.setText(exclList); 167 168 final Intent intent = activity.getIntent(); 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)) { 177 activity.setTitle(title); 178 } else { 179 activity.setTitle(R.string.proxy_settings_title); 180 } 181 } 182 183 /** 184 * validate syntax of hostname and port entries 185 * @return 0 on success, string resource ID on failure 186 */ validate(String hostname, String port, String exclList)187 public static int validate(String hostname, String port, String exclList) { 188 switch (Proxy.validate(hostname, port, exclList)) { 189 case Proxy.PROXY_VALID: 190 return 0; 191 case Proxy.PROXY_HOSTNAME_EMPTY: 192 return R.string.proxy_error_empty_host_set_port; 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: 198 return R.string.proxy_error_invalid_port; 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; 205 } 206 } 207 208 /** 209 * returns true on success, false if the user must correct something 210 */ saveToDb()211 boolean saveToDb() { 212 213 String hostname = mHostnameField.getText().toString().trim(); 214 String portStr = mPortField.getText().toString().trim(); 215 String exclList = mExclusionListField.getText().toString().trim(); 216 int port = 0; 217 218 int result = validate(hostname, portStr, exclList); 219 if (result != 0) { 220 showDialog(ERROR_DIALOG_ID); 221 return false; 222 } 223 224 if (portStr.length() > 0) { 225 try { 226 port = Integer.parseInt(portStr); 227 } catch (NumberFormatException ex) { 228 // should never happen - caught by validate above 229 return false; 230 } 231 } 232 ProxyInfo p = new ProxyInfo(hostname, port, exclList); 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 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. 239 ConnectivityManager cm = 240 (ConnectivityManager)getActivity().getSystemService(Context.CONNECTIVITY_SERVICE); 241 242 cm.setGlobalProxy(p); 243 return true; 244 } 245 246 OnClickListener mOKHandler = new OnClickListener() { 247 public void onClick(View v) { 248 if (saveToDb()) { 249 getActivity().onBackPressed(); 250 } 251 } 252 }; 253 254 OnClickListener mClearHandler = new OnClickListener() { 255 public void onClick(View v) { 256 mHostnameField.setText(""); 257 mPortField.setText(""); 258 mExclusionListField.setText(""); 259 } 260 }; 261 262 OnClickListener mDefaultHandler = new OnClickListener() { 263 public void onClick(View v) { 264 // TODO: populate based on connection status 265 populateFields(); 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 }; 277 278 @Override getMetricsCategory()279 public int getMetricsCategory() { 280 return SettingsEnums.PROXY_SELECTOR; 281 } 282 } 283