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