Android Context Set setWebkitProxyGingerbread(Context ctx, String host, int port)

Here you can find the source of setWebkitProxyGingerbread(Context ctx, String host, int port)

Description

Override WebKit Proxy settings

Parameter

Parameter Description
ctx Android ApplicationContext
host a parameter
port a parameter

Return

true if Proxy was successfully set

Declaration

private static boolean setWebkitProxyGingerbread(Context ctx,
        String host, int port) throws Exception 

Method Source Code

//package com.java2s;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import org.apache.http.HttpHost;

import android.content.Context;

public class Main {
    /**// w  w w  .  j a  v a 2 s .  com
     * Override WebKit Proxy settings
     *
     * @param ctx Android ApplicationContext
     * @param host
     * @param port
     * @return  true if Proxy was successfully set
     */
    private static boolean setWebkitProxyGingerbread(Context ctx,
            String host, int port) throws Exception {
        boolean ret = false;

        Object requestQueueObject = getRequestQueue(ctx);
        if (requestQueueObject != null) {
            //Create Proxy config object and set it into request Q
            HttpHost httpHost = new HttpHost(host, port, "http");
            setDeclaredField(requestQueueObject, "mProxyHost", httpHost);
            return true;
        }
        return false;

    }

    public static Object getRequestQueue(Context ctx) throws Exception {
        Object ret = null;
        Class networkClass = Class.forName("android.webkit.Network");
        if (networkClass != null) {
            Object networkObj = invokeMethod(networkClass, "getInstance",
                    new Object[] { ctx }, Context.class);
            if (networkObj != null) {
                ret = getDeclaredField(networkObj, "mRequestQueue");
            }
        }
        return ret;
    }

    private static void setDeclaredField(Object obj, String name,
            Object value) throws SecurityException, NoSuchFieldException,
            IllegalArgumentException, IllegalAccessException {
        Field f = obj.getClass().getDeclaredField(name);
        f.setAccessible(true);
        f.set(obj, value);
    }

    private static Object invokeMethod(Object object, String methodName,
            Object[] params, Class... types) throws Exception {
        Object out = null;
        Class c = object instanceof Class ? (Class) object : object
                .getClass();
        if (types != null) {
            Method method = c.getMethod(methodName, types);
            out = method.invoke(object, params);
        } else {
            Method method = c.getMethod(methodName);
            out = method.invoke(object);
        }
        //System.out.println(object.getClass().getName() + "." + methodName + "() = "+ out);
        return out;
    }

    private static Object getDeclaredField(Object obj, String name)
            throws SecurityException, NoSuchFieldException,
            IllegalArgumentException, IllegalAccessException {
        Field f = obj.getClass().getDeclaredField(name);
        f.setAccessible(true);
        Object out = f.get(obj);
        //System.out.println(obj.getClass().getName() + "." + name + " = "+ out);
        return out;
    }
}

Related

  1. setDefaultLocale(Context context, String locale)
  2. setLanguage(Context context, String language)
  3. setProxy(Context ctx)
  4. setProxy(Context ctx, String host, int port)
  5. setRevokedPermissions(String packageName, String[] permissions, Context ctx)
  6. setWebkitProxyICS(Context ctx, String host, int port)
  7. setWelcomeShown(final Context ctx)