Set Proxy for Android 4.1 and above. - Android Network

Android examples for Network:Proxy

Description

Set Proxy for Android 4.1 and above.

Demo Code

/*/* ww w.  j  ava 2s.c  o  m*/
 * Copyright (C) 2013 Daniel Velazco
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//package com.java2s;

import android.util.Log;
import android.webkit.WebView;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;

import java.lang.reflect.Method;

public class Main {
    private static final String LOG_TAG = "WebViewProxyUtil";

    /**
     * Set Proxy for Android 4.1 and above.
     */
    @SuppressWarnings("all")
    private static boolean setProxyJBPlus(WebView webview, String host,
            int port) {
        Log.d(LOG_TAG, "Setting proxy with >= 4.1 API.");

        try {
            Class wvcClass = Class.forName("android.webkit.WebViewClassic");
            Class wvParams[] = new Class[1];
            wvParams[0] = Class.forName("android.webkit.WebView");
            Method fromWebView = wvcClass.getDeclaredMethod("fromWebView",
                    wvParams);
            Object webViewClassic = fromWebView.invoke(null, webview);

            Class wv = Class.forName("android.webkit.WebViewClassic");
            Field mWebViewCoreField = wv.getDeclaredField("mWebViewCore");
            Object mWebViewCoreFieldInstance = getFieldValueSafely(
                    mWebViewCoreField, webViewClassic);

            Class wvc = Class.forName("android.webkit.WebViewCore");
            Field mBrowserFrameField = wvc
                    .getDeclaredField("mBrowserFrame");
            Object mBrowserFrame = getFieldValueSafely(mBrowserFrameField,
                    mWebViewCoreFieldInstance);

            Class bf = Class.forName("android.webkit.BrowserFrame");
            Field sJavaBridgeField = bf.getDeclaredField("sJavaBridge");
            Object sJavaBridge = getFieldValueSafely(sJavaBridgeField,
                    mBrowserFrame);

            Class ppclass = Class.forName("android.net.ProxyProperties");
            Class pparams[] = new Class[3];
            pparams[0] = String.class;
            pparams[1] = int.class;
            pparams[2] = String.class;
            Constructor ppcont = ppclass.getConstructor(pparams);

            Class jwcjb = Class
                    .forName("android.webkit.JWebCoreJavaBridge");
            Class params[] = new Class[1];
            params[0] = Class.forName("android.net.ProxyProperties");
            Method updateProxyInstance = jwcjb.getDeclaredMethod(
                    "updateProxy", params);

            updateProxyInstance.invoke(sJavaBridge,
                    ppcont.newInstance(host, port, null));
        } catch (Exception ex) {
            Log.e(LOG_TAG,
                    "Setting proxy with >= 4.1 API failed with error: "
                            + ex.getMessage());
            return false;
        }

        Log.d(LOG_TAG, "Setting proxy with >= 4.1 API successful!");
        return true;
    }

    private static Object getFieldValueSafely(Field field,
            Object classInstance) throws IllegalArgumentException,
            IllegalAccessException {
        boolean oldAccessibleValue = field.isAccessible();
        field.setAccessible(true);
        Object result = field.get(classInstance);
        field.setAccessible(oldAccessibleValue);
        return result;
    }
}

Related Tutorials