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