Skip to content

Commit fcc2a18

Browse files
committed
update
提高稳定性 bug fixes
1 parent baa553e commit fcc2a18

File tree

1 file changed

+67
-55
lines changed

1 file changed

+67
-55
lines changed

蚂蚁森林.js

Lines changed: 67 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
var password = [1, 2, 3, 4]; // 锁屏密码
3030
var takeImg = files.cwd() + "/take.png"; // 收取好友能量用到的图片
3131

32+
const PATTERN_SIZE = 3; // 图案解锁每行点数
3233
const MAX_RETRY_TIMES = 10; // 最大失败重试次数
3334
const TIMEOUT = 6000; // 超时时间:毫秒
3435
const ALIPAY = "com.eg.android.AlipayGphone"; // 支付宝包名
@@ -55,43 +56,37 @@ function start(takeImg, password) {
5556
var km = context.getSystemService(context.KEYGUARD_SERVICE);
5657
var isLocked = km.inKeyguardRestrictedInputMode(); // 是否已经上锁
5758
var isSecure = km.isKeyguardSecure(); // 是否设置了密码
58-
var hasLayer = packageName("com.android.systemui").className("android.widget.FrameLayout").exists(); // 是否有上滑图层
5959
var isRunning = parseInt(shell("ps | grep 'AlipayGphone' | wc -l", true).result); // 支付宝是否运行
6060

6161
log({
6262
isScreenOn: isScreenOn,
6363
isLocked: isLocked,
6464
isSecure: isSecure,
65-
hasLayer: hasLayer,
6665
isRunning: isRunning
6766
});
6867

6968
var robot = new Robot();
7069
var antForest = new AntForest(robot);
70+
71+
// 先打开APP,节省等待时间
7172
threads.start(function () {
72-
antForest.openApp(); // 先打开APP,节省等待时间
73+
antForest.openApp();
7374
});
7475
// 子线程监听Home键
7576
threads.start(function () {
7677
events.observeKey();
7778
events.onKeyDown("home", function (event) {
7879
toastLog("停止脚本");
80+
engines.stopAll();
7981
exit();
8082
});
8183
});
8284

83-
// 向上滑动即可解锁
84-
var secure = new Secure();
85-
if (hasLayer) {
86-
log("向上滑动");
87-
secure.openLayer(robot);
88-
}
89-
9085
if (isLocked) {
86+
var secure = new Secure(robot);
9187
secure.openLock(password);
9288
}
9389

94-
9590
antForest.launch();
9691
antForest.work();
9792

@@ -103,36 +98,49 @@ function start(takeImg, password) {
10398
KeyCode("KEYCODE_POWER");
10499
}
105100
// 退出全部线程
101+
engines.stopAll();
106102
exit();
107103
}
108104

109105
/**
110106
* 安全相关
111107
* @constructor
112108
*/
113-
function Secure() {
109+
function Secure(robot) {
110+
this.robot = robot;
111+
114112
this.openLock = function (password) {
115113
log("解锁");
116114
for (var i = 0; i < MAX_RETRY_TIMES; i++) {
115+
var hasLayer = packageName("com.android.systemui").className("android.widget.FrameLayout").exists(); // 是否有上滑图层
116+
// 向上滑动即可解锁
117+
if (hasLayer) {
118+
log("向上滑动");
119+
this.openLayer();
120+
}
121+
117122
if (this.unlock(password)) {
118123
return true;
119124
} else {
120125
toastLog("解锁失败,重试");
121-
sleep(500);
122-
log("向上滑动");
123-
this.openLayer(robot);
124126
}
125127
}
126128

127129
toastLog("解锁失败,不再重试");
130+
this.failed();
131+
};
132+
133+
this.failed = function () {
128134
KeyCode("KEYCODE_POWER");
135+
engines.stopAll();
129136
exit();
137+
return false;
130138
};
131139

132-
this.openLayer = function (robot) {
140+
this.openLayer = function () {
133141
var x = WIDTH / 2;
134142
var y = HEIGHT - 100;
135-
robot.swipe(x, y, x, 100, 500);
143+
this.robot.swipe(x, y, x, 100, 500);
136144
sleep(500); // 等待动画
137145
};
138146

@@ -156,38 +164,42 @@ function Secure() {
156164
if (!id(key_id).exists()) {
157165
return false;
158166
}
159-
id(key_id).findOne().click();
167+
id(key_id).findOne(TIMEOUT).click();
160168
}
161169
if (id("com.android.systemui:id/key_enter").exists()) {
162-
id("com.android.systemui:id/key_enter").findOne().click();
170+
id("com.android.systemui:id/key_enter").findOne(TIMEOUT).click();
163171
}
164172

165173
sleep(500); // 等待动画
166-
return !id("com.android.systemui:id/key0").find().length;
174+
if (id("android:id/message").textContains("重试").exists()) {
175+
toastLog("密码错误");
176+
return this.failed();
177+
}
178+
return !id("com.android.systemui:id/key0").exists();
167179
};
168180

169181
this.unlockPattern = function (password, len) {
170182
var selector = id("com.android.systemui:id/lockPatternView");
171183
if (!selector.exists()) {
172184
return false;
173185
}
174-
var pattern = selector.findOne();
186+
var pattern = selector.findOne(TIMEOUT);
175187
var rect = pattern.bounds();
176188
// 使用坐标查找按键
177189
var oX = rect.left, oY = rect.top; // 第一个点位置
178-
var w = (rect.right - rect.left) / 3, h = (rect.bottom - rect.top) / 3; // 2点之单间隔为边框的1/3
190+
var w = (rect.right - rect.left) / PATTERN_SIZE, h = (rect.bottom - rect.top) / PATTERN_SIZE; // 2点之单间隔为边框的1/3
179191
var points = [];
180192

181193
points[0] = {
182194
x: 0,
183195
y: 0
184196
};
185197
// 初始化每个点的坐标
186-
for (var i = 1; i <= 3; i++) {
187-
for (var j = 1; j <= 3; j++) {
198+
for (var i = 1; i <= PATTERN_SIZE; i++) {
199+
for (var j = 1; j <= PATTERN_SIZE; j++) {
188200
var row = i - 1;
189201
var col = j - 1;
190-
var index = 3 * (i - 1) + j; // 序号,从1开始
202+
var index = PATTERN_SIZE * (i - 1) + j; // 序号,从1开始
191203
points[index] = {
192204
x: oX + col * w + w / 2,
193205
y: oY + row * h + h / 2
@@ -205,6 +217,10 @@ function Secure() {
205217
gestures(gestureParam);
206218

207219
sleep(500); // 等待动画
220+
if (id("android:id/message").textContains("重试").exists()) {
221+
toastLog("密码错误");
222+
return this.failed();
223+
}
208224
return !selector.exists();
209225
};
210226
}
@@ -216,7 +232,6 @@ function Secure() {
216232
*/
217233
function AntForest(robot) {
218234
this.robot = robot;
219-
this.times = 0;
220235

221236
this.openApp = function () {
222237
toastLog("即将收取蚂蚁森林能量,请勿操作!");
@@ -227,22 +242,19 @@ function AntForest(robot) {
227242

228243
this.launch = function () {
229244
var times = 0;
230-
var launched;
231245
do {
232-
launched = this.doLaunch();
233-
if (launched) {
234-
break;
246+
if (this.doLaunch()) {
247+
return;
235248
} else {
236249
times++;
237250
this.robot.kill(ALIPAY);
238251
this.openApp();
239252
}
240253
} while (times < MAX_RETRY_TIMES);
241254

242-
if (!launched) {
243-
toastLog("运行失败");
244-
exit();
245-
}
255+
toastLog("运行失败");
256+
engines.stopAll();
257+
exit();
246258
};
247259

248260
this.doLaunch = function () {
@@ -256,7 +268,7 @@ function AntForest(robot) {
256268
for (var times = 0; times < MAX_RETRY_TIMES; times++) {
257269
if (btn.exists()) {
258270
log("点击按钮");
259-
success = this.robot.clickCenter(btn.findOne());
271+
success = this.robot.clickCenter(btn.findOne(TIMEOUT));
260272
}
261273
if (success) {
262274
break;
@@ -271,7 +283,7 @@ function AntForest(robot) {
271283

272284
// 等待加载
273285
if (id("com.alipay.mobile.nebula:id/h5_tv_title").text("蚂蚁森林").findOne(TIMEOUT)) {
274-
sleep(500); // 等待界面渲染
286+
sleep(2000); // 等待界面渲染
275287
toastLog("进入蚂蚁森林成功");
276288
} else {
277289
toastLog("进入蚂蚁森林失败");
@@ -300,6 +312,7 @@ function AntForest(robot) {
300312
var icon = images.read(takeImg);
301313
if (null === icon) {
302314
toastLog("缺少图片文件,请仔细查看使用方法的第一条!!!");
315+
engines.stopAll();
303316
exit();
304317
}
305318
// 截图权限申请
@@ -315,6 +328,7 @@ function AntForest(robot) {
315328
});
316329
if (!requestScreenCapture()) {
317330
toastLog("请求截图失败");
331+
engines.stopAll();
318332
exit();
319333
}
320334

@@ -327,8 +341,9 @@ function AntForest(robot) {
327341

328342
// 等待更多列表刷新
329343
if (id("com.alipay.mobile.nebula:id/h5_tv_title").text("好友排行榜").findOne(TIMEOUT)) {
330-
sleep(1000); // 等待界面渲染
331-
this.takeOthers(icon, desc("没有更多了").className("android.view.View"));
344+
sleep(2000); // 等待界面渲染
345+
//this.takeOthers(icon, desc("没有更多了").className("android.view.View"));
346+
this.takeOthers(icon, className("android.webkit.WebView").scrollable(true));
332347
this.robot.back();
333348
} else {
334349
toastLog("进入好友排行榜失败");
@@ -344,14 +359,14 @@ function AntForest(robot) {
344359
* @param keyword
345360
*/
346361
this.take = function (keyword) {
347-
var right_bottom = className("android.widget.Button").desc(keyword).findOne();
348-
var left_top = id("com.alipay.mobile.nebula:id/h5_tv_nav_back").findOne();
362+
var right_bottom = className("android.widget.Button").desc(keyword).findOne(TIMEOUT);
363+
var left_top = id("com.alipay.mobile.nebula:id/h5_tv_nav_back").findOne(TIMEOUT);
349364

350365
var filters = [];
351366
var left = 0;
352367
var right = WIDTH;
353-
var top = left_top.bounds().bottom;
354-
var bottom = right_bottom.bounds().top;
368+
var top = left_top ? left_top.bounds().bottom : 215;
369+
var bottom = right_bottom ? right_bottom.bounds().top : 1092;
355370

356371
log(left + "-" + top + "-" + right + "-" + bottom);
357372
var all = descMatches("^(绿色能量|\\d+k?g)$").boundsInside(left, top, right, bottom).clickable(true).find();
@@ -360,25 +375,27 @@ function AntForest(robot) {
360375
filters.push(x);
361376
});
362377

378+
// 等待能量球渲染
379+
sleep(1500);
380+
381+
// 右上角控件字体最大,故按字体大小(面积)倒序排
363382
filters.sort(function (o1, o2) {
364-
return distance(o1) - distance(o2);
383+
var rect1 = o1.bounds();
384+
var rect2 = o2.bounds();
385+
return rect2.width() * rect2.height() - rect1.width() * rect1.height();
365386
});
366387

388+
// 找到第一个并删除(右上角控件)
367389
if (filters.length > 0) {
368390
filters.splice(0, 1);
369391
}
370392

371393
for (var i = 0, len = filters.length; i < len; i++) {
372-
//原有的click无效
394+
// 原有的click无效
373395
this.robot.clickCenter(filters[i], 100);
374396
log("点击->" + filters[i]);
375397
sleep(200);
376398
}
377-
378-
function distance(o) {
379-
var rect = o.bounds();
380-
return Math.pow((rect.top - top), 2) + Math.pow((rect.right - right), 2);
381-
}
382399
};
383400

384401
/**
@@ -409,11 +426,6 @@ function AntForest(robot) {
409426
times++;
410427
}
411428
}
412-
/*if (bottomUi.length && (bottomUi[0].bounds().top < HEIGHT)) {
413-
break;
414-
} else {
415-
times++;
416-
}*/
417429
}
418430
};
419431

@@ -452,7 +464,7 @@ function AntForest(robot) {
452464

453465
// 等待好友列表刷新
454466
id("com.alipay.mobile.nebula:id/h5_tv_title").textMatches(/.+/).findOne(TIMEOUT);
455-
sleep(500); // 等待界面渲染
467+
sleep(1500); // 等待界面渲染及加载
456468
}
457469
}
458470
}

0 commit comments

Comments
 (0)