Example usage for android.webkit CookieManager setAcceptFileSchemeCookies

List of usage examples for android.webkit CookieManager setAcceptFileSchemeCookies

Introduction

In this page you can find the example usage for android.webkit CookieManager setAcceptFileSchemeCookies.

Prototype


public static void setAcceptFileSchemeCookies(boolean accept) 

Source Link

Document

Sets whether the application's WebView instances should send and accept cookies for file scheme URLs.

Usage

From source file:org.runbuddy.libtomahawk.resolver.ScriptAccount.java

@SuppressLint({ "AddJavascriptInterface", "SetJavaScriptEnabled" })
public ScriptAccount(String path, boolean manuallyInstalled) {
    String prefix = manuallyInstalled ? "file://" : "file:///android_asset";
    mPath = prefix + path;//  w w  w  . j  a  v a 2 s .co m
    mManuallyInstalled = manuallyInstalled;
    String[] parts = mPath.split("/");
    mName = parts[parts.length - 1];
    InputStream inputStream = null;
    try {
        if (mManuallyInstalled) {
            File metadataFile = new File(path + File.separator + "content" + File.separator + "metadata.json");
            inputStream = new FileInputStream(metadataFile);
        } else {
            inputStream = TomahawkApp.getContext().getAssets()
                    .open(path.substring(1) + "/content/metadata.json");
        }
        String metadataString = IOUtils.toString(inputStream, Charsets.UTF_8);
        mMetaData = GsonHelper.get().fromJson(metadataString, ScriptResolverMetaData.class);
        if (mMetaData == null) {
            Log.e(TAG, "Couldn't read metadata.json. Cannot instantiate ScriptAccount.");
            return;
        }
    } catch (IOException e) {
        Log.e(TAG, "ScriptAccount: " + e.getClass() + ": " + e.getLocalizedMessage());
        Log.e(TAG, "Couldn't read metadata.json. Cannot instantiate ScriptAccount.");
        return;
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                Log.e(TAG, "ScriptAccount: " + e.getClass() + ": " + e.getLocalizedMessage());
            }
        }
    }

    CookieManager.setAcceptFileSchemeCookies(true);

    mWebView = new WebView(TomahawkApp.getContext());
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        CookieManager.getInstance().setAcceptThirdPartyCookies(mWebView, true);
    }
    WebSettings settings = mWebView.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setDatabaseEnabled(true);
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        //noinspection deprecation
        settings.setDatabasePath(TomahawkApp.getContext().getDir("databases", Context.MODE_PRIVATE).getPath());
    }
    settings.setDomStorageEnabled(true);
    mWebView.setWebChromeClient(new TomahawkWebChromeClient());
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        mWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        WebView.setWebContentsDebuggingEnabled(true);
    }

    new Handler(Looper.getMainLooper()).post(new Runnable() {
        @Override
        public void run() {
            //initalize WebView
            String data = "<!DOCTYPE html>" + "<html>" + "<head><title>" + mName + "</title></head>" + "<body>"
                    + "<script src=\"file:///android_asset/js/rsvp-latest.min.js"
                    + "\" type=\"text/javascript\"></script>"
                    + "<script src=\"file:///android_asset/js/cryptojs-core.js"
                    + "\" type=\"text/javascript\"></script>";
            if (mMetaData.manifest.scripts != null) {
                for (String scriptPath : mMetaData.manifest.scripts) {
                    data += "<script src=\"" + mPath + "/content/" + scriptPath
                            + "\" type=\"text/javascript\"></script>";
                }
            }
            try {
                String[] cryptoJsScripts = TomahawkApp.getContext().getAssets().list("js/cryptojs");
                for (String scriptPath : cryptoJsScripts) {
                    data += "<script src=\"file:///android_asset/js/cryptojs/" + scriptPath
                            + "\" type=\"text/javascript\"></script>";
                }
            } catch (IOException e) {
                Log.e(TAG, "ScriptResolver: " + e.getClass() + ": " + e.getLocalizedMessage());
            }
            data += "<script src=\"file:///android_asset/js/tomahawk_android_pre.js"
                    + "\" type=\"text/javascript\"></script>"
                    + "<script src=\"file:///android_asset/js/tomahawk.js"
                    + "\" type=\"text/javascript\"></script>"
                    + "<script src=\"file:///android_asset/js/tomahawk-infosystem.js"
                    + "\" type=\"text/javascript\"></script>"
                    + "<script src=\"file:///android_asset/js/tomahawk_android_post.js"
                    + "\" type=\"text/javascript\"></script>" + "<script src=\"" + mPath + "/content/"
                    + mMetaData.manifest.main + "\" type=\"text/javascript\"></script>" + "</body></html>";
            mWebView.setWebViewClient(new ScriptWebViewClient(ScriptAccount.this));
            mWebView.addJavascriptInterface(new ScriptInterface(ScriptAccount.this), SCRIPT_INTERFACE_NAME);
            mWebView.loadDataWithBaseURL("file:///android_asset/test.html", data, "text/html", null, null);
        }
    });
}