Proxy.java revision 54b6cfa9a9e5b861a9930af873580d6dc20f773c
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 android.content.ContentResolver;
20import android.content.Context;
21import android.os.SystemProperties;
22import android.provider.Settings;
23import android.util.Log;
24
25import junit.framework.Assert;
26
27/**
28 * A convenience class for accessing the user and default proxy
29 * settings.
30 */
31final public class Proxy {
32
33    static final public String PROXY_CHANGE_ACTION =
34        "android.intent.action.PROXY_CHANGE";
35
36    /**
37     * Return the proxy host set by the user.
38     * @param ctx A Context used to get the settings for the proxy host.
39     * @return String containing the host name. If the user did not set a host
40     *         name it returns the default host. A null value means that no
41     *         host is to be used.
42     */
43    static final public String getHost(Context ctx) {
44        ContentResolver contentResolver = ctx.getContentResolver();
45        Assert.assertNotNull(contentResolver);
46        String host = Settings.System.getString(
47                contentResolver,
48                Settings.System.HTTP_PROXY);
49        if (host != null) {
50            int i = host.indexOf(':');
51            if (i == -1) {
52                if (android.util.Config.DEBUG) {
53                    Assert.assertTrue(host.length() == 0);
54                }
55                return null;
56            }
57            return host.substring(0, i);
58        }
59        return getDefaultHost();
60    }
61
62    /**
63     * Return the proxy port set by the user.
64     * @param ctx A Context used to get the settings for the proxy port.
65     * @return The port number to use or -1 if no proxy is to be used.
66     */
67    static final public int getPort(Context ctx) {
68        ContentResolver contentResolver = ctx.getContentResolver();
69        Assert.assertNotNull(contentResolver);
70        String host = Settings.System.getString(
71                contentResolver,
72                Settings.System.HTTP_PROXY);
73        if (host != null) {
74            int i = host.indexOf(':');
75            if (i == -1) {
76                if (android.util.Config.DEBUG) {
77                    Assert.assertTrue(host.length() == 0);
78                }
79                return -1;
80            }
81            if (android.util.Config.DEBUG) {
82                Assert.assertTrue(i < host.length());
83            }
84            return Integer.parseInt(host.substring(i+1));
85        }
86        return getDefaultPort();
87    }
88
89    /**
90     * Return the default proxy host specified by the carrier.
91     * @return String containing the host name or null if there is no proxy for
92     * this carrier.
93     */
94    static final public String getDefaultHost() {
95        String host = SystemProperties.get("net.gprs.http-proxy");
96        if (host != null) {
97            Uri u = Uri.parse(host);
98            host = u.getHost();
99            return host;
100        } else {
101            return null;
102        }
103    }
104
105    /**
106     * Return the default proxy port specified by the carrier.
107     * @return The port number to be used with the proxy host or -1 if there is
108     * no proxy for this carrier.
109     */
110    static final public int getDefaultPort() {
111        String host = SystemProperties.get("net.gprs.http-proxy");
112        if (host != null) {
113            Uri u = Uri.parse(host);
114            return u.getPort();
115        } else {
116            return -1;
117        }
118    }
119
120};
121