Java Code Examples for android.webkit.CookieManager#flush()

The following examples show how to use android.webkit.CookieManager#flush() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WebViewSignInScene.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
@SuppressWarnings("deprecation")
@SuppressLint("SetJavaScriptEnabled")
public View onCreateView2(LayoutInflater inflater,
        @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    Context context = getContext2();
    AssertUtils.assertNotNull(context);

    EhUtils.signOut(context);

    // http://stackoverflow.com/questions/32284642/how-to-handle-an-uncatched-exception
    CookieManager cookieManager = CookieManager.getInstance();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        cookieManager.flush();
        cookieManager.removeAllCookies(null);
        cookieManager.removeSessionCookies(null);
    } else {
        CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(context);
        cookieSyncManager.startSync();
        cookieManager.removeAllCookie();
        cookieManager.removeSessionCookie();
        cookieSyncManager.stopSync();
    }

    mWebView = new WebView(context);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.setWebViewClient(new LoginWebViewClient());
    mWebView.loadUrl(EhUrl.URL_SIGN_IN);
    return mWebView;
}
 
Example 2
Source File: MyTagsActivity.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  // http://stackoverflow.com/questions/32284642/how-to-handle-an-uncatched-exception
  CookieManager cookieManager = CookieManager.getInstance();
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    cookieManager.flush();
    cookieManager.removeAllCookies(null);
    cookieManager.removeSessionCookies(null);
  } else {
    CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(this);
    cookieSyncManager.startSync();
    cookieManager.removeAllCookie();
    cookieManager.removeSessionCookie();
    cookieSyncManager.stopSync();
  }

  // Copy cookies from okhttp cookie store to CookieManager
  url = EhUrl.getMyTagsUrl();
  EhCookieStore store = EhApplication.getEhCookieStore(this);
  for (Cookie cookie : store.getCookies(HttpUrl.parse(url))) {
    cookieManager.setCookie(url, cookie.toString());
  }

  setContentView(R.layout.activity_my_tags);
  setNavigationIcon(R.drawable.v_arrow_left_dark_x24);
  webView = findViewById(R.id.webview);
  webView.getSettings().setJavaScriptEnabled(true);
  webView.setWebViewClient(new MyTagsWebViewClient());
  webView.setWebChromeClient(new DialogWebChromeClient(this));
  webView.loadUrl(url);
  progress = findViewById(R.id.progress);
}
 
Example 3
Source File: UConfigActivity.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  // http://stackoverflow.com/questions/32284642/how-to-handle-an-uncatched-exception
  CookieManager cookieManager = CookieManager.getInstance();
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    cookieManager.flush();
    cookieManager.removeAllCookies(null);
    cookieManager.removeSessionCookies(null);
  } else {
    CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(this);
    cookieSyncManager.startSync();
    cookieManager.removeAllCookie();
    cookieManager.removeSessionCookie();
    cookieSyncManager.stopSync();
  }

  // Copy cookies from okhttp cookie store to CookieManager
  url = EhUrl.getUConfigUrl();
  EhCookieStore store = EhApplication.getEhCookieStore(this);
  for (Cookie cookie : store.getCookies(HttpUrl.parse(url))) {
    cookieManager.setCookie(url, cookie.toString());
  }

  setContentView(R.layout.activity_u_config);
  setNavigationIcon(R.drawable.v_arrow_left_dark_x24);
  webView = (WebView) findViewById(R.id.webview);
  webView.getSettings().setJavaScriptEnabled(true);
  webView.setWebViewClient(new UConfigWebViewClient());
  webView.setWebChromeClient(new DialogWebChromeClient(this));
  webView.loadUrl(url);
  progress = (ProgressView) findViewById(R.id.progress);

  Snackbar.make(webView, R.string.apply_tip, Snackbar.LENGTH_LONG).show();
}
 
Example 4
Source File: WebViewActivity.java    From CloudReader with Apache License 2.0 5 votes vote down vote up
/**
     * 同步cookie
     */
    private void syncCookie(String url) {
        if (!TextUtils.isEmpty(url) && url.contains("wanandroid")) {
            try {
                CookieSyncManager.createInstance(this);
                CookieManager cookieManager = CookieManager.getInstance();
                cookieManager.setAcceptCookie(true);
                cookieManager.removeSessionCookie();// 移除
                cookieManager.removeAllCookie();
                String cookie = SPUtils.getString("cookie", "");
                if (!TextUtils.isEmpty(cookie)) {
                    String[] split = cookie.split(";");
                    for (int i = 0; i < split.length; i++) {
                        cookieManager.setCookie(url, split[i]);
                    }
                }
//            String cookies = cookieManager.getCookie(url);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    cookieManager.flush();
                } else {
                    CookieSyncManager.getInstance().sync();
                }
            } catch (Exception e) {
                DebugUtil.error("==异常==", e.toString());
            }
        }
    }
 
Example 5
Source File: TopSlidWebView.java    From MyBlogDemo with Apache License 2.0 5 votes vote down vote up
@SuppressLint("NewApi")
private void setCookie(String url) {
    URI uri = URI.create(url);

    if (TextUtils.isEmpty(uri.getHost())) {
        return;
    }

    if (Build.VERSION.SDK_INT < 21) {
        CookieSyncManager.createInstance(getContext());
    }

    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptCookie(true);
    cookieManager.removeSessionCookie();
    // 注入cookies
    List<String> cookies = getCookies();
    for (String cookie : cookies) {
        cookieManager.setCookie(uri.getHost(), cookie);
    }

    if (Build.VERSION.SDK_INT >= 21) {
        cookieManager.flush();
    } else {
        CookieSyncManager.getInstance().sync();
    }
}
 
Example 6
Source File: WebViewSignInScene.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
@SuppressWarnings("deprecation")
@SuppressLint("SetJavaScriptEnabled")
public View onCreateView2(LayoutInflater inflater,
        @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    Context context = getContext2();
    AssertUtils.assertNotNull(context);

    EhUtils.signOut(context);

    // http://stackoverflow.com/questions/32284642/how-to-handle-an-uncatched-exception
    CookieManager cookieManager = CookieManager.getInstance();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        cookieManager.flush();
        cookieManager.removeAllCookies(null);
        cookieManager.removeSessionCookies(null);
    } else {
        CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(context);
        cookieSyncManager.startSync();
        cookieManager.removeAllCookie();
        cookieManager.removeSessionCookie();
        cookieSyncManager.stopSync();
    }

    mWebView = new WebView(context);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.setWebViewClient(new LoginWebViewClient());
    mWebView.loadUrl(EhUrl.URL_SIGN_IN);
    return mWebView;
}
 
Example 7
Source File: MyTagsActivity.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  // http://stackoverflow.com/questions/32284642/how-to-handle-an-uncatched-exception
  CookieManager cookieManager = CookieManager.getInstance();
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    cookieManager.flush();
    cookieManager.removeAllCookies(null);
    cookieManager.removeSessionCookies(null);
  } else {
    CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(this);
    cookieSyncManager.startSync();
    cookieManager.removeAllCookie();
    cookieManager.removeSessionCookie();
    cookieSyncManager.stopSync();
  }

  // Copy cookies from okhttp cookie store to CookieManager
  url = EhUrl.getMyTagsUrl();
  EhCookieStore store = EhApplication.getEhCookieStore(this);
  for (Cookie cookie : store.getCookies(HttpUrl.parse(url))) {
    cookieManager.setCookie(url, cookie.toString());
  }

  setContentView(R.layout.activity_my_tags);
  setNavigationIcon(R.drawable.v_arrow_left_dark_x24);
  webView = findViewById(R.id.webview);
  webView.getSettings().setJavaScriptEnabled(true);
  webView.setWebViewClient(new MyTagsWebViewClient());
  webView.setWebChromeClient(new DialogWebChromeClient(this));
  webView.loadUrl(url);
  progress = findViewById(R.id.progress);
}
 
Example 8
Source File: UConfigActivity.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  // http://stackoverflow.com/questions/32284642/how-to-handle-an-uncatched-exception
  CookieManager cookieManager = CookieManager.getInstance();
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    cookieManager.flush();
    cookieManager.removeAllCookies(null);
    cookieManager.removeSessionCookies(null);
  } else {
    CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(this);
    cookieSyncManager.startSync();
    cookieManager.removeAllCookie();
    cookieManager.removeSessionCookie();
    cookieSyncManager.stopSync();
  }

  // Copy cookies from okhttp cookie store to CookieManager
  url = EhUrl.getUConfigUrl();
  EhCookieStore store = EhApplication.getEhCookieStore(this);
  for (Cookie cookie : store.getCookies(HttpUrl.parse(url))) {
    cookieManager.setCookie(url, cookie.toString());
  }

  setContentView(R.layout.activity_u_config);
  setNavigationIcon(R.drawable.v_arrow_left_dark_x24);
  webView = (WebView) findViewById(R.id.webview);
  webView.getSettings().setJavaScriptEnabled(true);
  webView.setWebViewClient(new UConfigWebViewClient());
  webView.setWebChromeClient(new DialogWebChromeClient(this));
  webView.loadUrl(url);
  progress = (ProgressView) findViewById(R.id.progress);

  Snackbar.make(webView, R.string.apply_tip, Snackbar.LENGTH_LONG).show();
}
 
Example 9
Source File: QunarWebActvity.java    From imsdk-android with MIT License 4 votes vote down vote up
public void synCookie() {

        //下面的变量都是暂时处理 日后逻辑清晰重新构成
        Boolean isHistory = mUrl.contains("main_controller");
        CookieSyncManager.createInstance(this);
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);
        if (isHistory) {
            cookieManager.removeAllCookie();
        }
        if (from != null && from.equals(Constants.BundleValue.UC_LOGIN)) {
            cookieManager.removeAllCookie();
        }
        if (!TextUtils.isEmpty(mUrl) && mUrl.contains(DOMAIN)) {
            String qvtStr = CurrentPreference.getInstance().getQvt();
            boolean qvt = false;
            if (!TextUtils.isEmpty(qvtStr) && !isHistory) {
                QVTResponseResult qvtResponseResult = JsonUtils.getGson().fromJson(qvtStr, QVTResponseResult.class);
                if (qvtResponseResult.ret) {
                    cookieManager.setCookie(DOMAIN, "_v=" + qvtResponseResult.data.vcookie + ";");
                    cookieManager.setCookie(DOMAIN, "_t=" + qvtResponseResult.data.tcookie + ";");
                    cookieManager.setCookie(DOMAIN, "_q=" + qvtResponseResult.data.qcookie + ";");
                    cookieManager.setCookie(DOMAIN, "q_d=" + QtalkNavicationService.getInstance().getXmppdomain() + ";");
                    qvt = true;
                }
            }
//            if (!isHistory) {
//                if (!qvt) {
//                    cookieManager.setCookie(DOMAIN, "_v=;");
//                    cookieManager.setCookie(DOMAIN, "_t=;");
//                    cookieManager.setCookie(DOMAIN, "_q=;");
//                    cookieManager.setCookie(DOMAIN, "q_d=;");
//                }
//            }
        }

        if (mUrl.contains("/package/plts/dashboard")) {
            cookieManager.setCookie(DOMAIN, "q_u=" + CurrentPreference.getInstance().getUserid() + ";");
            cookieManager.setCookie(DOMAIN, "q_nm=" + CurrentPreference.getInstance().getUserid() + ";");
            cookieManager.setCookie(DOMAIN, "q_d=" + QtalkNavicationService.getInstance().getXmppdomain() + ";");
        }

        if(isVideoAudioCall){
            cookieManager.setCookie(DOMAIN, "_caller=" + QtalkStringUtils.parseId(videoCaller) + ";");
            cookieManager.setCookie(DOMAIN, "_callee=" + QtalkStringUtils.parseId(videoCallee) + ";");
            cookieManager.setCookie(DOMAIN, "_calltime=" + System.currentTimeMillis() + ";");
            cookieManager.setCookie(DOMAIN, "u=" + CurrentPreference.getInstance().getUserid() + ";");
        }else {
            cookieManager.setCookie(DOMAIN, "_caller=;");
            cookieManager.setCookie(DOMAIN, "_callee=;");
            cookieManager.setCookie(DOMAIN, "_calltime=;");
            cookieManager.setCookie(DOMAIN, "u=;");
        }


        //默认种qckey
        cookieManager.setCookie(DOMAIN, "q_ckey=" + Protocol.getCKEY() + ";");

        if (QtalkSDK.getInstance().isLoginStatus()) {
            cookieManager.setCookie(DOMAIN, "_u=" + CurrentPreference.getInstance().getUserid() + ";");
            cookieManager.setCookie(DOMAIN, "q_u=" + CurrentPreference.getInstance().getUserid() + ";");
            cookieManager.setCookie(DOMAIN, "_k=" + CurrentPreference.getInstance().getVerifyKey() + ";");
            cookieManager.setCookie(DOMAIN, "q_d=" + QtalkNavicationService.getInstance().getXmppdomain() + ";");
        } else {
            cookieManager.setCookie(DOMAIN, "_u=;");
            cookieManager.setCookie(DOMAIN, "_k=;");
            cookieManager.setCookie(DOMAIN, "q_d=;");
        }

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            cookieManager.flush();
        } else {
            CookieSyncManager.getInstance().sync();
        }

        Logger.i("QunarWebActvity getCookie:"+ cookieManager.getCookie(DOMAIN));
    }
 
Example 10
Source File: PrebidServerAdapter.java    From prebid-mobile-android with Apache License 2.0 4 votes vote down vote up
/**
 * Synchronize the uuid2 cookie to the Webview Cookie Jar
 * This is only done if there is no present cookie.
 *
 * @param headers headers to extract cookies from for syncing
 */
@SuppressWarnings("deprecation")
private void httpCookieSync(Map<String, List<String>> headers) {
    if (headers == null || headers.isEmpty()) return;
    CookieManager cm = CookieManager.getInstance();
    if (cm == null) {
        LogUtil.i("PrebidNewAPI", "Unable to find a CookieManager");
        return;
    }
    try {
        String existingUUID = getExistingCookie();

        for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
            String key = entry.getKey();
            // Only "Set-cookie" and "Set-cookie2" pair will be parsed
            if (key != null && (key.equalsIgnoreCase(PrebidServerSettings.VERSION_ZERO_HEADER)
                    || key.equalsIgnoreCase(PrebidServerSettings.VERSION_ONE_HEADER))) {
                for (String cookieStr : entry.getValue()) {
                    if (!TextUtils.isEmpty(cookieStr) && cookieStr.contains(PrebidServerSettings.AN_UUID)) {
                        // pass uuid2 to WebView Cookie jar if it's empty or outdated
                        if (existingUUID == null || !cookieStr.contains(existingUUID)) {
                            cm.setCookie(PrebidServerSettings.COOKIE_DOMAIN, cookieStr);
                            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                                // CookieSyncManager is deprecated in API 21 Lollipop
                                CookieSyncManager.createInstance(PrebidMobile.getApplicationContext());
                                CookieSyncManager csm = CookieSyncManager.getInstance();
                                if (csm == null) {
                                    LogUtil.i("Unable to find a CookieSyncManager");
                                    return;
                                }
                                csm.sync();
                            } else {
                                cm.flush();
                            }
                        }
                    }
                }
            }
        }
    } catch (IllegalStateException ise) {
    } catch (Exception e) {
    }
}
 
Example 11
Source File: WebviewUtil.java    From mobile-sdk-android with Apache License 2.0 4 votes vote down vote up
/**
 * Synchronize the uuid2 cookie to the Webview Cookie Jar
 * This is only done if there is no present cookie.
 * @param headers headers to extract cookies from for syncing
 */
@SuppressWarnings("deprecation")
public static void cookieSync(Map<String, List<String>> headers) {
    if (headers == null || headers.isEmpty()) return;
    CookieManager cm = CookieManager.getInstance();

    if (cm == null) {
       // Logger.i(Logger.LOGTAG, "Unable to find a CookieManager");
        return;
    }
    try {
        String existingUUID = getExistingANUUID();

        for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
            String key = entry.getKey();
            // Only "Set-cookie" and "Set-cookie2" pair will be parsed
            if (key != null && (key.equalsIgnoreCase(VERSION_ZERO_HEADER)
                    || key.equalsIgnoreCase(VERSION_ONE_HEADER))) {
                for (String cookieStr : entry.getValue()) {
                    if (!TextUtils.isEmpty(cookieStr) && cookieStr.contains(Settings.AN_UUID)) {
                        // pass uuid2 to WebView Cookie jar if it's empty or outdated
                        if ((existingUUID == null || !cookieStr.contains(existingUUID))) {
                            cm.setCookie(Settings.getCookieDomain(), cookieStr);

                            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                                // CookieSyncManager is deprecated in API 21 Lollipop
                                CookieSyncManager csm = CookieSyncManager.getInstance();
                                if (csm == null) {
                                    Clog.i(Clog.httpRespLogTag,"Unable to find a CookieSyncManager");
                                    return;
                                }
                                csm.sync();
                            } else {
                                cm.flush();
                            }
                        }
                    }
                }
            }
        }
    } catch (IllegalStateException ise) {
    } catch (Exception e) {
    }
}