Explorar o código

[修改]更改SharedPreferences存取cookie为跨进程,更改错误清理cookie为指定游戏的host

zeki %!s(int64=4) %!d(string=hai) anos
pai
achega
ebf2a6031d

+ 19 - 24
app/src/main/java/com/sheep/gamegroup/module/webview/fragment/FgtWebX5.java

@@ -26,7 +26,6 @@ import android.widget.ProgressBar;
 import com.sheep.gamegroup.absBase.BaseActivity;
 import com.sheep.gamegroup.dateview.DateUtil;
 import com.sheep.gamegroup.event.WXLoginAuthEvent;
-import com.sheep.gamegroup.model.api.IWeb;
 import com.sheep.gamegroup.model.entity.WebParams;
 import com.sheep.gamegroup.util.*;
 import com.sheep.gamegroup.util.h5game.H5GameUtil;
@@ -58,7 +57,9 @@ import com.zhy.http.okhttp.callback.StringCallback;
 
 import java.io.File;
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 import butterknife.BindView;
@@ -468,7 +469,9 @@ public class FgtWebX5 extends BaseFgtWebX5 {
                 if (s.contains("page-error.css")) {
                     Uri uri = Uri.parse(getWebUrl());
                     String host = uri.getHost();
-                    CookieManager.getInstance().setCookie(host, "");
+                    List<String> hostList = new ArrayList<>();
+                    hostList.add(host);
+                    WebViewUtils.deleteWebViewCookiesForDomains(getContext(),hostList);
                     Log.d("!!!!!!s", "清理了cookie~~~");
                     AndroidSchedulers.mainThread().scheduleDirect(() -> {
                         loadJs("window.location.reload()");
@@ -726,28 +729,20 @@ public class FgtWebX5 extends BaseFgtWebX5 {
     //获取方块消消乐的本地用户信息
     private void getXXLGameUserInfo(boolean shouldLoad) {
         String js = "window.localStorage.getItem('userInfo');";
-        mWebView.evaluateJavascript(js, new ValueCallback<String>() {
-            @Override
-            public void onReceiveValue(String s) {
-                Log.d("!!!!!!!!", s);
-                if (!s.equals("null")) {
-                    SharedPreferencesUtil.putData(KEY_GAME_USER_INFO, s);
-                } else {
-                    if (shouldLoad) {
-                        String oldUserInfo = (String) SharedPreferencesUtil.getData(KEY_GAME_USER_INFO, "");
-                        if (oldUserInfo != null && !oldUserInfo.isEmpty()) {
-                            String jsUrl = "window.localStorage.setItem('userInfo','" + oldUserInfo + "')";
-                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
-                                mWebView.evaluateJavascript(jsUrl, new ValueCallback<String>() {
-                                    @Override
-                                    public void onReceiveValue(String s) {
-                                        mWebView.reload();
-                                    }
-                                });
-                            } else {
-                                mWebView.loadUrl(jsUrl);
-                                mWebView.reload();
-                            }
+        mWebView.evaluateJavascript(js, s -> {
+            Log.d("!!!!!!!!", s);
+            if (!s.equals("null")) {
+                SharedPreferencesUtil.putData(KEY_GAME_USER_INFO, s);
+            } else {
+                if (shouldLoad) {
+                    String oldUserInfo = (String) SharedPreferencesUtil.getData(KEY_GAME_USER_INFO, "");
+                    if (oldUserInfo != null && !oldUserInfo.isEmpty()) {
+                        String jsUrl = "window.localStorage.setItem('userInfo','" + oldUserInfo + "')";
+                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
+                            mWebView.evaluateJavascript(jsUrl, s1 -> mWebView.reload());
+                        } else {
+                            mWebView.loadUrl(jsUrl);
+                            mWebView.reload();
                         }
                     }
                 }

+ 55 - 0
app/src/main/java/com/sheep/gamegroup/module/webview/fragment/WebViewUtils.java

@@ -0,0 +1,55 @@
+package com.sheep.gamegroup.module.webview.fragment;
+
+import android.content.Context;
+import com.tencent.smtt.sdk.CookieManager;
+import com.tencent.smtt.sdk.CookieSyncManager;
+
+import java.util.Collection;
+
+/**
+ * @Author: ZEKI https://github.com/ZYF99
+ * @Date: 2021/6/1 1:58 下午
+ */
+public class WebViewUtils {
+    public static void deleteWebViewCookiesForDomains(Context context, Collection<String> domains) {
+        for (String d : domains) {
+            deleteWebViewCookiesForDomain(context, d, false);
+            deleteWebViewCookiesForDomain(context, d, true);
+        }
+    }
+
+    public static void deleteWebViewCookiesForDomain(Context context, String domain, boolean secure) {
+        CookieSyncManager csm = CookieSyncManager.createInstance(context);
+        CookieManager cm = CookieManager.getInstance();
+
+        /* http://code.google.com/p/android/issues/detail?id=19294 */
+        /*if (AndroidUtils.SDK_INT >= 11) {
+            // don't trim leading '.'s
+        } else {
+            *//* Trim leading '.'s *//*
+            if (domain.startsWith(".")) domain = domain.substring(1);
+        }*/
+
+        /* Cookies are stored by domain, and are not different for different schemes (i.e. http vs
+         * https) (although they do have an optional 'secure' flag.) */
+        domain = "http" + (secure ? "s" : "") + "://" + domain;
+        String cookieGlob = cm.getCookie(domain);
+        if (cookieGlob != null) {
+            String[] cookies = cookieGlob.split(";");
+            for (String cookieTuple : cookies) {
+                String[] cookieParts = cookieTuple.split("=");
+
+                /* setCookie has changed a lot between different versions of Android with respect to
+                 * how it handles cookies like these, which are set in order to clear an existing
+                 * cookie.  This way of invoking it seems to work on all versions. */
+                cm.setCookie(domain, cookieParts[0] + "=;");
+
+                /* These calls have worked for some subset of the the set of all versions of
+                 * Android:
+                 * cm.setCookie(domain, cookieParts[0] + "=");
+                 * cm.setCookie(domain, cookieParts[0]); */
+            }
+            csm.sync();
+        }
+    }
+}