From 96640ad91e3b8a200f974dbe259ede0efcd7f215 Mon Sep 17 00:00:00 2001 From: juntao Date: Sun, 12 Sep 2021 16:44:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=95=BF=E6=8C=89=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E6=A0=87=E7=AD=BE=E4=B8=8D=E8=83=BD=E4=BF=9D=E5=AD=98?= =?UTF-8?q?=E9=83=A8=E5=88=86=E9=A1=B5=E9=9D=A2=E5=BF=AB=E6=8D=B7=E8=B7=B3?= =?UTF-8?q?=E8=BD=AC=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/gh/base/BaseActivity.java | 4 - .../com/gh/common/util/EntranceUtils.java | 123 ++++++++++++------ 2 files changed, 84 insertions(+), 43 deletions(-) diff --git a/app/src/main/java/com/gh/base/BaseActivity.java b/app/src/main/java/com/gh/base/BaseActivity.java index bb64e1f69a..0fc123d3f0 100644 --- a/app/src/main/java/com/gh/base/BaseActivity.java +++ b/app/src/main/java/com/gh/base/BaseActivity.java @@ -87,10 +87,6 @@ public abstract class BaseActivity extends BaseAppCompatActivity implements Easy protected String mEntrance; private boolean mIsExistLogoutDialog; - private boolean mHasAddTaskFloat = false; - private View mTaskBackView; - private WindowManager mWM; - private WindowManager.LayoutParams mWmParams; public long startPageTime = 0; protected final Handler mBaseHandler = new BaseHandler(this); diff --git a/app/src/main/java/com/gh/common/util/EntranceUtils.java b/app/src/main/java/com/gh/common/util/EntranceUtils.java index 43564b1e02..9348ce7b22 100644 --- a/app/src/main/java/com/gh/common/util/EntranceUtils.java +++ b/app/src/main/java/com/gh/common/util/EntranceUtils.java @@ -21,6 +21,7 @@ import com.gh.gamecenter.normal.NormalFragment; import com.halo.assistant.HaloApp; import com.lightgame.utils.Utils; +import org.json.JSONException; import org.json.JSONObject; import java.util.Iterator; @@ -307,8 +308,6 @@ public class EntranceUtils { public static void saveShortcut(String activityName, @Nullable Bundle bundle) { if (BuildConfig.DEBUG) { - JSONObject json = new JSONObject(); - if (activityName.contains("MainActivity")) { SPUtils.setString(EntranceUtils.KEY_BUNDLE, ""); return; @@ -316,28 +315,9 @@ public class EntranceUtils { if (bundle == null) bundle = new Bundle(); try { - Set keys = bundle.keySet(); + JSONObject json = new JSONObject(); json.put(KEY_TO, activityName); - for (String key : keys) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { - Object object = bundle.get(key); - if (object instanceof Parcelable) { - if (object.toString().toLowerCase().contains("gameentity")) { - Utils.toast(HaloApp.getInstance().getApplication(), "暂不支持带游戏实体的页面保存捷径"); - return; - } - - String parcelableName = key + ":parcelable"; - String parcelableType = object.getClass().getName(); - String objectJsonString = GsonUtils.toJson(object); - JSONObject jObject = new JSONObject(objectJsonString); - json.put(parcelableName, parcelableType); - json.put(key, jObject); - } else { - json.put(key, JSONObject.wrap(bundle.get(key))); - } - } - } + json = getJsonFromBundle(bundle, json, null); Utils.toast(HaloApp.getInstance().getApplication(), "保存捷径成功"); SPUtils.setString(EntranceUtils.KEY_BUNDLE, json.toString()); } catch (Exception e) { @@ -347,6 +327,47 @@ public class EntranceUtils { } } + /** + * 将 Bundle 转为 Json + */ + private static JSONObject getJsonFromBundle(Bundle bundle, JSONObject json, @Nullable JSONObject bundleWrapper) { + Set keys = bundle.keySet(); + + for (String key : keys) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + try { + Object object = bundle.get(key); + if (object instanceof Bundle) { + json = getJsonFromBundle((Bundle) object, json, new JSONObject()); + } else if (object instanceof Parcelable) { + String parcelableName = key + ":parcelable"; + String parcelableType = object.getClass().getName(); + String objectJsonString = GsonUtils.toJson(object); + JSONObject jObject = null; + jObject = new JSONObject(objectJsonString); + + if (bundleWrapper != null) { + bundleWrapper.put(parcelableName, parcelableType); + bundleWrapper.put(key, jObject); + json.put(":bundle", bundleWrapper); + } else { + json.put(parcelableName, parcelableType); + json.put(key, jObject); + } + } else { + json.put(key, JSONObject.wrap(bundle.get(key))); + } + } catch (JSONException e) { + e.printStackTrace(); + } + } + } + return json; + } + + /** + * 快捷地跳转到上次保存的页面 + */ public static void jumpShortcut(Activity activity) { if (BuildConfig.DEBUG) { if (!hasShortcut()) return; @@ -354,22 +375,7 @@ public class EntranceUtils { Bundle bundle = new Bundle(); try { JSONObject jsonObject = new JSONObject(SPUtils.getString(EntranceUtils.KEY_BUNDLE)); - Iterator iter = jsonObject.keys(); - String parcelableName = ""; - while (iter.hasNext()) { - String key = (String) iter.next(); - String value = jsonObject.getString(key); - if (key.contains(":parcelable")) { - parcelableName = value; - } else { - if (!TextUtils.isEmpty(parcelableName)) { - Class gClass = Class.forName(parcelableName); - bundle.putParcelable(key, ((Parcelable) GsonUtils.fromJson(value, gClass))); - } else { - bundle.putString(key, value); - } - } - } + getBundleFromJson(bundle, jsonObject, null); } catch (Exception e) { e.printStackTrace(); } @@ -378,6 +384,45 @@ public class EntranceUtils { } } + /** + * 从 JSON 里还原 Bundle + */ + private static void getBundleFromJson(Bundle bundle, JSONObject jsonObject, @Nullable Bundle bundleWithin) throws JSONException, ClassNotFoundException { + Iterator iter = jsonObject.keys(); + String parcelableName = ""; + while (iter.hasNext()) { + String key = (String) iter.next(); + String value = jsonObject.getString(key); + if (key.contains(":parcelable")) { + parcelableName = value; + } else if (key.contains(":bundle")) { + getBundleFromJson(bundle, new JSONObject(value), new Bundle()); + } else { + if (bundleWithin != null) { + if (!TextUtils.isEmpty(parcelableName)) { + Class gClass = Class.forName(parcelableName); + bundleWithin.putParcelable(key, ((Parcelable) GsonUtils.fromJson(value, gClass))); + } else { + bundleWithin.putString(key, value); + } + + // TODO 支持多层 bundle 嵌套 + // TODO 直接用 GSON 提供的 Bundle Adapter https://github.com/google-gson/typeadapters/blob/master/android/src/main/java/BundleTypeAdapterFactory.java + if (!bundle.containsKey("normalFragmentBundle")) { + bundle.putBundle("normalFragmentBundle", bundleWithin); + } + } else { + if (!TextUtils.isEmpty(parcelableName)) { + Class gClass = Class.forName(parcelableName); + bundle.putParcelable(key, ((Parcelable) GsonUtils.fromJson(value, gClass))); + } else { + bundle.putString(key, value); + } + } + } + } + } + public static boolean hasShortcut() { return !TextUtils.isEmpty(SPUtils.getString(EntranceUtils.KEY_BUNDLE)); }