Skip to content

Commit

Permalink
added deleteCookie() and deleteCookies() on Android (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
pichillilorenzo committed Oct 25, 2018
1 parent 40dccfd commit 81a4b6a
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 14 deletions.
19 changes: 7 additions & 12 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import android.os.Build;
import android.util.Log;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.ValueCallback;

import java.net.HttpCookie;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
Expand Down Expand Up @@ -47,6 +50,16 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) {
case "getCookies":
result.success(MyCookieManager.getCookies((String) call.argument("url")));
break;
case "deleteCookie":
{
String url = (String) call.argument("url");
String name = (String) call.argument("name");
MyCookieManager.deleteCookie(url, name, result);
}
break;
case "deleteCookies":
MyCookieManager.deleteCookies(result);
break;
default:
result.notImplemented();
}
Expand All @@ -71,7 +84,7 @@ public static void setCookie(String url,
cookieValue += "; Path=" + path;

if (expiresDate != null)
cookieValue += "; Max-Age=" + expiresDate.toString();
cookieValue += "; Expires=" + getCookieExpirationDate(expiresDate);

if (isHTTPOnly != null && isHTTPOnly)
cookieValue += "; HttpOnly";
Expand All @@ -88,10 +101,15 @@ public void onReceiveValue(Boolean aBoolean) {
result.success(true);
}
});
cookieManager.flush();
}
else {
CookieSyncManager cookieSyncMngr = CookieSyncManager.createInstance(registrar.context());
cookieSyncMngr.startSync();
cookieManager.setCookie(url, cookieValue);
result.success(true);
cookieSyncMngr.stopSync();
cookieSyncMngr.sync();
}
}

Expand All @@ -115,4 +133,56 @@ public static List<Map<String, Object>> getCookies(final String url) {

}

public static void deleteCookie(String url, String cookieName, final MethodChannel.Result result) {

String cookieValue = cookieName + "=; Expires=Thu, 01 Jan 1970 00:00:01 GMT;";

CookieManager cookieManager = CookieManager.getInstance();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cookieManager.setCookie(url, cookieValue, new ValueCallback<Boolean>() {
@Override
public void onReceiveValue(Boolean aBoolean) {
result.success(true);
}
});
cookieManager.flush();
}
else {
CookieSyncManager cookieSyncMngr = CookieSyncManager.createInstance(registrar.context());
cookieSyncMngr.startSync();
cookieManager.setCookie(url, cookieValue);
result.success(true);
cookieSyncMngr.stopSync();
cookieSyncMngr.sync();
}
}

public static void deleteCookies(final MethodChannel.Result result) {
CookieManager cookieManager = CookieManager.getInstance();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cookieManager.removeAllCookies(new ValueCallback<Boolean>() {
@Override
public void onReceiveValue(Boolean aBoolean) {
result.success(true);
}
});
cookieManager.flush();
}
else {
CookieSyncManager cookieSyncMngr = CookieSyncManager.createInstance(registrar.context());
cookieSyncMngr.startSync();
cookieManager.removeAllCookie();
result.success(true);
cookieSyncMngr.stopSync();
cookieSyncMngr.sync();
}
}

public static String getCookieExpirationDate(Long timestamp) {
final SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy hh:mm:ss z");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
return sdf.format(new Date(timestamp));
}

}

0 comments on commit 81a4b6a

Please sign in to comment.