Example usage for android.net Proxy PROXY_CHANGE_ACTION

List of usage examples for android.net Proxy PROXY_CHANGE_ACTION

Introduction

In this page you can find the example usage for android.net Proxy PROXY_CHANGE_ACTION.

Prototype

String PROXY_CHANGE_ACTION

To view the source code for android.net Proxy PROXY_CHANGE_ACTION.

Click Source Link

Document

Used to notify an app that's caching the proxy that either the default connection has changed or any connection's proxy has changed.

Usage

From source file:info.guardianproject.netcipher.webkit.WebkitProxy.java

@TargetApi(19)
private static boolean setKitKatProxy(String appClass, Context appContext, String host, int port) {
    //Context appContext = webView.getContext().getApplicationContext();

    if (host != null) {
        System.setProperty("http.proxyHost", host);
        System.setProperty("http.proxyPort", Integer.toString(port));
        System.setProperty("https.proxyHost", host);
        System.setProperty("https.proxyPort", Integer.toString(port));
    }/* www  . jav  a2 s. c  o m*/

    try {
        Class applictionCls = Class.forName(appClass);
        Field loadedApkField = applictionCls.getField("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);

                    if (host != null) {
                        /*********** 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);
                }
            }
        }
        return true;
    } catch (ClassNotFoundException e) {
        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
        String exceptionAsString = sw.toString();
        Log.v(TAG, e.getMessage());
        Log.v(TAG, exceptionAsString);
    } catch (NoSuchFieldException e) {
        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
        String exceptionAsString = sw.toString();
        Log.v(TAG, e.getMessage());
        Log.v(TAG, exceptionAsString);
    } catch (IllegalAccessException e) {
        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
        String exceptionAsString = sw.toString();
        Log.v(TAG, e.getMessage());
        Log.v(TAG, exceptionAsString);
    } catch (IllegalArgumentException e) {
        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
        String exceptionAsString = sw.toString();
        Log.v(TAG, e.getMessage());
        Log.v(TAG, exceptionAsString);
    } catch (NoSuchMethodException e) {
        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
        String exceptionAsString = sw.toString();
        Log.v(TAG, e.getMessage());
        Log.v(TAG, exceptionAsString);
    } catch (InvocationTargetException e) {
        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
        String exceptionAsString = sw.toString();
        Log.v(TAG, e.getMessage());
        Log.v(TAG, exceptionAsString);
    } catch (InstantiationException e) {
        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
        String exceptionAsString = sw.toString();
        Log.v(TAG, e.getMessage());
        Log.v(TAG, exceptionAsString);
    }
    return false;
}

From source file:info.guardianproject.netcipher.web.WebkitProxy.java

@TargetApi(21) // for android.util.ArrayMap methods
@SuppressWarnings("rawtypes")
private static boolean setWebkitProxyLollipop(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 {/*from  w w  w .j a va 2 s .c o m*/
        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);
                }
            }
        }
        return true;
    } catch (ClassNotFoundException e) {
        Log.d("ProxySettings",
                "Exception setting WebKit proxy on Lollipop through ProxyChangeListener: " + e.toString());
    } catch (NoSuchFieldException e) {
        Log.d("ProxySettings",
                "Exception setting WebKit proxy on Lollipop through ProxyChangeListener: " + e.toString());
    } catch (IllegalAccessException e) {
        Log.d("ProxySettings",
                "Exception setting WebKit proxy on Lollipop through ProxyChangeListener: " + e.toString());
    } catch (NoSuchMethodException e) {
        Log.d("ProxySettings",
                "Exception setting WebKit proxy on Lollipop through ProxyChangeListener: " + e.toString());
    } catch (InvocationTargetException e) {
        Log.d("ProxySettings",
                "Exception setting WebKit proxy on Lollipop through ProxyChangeListener: " + e.toString());
    }
    return false;
}

From source file:info.guardianproject.netcipher.webkit.WebkitProxy.java

@TargetApi(21) // for android.util.ArrayMap methods
@SuppressWarnings("rawtypes")
private static boolean setWebkitProxyLollipop(Context appContext, String host, int port) {
    System.setProperty("http.proxyHost", host);
    System.setProperty("http.proxyPort", Integer.toString(port));
    System.setProperty("https.proxyHost", host);
    System.setProperty("https.proxyPort", Integer.toString(port));
    try {//from   w ww.j a v a 2 s .c  om
        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);
                }
            }
        }
        return true;
    } catch (ClassNotFoundException e) {
        Log.d("ProxySettings",
                "Exception setting WebKit proxy on Lollipop through ProxyChangeListener: " + e.toString());
    } catch (NoSuchFieldException e) {
        Log.d("ProxySettings",
                "Exception setting WebKit proxy on Lollipop through ProxyChangeListener: " + e.toString());
    } catch (IllegalAccessException e) {
        Log.d("ProxySettings",
                "Exception setting WebKit proxy on Lollipop through ProxyChangeListener: " + e.toString());
    } catch (NoSuchMethodException e) {
        Log.d("ProxySettings",
                "Exception setting WebKit proxy on Lollipop through ProxyChangeListener: " + e.toString());
    } catch (InvocationTargetException e) {
        Log.d("ProxySettings",
                "Exception setting WebKit proxy on Lollipop through ProxyChangeListener: " + e.toString());
    }
    return false;
}

From source file:info.guardianproject.netcipher.web.WebkitProxy.java

private static boolean sendProxyChangedIntent(Context ctx, String host, int port) {

    try {//ww w.  ja va2s  .  c o  m
        Class proxyPropertiesClass = Class.forName("android.net.ProxyProperties");
        if (proxyPropertiesClass != null) {
            Constructor c = proxyPropertiesClass.getConstructor(String.class, Integer.TYPE, String.class);

            if (c != null) {
                c.setAccessible(true);
                Object properties = c.newInstance(host, port, null);

                Intent intent = new Intent(android.net.Proxy.PROXY_CHANGE_ACTION);
                intent.putExtra("proxy", (Parcelable) properties);
                ctx.sendBroadcast(intent);

            }

        }
    } catch (Exception e) {
        Log.e("ProxySettings", "Exception sending Intent ", e);
    } catch (Error e) {
        Log.e("ProxySettings", "Exception sending Intent ", e);
    }

    return false;

}