import android.webkit.CookieManager; //导入方法依赖的package包/类
/**
* 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> headers) {
if (headers == null || headers.isEmpty()) return;
CookieManager cm = CookieManager.getInstance();
if (cm == null) {
LogUtil.i(Settings.TAG, "Unable to find a CookieManager");
return;
}
try {
String existingUUID = getExistingCookie();
for (Map.Entry> entry : headers.entrySet()) {
String key = entry.getKey();
// Only "Set-cookie" and "Set-cookie2" pair will be parsed
if (key != null && (key.equalsIgnoreCase(Settings.VERSION_ZERO_HEADER)
|| key.equalsIgnoreCase(Settings.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.COOKIE_DOMAIN, cookieStr);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
// CookieSyncManager is deprecated in API 21 Lollipop
CookieSyncManager.createInstance(context);
CookieSyncManager csm = CookieSyncManager.getInstance();
if (csm == null) {
LogUtil.i(Settings.TAG, "Unable to find a CookieSyncManager");
return;
}
csm.sync();
} else {
cm.flush();
}
}
}
}
}
}
} catch (IllegalStateException ise) {
} catch (Exception e) {
}
}