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