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