android.net.Proxy Java Examples

The following examples show how to use android.net.Proxy. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpUtils.java    From MiBandDecompiled with Apache License 2.0 6 votes vote down vote up
private static String b(Context context)
{
    if (android.os.Build.VERSION.SDK_INT < 11)
    {
        if (context != null)
        {
            String s = Proxy.getHost(context);
            if (TextUtils.isEmpty(s))
            {
                s = Proxy.getDefaultHost();
            }
            return s;
        } else
        {
            return Proxy.getDefaultHost();
        }
    } else
    {
        return System.getProperty("http.proxyHost");
    }
}
 
Example #2
Source File: ProxyChangeListener.java    From cronet with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void registerReceiver() {
    if (mProxyReceiver != null) {
        return;
    }
    IntentFilter filter = new IntentFilter();
    filter.addAction(Proxy.PROXY_CHANGE_ACTION);
    mProxyReceiver = new ProxyReceiver();
    ContextUtils.getApplicationContext().registerReceiver(mProxyReceiver, filter);
}
 
Example #3
Source File: NetworkUtils.java    From letv with Apache License 2.0 6 votes vote down vote up
public static boolean checkIsProxyNet(Context context) {
    boolean isAboveICS;
    String proxyAddress;
    ((WifiManager) context.getSystemService("wifi")).getConnectionInfo();
    if (VERSION.SDK_INT >= 14) {
        isAboveICS = true;
    } else {
        isAboveICS = false;
    }
    if (isAboveICS) {
        proxyAddress = System.getProperty("http.proxyHost");
    } else {
        proxyAddress = Proxy.getHost(context);
    }
    if (TextUtils.isEmpty(proxyAddress)) {
        return false;
    }
    return true;
}
 
Example #4
Source File: q.java    From letv with Apache License 2.0 6 votes vote down vote up
private static HttpURLConnection a(URL url, String str) throws IOException {
    String replaceFirst;
    int i = 80;
    String defaultHost = Proxy.getDefaultHost();
    int defaultPort = Proxy.getDefaultPort();
    if (defaultPort == -1) {
        defaultPort = 80;
    }
    String host = url.getHost();
    int port = url.getPort();
    if (port != -1) {
        i = port;
    }
    if (str.indexOf(new StringBuilder(String.valueOf(host)).append(NetworkUtils.DELIMITER_COLON).append(i).toString()) != -1) {
        replaceFirst = str.replaceFirst(new StringBuilder(String.valueOf(host)).append(NetworkUtils.DELIMITER_COLON).append(i).toString(), new StringBuilder(String.valueOf(defaultHost)).append(NetworkUtils.DELIMITER_COLON).append(defaultPort).toString());
    } else {
        replaceFirst = str.replaceFirst(host, new StringBuilder(String.valueOf(defaultHost)).append(NetworkUtils.DELIMITER_COLON).append(defaultPort).toString());
    }
    try {
        HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(replaceFirst).openConnection();
        httpURLConnection.setRequestProperty("X-Online-Host", new StringBuilder(String.valueOf(host)).append(NetworkUtils.DELIMITER_COLON).append(i).toString());
        return httpURLConnection;
    } catch (MalformedURLException e) {
        return null;
    }
}
 
Example #5
Source File: HttpUtils.java    From letv with Apache License 2.0 6 votes vote down vote up
private static int a(Context context) {
    int i = -1;
    if (VERSION.SDK_INT >= 11) {
        Object property = System.getProperty("http.proxyPort");
        if (TextUtils.isEmpty(property)) {
            return i;
        }
        try {
            return Integer.parseInt(property);
        } catch (NumberFormatException e) {
            return i;
        }
    } else if (context == null) {
        return Proxy.getDefaultPort();
    } else {
        i = Proxy.getPort(context);
        if (i < 0) {
            return Proxy.getDefaultPort();
        }
        return i;
    }
}
 
Example #6
Source File: ProxyChangeListener.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void registerReceiver() {
    if (mProxyReceiver != null) {
        return;
    }
    IntentFilter filter = new IntentFilter();
    filter.addAction(Proxy.PROXY_CHANGE_ACTION);
    mProxyReceiver = new ProxyReceiver();
    mContext.getApplicationContext().registerReceiver(mProxyReceiver, filter);
}
 
Example #7
Source File: MediaPlayer.java    From droidel with Apache License 2.0 5 votes vote down vote up
private void setupProxyListener(Context context) {
    IntentFilter filter = new IntentFilter();
    filter.addAction(Proxy.PROXY_CHANGE_ACTION);
    mProxyReceiver = new ProxyReceiver();
    mProxyContext = context;

    Intent currentProxy =
        context.getApplicationContext().registerReceiver(mProxyReceiver, filter);

    if (currentProxy != null) {
        handleProxyBroadcast(currentProxy);
    }
}
 
Example #8
Source File: ProxyChangeListener.java    From 365browser with Apache License 2.0 5 votes vote down vote up
private void registerReceiver() {
    if (mProxyReceiver != null) {
        return;
    }
    IntentFilter filter = new IntentFilter();
    filter.addAction(Proxy.PROXY_CHANGE_ACTION);
    mProxyReceiver = new ProxyReceiver();
    ContextUtils.getApplicationContext().registerReceiver(mProxyReceiver, filter);
}
 
Example #9
Source File: ProxyChangeListener.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@Override
public void onReceive(Context context, final Intent intent) {
    if (intent.getAction().equals(Proxy.PROXY_CHANGE_ACTION)) {
        runOnThread(new Runnable() {
            @Override
            public void run() {
                proxySettingsChanged(ProxyReceiver.this, extractNewProxy(intent));
            }
        });
    }
}
 
Example #10
Source File: MediaPlayer.java    From droidel with Apache License 2.0 5 votes vote down vote up
private void handleProxyBroadcast(Intent intent) {
    ProxyProperties props =
        (ProxyProperties)intent.getExtra(Proxy.EXTRA_PROXY_INFO);

    if (props == null || props.getHost() == null) {
        updateProxyConfig(null);
    } else {
        updateProxyConfig(props);
    }
}
 
Example #11
Source File: WebViewProxyUtil.java    From FaceSlim with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Set Proxy for Android 5.0 and above.
 */
@SuppressWarnings("all")
private static boolean setLollipopWebViewProxy(Context appContext, String host, int port) {
    System.setProperty("http.proxyHost", host);
    System.setProperty("http.proxyPort", port + "");
    System.setProperty("https.proxyHost", host);
    System.setProperty("https.proxyPort", port + "");
    try {
        Class applictionCls = Class.forName("android.app.Application");
        Field loadedApkField = applictionCls.getDeclaredField("mLoadedApk");
        loadedApkField.setAccessible(true);
        Object loadedApk = loadedApkField.get(appContext);
        Class loadedApkCls = Class.forName("android.app.LoadedApk");
        Field receiversField = loadedApkCls.getDeclaredField("mReceivers");
        receiversField.setAccessible(true);
        ArrayMap receivers = (ArrayMap) receiversField.get(loadedApk);
        for (Object receiverMap : receivers.values()) {
            for (Object rec : ((ArrayMap) receiverMap).keySet()) {
                Class clazz = rec.getClass();
                if (clazz.getName().contains("ProxyChangeListener")) {
                    Method onReceiveMethod = clazz.getDeclaredMethod("onReceive", Context.class, Intent.class);
                    Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
                    /***** In Lollipop, ProxyProperties went public as ProxyInfo *****/
                    final String CLASS_NAME = "android.net.ProxyInfo";
                    Class cls = Class.forName(CLASS_NAME);
                    /***** ProxyInfo lacks constructors, use the static buildDirectProxy method instead *****/
                    Method buildDirectProxyMethod = cls.getMethod("buildDirectProxy", String.class, Integer.TYPE);
                    Object proxyInfo = buildDirectProxyMethod.invoke(cls, host, port);
                    intent.putExtra("proxy", (Parcelable) proxyInfo);
                    onReceiveMethod.invoke(rec, appContext, intent);
                }
            }
        }
    } catch (Exception e) {
        Log.e(LOG_TAG, "Setting proxy with >= 5.0 API failed with", e);
        return false;
    }
    Log.d(LOG_TAG, "Setting proxy with >= 5.0 API successful!");
    return true;
}
 
Example #12
Source File: ProxyUtils.java    From AndroidHttpCapture with MIT License 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.KITKAT)
private static boolean setProxyLollipop(final Context context, String host, int port) {
    System.setProperty("http.proxyHost", host);
    System.setProperty("http.proxyPort", port + "");
    System.setProperty("https.proxyHost", host);
    System.setProperty("https.proxyPort", port + "");
    try {
        Context appContext = context.getApplicationContext();
        Class applictionClass = Class.forName("android.app.Application");
        Field mLoadedApkField = applictionClass.getDeclaredField("mLoadedApk");
        mLoadedApkField.setAccessible(true);
        Object mloadedApk = mLoadedApkField.get(appContext);
        Class loadedApkClass = Class.forName("android.app.LoadedApk");
        Field mReceiversField = loadedApkClass.getDeclaredField("mReceivers");
        mReceiversField.setAccessible(true);
        ArrayMap receivers = (ArrayMap) mReceiversField.get(mloadedApk);
        for (Object receiverMap : receivers.values()) {
            for (Object receiver : ((ArrayMap) receiverMap).keySet()) {
                Class clazz = receiver.getClass();
                if (clazz.getName().contains("ProxyChangeListener")) {
                    Method onReceiveMethod = clazz.getDeclaredMethod("onReceive", Context.class, Intent.class);
                    Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
                    onReceiveMethod.invoke(receiver, appContext, intent);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }

    return true;
}
 
Example #13
Source File: HttpUtils.java    From letv with Apache License 2.0 5 votes vote down vote up
private static String b(Context context) {
    if (VERSION.SDK_INT >= 11) {
        return System.getProperty("http.proxyHost");
    }
    if (context == null) {
        return Proxy.getDefaultHost();
    }
    String host = Proxy.getHost(context);
    if (TextUtils.isEmpty(host)) {
        return Proxy.getDefaultHost();
    }
    return host;
}
 
Example #14
Source File: LeService.java    From letv with Apache License 2.0 5 votes vote down vote up
private String detectProxy() {
    String proxyUrl = null;
    if (!TextUtils.isEmpty(Proxy.getDefaultHost())) {
        proxyUrl = String.format(FORMAT_PROXY_URL, new Object[]{Proxy.getDefaultHost(), Integer.valueOf(Proxy.getDefaultPort())});
    }
    LogTool.i(TAG, "detectProxy. detected the proxy url(%s)", proxyUrl);
    return TextUtils.isEmpty(proxyUrl) ? "" : URLEncoder.encode(proxyUrl);
}
 
Example #15
Source File: ProxyChangeListener.java    From cronet with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
@UsedByReflection("WebView embedders call this to override proxy settings")
public void onReceive(Context context, final Intent intent) {
    if (intent.getAction().equals(Proxy.PROXY_CHANGE_ACTION)) {
        runOnThread(new Runnable() {
            @Override
            public void run() {
                proxySettingsChanged(ProxyReceiver.this, extractNewProxy(intent));
            }
        });
    }
}
 
Example #16
Source File: ProxyUtil.java    From CapturePacket with MIT License 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.KITKAT)
private static boolean setProxyLollipop(final Context context, String host, int port) {
    System.setProperty("http.proxyHost", host);
    System.setProperty("http.proxyPort", port + "");
    System.setProperty("https.proxyHost", host);
    System.setProperty("https.proxyPort", port + "");
    try {
        Context appContext = context.getApplicationContext();
        Class applictionClass = Class.forName("android.app.Application");
        Field mLoadedApkField = applictionClass.getDeclaredField("mLoadedApk");
        mLoadedApkField.setAccessible(true);
        Object mloadedApk = mLoadedApkField.get(appContext);
        Class loadedApkClass = Class.forName("android.app.LoadedApk");
        Field mReceiversField = loadedApkClass.getDeclaredField("mReceivers");
        mReceiversField.setAccessible(true);
        ArrayMap receivers = (ArrayMap) mReceiversField.get(mloadedApk);
        for (Object receiverMap : receivers.values()) {
            for (Object receiver : ((ArrayMap) receiverMap).keySet()) {
                Class clazz = receiver.getClass();
                if (clazz.getName().contains("ProxyChangeListener")) {
                    Method onReceiveMethod = clazz.getDeclaredMethod("onReceive", Context.class, Intent.class);
                    Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
                    onReceiveMethod.invoke(receiver, appContext, intent);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }

    return true;
}
 
Example #17
Source File: ProxyChangeListener.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void registerReceiver() {
    if (mProxyReceiver != null) {
        return;
    }
    IntentFilter filter = new IntentFilter();
    filter.addAction(Proxy.PROXY_CHANGE_ACTION);
    mProxyReceiver = new ProxyReceiver();
    mContext.getApplicationContext().registerReceiver(mProxyReceiver, filter);
}
 
Example #18
Source File: ProxyChangeListener.java    From android-chromium with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(Proxy.PROXY_CHANGE_ACTION)) {
        proxySettingsChanged(extractNewProxy(intent));
    }
}
 
Example #19
Source File: WebViewProxyUtil.java    From FaceSlim with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Set Proxy for Android 4.4 and above.
 */
@SuppressWarnings("all")
private static boolean setKitKatWebViewProxy(Context appContext, String host, int port) {
    System.setProperty("http.proxyHost", host);
    System.setProperty("http.proxyPort", port + "");
    System.setProperty("https.proxyHost", host);
    System.setProperty("https.proxyPort", port + "");
    try {
        Class applictionCls = Class.forName("android.app.Application");
        Field loadedApkField = applictionCls.getDeclaredField("mLoadedApk");
        loadedApkField.setAccessible(true);
        Object loadedApk = loadedApkField.get(appContext);
        Class loadedApkCls = Class.forName("android.app.LoadedApk");
        Field receiversField = loadedApkCls.getDeclaredField("mReceivers");
        receiversField.setAccessible(true);
        ArrayMap receivers = (ArrayMap) receiversField.get(loadedApk);
        for (Object receiverMap : receivers.values()) {
            for (Object rec : ((ArrayMap) receiverMap).keySet()) {
                Class clazz = rec.getClass();
                if (clazz.getName().contains("ProxyChangeListener")) {
                    Method onReceiveMethod = clazz.getDeclaredMethod("onReceive", Context.class, Intent.class);
                    Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);

                    /*********** optional, may be need in future *************/
                    final String CLASS_NAME = "android.net.ProxyProperties";
                    Class cls = Class.forName(CLASS_NAME);
                    Constructor constructor = cls.getConstructor(String.class, Integer.TYPE, String.class);
                    constructor.setAccessible(true);
                    Object proxyProperties = constructor.newInstance(host, port, null);
                    intent.putExtra("proxy", (Parcelable) proxyProperties);
                    /*********** optional, may be need in future *************/

                    onReceiveMethod.invoke(rec, appContext, intent);
                }
            }
        }
    } catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException |
            IllegalArgumentException | NoSuchMethodException | InvocationTargetException |
            InstantiationException e) {
        Log.e(LOG_TAG, "Setting proxy with >= 4.4 API failed with error: ", e);
        return false;
    }

    Log.d(LOG_TAG, "Setting proxy with >= 4.4 API successful!");
    return true;
}
 
Example #20
Source File: WebGuiActivity.java    From syncthing-android with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Set webview proxy and sites that are not retrieved using proxy.
 * Compatible with KitKat or higher android version.
 * Returns boolean if successful.
 * Source: https://stackoverflow.com/a/26781539
 */
@SuppressLint("PrivateApi")
public static boolean setWebViewProxy(Context appContext, String host, int port, String exclusionList) {
    if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
        // Not supported on android version lower than KitKat.
        return false;
    }

    Properties properties = System.getProperties();
    properties.setProperty("http.proxyHost", host);
    properties.setProperty("http.proxyPort", Integer.toString(port));
    properties.setProperty("https.proxyHost", host);
    properties.setProperty("https.proxyPort", Integer.toString(port));
    properties.setProperty("http.nonProxyHosts", exclusionList);
    properties.setProperty("https.nonProxyHosts", exclusionList);

    try {
        Class applictionCls = Class.forName("android.app.Application");
        Field loadedApkField = applictionCls.getDeclaredField("mLoadedApk");
        loadedApkField.setAccessible(true);
        Object loadedApk = loadedApkField.get(appContext);
        Class loadedApkCls = Class.forName("android.app.LoadedApk");
        Field receiversField = loadedApkCls.getDeclaredField("mReceivers");
        receiversField.setAccessible(true);
        ArrayMap receivers = (ArrayMap) receiversField.get(loadedApk);
        for (Object receiverMap : receivers.values()) {
            for (Object rec : ((ArrayMap) receiverMap).keySet()) {
                Class clazz = rec.getClass();
                if (clazz.getName().contains("ProxyChangeListener")) {
                    Method onReceiveMethod = clazz.getDeclaredMethod("onReceive", Context.class, Intent.class);
                    Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);

                    String CLASS_NAME;
                    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
                        CLASS_NAME = "android.net.ProxyProperties";
                    } else {
                        CLASS_NAME = "android.net.ProxyInfo";
                    }
                    Class cls = Class.forName(CLASS_NAME);
                    Constructor constructor = cls.getConstructor(String.class, Integer.TYPE, String.class);
                    constructor.setAccessible(true);
                    Object proxyProperties = constructor.newInstance(host, port, exclusionList);
                    intent.putExtra("proxy", (Parcelable) proxyProperties);

                    onReceiveMethod.invoke(rec, appContext, intent);
                }
            }
        }
        return true;
    } catch (Exception e) {
        Log.w(TAG, "setWebViewProxy exception", e);
    }
    return false;
}
 
Example #21
Source File: MediaPlayer.java    From droidel with Apache License 2.0 4 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(Proxy.PROXY_CHANGE_ACTION)) {
        handleProxyBroadcast(intent);
    }
}
 
Example #22
Source File: ProxyChangeListener.java    From android-chromium with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(Proxy.PROXY_CHANGE_ACTION)) {
        proxySettingsChanged(extractNewProxy(intent));
    }
}
 
Example #23
Source File: ActivityThread.java    From AndroidComponentPlugin with Apache License 2.0 4 votes vote down vote up
public void setHttpProxy(String host, String port, String exclList) {
    Proxy.setHttpProxySystemProperty(host, port, exclList);
}
 
Example #24
Source File: NetworkUtil.java    From Android-Application-ZJB with Apache License 2.0 4 votes vote down vote up
public static String[] getProxyHostAndPort(Context context) {
    return getNetworkType(context) == NetworkType.WIFI ?
            new String[]{"", "-1"}
            : new String[]{Proxy.getDefaultHost(), "" + Proxy.getDefaultPort()};
}
 
Example #25
Source File: NetworkUtils.java    From Common with Apache License 2.0 4 votes vote down vote up
private static boolean isWapNetwork(NetworkInfo networkInfo) {
    return isMobileNetwork(networkInfo) && !TextUtils.isEmpty(Proxy.getDefaultHost());
}
 
Example #26
Source File: NetworkUtils.java    From letv with Apache License 2.0 4 votes vote down vote up
public static boolean isProxy() {
    return !TextUtils.isEmpty(Proxy.getDefaultHost());
}
 
Example #27
Source File: ActivityThread.java    From AndroidComponentPlugin with Apache License 2.0 4 votes vote down vote up
public void setHttpProxy(String host, String port, String exclList) {
    Proxy.setHttpProxySystemProperty(host, port, exclList);
}
 
Example #28
Source File: DownloadThread.java    From Alite with GNU General Public License v3.0 3 votes vote down vote up
/**
    * Returns the preferred proxy to be used by clients. This is a wrapper
    * around {@link android.net.Proxy#getHost()}. Currently no proxy will be
    * returned for localhost or if the active network is Wi-Fi.
    * 
    * @param context the context which will be passed to
    *            {@link android.net.Proxy#getHost()}
    * @param url the target URL for the request
    * @note Calling this method requires permission
    *       android.permission.ACCESS_NETWORK_STATE
    * @return The preferred proxy to be used by clients, or null if there is no
    *         proxy.
    */
   @SuppressWarnings("deprecation")
public HttpHost getPreferredHttpHost(Context context,
           String url) {
       if (!isLocalHost(url) && !mService.isWiFi()) {
           final String proxyHost = Proxy.getHost(context);
           if (proxyHost != null) {
               return new HttpHost(proxyHost, Proxy.getPort(context), "http");
           }
       }

       return null;
   }
 
Example #29
Source File: DownloadThread.java    From travelguide with Apache License 2.0 3 votes vote down vote up
/**
 * Returns the preferred proxy to be used by clients. This is a wrapper
 * around {@link android.net.Proxy#getHost()}. Currently no proxy will be
 * returned for localhost or if the active network is Wi-Fi.
 * 
 * @param context the context which will be passed to
 *            {@link android.net.Proxy#getHost()}
 * @param url the target URL for the request
 * @note Calling this method requires permission
 *       android.permission.ACCESS_NETWORK_STATE
 * @return The preferred proxy to be used by clients, or null if there is no
 *         proxy.
 */
public HttpHost getPreferredHttpHost(Context context,
        String url) {
    if (!isLocalHost(url) && !mService.isWiFi()) {
        final String proxyHost = Proxy.getHost(context);
        if (proxyHost != null) {
            return new HttpHost(proxyHost, Proxy.getPort(context), "http");
        }
    }

    return null;
}