forked from SmartisanTech/Wrench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht1wrench.lua
executable file
·1699 lines (1556 loc) · 55.5 KB
/
t1wrench.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/lua
-- module
local M
-- functions
local t1_call, t1_run, t1_adb_mail, t1_save_mail_heads
local shell_quote, putclip, t1_post, push_text
local adb_start_activity
local picture_to_weixin_share, picture_to_weibo_share
local picture_to_momo_share, t1_add_mms_receiver
local adb_get_input_window_dump, adb_top_window
local adb_start_weixin_share, adb_is_window
local adb_focused_window
local t1_config, check_phone
local emoji_for_qq, debug, get_a_note, emoji_for_weixin, emoji_for_qq_or_weixin
local adb_get_last_pic, debugging
local adb_weixin_lucky_money
local adb_weixin_lucky_money_output
local t1_find_weixin_contact
local adb_start_service_and_wait_file_gone
local adb_start_service_and_wait_file, adb_am
-- variables
local where_is_dial_key
local rows_mail_att_finder
local UNAME_CMD = "uname || busybox uname || { echo -n Lin && echo -n ux; }"
local is_debugging = false
local using_scroll_lock = true
local using_adb_root
local adb_unquoter
local is_windows = false
local debug_set_x = ""
local ime_height_ref = 874
local default_width, default_height = 1080, 1920
local init_width, init_height = 1080, 1920
local app_width, app_height = 1080,1920
local width_ratio, height_ratio = app_width / default_width, app_height / default_height
local using_smartisan_os = true
local using_xiaomi_os = false
local using_oppo_os = false
local brand = "smartisan"
local model = "T1"
local qq_emojis, weixin_emojis
local sdk_version = 19
local emojis, emojis_map
local the_true_adb = "./the-true-adb"
local qq_emoji_table = {
"微笑", "撇嘴", "色", "发呆", "得意", "流泪", "害羞", "闭嘴",
"睡", "大哭", "尴尬", "发怒", "调皮", "呲牙", "惊讶", "难过",
"酷", "冷汗", "抓狂", "吐", "偷笑", "可爱", "白眼", "傲慢",
"饥饿", "困", "惊恐", "流汗", "憨笑", "大兵", "奋斗", "咒骂",
"疑问", "嘘", "晕", "折磨", "衰", "骷髅", "敲打", "再见",
"擦汗", "抠鼻", "鼓掌", "糗大了", "坏笑", "左哼哼", "右哼哼", "哈欠",
"鄙视", "委屈", "快哭了", "阴险", "亲亲", "吓", "可怜", "菜刀",
"西瓜", "啤酒", "篮球", "乒乓", "咖啡", "饭", "猪头", "玫瑰",
"凋谢", "示爱", "爱心", "心碎", "蛋糕", "闪电", "炸弹", "刀",
"足球", "瓢虫", "便便", "月亮", "太阳", "礼物", "拥抱", "强",
"弱", "握手", "胜利", "抱拳", "勾引", "拳头", "差劲", "爱你",
"NO", "OK", "爱情", "飞吻", "跳跳", "发抖", "怄火", "转圈",
"磕头", "回头", "跳绳", "挥手", "激动", "街舞", "献吻", "左太极",
"右太极",
}
for i in ipairs(qq_emoji_table) do
qq_emoji_table[qq_emoji_table[i]] = i;
end
local p = io.popen("the-true-adb version")
local v = p:read("*a")
adb_unquoter = ""
if v:match("1.0.31") then
adb_unquoter = '\\"'
end
if package.config:sub(1, 1) == '/' then
shell_quote = function (str)
return "'" .. string.gsub(str, "'", "'\\''") .. "'"
end
if is_debugging then
debug_set_x = "set -x; "
else
debug_set_x = ""
end
else -- windows
shell_quote = function (str)
str = str:gsub('\n', '')
str = str:gsub('\\', '\\\\')
str = str:gsub('"', '\\"')
return '"' .. str .. '"'
end
debug_set_x = ""
is_windows = true
the_true_adb = ".\\the-true-adb"
end
emoji_for_qq = function(text)
return emoji_for_qq_or_weixin(text, qq_emojis)
end
emoji_for_qq_or_weixin = function(text, which_emojis)
local s = 1
local replace = ""
repeat
local fs, fe = text:find("%[.-%]", s)
if fs then
local emoji = text:sub(fs + 1, fe - 1)
if qq_emoji_table[emoji] then
replace = replace .. text:sub(s, fs - 1)
if which_emojis == qq_emojis then
local idx = qq_emoji_table[emoji]
replace = replace .. which_emojis[idx]
else
replace = replace .. "/" .. emoji
end
s = fe + 1
else
replace = replace .. text:sub(s, fs)
s = fs + 1
end
else
replace = replace .. text:sub(s)
break
end
until s > #text
return replace
end
emoji_for_weixin = function(text)
return emoji_for_qq_or_weixin(text, weixin_emojis)
end
local function system(cmds)
if type(cmds) == 'string' then
return os.execute(cmds)
elseif type(cmds) == 'table' then
command_str = ''
for i = 1, #cmds do
if i == 1 and is_windows then
command_str = command_str .. cmds[i] .. ' '
else
command_str = command_str .. shell_quote(cmds[i]) .. ' '
end
end
return os.execute(debug_set_x .. command_str)
end
end
debugging = function(fmt, ...)
if is_debugging then
debug(fmt, ...)
end
end
debug = function(fmt, ...)
print(string.format(fmt, ...))
end
local function split(pat, str, allow_null)
local start = 1
if pat == ' ' then
pat = "%s+"
end
local list, i, j = {}
while true do
i, j = str:find(pat, start)
if (i and i >= start) then
if i > start then
list[#list + 1] = str:sub(start, i - 1)
elseif allow_null then
list[#list + 1] = ""
end
elseif #str >= start then
list[#list + 1] = str:sub(start)
elseif allow_null then
list[#list + 1] = ""
end
if i then
start = j + 1
else
break
end
end
return list
end
local function replace_img_with_emoji(text, html)
debugging("text is %s, html is %s", text, html)
local texts = split("", text, true)
for k, v in pairs(texts) do
print(k, v)
end
local n = 2
local res = texts[1]
for emoji in html:gmatch('img src="(.-)"') do
debugging("emoji is %s", emoji)
if not emojis then
emojis = require"emojis"
emojis_map = {}
for k, v in ipairs(emojis) do
emojis_map[v[3]] = v[1]
end
end
emoji = emojis_map[emoji] or "[unknown emoji]"
res = res .. emoji
if texts[n] then
res = res .. texts[n]
end
n = n + 1
end
debugging("res is %s", res)
return res
end
local function join(mid, args)
text = ''
for i = 1, #args do
if i ~= 1 then
text = text .. mid
end
text = text .. args[i]
end
return text
end
local function adb_do(func, cmds)
if type(cmds) == 'string' then
return adb_do(func, {"sh", "-c", cmds})
else
assert(type(cmds) == 'table', "command must be a sequence");
if (#cmds == 1) then
return adb_do(func, cmds[1])
end
command_str = ''
quoted_cmds = {}
for i = 1, #cmds do
quoted_cmds[i] = shell_quote(shell_quote(cmds[i]))
if string.find(quoted_cmds[i], " ") then
quoted_cmds[i] = adb_unquoter .. quoted_cmds[i] .. adb_unquoter
end
command_str = command_str .. quoted_cmds[i] .. ' '
end
return func(debug_set_x .. the_true_adb .. " shell " .. command_str)
end
end
local function adb_shell(cmds)
return adb_do(os.execute, cmds)
end
local function adb_pipe(cmds)
local pipe = adb_do(io.popen, cmds)
if not pipe then
return ""
end
local out = pipe:read('*a')
if not out then
return ""
end
return out:gsub("\r", "")
end
adb_is_window = function (w)
return w == adb_focused_window()
end
adb_start_activity = function(a)
adb_am("am start -n " .. a)
end
adb_focused_window = function()
local wdump = adb_pipe{"dumpsys", "window"}
local match = string.match(wdump, "mFocusedWindow[^}]*%s(%S+)}")
if match then
return match
end
match = wdump:match("mTopFullscreenOpaqueWindowState=Window.-(%S+)%s+paused=false}")
if match then
return match
end
if check_phone() or true then
return adb_focused_window()
end
error("Can't find focused window: " .. wdump:sub(1, 20))
end
adb_am = function(cmd)
if type(cmd) ~= 'string' then
cmd = join(' ', cmd)
end
if adb_quick_am ~= nil then
adb_quick_am{cmd}
else
adb_shell(cmd)
end
end
local function adb_event(events)
if type(events) == 'string' then
adb_event(split(" ", events))
return
end
command_str = ''
i = 1
while true do
if not events[i] then
if not events[i - 1] then
debugging("Error at i = %d, events: %s", i, join(' ', events))
error("Error: wrong number of events?")
else
break
end
end
if tonumber(events[i]) then
local add = ('input tap %d %d;'):format(events[i] * width_ratio, events[i+1] * height_ratio)
command_str = command_str .. add
i = i + 2
elseif events[i] == 'tap2' or events[i] == 'adb-tap-2' then
i = i + 1
local add = ('input tap %d %d;'):format(events[i] * width_ratio, events[i+1] * height_ratio)
command_str = command_str .. add .. add
i = i + 2
elseif (events[i]):match('^adb%-long%-press') then
ms = 500
if (events[i]):match('^adb%-long%-press%-%d+') then
ms = (events[i]):sub(#"adb-long-press-" + 1)
end
if sdk_version < 18 then
ms = ""
end
local add = ('input touchscreen swipe %d %d %d %d %s;'):format(
events[i+1] * width_ratio, events[i+2] * height_ratio,
events[i+1] * width_ratio, events[i+2] * height_ratio, ms)
if sdk_version < 17 then
add = add:gsub("touchscreen ", "")
end
command_str = command_str .. add
if sdk_version < 18 then
command_str = command_str .. add
end
i = i + 3
elseif events[i] == 'key' or events[i] == 'adb-key' then
command_str = command_str .. ('input keyevent %s;'):format(events[i+1]:upper())
i = i + 2
elseif events[i] == 'sleep' then
command_str = command_str .. ('sleep %s || busybox sleep %s;'):format(events[i+1], events[i+1])
i = i + 2
elseif events[i] == 'swipe' or (events[i]):match('adb%-swipe%-') then
ms = 500
if (events[i]):match('adb%-swipe%-') then
ms = (events[i]):sub(#'adb-swipe-' + 1)
end
if sdk_version < 18 then
ms = ""
end
local add = ('input touchscreen swipe %d %d %d %d %s;'):format(
events[i+1] * width_ratio, events[i+2] * height_ratio,
events[i+3] * width_ratio, events[i+4] * height_ratio, ms)
if sdk_version < 17 then
add = add:gsub("touchscreen ", "")
end
command_str = command_str .. add
if sdk_version < 18 then
command_str = command_str .. add
end
i = i + 5
elseif events[i] == 'adb-tap' then
i = i + 1
else
error(string.format("Error: unknown event: %d: '%s' (%s)", i, events[i], join(' ', events)))
end
end
if adb_quick_input ~= nil then
debugging("doing with " .. command_str)
adb_quick_input{command_str}
else
adb_shell(command_str)
end
end
local function adb_tap_bot_left()
adb_event{20, 1882}
end
local function adb_tap_mid_bot()
adb_event{560, 1840}
end
local function sleep(time)
adb_event(("sleep %s"):format(time))
end
local function weibo_text_share(window)
local repost = '?'
if window == "com.sina.weibo/com.sina.weibo.DetailWeiboActivity" then
repost = select_args{'转发还是评论', '转发', '评论', '转发并评论'}
if repost:match('转发') then
debugging("doing post")
adb_tap_bot_left()
else
adb_tap_mid_bot()
end
sleep(1)
end
local input_method, ime_height = adb_get_input_window_dump()
if ime_height ~= 0 then
adb_event("key back")
end
if repost:match('并') then
adb_event("sleep .1 adb-tap 57 1704")
end
if using_scroll_lock then
adb_event{'key', 'scroll_lock', 991, 166}
elseif using_smartisan_os then
adb_event("adb-tap 24 308 adb-key SPACE adb-long-press-800 17 294 adb-tap 545 191 adb-tap 991 166")
elseif using_xiaomi_os then
adb_event("adb-tap-2 24 308 sleep .1 adb-tap 77 179 adb-tap 991 166")
elseif using_oppo_os then
adb_event("adb-long-press-800 34 312 adb-tap 72 149 adb-tap 991 166")
else
adb_event("adb-key space adb-long-press-800 17 294 adb-tap-2 991 166")
end
end
local function t1_share_to_weibo(text)
adb_am{"am", "start", "-n", "com.sina.weibo/com.sina.weibo.composerinde.OriginalComposerActivity"}
if text then putclip(text) else sleep(1) end
t1_post()
end
adb_top_window = function()
-- dumpsys window|grep mFocusedWindow|perl -npe 's/.*?(\S+)}$/$1/')
local adb_window_dump = adb_pipe("dumpsys window")
if not adb_window_dump then return nil end
local focused_line = adb_window_dump:match("mFocusedWindow=.-}")
if not focused_line then return nil end
local top_window = focused_line:match("%S+}$")
if not top_window then return nil end
return top_window:sub(1, -2)
end
adb_start_weixin_share = function(text_or_image)
if using_adb_root then
if text_or_image == 'text' then
adb_am("am start -n com.tencent.mm/com.tencent.mm.plugin.sns.ui.SnsCommentUI --ei sns_comment_type 1")
elseif text_or_image == 'image' then
adb_am("am start -n com.tencent.mm/com.tencent.mm.plugin.sns.ui.SnsUploadUI")
else
error("Can only do image or text")
end
return
end
local click = "adb-tap"
if text_or_image == 'text' then
click = "adb-long-press-800"
elseif text_or_image ~= 'image' then
error("Can only do image or text")
end
adb_am("am start -n com.tencent.mm/com.tencent.mm.ui.LauncherUI")
for i = 1, 3 do
if adb_top_window() ~= "com.tencent.mm/com.tencent.mm.ui.LauncherUI" then
adb_event("adb-tap 88 170 sleep " .. (.2 * i))
adb_am("am start -n com.tencent.mm/com.tencent.mm.ui.LauncherUI")
else
adb_event("adb-tap-2 88 170")
break
end
end
adb_event("adb-tap 654 1850 sleep .1 adb-tap 332 358 sleep .2 " .. click .. " 961 160")
if text_or_image == 'image' then
adb_event("adb-tap 213 929") -- choose picture
end
end
local function t1_share_to_weixin(text)
adb_start_weixin_share('text')
if text then
text = text:gsub("\n", "\n")
putclip(text)
else
sleep(1)
end
t1_post()
end
local function weixin_text_share(window, text)
if text then
text = text:gsub("\n", "\n")
end
if using_scroll_lock then
adb_event{'key', 'scroll_lock', 961, 171}
elseif using_smartisan_os then
adb_event(
[[
adb-key SPACE
adb-tap
adb-tap 117 283 adb-tap 117 283 adb-tap 325 170 adb-tap 860 155 adb-tap 961 171
]])
elseif using_xiaomi_os then
adb_event("adb-long-press-800 422 270 adb-tap 147 213 adb-tap 1007 134")
elseif using_oppo_os then
adb_event("adb-tap 87 312 adb-tap 92 156 adb-tap 947 132")
else
adb_event("adb-key space adb-long-press-800 111 369 adb-tap 97 265 adb-tap 991 166")
end
end
local function t1_sms(window)
if using_scroll_lock then
adb_event{182, 1079, 'key', 'scroll_lock', 864, 921}
else
local input_method, ime_height = adb_get_input_window_dump()
if ime_height == 0 then
adb_event("adb-tap 182 1079 sleep .8")
end
local y_double_click = 928
local y_paste = 811
local y_send = y_double_click
adb_event(
([[
adb-long-press-800 522 %d
adb-tap 149 %d
adb-tap 919 %d
]]):format(y_double_click, y_paste, y_send)
)
end
end
local function t1_google_plus(window)
if using_scroll_lock then
adb_event{467, 650, 'key', 'scroll_lock', 932, 1818}
else
adb_event(
[[
adb-tap 233 503
sleep .5
adb-tap 571 1821
adb-tap 571 1821
]])
local input_method, ime_height = adb_get_input_window_dump()
if ime_height ~= 0 then
adb_event("key back")
end
adb_event(
[[
adb-tap-2 105 464
adb-tap 286 259
adb-tap 875 255
adb-tap 922 1819
]]
)
end
end
local function t1_smartisan_notes(window)
if using_scroll_lock then
adb_event{'key', 'scroll_lock', 940, 140, 933, 117, 323, 1272, 919, 123}
else
adb_event(
[[
adb-long-press 428 412
adb-tap 80 271
adb-tap 940 140
adb-tap 933 117
adb-tap 323 1272
adb-tap 919 123
]]
)
end
end
local function t1_mail(window)
if window == 'com.android.email/com.android.email.activity.Welcome' or window == 'com.android.email/com.android.email2.ui.MailActivityEmail' then
adb_tap_mid_bot()
sleep(2)
end
if using_scroll_lock then
adb_event{'key', 'scroll_lock'}
else
local input_method, ime_height = adb_get_input_window_dump()
local virtual_key_ratio = app_height / init_height
local ime_height_diff = ime_height / (init_height / default_height) - ime_height_ref
local y_start_scroll = 1022 / virtual_key_ratio - ime_height_diff
adb_event(
([[
adb-swipe-300 586 %d 586 68
adb-tap 560 1840
adb-tap-2 299 299
adb-tap 505 192
]]):format(y_start_scroll)
)
end
if window == 'com.google.android.gm/com.google.android.gm.ComposeActivityGmail' then
adb_event{806, 178}
else
adb_event("sleep .1 adb-tap 998 174")
end
end
local function t1_paste()
if using_scroll_lock then
adb_event{'key', 'scroll_lock'}
else
return "无法在此窗口内贴粘"
end
end
local function last(func)
local x, y
y = func()
while y do
x = y
y = func()
end
return x
end
adb_get_input_window_dump = function()
-- $(adb dumpsys window | perl -ne 'print if m/^\s*Window #\d+ Window\{[a-f0-9]+.*\SInputMethod/i .. m/^\s*mHasSurface/')
local dump = adb_pipe{'dumpsys', 'window'}
local input_method = {}
local started = false
dump = split("\n", dump)
for i = 1, #dump do
if not started and dump[i]:match("^%s*Window #?%d* ?Window{[a-f0-9]+.*%sInputMethod") then
started = true
debugging("started is true")
end
if started == true then
debugging("got a line: %s", dump[i])
input_method[#input_method + 1] = dump[i]
if dump[i]:match("^%s*mHasSurface") then
started = false
end
end
end
local input_window_dump = join("\n", input_method)
local input_method = string.match(input_window_dump, "mHasSurface=true")
local ime_xy = last(string.gmatch(input_window_dump, "Requested w=%d+ h=%d+"))
local ime_height = 0
if input_method and ime_xy:match('Requested w=%d+ h=') then
ime_height = ime_xy:match('Requested w=%d+ h=(%d+)')
if tonumber((ime_height - (init_height - app_height)) * default_height / init_height ) >= 1200 then -- new version of google pinyin ime?
if input_window_dump:match('package=com.google.android.inputmethod.pinyin') then
ime_height = (1920 - 1140) * init_height / default_height + (init_height - app_height)
elseif input_window_dump:match('package=com.google.android.inputmethod.latin') then
ime_height = 800 * init_height / default_height + (init_height - app_height)
end
end
end
return input_method, ime_height
end
local function adb_input_method_is_null()
-- if adb dumpsys input_method | grep mServedInputConnection=null -q; then
local dump = adb_pipe{'dumpsys', 'input_method'}
if dump:match("mServedInputConnection=null") then
return true
else
return false
end
end
check_phone = function()
if not adb_pipe(UNAME_CMD):match("Linux") then
error("Error: can't put text on phone, not connected?")
end
end
adb_start_service_and_wait_file_gone = function(service_cmd, file)
adb_am("am startservice --user 0 -n " .. service_cmd)
adb_shell(
(
[[
for x in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20; do
if test -e %s; then
sleep .1 || busybox sleep .1;
echo $x;
else
exit;
fi;
done
]]):format(file))
end
adb_start_service_and_wait_file = function(service_cmd, file)
adb_shell(
(
[[
rm %s;
am startservice --user 0 -n %s&
for x in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20; do
if test ! -e %s; then
sleep .1 || busybox sleep .1;
echo $x;
else
exit;
fi;
done
]]):format(file, service_cmd, file))
end
push_text = function(text)
if not text and os.getenv("PUTCLIP_ANDROID_FILE") then
local file = io.open(os.getenv("PUTCLIP_ANDROID_FILE"))
text = file:read("*a")
file:close()
local window = adb_focused_window()
if window:match("com.tencent.mobileqq") then
text = emoji_for_qq(text)
end
end
local file, path
local tmp = os.getenv("TEMP") or "/tmp"
path = tmp .. package.config:sub(1, 1) .. "lua-smartisan-t1.txt"
file = io.open(path, "w")
if not file then
error("TEMP env not set")
end
file:write(text)
file:close()
check_phone()
system{the_true_adb, 'push', path, '/sdcard/putclip.txt'}
end
putclip = function(text)
push_text(text)
adb_start_service_and_wait_file_gone('com.bhj.setclip/.PutClipService', '/sdcard/putclip.txt')
end
t1_config = function()
-- install the apk
system(the_true_adb .. " devices")
local uname = adb_pipe(UNAME_CMD)
if not uname:match("Linux") then
local home = os.getenv("HOME")
if is_windows then
home = os.getenv("USERPROFILE")
end
if not home or home == "" or home == "/" then
error("Your HOME environment variable is not set up correctly: '" .. home .. "'")
end
local dot_android = home .. package.config:sub(1, 1) .. ".android"
if is_windows then
system{"md", dot_android}
else
system{"mkdir", "-p", dot_android}
end
local ini = dot_android .. package.config:sub(1, 1) .. "adb_usb.ini"
local ini_file = io.open(ini, "r")
local done_vid = true
if not ini_file then
done_vid = false
else
local ini_lines = ini_file:read("*a")
if ini_lines ~= "0x29a9\n" then
done_vid = false
end
ini_file:close()
end
if not done_vid then
local err
ini_file, err = io.open(ini, "w")
if not ini_file then
error("can't open " .. ini .. ": " .. err)
end
ini_file:write("0x29a9\n")
ini_file:close()
system{the_true_adb, "kill-server"}
error("Done config for your adb devices, please try again")
else
error("No phone found, can't set up, uname is: " .. uname)
end
end
local setclip_phone_md5 = adb_pipe("cat /sdcard/t1wrench-setclip.md5")
local md5file = io.open("setclip.apk.md5")
local setclip_local_md5 = md5file:read("*a")
io.close(md5file)
debugging("on phone: %s, local: %s", setclip_phone_md5, setclip_local_md5)
if setclip_phone_md5 ~= setclip_local_md5 then
local install_output = io.popen(the_true_adb .. " install -r SetClip.apk"):read("*a")
if install_output:match("\nSuccess\r?\n") then
system(the_true_adb .. " push setclip.apk.md5 /sdcard/t1wrench-setclip.md5")
local setclip_phone_md5 = adb_pipe("cat /sdcard/t1wrench-setclip.md5")
local md5file = io.open("setclip.apk.md5")
local setclip_local_md5 = md5file:read("*a")
io.close(md5file)
if setclip_phone_md5 ~= setclip_local_md5 then
error("Can't mark the setclip.apk as been installed")
end
else
if not os.execute("test -e .quiet-apk-install-failure") then
error("Install setclip.apk failed, output is " .. install_output)
end
end
end
local weixin_phone_file, _, errno = io.open("weixin-phones.txt", "rb")
if not vcf_file then
adb_start_service_and_wait_file("com.bhj.setclip/.PutClipService --ei listcontacts 1", "/sdcard/listcontacts.txt")
system(the_true_adb .. " pull /sdcard/listcontacts.txt weixin-phones.txt")
end
sdk_version = adb_pipe("getprop ro.build.version.sdk")
brand = adb_pipe("getprop ro.product.brand"):gsub("\n.*", "")
model = adb_pipe("getprop ro.product.model"):gsub("\n.*", "")
debugging("sdk is %s\nbrand is %s\nmodel is %s\n", sdk_version, brand, model)
sdk_version = tonumber(sdk_version)
if tonumber(sdk_version) < 16 then
error("Error, you phone's sdk version is " .. sdk_version .. ", must be at least 16")
end
local dump = adb_pipe{'dumpsys', 'window'}
init_width = dump:match('init=(%d+x%d+)')
init_height = tonumber(init_width:match('x(%d+)'))
init_width = tonumber(init_width:match('(%d+)x'))
app_width = dump:match('app=(%d+x%d+)')
app_height = app_width:match('x(%d+)')
app_width = app_width:match('(%d+)x')
width_ratio, height_ratio = app_width / default_width, app_height / default_height
if brand:match("smartisan") then
using_smartisan_os = true
else
using_smartisan_os = false
end
if brand:match("Xiaomi") then
using_xiaomi_os = true
elseif brand:match("OPPO") then
using_oppo_os = true
end
local id = adb_pipe("id")
if id:match("uid=0") then
using_adb_root = true
else
using_adb_root = false
end
local scroll = adb_pipe("getprop persist.smartisan.pastetool")
if scroll:match("1") then
debugging("pastetool is true")
using_scroll_lock = true
else
using_scroll_lock = false
debugging("pastetool is false")
end
return ("brand is %s, paste is %s"):format(brand, using_scroll_lock)
end
get_a_note = function(text)
if text then
push_text(text)
end
adb_am("am startservice --user 0 -n com.bhj.setclip/.PutClipService --ei share-to-note 1")
for i = 1, 10 do
local window = adb_focused_window()
if window ~= 'com.smartisanos.notes/com.smartisanos.notes.NotesActivity' then
sleep(.1 * i)
end
end
adb_event(
[[
adb-tap 958 135
adb-tap 958 135
sleep .1
adb-tap 344 1636
sleep .1
adb-tap 711 151
]])
adb_event(
[[
sleep .5
adb-key BACK
sleep .5
adb-key BACK
]])
adb_get_last_pic('notes', true)
end
adb_get_last_pic = function(which, remove)
if which == 'notes' then
local dir = '/sdcard/smartisan/notes'
local ls_out1 = adb_pipe("busybox ls -t -1 " .. dir)
ls_out1 = ls_out1:gsub("\n.*", "")
ls_out1 = ls_out1:gsub("\x1b.-m", "")
ls_out1 = ls_out1:gsub("%?+", "*")
if ls_out1:match('%*') then
ls_out1 = adb_pipe(('bash -c "ls %s/%s"'):format(dir, ls_out1))
ls_out1 = ls_out1:gsub("\n", "")
ls_out1 = ls_out1:gsub(".*/", "")
end
system{the_true_adb, "pull", ("%s/%s"):format(dir, ls_out1), ("last-pic-%s.png"):format(which)}
if remove then
system{the_true_adb, "shell", "rm", ("%s/%s"):format(dir, ls_out1)}
adb_am(("am startservice --user 0 -n com.bhj.setclip/.PutClipService --es picture %s/%s"):format(dir, ls_out1))
end
end
end
t1_post = function(text) -- use weixin
local window = adb_focused_window()
if text then
if window:match("com.tencent.mobileqq") then
putclip(emoji_for_qq(text))
elseif window:match("com.tencent.mm/") then
putclip(emoji_for_weixin(text))
else
putclip(text)
end
end
if window then print("window is " .. window) end
if window == "com.immomo.momo/com.immomo.momo.android.activity.feed.PublishFeedActivity"
or (window:match("^com.sina.weibo/") and not window:match("com.sina.weibo/com.sina.weibo.weiyou.DMSingleChatActivity"))
then
weibo_text_share(window)
return
elseif window == "com.google.android.gm/com.google.android.gm.ConversationListActivityGmail" then
local how = select_args{"请选择回复方法", "单独回复", "群体回复", "手动回复"}
if how == "单独回复" then
adb_event("key dpad_down key dpad_down key tab key tab key enter sleep 1")
elseif how == "群体回复" then
adb_event("key dpad_down key tab key dpad_down key enter key enter key tab key tab key enter sleep 1")
else
adb_event("sleep 1")
end
t1_post()
return
elseif window == "com.google.android.gm/com.google.android.gm.ComposeActivityGmail" then
adb_event("key scroll_lock adb-tap 870 173")
return
elseif window == "com.tencent.mobileqq/com.tencent.mobileqq.activity.QQLSActivity" then
adb_event("adb-tap 297 884 key scroll_lock adb-tap 923 909")
elseif window == "com.tencent.mm/com.tencent.mm.plugin.sns.ui.SnsUploadUI" or window == "com.tencent.mm/com.tencent.mm.plugin.sns.ui.SnsCommentUI" then
weixin_text_share(window, text)
return
elseif window == "SmsPopupDialog" then
t1_sms(window)
return
elseif window == "com.google.android.apps.plus/com.google.android.apps.plus.phone.sharebox.PlusShareboxActivity" then
t1_google_plus(window)
return
elseif window == "com.smartisanos.notes/com.smartisanos.notes.NotesActivity" then
t1_smartisan_notes(window)
return
elseif window == "com.android.email/com.android.mail.compose.ComposeActivity" or
window == "com.android.email/com.android.email.activity.Welcome" or
window == "com.android.email/com.android.email2.ui.MailActivityEmail" or
window == "com.android.email/com.android.email.activity.ComposeActivityEmail" then
t1_mail(window)
return
elseif string.match(window, "^PopupWindow:") then
t1_paste()
return
else
local add, post_button = '', '958 1820'
local input_method, ime_height = adb_get_input_window_dump() -- $(adb dumpsys window | perl -ne 'print if m/^\s*Window #\d+ Window\{[a-f0-9]* u0 InputMethod\}/i .. m/^\s*mHasSurface/')
-- debugging("input_method is %s", input_method)
-- debugging("ime_xy is %s", ime_xy)
if input_method then
add = "key BACK"
else
add = "" -- # add="560 1840 key DEL key BACK"
end
if input_method then
if ime_height ~= 0 then
add = ''
post_button = ('984 %d'):format(1920 - ime_height - 100)
end
else
if adb_input_method_is_null() then -- if adb dumpsys input_method | grep mServedInputConnection=null -q; then
add = '560 1840 sleep .1 key back sleep .1'
end
end
if window == "com.github.mobile/com.github.mobile.ui.issue.CreateCommentActivity" then
post_button = '954 166'
end