public CookieStore(SyncedStore sp) { super(); mSharedPreferences = sp; SyncedStore.Reader r = mSharedPreferences.read(); try { for (String k : r.keySet()) { if (k.startsWith(COOKIE_PREFIX) && k.endsWith(COOKIE_VALUE_TAG)) { String cookieName = k.substring(COOKIE_PREFIX_LEN, k.length() - COOKIE_VALUE_TAG_LEN); BasicClientCookie c = cookieFromPrefs(r, cookieName); super.addCookie(c); } } } finally { r.complete(); } }
private BasicClientCookie cookieFromPrefs(SyncedStore.Reader r, String cookieName) { String prefix = COOKIE_PREFIX + cookieName; BasicClientCookie c = null; String cookieValue = r.getString(prefix + "value", null); if (cookieValue == null) return null; String cookiePath = r.getString(prefix + "path", null); String cookieDomain = r.getString(prefix + "domain", null); String cookieExpiry = r.getString(prefix + "expiry", null); c = new BasicClientCookie(cookieName, cookieValue); c.setPath(cookiePath); c.setDomain(cookieDomain); c.setExpiryDate(dateFromString(cookieExpiry)); return c; }
@Override public synchronized void addCookie(Cookie cookie) { super.addCookie(cookie); String name = cookie.getName(); Cookie existing = null; SyncedStore.Reader r = mSharedPreferences.read(); try { existing = cookieFromPrefs(r, name); } finally { r.complete(); } if (existing == null || !existing.getValue().equals(cookie.getValue()) || !existing.getPath().equals(cookie.getPath()) || !existing.getDomain().equals(cookie.getDomain()) || !existing.getExpiryDate().equals(cookie.getExpiryDate())) { SyncedStore.Editor e = mSharedPreferences.edit(); try { // clear the existing cookie from prefs. String prefix = COOKIE_PREFIX + name; for (String k : e.keySet()) { if (k.startsWith(prefix)) { e.remove(k); } } // and then, if new cookie is persistent, then add it! if (cookie.getExpiryDate() != null) { cookieToPrefs(cookie, e); } } finally { e.commit(); } } }