All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sdklite.sphere.hybrid.HybridWebView Maven / Gradle / Ivy

package com.sdklite.sphere.hybrid;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.AttributeSet;
import android.webkit.WebSettings;
import android.webkit.WebView;

/**
 * Customized web view
 *
 * @author johnsonlee
 * @since 1.0.0
 */
public class HybridWebView extends WebView {

    public HybridWebView(Context context) {
        this(context, null);
    }

    public HybridWebView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HybridWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        final WebSettings settings = getSettings();
        final String appVersion = getVersionString(context);
        final String userAgent = settings.getUserAgentString();
        settings.setAppCacheEnabled(true);
        settings.setBuiltInZoomControls(false);
        settings.setDatabaseEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setJavaScriptEnabled(true);
        settings.setGeolocationEnabled(true);
        settings.setUserAgentString(userAgent + " " + appVersion);

        this.setWebViewClient(new HybridWebViewClient(this));
        this.setWebChromeClient(new HybridWebChromeClient(this));
        this.setDownloadListener(new HybridDownloadListener(this));
    }

    private String getVersionString(final Context context) {
        final PackageManager pm = context.getPackageManager();
        final String packageName = context.getPackageName();

        try {
            final PackageInfo pi = pm.getPackageInfo(packageName, 0);
            return packageName + "/" + pi.versionName;
        } catch (NameNotFoundException e) {
            return "";
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy