Example usage for android.webkit CookieSyncManager createInstance

List of usage examples for android.webkit CookieSyncManager createInstance

Introduction

In this page you can find the example usage for android.webkit CookieSyncManager createInstance.

Prototype

public static CookieSyncManager createInstance(Context context) 

Source Link

Document

Create a singleton CookieSyncManager within a context

Usage

From source file:com.codename1.impl.android.AndroidImplementation.java

public void addCookie(Cookie[] cs, boolean addToWebViewCookieManager, boolean sync) {
    if (addToWebViewCookieManager) {
        CookieManager mgr;/*from w ww. ja va  2 s.c  om*/
        CookieSyncManager syncer;
        try {
            syncer = CookieSyncManager.getInstance();
            mgr = getCookieManager();
        } catch (IllegalStateException ex) {
            syncer = CookieSyncManager.createInstance(this.getContext());
            mgr = getCookieManager();
        }
        java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss z");
        format.setTimeZone(TimeZone.getTimeZone("GMT"));

        for (Cookie c : cs) {
            String cookieString = c.getName() + "=" + c.getValue() + "; Domain=" + c.getDomain() + "; Path="
                    + c.getPath() + "; " + (c.isSecure() ? "Secure;" : "")
                    + (c.getExpires() != 0 ? (" Expires=" + format.format(new Date(c.getExpires())) + ";") : "")
                    + (c.isHttpOnly() ? "httpOnly;" : "");
            mgr.setCookie("http" + (c.isSecure() ? "s" : "") + "://" + c.getDomain() + c.getPath(),
                    cookieString);

        }

        if (sync) {
            syncer.sync();
        }
    }
    super.addCookie(cs);

}