-
Notifications
You must be signed in to change notification settings - Fork 8
/
config_unlock.js
2511 lines (2276 loc) · 83.7 KB
/
config_unlock.js
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
"ui";
/**
* @description graphic configuration tool for unlock module
*
* @example
* require("./MODULE_UNLOCK").unlock();
*
* @since Jan 9, 2020
* @author SuperMonster003 {@link https://github.com/SuperMonster003}
*
* @modifiedBy NickHopps July 24, 2020
*/
/* ========================= INITIATE ========================= */
//检测并开启无障碍服务(作为单文件运行时需要去掉这段并手动打开无障碍服务)
let ispt = require("./lib/Inspector");
ispt.checkA11yService(true);
// 设置屏幕为纵向显示
let _ActivityInfo = android.content.pm.ActivityInfo;
let _ORIENTATION = _ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
activity.setRequestedOrientation(_ORIENTATION);
/* ======================= END INITIATE ======================= */
/* ====================== PREREQUISITES ======================= */
// 默认解锁设置
let DEFAULT_UNLOCK = {
unlock_code: null,
unlock_max_try_times: 20,
unlock_pattern_strategy: "segmental",
unlock_pattern_size: 3,
unlock_pattern_swipe_time_segmental: 120,
unlock_pattern_swipe_time_solid: 200,
unlock_dismiss_layer_bottom: 0.8,
unlock_dismiss_layer_top: 0.2,
unlock_dismiss_layer_swipe_time: 110,
};
// 默认UI设置
let DEFAULT_SETTINGS = {
item_area_width: 0.78,
subhead_color: "#03a6ef",
subhead_highlight_color: "#bf360c",
info_color: "#78909c",
title_default_color: "#202020",
title_caution_color: "#b71c1c",
title_bg_color: "#03a6ef",
list_title_bg_color: "#afefff",
btn_on_color: "#ffffff",
btn_off_color: "#bbcccc",
split_line_color: "#bbcccc",
caution_btn_color: "#ff3d00",
attraction_btn_color: "#7b1fa2",
warn_btn_color: "#f57c00",
content_default_color: "#757575",
content_dark_color: "#546e7a",
content_warn_color: "#ad1457",
hint_btn_dark_color: "#a1887f",
hint_btn_bright_color: "#26a69a",
};
// 加载依赖
let dialogs = ModuleDialog(runtime, global);
let storage = ModuleStorage();
let { encrypt } = ModulePWMAP();
let { WIDTH, cX } = getDisplay();
let {
equalObjects,
deepCloneObject,
alertTitle,
waitForAction,
classof,
keycode,
} = ModuleCommonFunc();
/* ==================== END PREREQUISITES ===================== */
// 加载本地储存
let storage_unlock = storage.create("unlock");
let session_params = {};
let view_pages = {};
let dynamic_views = [];
let storage_config = initStorageConfig();
let session_config = deepCloneObject(storage_config);
let needSave = () => !equalObjects(session_config, storage_config);
let saveNow = () => storage_unlock.put("config", storage_config = deepCloneObject(session_config));
let updateAllValues = (only_new_flag) => {
let raw_idx = session_params.dynamic_views_raw_idx || 0;
dynamic_views.slice(only_new_flag ? raw_idx : 0).forEach(view => view.updateOpr(view));
if (only_new_flag) session_params.dynamic_views_raw_idx = dynamic_views.length;
};
let saveSession = (key, value, quiet_flag) => {
if (value !== undefined) {
if (classof(value, "Object") && classof(session_config[key], "Object")) {
Object.keys(value).forEach((k) => {
let v = value[k];
if (typeof v === "object") {
session_config[key][k] = Object.assign({}, session_config[key][k], v);
} else {
session_config[key][k] = v;
}
})
} else session_config[key] = value;
}
if (quiet_flag) return;
updateAllValues();
threads.start(function () {
let btn_save = null;
waitForAction(() => btn_save = session_params["homepage_btn_save"], 10e3, 80);
ui.post(() => needSave() ? btn_save.switch_on() : btn_save.switch_off());
});
};
let defs = Object.assign({}, DEFAULT_SETTINGS, {
homepage_title: "自动解锁配置",
item_area_width: cX(DEFAULT_SETTINGS.item_area_width) + "px",
title_bg_color: "#000000",
dialog_contents: DialogContents().dialog_contents,
});
initUI("#000000");
setHomePage(defs.homepage_title)
.add("subhead", new Layout("基本设置"))
.add("button", new Layout("锁屏密码", {
config_conj: "unlock_code",
hint: "加载中...",
newWindow() {
let diag = dialogs.builds(["设置锁屏解锁密码", this.config_conj, ["查看示例", "hint_btn_bright_color"], "返回", "确认", 1], {
inputHint: "密码将以密文形式存储在本地",
});
diag.on("neutral", () => {
let diag_demo = dialogs.builds(["锁屏密码示例", "unlock_code_demo", ["了解点阵简化", "hint_btn_bright_color"], 0, "关闭", 1]);
diag_demo.on("neutral", () => {
let diag_simp = dialogs.builds(["图案解锁密码简化", "about_pattern_simplification", 0, 0, "关闭", 1]);
diag_simp.on("positive", () => diag_simp.dismiss());
diag_simp.show();
});
diag_demo.on("positive", () => diag_demo.dismiss());
diag_demo.show();
});
diag.on("negative", () => diag.dismiss());
diag.on("positive", () => {
let input = diag.getInputEditText().getText().toString();
if (input && input.length < 3) return alertTitle(diag, "密码长度不小于 3 位");
if (input && !storage_unlock.get("unlock_code_safe_dialog_prompt_prompted")) {
let unlock_code_safe_dialog_prompt_prompted = false;
let diag_prompt = dialogs.builds([
"风险提示", "unlock_code_safe_confirm",
["了解详情", "hint_btn_bright_color"], "放弃", ["继续", "caution_btn_color"], 1, 1
]);
diag_prompt.on("check", checked => unlock_code_safe_dialog_prompt_prompted = !!checked);
diag_prompt.on("neutral", () => {
let diag_about = dialogs.builds([
"设备遗失对策", "about_lost_device_solution",
0, 0, "关闭", 1
]).on("positive", diag => diag.dismiss()).show();
let content_view = diag_about.getContentView();
let content_text_ori = content_view.getText().toString();
content_view.setAutoLinkMask(android.text.util.Linkify.WEB_URLS);
content_view.setText(content_text_ori);
});
diag_prompt.on("negative", () => diag_prompt.dismiss());
diag_prompt.on("positive", () => {
if (unlock_code_safe_dialog_prompt_prompted) {
storage_unlock.put("unlock_code_safe_dialog_prompt_prompted", true);
}
saveSession(this.config_conj, input ? encrypt(input) : "");
diag.dismiss();
diag_prompt.dismiss();
});
diag_prompt.show();
} else {
saveSession(this.config_conj, input ? encrypt(input) : "");
diag.dismiss();
}
});
diag.show();
},
updateOpr(view) {
view._hint.text(session_config[this.config_conj] ? "已设置" : "空");
},
}))
.add("split_line")
.add("subhead", new Layout("高级设置"))
.add("button", new Layout("最大尝试次数", {
config_conj: "unlock_max_try_times",
hint: "加载中...",
newWindow() {
let diag = dialogs.builds([
"设置解锁最大尝试次数", "",
["使用默认值", "hint_btn_dark_color"], "返回", "确认修改", 1,
], {
inputHint: "{x|5<=x<=50,x∈N}"
});
diag.on("neutral", () => diag.getInputEditText().setText(DEFAULT_UNLOCK[this.config_conj].toString()));
diag.on("negative", () => diag.dismiss());
diag.on("positive", dialog => {
let input = diag.getInputEditText().getText().toString();
if (input === "") return dialog.dismiss();
let value = +input;
if (isNaN(value)) return alertTitle(dialog, "输入值类型不合法");
if (value > 50 || value < 5) return alertTitle(dialog, "输入值范围不合法");
saveSession(this.config_conj, ~~value);
diag.dismiss();
});
diag.show();
},
updateOpr(view) {
view._hint.text((session_config[this.config_conj] || DEFAULT_UNLOCK[this.config_conj]).toString());
},
}))
.add("split_line")
.add("subhead", new Layout("提示层页面设置", {
subhead_color: "#bf360c"
}))
.add("button", new Layout("上滑时长", {
config_conj: "unlock_dismiss_layer_swipe_time",
hint: "加载中...",
newWindow() {
let diag = dialogs.builds([
"提示层页面上滑时长", this.config_conj,
["使用默认值", "hint_btn_dark_color"], "返回", "确认修改", 1,
], {
inputHint: "{x|110<=x<=1000,x∈N}"
});
diag.on("neutral", () => diag.getInputEditText().setText(DEFAULT_UNLOCK[this.config_conj].toString()));
diag.on("negative", () => diag.dismiss());
diag.on("positive", dialog => {
let input = diag.getInputEditText().getText().toString();
if (input === "") return dialog.dismiss();
let value = +input;
if (isNaN(value)) return alertTitle(dialog, "输入值类型不合法");
if (value > 1000 || value < 110) return alertTitle(dialog, "输入值范围不合法");
saveSession(this.config_conj, ~~value);
diag.dismiss();
});
diag.show();
},
updateOpr(view) {
view._hint.text((session_config[this.config_conj] || DEFAULT_UNLOCK[this.config_conj]).toString() + " ms");
},
}))
.add("button", new Layout("起点位置", {
config_conj: "unlock_dismiss_layer_bottom",
hint: "加载中...",
newWindow() {
let diag = dialogs.builds([
"提示层页面起点位置", this.config_conj,
["使用默认值", "hint_btn_dark_color"], "返回", "确认修改", 1,
], {
inputHint: "{x|0.5<=x<=0.95,x∈R+}"
});
diag.on("neutral", () => diag.getInputEditText().setText(DEFAULT_UNLOCK[this.config_conj].toString()));
diag.on("negative", () => diag.dismiss());
diag.on("positive", dialog => {
let input = diag.getInputEditText().getText().toString();
if (input === "") return dialog.dismiss();
input = +input;
if (isNaN(input)) return alertTitle(dialog, "输入值类型不合法");
let value = +(input.toFixed(2));
if (value > 0.95 || value < 0.5) return alertTitle(dialog, "输入值范围不合法");
saveSession(this.config_conj, value);
diag.dismiss();
});
diag.show();
},
updateOpr(view) {
let value = (session_config[this.config_conj] || DEFAULT_UNLOCK[this.config_conj]) * 100;
view._hint.text(value.toString() + "% H");
},
}))
.add("button", new Layout("终点位置", {
config_conj: "unlock_dismiss_layer_top",
hint: "加载中...",
newWindow() {
let diag = dialogs.builds([
"提示层页面终点位置", this.config_conj,
["使用默认值", "hint_btn_dark_color"], "返回", "确认修改", 1,
], {
inputHint: "{x|0.05<=x<=0.3,x∈R+}"
});
diag.on("neutral", () => diag.getInputEditText().setText(DEFAULT_UNLOCK[this.config_conj].toString()));
diag.on("negative", () => diag.dismiss());
diag.on("positive", dialog => {
let input = diag.getInputEditText().getText().toString();
if (input === "") return dialog.dismiss();
input = +input;
if (isNaN(input)) return alertTitle(dialog, "输入值类型不合法");
let value = +(input.toFixed(2));
if (value > 0.3 || value < 0.05) return alertTitle(dialog, "输入值范围不合法");
saveSession(this.config_conj, value);
diag.dismiss();
});
diag.show();
},
updateOpr(view) {
let value = (session_config[this.config_conj] || DEFAULT_UNLOCK[this.config_conj]) * 100;
view._hint.text(value.toString() + "% H");
},
}))
.add("split_line")
.add("subhead", new Layout("图案解锁设置", {
subhead_color: "#bf360c"
}))
.add("button", new Layout("滑动策略", {
config_conj: "unlock_pattern_strategy",
hint: "加载中...",
map: {
"segmental": "叠加路径", // gestures()
"solid": "连续路径", // gesture()
},
newWindow() {
let map = this.map;
let map_keys = Object.keys(map);
let diag = dialogs.builds(["图案解锁滑动策略", "", ["了解详情", "hint_btn_bright_color"], "返回", "确认修改", 1], {
items: map_keys.slice().map(value => map[value]),
itemsSelectMode: "single",
itemsSelectedIndex: map_keys.indexOf((session_config[this.config_conj] || DEFAULT_UNLOCK[this.config_conj]).toString()),
});
diag.on("neutral", () => {
let diag_about = dialogs.builds(["关于图案解锁滑动策略", "about_unlock_pattern_strategy", 0, 0, "关闭", 1]);
diag_about.on("positive", () => diag_about.dismiss());
diag_about.show();
});
diag.on("negative", () => diag.dismiss());
diag.on("positive", () => {
saveSession(this.config_conj, map_keys[diag.selectedIndex]);
diag.dismiss();
});
diag.show();
},
updateOpr(view) {
view._hint.text(this.map[(session_config[this.config_conj] || DEFAULT_UNLOCK[this.config_conj]).toString()]);
},
}))
.add("button", new Layout("滑动时长", {
config_conj: () => "unlock_pattern_swipe_time_" +
(session_config.unlock_pattern_strategy || DEFAULT_UNLOCK.unlock_pattern_strategy),
hint: "加载中...",
newWindow() {
let config_conj = this.config_conj();
let diag = dialogs.builds([
"设置图案解锁滑动时长", config_conj,
["使用默认值", "hint_btn_dark_color"], "返回", "确认修改", 1,
], {
inputHint: "{x|120<=x<=3000,x∈N}"
});
diag.on("neutral", () => diag.getInputEditText().setText(DEFAULT_UNLOCK[config_conj].toString()));
diag.on("negative", () => diag.dismiss());
diag.on("positive", dialog => {
let input = diag.getInputEditText().getText().toString();
if (input === "") return dialog.dismiss();
let value = +input;
if (isNaN(value)) return alertTitle(dialog, "输入值类型不合法");
if (value > 3000 || value < 120) return alertTitle(dialog, "输入值范围不合法");
saveSession(config_conj, ~~value);
diag.dismiss();
});
diag.show();
},
updateOpr(view) {
let config_conj = this.config_conj();
view._hint.text((session_config[config_conj] || DEFAULT_UNLOCK[config_conj]).toString() + " ms");
},
}))
.add("button", new Layout("点阵边长", {
config_conj: "unlock_pattern_size",
hint: "加载中...",
newWindow() {
let diag = dialogs.builds([
"设置图案解锁边长", this.config_conj,
["使用默认值", "hint_btn_dark_color"], "返回", "确认修改", 1,
], {
inputHint: "{x|3<=x<=6,x∈N}"
});
diag.on("neutral", () => diag.getInputEditText().setText(DEFAULT_UNLOCK[this.config_conj].toString()));
diag.on("negative", () => diag.dismiss());
diag.on("positive", dialog => {
let input = diag.getInputEditText().getText().toString();
if (input === "") return dialog.dismiss();
let value = +input;
if (isNaN(value)) return alertTitle(dialog, "输入值类型不合法");
if (value > 6 || value < 3) return alertTitle(dialog, "输入值范围不合法");
saveSession(this.config_conj, ~~value);
diag.dismiss();
});
diag.show();
},
updateOpr(view) {
view._hint.text((session_config[this.config_conj] || DEFAULT_UNLOCK[this.config_conj]).toString());
},
}));
ui.emitter.on("back_pressed", e => {
if (!needSave()) return;
e.consumed = true; // make default "back" dysfunctional
let diag = dialogs.builds([
"自动解锁配置未保存", "确定要退出吗",
"返回", ["强制退出", "caution_btn_color"],
["保存并退出", "hint_btn_bright_color"], 1,
]);
let dismissDiag = (save_flag, exit_flag) => {
if (save_flag) saveNow();
diag.dismiss();
if (exit_flag) exit();
};
diag.show()
.on("neutral", () => dismissDiag())
.on("negative", () => dismissDiag(false, "exit"))
.on("positive", () => dismissDiag("save", "exit"));
});
events.on("exit", () => {
listener.removeAllListeners();
threads.shutDownAll();
global.dialogs_pool.forEach((diag) => {
diag.dismiss();
diag = null;
});
ui.setContentView(ui.inflate('<vertical />'));
ui.main.getParent().removeAllViews();
ui.main.removeAllViews();
ui.finish();
});
updateAllValues();
// tool function(s) //
function initUI(status_bar_color) {
ui.layout(
'<vertical id="main"> \
<frame /> \
</vertical>'
);
ui.statusBarColor(status_bar_color || "#03a6ef");
}
function setHomePage(home_title, title_bg_color) {
let homepage = setPage(home_title, title_bg_color, parent_view => setButtons(parent_view, "homepage",
["save", "SAVE", "OFF", (btn_view) => {
if (!needSave()) return;
saveNow();
btn_view.switch_off();
toast("已保存");
}, {
btn_off_text_color: "#666666",
btn_off_icon_color: "#666666"
}]
));
ui.main.getParent().addView(homepage);
homepage._back_btn_area.setVisibility(8);
return homepage;
}
function setPage(title_param, title_bg_color, additions, options) {
let {no_margin_bottom, no_scroll_view, check_page_state} = options || {};
let title = title_param;
let view_page_name = "";
if (classof(title_param, "Array")) [title, view_page_name] = title_param;
session_params.page_title = title;
title_bg_color = title_bg_color || defs["title_bg_color"];
let new_view = ui.inflate('<vertical />');
new_view.addView(ui.inflate(
'<linear id="_title_bg" clickable="true"> \
<vertical id="_back_btn_area" marginRight="-22" layout_gravity="center"> \
<linear> \
<img src="@drawable/ic_chevron_left_black_48dp" h="31" bg="?selectableItemBackgroundBorderless" tint="#ffffff" layout_gravity="center"/> \
</linear> \
</vertical> \
<text id="_title_text" textColor="#ffffff" textSize="19" margin="16"/> \
</linear>'
));
new_view._back_btn_area.on("click", () => keycode(4));
new_view._title_text.text(title);
new_view._title_text.getPaint().setFakeBoldText(true);
let title_bg = typeof title_bg_color === "string" ? colors.parseColor(title_bg_color) : title_bg_color;
new_view._title_bg.setBackgroundColor(title_bg);
if (additions) typeof additions === "function" ? additions(new_view) : additions.forEach(f => f(new_view));
if (!no_scroll_view) {
new_view.addView(ui.inflate(
'<ScrollView> \
<vertical id="_page_content_view"/> \
</ScrollView>'
));
} else {
new_view.addView(ui.inflate(
'<vertical> \
<vertical id="_page_content_view"/> \
</vertical>'
));
}
if (!no_margin_bottom) {
new_view._page_content_view.addView(ui.inflate(
'<frame> \
<frame margin="0 0 0 8"/> \
</frame>'
));
}
new_view.add = (type, item_params) => {
let sub_view = setItem(type, item_params);
sub_view.view_page_name = view_page_name || null;
new_view._page_content_view.addView(sub_view);
if (sub_view.updateOpr) dynamic_views.push(sub_view);
return new_view;
};
if (view_page_name) {
view_pages[view_page_name] = new_view;
new_view.setTag(view_page_name);
}
return new_view;
// tool function(s) //
function setItem(type, item_params) {
let new_view = type.match(/^split_line/) && setSplitLine(item_params) ||
type === "subhead" && setSubHead(item_params) ||
type === "info" && setInfo(item_params) ||
type === "list" && setList(item_params) ||
type === "seekbar" && setSeekbar(item_params) ||
ui.inflate(
'<horizontal id="_item_area" padding="16 8" gravity="left|center"> \
<vertical id="_content" w="{{defs.item_area_width}}" h="40" gravity="left|center"> \
<text id="_title" textColor="#111111" textSize="16"/> \
</vertical> \
</horizontal>'
);
if (!item_params) return new_view;
Object.keys(item_params).forEach((key) => {
if (key.match(/listeners/)) return;
let item_data = item_params[key];
new_view[key] = typeof item_data === "function" ? item_data.bind(new_view) : item_data;
});
if (item_params.view_tag) new_view.setTag(item_params.view_tag);
if (type === "radio") {
new_view._item_area.removeAllViews();
let radiogroup_view = ui.inflate(
'<radiogroup id="_radiogroup" orientation="horizontal" padding="-6 0 0 0"/>'
);
item_params.view = new_view;
let title = item_params["title"];
let value = item_params["value"];
title.forEach(val => {
let radio_view = ui.inflate('<radio padding="0 0 12 0"/>');
radio_view.setText(val);
Object.keys(item_params.listener).forEach(listener => {
radio_view.on(listener, item_params.listener[listener].bind(item_params));
});
radiogroup_view._radiogroup.addView(radio_view);
});
new_view.addView(radiogroup_view);
}
let title = item_params["title"];
if (typeof title === "string" && new_view._title) new_view._title.text(title);
let hint = item_params["hint"];
if (hint) {
let hint_view = ui.inflate(
'<horizontal> \
<text id="_hint" textColor="#888888" textSize="13sp"/> \
<text id="_hint_color_indicator" visibility="gone" textColor="#888888" textSize="13sp"/> \
</horizontal>'
);
typeof hint === "string" && hint_view._hint.text(hint);
if (item_params.hint_text_color) hint_view._hint.setTextColor(item_params.hint_text_color);
new_view._content.addView(hint_view);
}
if (type.match(/.*switch$/)) {
let sw_view;
if (type === "switch") {
sw_view = ui.inflate(
'<switch id="_switch" checked="true" />'
);
if (item_params.default_state === false) sw_view._switch.setChecked(false);
}
if (type === "checkbox_switch") {
sw_view = ui.inflate(
'<vertical padding="8 0 0 0"> \
<checkbox id="_checkbox_switch" checked="true"/> \
</vertical>'
);
if (item_params.default_state === false) sw_view._checkbox_switch.setChecked(false);
}
new_view._item_area.addView(sw_view);
item_params.view = new_view;
let listener_ids = item_params["listener"];
Object.keys(listener_ids).forEach(id => {
let listeners = listener_ids[id];
Object.keys(listeners).forEach(listener => {
let callback = listeners[listener].bind(item_params);
if (id === "ui") ui.emitter.prependListener(listener, callback);
else new_view[id].on(listener, callback);
});
});
} else if (type.match(/^options/)) {
let opt_view = ui.inflate(
'<vertical id="_chevron_btn"> \
<img padding="10 0 0 0" src="@drawable/ic_chevron_right_black_48dp" h="31" bg="?selectableItemBackgroundBorderless" tint="#999999"/> \
</vertical>'
);
new_view._item_area.addView(opt_view);
item_params.view = new_view;
} else if (type === "button") {
let help_view = ui.inflate(
'<vertical id="_info_icon" visibility="gone"> \
<img src="@drawable/ic_info_outline_black_48dp" h="22" bg="?selectableItemBackgroundBorderless" tint="#888888"/> \
</vertical>'
);
new_view._item_area.addView(help_view);
item_params.view = new_view;
new_view._item_area.on("click", () => typeof item_params.showWindow === "function" && item_params.showWindow());
if (item_params.infoWindow) {
new_view._info_icon.setVisibility(0);
new_view._info_icon.on("click", () => item_params.infoWindow());
}
}
new_view.setNextPage = (page) => item_params.next_page = page;
new_view.getNextPage = () => item_params.next_page;
if (item_params.title_text_color) new_view._title.setTextColor(item_params.title_text_color);
return new_view;
// tool function(s) //
function setSplitLine(item) {
let line_color = item && item["split_line_color"] || defs["split_line_color"];
let new_view = ui.inflate(
'<vertical> \
<horizontal id="_line" w="*" h="1sp" margin="16 8"> \
</horizontal> \
</vertical>'
);
new_view.setTag(type);
line_color = typeof line_color === "string" ? colors.parseColor(line_color) : line_color;
new_view._line.setBackgroundColor(line_color);
new_view._line.setVisibility(type.match(/invisible/) ? 8 : 0);
return new_view;
}
function setSubHead(item) {
let title = item["title"];
let subhead_color = item["subhead_color"] || defs["subhead_color"];
let new_view = ui.inflate(
'<vertical> \
<text id="_text" textSize="14" margin="16 8"/> \
</vertical>'
);
new_view._text.text(title);
let title_color = typeof subhead_color === "string" ? colors.parseColor(subhead_color) : subhead_color;
new_view._text.setTextColor(title_color);
return new_view;
}
function setInfo(item) {
let title = item.title;
let info_color = item.info_color || defs.info_color;
session_params.info_color = info_color;
let new_view = ui.inflate(
'<horizontal> \
<linear padding="15 10 0 0"> \
<img src="@drawable/ic_info_outline_black_48dp" h="17" w="17" margin="0 1 4 0" tint="{{session_params.info_color}}"/> \
<text id="_info_text" textSize="13"/> \
</linear> \
</horizontal>'
);
new_view._info_text.text(title);
let title_color = typeof info_color === "string" ? colors.parseColor(info_color) : subhead_color;
new_view._info_text.setTextColor(title_color);
return new_view;
}
function setList(item_params) {
let list_title_bg_color = item_params["list_title_bg_color"] || defs["list_title_bg_color"];
let list_head = item_params["list_head"] || [];
list_head.forEach((o, idx) => {
let w = o.width;
if (!idx && !w) return session_params["list_width_0"] = ~~(0.3 * WIDTH) + "px";
session_params["list_width_" + idx] = w ? ~~(w * WIDTH) + "px" : -2;
});
session_params.list_checkbox = item_params["list_checkbox"];
let data_source_key_name = item_params["data_source_key_name"] || "unknown_key_name"; // just a key name
let getListItemName = num => list_head[num] && Object.keys(list_head[num])[0] || null;
// items are expected not more than 4
for (let i = 0; i < 4; i += 1) session_params["list_item_name_" + i] = getListItemName(i);
let new_view = ui.inflate(
'<vertical> \
<horizontal id="_list_title_bg"> \
<horizontal h="50" w="{{session_params[\'list_width_0\']}}" margin="8 0 0 0"> \
<checkbox id="_check_all" layout_gravity="left|center" clickable="false" /> \
</horizontal> \
</horizontal> \
<vertical> \
<list id="_list_data" fastScrollEnabled="true" focusable="true" scrollbars="none"> \
<horizontal> \
<horizontal w="{{this.width_0}}"> \
<checkbox id="_checkbox" checked="{{this.checked}}" h="50" margin="8 0 -16" layout_gravity="left|center" \
clickable="false" /> \
<text text="{{this.list_item_name_0}}" h="50" textSize="15" margin="16 0 0" ellipsize="end" lines="1" \
layout_gravity="left|center" gravity="left|center" /> \
</horizontal> \
<text text="{{this.list_item_name_1}}" w="{{session_params[\'list_width_1\'] || 1}}" \
visibility="{{session_params[\'list_item_name_1\'] ? \'visible\' : \'gone\'}}" textSize="15" h="50" margin="8 0 0 0" \
layout_gravity="left|center" gravity="left|center" /> \
<text text="{{this.list_item_name_2}}" w="{{session_params[\'list_width_2\'] || 1}}" \
visibility="{{session_params[\'list_item_name_2\'] ? \'visible\' : \'gone\'}}" textSize="15" h="50" \
layout_gravity="left|center" gravity="left|center" /> \
<text text="{{this.list_item_name_3}}" w="{{session_params[\'list_width_3\'] || 1}}" \
visibility="{{session_params[\'list_item_name_3\'] ? \'visible\' : \'gone\'}}" textSize="15" h="50" \
layout_gravity="left|center" gravity="left|center" /> \
</horizontal> \
</list> \
</vertical> \
</vertical>'
);
updateDataSource(data_source_key_name, "init", item_params.custom_data_source);
new_view["_check_all"].setVisibility(android.view.View[item_params["list_checkbox"].toUpperCase()]);
new_view["_list_data"].setDataSource(session_params[data_source_key_name]);
new_view["_list_title_bg"].attr("bg", list_title_bg_color);
new_view.setTag("list_page_view");
list_head.forEach((title_obj, idx) => {
let data_key_name = Object.keys(title_obj)[0];
let list_title_view = idx ? ui.inflate(
'<text textSize="15" />'
) : ui.inflate(
'<text textSize="15" padding="{{session_params.list_checkbox === \'gone\' ? 8 : 0}} 0 0 0" />'
);
list_title_view.setText(title_obj[data_key_name]);
list_title_view.on("click", () => {
if (!session_params[data_source_key_name][0]) return;
session_params["list_sort_flag_" + data_key_name] = session_params["list_sort_flag_" + data_key_name] ||
(session_params[data_source_key_name][0] < session_params[data_source_key_name][1] ? 1 : -1);
let session_data = session_params[data_source_key_name];
session_data = session_data.map((value, idx) => [idx, value]); // to attach indices
session_data.sort((a, b) => {
if (session_params["list_sort_flag_" + data_key_name] > 0) {
return a[1][a[1][data_key_name]] > b[1][b[1][data_key_name]];
} else return a[1][a[1][data_key_name]] < b[1][b[1][data_key_name]];
});
let indices_table = {};
session_data = session_data.map((value, idx) => {
indices_table[value[0]] = idx;
return value[1];
});
let deleted_items_idx = data_source_key_name + "_deleted_items_idx";
session_params[deleted_items_idx] = session_params[deleted_items_idx] || {};
let tmp_deleted_items_idx = {};
Object.keys(session_params[deleted_items_idx]).forEach(ori_idx_key => {
tmp_deleted_items_idx[indices_table[ori_idx_key]] = session_params[deleted_items_idx][ori_idx_key];
});
session_params[deleted_items_idx] = deepCloneObject(tmp_deleted_items_idx);
session_params[data_source_key_name].splice(0);
session_data.forEach(value => session_params[data_source_key_name].push(value));
session_params["list_sort_flag_" + data_key_name] *= -1;
// updateDataSource(data_source_key_name, "rewrite");
});
if (idx === 0) new_view["_check_all"].getParent().addView(list_title_view);
else new_view["_list_title_bg"].addView(list_title_view);
list_title_view.attr("gravity", "left|center");
list_title_view.attr("layout_gravity", "left|center");
list_title_view.attr("ellipsize", "end");
list_title_view.attr("lines", "1");
idx && list_title_view.attr("width", session_params["list_width_" + idx]);
});
item_params.view = new_view;
let listener_ids = item_params["listener"] || [];
Object.keys(listener_ids).forEach(id => {
let listeners = listener_ids[id];
Object.keys(listeners).forEach(listener => {
let callback = listeners[listener].bind(item_params);
if (id === "ui") ui.emitter.prependListener(listener, callback);
else new_view[id].on(listener, callback);
});
});
if (item_params.updateOpr) new_view.updateOpr = item_params.updateOpr.bind(new_view);
return new_view;
}
function setSeekbar(item_params) {
let {title, unit, config_conj, nums} = item_params;
let [min, max, init] = nums;
if (isNaN(+min)) min = 0;
if (isNaN(+init)) {
let _init = session_config[config_conj] || DEFAULT_AF[config_conj];
init = isNaN(+_init) ? min : _init;
}
if (isNaN(+max)) max = 100;
let new_view = ui.inflate(
'<vertical> \
<horizontal margin="16 8"> \
<text id="_text" gravity="left" layout_gravity="center"/> \
<seekbar id="_seekbar" w="*" style="@android:style/Widget.Material.SeekBar" layout_gravity="center"/> \
</horizontal> \
</vertical>'
);
new_view._seekbar.setMax(max - min);
new_view._seekbar.setProgress(init - min);
let update = (source) => new_view._text.setText((title ? title + ": " : "") + source.toString() + (unit ? " " + unit : ""));
update(init);
new_view._seekbar.setOnSeekBarChangeListener(
new android.widget.SeekBar.OnSeekBarChangeListener({
onProgressChanged(seek_bar, progress, from_user) {
let result = progress + min;
update(result);
$$save.session(config_conj, result);
},
onStartTrackingTouch: (seek_bar) => void 0,
onStopTrackingTouch: (seek_bar) => void 0,
})
);
return new_view;
}
}
}
function setButtons(parent_view, data_source_key_name, button_params_arr) {
let buttons_view = ui.inflate('<horizontal id="btn"/>');
let buttons_count = 0;
for (let i = 2, len = arguments.length; i < len; i += 1) {
let arg = arguments[i];
if (typeof arg !== "object") continue; // just in case
buttons_view.btn.addView(getButtonLayout.apply(null, arg));
buttons_count += 1;
}
parent_view._title_text.setWidth(~~((650 - 100 * buttons_count - (session_params.page_title !== defs.homepage_title ? 52 : 5)) * WIDTH / 720));
parent_view._title_bg.addView(buttons_view);
// tool function(s) //
function getButtonLayout(button_icon_file_name, button_text, switch_state, btn_click_listener, other_params) {
other_params = other_params || {};
session_params.button_icon_file_name = button_icon_file_name.replace(/^(ic_)?(.*?)(_black_48dp)?$/, "ic_$2_black_48dp");
session_params.button_text = button_text;
let btn_text = button_text.toLowerCase();
let btn_icon_id = "_icon_" + btn_text;
session_params.btn_icon_id = btn_icon_id;
let btn_text_id = "_text_" + btn_text;
session_params.btn_text_id = btn_text_id;
let def_on_color = defs.btn_on_color;
let def_off_color = defs.btn_off_color;
let view = buttonView();
let switch_on_color = [other_params["btn_on_icon_color"] || def_on_color, other_params["btn_on_text_color"] || def_on_color];
let switch_off_color = [other_params["btn_off_icon_color"] || def_off_color, other_params["btn_off_text_color"] || def_off_color];
view.switch_on = () => {
view[btn_icon_id].attr("tint", switch_on_color[0]);
view[btn_text_id].setTextColor(colors.parseColor(switch_on_color[1]));
};
view.switch_off = () => {
view[btn_icon_id].attr("tint", switch_off_color[0]);
view[btn_text_id].setTextColor(colors.parseColor(switch_off_color[1]));
};
switch_state === "OFF" ? view.switch_off() : view.switch_on();
view[btn_text_id].on("click", () => btn_click_listener && btn_click_listener(view));
session_params[data_source_key_name + "_btn_" + btn_text] = view;
return view;
// tool function(s) //
function buttonView() {
return ui.inflate(
'<vertical margin="13 0"> \
<img layout_gravity="center" id="{{session_params.btn_icon_id}}" src="@drawable/{{session_params.button_icon_file_name}}" h="31" bg="?selectableItemBackgroundBorderless" margin="0 7 0 0"/> \
<text w="50" id="{{session_params.btn_text_id}}" text="{{session_params.button_text}}" gravity="center" textSize="10" textStyle="bold" marginTop="-26" h="40" gravity="bottom|center"/> \
</vertical>'
);
}
}
}
// constructor(s) //
function Layout(title, params) {
params = params || {};
if (!classof(title, "Array")) this.title = title;
else {
this.title = title[0];
this.title_color = title[1];
}
Object.assign(this, params);
if (params.newWindow) {
Object.defineProperties(this, {
showWindow: {
get: () => params.newWindow.bind(this),
}
});
}
if (params.infoWindow) {
Object.defineProperties(this, {
infoWindow: {
get: () => params.infoWindow.bind(this),
}
});
}
if (params.listeners) {
Object.defineProperties(this, {
listener: {
get() {
return params.listeners;
},
},
});
}
if (params.updateOpr) {
Object.defineProperties(this, {
updateOpr: {
get: () => view => params.updateOpr(view),
},
});
}
}
function initStorageConfig() {
let storage_config = Object.assign({}, DEFAULT_UNLOCK, storage_unlock.get("config", {}));
storage_unlock.put("config", storage_config); // to refill storage data
return storage_config;
}
// module function(s) //
// updated at Nov 14, 2019
function ModuleDialog(__runtime__, scope) {
let dialogs = {};
dialogs.rawInput = function (title, prefill, callback) {
prefill = prefill || "";
if (isUiThread() && !callback) {
return new Promise(function (resolve, reject) {
rtDialogs().rawInput(title, prefill, function () {
resolve.apply(null, Array.prototype.slice.call(arguments));
});
});
}
return rtDialogs().rawInput(title, prefill, callback ? callback : null);
};
dialogs.input = function (title, prefill, callback) {
prefill = prefill || "";
if (isUiThread() && !callback) {
return new Promise(function (resolve, reject) {
rtDialogs().rawInput(title, prefill, function (str) {
resolve(eval(str));
});
});
}
if (callback) {
dialogs.rawInput(title, prefill, function (str) {
callback(eval(str));
});
return;
}
return eval(dialogs.rawInput(title, prefill), callback ? callback : null);
};
dialogs.prompt = dialogs.rawInput;
dialogs.alert = function (title, prefill, callback) {
prefill = prefill || "";
if (isUiThread() && !callback) {
return new Promise(function (resolve, reject) {
rtDialogs().alert(title, prefill, function () {