diff --git a/app/src/main/java/pansong291/xposed/quickenergy/AncientTree.java b/app/src/main/java/pansong291/xposed/quickenergy/AncientTree.java index 5b4dac69a..f70a005d5 100644 --- a/app/src/main/java/pansong291/xposed/quickenergy/AncientTree.java +++ b/app/src/main/java/pansong291/xposed/quickenergy/AncientTree.java @@ -22,8 +22,7 @@ public static void start() { @Override public void run() { try { - while (FriendIdMap.getCurrentUid() == null || FriendIdMap.getCurrentUid().isEmpty()) - Thread.sleep(101); + FriendIdMap.waitingCurrentUid(); ancientTree(Config.getAncientTreeCityCodeList()); // 二次检查 有时会返回繁忙漏保护 } catch (Throwable t) { Log.i(TAG, "start.run err:"); diff --git a/app/src/main/java/pansong291/xposed/quickenergy/AntCooperate.java b/app/src/main/java/pansong291/xposed/quickenergy/AntCooperate.java index 7c81213fa..09c08e370 100644 --- a/app/src/main/java/pansong291/xposed/quickenergy/AntCooperate.java +++ b/app/src/main/java/pansong291/xposed/quickenergy/AntCooperate.java @@ -21,8 +21,7 @@ public static void start() { @Override public void run() { try { - while (FriendIdMap.getCurrentUid() == null || FriendIdMap.getCurrentUid().isEmpty()) - Thread.sleep(100); + FriendIdMap.waitingCurrentUid(); String s = AntCooperateRpcCall.queryUserCooperatePlantList(); if (s == null) { Thread.sleep(RandomUtils.delay()); diff --git a/app/src/main/java/pansong291/xposed/quickenergy/AntFarm.java b/app/src/main/java/pansong291/xposed/quickenergy/AntFarm.java index ede92e8c7..ed356022b 100644 --- a/app/src/main/java/pansong291/xposed/quickenergy/AntFarm.java +++ b/app/src/main/java/pansong291/xposed/quickenergy/AntFarm.java @@ -2,14 +2,22 @@ import org.json.JSONArray; import org.json.JSONObject; -import pansong291.xposed.quickenergy.hook.AntFarmRpcCall; -import pansong291.xposed.quickenergy.util.*; -import pansong291.xposed.quickenergy.hook.DadaDailyRpcCall; -import java.util.HashSet; -import java.util.Set; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; + +import pansong291.xposed.quickenergy.hook.AntFarmRpcCall; +import pansong291.xposed.quickenergy.hook.DadaDailyRpcCall; +import pansong291.xposed.quickenergy.util.Config; +import pansong291.xposed.quickenergy.util.FriendIdMap; +import pansong291.xposed.quickenergy.util.Log; +import pansong291.xposed.quickenergy.util.PluginUtils; +import pansong291.xposed.quickenergy.util.RandomUtils; +import pansong291.xposed.quickenergy.util.Statistics; +import pansong291.xposed.quickenergy.util.StringUtil; +import pansong291.xposed.quickenergy.util.TimeUtil; public class AntFarm { private static final String TAG = AntFarm.class.getCanonicalName(); @@ -124,12 +132,10 @@ public static void start() { @Override public void run() { try { - while (FriendIdMap.getCurrentUid() == null || FriendIdMap.getCurrentUid().isEmpty()) - Thread.sleep(100); + FriendIdMap.waitingCurrentUid(); String s = AntFarmRpcCall.enterFarm("", FriendIdMap.getCurrentUid()); if (s == null) { - Thread.sleep(RandomUtils.delay()); - s = AntFarmRpcCall.enterFarm("", FriendIdMap.getCurrentUid()); + throw new RuntimeException("庄园加载失败"); } JSONObject jo = new JSONObject(s); if ("SUCCESS".equals(jo.getString("memo"))) { diff --git a/app/src/main/java/pansong291/xposed/quickenergy/AntForest.java b/app/src/main/java/pansong291/xposed/quickenergy/AntForest.java index 6e777dc25..b60abc31f 100644 --- a/app/src/main/java/pansong291/xposed/quickenergy/AntForest.java +++ b/app/src/main/java/pansong291/xposed/quickenergy/AntForest.java @@ -360,6 +360,7 @@ private static void canCollectSelfEnergy() { try { boolean hasMore = false; long start = System.currentTimeMillis(); + //TODO 修复 6秒拼手速活动导致数据错误,目前只能关闭该活动 String s = AntForestRpcCall.queryHomePage(); long end = System.currentTimeMillis(); if (s == null) { diff --git a/app/src/main/java/pansong291/xposed/quickenergy/AntMember.java b/app/src/main/java/pansong291/xposed/quickenergy/AntMember.java index b6d18a6e3..7a19244ba 100644 --- a/app/src/main/java/pansong291/xposed/quickenergy/AntMember.java +++ b/app/src/main/java/pansong291/xposed/quickenergy/AntMember.java @@ -20,8 +20,7 @@ public static void receivePoint() { @Override public void run() { try { - while (FriendIdMap.getCurrentUid() == null || FriendIdMap.getCurrentUid().isEmpty()) - Thread.sleep(100); + FriendIdMap.waitingCurrentUid(); if (Statistics.canMemberSignInToday(FriendIdMap.getCurrentUid())) { String s = AntMemberRpcCall.queryMemberSigninCalendar(); JSONObject jo = new JSONObject(s); diff --git a/app/src/main/java/pansong291/xposed/quickenergy/AntOcean.java b/app/src/main/java/pansong291/xposed/quickenergy/AntOcean.java index 0504655fc..f6824e01a 100644 --- a/app/src/main/java/pansong291/xposed/quickenergy/AntOcean.java +++ b/app/src/main/java/pansong291/xposed/quickenergy/AntOcean.java @@ -24,8 +24,7 @@ public static void start() { @Override public void run() { try { - while (FriendIdMap.getCurrentUid() == null || FriendIdMap.getCurrentUid().isEmpty()) - Thread.sleep(100); + FriendIdMap.waitingCurrentUid(); String s = AntOceanRpcCall.queryOceanStatus(); JSONObject jo = new JSONObject(s); if ("SUCCESS".equals(jo.getString("resultCode"))) { diff --git a/app/src/main/java/pansong291/xposed/quickenergy/AntSports.java b/app/src/main/java/pansong291/xposed/quickenergy/AntSports.java index 0392bdf02..67daef65b 100644 --- a/app/src/main/java/pansong291/xposed/quickenergy/AntSports.java +++ b/app/src/main/java/pansong291/xposed/quickenergy/AntSports.java @@ -27,8 +27,7 @@ public Thread setData(ClassLoader cl) { @Override public void run() { try { - while (FriendIdMap.getCurrentUid() == null || FriendIdMap.getCurrentUid().isEmpty()) - Thread.sleep(100); + FriendIdMap.waitingCurrentUid(); if (Config.openTreasureBox()) queryMyHomePage(loader); diff --git a/app/src/main/java/pansong291/xposed/quickenergy/Reserve.java b/app/src/main/java/pansong291/xposed/quickenergy/Reserve.java index 7383c7e4f..a706ba821 100644 --- a/app/src/main/java/pansong291/xposed/quickenergy/Reserve.java +++ b/app/src/main/java/pansong291/xposed/quickenergy/Reserve.java @@ -32,8 +32,7 @@ public static void start() { @Override public void run() { try { - while (FriendIdMap.getCurrentUid() == null || FriendIdMap.getCurrentUid().isEmpty()) - Thread.sleep(100); + FriendIdMap.waitingCurrentUid(); if (Config.reserve()) { animalReserve(); } diff --git a/app/src/main/java/pansong291/xposed/quickenergy/hook/AntFarmRpcCall.java b/app/src/main/java/pansong291/xposed/quickenergy/hook/AntFarmRpcCall.java index aaca6cb64..21343a245 100644 --- a/app/src/main/java/pansong291/xposed/quickenergy/hook/AntFarmRpcCall.java +++ b/app/src/main/java/pansong291/xposed/quickenergy/hook/AntFarmRpcCall.java @@ -3,6 +3,7 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.UUID; + import pansong291.xposed.quickenergy.util.RandomUtils; public class AntFarmRpcCall { diff --git a/app/src/main/java/pansong291/xposed/quickenergy/hook/RpcUtil.java b/app/src/main/java/pansong291/xposed/quickenergy/hook/RpcUtil.java index ac5f4e518..df7cffe8c 100644 --- a/app/src/main/java/pansong291/xposed/quickenergy/hook/RpcUtil.java +++ b/app/src/main/java/pansong291/xposed/quickenergy/hook/RpcUtil.java @@ -1,18 +1,20 @@ package pansong291.xposed.quickenergy.hook; -import de.robv.android.xposed.XposedHelpers; import org.json.JSONObject; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.text.DateFormat; + +import de.robv.android.xposed.XposedHelpers; import pansong291.xposed.quickenergy.AntForestNotification; import pansong291.xposed.quickenergy.AntForestToast; import pansong291.xposed.quickenergy.data.RuntimeInfo; import pansong291.xposed.quickenergy.util.Config; import pansong291.xposed.quickenergy.util.Log; +import pansong291.xposed.quickenergy.util.RandomUtils; import pansong291.xposed.quickenergy.util.StringUtil; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.text.DateFormat; - public class RpcUtil { private static final String TAG = RpcUtil.class.getCanonicalName(); private static Method rpcCallMethod; @@ -65,61 +67,85 @@ public static String request(String args0, String args1) { if (isInterrupted) { return null; } - try { - Object o; - if (rpcCallMethod.getParameterTypes().length == 12) { - o = rpcCallMethod.invoke( - null, args0, args1, "", true, null, null, false, curH5PageImpl, 0, "", false, -1); - } else { - o = rpcCallMethod.invoke( - null, args0, args1, "", true, null, null, false, curH5PageImpl, 0, "", false, -1, ""); - } - String str = getResponse(o); - Log.i(TAG, "argument: " + args0 + ", " + args1); - Log.i(TAG, "response: " + str); + String result = null; + int count = 0; + while (count < 3) { + count++; try { - JSONObject jo = new JSONObject(str); - if (jo.optString("memo", "").contains("系统繁忙")) { - isInterrupted = true; - AntForestNotification.setContentText("系统繁忙,可能需要滑动验证"); - Log.recordLog("系统繁忙,可能需要滑动验证"); - return str; + Object o; + if (rpcCallMethod.getParameterTypes().length == 12) { + o = rpcCallMethod.invoke( + null, args0, args1, "", true, null, null, false, curH5PageImpl, 0, "", false, -1); + } else { + o = rpcCallMethod.invoke( + null, args0, args1, "", true, null, null, false, curH5PageImpl, 0, "", false, -1, ""); } - } catch (Throwable ignored) { } - return str; - } catch (Throwable t) { - Log.i(TAG, "invoke err:"); - Log.printStackTrace(TAG, t); - if (t instanceof InvocationTargetException) { - String msg = t.getCause().getMessage(); - if (!StringUtil.isEmpty(msg)) { - if (msg.contains("登录超时")) { + String str = getResponse(o); + Log.i(TAG, "argument: " + args0 + ", " + args1); + Log.i(TAG, "response: " + str); + try { + JSONObject jo = new JSONObject(str); + if (jo.optString("memo", "").contains("系统繁忙")) { isInterrupted = true; - AntForestNotification.setContentText("登录超时"); - if (AntForestToast.context != null) { - if (Config.timeoutRestart()) { - Log.recordLog("尝试重启!"); - if (Config.timeoutType() == XposedHook.StayAwakeType.ALARM) { - XposedHook.alarmHook(AntForestToast.context, 3000, true); - } else { - XposedHook.alarmBroadcast(AntForestToast.context, 3000, true); + AntForestNotification.setContentText("系统繁忙,可能需要滑动验证"); + Log.recordLog("系统繁忙,可能需要滑动验证"); + return str; + } + } catch (Throwable ignored) { + } + return str; + } catch (Throwable t) { + Log.i(TAG, "rpc [" + args0 + "] err:"); + Log.printStackTrace(TAG, t); + result = null; + if (t instanceof InvocationTargetException) { + String msg = t.getCause().getMessage(); + if (!StringUtil.isEmpty(msg)) { + if (msg.contains("登录超时")) { + if (!isInterrupted && !XposedHook.getIsRestart()) { + isInterrupted = true; + AntForestNotification.setContentText("登录超时"); + if (AntForestToast.context != null) { + if (Config.timeoutRestart()) { + Log.recordLog("尝试重启!"); + XposedHook.restartHook(AntForestToast.context, Config.timeoutType(), 500, true); + } } } + try { + Thread.sleep(3000 + RandomUtils.delay()); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + continue; + } else if (msg.contains("[1004]") && "alipay.antmember.forest.h5.collectEnergy".equals(args0)) { + if (Config.waitWhenException() > 0) { + long waitTime = System.currentTimeMillis() + Config.waitWhenException(); + RuntimeInfo.getInstance().put(RuntimeInfo.RuntimeInfoKey.ForestPauseTime, waitTime); + AntForestNotification.setContentText("触发异常,等待至" + DateFormat.getDateTimeInstance().format(waitTime)); + Log.recordLog("触发异常,等待至" + DateFormat.getDateTimeInstance().format(waitTime)); + } + try { + Thread.sleep(900 + RandomUtils.delay()); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + continue; + } else if (msg.contains("MMTPException")) { + result = "{\"resultCode\":\"FAIL\",\"memo\":\"MMTPException\",\"resultDesc\":\"MMTPException\"}"; + try { + Thread.sleep(900 + RandomUtils.delay()); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + continue; } - } else if (msg.contains("[1004]") && "alipay.antmember.forest.h5.collectEnergy".equals(args0)) { - if (Config.waitWhenException() > 0) { - long waitTime = System.currentTimeMillis() + Config.waitWhenException(); - RuntimeInfo.getInstance().put(RuntimeInfo.RuntimeInfoKey.ForestPauseTime, waitTime); - AntForestNotification.setContentText("触发异常,等待至" + DateFormat.getDateTimeInstance().format(waitTime)); - Log.recordLog("触发异常,等待至" + DateFormat.getDateTimeInstance().format(waitTime)); - } - } else if (msg.contains("MMTPException")) { - return "{\"resultCode\":\"FAIL\",\"memo\":\"MMTPException\",\"resultDesc\":\"MMTPException\"}"; } } + return result; } } - return null; + return result; } public static String getResponse(Object resp) throws Throwable { diff --git a/app/src/main/java/pansong291/xposed/quickenergy/hook/XposedHook.java b/app/src/main/java/pansong291/xposed/quickenergy/hook/XposedHook.java index a3c706e8d..ad46c7796 100644 --- a/app/src/main/java/pansong291/xposed/quickenergy/hook/XposedHook.java +++ b/app/src/main/java/pansong291/xposed/quickenergy/hook/XposedHook.java @@ -12,18 +12,39 @@ import android.os.Build; import android.os.Handler; import android.os.PowerManager; + +import java.util.Calendar; +import java.util.Map; + import de.robv.android.xposed.IXposedHookLoadPackage; import de.robv.android.xposed.XC_MethodHook; import de.robv.android.xposed.XC_MethodReplacement; import de.robv.android.xposed.XposedHelpers; import de.robv.android.xposed.callbacks.XC_LoadPackage; -import pansong291.xposed.quickenergy.*; +import pansong291.xposed.quickenergy.AncientTree; +import pansong291.xposed.quickenergy.AntBookRead; +import pansong291.xposed.quickenergy.AntCooperate; +import pansong291.xposed.quickenergy.AntFarm; +import pansong291.xposed.quickenergy.AntForest; +import pansong291.xposed.quickenergy.AntForestNotification; +import pansong291.xposed.quickenergy.AntForestToast; +import pansong291.xposed.quickenergy.AntMember; +import pansong291.xposed.quickenergy.AntOcean; +import pansong291.xposed.quickenergy.AntOrchard; +import pansong291.xposed.quickenergy.AntSports; +import pansong291.xposed.quickenergy.AntStall; +import pansong291.xposed.quickenergy.ConsumeGold; +import pansong291.xposed.quickenergy.GreenFinance; +import pansong291.xposed.quickenergy.OmegakoiTown; +import pansong291.xposed.quickenergy.Reserve; import pansong291.xposed.quickenergy.data.RuntimeInfo; import pansong291.xposed.quickenergy.ui.MainActivity; -import pansong291.xposed.quickenergy.util.*; - -import java.util.Calendar; -import java.util.Map; +import pansong291.xposed.quickenergy.util.Config; +import pansong291.xposed.quickenergy.util.FriendIdMap; +import pansong291.xposed.quickenergy.util.Log; +import pansong291.xposed.quickenergy.util.PluginUtils; +import pansong291.xposed.quickenergy.util.Statistics; +import pansong291.xposed.quickenergy.util.TimeUtil; public class XposedHook implements IXposedHookLoadPackage { private static final String TAG = XposedHook.class.getCanonicalName(); @@ -37,16 +58,22 @@ public class XposedHook implements IXposedHookLoadPackage { private static boolean isHooked = false; + private static boolean isRestart = false; + public enum StayAwakeType { BROADCAST, ALARM, NONE; - public static final CharSequence[] nickNames = { "广播", "闹钟", "不重启" }; + public static final CharSequence[] nickNames = {"广播", "闹钟", "不重启"}; } public enum StayAwakeTarget { SERVICE, ACTIVITY; - public static final CharSequence[] nickNames = { "Service", "Activity" }; + public static final CharSequence[] nickNames = {"Service", "Activity"}; + } + + public static boolean getIsRestart() { + return isRestart; } @Override @@ -75,6 +102,16 @@ protected void beforeHookedMethod(MethodHookParam param) { } } + private static void restartHandler() { + if (handler != null && runnable != null) { + handler.removeCallbacks(runnable); + AntForest.stop(); + AntForestNotification.stop(service, false); + AntForestNotification.start(service); + handler.post(runnable); + } + } + private static void initHandler() { Log.recordLog("尝试初始化"); if (runnable == null) { @@ -127,12 +164,8 @@ public void run() { Config.setAlarm7(AntForestToast.context); } } + restartHandler(); AntForestToast.show("芝麻粒加载成功"); - handler.removeCallbacks(runnable); - AntForest.stop(); - AntForestNotification.stop(service, false); - AntForestNotification.start(service); - handler.post(runnable); } catch (Throwable th) { Log.i(TAG, "initHandler err:"); Log.printStackTrace(TAG, th); @@ -147,14 +180,23 @@ private void hookService(ClassLoader loader) { protected void afterHookedMethod(MethodHookParam param) { Log.i(TAG, "Activity onResume"); RpcUtil.isInterrupted = false; - PermissionUtil.requestPermissions((Activity) param.thisObject); + //PermissionUtil.requestPermissions((Activity) param.thisObject); AntForestNotification.setContentText("运行中..."); String targetUid = RpcUtil.getUserId(loader); - if (targetUid == null || targetUid.equals(FriendIdMap.getCurrentUid())) { + if (targetUid == null) { return; } - FriendIdMap.setCurrentUid(targetUid); - if (handler != null) { + if (!targetUid.equals(FriendIdMap.getCurrentUid())) { + FriendIdMap.setCurrentUid(targetUid); + } else if (!isRestart) { + return; + } + if (isRestart) { + Log.i(TAG, "Activity isRestart"); + isRestart = false; + restartHandler(); + ((Activity) param.thisObject).finish(); + } else if (handler != null) { initHandler(); } } @@ -187,11 +229,7 @@ protected void afterHookedMethod(MethodHookParam param) { wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, service.getClass().getName()); wakeLock.acquire(); - if (Config.stayAwakeType() == StayAwakeType.BROADCAST) { - alarmBroadcast(AntForestToast.context, 30 * 60 * 1000, false); - } else if (Config.stayAwakeType() == StayAwakeType.ALARM) { - alarmHook(AntForestToast.context, 30 * 60 * 1000, false); - } + restartHook(AntForestToast.context, Config.stayAwakeType(), 30 * 60 * 1000, false); } initHandler(); } @@ -262,6 +300,14 @@ protected void afterHookedMethod(MethodHookParam param) { } + public static void restartHook(Context context, StayAwakeType stayAwakeType, long delayTime, boolean force) { + if (stayAwakeType == StayAwakeType.ALARM) { + alarmHook(context, delayTime, force); + } else { + alarmBroadcast(context, delayTime, force); + } + } + public static void restartHook(Context context, boolean force) { try { Intent intent; @@ -269,6 +315,9 @@ public static void restartHook(Context context, boolean force) { intent = new Intent(Intent.ACTION_VIEW); intent.setClassName(ClassMember.PACKAGE_NAME, ClassMember.CURRENT_USING_ACTIVITY); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + if (force) { + isRestart = true; + } context.startActivity(intent); } else { intent = new Intent(); @@ -309,6 +358,9 @@ public static void alarmHook(Context context, long delayTime, boolean force) { Intent it = new Intent(); it.setClassName(ClassMember.PACKAGE_NAME, ClassMember.CURRENT_USING_ACTIVITY); pi = PendingIntent.getActivity(context, 1, it, getPendingIntentFlag()); + if (force) { + isRestart = true; + } } else { Intent it = new Intent(); it.setClassName(ClassMember.PACKAGE_NAME, ClassMember.CURRENT_USING_SERVICE); diff --git a/app/src/main/java/pansong291/xposed/quickenergy/util/FileUtils.java b/app/src/main/java/pansong291/xposed/quickenergy/util/FileUtils.java index b38ecabf1..6e03233df 100644 --- a/app/src/main/java/pansong291/xposed/quickenergy/util/FileUtils.java +++ b/app/src/main/java/pansong291/xposed/quickenergy/util/FileUtils.java @@ -54,12 +54,9 @@ public static File getMainDirectoryFile() { if (!storageDir.exists()) { storageDir.mkdirs(); } - File useMedia = new File(storageDir, "useMedia"); - if (useMedia.exists()) { - String storageDirStr = Environment.getExternalStorageDirectory() + File.separator + "Android" + - File.separator + "media" + File.separator + ClassMember.PACKAGE_NAME; - storageDir = new File(storageDirStr); - } + String storageDirStr = Environment.getExternalStorageDirectory() + File.separator + "Android" + + File.separator + "media" + File.separator + ClassMember.PACKAGE_NAME; + storageDir = new File(storageDirStr); mainDirectory = new File(storageDir, "xqe"); if (!mainDirectory.exists()) { diff --git a/app/src/main/java/pansong291/xposed/quickenergy/util/FriendIdMap.java b/app/src/main/java/pansong291/xposed/quickenergy/util/FriendIdMap.java index ee047facb..e732b81da 100644 --- a/app/src/main/java/pansong291/xposed/quickenergy/util/FriendIdMap.java +++ b/app/src/main/java/pansong291/xposed/quickenergy/util/FriendIdMap.java @@ -1,15 +1,14 @@ package pansong291.xposed.quickenergy.util; -import pansong291.xposed.quickenergy.AntFarm; -import pansong291.xposed.quickenergy.hook.FriendManager; -import pansong291.xposed.quickenergy.hook.XposedHook; - import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; +import pansong291.xposed.quickenergy.hook.FriendManager; +import pansong291.xposed.quickenergy.hook.XposedHook; + public class FriendIdMap { private static final String TAG = FriendIdMap.class.getCanonicalName(); @@ -142,4 +141,16 @@ public static Map getIdMap() { return idMap; } + public static void waitingCurrentUid() throws InterruptedException { + int count = 1; + while (getCurrentUid() == null || getCurrentUid().isEmpty()) { + if (count > 3) { + throw new InterruptedException("获取当前用户超时"); + } else { + count++; + Thread.sleep(1000); + } + } + } + }