added deleteCookie() and deleteCookies() on Android (#8)

This commit is contained in:
pichillilorenzo 2018-10-26 01:55:05 +02:00
parent 40dccfde53
commit 81a4b6ad7f
2 changed files with 79 additions and 14 deletions

View File

@ -15,12 +15,8 @@
</component>
<component name="ChangeListManager">
<list default="true" id="9b41f7a2-a71e-4923-91fb-249d7815b3e7" name="Default" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserFlutterPlugin.java" beforeDir="false" afterPath="$PROJECT_DIR$/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserFlutterPlugin.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/MyCookieManager.java" beforeDir="false" afterPath="$PROJECT_DIR$/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/MyCookieManager.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/example/android/build.gradle" beforeDir="false" afterPath="$PROJECT_DIR$/example/android/build.gradle" afterDir="false" />
<change beforePath="$PROJECT_DIR$/example/lib/main.dart" beforeDir="false" afterPath="$PROJECT_DIR$/example/lib/main.dart" afterDir="false" />
<change beforePath="$PROJECT_DIR$/lib/flutter_inappbrowser.dart" beforeDir="false" afterPath="$PROJECT_DIR$/lib/flutter_inappbrowser.dart" afterDir="false" />
<change beforePath="$PROJECT_DIR$/ios/Classes/InAppBrowserWebViewController.swift" beforeDir="false" afterPath="$PROJECT_DIR$/ios/Classes/InAppBrowserWebViewController.swift" afterDir="false" />
</list>
<ignored path="$PROJECT_DIR$/.dart_tool/" />
<ignored path="$PROJECT_DIR$/.idea/" />
@ -40,8 +36,8 @@
<file leaf-file-name="flutter_inappbrowser.dart" pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/lib/flutter_inappbrowser.dart">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="542">
<caret line="1178" column="47" selection-start-line="1178" selection-start-column="38" selection-end-line="1178" selection-end-column="47" />
<state relative-caret-position="269">
<caret line="1179" column="42" lean-forward="true" selection-start-line="1179" selection-start-column="42" selection-end-line="1179" selection-end-column="42" />
<folding>
<element signature="e#814#831#0" expanded="true" />
</folding>
@ -152,7 +148,7 @@
</option>
</component>
<component name="ProjectFrameBounds">
<option name="x" value="2053" />
<option name="x" value="133" />
<option name="y" value="23" />
<option name="width" value="1787" />
<option name="height" value="1057" />
@ -349,8 +345,7 @@
<servers />
</component>
<component name="ToolWindowManager">
<frame x="2053" y="23" width="1787" height="1057" extended-state="0" />
<editor active="true" />
<frame x="133" y="23" width="1787" height="1057" extended-state="0" />
<layout>
<window_info anchor="bottom" id="Android Profiler" order="7" show_stripe_button="false" />
<window_info anchor="bottom" id="TODO" order="6" />
@ -664,8 +659,8 @@
</entry>
<entry file="file://$PROJECT_DIR$/lib/flutter_inappbrowser.dart">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="542">
<caret line="1178" column="47" selection-start-line="1178" selection-start-column="38" selection-end-line="1178" selection-end-column="47" />
<state relative-caret-position="269">
<caret line="1179" column="42" lean-forward="true" selection-start-line="1179" selection-start-column="42" selection-end-line="1179" selection-end-column="42" />
<folding>
<element signature="e#814#831#0" expanded="true" />
</folding>

View File

@ -3,13 +3,16 @@ package com.pichillilorenzo.flutter_inappbrowser;
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;
@ -47,6 +50,16 @@ public class MyCookieManager implements MethodChannel.MethodCallHandler {
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();
}
@ -71,7 +84,7 @@ public class MyCookieManager implements MethodChannel.MethodCallHandler {
cookieValue += "; Path=" + path;
if (expiresDate != null)
cookieValue += "; Max-Age=" + expiresDate.toString();
cookieValue += "; Expires=" + getCookieExpirationDate(expiresDate);
if (isHTTPOnly != null && isHTTPOnly)
cookieValue += "; HttpOnly";
@ -88,10 +101,15 @@ public class MyCookieManager implements MethodChannel.MethodCallHandler {
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();
}
}
@ -115,4 +133,56 @@ public class MyCookieManager implements MethodChannel.MethodCallHandler {
}
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));
}
}