Skip to content

Commit

Permalink
添加双击卡收能量提示,调整保活方式,
Browse files Browse the repository at this point in the history
  • Loading branch information
constanline committed Jul 21, 2023
1 parent 102fb06 commit 56ebb03
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 54 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
minSdkVersion 23
//noinspection ExpiredTargetSdkVersion
targetSdkVersion 29
versionCode 11
versionName "1.0.2"
versionCode 14
versionName "1.0.3"
}
buildTypes {
release {
Expand Down
13 changes: 11 additions & 2 deletions app/src/main/java/pansong291/xposed/quickenergy/AntForest.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ private static void fillUserRobFlag(ClassLoader loader, List<String> idList) {
try {
String strList = new JSONArray(idList).toString();
String s = AntForestRpcCall.fillUserRobFlag(strList);
Thread.sleep(500);
JSONObject jo = new JSONObject(s);
checkCanCollectEnergy(loader, jo);
} catch (Throwable t) {
Expand Down Expand Up @@ -310,6 +311,10 @@ private static int getEnergyId(int waterEnergy) {
}

private static void canCollectEnergy(ClassLoader loader, String userId, boolean laterCollect) {
if (Config.forestPauseTime() > System.currentTimeMillis()) {
Log.recordLog("异常等待中,暂不执行检测!", "");
return;
}
try {
long start = System.currentTimeMillis();
String s = AntForestRpcCall.queryFriendHomePage(userId);
Expand All @@ -328,7 +333,7 @@ private static void canCollectEnergy(ClassLoader loader, String userId, boolean
String loginId = userName;
if (jo.has("loginId"))
loginId += "(" + jo.getString("loginId") + ")";
FriendIdMap.putIdMap(userId, loginId);
FriendIdMap.putIdMapIfEmpty(userId, loginId);
Log.recordLog("进入【" + loginId + "】的蚂蚁森林", "");
FriendIdMap.saveIdMap();
JSONArray jaProps = jo.optJSONArray("usingUserProps");
Expand Down Expand Up @@ -395,6 +400,10 @@ private static int collectEnergy(String userId, long bubbleId, String userName,
}

private static int collectEnergy(String userId, long bubbleId, String userName, String bizNo, String extra) {
if (Config.forestPauseTime() > System.currentTimeMillis()) {
Log.recordLog("异常等待中,暂不执行检测!", "");
return 0;
}
int collected = 0;
if (checkCollectLimited()) {
return 0;
Expand Down Expand Up @@ -423,7 +432,7 @@ private static int collectEnergy(String userId, long bubbleId, String userName,
if (collected > 0) {
totalCollected += collected;
Statistics.addData(Statistics.DataType.COLLECTED, collected);
String str = "偷取【" + userName + "】的能量【" + collected + "克】【" + extra + "】";
String str = "偷取【" + userName + "】的能量【" + collected + "克】" + (StringUtil.isEmpty(extra) ? "" : "【" + extra + "】");
Log.forest(str);
AntForestToast.show(str);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ public static String request(String args0, String args1) {
AntForestToast.context.sendBroadcast(it);
Log.recordLog("发送XposedEdgePro广播", Config.xEdgeProData());
}
if (Config.stayAwake()) {
if (Config.timeoutRestart()) {
if (Config.stayAwakeType() == XposedHook.StayAwakeType.BROADCAST) {
XposedHook.restartActivity(AntForestToast.context);
} else if (Config.stayAwakeType() == XposedHook.StayAwakeType.ALARM) {
XposedHook.alarmActivity(AntForestToast.context, 1000);
XposedHook.restartHook(AntForestToast.context, true);
} else {
XposedHook.alarmHook(AntForestToast.context, 1000, true);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.app.PendingIntent;
import android.app.Service;
import android.content.*;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
Expand Down Expand Up @@ -39,6 +40,12 @@ public enum StayAwakeType {
{"广播", "闹钟", "不重启"};
}

public enum StayAwakeTarget {
SERVICE, ACTIVITY;
public static final CharSequence[] nickNames =
{"Service", "Activity"};
}

@Override
public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpparam) {
if ("pansong291.xposed.quickenergy.repair".equals(lpparam.packageName)) {
Expand Down Expand Up @@ -69,9 +76,7 @@ public void run() {

FriendManager.fillUser(XposedHook.classLoader);
if (Config.collectEnergy() || Config.enableFarm()) {
Message msg = Message.obtain(null, this);
msg.what = 999;
handler.sendMessageDelayed(msg, Config.checkInterval());
handler.postDelayed(this, Config.checkInterval());
}
else {
AntForestNotification.stop(service, false);
Expand Down Expand Up @@ -100,7 +105,7 @@ public void run() {
private void hookService(ClassLoader loader) {
try {
XposedHelpers.findAndHookMethod(
ClassMember.CURRENT_USING_SERVICE, loader, "onBind", Intent.class, new XC_MethodHook() {
"android.app.Service", loader, "onCreate", new XC_MethodHook() {
ClassLoader loader;

public XC_MethodHook setData(ClassLoader cl) {
Expand All @@ -110,7 +115,7 @@ public XC_MethodHook setData(ClassLoader cl) {

@SuppressLint("WakelockTimeout")
@Override
protected void afterHookedMethod(MethodHookParam param) {
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
Service service = (Service) param.thisObject;
if (!ClassMember.CURRENT_USING_SERVICE.equals(service.getClass().getCanonicalName())) {
return;
Expand All @@ -128,17 +133,18 @@ protected void afterHookedMethod(MethodHookParam param) {
}
if (Config.stayAwake()) {
if (Config.stayAwakeType() == StayAwakeType.BROADCAST) {
AntForestToast.context.sendBroadcast(new Intent("com.eg.android.AlipayGphone.xqe.broadcast"));
alarmBroadcast(AntForestToast.context, 30 * 60 * 1000, false);
} else if (Config.stayAwakeType() == StayAwakeType.ALARM) {
alarmActivity(AntForestToast.context, 30 * 60 * 1000);
alarmHook(AntForestToast.context, 30 * 60 * 1000, false);
}
}
initHandler();
super.afterHookedMethod(param);
}
}.setData(loader));
Log.i(TAG, "hook onBind successfully");
Log.i(TAG, "hook onCreate successfully");
} catch (Throwable t) {
Log.i(TAG, "hook onBind err:");
Log.i(TAG, "hook onCreate err:");
Log.printStackTrace(TAG, t);
}
}
Expand All @@ -158,33 +164,64 @@ private void hookRpcCall(ClassLoader loader) {

}

public static void restartActivity(Context context) {
public static void restartHook(Context context, boolean force) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setComponent(new ComponentName(ClassMember.PACKAGE_NAME, ClassMember.CURRENT_USING_ACTIVITY));
if (force || Config.stayAwakeTarget() == StayAwakeTarget.ACTIVITY) {
intent.setComponent(new ComponentName(ClassMember.PACKAGE_NAME, ClassMember.CURRENT_USING_ACTIVITY));
} else {
intent.setComponent(new ComponentName(ClassMember.PACKAGE_NAME, ClassMember.CURRENT_USING_SERVICE));
}
intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
context.startActivity(intent);
}

public static void alarmActivity(Context context, long delayTime) {
public static void alarmBroadcast(Context context, long delayTime, boolean force) {
try {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent it = new Intent();
it.setClassName(ClassMember.PACKAGE_NAME, ClassMember.CURRENT_USING_ACTIVITY);
PendingIntent pi = PendingIntent.getActivity(context, 0, it,
Intent intent = new Intent("com.eg.android.AlipayGphone.xqe.broadcast");
intent.putExtra("force", force);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + delayTime, pi);
} catch (Throwable th) {
Log.printStackTrace(TAG, th);
}
}

public static void alarmHook(Context context, long delayTime, boolean force) {
try {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pi;
if (force || Config.stayAwakeTarget() == StayAwakeTarget.ACTIVITY) {
Intent it = new Intent();
it.setClassName(ClassMember.PACKAGE_NAME, ClassMember.CURRENT_USING_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
pi = PendingIntent.getForegroundService(context, 0, it,
PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
} else {
pi = PendingIntent.getService(context, 0, it,
PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
}
} else {
Intent it = new Intent();
it.setClassName(ClassMember.PACKAGE_NAME, ClassMember.CURRENT_USING_ACTIVITY);
pi = PendingIntent.getActivity(context, 0, it,
PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
}
alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + delayTime, pi);
} catch (Throwable th) {
Log.printStackTrace(TAG, th);
}
}

private static class AlipayBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if ("com.eg.android.AlipayGphone.xqe.broadcast".equals(action)) {
restartActivity(context);
boolean force = intent.getBooleanExtra("force", false);
restartHook(context, force);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,13 @@ public static void showStayAwakeType(Context c, CharSequence title) {
.create().show();
}

public static void showStayAwakeTarget(Context c, CharSequence title) {
new AlertDialog.Builder(c)
.setTitle(title)
.setSingleChoiceItems(XposedHook.StayAwakeTarget.nickNames, Config.stayAwakeTarget().ordinal(),
(p1, p2) -> Config.setStayAwakeTarget(p2))
.setPositiveButton("OK", null)
.create().show();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
Expand All @@ -21,8 +18,6 @@
import android.widget.TextView;
import android.widget.Toast;
import pansong291.xposed.quickenergy.R;
import pansong291.xposed.quickenergy.hook.ClassMember;
import pansong291.xposed.quickenergy.hook.XposedHook;
import pansong291.xposed.quickenergy.util.FileUtils;
import pansong291.xposed.quickenergy.util.PermissionUtil;
import pansong291.xposed.quickenergy.util.RandomUtils;
Expand Down Expand Up @@ -70,7 +65,7 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setModuleActive(isExpModuleActive(this));
PermissionUtil.verifyStoragePermissions(this);
PermissionUtil.requestPermissions(this);

tvStatistics = findViewById(R.id.tv_statistics);
btnHelp = findViewById(R.id.btn_help);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@
import android.widget.CheckBox;
import android.widget.Toast;
import pansong291.xposed.quickenergy.R;
import pansong291.xposed.quickenergy.hook.XposedHook;
import pansong291.xposed.quickenergy.util.Config;
import pansong291.xposed.quickenergy.util.CooperationIdMap;
import pansong291.xposed.quickenergy.util.FriendIdMap;

public class SettingsActivity extends Activity
{
CheckBox cb_immediateEffect, cb_recordLog, cb_showToast,
cb_stayAwake, cb_autoRestart,
cb_stayAwake, cb_timeoutRestart,
cb_collectEnergy, cb_helpFriendCollect, cb_receiveForestTaskAward,
cb_cooperateWater, cb_energyRain,
cb_enableFarm, cb_rewardFriend, cb_sendBackAnimal,
Expand All @@ -43,7 +42,7 @@ protected void onCreate(Bundle savedInstanceState)
cb_recordLog = findViewById(R.id.cb_recordLog);
cb_showToast = findViewById(R.id.cb_showToast);
cb_stayAwake = findViewById(R.id.cb_stayAwake);
cb_autoRestart = findViewById(R.id.cb_autoRestart);
cb_timeoutRestart = findViewById(R.id.cb_timeoutRestart);
cb_collectEnergy = findViewById(R.id.cb_collectEnergy);
cb_helpFriendCollect = findViewById(R.id.cb_helpFriendCollect);
cb_receiveForestTaskAward = findViewById(R.id.cb_receiveForestTaskAward);
Expand Down Expand Up @@ -77,7 +76,7 @@ protected void onResume()
cb_recordLog.setChecked(Config.recordLog());
cb_showToast.setChecked(Config.showToast());
cb_stayAwake.setChecked(Config.stayAwake());
cb_autoRestart.setChecked(Config.autoRestart());
cb_timeoutRestart.setChecked(Config.timeoutRestart());
cb_collectEnergy.setChecked(Config.collectEnergy());
cb_helpFriendCollect.setChecked(Config.helpFriendCollect());
cb_receiveForestTaskAward.setChecked(Config.receiveForestTaskAward());
Expand Down Expand Up @@ -126,8 +125,8 @@ public void onClick(View v)
Config.setStayAwake(cb.isChecked());
break;

case R.id.cb_autoRestart:
Config.setAutoRestart(cb.isChecked());
case R.id.cb_timeoutRestart:
Config.setTimeoutRestart(cb.isChecked());
break;

case R.id.cb_collectEnergy:
Expand Down Expand Up @@ -229,6 +228,10 @@ public void onClick(View v)
ChoiceDialog.showStayAwakeType(this, btn.getText());
break;

case R.id.btn_stayAwakeTarget:
ChoiceDialog.showStayAwakeTarget(this, btn.getText());
break;

case R.id.btn_checkInterval:
EditDialog.showEditDialog(this, btn.getText(), EditDialog.EditMode.CHECK_INTERVAL);
break;
Expand Down
Loading

0 comments on commit 56ebb03

Please sign in to comment.