Przeglądaj źródła

[修改]修复消消乐每次进入都是新手

zeki 5 lat temu
rodzic
commit
89fbc1df80

+ 54 - 11
app/src/main/java/com/sheep/gamegroup/module/webview/fragment/FgtWebX5.java

@@ -4,6 +4,7 @@ import android.annotation.SuppressLint;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
+import android.content.SharedPreferences;
 import android.graphics.Bitmap;
 import android.net.Uri;
 import android.os.Build;
@@ -14,6 +15,7 @@ import android.support.v7.app.ActionBar;
 import android.support.v7.app.AlertDialog;
 import android.text.TextUtils;
 import android.util.DisplayMetrics;
+import android.util.Log;
 import android.view.View;
 import android.view.ViewGroup;
 import android.view.animation.Animation;
@@ -101,6 +103,7 @@ public class FgtWebX5 extends BaseFgtWebX5 {
     public void initView() {
 //        mWebView = new WebView(SheepApp.getInstance());
         mWebView = new WebView(getActivity());
+        //getSavedCookies();
         webContainer.addView(mWebView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
 //        ViewUtil.setImage(act_web_loading_iv, R.drawable.gif_sheep_loading);
         ViewUtil.setGif(act_web_loading_iv, R.drawable.gif_sheep_loading);
@@ -172,9 +175,8 @@ public class FgtWebX5 extends BaseFgtWebX5 {
 
         webView.setClickable(true);
         WebSettings webSetting = webView.getSettings();
-        webSetting.setJavaScriptEnabled(true);
-        webSetting.setJavaScriptCanOpenWindowsAutomatically(true);
-        webSetting.setAllowFileAccess(true);
+
+
         //webSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
         //自适应屏幕
         WebSettings.ZoomDensity zoomDensity = WebSettings.ZoomDensity.MEDIUM;
@@ -189,7 +191,19 @@ public class FgtWebX5 extends BaseFgtWebX5 {
                 zoomDensity = WebSettings.ZoomDensity.FAR;
                 break;
         }
+        webSetting.setJavaScriptEnabled(true);
+        webSetting.setGeolocationEnabled(true);
+        webSetting.setJavaScriptCanOpenWindowsAutomatically(true);
         webSetting.setDefaultZoom(zoomDensity);
+        webSetting.setSupportMultipleWindows(true);
+        webSetting.setDomStorageEnabled(true);
+        webSetting.setAppCacheMaxSize(1024 * 1024 * 8);
+        String appCachePath = SheepApp.getInstance().getApplicationContext().getCacheDir().getAbsolutePath();
+        webSetting.setAppCachePath(appCachePath);
+        webSetting.setAllowFileAccess(true);
+        webSetting.setAppCacheEnabled(true);
+        webSetting.setDatabaseEnabled(true);
+        webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND);
         /*webSetting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
         webSetting.setLoadWithOverviewMode(true);
         webSetting.setTextZoom(100);
@@ -197,16 +211,10 @@ public class FgtWebX5 extends BaseFgtWebX5 {
         webSetting.setSupportZoom(false);*/
 //        webSetting.setSupportZoom(true);
 //        webSetting.setBuiltInZoomControls(true);
-
-        webSetting.setSupportMultipleWindows(true);
         // webSetting.setLoadWithOverviewMode(true);
-        webSetting.setAppCacheEnabled(true);
 //        webSetting.setAppCacheMaxSize(Long.MAX_VALUE);
         // webSetting.setDatabaseEnabled(true);
-        webSetting.setDomStorageEnabled(true);
-        webSetting.setGeolocationEnabled(true);
         // webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);
-        webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND);
         // webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH);
 //        webSetting.setCacheMode(NetUtil.CAN_CONNECT_SERVICE ? WebSettings.LOAD_DEFAULT : WebSettings.LOAD_CACHE_ELSE_NETWORK);
 
@@ -234,6 +242,7 @@ public class FgtWebX5 extends BaseFgtWebX5 {
     public void onDetach() {
         if (mWebView != null) {
             try {
+                getGameUserInfo(false);
                 if (mWebView.getParent() != null) {
                     ((ViewGroup) mWebView.getParent()).removeView(mWebView);
                 }
@@ -485,6 +494,7 @@ public class FgtWebX5 extends BaseFgtWebX5 {
                 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                     CookieSyncManager.getInstance().sync();
                 }
+                getGameUserInfo(true);
                 loadJs();
             }
         });
@@ -638,8 +648,8 @@ public class FgtWebX5 extends BaseFgtWebX5 {
 
     // 注入js函数监听
     public void loadJs() {
-        if (TextUtils.isEmpty(js))
-            return;
+
+        if (TextUtils.isEmpty(js)) return;
         ViewUtil.loadJs(mWebView, getJs());
         LogUtil.println("ActWebX5", "loadJs", js);
     }
@@ -652,6 +662,39 @@ public class FgtWebX5 extends BaseFgtWebX5 {
         return BuildConfig.DEBUG ? testJsStart + "\n" + js : releaseJsStart + "\n" + js;
     }
 
+    private static final String KEY_GAME_USER_INFO = "key_game_user_info";
+
+    private void getGameUserInfo(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();
+                            }
+                        }
+                    }
+                }
+            }
+        });
+    }
+
     @Override
     public boolean isResponseBack() {
         return responseBack;

+ 2 - 0
app/src/main/java/com/sheep/gamegroup/util/Jump2View.java

@@ -1048,10 +1048,12 @@ public class Jump2View {
         }
         Intent intent;
         if (SheepApp.getInstance().shouldOpenWebInNewProcess) {
+            //intent = new Intent(context, ActWebX5.class);
             intent = new Intent(context, ActWebX5NewProcess.class);
         } else {
             intent = new Intent(context, ActWebX5.class);
         }
+
         intent.putExtra(WebParams.class.getSimpleName(), webParams);
         if (needNewTask) {
             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

+ 256 - 0
app/src/main/java/com/sheep/gamegroup/util/SharedPreferencesUtil.java

@@ -0,0 +1,256 @@
+package com.sheep.gamegroup.util;
+
+import android.content.Context;
+import android.content.SharedPreferences;
+import android.util.Log;
+
+import com.google.gson.Gson;
+import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+public class SharedPreferencesUtil {
+
+    private static SharedPreferencesUtil util;
+    private static SharedPreferences sp;
+
+    private SharedPreferencesUtil(Context context, String name) {
+        sp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
+    }
+
+    /**
+     * 初始化SharedPreferencesUtil,只需要初始化一次,建议在Application中初始化
+     * @param context 上下文对象
+     * @param name    SharedPreferences Name
+     */
+    public static void getInstance(Context context, String name) {
+        if (util == null) {
+            util = new SharedPreferencesUtil(context, name);
+        }
+    }
+
+    /**
+     * 保存数据到SharedPreferences
+     *
+     * @param key   键
+     * @param value 需要保存的数据
+     * @return 保存结果
+     */
+    public static boolean putData(String key, Object value) {
+        boolean result;
+        SharedPreferences.Editor editor = sp.edit();
+        String type = value.getClass().getSimpleName();
+        try {
+            switch (type) {
+                case "Boolean":
+                    editor.putBoolean(key, (Boolean) value);
+                    break;
+                case "Long":
+                    editor.putLong(key, (Long) value);
+                    break;
+                case "Float":
+                    editor.putFloat(key, (Float) value);
+                    break;
+                case "String":
+                    editor.putString(key, (String) value);
+                    break;
+                case "Integer":
+                    editor.putInt(key, (Integer) value);
+                    break;
+                default:
+                    Gson gson = new Gson();
+                    String json = gson.toJson(value);
+                    editor.putString(key, json);
+                    break;
+            }
+            result = true;
+        } catch (Exception e) {
+            result = false;
+            e.printStackTrace();
+        }
+        editor.apply();
+        return result;
+    }
+
+    /**
+     * 获取SharedPreferences中保存的数据
+     * @param key          键
+     * @param defaultValue 获取失败默认值
+     * @return 从SharedPreferences读取的数据
+     */
+    public static Object getData(String key, Object defaultValue) {
+        Object result;
+        String type = defaultValue.getClass().getSimpleName();
+        try {
+            switch (type) {
+                case "Boolean":
+                    result = sp.getBoolean(key, (Boolean) defaultValue);
+                    break;
+                case "Long":
+                    result = sp.getLong(key, (Long) defaultValue);
+                    break;
+                case "Float":
+                    result = sp.getFloat(key, (Float) defaultValue);
+                    break;
+                case "String":
+                    result = sp.getString(key, (String) defaultValue);
+                    break;
+                case "Integer":
+                    result = sp.getInt(key, (Integer) defaultValue);
+                    break;
+                default:
+                    Gson gson = new Gson();
+                    String json = sp.getString(key, "");
+                    if (!json.equals("") && json.length() > 0) {
+                        result = gson.fromJson(json, defaultValue.getClass());
+                    } else {
+                        result = defaultValue;
+                    }
+                    break;
+            }
+        } catch (Exception e) {
+            result = null;
+            e.printStackTrace();
+        }
+        return result;
+    }
+
+    /**
+     * 用于保存集合
+     *
+     * @param key  key
+     * @param list 集合数据
+     * @return 保存结果
+     */
+    public static <T> boolean putListData(String key, List<T> list) {
+        boolean result;
+        String type = list.get(0).getClass().getSimpleName();
+        SharedPreferences.Editor editor = sp.edit();
+        JsonArray array = new JsonArray();
+        try {
+            switch (type) {
+                case "Boolean":
+                    for (int i = 0; i < list.size(); i++) {
+                        array.add((Boolean) list.get(i));
+                    }
+                    break;
+                case "Long":
+                    for (int i = 0; i < list.size(); i++) {
+                        array.add((Long) list.get(i));
+                    }
+                    break;
+                case "Float":
+                    for (int i = 0; i < list.size(); i++) {
+                        array.add((Float) list.get(i));
+                    }
+                    break;
+                case "String":
+                    for (int i = 0; i < list.size(); i++) {
+                        array.add((String) list.get(i));
+                    }
+                    break;
+                case "Integer":
+                    for (int i = 0; i < list.size(); i++) {
+                        array.add((Integer) list.get(i));
+                    }
+                    break;
+                default:
+                    Gson gson = new Gson();
+                    for (int i = 0; i < list.size(); i++) {
+                        JsonElement obj = gson.toJsonTree(list.get(i));
+                        array.add(obj);
+                    }
+                    break;
+            }
+            editor.putString(key, array.toString());
+            result = true;
+        } catch (Exception e) {
+            result = false;
+            e.printStackTrace();
+        }
+        editor.apply();
+        return result;
+    }
+
+    /**
+     * 获取保存的List
+     *
+     * @param key key
+     * @return 对应的Lis集合
+     */
+    public static <T> List<T> getListData(String key, Class<T> cls) {
+        List<T> list = new ArrayList<>();
+        String json = sp.getString(key, "");
+        if (!json.equals("") && json.length() > 0) {
+            Gson gson = new Gson();
+            JsonArray array = new JsonParser().parse(json).getAsJsonArray();
+            for (JsonElement elem : array) {
+                list.add(gson.fromJson(elem, cls));
+            }
+        }
+        return list;
+    }
+
+    /**
+     * 用于保存集合
+     *
+     * @param key key
+     * @param map map数据
+     * @return 保存结果
+     */
+    public static <K, V> boolean putHashMapData(String key, Map<K, V> map) {
+        boolean result;
+        SharedPreferences.Editor editor = sp.edit();
+        try {
+            Gson gson = new Gson();
+            String json = gson.toJson(map);
+            editor.putString(key, json);
+            result = true;
+        } catch (Exception e) {
+            result = false;
+            e.printStackTrace();
+        }
+        editor.apply();
+        return result;
+    }
+
+    /**
+     * 用于保存集合
+     *
+     * @param key key
+     * @return HashMap
+     */
+    public static <V> HashMap<String, V> getHashMapData(String key, Class<V> clsV) {
+        String json = sp.getString(key, "");
+        HashMap<String, V> map = new HashMap<>();
+        Gson gson = new Gson();
+        JsonObject obj = new JsonParser().parse(json).getAsJsonObject();
+        Set<Map.Entry<String, JsonElement>> entrySet = obj.entrySet();
+        for (Map.Entry<String, JsonElement> entry : entrySet) {
+            String entryKey = entry.getKey();
+            JsonObject value = (JsonObject) entry.getValue();
+            map.put(entryKey, gson.fromJson(value, clsV));
+        }
+        Log.e("SharedPreferencesUtil", obj.toString());
+        return map;
+    }
+    /**
+     * @Author AlanMa
+     * @Description 清除数据
+     * @Date 2020/4/26
+     * @Param [key]
+     * @return void
+     */
+    public static void cleanByKey(String key){
+        SharedPreferences.Editor editor = sp.edit();
+        editor.remove(key);
+        editor.commit();
+    }
+}

+ 0 - 2
app/src/main/java/com/sheep/gamegroup/view/activity/ActWebX5NewProcess.java

@@ -4,7 +4,5 @@ public class ActWebX5NewProcess extends ActWebX5{
     @Override
     protected void onDestroy() {
         super.onDestroy();
-        //杀掉当前进程
-        android.os.Process.killProcess(android.os.Process.myPid());
     }
 }

+ 0 - 2
app/src/main/java/com/sheep/gamegroup/view/activity/SplashAct.java

@@ -185,10 +185,8 @@ public class SplashAct extends BaseActivity {
                     public void onFail(int i) {
                         if (BuildConfig.DEBUG)
                             Log.d("!!!!!Fail", String.valueOf(i));
-                        if (i == KLSplashAdEvent_Timeout) {
                             //拉取广告错误或超时,直接跳过
                             sendHandlerMessages(0);
-                        }
                     }
 
                     @Override

+ 10 - 22
app/src/main/java/com/sheep/jiuyan/samllsheep/SheepApp.java

@@ -37,24 +37,7 @@ import com.sheep.gamegroup.module.login.LoginAct;
 import com.sheep.gamegroup.module.qrcode.FaceWithQRActivity;
 import com.sheep.gamegroup.module.skin.util.SkinUtil;
 import com.sheep.gamegroup.usage.AppUsageManager;
-import com.sheep.gamegroup.util.ActionUtil;
-import com.sheep.gamegroup.util.AppUtil;
-import com.sheep.gamegroup.util.CertificationUtil;
-import com.sheep.gamegroup.util.ChannelContent;
-import com.sheep.gamegroup.util.CommonUtil;
-import com.sheep.gamegroup.util.ConfigUtil;
-import com.sheep.gamegroup.util.ConnectAddress;
-import com.sheep.gamegroup.util.DataKey;
-import com.sheep.gamegroup.util.DataUtil;
-import com.sheep.gamegroup.util.Jump2View;
-import com.sheep.gamegroup.util.LogUtil;
-import com.sheep.gamegroup.util.MyFileNameGenerator;
-import com.sheep.gamegroup.util.NetUtil;
-import com.sheep.gamegroup.util.RefreshUtil;
-import com.sheep.gamegroup.util.SheepDownloadConnection;
-import com.sheep.gamegroup.util.SysAppUtil;
-import com.sheep.gamegroup.util.TextToSpeechUtil;
-import com.sheep.gamegroup.util.UMConfigUtils;
+import com.sheep.gamegroup.util.*;
 import com.sheep.gamegroup.view.activity.*;
 import com.sheep.jiuyan.samllsheep.service.AutoCheckService;
 import com.sheep.jiuyan.samllsheep.service.DownloadService;
@@ -207,12 +190,13 @@ public class SheepApp extends MultiDexApplication {
     public void onCreate() {
 //        configStrictMode();
         super.onCreate();
-        initWebViewDataDirectory(this);
-        if (AppUtil.isMainProcess(this) || AppUtil.isWebProcess(this)) {
+        SharedPreferencesUtil.getInstance(this,"cache");
+        mSheepApp = this;
+        connectAddress = ConnectAddress.sheep.getDefaultConnectAddress();
+        if (AppUtil.isMainProcess(this)) {
             boolean isNotSupport = Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN_MR1;
             ContextHolder.setContext(this);
-            mSheepApp = this;
-            connectAddress = ConnectAddress.sheep.getDefaultConnectAddress();
+
             if (!isNotSupport) {
                 registerActivityLifecycleCallbacks(activityLifecycleCallbacks);
             }
@@ -247,6 +231,9 @@ public class SheepApp extends MultiDexApplication {
     public static void initWebViewDataDirectory(Context context) {
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
             String processName = getProcessName();
+            Log.d("!!!大的进程名",context.getPackageName());
+            Log.d("!!!当前的进程名",processName);
+
             if (!context.getPackageName().equals(processName)) {//判断是否是默认进程名称
                 WebView.setDataDirectorySuffix(processName);
                 android.webkit.WebView.setDataDirectorySuffix(processName);
@@ -799,6 +786,7 @@ public class SheepApp extends MultiDexApplication {
     protected void attachBaseContext(Context base) {
         super.attachBaseContext(base);
         MultiDex.install(this);
+        initWebViewDataDirectory(this);
 //        PluginManager.getInstance(base).init();//插件化框架
     }
 

+ 4 - 0
app/src/main/res/drawable/bg_top_corner_8dp.xml

@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
+    <corners android:topLeftRadius="8dp" android:topRightRadius="8dp"/>
+</shape>