Proxy.java revision cc0aad38f0e9a9096f0acb71dfd1f6a8ebc7d344
1/* 2 * Copyright (C) 2007 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 android.net; 18 19import org.apache.http.HttpHost; 20 21import android.content.ContentResolver; 22import android.content.Context; 23import android.os.SystemProperties; 24import android.provider.Settings; 25import android.util.Log; 26 27import java.net.InetAddress; 28import java.net.URI; 29import java.net.UnknownHostException; 30 31import junit.framework.Assert; 32 33/** 34 * A convenience class for accessing the user and default proxy 35 * settings. 36 */ 37final public class Proxy { 38 39 // Set to true to enable extra debugging. 40 static final private boolean DEBUG = false; 41 42 static final public String PROXY_CHANGE_ACTION = 43 "android.intent.action.PROXY_CHANGE"; 44 45 /** 46 * Return the proxy host set by the user. 47 * @param ctx A Context used to get the settings for the proxy host. 48 * @return String containing the host name. If the user did not set a host 49 * name it returns the default host. A null value means that no 50 * host is to be used. 51 */ 52 static final public String getHost(Context ctx) { 53 ContentResolver contentResolver = ctx.getContentResolver(); 54 Assert.assertNotNull(contentResolver); 55 String host = Settings.Secure.getString( 56 contentResolver, 57 Settings.Secure.HTTP_PROXY); 58 if (host != null) { 59 int i = host.indexOf(':'); 60 if (i == -1) { 61 if (DEBUG) { 62 Assert.assertTrue(host.length() == 0); 63 } 64 return null; 65 } 66 return host.substring(0, i); 67 } 68 return getDefaultHost(); 69 } 70 71 /** 72 * Return the proxy port set by the user. 73 * @param ctx A Context used to get the settings for the proxy port. 74 * @return The port number to use or -1 if no proxy is to be used. 75 */ 76 static final public int getPort(Context ctx) { 77 ContentResolver contentResolver = ctx.getContentResolver(); 78 Assert.assertNotNull(contentResolver); 79 String host = Settings.Secure.getString( 80 contentResolver, 81 Settings.Secure.HTTP_PROXY); 82 if (host != null) { 83 int i = host.indexOf(':'); 84 if (i == -1) { 85 if (DEBUG) { 86 Assert.assertTrue(host.length() == 0); 87 } 88 return -1; 89 } 90 if (DEBUG) { 91 Assert.assertTrue(i < host.length()); 92 } 93 return Integer.parseInt(host.substring(i+1)); 94 } 95 return getDefaultPort(); 96 } 97 98 /** 99 * Return the default proxy host specified by the carrier. 100 * @return String containing the host name or null if there is no proxy for 101 * this carrier. 102 */ 103 static final public String getDefaultHost() { 104 String host = SystemProperties.get("net.gprs.http-proxy"); 105 if (host != null) { 106 Uri u = Uri.parse(host); 107 host = u.getHost(); 108 return host; 109 } else { 110 return null; 111 } 112 } 113 114 /** 115 * Return the default proxy port specified by the carrier. 116 * @return The port number to be used with the proxy host or -1 if there is 117 * no proxy for this carrier. 118 */ 119 static final public int getDefaultPort() { 120 String host = SystemProperties.get("net.gprs.http-proxy"); 121 if (host != null) { 122 Uri u = Uri.parse(host); 123 return u.getPort(); 124 } else { 125 return -1; 126 } 127 } 128 129 /** 130 * Returns the preferred proxy to be used by clients. This is a wrapper 131 * around {@link android.net.Proxy#getHost()}. Currently no proxy will 132 * be returned for localhost or if the active network is Wi-Fi. 133 * 134 * @param context the context which will be passed to 135 * {@link android.net.Proxy#getHost()} 136 * @param url the target URL for the request 137 * @note Calling this method requires permission 138 * android.permission.ACCESS_NETWORK_STATE 139 * @return The preferred proxy to be used by clients, or null if there 140 * is no proxy. 141 * 142 * {@hide} 143 */ 144 static final public HttpHost getPreferredHttpHost(Context context, 145 String url) { 146 if (!isLocalHost(url) && !isNetworkWifi(context)) { 147 final String proxyHost = Proxy.getHost(context); 148 if (proxyHost != null) { 149 return new HttpHost(proxyHost, Proxy.getPort(context), "http"); 150 } 151 } 152 153 return null; 154 } 155 156 static final private boolean isLocalHost(String url) { 157 if (url == null) { 158 return false; 159 } 160 161 try { 162 final URI uri = URI.create(url); 163 final String host = uri.getHost(); 164 if (host != null) { 165 if (host.equalsIgnoreCase("localhost")) { 166 return true; 167 } 168 if (InetAddress.getByName(host).isLoopbackAddress()) { 169 return true; 170 } 171 } 172 } catch (UnknownHostException uex) { 173 // Ignore (INetworkSystem.ipStringToByteArray) 174 } catch (IllegalArgumentException iex) { 175 // Ignore (URI.create) 176 } 177 178 return false; 179 } 180 181 static final private boolean isNetworkWifi(Context context) { 182 if (context == null) { 183 return false; 184 } 185 186 final ConnectivityManager connectivity = (ConnectivityManager) 187 context.getSystemService(Context.CONNECTIVITY_SERVICE); 188 if (connectivity != null) { 189 final NetworkInfo info = connectivity.getActiveNetworkInfo(); 190 if (info != null && 191 info.getType() == ConnectivityManager.TYPE_WIFI) { 192 return true; 193 } 194 } 195 196 return false; 197 } 198}; 199