Example usage for android.webkit WebView getContext

List of usage examples for android.webkit WebView getContext

Introduction

In this page you can find the example usage for android.webkit WebView getContext.

Prototype

@ViewDebug.CapturedViewProperty
public final Context getContext() 

Source Link

Document

Returns the context the view is running in, through which it can access the current theme, resources, etc.

Usage

From source file:Main.java

public static void webViewLoadLocalJs(WebView view, String path) {
    String jsContent = assetFile2Str(view.getContext(), path);
    view.loadUrl("javascript:" + jsContent);
}

From source file:com.dwdesign.tweetings.util.WebViewProxySettings.java

public static void resetProxy(WebView webView) {
    final Context context = webView.getContext();
    try {//w w w.  j av a  2 s  .c  o m
        final Object requestQueueObject = getRequestQueue(context);
        if (requestQueueObject != null) {
            setDeclaredField(requestQueueObject, "mProxyHost", null);
        }
    } catch (final Exception e) {
        Log.e("ProxySettings", "Exception resetting WebKit proxy: " + e.toString());
    }
}

From source file:org.mozilla.focus.browser.LocalizedContent.java

/**
 * Load the content for focus:about//from w  ww . j av a2  s  . c o m
 */
private static void loadAbout(@NonNull final WebView webView) {
    final Context context = webView.getContext();
    final Resources resources = Locales.getLocalizedResources(context);

    final Map<String, String> substitutionMap = new ArrayMap<>();
    final String appName = context.getResources().getString(R.string.app_name);
    final String learnMoreURL = SupportUtils.getManifestoURL();

    String aboutVersion = "";
    try {
        final PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        aboutVersion = String.format("%s (Build #%s)", packageInfo.versionName, packageInfo.versionCode);
    } catch (PackageManager.NameNotFoundException e) {
        // Nothing to do if we can't find the package name.
    }
    substitutionMap.put("%about-version%", aboutVersion);

    final String aboutContent = resources.getString(R.string.about_content, appName, learnMoreURL);
    substitutionMap.put("%about-content%", aboutContent);

    final String wordmark = HtmlLoader.loadPngAsDataURI(context, R.drawable.wordmark);
    substitutionMap.put("%wordmark%", wordmark);

    putLayoutDirectionIntoMap(webView, substitutionMap);

    final String data = HtmlLoader.loadResourceFile(context, R.raw.about, substitutionMap);
    // We use a file:/// base URL so that we have the right origin to load file:/// css and image resources.
    webView.loadDataWithBaseURL("file:///android_res/raw/about.html", data, "text/html", "UTF-8", null);
}

From source file:org.mozilla.focus.browser.LocalizedContent.java

/**
 * Load the content for focus:rights/* w  w w .  jav a2  s .  co  m*/
 */
private static void loadRights(@NonNull final WebView webView) {
    final Context context = webView.getContext();
    final Resources resources = Locales.getLocalizedResources(context);

    final Map<String, String> substitutionMap = new ArrayMap<>();

    final String appName = context.getResources().getString(R.string.app_name);
    final String mplUrl = "https://www.mozilla.org/en-US/MPL/";
    final String trademarkPolicyUrl = "https://www.mozilla.org/foundation/trademarks/policy/";
    final String gplUrl = "gpl.html";
    final String trackingProtectionUrl = "https://wiki.mozilla.org/Security/Tracking_protection#Lists";
    final String licensesUrl = "licenses.html";

    final String content1 = resources.getString(R.string.your_rights_content1, appName);
    substitutionMap.put("%your-rights-content1%", content1);

    final String content2 = resources.getString(R.string.your_rights_content2, appName, mplUrl);
    substitutionMap.put("%your-rights-content2%", content2);

    final String content3 = resources.getString(R.string.your_rights_content3, appName, trademarkPolicyUrl);
    substitutionMap.put("%your-rights-content3%", content3);

    final String content4 = resources.getString(R.string.your_rights_content4, appName, licensesUrl);
    substitutionMap.put("%your-rights-content4%", content4);

    final String content5 = resources.getString(R.string.your_rights_content5, appName, gplUrl,
            trackingProtectionUrl);
    substitutionMap.put("%your-rights-content5%", content5);

    putLayoutDirectionIntoMap(webView, substitutionMap);

    final String data = HtmlLoader.loadResourceFile(context, R.raw.rights, substitutionMap);
    // We use a file:/// base URL so that we have the right origin to load file:/// css and image resources.
    webView.loadDataWithBaseURL("file:///android_asset/rights.html", data, "text/html", "UTF-8", null);
}

From source file:Main.java

@SuppressLint("NewApi")
@SuppressWarnings("all")
private static boolean setProxyKK(WebView webView, String host, int port, String applicationClassName) {
    Context appContext = webView.getContext().getApplicationContext();
    if (null == host) {
        System.clearProperty("http.proxyHost");
        System.clearProperty("http.proxyPort");
        System.clearProperty("https.proxyHost");
        System.clearProperty("https.proxyPort");
    } else {//from   w  w w  . ja va 2  s .  c o  m
        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(applicationClassName);
        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);
        Map receivers = (Map) receiversField.get(loadedApk);
        for (Object receiverMap : receivers.values()) {
            for (Object rec : ((Map) 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);
                }
            }
        }
        Log.d(LOG_TAG, "Setting proxy with >= 4.4 API successful!");
        return true;
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:com.dwdesign.tweetings.util.WebViewProxySettings.java

public static boolean setProxy(WebView webView, String host, int port) {
    final Context context = webView.getContext();
    // PSIPHON: added support for Android 4.x WebView proxy
    try {/*  w  w w .  j ava 2  s  . c om*/
        final Class<?> webViewCoreClass = Class.forName("android.webkit.WebViewCore");
        final Class<?> proxyPropertiesClass = Class.forName("android.net.ProxyProperties");
        if (webViewCoreClass != null && proxyPropertiesClass != null) {
            final Method m = webViewCoreClass.getDeclaredMethod("sendStaticMessage", Integer.TYPE,
                    Object.class);
            final Constructor<?> c = proxyPropertiesClass.getConstructor(String.class, Integer.TYPE,
                    String.class);

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

                // android.webkit.WebViewCore.EventHub.PROXY_CHANGED = 193;
                m.invoke(null, 193, properties);
                return true;
            }
        }
    } catch (final Exception e) {
        Log.e("ProxySettings",
                "Exception setting WebKit proxy through android.net.ProxyProperties: " + e.toString());
    }

    try {
        final Object requestQueueObject = getRequestQueue(context);
        if (requestQueueObject != null) {
            // Create Proxy config object and set it into request Q
            final HttpHost httpHost = new HttpHost(host, port, "http");
            setDeclaredField(requestQueueObject, "mProxyHost", httpHost);
            // Log.d("Webkit Setted Proxy to: " + host + ":" + port);
            return true;
        }
    } catch (final Exception e) {
        Log.e("ProxySettings",
                "Exception setting WebKit proxy through android.webkit.Network: " + e.toString());
    }

    return false;
}

From source file:com.appnexus.opensdk.ANJAMImplementation.java

private static void callExternalBrowser(WebView webView, Uri uri) {
    String urlParam = uri.getQueryParameter("url");

    if ((webView.getContext() == null) || (urlParam == null) || (!urlParam.startsWith("http"))) {
        return;/*  ww  w  . j av a2 s  .c om*/
    }
    try {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(Uri.decode(urlParam)));
        webView.getContext().startActivity(intent);
    } catch (ActivityNotFoundException e) {
        Toast.makeText(webView.getContext(), R.string.action_cant_be_completed, Toast.LENGTH_SHORT).show();
    }
}

From source file:com.appnexus.opensdk.ANJAMImplementation.java

private static void callMayDeepLink(WebView webView, Uri uri) {
    boolean mayDeepLink;
    String cb = uri.getQueryParameter("cb");
    String urlParam = uri.getQueryParameter("url");

    if ((webView.getContext() == null) || (webView.getContext().getPackageManager() == null)
            || (urlParam == null)) {//from w  ww.j a va  2  s . com
        mayDeepLink = false;
    } else {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(Uri.decode(urlParam)));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mayDeepLink = intent.resolveActivity(webView.getContext().getPackageManager()) != null;
    }

    LinkedList<BasicNameValuePair> list = new LinkedList<BasicNameValuePair>();
    list.add(new BasicNameValuePair(KEY_CALLER, CALL_MAYDEEPLINK));
    list.add(new BasicNameValuePair("mayDeepLink", String.valueOf(mayDeepLink)));
    loadResult(webView, cb, list);
}

From source file:com.appnexus.opensdk.ANJAMImplementation.java

private static void callDeepLink(WebView webView, Uri uri) {
    String cb = uri.getQueryParameter("cb");
    String urlParam = uri.getQueryParameter("url");

    LinkedList<BasicNameValuePair> list = new LinkedList<BasicNameValuePair>();
    list.add(new BasicNameValuePair(KEY_CALLER, CALL_DEEPLINK));

    if ((webView.getContext() == null) || (urlParam == null)) {
        loadResult(webView, cb, list);/*from  w w w.java 2 s .c o m*/
        return;
    }
    try {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(Uri.decode(urlParam)));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        webView.getContext().startActivity(intent);
    } catch (ActivityNotFoundException e) {
        loadResult(webView, cb, list);
    }
}

From source file:com.facebook.react.views.webview.ReactWebViewManager.java

private static void dispatchEvent(WebView webView, Event event) {
    ReactContext reactContext = (ReactContext) webView.getContext();
    EventDispatcher eventDispatcher = reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher();
    eventDispatcher.dispatchEvent(event);
}