Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e0c710ff5f | ||
|
|
2946459f1d | ||
|
|
5b5f328e06 | ||
|
|
b496e18e7a | ||
|
|
4abb2ab0ce | ||
|
|
16edaacf37 | ||
|
|
b69aea9d14 | ||
|
|
656d372612 | ||
|
|
09a2ebcc61 | ||
|
|
b74d0e9006 | ||
|
|
a7c81130cd | ||
|
|
c0b75cdbe9 | ||
|
|
2c15c74697 | ||
|
|
ba9308bfbc | ||
|
|
21050b49d4 | ||
|
|
842da49b08 | ||
|
|
44fb2b70a5 | ||
|
|
9ef95f56cf | ||
|
|
b5b08404bd | ||
|
|
17abca7f7d | ||
|
|
c8d4782b2a | ||
|
|
e0af610c2c | ||
|
|
ddc645c323 | ||
|
|
779dd1f8fd | ||
|
|
cf10e2ff6d | ||
|
|
55d1094e55 | ||
|
|
8248ac6e2f | ||
|
|
4b2ac8ae5e | ||
|
|
085e0938cb | ||
|
|
9a2c0d7121 | ||
|
|
aa8834b941 | ||
|
|
d6b524bc47 | ||
|
|
1ca960995f | ||
|
|
31be34f551 | ||
|
|
e0d1c42391 | ||
|
|
1b9c3050aa | ||
|
|
5ba0afdd24 | ||
|
|
a8eb4ce47a | ||
|
|
feba6dfeb0 | ||
|
|
061f4a0ed0 | ||
|
|
f7c273a4f3 | ||
|
|
096dae3cd0 | ||
|
|
eebf51f4a6 | ||
|
|
000ae02d47 | ||
|
|
84c1ee3e88 | ||
|
|
0fea2076d9 | ||
|
|
b7a90b171f | ||
|
|
7a68f331b0 | ||
|
|
9e7abd5a85 | ||
|
|
9bc3f1137e | ||
|
|
0c7e3ac8b4 | ||
|
|
715f863167 | ||
|
|
e062301d42 | ||
|
|
77396558d7 | ||
|
|
d11c16c549 | ||
|
|
6a90ac40be | ||
|
|
25cc1d77b1 | ||
|
|
fd489d93bb | ||
|
|
e80487e379 | ||
|
|
3b4c2981ac | ||
|
|
348d45f2f5 | ||
|
|
4db96c7bdb | ||
|
|
b1617c6996 | ||
|
|
c078166e8d | ||
|
|
092936eda5 | ||
|
|
257a999300 | ||
|
|
df6a5040a5 | ||
|
|
3085ca2eeb | ||
|
|
fa4005cf9a | ||
|
|
c4cd375c04 | ||
|
|
19d9bb198f | ||
|
|
7016190ce5 | ||
|
|
8020ffa666 | ||
|
|
bc9914ffa6 | ||
|
|
4ded4ff470 | ||
|
|
7742299651 | ||
|
|
0b348a59d2 | ||
|
|
e51d54e346 | ||
|
|
e06b1f0177 | ||
|
|
178a870608 | ||
|
|
a7d3d7c651 | ||
|
|
c13024f1c7 | ||
|
|
c53c0ef727 | ||
|
|
cd93fe2646 | ||
|
|
7b506058a4 | ||
|
|
f658f74f35 | ||
|
|
eae5efa69d | ||
|
|
4e6051085e | ||
|
|
f174ccfc04 | ||
|
|
e1252d0554 | ||
|
|
afe638fb90 | ||
|
|
17699ce889 | ||
|
|
4afa06b7b9 | ||
|
|
2d02815aba | ||
|
|
ae1824c6a2 | ||
|
|
d63cc39478 | ||
|
|
51d6775429 | ||
|
|
35d92f1386 | ||
|
|
ba0ef6dccc | ||
|
|
2a325e6700 | ||
|
|
4e7566f06d | ||
|
|
d95bf4b71d | ||
|
|
43ddf6ccc4 | ||
|
|
b55da640e1 | ||
|
|
90534c3c9f | ||
|
|
88c9e5e902 | ||
|
|
ac41117a78 | ||
|
|
2418c5f625 | ||
|
|
52156aa9ff | ||
|
|
6010fb69a4 | ||
|
|
d5abbb3584 | ||
|
|
8c27504ee2 | ||
|
|
8a20525da3 | ||
|
|
c7b8326b52 | ||
|
|
ee8b80651e | ||
|
|
589676c52a | ||
|
|
83085d5f9a | ||
|
|
a1a204d0d5 | ||
|
|
9a43c36758 | ||
|
|
e017efe35f | ||
|
|
1645c17ab7 | ||
|
|
075b800932 | ||
|
|
56f9138db4 | ||
|
|
20c1b7acdb | ||
|
|
a10022d797 | ||
|
|
aafbbbec38 | ||
|
|
028a994070 | ||
|
|
581125db62 | ||
|
|
295770002a | ||
|
|
1a66d84bd0 | ||
|
|
cd3f51b66d | ||
|
|
6d8ac4c3da | ||
|
|
156382eaad | ||
|
|
2c155fe599 | ||
|
|
323de69ba0 | ||
|
|
0cdc6be34f | ||
|
|
36c1703763 | ||
|
|
ee3d2f681c | ||
|
|
11ce900868 | ||
|
|
b936dd2533 | ||
|
|
fab8cd0c1f | ||
|
|
ec3a962ec8 | ||
|
|
348813109b | ||
|
|
59598094df | ||
|
|
8b6ae59eda | ||
|
|
b3d8653882 | ||
|
|
719d1ebbb8 | ||
|
|
a23f902e9a | ||
|
|
79559635e1 | ||
|
|
c57a4e4132 | ||
|
|
c6f7d9d869 | ||
|
|
9fcddd06e4 | ||
|
|
3526a2b0e9 | ||
|
|
56187aa4dc | ||
|
|
9e698f382d | ||
|
|
d2583a6d96 | ||
|
|
250e4fddd9 | ||
|
|
2f274d969e | ||
|
|
2d02eda9e3 | ||
|
|
f26a9518e0 | ||
|
|
2b92bcf0a9 | ||
|
|
0ce6296318 | ||
|
|
2c90ceb5ca | ||
|
|
2fde327195 | ||
|
|
3aee440f67 | ||
|
|
290c1777b3 | ||
|
|
e0c8b64c65 | ||
|
|
810d339b4e | ||
|
|
53ce1e6934 | ||
|
|
ae4d3b2d65 | ||
|
|
1cdd51de21 | ||
|
|
9cf4702f69 | ||
|
|
cb5455c990 | ||
|
|
db86fef717 | ||
|
|
8bffeb52a7 | ||
|
|
9d2bbd164b | ||
|
|
a045766c7a | ||
|
|
34cff8c8cb | ||
|
|
03ec2af92e | ||
|
|
c2a8e27875 | ||
|
|
3fae5129d8 | ||
|
|
8311f7baaa | ||
|
|
17e39f94cc | ||
|
|
cd1e10c17c | ||
|
|
5defb428ff | ||
|
|
79a066e68a | ||
|
|
3e34bdf7fc | ||
|
|
bdf31231f0 | ||
|
|
17de611a22 | ||
|
|
4744191242 | ||
|
|
c81be6734a | ||
|
|
663fedb81b | ||
|
|
200d29dc1e | ||
|
|
41f6948f13 | ||
|
|
24e36b6283 | ||
|
|
1b1df14937 | ||
|
|
895f75a3c5 | ||
|
|
e428a8be57 | ||
|
|
a438d2dd88 | ||
|
|
f2107edaa3 | ||
|
|
644f93a46d | ||
|
|
dc52e0534e | ||
|
|
15e63eb86c | ||
|
|
842e59e893 | ||
|
|
2b7d6745ea | ||
|
|
3c41063a38 | ||
|
|
da8e84acad | ||
|
|
0b7658eb9b | ||
|
|
2d7bda3407 | ||
|
|
a7d11d70d4 | ||
|
|
4b25fb8363 | ||
|
|
b1aca0578c | ||
|
|
78d3021616 | ||
|
|
14a7a64af2 | ||
|
|
6156b30a36 | ||
|
|
231afd3e71 | ||
|
|
14803997ba | ||
|
|
9a99597d4e | ||
|
|
7079c3814b | ||
|
|
35a95e2b9b | ||
|
|
99d63c2396 | ||
|
|
62383ed31b | ||
|
|
a10f9f2ce8 | ||
|
|
924d3c4ea3 | ||
|
|
be83b53941 | ||
|
|
63657d5286 | ||
|
|
9bd670cfd3 | ||
|
|
2753804652 | ||
|
|
971c75e754 | ||
|
|
bbe29cbd72 | ||
|
|
f91deda5ba | ||
|
|
bd11a7fcc2 | ||
|
|
6edc8f06f3 | ||
|
|
7e3d170c83 | ||
|
|
cde3ea0c95 | ||
|
|
5a34fb3443 | ||
|
|
c000617928 | ||
|
|
01e4747483 | ||
|
|
77eb761fc3 | ||
|
|
64c95c1bc5 | ||
|
|
5e5f9ae4bc | ||
|
|
015059321a | ||
|
|
cc6ae217a4 | ||
|
|
a1c730caac | ||
|
|
365f6936a3 | ||
|
|
a6f2034377 | ||
|
|
95631ef468 | ||
|
|
5dc02c0bd5 | ||
|
|
ea43837e62 | ||
|
|
4c359e50f8 | ||
|
|
1feca411b6 | ||
|
|
4dfcf96897 | ||
|
|
90568f2216 | ||
|
|
4440763f44 | ||
|
|
5b80fee02e | ||
|
|
e21a0eca5c | ||
|
|
83a1096c08 | ||
|
|
59f1e7f471 | ||
|
|
12374fb573 | ||
|
|
734bffd279 | ||
|
|
478d6c9035 | ||
|
|
a6048c84bd | ||
|
|
c0ab14e241 | ||
|
|
23332bf4a6 | ||
|
|
a55293448e | ||
|
|
f809d724a0 | ||
|
|
32e2e91013 | ||
|
|
5b8d3a2824 | ||
|
|
d4b152e7c0 | ||
|
|
c62c84ae4d | ||
|
|
9e1869edde | ||
|
|
1a164ff8f1 | ||
|
|
6b3a4aec16 | ||
|
|
150a687f15 | ||
|
|
fc1d514475 | ||
|
|
5528bef09a | ||
|
|
4013740429 | ||
|
|
000a932ff1 | ||
|
|
60cd7728e6 | ||
|
|
4f37088e20 | ||
|
|
d7dc6487d0 | ||
|
|
61c9dcbd1d | ||
|
|
8f0404242d | ||
|
|
286af0e5bf | ||
|
|
6c49904bf8 | ||
|
|
509ee08e8e | ||
|
|
fdae6f52cc | ||
|
|
52ef7cd956 | ||
|
|
d8bd81a0b6 | ||
|
|
c2e33c7f50 | ||
|
|
a9498a1b92 | ||
|
|
f74b8373e3 | ||
|
|
809f628bc0 | ||
|
|
54a2b28219 | ||
|
|
0719c53e86 | ||
|
|
e9f3deaec0 | ||
|
|
8207be5940 | ||
|
|
c2e53cc8b9 | ||
|
|
251b99f76d | ||
|
|
c4c86104b5 | ||
|
|
857b1d9b57 | ||
|
|
277deace47 | ||
|
|
30c659110f | ||
|
|
a8b6adfba2 | ||
|
|
1548e16fbe | ||
|
|
0e34baf1bf | ||
|
|
2c461b5ccf | ||
|
|
2adbeddde3 | ||
|
|
e0a79fcc63 | ||
|
|
5e07813a57 | ||
|
|
400d478bca | ||
|
|
786ea2ca37 | ||
|
|
ba169d35a4 | ||
|
|
f2dbb31951 | ||
|
|
fe371f1d66 | ||
|
|
1b8ae87b08 | ||
|
|
fece5f64ce | ||
|
|
bcedbd85ef | ||
|
|
dc4ea5e96d | ||
|
|
7e0e01295f | ||
|
|
88268a22a9 | ||
|
|
428a1cb0d2 | ||
|
|
b3dda2a1ff | ||
|
|
74f9330c38 | ||
|
|
ebf99eda55 | ||
|
|
7d05e5382a | ||
|
|
e559520df3 | ||
|
|
a9b300d357 | ||
|
|
f58f930446 | ||
|
|
f10bf6d714 | ||
|
|
6898300c30 | ||
|
|
85bc6fdd28 | ||
|
|
4cdf5a8ba6 | ||
|
|
8ca220e98a | ||
|
|
ec2d37b295 | ||
|
|
3b2118331b | ||
|
|
ed89e504b0 | ||
|
|
30eec80177 | ||
|
|
07f730aba6 | ||
|
|
3bfd282614 | ||
|
|
d031954976 | ||
|
|
86f9b9d0f7 | ||
|
|
3b1f98d675 | ||
|
|
66e37c8f6b | ||
|
|
3fc6adb2b7 | ||
|
|
1c971c161f | ||
|
|
8883d048a7 | ||
|
|
66dbbb1eee | ||
|
|
cb7bbeba99 | ||
|
|
7bf8de139f | ||
|
|
b7ffc255f5 | ||
|
|
06aa4c60c8 | ||
|
|
07e4db9240 | ||
|
|
c0144809c6 | ||
|
|
ffeadc9e44 | ||
|
|
a15ed69624 | ||
|
|
b0fd40444f | ||
|
|
8cddc0d974 | ||
|
|
08b76b643d | ||
|
|
13c623b788 | ||
|
|
b77bd6b3f9 | ||
|
|
d3dec6981a | ||
|
|
5dd617be69 | ||
|
|
1b093469bf | ||
|
|
7286c81885 | ||
|
|
4ef7707736 | ||
|
|
c06a9f915d | ||
|
|
fe2907974a | ||
|
|
3e79617fc9 | ||
|
|
52e2fe6fed | ||
|
|
8e4265a5f8 | ||
|
|
623de00376 | ||
|
|
6320900d7c | ||
|
|
ab1ff284ca | ||
|
|
37ad3d0882 | ||
|
|
c94123da06 | ||
|
|
7eef1727a2 | ||
|
|
6f692db069 | ||
|
|
aefca644e8 | ||
|
|
99ff71b7e1 | ||
|
|
430ee19dd3 | ||
|
|
d9dbafb51b | ||
|
|
d78b3a7fa0 | ||
|
|
424dc6dd49 | ||
|
|
1f4347a316 | ||
|
|
acd9f2ae1c | ||
|
|
d753c36195 | ||
|
|
7dd0deb931 | ||
|
|
ba1126db0e | ||
|
|
359a417692 | ||
|
|
42ac0055c2 | ||
|
|
c0dd03e3f7 | ||
|
|
6b1bda629f | ||
|
|
0b43daf507 | ||
|
|
894038280e | ||
|
|
2704ab9afb | ||
|
|
8d604848da | ||
|
|
6f6f123880 | ||
|
|
620b037a17 | ||
|
|
a653454e7d | ||
|
|
38a8c9eb34 | ||
|
|
8cdf16a272 | ||
|
|
24333a24ae | ||
|
|
e83c542886 | ||
|
|
c61824b362 | ||
|
|
591f918412 | ||
|
|
ab1335102a | ||
|
|
41cdc33432 | ||
|
|
ef2cfec1a2 | ||
|
|
6f2a5d8cdb | ||
|
|
c3d52d430a | ||
|
|
806897adf8 | ||
|
|
c9b1bc175a | ||
|
|
f4d9d9795a | ||
|
|
08e6d670de | ||
|
|
a2848d52fc | ||
|
|
fa219a674e | ||
|
|
0d0faa3218 | ||
|
|
09ab930c56 | ||
|
|
9b7b411825 | ||
|
|
aacde8871e | ||
|
|
1aa1addcf7 | ||
|
|
a305980a78 | ||
|
|
0def055e8b | ||
|
|
29e9291030 | ||
|
|
e9015d95e4 | ||
|
|
9cedcc4395 | ||
|
|
744d8f4262 | ||
|
|
d9efc7aa19 | ||
|
|
53525cffe9 | ||
|
|
c1269ede63 | ||
|
|
6102a33397 | ||
|
|
192e8ef664 | ||
|
|
5778230ee4 | ||
|
|
9e959306b1 | ||
|
|
7a7f4eb458 | ||
|
|
050545411d | ||
|
|
28b394d5ea | ||
|
|
a3761dab35 | ||
|
|
79ae51a973 | ||
|
|
b724286886 | ||
|
|
9c52e789b3 | ||
|
|
717a3740c6 | ||
|
|
a8738b9ca9 | ||
|
|
ea5dba02b1 | ||
|
|
9fbcb59f32 | ||
|
|
9b312d4a71 | ||
|
|
480002e50d | ||
|
|
9cc775f548 | ||
|
|
514e50da74 | ||
|
|
9dea931db0 | ||
|
|
12c60715d2 | ||
|
|
23e0c45a52 | ||
|
|
c0c9c834e7 | ||
|
|
9aaa0e8d36 | ||
|
|
7bb4daabe2 | ||
|
|
b0203874af | ||
|
|
2ee757e0b2 | ||
|
|
b4db2885aa | ||
|
|
4381237eb3 | ||
|
|
d91d333baa | ||
|
|
5f6f979bce | ||
|
|
881b76fa3e | ||
|
|
e92dc29896 | ||
|
|
07e64a30e4 | ||
|
|
ac8765655f | ||
|
|
b5c8cfd9f8 | ||
|
|
71b11a3708 | ||
|
|
8040394150 | ||
|
|
a07856a877 | ||
|
|
6e990d3359 | ||
|
|
031cdbf187 | ||
|
|
7198bda02e | ||
|
|
18df20b675 | ||
|
|
581e6d2e5c | ||
|
|
91c92f9dac | ||
|
|
886e84a0c5 | ||
|
|
bfeeb955f0 | ||
|
|
beebffa5fc | ||
|
|
bf6085bdf9 | ||
|
|
db064e788b | ||
|
|
23be3be21e | ||
|
|
02ab809c34 | ||
|
|
4d538714e4 | ||
|
|
43e968864d | ||
|
|
03f01bb5a0 | ||
|
|
a7a6f60ebf | ||
|
|
4debd0a292 | ||
|
|
f0fa010df4 | ||
|
|
005112043e | ||
|
|
1d118af0f5 | ||
|
|
fa0bfc256a | ||
|
|
602905ef25 | ||
|
|
1f429e4773 | ||
|
|
6ce6699355 | ||
|
|
3d46ae8440 | ||
|
|
bbbea1db9c | ||
|
|
dc7f353e67 | ||
|
|
6ca6f14ad9 | ||
|
|
03fd2708cd | ||
|
|
6149497885 | ||
|
|
f810ef03a7 | ||
|
|
eeef444df7 | ||
|
|
03769e6cf6 | ||
|
|
b0bd759035 | ||
|
|
eb92b9ca78 | ||
|
|
411ab8db38 | ||
|
|
a679c17c64 | ||
|
|
87b7c46eb5 | ||
|
|
a8c0a05c28 | ||
|
|
6ac512e749 | ||
|
|
be55f3cf90 | ||
|
|
cbbbc7f211 | ||
|
|
623c4c3e39 | ||
|
|
b0ab1f0f2d | ||
|
|
733937ec74 | ||
|
|
14a8818563 | ||
|
|
695d00506b | ||
|
|
e1a90a79ec | ||
|
|
c6cedf7a02 | ||
|
|
9d870a3781 | ||
|
|
e0a33f3013 | ||
|
|
e3e95788b4 | ||
|
|
0e87595692 | ||
|
|
e5361949df | ||
|
|
62290e2812 | ||
|
|
489b82e8f4 | ||
|
|
4e8d6e058b | ||
|
|
535e3abe92 | ||
|
|
35fbd4443e | ||
|
|
edf667f462 | ||
|
|
a22be909a7 | ||
|
|
915f6d0b46 | ||
|
|
c23e7af24b | ||
|
|
91fdbaff4d | ||
|
|
c6f258f01d | ||
|
|
8811f92aea | ||
|
|
0726505c25 | ||
|
|
4df1d6cb0a | ||
|
|
773b5d4dab | ||
|
|
660fa66df4 | ||
|
|
c2992e3a35 | ||
|
|
d07649bcf8 | ||
|
|
5e1baf2104 | ||
|
|
e8701b65c6 | ||
|
|
517d6f6d0b | ||
|
|
b7c5c15280 | ||
|
|
0561a9bcf6 | ||
|
|
05a14a3610 | ||
|
|
b3737448d7 | ||
|
|
bfa91bf2fb | ||
|
|
b7948f7965 | ||
|
|
b0f310b2fe | ||
|
|
ac598fdfab | ||
|
|
60e6a04d0d | ||
|
|
c4884022af | ||
|
|
a70091bf99 | ||
|
|
ebaf75df95 | ||
|
|
2f044a17e1 | ||
|
|
782dafca33 | ||
|
|
7174bbc0bd | ||
|
|
f177045e89 | ||
|
|
e32d66e0aa | ||
|
|
0237aa31cb | ||
|
|
18921f8750 | ||
|
|
e848360c80 | ||
|
|
a075a7cba8 | ||
|
|
f98630cff1 | ||
|
|
325e756e4c | ||
|
|
556c623e98 | ||
|
|
de5d74a1f0 | ||
|
|
41decf85c9 | ||
|
|
51fa017b8b | ||
|
|
e78dd6f9dd | ||
|
|
0e8fcdd4d1 | ||
|
|
717fc422ff | ||
|
|
b07c4b7e6c | ||
|
|
ff2b4cf6e1 | ||
|
|
a131fa3dd7 | ||
|
|
b3a212c7fd | ||
|
|
0e608d169e | ||
|
|
c38fa2a70f | ||
|
|
4c54c4653e | ||
|
|
5ae5943cc8 | ||
|
|
857d367d15 | ||
|
|
8c766115db | ||
|
|
7bd36dd274 | ||
|
|
bd246300e0 | ||
|
|
31be546e8e | ||
|
|
d57983032e | ||
|
|
53b3190bbf | ||
|
|
d924237b7e | ||
|
|
da29fc2f25 | ||
|
|
3bbf195870 | ||
|
|
75dac72640 | ||
|
|
df9af9c35a | ||
|
|
cccc8e829e | ||
|
|
e0b1b18299 | ||
|
|
02d607817a | ||
|
|
8ae62e21a7 | ||
|
|
14a59262af | ||
|
|
12f46e2e4e | ||
|
|
dec2e6b69f | ||
|
|
e82327a979 | ||
|
|
81fb2ddcd1 | ||
|
|
a8922cd0b2 | ||
|
|
4f3ea48ec7 | ||
|
|
150265adf6 | ||
|
|
aff627c0cd | ||
|
|
2b803d4193 | ||
|
|
464fa7c0c9 | ||
|
|
e9564b1d33 | ||
|
|
c75929789d | ||
|
|
63b7f47f30 | ||
|
|
5650e6ccfe | ||
|
|
a0d3706497 | ||
|
|
0013ef42bd | ||
|
|
8da5f53856 | ||
|
|
d0c150d419 | ||
|
|
f52b828fe1 | ||
|
|
8babadc33e | ||
|
|
7047de4959 | ||
|
|
9b5e286b90 | ||
|
|
99d98b6227 | ||
|
|
03a343784d | ||
|
|
046350c0f9 | ||
|
|
a1f267916e | ||
|
|
a4b0c1fe36 | ||
|
|
6aa0878480 | ||
|
|
897a56fa4a | ||
|
|
86a1e405b9 | ||
|
|
e33fdaf74d | ||
|
|
a554982f08 | ||
|
|
553fa9245c | ||
|
|
f862013c6a | ||
|
|
7dd87c1ff6 | ||
|
|
900da46090 | ||
|
|
7d54b78521 | ||
|
|
ccc7d467c2 | ||
|
|
8b6c358266 | ||
|
|
444388e432 | ||
|
|
b4c00d866c | ||
|
|
b9606fcb49 | ||
|
|
ab6dfa0f08 | ||
|
|
81ee3c47f5 | ||
|
|
7a59f821cf | ||
|
|
6f7276bcdf | ||
|
|
2cd884bd9d | ||
|
|
40f4554373 | ||
|
|
76321d3491 | ||
|
|
c3b5db18a7 | ||
|
|
71afc90b0e | ||
|
|
c03d08b9f4 | ||
|
|
b9d8931f3e | ||
|
|
ec8bd2fc14 | ||
|
|
dd1bbf1226 | ||
|
|
c2c86ff58e | ||
|
|
2d2840c1bb | ||
|
|
571969fca9 | ||
|
|
16fa52790c | ||
|
|
c88c252587 | ||
|
|
253cb03de8 | ||
|
|
0b74adc13b | ||
|
|
6fc2328475 | ||
|
|
e9c4201acc | ||
|
|
9edb52c909 | ||
|
|
f4c7320bb5 | ||
|
|
17581ca204 | ||
|
|
11edb8ce5d | ||
|
|
40caa7b1eb | ||
|
|
e3f4c24983 | ||
|
|
dd0482cb26 | ||
|
|
4052007de8 | ||
|
|
85b03d9d8a | ||
|
|
36c3fd6f5d | ||
|
|
d397a5d35e | ||
|
|
43d83ad919 | ||
|
|
e71dd1d576 | ||
|
|
72e28b8e04 | ||
|
|
3432a15c62 | ||
|
|
45a5bae8b2 | ||
|
|
a566393a89 | ||
|
|
5f2f3f9122 | ||
|
|
c7fccf7e70 | ||
|
|
3d92d061f1 | ||
|
|
f2759675a1 | ||
|
|
2e6ff78662 | ||
|
|
8ca0a32932 | ||
|
|
72fbcd4e0e | ||
|
|
a5719a9c1d | ||
|
|
bad41d5027 | ||
|
|
3bfb95f190 | ||
|
|
f04695feb6 | ||
|
|
94da911f93 | ||
|
|
f4771d4ddb | ||
|
|
9f7c4adb0f | ||
|
|
6fb20fc6c1 | ||
|
|
ce8e91aab8 | ||
|
|
fe4ab6fdab | ||
|
|
e60b6922c3 | ||
|
|
d3d2c907ad | ||
|
|
84306e0b28 | ||
|
|
f7c2148d5f | ||
|
|
46e5a0e121 | ||
|
|
2f5b52f12c | ||
|
|
20f9120583 | ||
|
|
e65428cae1 | ||
|
|
e5862e807c | ||
|
|
911fb3cae0 | ||
|
|
d76821f6a6 | ||
|
|
1d5a680a5b | ||
|
|
2f38e17fd6 | ||
|
|
7de4bea5f7 | ||
|
|
35db8baa2a | ||
|
|
e774dd3a6c | ||
|
|
f3619c6b43 | ||
|
|
b807081f9d | ||
|
|
0807d7f1ce | ||
|
|
8b6894716b | ||
|
|
cb733d8e38 | ||
|
|
d9c7daebea | ||
|
|
bbe8fd6b8f | ||
|
|
8f8fe513fa | ||
|
|
8a4df1f2ba | ||
|
|
dacbf5edb3 | ||
|
|
4e145f46db | ||
|
|
53dc622376 | ||
|
|
aab8fabb1f | ||
|
|
86bb240633 | ||
|
|
c6b3aec1af | ||
|
|
e6c4b32a47 | ||
|
|
b71e83047d | ||
|
|
8609336240 | ||
|
|
ded11a81a5 | ||
|
|
0c00ea9292 | ||
|
|
672b53d8a9 | ||
|
|
bc5032b2d0 | ||
|
|
4cefa439ec | ||
|
|
4d2b7b3ad0 | ||
|
|
58f85cc436 | ||
|
|
5c50d7622b | ||
|
|
e90b31bf69 | ||
|
|
b43d60138b | ||
|
|
9f4faebfe6 | ||
|
|
b6f84d8de6 | ||
|
|
230eec7c59 | ||
|
|
3b9cfefd5f | ||
|
|
c1b701f1f9 | ||
|
|
4744caaacc | ||
|
|
b58912462a | ||
|
|
f3cadf594d | ||
|
|
a8834ee21c | ||
|
|
0c59403e49 | ||
|
|
431a771672 | ||
|
|
b043b06208 | ||
|
|
abccf713df | ||
|
|
4b83f6f28f | ||
|
|
e843a547a5 | ||
|
|
40d9bfa30b | ||
|
|
69669f0a50 | ||
|
|
4c20d27d33 | ||
|
|
06eb277b34 | ||
|
|
86120277c7 | ||
|
|
62927cb6a8 | ||
|
|
22f31f2ebc | ||
|
|
c8cfaeecf6 | ||
|
|
714d334067 | ||
|
|
ca324995b3 | ||
|
|
f301eab5b1 | ||
|
|
87ac60a3ab | ||
|
|
a2f89f07fa | ||
|
|
57d7d2b798 | ||
|
|
6f3bc25b64 | ||
|
|
62477dac87 | ||
|
|
0dfee513f4 | ||
|
|
31f97cb840 | ||
|
|
93e2d36dbb | ||
|
|
aed3d08167 | ||
|
|
9c5f11dd9d | ||
|
|
1af7aa8a16 | ||
|
|
cf6f793478 | ||
|
|
145948f506 | ||
|
|
b068492d10 | ||
|
|
8c94d39ae4 | ||
|
|
c584d72f41 | ||
|
|
cfd248f818 | ||
|
|
b9e59ee2f8 | ||
|
|
439899fbdc | ||
|
|
177c1889fa | ||
|
|
ebce847724 | ||
|
|
c6b6d9a310 | ||
|
|
6601a45472 | ||
|
|
d66fb9a783 | ||
|
|
c244369875 | ||
|
|
f9c812864e | ||
|
|
87d0cdd9e5 | ||
|
|
d237a18aae | ||
|
|
0159cf30ae | ||
|
|
6aba1670ec | ||
|
|
447a31ea00 | ||
|
|
9e410a61ef | ||
|
|
4588b7eaa5 | ||
|
|
aef55c9207 | ||
|
|
9a587e9dd2 | ||
|
|
cbe834c71d | ||
|
|
020495cebf | ||
|
|
54d3cccb67 | ||
|
|
b4adf57f39 | ||
|
|
b796dbcde3 | ||
|
|
ba2c69690f | ||
|
|
c3b60f1eac | ||
|
|
2bb387eeaf | ||
|
|
fd67c733aa | ||
|
|
eac7048360 | ||
|
|
34de943c9f | ||
|
|
302b43c9e8 | ||
|
|
07b3917c4e | ||
|
|
1ace248121 | ||
|
|
7a96feb049 | ||
|
|
5df1d099a1 | ||
|
|
ed2db49cdb | ||
|
|
7b2c99357e | ||
|
|
4868c40795 | ||
|
|
27ff17db50 | ||
|
|
c15a52eb2a | ||
|
|
faa54dedb1 | ||
|
|
59bdb4b2f0 | ||
|
|
4990d757ae | ||
|
|
5f71c3dcb4 | ||
|
|
1892a750b7 | ||
|
|
020f853bb1 | ||
|
|
9ccddae60a | ||
|
|
b7a2d93f03 | ||
|
|
2fc8b421b0 | ||
|
|
180466674f | ||
|
|
ac23a4312d | ||
|
|
aaea83b932 | ||
|
|
8425ea0b2f | ||
|
|
100a6569b7 | ||
|
|
e8c4b9ab78 | ||
|
|
eff5467ccf | ||
|
|
769308c9f0 | ||
|
|
e1de57c98c | ||
|
|
9c6a5af87e | ||
|
|
9d88577108 | ||
|
|
dbebc5956f | ||
|
|
52ca5a7eb8 | ||
|
|
78b898dc5c | ||
|
|
1e81157d5c | ||
|
|
5cff3db9b5 | ||
|
|
25fe80f252 | ||
|
|
413bb2cd82 | ||
|
|
b867bbc7a6 | ||
|
|
3bf6f763aa | ||
|
|
2c0d621b16 | ||
|
|
e930ebf42f | ||
|
|
4e3862b1c5 | ||
|
|
e3ffb9a473 | ||
|
|
e485d79bb1 | ||
|
|
49edbb8185 | ||
|
|
32f7407752 | ||
|
|
aef02d34a5 | ||
|
|
bb2f9bb309 | ||
|
|
140b6528a6 | ||
|
|
fa3cf650ea | ||
|
|
cfe2cff74c | ||
|
|
1def52e41a | ||
|
|
cc4eab3244 | ||
|
|
4327e48c1f | ||
|
|
c8c077c14b | ||
|
|
f82221552b | ||
|
|
bfe0aad951 | ||
|
|
2f5d8edb03 | ||
|
|
ac0acafff9 | ||
|
|
9333150c3b | ||
|
|
0c71c6856b | ||
|
|
6a47a8a1e1 | ||
|
|
04ee1a6362 | ||
|
|
9ead29643d | ||
|
|
d8e7bd4ccc |
No files matched your search
@@ -2,3 +2,4 @@
|
||||
ij_kotlin_imports_layout = *
|
||||
ktlint_code_style = intellij_idea
|
||||
ktlint_function_naming_ignore_when_annotated_with = Composable
|
||||
ktlint_standard_class-signature = disabled
|
||||
@@ -0,0 +1,65 @@
|
||||
name: Baseline profile generation
|
||||
|
||||
on:
|
||||
schedule:
|
||||
- cron: '30 0 * * *'
|
||||
workflow_dispatch:
|
||||
push:
|
||||
branches:
|
||||
- '**'
|
||||
paths:
|
||||
- 'benchmark/**'
|
||||
- '.github/workflows/baseline-profile.yml'
|
||||
|
||||
jobs:
|
||||
baseline-profile:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Free disk space
|
||||
run: rm -rf $AGENT_TOOLSDIRECTORY
|
||||
|
||||
- name: Enable KVM group perms
|
||||
run: |
|
||||
echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules
|
||||
sudo udevadm control --reload-rules
|
||||
sudo udevadm trigger --name-match=kvm
|
||||
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v5
|
||||
|
||||
- name: Setup Java
|
||||
uses: actions/setup-java@v5
|
||||
with:
|
||||
distribution: temurin
|
||||
java-version: 21
|
||||
|
||||
- name: Install Rust Toolchain
|
||||
run: rustup target add armv7-linux-androideabi
|
||||
|
||||
- name: Rust Cache
|
||||
uses: Swatinem/rust-cache@v2
|
||||
with:
|
||||
workspaces: "./app/src/main/rust"
|
||||
|
||||
- name: CMake Cache
|
||||
uses: actions/cache/restore@v4
|
||||
with:
|
||||
path: app/.cxx
|
||||
key: cmake-
|
||||
|
||||
- name: Setup Gradle
|
||||
uses: gradle/actions/setup-gradle@v5
|
||||
with:
|
||||
cache-encryption-key: ${{ secrets.GradleEncryptionKey }}
|
||||
cache-read-only: true
|
||||
|
||||
- name: Generate Baseline Profile
|
||||
run: ./gradlew generateBaselineProfile
|
||||
-Pandroid.testoptions.manageddevices.emulator.gpu="swiftshader_indirect"
|
||||
|
||||
- name: Upload reports
|
||||
if: failure()
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: reports
|
||||
path: benchmark/build/reports
|
||||
+27
-14
@@ -12,16 +12,16 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@v5
|
||||
|
||||
- name: Setup Java
|
||||
uses: actions/setup-java@v4
|
||||
uses: actions/setup-java@v5
|
||||
with:
|
||||
distribution: temurin
|
||||
java-version: 21
|
||||
|
||||
- name: Install Rust Toolchain
|
||||
run: rustup update && rustup target add armv7-linux-androideabi aarch64-linux-android x86_64-linux-android && rustup component add rustfmt clippy
|
||||
run: rustup target add armv7-linux-androideabi
|
||||
|
||||
- name: Rust Cache
|
||||
uses: Swatinem/rust-cache@v2
|
||||
@@ -38,10 +38,13 @@ jobs:
|
||||
run: cargo clippy --target aarch64-linux-android --all-features -- -D warnings
|
||||
|
||||
- name: Setup Gradle
|
||||
uses: gradle/actions/setup-gradle@v4
|
||||
uses: gradle/actions/setup-gradle@v5
|
||||
with:
|
||||
cache-read-only: true
|
||||
|
||||
- name: Check build-logic
|
||||
run: ./gradlew :build-logic:convention:check
|
||||
|
||||
- name: Spotless Check
|
||||
run: ./gradlew spotlessCheck
|
||||
|
||||
@@ -52,16 +55,16 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@v5
|
||||
|
||||
- name: Setup Java
|
||||
uses: actions/setup-java@v4
|
||||
uses: actions/setup-java@v5
|
||||
with:
|
||||
distribution: temurin
|
||||
java-version: 21
|
||||
|
||||
- name: Install Rust Toolchain
|
||||
run: rustup update && rustup target add armv7-linux-androideabi aarch64-linux-android x86_64-linux-android && rustup component add rustfmt clippy
|
||||
run: rustup target add armv7-linux-androideabi
|
||||
|
||||
- name: Rust Cache
|
||||
uses: Swatinem/rust-cache@v2
|
||||
@@ -71,7 +74,7 @@ jobs:
|
||||
save-if: "false"
|
||||
|
||||
- name: Setup Gradle
|
||||
uses: gradle/actions/setup-gradle@v4
|
||||
uses: gradle/actions/setup-gradle@v5
|
||||
with:
|
||||
cache-encryption-key: ${{ secrets.GradleEncryptionKey }}
|
||||
|
||||
@@ -79,9 +82,13 @@ jobs:
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
path: app/.cxx
|
||||
key: cmake-default-${{ hashFiles('gradle/libs.versions.toml', 'app/build.gradle.kts', 'app/src/main/cpp/CMakeLists.txt') }}
|
||||
key: cmake-default-${{ hashFiles('rust-toolchain.toml', 'settings.gradle.kts', 'app/src/main/cpp/CMakeLists.txt', 'app/src/main/rust/Cargo.lock') }}
|
||||
restore-keys: cmake-default-
|
||||
|
||||
# https://github.com/mikepenz/AboutLibraries/issues/1225
|
||||
- name: Generate aboutlibraries.json
|
||||
run: ./gradlew :app:prepareLibraryDefinitionsDefaultRelease --rerun
|
||||
|
||||
- name: Build
|
||||
run: ./gradlew assembleDefaultRelease
|
||||
|
||||
@@ -104,6 +111,7 @@ jobs:
|
||||
path: app/build/outputs/apk/default/release/app-default-armeabi-v7a-release.apk
|
||||
|
||||
- name: Upload mapping
|
||||
if: always()
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: default-mapping-${{ github.sha }}
|
||||
@@ -119,16 +127,16 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@v5
|
||||
|
||||
- name: Setup Java
|
||||
uses: actions/setup-java@v4
|
||||
uses: actions/setup-java@v5
|
||||
with:
|
||||
distribution: temurin
|
||||
java-version: 21
|
||||
|
||||
- name: Install Rust Toolchain
|
||||
run: rustup update && rustup target add armv7-linux-androideabi aarch64-linux-android x86_64-linux-android && rustup component add rustfmt clippy
|
||||
run: rustup target add armv7-linux-androideabi
|
||||
|
||||
- name: Rust Cache
|
||||
uses: Swatinem/rust-cache@v2
|
||||
@@ -138,7 +146,7 @@ jobs:
|
||||
save-if: "false"
|
||||
|
||||
- name: Setup Gradle
|
||||
uses: gradle/actions/setup-gradle@v4
|
||||
uses: gradle/actions/setup-gradle@v5
|
||||
with:
|
||||
cache-encryption-key: ${{ secrets.GradleEncryptionKey }}
|
||||
|
||||
@@ -146,9 +154,13 @@ jobs:
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
path: app/.cxx
|
||||
key: cmake-marshmallow-${{ hashFiles('gradle/libs.versions.toml', 'app/build.gradle.kts', 'app/src/main/cpp/CMakeLists.txt') }}
|
||||
key: cmake-marshmallow-${{ hashFiles('rust-toolchain.toml', 'settings.gradle.kts', 'app/src/main/cpp/CMakeLists.txt', 'app/src/main/rust/Cargo.lock') }}
|
||||
restore-keys: cmake-marshmallow-
|
||||
|
||||
# https://github.com/mikepenz/AboutLibraries/issues/1225
|
||||
- name: Generate aboutlibraries.json
|
||||
run: ./gradlew :app:prepareLibraryDefinitionsMarshmallowRelease --rerun
|
||||
|
||||
- name: Build
|
||||
run: ./gradlew assembleMarshmallowRelease
|
||||
|
||||
@@ -171,6 +183,7 @@ jobs:
|
||||
path: app/build/outputs/apk/marshmallow/release/app-marshmallow-armeabi-v7a-release.apk
|
||||
|
||||
- name: Upload mapping
|
||||
if: always()
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: marshmallow-mapping-${{ github.sha }}
|
||||
|
||||
@@ -9,19 +9,8 @@ jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup Java
|
||||
uses: actions/setup-java@v4
|
||||
with:
|
||||
distribution: temurin
|
||||
java-version: 21
|
||||
|
||||
- name: Free disk space
|
||||
run: |
|
||||
shopt -s extglob
|
||||
rm -rf ${ANDROID_SDK_ROOT}/ndk/!(27*)
|
||||
run: rm -rf $AGENT_TOOLSDIRECTORY
|
||||
|
||||
- name: Enable KVM group perms
|
||||
run: |
|
||||
@@ -29,8 +18,17 @@ jobs:
|
||||
sudo udevadm control --reload-rules
|
||||
sudo udevadm trigger --name-match=kvm
|
||||
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v5
|
||||
|
||||
- name: Setup Java
|
||||
uses: actions/setup-java@v5
|
||||
with:
|
||||
distribution: temurin
|
||||
java-version: 21
|
||||
|
||||
- name: Install Rust Toolchain
|
||||
run: rustup update && rustup target add armv7-linux-androideabi aarch64-linux-android x86_64-linux-android
|
||||
run: rustup target add armv7-linux-androideabi
|
||||
|
||||
- name: Rust Cache
|
||||
uses: Swatinem/rust-cache@v2
|
||||
@@ -38,21 +36,27 @@ jobs:
|
||||
workspaces: "./app/src/main/rust"
|
||||
|
||||
- name: CMake Cache
|
||||
uses: actions/cache@v4
|
||||
uses: actions/cache/restore@v4
|
||||
with:
|
||||
path: app/.cxx
|
||||
key: cmake-${{ hashFiles('gradle/libs.versions.toml', 'app/build.gradle.kts', 'app/src/main/cpp/CMakeLists.txt') }}
|
||||
restore-keys: cmake-
|
||||
key: cmake-
|
||||
|
||||
- name: Setup Gradle
|
||||
uses: gradle/actions/setup-gradle@v4
|
||||
uses: gradle/actions/setup-gradle@v5
|
||||
with:
|
||||
cache-encryption-key: ${{ secrets.GradleEncryptionKey }}
|
||||
cache-read-only: true
|
||||
|
||||
- name: Generate Baseline Profile
|
||||
run: ./gradlew generateBaselineProfile
|
||||
-Pandroid.testoptions.manageddevices.emulator.gpu="swiftshader_indirect"
|
||||
|
||||
# https://github.com/mikepenz/AboutLibraries/issues/1225
|
||||
- name: Generate aboutlibraries.json
|
||||
run: ./gradlew
|
||||
:app:prepareLibraryDefinitionsDefaultRelease --rerun
|
||||
:app:prepareLibraryDefinitionsMarshmallowRelease --rerun
|
||||
|
||||
- name: Build
|
||||
run: ./gradlew assembleRelease
|
||||
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
name: 'Update Chrome Version'
|
||||
|
||||
on:
|
||||
schedule:
|
||||
- cron: '0 0 1 * *'
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
action:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Update Chrome Version
|
||||
env:
|
||||
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
run: |
|
||||
cd chrome-for-testing
|
||||
curl -OLf https://googlechromelabs.github.io/chrome-for-testing/LATEST_RELEASE_STABLE
|
||||
git config user.name "github-actions[bot]"
|
||||
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
|
||||
git config push.autoSetupRemote true
|
||||
git switch -c chore/chrome
|
||||
if git commit -am "Update Chrome version for default User-Agent"; then
|
||||
git push
|
||||
gh pr create -f
|
||||
gh pr merge chore/chrome --auto -dr
|
||||
fi
|
||||
+4
-2
@@ -1,8 +1,10 @@
|
||||
.gradle
|
||||
/local.properties
|
||||
.DS_Store
|
||||
/build
|
||||
/.kotlin
|
||||
build
|
||||
.cxx
|
||||
.kotlin
|
||||
*.iml
|
||||
.idea
|
||||
.vscode
|
||||
/captures
|
||||
@@ -1,5 +1,155 @@
|
||||
# 变更日志
|
||||
|
||||
## [1.14.4] - 2025-10-12
|
||||
|
||||
### 改进
|
||||
|
||||
* [阅读器] 支持禁用使用音量键翻页 [#2593](https://github.com/FooIbar/EhViewer/issues/2593)
|
||||
* 视搜索警告为错误 [#2604](https://github.com/FooIbar/EhViewer/issues/2604)
|
||||
* 余额不足时停止下载并显示通知 [#2479](https://github.com/FooIbar/EhViewer/issues/2479)
|
||||
* [阅读器] 使用默认阅读模式时对带有 webtoon 标签的画廊自动使用条漫模式 [#2632](https://github.com/FooIbar/EhViewer/issues/2632)
|
||||
* 优化按页数排序下载项目的速度
|
||||
* 开启显示标签投票状态时为 power >= 100 的标签添加下划线
|
||||
* 杂项优化
|
||||
|
||||
### Bug 修复
|
||||
|
||||
* Android 16 QPR1 上画廊详情中的标签文本对比度不足
|
||||
* 初始化 DownloadManager 用时过长导致 ANR [#2588](https://github.com/FooIbar/EhViewer/issues/2588)
|
||||
* 某些设备上无法下载名称较长的画廊 [#2607](https://github.com/FooIbar/EhViewer/issues/2607)
|
||||
* 评论区链接实际可点击区域比画面显示上的略长 [#2615](https://github.com/FooIbar/EhViewer/issues/2615)
|
||||
* 主页快速搜索再次打开出现以前的画廊重复出现在最上层 [#2628](https://github.com/FooIbar/EhViewer/issues/2628)
|
||||
* 非条漫模式下原始大小较小的动图不播放 [#2652](https://github.com/FooIbar/EhViewer/issues/2652)
|
||||
* Android 9 以下打开评论区切屏后回来会闪退 [#2655](https://github.com/FooIbar/EhViewer/issues/2655)
|
||||
* 杂项修复
|
||||
|
||||
|
||||
## [1.14.3] - 2025-09-01
|
||||
|
||||
### 新功能
|
||||
|
||||
* 支持以 label: 语法搜索下载标签
|
||||
|
||||
### Bug 修复
|
||||
|
||||
* [阅读器] 关闭滤镜设置时崩溃 [#2573](https://github.com/FooIbar/EhViewer/issues/2573)
|
||||
* 编辑评论时上下文菜单缺少格式化选项
|
||||
* 某些情况下搜索相似画廊时出错
|
||||
|
||||
|
||||
## [1.14.2] - 2025-08-16
|
||||
|
||||
### 改进
|
||||
|
||||
* [下载] 支持将最低下载速度设为 0 [#2545](https://github.com/FooIbar/EhViewer/issues/2545)
|
||||
* [下载] 移除令人困惑的连接超时设置 [#2556](https://github.com/FooIbar/EhViewer/issues/2556)
|
||||
|
||||
### Bug 修复
|
||||
|
||||
* 下载速度计算不准确导致下载失败误报 [#2545](https://github.com/FooIbar/EhViewer/issues/2545)
|
||||
* 无法解析带有 location: 标签的画廊
|
||||
|
||||
|
||||
## [1.14.1] - 2025-07-31
|
||||
|
||||
### 改进
|
||||
|
||||
* 更新 GitHub 访问令牌
|
||||
|
||||
|
||||
## [1.14.0] - 2025-07-23
|
||||
|
||||
### 重大变更
|
||||
|
||||
* 默认隐藏分数为 -100 的评论
|
||||
* 迁移部分组件到 Material 3 Expressive
|
||||
|
||||
### 新功能
|
||||
|
||||
* [EH] 支持显示标签投票状态
|
||||
* 支持添加画廊标签时自动补全
|
||||
* [下载] 支持指定连接超时和最低响应速度 [#1915](https://github.com/FooIbar/EhViewer/issues/1915)
|
||||
* [阅读器] 支持反转物理按键控制 [#1962](https://github.com/FooIbar/EhViewer/issues/1962)
|
||||
* [高级] 支持禁用 QUIC 支持
|
||||
* [高级] 重新添加桌面版网站选项以绕过 Cloudflare [#2230](https://github.com/FooIbar/EhViewer/issues/2230)
|
||||
* 支持 Android 6.0 上的动画 WebP
|
||||
|
||||
### 改进
|
||||
|
||||
* 优化压缩包加载性能
|
||||
* 优化预加载策略
|
||||
* 更新日本语翻译
|
||||
* 为已选中的项目使用不同的颜色 [#2121](https://github.com/FooIbar/EhViewer/issues/2121)
|
||||
* 缩略图模式下显示已收藏图标 [#2128](https://github.com/FooIbar/EhViewer/issues/2128)
|
||||
* 迁移大部分解析器到 Rust 实现
|
||||
* 优化动画 WebP 解码性能 [#2415](https://github.com/FooIbar/EhViewer/issues/2415)
|
||||
* 优化标签自动补全命中率 [#2473](https://github.com/FooIbar/EhViewer/issues/2473)
|
||||
* 在状态栏后绘制半透明背景以提高对比度
|
||||
* 杂项优化
|
||||
|
||||
### Bug 修复
|
||||
|
||||
* 退出阅读器时崩溃
|
||||
* Android 8.0 以下的设备无法读取某些压缩包
|
||||
* 列表模式下点击画廊封面无法进入详情页
|
||||
* 点击某些评论时崩溃
|
||||
* 开启裁剪边缘时某些图片被不正确裁剪
|
||||
* 下载数较多时无网络时启动 app 时崩溃 [#2041](https://github.com/FooIbar/EhViewer/issues/2041)
|
||||
* 标签翻译数据无法更新 [#2075](https://github.com/FooIbar/EhViewer/issues/2075)
|
||||
* 画廊详情页面显示的收藏夹名称错误 [#2081](https://github.com/FooIbar/EhViewer/issues/2081)
|
||||
* 不使用 CI 频道时无法更新
|
||||
* 添加后移动快捷搜索闪退 [#2124](https://github.com/FooIbar/EhViewer/issues/2124)
|
||||
* 某些数据无法导入
|
||||
* App 位于后台时清除下载通知时崩溃
|
||||
* 杂项修复
|
||||
|
||||
|
||||
## [1.13.1] - 2024-11-11
|
||||
|
||||
### 重大变更
|
||||
|
||||
* 由于网站限制,移除搜索封面功能
|
||||
* 移除桌面版网站选项,默认使用 1280x 分辨率
|
||||
|
||||
### 改进
|
||||
|
||||
* 更新日本语翻译
|
||||
* 优化压缩包读取
|
||||
* 缓解某些小米系统上应用闪退 [#1826](https://github.com/FooIbar/EhViewer/issues/1826)
|
||||
* 杂项优化
|
||||
|
||||
### Bug 修复
|
||||
|
||||
* Android 9 上的下载失败误报 [#1914](https://github.com/FooIbar/EhViewer/issues/1914)
|
||||
|
||||
|
||||
## [1.13.0] - 2024-11-01
|
||||
|
||||
### 重大变更
|
||||
|
||||
* 不再捆绑 Cronet 库,在不支持 HttpEngine 的设备上回退到 OkHttp
|
||||
* 适配 E 站缩略图和 WebP 等相关变更
|
||||
|
||||
### 改进
|
||||
|
||||
* 使用磁力链接代替种子下载
|
||||
* 在搜索栏右侧显示论坛头像
|
||||
* 支持重置下载路径
|
||||
* 为 Android 10 以下且没有 DocumentsUI 的设备创建默认下载目录 [#1735](https://github.com/FooIbar/EhViewer/issues/1735)
|
||||
* 重新添加了强制使用 e-hentai 缩略图服务器的选项
|
||||
* 阅读器支持 PageUp/PageDown 和方向键上/下翻页 [#1801](https://github.com/FooIbar/EhViewer/issues/1801)
|
||||
* 未找到搜索结果时显示可能存在的警告 [#1787](https://github.com/FooIbar/EhViewer/issues/1787)
|
||||
* 更新了日本语翻译
|
||||
* 杂项优化
|
||||
|
||||
### Bug 修复
|
||||
|
||||
* 阅读器屏幕方向与手机屏幕方向不同时闪退 [#1779](https://github.com/FooIbar/EhViewer/issues/1779)
|
||||
* 下载文件哈希不匹配导致下载失败 [#1811](https://github.com/FooIbar/EhViewer/issues/1811)
|
||||
* 无法同时下载和查看 [#1822](https://github.com/FooIbar/EhViewer/issues/1822)
|
||||
* 杂项修复
|
||||
|
||||
|
||||
## [1.12.1] - 2024-09-28
|
||||
|
||||
### 新功能
|
||||
|
||||
@@ -8,6 +8,10 @@
|
||||
<a href="/docs/README/zh-tw.md">
|
||||
正體中文
|
||||
</a>
|
||||
<span> | </span>
|
||||
<a href="/docs/README/ja.md">
|
||||
日本語
|
||||
</a>
|
||||
</p>
|
||||
|
||||
<h1 align="center">
|
||||
@@ -59,17 +63,19 @@
|
||||
|
||||
# Description
|
||||
|
||||
EhViewer fork dedicated to lightweight and high-performance
|
||||
A modern EhViewer fork dedicated to high-performance
|
||||
|
||||
with [Material Design 3](https://m3.material.io/)
|
||||
and [Dynamic Color](https://m3.material.io/styles/color/dynamic-color/overview) Support
|
||||
|
||||
# Download
|
||||
|
||||
| Flavor | Feature |
|
||||
|-------------|----------------------------------|
|
||||
| Default | Android 8.0+, full support |
|
||||
| Marshmallow | Android 6.0-7.1, limited support |
|
||||
| Flavor | Minimum Android Version | Notes |
|
||||
|-------------|-------------------------|--------------------------------|
|
||||
| Default | 8.0 | Full support |
|
||||
| Marshmallow | 6.0* | Limited support, no guarantees |
|
||||
|
||||
*Devices running Android 6 need to have the [ISRG Root X1](https://letsencrypt.org/certs/isrgrootx1.pem) certificate installed
|
||||
|
||||
<a href="https://github.com/FooIbar/EhViewer/releases">
|
||||
<img alt="Get it on GitHub" src="https://github.com/Ehviewer-Overhauled/Art/blob/master/get-it-on-github.svg" width="200px"/>
|
||||
@@ -92,7 +98,6 @@ Here is the libraries
|
||||
- [Coil](https://coil-kt.github.io/coil/)
|
||||
- [Compose Destinations](https://composedestinations.rafaelcosta.xyz/)
|
||||
- [libarchive](https://www.libarchive.org/)
|
||||
- [jsoup](https://jsoup.org/)
|
||||
|
||||
# License
|
||||
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
/build
|
||||
/.cxx
|
||||
+72
-114
@@ -1,48 +1,22 @@
|
||||
import com.mikepenz.aboutlibraries.plugin.DuplicateMode.MERGE
|
||||
import com.mikepenz.aboutlibraries.plugin.DuplicateRule.GROUP
|
||||
import org.jetbrains.kotlin.compose.compiler.gradle.ComposeFeatureFlag
|
||||
import com.mikepenz.aboutlibraries.plugin.DuplicateMode
|
||||
import com.mikepenz.aboutlibraries.plugin.DuplicateRule
|
||||
import java.util.regex.Pattern
|
||||
|
||||
val isRelease: Boolean
|
||||
get() = gradle.startParameter.taskNames.any { it.contains("Release") }
|
||||
|
||||
plugins {
|
||||
alias(libs.plugins.android.application)
|
||||
alias(libs.plugins.kotlin.android)
|
||||
alias(libs.plugins.kotlin.compose)
|
||||
alias(libs.plugins.kotlin.parcelize)
|
||||
alias(libs.plugins.ehviewer.android.application)
|
||||
alias(libs.plugins.kotlin.serialization)
|
||||
alias(libs.plugins.ksp)
|
||||
alias(libs.plugins.spotless)
|
||||
alias(libs.plugins.aboutlibrariesPlugin)
|
||||
alias(libs.plugins.composeCompilerReportGenerator)
|
||||
alias(libs.plugins.aboutlibraries)
|
||||
alias(libs.plugins.aboutlibrariesAndroid)
|
||||
alias(libs.plugins.baselineprofile)
|
||||
}
|
||||
|
||||
val supportedAbis = arrayOf("arm64-v8a", "x86_64", "armeabi-v7a")
|
||||
|
||||
android {
|
||||
compileSdk = 35
|
||||
buildToolsVersion = "35.0.0"
|
||||
ndkVersion = "27.1.12297006"
|
||||
|
||||
androidResources {
|
||||
generateLocaleConfig = true
|
||||
localeFilters += listOf(
|
||||
"zh",
|
||||
"zh-rCN",
|
||||
"zh-rHK",
|
||||
"zh-rTW",
|
||||
"es",
|
||||
"ja",
|
||||
"ko",
|
||||
"fr",
|
||||
"de",
|
||||
"th",
|
||||
"tr",
|
||||
"nb-rNO",
|
||||
)
|
||||
}
|
||||
|
||||
splits {
|
||||
abi {
|
||||
isEnable = true
|
||||
@@ -78,15 +52,12 @@ android {
|
||||
}.standardOutput.asText.get().trim().removePrefix("https://github.com/").removePrefix("git@github.com:")
|
||||
.removeSuffix(".git")
|
||||
|
||||
val chromeVersion = rootProject.layout.projectDirectory.file("chrome-for-testing/LATEST_RELEASE_STABLE").asFile
|
||||
.readText().substringBefore('.')
|
||||
val chromeVersion = rootProject.layout.projectDirectory.file("chrome.version").asFile.readText().trim()
|
||||
|
||||
defaultConfig {
|
||||
applicationId = "moe.tarsin.ehviewer"
|
||||
minSdk = 26
|
||||
targetSdk = 35
|
||||
versionCode = 180060
|
||||
versionName = "1.12.1"
|
||||
versionCode = 180063
|
||||
versionName = "1.14.4"
|
||||
// versionNameSuffix = "-SNAPSHOT"
|
||||
buildConfigField("String", "RAW_VERSION_NAME", "\"$versionName${versionNameSuffix.orEmpty()}\"")
|
||||
buildConfigField("String", "COMMIT_SHA", "\"$commitSha\"")
|
||||
@@ -99,19 +70,15 @@ android {
|
||||
}
|
||||
debugSymbolLevel = "FULL"
|
||||
}
|
||||
externalNativeBuild {
|
||||
cmake {
|
||||
arguments += "-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
flavorDimensions += "api"
|
||||
|
||||
productFlavors {
|
||||
create("default")
|
||||
create("default") {
|
||||
minSdk = 26
|
||||
}
|
||||
create("marshmallow") {
|
||||
minSdk = 23
|
||||
applicationIdSuffix = ".m"
|
||||
versionNameSuffix = "-M"
|
||||
}
|
||||
@@ -127,16 +94,46 @@ android {
|
||||
isCoreLibraryDesugaringEnabled = true
|
||||
}
|
||||
|
||||
lint {
|
||||
checkReleaseBuilds = false
|
||||
disable += setOf("MissingTranslation", "MissingQuantity")
|
||||
error += setOf("InlinedApi")
|
||||
}
|
||||
|
||||
packaging {
|
||||
dex {
|
||||
useLegacyPackaging = false
|
||||
}
|
||||
jniLibs {
|
||||
excludes += "**/libdatastore_shared_counter.so" // DataStore multi-process
|
||||
}
|
||||
resources {
|
||||
// Required by Layout Inspector
|
||||
pickFirsts += "/META-INF/androidx.compose.ui_ui.version"
|
||||
|
||||
excludes += listOf(
|
||||
"/META-INF/**",
|
||||
"/kotlin/**",
|
||||
"**.txt",
|
||||
"**.bin",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
androidResources {
|
||||
ignoreAssetsPatterns += listOf(
|
||||
"!PublicSuffixDatabase.list", // OkHttp
|
||||
"!composepreference.preference.generated.resources",
|
||||
)
|
||||
generateLocaleConfig = true
|
||||
localeFilters += listOf(
|
||||
"zh",
|
||||
"zh-rCN",
|
||||
"zh-rHK",
|
||||
"zh-rTW",
|
||||
"es",
|
||||
"ja",
|
||||
"ko",
|
||||
"fr",
|
||||
"de",
|
||||
"th",
|
||||
"tr",
|
||||
"nb-rNO",
|
||||
)
|
||||
}
|
||||
|
||||
dependenciesInfo.includeInApk = false
|
||||
@@ -168,26 +165,15 @@ android {
|
||||
namespace = "com.hippo.ehviewer"
|
||||
}
|
||||
|
||||
composeCompiler {
|
||||
featureFlags = setOf(ComposeFeatureFlag.OptimizeNonSkippingGroups)
|
||||
}
|
||||
|
||||
androidComponents {
|
||||
onVariants(selector().withBuildType("release")) {
|
||||
it.packaging.resources.excludes.addAll(
|
||||
"/META-INF/**",
|
||||
"/kotlin/**",
|
||||
"**.txt",
|
||||
"**.bin",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
baselineProfile {
|
||||
mergeIntoMain = true
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(projects.core.data)
|
||||
implementation(projects.core.i18n)
|
||||
implementation(projects.core.ui)
|
||||
|
||||
// https://developer.android.com/jetpack/androidx/releases/activity
|
||||
implementation(libs.androidx.activity.compose)
|
||||
implementation(libs.androidx.appcompat)
|
||||
@@ -201,12 +187,15 @@ dependencies {
|
||||
implementation(libs.compose.destinations.core)
|
||||
ksp(libs.compose.destinations.compiler)
|
||||
|
||||
implementation(libs.compose.preference) {
|
||||
// R8 won't remove it because it adds a content provider
|
||||
exclude(group = "org.jetbrains.compose.components", module = "components-resources")
|
||||
}
|
||||
|
||||
implementation(libs.androidx.core)
|
||||
implementation(libs.androidx.core.splashscreen)
|
||||
|
||||
implementation(libs.androidx.constraintlayout.compose)
|
||||
implementation(libs.androidx.datastore)
|
||||
implementation(libs.androidx.graphics.path)
|
||||
|
||||
// https://developer.android.com/jetpack/androidx/releases/lifecycle
|
||||
implementation(libs.androidx.lifecycle.process)
|
||||
@@ -216,15 +205,16 @@ dependencies {
|
||||
implementation(libs.androidx.paging.compose)
|
||||
|
||||
// https://developer.android.com/jetpack/androidx/releases/room
|
||||
ksp(libs.androidx.room.compiler)
|
||||
implementation(libs.androidx.room.paging)
|
||||
|
||||
implementation(libs.androidx.work.runtime)
|
||||
implementation(libs.material.motion.core)
|
||||
implementation(libs.material.kolor)
|
||||
|
||||
implementation(libs.bundles.splitties)
|
||||
|
||||
implementation(libs.okio.jvm)
|
||||
// https://square.github.io/okhttp/changelogs/changelog/
|
||||
implementation(platform(libs.okhttp.bom))
|
||||
|
||||
implementation(libs.logcat)
|
||||
|
||||
@@ -237,8 +227,7 @@ dependencies {
|
||||
implementation(libs.reorderable)
|
||||
|
||||
implementation(platform(libs.arrow.stack))
|
||||
implementation(libs.arrow.fx.coroutines)
|
||||
implementation(libs.arrow.resilience)
|
||||
implementation(libs.bundles.arrow)
|
||||
|
||||
// https://coil-kt.github.io/coil/changelog/
|
||||
implementation(platform(libs.coil.bom))
|
||||
@@ -246,18 +235,15 @@ dependencies {
|
||||
|
||||
implementation(libs.telephoto.zoomable)
|
||||
|
||||
implementation(libs.ktor.client.core)
|
||||
implementation(libs.ktor.client.okhttp)
|
||||
|
||||
implementation(libs.bundles.kotlinx.serialization)
|
||||
|
||||
implementation(libs.kotlinx.coroutines.android)
|
||||
implementation(libs.kotlinx.datetime)
|
||||
implementation(libs.jsoup)
|
||||
|
||||
coreLibraryDesugaring(libs.desugar)
|
||||
|
||||
implementation(libs.cronet.embedded)
|
||||
|
||||
implementation(libs.androidx.profileinstaller)
|
||||
"baselineProfile"(project(":benchmark"))
|
||||
|
||||
@@ -266,54 +252,26 @@ dependencies {
|
||||
}
|
||||
|
||||
kotlin {
|
||||
jvmToolchain(21)
|
||||
compilerOptions {
|
||||
freeCompilerArgs = listOf(
|
||||
// https://kotlinlang.org/docs/compiler-reference.html#progressive
|
||||
"-progressive",
|
||||
"-Xjvm-default=all",
|
||||
"-Xcontext-receivers",
|
||||
"-Xwhen-guards",
|
||||
"-Xsuppress-warning=CONTEXT_RECEIVERS_DEPRECATED",
|
||||
|
||||
"-opt-in=coil3.annotation.ExperimentalCoilApi",
|
||||
"-opt-in=androidx.compose.foundation.layout.ExperimentalLayoutApi",
|
||||
"-opt-in=androidx.compose.material3.ExperimentalMaterial3Api",
|
||||
"-opt-in=androidx.compose.material3.adaptive.ExperimentalMaterial3AdaptiveApi",
|
||||
"-opt-in=androidx.compose.ui.ExperimentalComposeUiApi",
|
||||
"-opt-in=androidx.compose.foundation.ExperimentalFoundationApi",
|
||||
"-opt-in=androidx.compose.animation.ExperimentalAnimationApi",
|
||||
"-opt-in=androidx.compose.animation.ExperimentalSharedTransitionApi",
|
||||
"-opt-in=androidx.paging.ExperimentalPagingApi",
|
||||
"-opt-in=kotlin.contracts.ExperimentalContracts",
|
||||
"-opt-in=kotlinx.coroutines.ExperimentalCoroutinesApi",
|
||||
"-opt-in=kotlinx.coroutines.FlowPreview",
|
||||
"-opt-in=kotlinx.serialization.ExperimentalSerializationApi",
|
||||
"-opt-in=splitties.experimental.ExperimentalSplittiesApi",
|
||||
"-opt-in=splitties.preferences.DataStorePreferencesPreview",
|
||||
optIn.addAll(
|
||||
"coil3.annotation.ExperimentalCoilApi",
|
||||
"androidx.paging.ExperimentalPagingApi",
|
||||
"me.saket.telephoto.ExperimentalTelephotoApi",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
ksp {
|
||||
arg("room.schemaLocation", "$projectDir/schemas")
|
||||
arg("compose-destinations.codeGenPackageName", "com.hippo.ehviewer.ui")
|
||||
}
|
||||
|
||||
aboutLibraries {
|
||||
duplicationMode = MERGE
|
||||
duplicationRule = GROUP
|
||||
}
|
||||
|
||||
val ktlintVersion = libs.ktlint.get().version
|
||||
|
||||
spotless {
|
||||
kotlin {
|
||||
// https://github.com/diffplug/spotless/issues/111
|
||||
target("src/**/*.kt")
|
||||
ktlint(ktlintVersion)
|
||||
collect {
|
||||
includePlatform = false
|
||||
}
|
||||
kotlinGradle {
|
||||
ktlint(ktlintVersion)
|
||||
library {
|
||||
exclusionPatterns.add(Pattern.compile("org\\.jetbrains\\.(?:compose|androidx)\\..*"))
|
||||
duplicationMode = DuplicateMode.MERGE
|
||||
duplicationRule = DuplicateRule.GROUP
|
||||
}
|
||||
}
|
||||
Vendored
+4
-2
@@ -11,11 +11,13 @@
|
||||
public static final ** CREATOR;
|
||||
}
|
||||
|
||||
-keepclassmembers,allowobfuscation class com.hippo.ehviewer.coil.AnimatedWebPDrawable {
|
||||
java.nio.ByteBuffer source;
|
||||
}
|
||||
|
||||
# Ktor logger
|
||||
-dontwarn org.slf4j.impl.StaticLoggerBinder
|
||||
|
||||
-dontwarn org.conscrypt.Conscrypt
|
||||
|
||||
# https://issuetracker.google.com/222232895
|
||||
-dontwarn androidx.window.extensions.**
|
||||
-dontwarn androidx.window.sidecar.Sidecar*
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
From d1db03ebd9989d10a5a905b792b712c6487a0057 Mon Sep 17 00:00:00 2001
|
||||
From: FooIbar <118464521+FooIbar@users.noreply.github.com>
|
||||
Date: Sat, 24 Feb 2024 12:03:15 +0000
|
||||
Subject: [PATCH] Don't build other binaries
|
||||
|
||||
---
|
||||
CMakeLists.txt | 8 ++++----
|
||||
1 file changed, 4 insertions(+), 4 deletions(-)
|
||||
|
||||
diff --git a/CMakeLists.txt b/CMakeLists.txt
|
||||
index a346dd02..52b5d0d8 100644
|
||||
--- a/CMakeLists.txt
|
||||
+++ b/CMakeLists.txt
|
||||
@@ -1567,7 +1567,7 @@ endif()
|
||||
# xzdec and lzmadec
|
||||
#############################################################################
|
||||
|
||||
-if(HAVE_DECODERS AND (NOT MSVC OR MSVC_VERSION GREATER_EQUAL 1900))
|
||||
+if(0)
|
||||
foreach(XZDEC xzdec lzmadec)
|
||||
add_executable("${XZDEC}"
|
||||
src/common/sysdefs.h
|
||||
@@ -1624,7 +1624,7 @@ endif()
|
||||
# lzmainfo
|
||||
#############################################################################
|
||||
|
||||
-if(HAVE_DECODERS AND (NOT MSVC OR MSVC_VERSION GREATER_EQUAL 1900))
|
||||
+if(0)
|
||||
add_executable(lzmainfo
|
||||
src/common/sysdefs.h
|
||||
src/common/tuklib_common.h
|
||||
@@ -1680,7 +1680,7 @@ endif()
|
||||
# xz
|
||||
#############################################################################
|
||||
|
||||
-if(NOT MSVC OR MSVC_VERSION GREATER_EQUAL 1900)
|
||||
+if(0)
|
||||
add_executable(xz
|
||||
src/common/mythread.h
|
||||
src/common/sysdefs.h
|
||||
@@ -1914,7 +1914,7 @@ endif()
|
||||
# Scripts
|
||||
#############################################################################
|
||||
|
||||
-if(UNIX)
|
||||
+if(0)
|
||||
# NOTE: This isn't as sophisticated as in the Autotools build which
|
||||
# uses posix-shell.m4 but hopefully this doesn't need to be either.
|
||||
# CMake likely won't be used on as many (old) obscure systems as the
|
||||
--
|
||||
2.34.1
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
From e971b9f23727833cc39b9e325db30f383e5bfc30 Mon Sep 17 00:00:00 2001
|
||||
From: Dude so hot <djohn@fbi.gov>
|
||||
Date: Thu, 14 Nov 2024 00:28:13 +0800
|
||||
Subject: [PATCH] Fix zip_time performance
|
||||
|
||||
Signed-off-by: Dude so hot <djohn@fbi.gov>
|
||||
---
|
||||
libarchive/archive_time.c | 1 +
|
||||
1 file changed, 1 insertion(+)
|
||||
|
||||
diff --git a/libarchive/archive_time.c b/libarchive/archive_time.c
|
||||
index 3352c809..9289bf1e 100644
|
||||
--- a/libarchive/archive_time.c
|
||||
+++ b/libarchive/archive_time.c
|
||||
@@ -53,6 +53,7 @@ FILETIME_to_ntfs(const FILETIME* filetime)
|
||||
int64_t
|
||||
dos_to_unix(uint32_t dos_time)
|
||||
{
|
||||
+ return 0;
|
||||
uint16_t msTime, msDate;
|
||||
struct tm ts;
|
||||
time_t t;
|
||||
--
|
||||
2.47.0
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
From aee8f7147ddb262fd9e5feee8d5b17094cf3470f Mon Sep 17 00:00:00 2001
|
||||
From: FooIbar <118464521+FooIbar@users.noreply.github.com>
|
||||
Date: Tue, 26 Sep 2023 00:09:25 +0800
|
||||
Subject: [PATCH] Insert link libs
|
||||
|
||||
---
|
||||
CMakeLists.txt | 2 +-
|
||||
1 file changed, 1 insertion(+), 1 deletion(-)
|
||||
|
||||
diff --git a/CMakeLists.txt b/CMakeLists.txt
|
||||
index ec97e4c7..420f204c 100644
|
||||
--- a/CMakeLists.txt
|
||||
+++ b/CMakeLists.txt
|
||||
@@ -438,7 +438,7 @@ IF(DEFINED __GNUWIN32PATH AND EXISTS "${__GNUWIN32PATH}")
|
||||
# # endif
|
||||
ENDIF(DEFINED __GNUWIN32PATH AND EXISTS "${__GNUWIN32PATH}")
|
||||
|
||||
-SET(ADDITIONAL_LIBS "")
|
||||
+SET(ADDITIONAL_LIBS ${LIBARCHIVE_CUSTOM_LIBS})
|
||||
#
|
||||
# Find ZLIB
|
||||
#
|
||||
--
|
||||
2.34.1
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
From 86d199429b3fabc83f7dcc9afd72e7b4c7750b1a Mon Sep 17 00:00:00 2001
|
||||
From: FooIbar <118464521+FooIbar@users.noreply.github.com>
|
||||
Date: Thu, 14 Nov 2024 20:22:52 +0800
|
||||
Subject: [PATCH] Use UTF-8 as default charset on bionic
|
||||
|
||||
---
|
||||
libarchive/archive_string.c | 4 +++-
|
||||
1 file changed, 3 insertions(+), 1 deletion(-)
|
||||
|
||||
diff --git a/libarchive/archive_string.c b/libarchive/archive_string.c
|
||||
index abf7ad66..63a73c58 100644
|
||||
--- a/libarchive/archive_string.c
|
||||
+++ b/libarchive/archive_string.c
|
||||
@@ -423,7 +423,9 @@ static const char *
|
||||
default_iconv_charset(const char *charset) {
|
||||
if (charset != NULL && charset[0] != '\0')
|
||||
return charset;
|
||||
-#if HAVE_LOCALE_CHARSET && !defined(__APPLE__)
|
||||
+#ifdef __BIONIC__
|
||||
+ return "UTF-8";
|
||||
+#elif HAVE_LOCALE_CHARSET && !defined(__APPLE__)
|
||||
/* locale_charset() is broken on Mac OS */
|
||||
return locale_charset();
|
||||
#elif HAVE_NL_LANGINFO
|
||||
--
|
||||
2.43.0
|
||||
|
||||
@@ -18,20 +18,26 @@ if (NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||
endif (NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||
|
||||
option(BUILD_TESTING OFF)
|
||||
|
||||
set(XZ_PATCH ${CMAKE_CURRENT_LIST_DIR}/0001-Don-t-build-other-binaries.patch)
|
||||
option(XZ_DOC OFF)
|
||||
option(XZ_LZIP_DECODER OFF)
|
||||
option(XZ_MICROLZMA_DECODER OFF)
|
||||
option(XZ_MICROLZMA_ENCODER OFF)
|
||||
option(XZ_TOOL_LZMADEC OFF)
|
||||
option(XZ_TOOL_LZMAINFO OFF)
|
||||
option(XZ_TOOL_XZ OFF)
|
||||
option(XZ_TOOL_XZDEC OFF)
|
||||
FetchContent_Declare(
|
||||
liblzma
|
||||
GIT_REPOSITORY https://github.com/tukaani-project/xz.git
|
||||
GIT_TAG v5.6.2
|
||||
PATCH_COMMAND git apply --check -R ${XZ_PATCH} || git apply ${XZ_PATCH}
|
||||
GIT_TAG v5.8.1
|
||||
GIT_SHALLOW 1
|
||||
)
|
||||
|
||||
FetchContent_MakeAvailable(liblzma)
|
||||
include_directories(${liblzma_SOURCE_DIR}/src/liblzma/api)
|
||||
|
||||
# Build GNUTLS libnettle
|
||||
if (CMAKE_HOST_SYSTEM_NAME STREQUAL Windows)
|
||||
if (CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
|
||||
execute_process(
|
||||
COMMAND wsl wslpath ${CMAKE_CURRENT_LIST_DIR}
|
||||
OUTPUT_VARIABLE CURRENT_DIR
|
||||
@@ -43,15 +49,15 @@ endif ()
|
||||
set(NDK_VERSION "${ANDROID_NDK_MAJOR}.${ANDROID_NDK_MINOR}.${ANDROID_NDK_BUILD}")
|
||||
ExternalProject_Add(
|
||||
nettle
|
||||
PREFIX nettle
|
||||
GIT_REPOSITORY https://gitlab.com/gnutls/nettle.git
|
||||
GIT_TAG nettle_3.10_release_20240616
|
||||
GIT_TAG nettle_3.10.2_release_20250626
|
||||
GIT_SHALLOW 1
|
||||
GIT_CONFIG core.autocrlf=input
|
||||
CONFIGURE_COMMAND bash ${CURRENT_DIR}/configure_nettle.sh ${NDK_VERSION} ${CMAKE_C_COMPILER_TARGET}
|
||||
BUILD_COMMAND bash -c "make libnettle.a CFLAGS='${CMAKE_C_FLAGS}'"
|
||||
BUILD_COMMAND bash -c "make -j4 libnettle.a CFLAGS='${CMAKE_C_FLAGS} -Qunused-arguments'"
|
||||
INSTALL_COMMAND ""
|
||||
BUILD_IN_SOURCE 1
|
||||
BUILD_BYPRODUCTS nettle/src/nettle/libnettle.a
|
||||
BUILD_BYPRODUCTS <SOURCE_DIR>/libnettle.a
|
||||
)
|
||||
|
||||
ExternalProject_Get_Property(nettle SOURCE_DIR)
|
||||
@@ -83,33 +89,69 @@ option(ENABLE_CAT OFF)
|
||||
option(ENABLE_UNZIP OFF)
|
||||
option(ENABLE_TEST OFF)
|
||||
|
||||
# Configure libarchive link's static lib
|
||||
SET(LIBARCHIVE_CUSTOM_LIBS "libnettle" "liblzma")
|
||||
|
||||
set(LIBARCHIVE_PATCH ${CMAKE_CURRENT_LIST_DIR}/0001-Insert-link-libs.patch)
|
||||
set(LIBARCHIVE_PATCH
|
||||
${CMAKE_CURRENT_LIST_DIR}/0001-Fix-zip_time-performance.patch
|
||||
${CMAKE_CURRENT_LIST_DIR}/0002-Use-UTF-8-as-default-charset-on-bionic.patch
|
||||
)
|
||||
FetchContent_Declare(
|
||||
libarchive
|
||||
GIT_REPOSITORY https://github.com/libarchive/libarchive.git
|
||||
GIT_TAG v3.7.6
|
||||
GIT_TAG v3.8.1
|
||||
GIT_SHALLOW 1
|
||||
PATCH_COMMAND git apply --check -R ${LIBARCHIVE_PATCH} || git apply ${LIBARCHIVE_PATCH}
|
||||
)
|
||||
|
||||
FetchContent_MakeAvailable(libarchive)
|
||||
target_link_libraries(archive_static liblzma libnettle)
|
||||
include_directories(${libarchive_SOURCE_DIR}/libarchive)
|
||||
|
||||
FetchContent_Declare(
|
||||
libwebp
|
||||
GIT_REPOSITORY https://github.com/webmproject/libwebp.git
|
||||
GIT_TAG v1.6.0
|
||||
GIT_SHALLOW 1
|
||||
)
|
||||
|
||||
option(WEBP_BUILD_ANIM_UTILS OFF)
|
||||
option(WEBP_BUILD_CWEBP OFF)
|
||||
option(WEBP_BUILD_DWEBP OFF)
|
||||
option(WEBP_BUILD_VWEBP OFF)
|
||||
option(WEBP_BUILD_WEBPINFO OFF)
|
||||
option(WEBP_BUILD_LIBWEBPMUX OFF)
|
||||
option(WEBP_BUILD_EXTRAS OFF)
|
||||
option(WEBP_ENABLE_SWAP_16BIT_CSP ON)
|
||||
|
||||
# Disable runtime detection of NEON support as NEON is required since API level 23
|
||||
# https://source.android.com/docs/compatibility/6.0/android-6.0-cdd#3_3_1_application_binary_interfaces
|
||||
unset(ANDROID)
|
||||
FetchContent_MakeAvailable(libwebp)
|
||||
if (CMAKE_SYSTEM_NAME STREQUAL "Android")
|
||||
set(ANDROID 1)
|
||||
endif ()
|
||||
|
||||
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ../../../../rust-toolchain.toml)
|
||||
unset(Rust_TOOLCHAIN CACHE)
|
||||
FetchContent_Declare(
|
||||
Corrosion
|
||||
GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git
|
||||
GIT_TAG v0.5
|
||||
GIT_TAG v0.5.2
|
||||
GIT_SHALLOW 1
|
||||
)
|
||||
|
||||
FetchContent_MakeAvailable(Corrosion)
|
||||
corrosion_import_crate(MANIFEST_PATH ../rust/Cargo.toml)
|
||||
corrosion_add_target_local_rustflags(ehviewer_rust "-Clink-args=-Wl,-z,max-page-size=16384")
|
||||
if (ANDROID_NATIVE_API_LEVEL GREATER_EQUAL 26)
|
||||
corrosion_set_features(ehviewer_rust FEATURES api-level-26)
|
||||
endif ()
|
||||
corrosion_import_crate(
|
||||
MANIFEST_PATH ../rust/Cargo.toml
|
||||
LOCKED
|
||||
FEATURES $<IF:$<VERSION_GREATER_EQUAL:${ANDROID_NATIVE_API_LEVEL},26>,android-26,android>
|
||||
FLAGS $<$<NOT:$<CONFIG:Debug>>:-Z build-std -Z build-std-features=>
|
||||
)
|
||||
corrosion_add_target_rustflags(ehviewer_rust $<$<NOT:$<CONFIG:Debug>>:-Z unstable-options -C panic=immediate-abort>)
|
||||
corrosion_link_libraries(ehviewer_rust android jnigraphics log webp webpdemux)
|
||||
|
||||
# Build and link our app's native lib
|
||||
add_library(${PROJECT_NAME} SHARED archive.c gifutils.c hash.c natsort/strnatcmp.c)
|
||||
target_link_libraries(${PROJECT_NAME} ehviewer_rust archive_static log jnigraphics -Wl,--exclude-libs,ALL)
|
||||
target_link_libraries(
|
||||
${PROJECT_NAME} archive_static ehviewer_rust
|
||||
-Wl,-u,JNI_OnLoad -Wl,--undefined-glob,Java_*
|
||||
-Wl,--version-script,${CMAKE_SOURCE_DIR}/libehviewer.map.txt
|
||||
)
|
||||
+135
-174
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2022 Tarsin Norbin
|
||||
* Copyright 2022-2024 Tarsin Norbin
|
||||
*
|
||||
* This file is part of EhViewer
|
||||
*
|
||||
@@ -45,33 +45,29 @@ typedef struct {
|
||||
typedef struct {
|
||||
const char *filename;
|
||||
int index;
|
||||
size_t size;
|
||||
ssize_t size;
|
||||
void *addr;
|
||||
} entry;
|
||||
|
||||
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
static archive_ctx **ctx_pool = NULL;
|
||||
#define CTX_POOL_SIZE 20
|
||||
#define MAX_PARALLEL_DECOMP 4
|
||||
#define max(a, b) ((a) > (b) ? (a) : (b))
|
||||
|
||||
static void *mempool = MAP_FAILED;
|
||||
static size_t *mempoolofs = NULL;
|
||||
static int page_size = 0;
|
||||
|
||||
#define PAGE_ALIGN(x) ((x + page_size - 1) & ~(page_size - 1))
|
||||
|
||||
#define MEMPOOL_ADDR_BY_SORTED_IDX(x) (mempool + (index ? mempoolofs[index - 1] : 0))
|
||||
#define MEMPOOL_SIZE (mempoolofs[entryCount - 1])
|
||||
|
||||
#define PROT_RW (PROT_WRITE | PROT_READ)
|
||||
#define MAP_ANON_POOL (MAP_ANONYMOUS | MAP_NORESERVE | MAP_PRIVATE)
|
||||
|
||||
static pthread_mutex_t ctx_pool_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
static archive_ctx **ctx_pool = NULL;
|
||||
static pthread_mutex_t buffer_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
static void *decode_buffer[MAX_PARALLEL_DECOMP];
|
||||
static bool need_encrypt = false;
|
||||
static char *passwd = NULL;
|
||||
static void *archiveAddr = MAP_FAILED;
|
||||
static size_t archiveSize = 0;
|
||||
static entry *entries = NULL;
|
||||
static size_t entryCount = 0;
|
||||
static ssize_t max_file_size = 0;
|
||||
|
||||
const char supportExt[10][6] = {
|
||||
#define SUPPORT_EXT_COUNT 11
|
||||
|
||||
const char supportExt[SUPPORT_EXT_COUNT][5] = {
|
||||
"jpeg",
|
||||
"jpg",
|
||||
"png",
|
||||
@@ -80,6 +76,7 @@ const char supportExt[10][6] = {
|
||||
"bmp",
|
||||
"ico",
|
||||
"wbmp",
|
||||
"heic",
|
||||
"heif",
|
||||
"avif"
|
||||
};
|
||||
@@ -91,24 +88,19 @@ static inline int filename_is_playable_file(const char *name) {
|
||||
if (!dotptr++)
|
||||
return false;
|
||||
int i;
|
||||
for (i = 0; i < 10; i++)
|
||||
for (i = 0; i < SUPPORT_EXT_COUNT; i++)
|
||||
if (strcmp(dotptr, supportExt[i]) == 0)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
static inline const char *archive_entry_pathname2(struct archive_entry *entry) {
|
||||
const char *name = archive_entry_pathname_utf8(entry);
|
||||
return name ? name : archive_entry_pathname(entry);
|
||||
}
|
||||
|
||||
static inline bool archive_entry_is_file(struct archive_entry *entry) {
|
||||
return archive_entry_filetype(entry) == AE_IFREG;
|
||||
}
|
||||
|
||||
static inline bool archive_entry_is_playable(struct archive_entry *entry) {
|
||||
return archive_entry_is_file(entry) &&
|
||||
filename_is_playable_file(archive_entry_pathname2(entry));
|
||||
filename_is_playable_file(archive_entry_pathname(entry));
|
||||
}
|
||||
|
||||
static inline int compare_entries(const void *a, const void *b) {
|
||||
@@ -117,52 +109,64 @@ static inline int compare_entries(const void *a, const void *b) {
|
||||
return strnatcmp(fa, fb);
|
||||
}
|
||||
|
||||
static int archive_map_entries_index(archive_ctx *ctx, bool sort) {
|
||||
#define ADDR_IN_FILE_MAPPING(addr) (addr >= archiveAddr && addr < archiveAddr + archiveSize)
|
||||
|
||||
static bool fill_entry_zero_copy(struct archive *arc, entry *entry) {
|
||||
void *buffer = NULL;
|
||||
size_t buffer_size = 0;
|
||||
la_int64_t output_ofs = 0;
|
||||
archive_read_data_block(arc, (const void **) &buffer, &buffer_size, &output_ofs);
|
||||
bool zero_copy = ADDR_IN_FILE_MAPPING(buffer) && !output_ofs && buffer_size == entry->size;
|
||||
entry->addr = zero_copy ? buffer : NULL;
|
||||
return zero_copy;
|
||||
}
|
||||
|
||||
static void archive_map_entries_index(archive_ctx *ctx, bool sort) {
|
||||
int count = 0;
|
||||
bool zero_copy = true;
|
||||
while (archive_read_next_header(ctx->arc, &ctx->entry) == ARCHIVE_OK) {
|
||||
const char *name = archive_entry_pathname2(ctx->entry);
|
||||
const char *name = archive_entry_pathname(ctx->entry);
|
||||
if (archive_entry_is_file(ctx->entry) && filename_is_playable_file(name)) {
|
||||
entries[count].filename = strdup(name);
|
||||
entries[count].index = count;
|
||||
entries[count].size = archive_entry_size(ctx->entry);
|
||||
ssize_t size = archive_entry_size(ctx->entry);
|
||||
max_file_size = max(size, max_file_size);
|
||||
entries[count].size = size;
|
||||
// We don't expect zero copy if first content can't do zero copy
|
||||
if (zero_copy) zero_copy = fill_entry_zero_copy(ctx->arc, &entries[count]);
|
||||
count++;
|
||||
}
|
||||
}
|
||||
if (sort)
|
||||
qsort(entries, entryCount, sizeof(entry), compare_entries);
|
||||
return count;
|
||||
if (sort) qsort(entries, entryCount, sizeof(entry), compare_entries);
|
||||
}
|
||||
|
||||
static bool archive_prealloc_mempool() {
|
||||
mempoolofs = calloc(entryCount, sizeof(size_t));
|
||||
for (int i = 0; i < entryCount; ++i) {
|
||||
if (!i)
|
||||
mempoolofs[i] = PAGE_ALIGN(entries[i].size);
|
||||
else
|
||||
mempoolofs[i] = PAGE_ALIGN(entries[i].size) + mempoolofs[i - 1];
|
||||
static void *acquire_decode_buffer() {
|
||||
void *addr = NULL;
|
||||
pthread_mutex_lock(&buffer_mutex);
|
||||
for (int i = 0; i < MAX_PARALLEL_DECOMP; ++i) {
|
||||
addr = decode_buffer[i];
|
||||
if (addr) {
|
||||
decode_buffer[i] = NULL;
|
||||
break;
|
||||
}
|
||||
}
|
||||
mempool = mmap(0, MEMPOOL_SIZE, PROT_RW, MAP_ANON_POOL, -1, 0);
|
||||
if (mempool == MAP_FAILED) {
|
||||
LOGE("%s%s", "mmap failed with error ", strerror(errno));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
pthread_mutex_unlock(&buffer_mutex);
|
||||
if (!addr) addr = malloc(max_file_size);
|
||||
return addr;
|
||||
}
|
||||
|
||||
#define ADDR_IN_FILE_MAPPING(addr) (addr >= archiveAddr && addr < archiveAddr + archiveSize)
|
||||
|
||||
static void mempool_release_pages(void *addr, size_t size) {
|
||||
size = PAGE_ALIGN(size);
|
||||
madvise_log_if_error(addr, size, MADV_DONTNEED);
|
||||
}
|
||||
|
||||
static bool kernel_can_prefault = true;
|
||||
|
||||
static void mempool_prefault_pages(void *addr, size_t size) {
|
||||
size = PAGE_ALIGN(size);
|
||||
if (kernel_can_prefault) {
|
||||
if (madvise(addr, size, MADV_POPULATE_WRITE)) kernel_can_prefault = false;
|
||||
static void release_decode_buffer(void *buffer) {
|
||||
pthread_mutex_lock(&buffer_mutex);
|
||||
for (int i = 0; i < MAX_PARALLEL_DECOMP; ++i) {
|
||||
void *addr = decode_buffer[i];
|
||||
if (!addr) {
|
||||
decode_buffer[i] = buffer;
|
||||
pthread_mutex_unlock(&buffer_mutex);
|
||||
return;
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&buffer_mutex);
|
||||
free(buffer);
|
||||
}
|
||||
|
||||
static int archive_list_all_entries(archive_ctx *ctx) {
|
||||
@@ -181,18 +185,10 @@ static void archive_release_ctx(archive_ctx *ctx) {
|
||||
}
|
||||
}
|
||||
|
||||
static int archive_alloc_ctx(archive_ctx **ctxptr) {
|
||||
archive_ctx *ctx;
|
||||
int err;
|
||||
ctx = calloc(1, sizeof(archive_ctx));
|
||||
if (!ctx)
|
||||
return -ENOMEM;
|
||||
static archive_ctx *archive_alloc_ctx() {
|
||||
archive_ctx *ctx = calloc(1, sizeof(archive_ctx));
|
||||
ctx->arc = archive_read_new();
|
||||
ctx->using = 1;
|
||||
if (!ctx->arc) {
|
||||
free(ctx);
|
||||
return -ENOMEM;
|
||||
}
|
||||
archive_read_support_format_tar(ctx->arc);
|
||||
archive_read_support_format_7zip(ctx->arc);
|
||||
archive_read_support_format_rar5(ctx->arc);
|
||||
@@ -202,14 +198,14 @@ static int archive_alloc_ctx(archive_ctx **ctxptr) {
|
||||
archive_read_set_option(ctx->arc, "zip", "ignorecrc32", "1");
|
||||
if (passwd)
|
||||
archive_read_add_passphrase(ctx->arc, passwd);
|
||||
err = archive_read_open_memory(ctx->arc, archiveAddr, archiveSize);
|
||||
if (err) {
|
||||
int err = archive_read_open_memory(ctx->arc, archiveAddr, archiveSize);
|
||||
if (err < ARCHIVE_OK) {
|
||||
LOGE("%s%s", "Open archive failed: ", archive_error_string(ctx->arc));
|
||||
archive_read_free(ctx->arc);
|
||||
free(ctx);
|
||||
return err;
|
||||
return NULL;
|
||||
}
|
||||
*ctxptr = ctx;
|
||||
return 0;
|
||||
return ctx;
|
||||
}
|
||||
|
||||
static int archive_skip_to_index(archive_ctx *ctx, int index) {
|
||||
@@ -226,7 +222,7 @@ static int archive_skip_to_index(archive_ctx *ctx, int index) {
|
||||
static int archive_get_ctx(archive_ctx **ctxptr, int idx) {
|
||||
int ret;
|
||||
archive_ctx *ctx = NULL;
|
||||
pthread_mutex_lock(&mutex);
|
||||
pthread_mutex_lock(&ctx_pool_mutex);
|
||||
for (int i = 0; i < CTX_POOL_SIZE; i++) {
|
||||
if (!ctx_pool[i])
|
||||
continue;
|
||||
@@ -241,16 +237,14 @@ static int archive_get_ctx(archive_ctx **ctxptr, int idx) {
|
||||
}
|
||||
if (ctx)
|
||||
ctx->using = 1;
|
||||
pthread_mutex_unlock(&mutex);
|
||||
pthread_mutex_unlock(&ctx_pool_mutex);
|
||||
|
||||
if (!ctx) {
|
||||
archive_ctx *victimCtx = NULL;
|
||||
int victimIdx = 0;
|
||||
int replace = 1;
|
||||
ret = archive_alloc_ctx(&ctx);
|
||||
if (ret)
|
||||
return ret;
|
||||
pthread_mutex_lock(&mutex);
|
||||
ctx = archive_alloc_ctx();
|
||||
pthread_mutex_lock(&ctx_pool_mutex);
|
||||
for (int i = 0; i < CTX_POOL_SIZE; i++) {
|
||||
if (!ctx_pool[i]) {
|
||||
ctx_pool[i] = ctx;
|
||||
@@ -265,13 +259,13 @@ static int archive_get_ctx(archive_ctx **ctxptr, int idx) {
|
||||
}
|
||||
}
|
||||
if (replace) ctx_pool[victimIdx] = ctx;
|
||||
pthread_mutex_unlock(&mutex);
|
||||
pthread_mutex_unlock(&ctx_pool_mutex);
|
||||
if (replace) archive_release_ctx(victimCtx);
|
||||
}
|
||||
ret = archive_skip_to_index(ctx, idx);
|
||||
if (ret != idx) {
|
||||
ret = archive_errno(ctx->arc);
|
||||
LOGE("Skip to index failed:%s", archive_error_string(ctx->arc));
|
||||
LOGE("Skip to index failed: %s", archive_error_string(ctx->arc));
|
||||
archive_release_ctx(ctx);
|
||||
return ret;
|
||||
}
|
||||
@@ -283,7 +277,6 @@ JNIEXPORT jint JNICALL
|
||||
Java_com_hippo_ehviewer_jni_ArchiveKt_openArchive(JNIEnv *env, jclass thiz, jint fd, jlong size, jboolean sort_entries) {
|
||||
EH_UNUSED(env);
|
||||
EH_UNUSED(thiz);
|
||||
page_size = getpagesize();
|
||||
archive_ctx *ctx = NULL;
|
||||
archiveAddr = mmap(0, size, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||
if (archiveAddr == MAP_FAILED) {
|
||||
@@ -292,106 +285,74 @@ Java_com_hippo_ehviewer_jni_ArchiveKt_openArchive(JNIEnv *env, jclass thiz, jint
|
||||
}
|
||||
archiveSize = size;
|
||||
ctx_pool = calloc(CTX_POOL_SIZE, sizeof(archive_ctx **));
|
||||
if (!ctx_pool) {
|
||||
LOGE("Allocate archive ctx pool failed:ENOMEM");
|
||||
ctx = archive_alloc_ctx();
|
||||
if (!ctx) return 0;
|
||||
|
||||
entryCount = archive_list_all_entries(ctx);
|
||||
LOGI("%s%zu%s", "Found ", entryCount, " images in archive");
|
||||
if (!entryCount) {
|
||||
LOGE("%s%s", "Archive read failed: ", archive_error_string(ctx->arc));
|
||||
archive_release_ctx(ctx);
|
||||
return 0;
|
||||
}
|
||||
int r = archive_alloc_ctx(&ctx);
|
||||
if (r) {
|
||||
if (r == -ENOMEM)
|
||||
LOGE("%s", "Mem alloc failed");
|
||||
LOGE("%s%s", "Archive open failed:", archive_error_string(ctx->arc));
|
||||
return 0;
|
||||
} else {
|
||||
entryCount = archive_list_all_entries(ctx);
|
||||
LOGI("%s%zu%s", "Found ", entryCount, " image entries in archive");
|
||||
if (!entryCount) {
|
||||
LOGE("%s%s", "Archive open failed:", archive_error_string(ctx->arc));
|
||||
archive_release_ctx(ctx);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// We must read through the file|vm then we can know whether it is encrypted
|
||||
int encryptRet = archive_read_has_encrypted_entries(ctx->arc);
|
||||
switch (encryptRet) {
|
||||
case 1: // At lease 1 encrypted entry
|
||||
need_encrypt = true;
|
||||
break;
|
||||
case 0: // format supports but no encrypted entry found
|
||||
default:
|
||||
need_encrypt = false;
|
||||
}
|
||||
|
||||
int format = archive_format(ctx->arc);
|
||||
switch (format) {
|
||||
case ARCHIVE_FORMAT_ZIP:
|
||||
case ARCHIVE_FORMAT_RAR_V5:
|
||||
madvise_log_if_error(archiveAddr, archiveSize, MADV_SEQUENTIAL);
|
||||
break;
|
||||
case ARCHIVE_FORMAT_7ZIP: // Seek is bad
|
||||
madvise_log_if_error(archiveAddr, archiveSize, MADV_RANDOM);
|
||||
break;
|
||||
default:;
|
||||
}
|
||||
// We must read through the file|vm then we can know whether it is encrypted
|
||||
int encryptRet = archive_read_has_encrypted_entries(ctx->arc);
|
||||
switch (encryptRet) {
|
||||
case 1: // At lease 1 encrypted entry
|
||||
need_encrypt = true;
|
||||
break;
|
||||
case 0: // format supports but no encrypted entry found
|
||||
default:
|
||||
need_encrypt = false;
|
||||
}
|
||||
|
||||
archive_release_ctx(ctx);
|
||||
|
||||
r = archive_alloc_ctx(&ctx);
|
||||
if (r) {
|
||||
if (r == -ENOMEM)
|
||||
LOGE("%s", "Mem alloc failed");
|
||||
r = 0;
|
||||
LOGE("%s%s", "Archive open failed:", archive_error_string(ctx->arc));
|
||||
} else {
|
||||
entries = calloc(entryCount, sizeof(entry));
|
||||
r = archive_map_entries_index(ctx, sort_entries);
|
||||
if (!archive_prealloc_mempool()) {
|
||||
r = 0;
|
||||
}
|
||||
int format = archive_format(ctx->arc);
|
||||
switch (format) {
|
||||
case ARCHIVE_FORMAT_ZIP:
|
||||
case ARCHIVE_FORMAT_RAR_V5:
|
||||
madvise_log_if_error(archiveAddr, archiveSize, MADV_SEQUENTIAL);
|
||||
break;
|
||||
case ARCHIVE_FORMAT_7ZIP: // Seek is bad
|
||||
madvise_log_if_error(archiveAddr, archiveSize, MADV_RANDOM);
|
||||
break;
|
||||
default:;
|
||||
}
|
||||
|
||||
archive_release_ctx(ctx);
|
||||
return r;
|
||||
|
||||
ctx = archive_alloc_ctx();
|
||||
if (!ctx) return 0;
|
||||
entries = calloc(entryCount, sizeof(entry));
|
||||
archive_map_entries_index(ctx, sort_entries);
|
||||
archive_release_ctx(ctx);
|
||||
return (int) entryCount;
|
||||
}
|
||||
|
||||
JNIEXPORT jobject JNICALL
|
||||
Java_com_hippo_ehviewer_jni_ArchiveKt_extractToByteBuffer(JNIEnv *env, jclass thiz, jint index) {
|
||||
EH_UNUSED(env);
|
||||
EH_UNUSED(thiz);
|
||||
void *addr = MEMPOOL_ADDR_BY_SORTED_IDX(index);
|
||||
size_t size = entries[index].size;
|
||||
index = entries[index].index;
|
||||
archive_ctx *ctx = NULL;
|
||||
int ret = archive_get_ctx(&ctx, index);
|
||||
if (ret) return 0;
|
||||
void *buffer = NULL;
|
||||
size_t buffer_size = 0;
|
||||
la_int64_t output_ofs = 0;
|
||||
archive_read_data_block(ctx->arc, (const void **) &buffer, &buffer_size, &output_ofs);
|
||||
bool zero_copy = ADDR_IN_FILE_MAPPING(buffer) && !output_ofs && buffer_size == size;
|
||||
if (zero_copy) {
|
||||
ctx->using = 0;
|
||||
return (*env)->NewDirectByteBuffer(env, buffer, buffer_size);
|
||||
entry *entry = &entries[index];
|
||||
ssize_t size = entry->size;
|
||||
if (entry->addr) {
|
||||
return (*env)->NewDirectByteBuffer(env, entry->addr, size);
|
||||
} else {
|
||||
size_t bytes = 0;
|
||||
mempool_prefault_pages(addr, size);
|
||||
do {
|
||||
memcpy(addr + output_ofs, buffer, buffer_size);
|
||||
bytes += buffer_size;
|
||||
ret = archive_read_data_block(ctx->arc, (const void **) &buffer, &buffer_size, &output_ofs);
|
||||
} while (bytes != size && !ret);
|
||||
ctx->using = 0;
|
||||
if (bytes == size) {
|
||||
return (*env)->NewDirectByteBuffer(env, addr, size);
|
||||
} else {
|
||||
if (ret < 0) {
|
||||
LOGE("%s%s", "Archive read failed:", archive_error_string(ctx->arc));
|
||||
archive_ctx *ctx = NULL;
|
||||
if (!archive_get_ctx(&ctx, entry->index)) {
|
||||
void *addr = acquire_decode_buffer();
|
||||
ssize_t bytes = archive_read_data(ctx->arc, addr, size);
|
||||
ctx->using = 0;
|
||||
if (bytes == size) {
|
||||
return (*env)->NewDirectByteBuffer(env, addr, size);
|
||||
} else {
|
||||
LOGE("%s", "No enough data read, WTF?");
|
||||
if (bytes < 0) {
|
||||
LOGE("%s%s", "Archive read failed: ", archive_error_string(ctx->arc));
|
||||
} else {
|
||||
LOGE("%s", "No enough data read, WTF?");
|
||||
}
|
||||
}
|
||||
release_decode_buffer(addr);
|
||||
}
|
||||
mempool_release_pages(addr, size);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -413,12 +374,11 @@ Java_com_hippo_ehviewer_jni_ArchiveKt_closeArchive(JNIEnv *env, jclass thiz) {
|
||||
munmap(archiveAddr, archiveSize);
|
||||
archiveAddr = MAP_FAILED;
|
||||
}
|
||||
if (mempool != MAP_FAILED) {
|
||||
munmap(mempool, MEMPOOL_SIZE);
|
||||
mempool = MAP_FAILED;
|
||||
for (int i = 0; i < MAX_PARALLEL_DECOMP; ++i) {
|
||||
free(decode_buffer[i]);
|
||||
decode_buffer[i] = NULL;
|
||||
}
|
||||
free(mempoolofs);
|
||||
mempoolofs = NULL;
|
||||
max_file_size = 0;
|
||||
if (entries) {
|
||||
for (int i = 0; i < entryCount; ++i) {
|
||||
free((void *) entries[i].filename);
|
||||
@@ -445,15 +405,15 @@ Java_com_hippo_ehviewer_jni_ArchiveKt_providePassword(JNIEnv *env, jclass thiz,
|
||||
passwd = realloc(passwd, len + 1);
|
||||
(*env)->GetStringUTFRegion(env, str, 0, len, passwd);
|
||||
passwd[len] = 0;
|
||||
archive_alloc_ctx(&ctx);
|
||||
void *tmpBuf = alloca(4096);
|
||||
ctx = archive_alloc_ctx();
|
||||
char tmpBuf[4096];
|
||||
while (archive_read_next_header(ctx->arc, &entry) == ARCHIVE_OK) {
|
||||
if (!archive_entry_is_playable(entry))
|
||||
continue;
|
||||
if (!archive_entry_is_encrypted(entry))
|
||||
continue;
|
||||
if (archive_read_data(ctx->arc, tmpBuf, 4096) < ARCHIVE_OK) {
|
||||
LOGE("%s%s", "Archive read failed:", archive_error_string(ctx->arc));
|
||||
LOGE("%s%s", "Archive read failed: ", archive_error_string(ctx->arc));
|
||||
ret = false;
|
||||
}
|
||||
break;
|
||||
@@ -489,8 +449,9 @@ JNIEXPORT void JNICALL
|
||||
Java_com_hippo_ehviewer_jni_ArchiveKt_releaseByteBuffer(JNIEnv *env, jclass thiz, jobject buffer) {
|
||||
EH_UNUSED(thiz);
|
||||
void *addr = (*env)->GetDirectBufferAddress(env, buffer);
|
||||
size_t size = (*env)->GetDirectBufferCapacity(env, buffer);
|
||||
if (!ADDR_IN_FILE_MAPPING(addr)) mempool_release_pages(addr, size);
|
||||
if (!ADDR_IN_FILE_MAPPING(addr)) {
|
||||
release_decode_buffer(addr);
|
||||
}
|
||||
}
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
@@ -515,7 +476,7 @@ Java_com_hippo_ehviewer_jni_ArchiveKt_archiveFdBatch(JNIEnv *env, jclass clazz,
|
||||
archive_entry_copy_stat(entry, &st);
|
||||
archive_entry_set_perm(entry, 0644);
|
||||
archive_write_header(arc, entry);
|
||||
int len;
|
||||
size_t len;
|
||||
do {
|
||||
len = read(fd, buff, sizeof(buff));
|
||||
archive_write_data(arc, buff, len);
|
||||
|
||||
@@ -37,7 +37,7 @@ static void doRewrite(byte *addr, size_t size) {
|
||||
byte *end = addr + i + 4;
|
||||
if (end[4] != 0) continue;
|
||||
int frameDelay = end[2] << 8 | end[1];
|
||||
if (frameDelay > MINIMUM_FRAME_DELAY)
|
||||
if (frameDelay >= MINIMUM_FRAME_DELAY)
|
||||
break; // Quit if the first block looks normal, for performance
|
||||
end[1] = DEFAULT_FRAME_DELAY;
|
||||
end[2] = 0;
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
global:
|
||||
JNI_OnLoad;
|
||||
Java_*;
|
||||
local:
|
||||
*;
|
||||
};
|
||||
@@ -30,7 +30,6 @@
|
||||
* negative chars in their default char type.
|
||||
*/
|
||||
|
||||
#include <stddef.h> /* size_t */
|
||||
#include <ctype.h>
|
||||
|
||||
#include "strnatcmp.h"
|
||||
@@ -39,28 +38,21 @@
|
||||
/* These are defined as macros to make it easier to adapt this code to
|
||||
* different characters types or comparison functions. */
|
||||
static inline int
|
||||
nat_isdigit(nat_char a)
|
||||
nat_isdigit(const char *a)
|
||||
{
|
||||
return isdigit((unsigned char) a);
|
||||
return isdigit((unsigned char) *a);
|
||||
}
|
||||
|
||||
|
||||
static inline int
|
||||
nat_isspace(nat_char a)
|
||||
nat_isspace(const char *a)
|
||||
{
|
||||
return isspace((unsigned char) a);
|
||||
}
|
||||
|
||||
|
||||
static inline nat_char
|
||||
nat_toupper(nat_char a)
|
||||
{
|
||||
return toupper((unsigned char) a);
|
||||
return isspace((unsigned char) *a) || *a == '0' && nat_isdigit(a + 1);
|
||||
}
|
||||
|
||||
|
||||
static int
|
||||
compare_right(nat_char const *a, nat_char const *b)
|
||||
compare_right(const char *a, const char *b)
|
||||
{
|
||||
int bias = 0;
|
||||
|
||||
@@ -69,11 +61,11 @@ compare_right(nat_char const *a, nat_char const *b)
|
||||
both numbers to know that they have the same magnitude, so we
|
||||
remember it in BIAS. */
|
||||
for (;; a++, b++) {
|
||||
if (!nat_isdigit(*a) && !nat_isdigit(*b))
|
||||
if (!nat_isdigit(a) && !nat_isdigit(b))
|
||||
return bias;
|
||||
if (!nat_isdigit(*a))
|
||||
if (!nat_isdigit(a))
|
||||
return -1;
|
||||
if (!nat_isdigit(*b))
|
||||
if (!nat_isdigit(b))
|
||||
return +1;
|
||||
if (*a < *b) {
|
||||
if (!bias)
|
||||
@@ -87,60 +79,38 @@ compare_right(nat_char const *a, nat_char const *b)
|
||||
}
|
||||
|
||||
|
||||
static int
|
||||
strnatcmp0(nat_char const *a, nat_char const *b, int fold_case)
|
||||
int
|
||||
strnatcmp(const char *a, const char *b)
|
||||
{
|
||||
int ai, bi;
|
||||
nat_char ca, cb;
|
||||
int result;
|
||||
int result;
|
||||
|
||||
ai = bi = 0;
|
||||
while (1) {
|
||||
ca = a[ai]; cb = b[bi];
|
||||
while (1) {
|
||||
/* skip over leading spaces or zeros */
|
||||
while (nat_isspace(a))
|
||||
a++;
|
||||
|
||||
/* skip over leading spaces or zeros */
|
||||
while (nat_isspace(ca) || ca == '0')
|
||||
ca = a[++ai];
|
||||
|
||||
while (nat_isspace(cb) || cb == '0')
|
||||
cb = b[++bi];
|
||||
while (nat_isspace(b))
|
||||
b++;
|
||||
|
||||
/* process run of digits */
|
||||
if (nat_isdigit(ca) && nat_isdigit(cb)) {
|
||||
if ((result = compare_right(a+ai, b+bi)) != 0)
|
||||
if (nat_isdigit(a) && nat_isdigit(b)) {
|
||||
if ((result = compare_right(a, b)) != 0)
|
||||
return result;
|
||||
}
|
||||
|
||||
if (!ca && !cb) {
|
||||
if (!*a && !*b) {
|
||||
/* The strings compare the same. Perhaps the caller
|
||||
will want to call strcmp to break the tie. */
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (fold_case) {
|
||||
ca = nat_toupper(ca);
|
||||
cb = nat_toupper(cb);
|
||||
}
|
||||
|
||||
if (ca < cb)
|
||||
if (*a < *b)
|
||||
return -1;
|
||||
|
||||
if (ca > cb)
|
||||
if (*a > *b)
|
||||
return +1;
|
||||
|
||||
++ai; ++bi;
|
||||
a++; b++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
strnatcmp(nat_char const *a, nat_char const *b) {
|
||||
return strnatcmp0(a, b, 0);
|
||||
}
|
||||
|
||||
|
||||
/* Compare, recognizing numeric string and ignoring case. */
|
||||
int
|
||||
strnatcasecmp(nat_char const *a, nat_char const *b) {
|
||||
return strnatcmp0(a, b, 1);
|
||||
}
|
||||
@@ -24,14 +24,7 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* CUSTOMIZATION SECTION
|
||||
*
|
||||
* You can change this typedef, but must then also change the inline
|
||||
* functions in strnatcmp.c */
|
||||
typedef char nat_char;
|
||||
|
||||
int strnatcmp(nat_char const *a, nat_char const *b);
|
||||
int strnatcasecmp(nat_char const *a, nat_char const *b);
|
||||
int strnatcmp(const char *a, const char *b);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
-460
@@ -1,460 +0,0 @@
|
||||
/*
|
||||
* Copyright 2022 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package androidx.compose.foundation.gestures
|
||||
|
||||
import androidx.compose.animation.core.AnimationSpec
|
||||
import androidx.compose.animation.core.DecayAnimationSpec
|
||||
import androidx.compose.animation.core.FloatDecayAnimationSpec
|
||||
import androidx.compose.animation.core.generateDecayAnimationSpec
|
||||
import androidx.compose.foundation.ExperimentalFoundationApi
|
||||
import androidx.compose.foundation.gestures.HorizontalDragEvent.DragDelta
|
||||
import androidx.compose.foundation.gestures.snapping.SnapLayoutInfoProvider
|
||||
import androidx.compose.foundation.gestures.snapping.snapFlingBehavior
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.geometry.Offset
|
||||
import androidx.compose.ui.input.pointer.PointerInputChange
|
||||
import androidx.compose.ui.input.pointer.positionChange
|
||||
import androidx.compose.ui.layout.Measurable
|
||||
import androidx.compose.ui.layout.MeasureResult
|
||||
import androidx.compose.ui.layout.MeasureScope
|
||||
import androidx.compose.ui.node.LayoutModifierNode
|
||||
import androidx.compose.ui.node.ModifierNodeElement
|
||||
import androidx.compose.ui.node.ObserverModifierNode
|
||||
import androidx.compose.ui.node.currentValueOf
|
||||
import androidx.compose.ui.node.observeReads
|
||||
import androidx.compose.ui.node.requireLayoutDirection
|
||||
import androidx.compose.ui.platform.InspectorInfo
|
||||
import androidx.compose.ui.platform.LocalDensity
|
||||
import androidx.compose.ui.platform.debugInspectorInfo
|
||||
import androidx.compose.ui.unit.Constraints
|
||||
import androidx.compose.ui.unit.Density
|
||||
import androidx.compose.ui.unit.IntSize
|
||||
import androidx.compose.ui.unit.LayoutDirection
|
||||
import androidx.compose.ui.unit.Velocity
|
||||
import androidx.compose.ui.unit.dp
|
||||
import kotlin.math.abs
|
||||
import kotlin.math.absoluteValue
|
||||
import kotlin.math.roundToInt
|
||||
import kotlin.math.sign
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@ExperimentalFoundationApi
|
||||
fun <T> Modifier.anchoredHorizontalDraggable(
|
||||
state: AnchoredDraggableState<T>,
|
||||
enableDragFromStartToEnd: Boolean = true,
|
||||
enableDragFromEndToStart: Boolean = true,
|
||||
enabled: Boolean = true,
|
||||
startDragImmediately: Boolean = state.isAnimationRunning,
|
||||
flingBehavior: FlingBehavior? = null,
|
||||
): Modifier = this then AnchoredHorizontalDraggableElement(
|
||||
state = state,
|
||||
canDrag = { (x, y) ->
|
||||
val enableDrag = if (x > 0) enableDragFromStartToEnd else enableDragFromEndToStart
|
||||
enableDrag && x.absoluteValue > y.absoluteValue * 2f
|
||||
},
|
||||
enabled = enabled,
|
||||
startDragImmediately = startDragImmediately,
|
||||
flingBehavior = flingBehavior,
|
||||
)
|
||||
|
||||
private class AnchoredHorizontalDraggableElement<T>(
|
||||
private val state: AnchoredDraggableState<T>,
|
||||
private val canDrag: (Offset) -> Boolean,
|
||||
private val enabled: Boolean,
|
||||
private val startDragImmediately: Boolean,
|
||||
private val flingBehavior: FlingBehavior?,
|
||||
) : ModifierNodeElement<AnchoredHorizontalDraggableNode<T>>() {
|
||||
override fun create() = AnchoredHorizontalDraggableNode(
|
||||
state,
|
||||
canDrag,
|
||||
enabled,
|
||||
startDragImmediately,
|
||||
flingBehavior,
|
||||
)
|
||||
|
||||
override fun update(node: AnchoredHorizontalDraggableNode<T>) {
|
||||
node.update(
|
||||
state,
|
||||
canDrag,
|
||||
enabled,
|
||||
startDragImmediately,
|
||||
flingBehavior,
|
||||
)
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
var result = state.hashCode()
|
||||
result = 31 * result + canDrag.hashCode()
|
||||
result = 31 * result + enabled.hashCode()
|
||||
result = 31 * result + startDragImmediately.hashCode()
|
||||
result = 31 * result + flingBehavior.hashCode()
|
||||
return result
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
|
||||
if (other !is AnchoredHorizontalDraggableElement<*>) return false
|
||||
|
||||
if (state != other.state) return false
|
||||
if (canDrag != other.canDrag) return false
|
||||
if (enabled != other.enabled) return false
|
||||
if (startDragImmediately != other.startDragImmediately) return false
|
||||
if (flingBehavior != other.flingBehavior) return false
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
override fun InspectorInfo.inspectableProperties() {
|
||||
name = "anchoredDraggable"
|
||||
properties["state"] = state
|
||||
properties["canDrag"] = canDrag
|
||||
properties["enabled"] = enabled
|
||||
properties["startDragImmediately"] = startDragImmediately
|
||||
properties["flingBehavior"] = flingBehavior
|
||||
}
|
||||
}
|
||||
|
||||
private class AnchoredHorizontalDraggableNode<T>(
|
||||
private var state: AnchoredDraggableState<T>,
|
||||
private var canDrag: (Offset) -> Boolean,
|
||||
enabled: Boolean,
|
||||
private var startDragImmediately: Boolean,
|
||||
private var flingBehavior: FlingBehavior?,
|
||||
) : HorizontalDragGestureNode(enabled = enabled),
|
||||
ObserverModifierNode {
|
||||
|
||||
lateinit var resolvedFlingBehavior: FlingBehavior
|
||||
lateinit var density: Density
|
||||
|
||||
private val isReverseDirection: Boolean
|
||||
get() = requireLayoutDirection() == LayoutDirection.Rtl
|
||||
|
||||
override fun onAttach() {
|
||||
updateFlingBehavior(flingBehavior)
|
||||
}
|
||||
|
||||
override fun onObservedReadsChanged() {
|
||||
val newDensity = currentValueOf(LocalDensity)
|
||||
if (density != newDensity) {
|
||||
density = newDensity
|
||||
updateFlingBehavior(flingBehavior)
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateFlingBehavior(newFlingBehavior: FlingBehavior?) {
|
||||
// Fall back to default fling behavior if the new fling behavior is null
|
||||
this.resolvedFlingBehavior =
|
||||
if (newFlingBehavior == null) {
|
||||
// Only register for LocalDensity snapshot updates if we are creating a decay
|
||||
observeReads { density = currentValueOf(LocalDensity) }
|
||||
anchoredDraggableFlingBehavior(
|
||||
snapAnimationSpec = AnchoredDraggableDefaults.SnapAnimationSpec,
|
||||
positionalThreshold = AnchoredDraggableDefaults.PositionalThreshold,
|
||||
density = density,
|
||||
state = state,
|
||||
)
|
||||
} else {
|
||||
newFlingBehavior
|
||||
}
|
||||
}
|
||||
|
||||
override fun canDrag(change: PointerInputChange): Boolean = canDrag(change.positionChange().reverseIfNeeded())
|
||||
|
||||
override suspend fun drag(forEachDelta: suspend ((dragDelta: DragDelta) -> Unit) -> Unit) {
|
||||
state.anchoredDrag {
|
||||
forEachDelta { dragDelta ->
|
||||
dragTo(state.newOffsetForDelta(dragDelta.delta.reverseIfNeeded().x))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDragStarted(startedPosition: Offset) { }
|
||||
|
||||
override fun onDragStopped(velocity: Velocity) {
|
||||
if (!isAttached) return
|
||||
coroutineScope.launch {
|
||||
fling(velocity.reverseIfNeeded().x)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun fling(velocity: Float): Float {
|
||||
var leftoverVelocity = velocity
|
||||
state.anchoredDrag {
|
||||
val scrollScope =
|
||||
object : ScrollScope {
|
||||
override fun scrollBy(pixels: Float): Float {
|
||||
val newOffset = state.newOffsetForDelta(pixels)
|
||||
val consumed = newOffset - state.offset
|
||||
dragTo(newOffset)
|
||||
return consumed
|
||||
}
|
||||
}
|
||||
with(resolvedFlingBehavior) {
|
||||
leftoverVelocity = scrollScope.performFling(velocity)
|
||||
}
|
||||
}
|
||||
return leftoverVelocity
|
||||
}
|
||||
|
||||
override fun startDragImmediately(): Boolean = startDragImmediately
|
||||
|
||||
fun update(
|
||||
state: AnchoredDraggableState<T>,
|
||||
canDrag: (Offset) -> Boolean,
|
||||
enabled: Boolean,
|
||||
startDragImmediately: Boolean,
|
||||
flingBehavior: FlingBehavior?,
|
||||
) {
|
||||
this.flingBehavior = flingBehavior
|
||||
|
||||
var resetPointerInputHandling = false
|
||||
|
||||
if (this.state != state) {
|
||||
this.state = state
|
||||
updateFlingBehavior(flingBehavior)
|
||||
resetPointerInputHandling = true
|
||||
}
|
||||
|
||||
this.canDrag = canDrag
|
||||
this.startDragImmediately = startDragImmediately
|
||||
|
||||
update(
|
||||
enabled = enabled,
|
||||
shouldResetPointerInputHandling = resetPointerInputHandling,
|
||||
)
|
||||
}
|
||||
|
||||
private fun Velocity.reverseIfNeeded() = if (isReverseDirection) this * -1f else this * 1f
|
||||
private fun Offset.reverseIfNeeded() = if (isReverseDirection) this * -1f else this * 1f
|
||||
}
|
||||
|
||||
internal fun <T> AnchoredDraggableState<T>.newOffsetForDelta(delta: Float) =
|
||||
((if (offset.isNaN()) 0f else offset) + delta)
|
||||
.coerceIn(anchors.minPosition(), anchors.maxPosition())
|
||||
|
||||
/**
|
||||
* Compute the target anchor based on the [currentOffset], [velocity] and [positionalThreshold] and
|
||||
* [velocityThreshold].
|
||||
*
|
||||
* @return The suggested target anchor
|
||||
*/
|
||||
@ExperimentalFoundationApi
|
||||
private fun <T> DraggableAnchors<T>.computeTarget(
|
||||
currentOffset: Float,
|
||||
currentValue: T,
|
||||
velocity: Float,
|
||||
positionalThreshold: (totalDistance: Float) -> Float,
|
||||
velocityThreshold: () -> Float,
|
||||
): T {
|
||||
val currentAnchors = this
|
||||
val currentAnchorPosition = currentAnchors.positionOf(currentValue)
|
||||
val velocityThresholdPx = velocityThreshold()
|
||||
return if (currentAnchorPosition == currentOffset || currentAnchorPosition.isNaN()) {
|
||||
currentValue
|
||||
} else {
|
||||
if (abs(velocity) >= abs(velocityThresholdPx)) {
|
||||
currentAnchors.closestAnchor(currentOffset, sign(velocity) > 0)!!
|
||||
} else {
|
||||
val neighborAnchor =
|
||||
currentAnchors.closestAnchor(
|
||||
currentOffset,
|
||||
currentOffset - currentAnchorPosition > 0,
|
||||
)!!
|
||||
val neighborAnchorPosition = currentAnchors.positionOf(neighborAnchor)
|
||||
val distance = abs(currentAnchorPosition - neighborAnchorPosition)
|
||||
val relativeThreshold = abs(positionalThreshold(distance))
|
||||
val relativePosition = abs(currentAnchorPosition - currentOffset)
|
||||
if (relativePosition <= relativeThreshold) currentValue else neighborAnchor
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a [FlingBehavior] for use with [Modifier.anchoredDraggable].
|
||||
*
|
||||
* @param state The [AnchoredDraggableState] that will be used for the fling animation
|
||||
* @param positionalThreshold A positional threshold that needs to be crossed in order to reach the
|
||||
* next anchor when flinging, in pixels. This can be a derived from the distance that the lambda
|
||||
* is invoked with.
|
||||
* @param snapAnimationSpec The animation spec that will be used to snap to a new state.
|
||||
*/
|
||||
@ExperimentalFoundationApi
|
||||
internal fun <T> anchoredDraggableFlingBehavior(
|
||||
state: AnchoredDraggableState<T>,
|
||||
density: Density,
|
||||
positionalThreshold: (totalDistance: Float) -> Float,
|
||||
snapAnimationSpec: AnimationSpec<Float>,
|
||||
): TargetedFlingBehavior =
|
||||
snapFlingBehavior(
|
||||
decayAnimationSpec = NoOpDecayAnimationSpec,
|
||||
snapAnimationSpec = snapAnimationSpec,
|
||||
snapLayoutInfoProvider =
|
||||
AnchoredDraggableLayoutInfoProvider(
|
||||
state = state,
|
||||
positionalThreshold = positionalThreshold,
|
||||
velocityThreshold = { with(density) { 125.dp.toPx() } },
|
||||
),
|
||||
)
|
||||
|
||||
@Suppress("FunctionName")
|
||||
@OptIn(ExperimentalFoundationApi::class)
|
||||
private fun <T> AnchoredDraggableLayoutInfoProvider(
|
||||
state: AnchoredDraggableState<T>,
|
||||
positionalThreshold: (totalDistance: Float) -> Float,
|
||||
velocityThreshold: () -> Float,
|
||||
): SnapLayoutInfoProvider =
|
||||
object : SnapLayoutInfoProvider {
|
||||
|
||||
// We never decay in AnchoredDraggable's fling
|
||||
override fun calculateApproachOffset(velocity: Float, decayOffset: Float) = 0f
|
||||
|
||||
override fun calculateSnapOffset(velocity: Float): Float {
|
||||
val currentOffset = state.requireOffset()
|
||||
val target =
|
||||
state.anchors.computeTarget(
|
||||
currentOffset = currentOffset,
|
||||
currentValue = state.currentValue,
|
||||
velocity = velocity,
|
||||
positionalThreshold = positionalThreshold,
|
||||
velocityThreshold = velocityThreshold,
|
||||
)
|
||||
return state.anchors.positionOf(target) - currentOffset
|
||||
}
|
||||
}
|
||||
|
||||
private val NoOpDecayAnimationSpec: DecayAnimationSpec<Float> =
|
||||
object : FloatDecayAnimationSpec {
|
||||
override val absVelocityThreshold = 0f
|
||||
|
||||
override fun getValueFromNanos(
|
||||
playTimeNanos: Long,
|
||||
initialValue: Float,
|
||||
initialVelocity: Float,
|
||||
) = 0f
|
||||
|
||||
override fun getDurationNanos(initialValue: Float, initialVelocity: Float) = 0L
|
||||
|
||||
override fun getVelocityFromNanos(
|
||||
playTimeNanos: Long,
|
||||
initialValue: Float,
|
||||
initialVelocity: Float,
|
||||
) = 0f
|
||||
|
||||
override fun getTargetValue(initialValue: Float, initialVelocity: Float) = 0f
|
||||
}.generateDecayAnimationSpec()
|
||||
|
||||
/**
|
||||
* This Modifier allows configuring an [AnchoredDraggableState]'s anchors based on this layout
|
||||
* node's size and offsetting it.
|
||||
* It considers lookahead and reports the appropriate size and measurement for the appropriate
|
||||
* phase.
|
||||
*
|
||||
* @param state The state the anchors should be attached to
|
||||
* @param orientation The orientation the component should be offset in
|
||||
* @param anchors Lambda to calculate the anchors based on this layout's size and the incoming
|
||||
* constraints. These can be useful to avoid subcomposition.
|
||||
*/
|
||||
internal fun <T> Modifier.draggableAnchors(
|
||||
state: AnchoredDraggableState<T>,
|
||||
orientation: Orientation,
|
||||
anchors: (size: IntSize, constraints: Constraints) -> Pair<DraggableAnchors<T>, T>,
|
||||
) = this then DraggableAnchorsElement(state, anchors, orientation)
|
||||
|
||||
private class DraggableAnchorsElement<T>(
|
||||
private val state: AnchoredDraggableState<T>,
|
||||
private val anchors: (size: IntSize, constraints: Constraints) -> Pair<DraggableAnchors<T>, T>,
|
||||
private val orientation: Orientation,
|
||||
) : ModifierNodeElement<DraggableAnchorsNode<T>>() {
|
||||
|
||||
override fun create() = DraggableAnchorsNode(state, anchors, orientation)
|
||||
|
||||
override fun update(node: DraggableAnchorsNode<T>) {
|
||||
node.state = state
|
||||
node.anchors = anchors
|
||||
node.orientation = orientation
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
|
||||
if (other !is DraggableAnchorsElement<*>) return false
|
||||
|
||||
if (state != other.state) return false
|
||||
if (anchors !== other.anchors) return false
|
||||
if (orientation != other.orientation) return false
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
var result = state.hashCode()
|
||||
result = 31 * result + anchors.hashCode()
|
||||
result = 31 * result + orientation.hashCode()
|
||||
return result
|
||||
}
|
||||
|
||||
override fun InspectorInfo.inspectableProperties() {
|
||||
debugInspectorInfo {
|
||||
properties["state"] = state
|
||||
properties["anchors"] = anchors
|
||||
properties["orientation"] = orientation
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class DraggableAnchorsNode<T>(
|
||||
var state: AnchoredDraggableState<T>,
|
||||
var anchors: (size: IntSize, constraints: Constraints) -> Pair<DraggableAnchors<T>, T>,
|
||||
var orientation: Orientation,
|
||||
) : Modifier.Node(),
|
||||
LayoutModifierNode {
|
||||
private var didLookahead: Boolean = false
|
||||
|
||||
override fun onDetach() {
|
||||
didLookahead = false
|
||||
}
|
||||
|
||||
override fun MeasureScope.measure(
|
||||
measurable: Measurable,
|
||||
constraints: Constraints,
|
||||
): MeasureResult {
|
||||
val placeable = measurable.measure(constraints)
|
||||
// If we are in a lookahead pass, we only want to update the anchors here and not in
|
||||
// post-lookahead. If there is no lookahead happening (!isLookingAhead && !didLookahead),
|
||||
// update the anchors in the main pass.
|
||||
if (!isLookingAhead || !didLookahead) {
|
||||
val size = IntSize(placeable.width, placeable.height)
|
||||
val newAnchorResult = anchors(size, constraints)
|
||||
state.updateAnchors(newAnchorResult.first, newAnchorResult.second)
|
||||
}
|
||||
didLookahead = isLookingAhead || didLookahead
|
||||
return layout(placeable.width, placeable.height) {
|
||||
// In a lookahead pass, we use the position of the current target as this is where any
|
||||
// ongoing animations would move. If the component is in a settled state, lookahead
|
||||
// and post-lookahead will converge.
|
||||
val offset = if (isLookingAhead) {
|
||||
state.anchors.positionOf(state.targetValue)
|
||||
} else {
|
||||
state.requireOffset()
|
||||
}
|
||||
val xOffset = if (orientation == Orientation.Horizontal) offset else 0f
|
||||
val yOffset = if (orientation == Orientation.Vertical) offset else 0f
|
||||
placeable.placeRelative(xOffset.roundToInt(), yOffset.roundToInt())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,103 +0,0 @@
|
||||
@file:Suppress("ktlint:standard:property-naming")
|
||||
|
||||
package androidx.compose.material3
|
||||
|
||||
import androidx.compose.foundation.interaction.MutableInteractionSource
|
||||
import androidx.compose.foundation.interaction.collectIsFocusedAsState
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.offset
|
||||
import androidx.compose.foundation.layout.sizeIn
|
||||
import androidx.compose.foundation.text.BasicTextField
|
||||
import androidx.compose.foundation.text.KeyboardOptions
|
||||
import androidx.compose.foundation.text.input.TextFieldLineLimits
|
||||
import androidx.compose.foundation.text.input.TextFieldState
|
||||
import androidx.compose.material3.SearchBarDefaults.InputFieldHeight
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.focus.FocusRequester
|
||||
import androidx.compose.ui.focus.focusRequester
|
||||
import androidx.compose.ui.focus.onFocusChanged
|
||||
import androidx.compose.ui.graphics.SolidColor
|
||||
import androidx.compose.ui.graphics.takeOrElse
|
||||
import androidx.compose.ui.platform.LocalFocusManager
|
||||
import androidx.compose.ui.text.TextStyle
|
||||
import androidx.compose.ui.text.input.ImeAction
|
||||
import androidx.compose.ui.text.input.VisualTransformation
|
||||
import androidx.compose.ui.unit.dp
|
||||
import kotlinx.coroutines.delay
|
||||
|
||||
@Composable
|
||||
fun SearchBarInputField(
|
||||
state: TextFieldState,
|
||||
onSearch: () -> Unit,
|
||||
expanded: Boolean,
|
||||
onExpandedChange: (Boolean) -> Unit,
|
||||
modifier: Modifier = Modifier,
|
||||
enabled: Boolean = true,
|
||||
label: @Composable (() -> Unit)? = null,
|
||||
placeholder: @Composable (() -> Unit)? = null,
|
||||
leadingIcon: @Composable (() -> Unit)? = null,
|
||||
trailingIcon: @Composable (() -> Unit)? = null,
|
||||
colors: TextFieldColors = SearchBarDefaults.inputFieldColors(),
|
||||
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
|
||||
) {
|
||||
val focusRequester = remember { FocusRequester() }
|
||||
val focusManager = LocalFocusManager.current
|
||||
val focused by interactionSource.collectIsFocusedAsState()
|
||||
val textColor = LocalTextStyle.current.color.takeOrElse {
|
||||
if (focused) colors.focusedTextColor else colors.unfocusedTextColor
|
||||
}
|
||||
|
||||
BasicTextField(
|
||||
state = state,
|
||||
modifier = modifier.sizeIn(
|
||||
minWidth = SearchBarMinWidth,
|
||||
maxWidth = SearchBarMaxWidth,
|
||||
minHeight = InputFieldHeight,
|
||||
).focusRequester(focusRequester).onFocusChanged {
|
||||
if (it.isFocused) onExpandedChange(true)
|
||||
}.fillMaxWidth(),
|
||||
enabled = enabled,
|
||||
lineLimits = TextFieldLineLimits.SingleLine,
|
||||
textStyle = LocalTextStyle.current.merge(TextStyle(color = textColor)),
|
||||
cursorBrush = SolidColor(colors.cursorColor),
|
||||
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Search),
|
||||
onKeyboardAction = { onSearch() },
|
||||
interactionSource = interactionSource,
|
||||
decorator = @Composable { innerTextField ->
|
||||
TextFieldDefaults.DecorationBox(
|
||||
value = state.text.toString(),
|
||||
innerTextField = innerTextField,
|
||||
enabled = enabled,
|
||||
singleLine = true,
|
||||
visualTransformation = VisualTransformation.None,
|
||||
interactionSource = interactionSource,
|
||||
placeholder = placeholder,
|
||||
leadingIcon = leadingIcon?.let { leading -> { Box(Modifier.offset(x = SearchBarIconOffsetX)) { leading() } } },
|
||||
trailingIcon = trailingIcon?.let { trailing -> { Box(Modifier.offset(x = -SearchBarIconOffsetX)) { trailing() } } },
|
||||
shape = SearchBarDefaults.inputFieldShape,
|
||||
colors = colors,
|
||||
container = {},
|
||||
)
|
||||
},
|
||||
)
|
||||
|
||||
val shouldClearFocus = !expanded && focused
|
||||
LaunchedEffect(expanded) {
|
||||
if (shouldClearFocus) {
|
||||
// Not strictly needed according to the motion spec, but since the animation
|
||||
// already has a delay, this works around b/261632544.
|
||||
delay(AnimationDelayMillis.toLong())
|
||||
focusManager.clearFocus()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val SearchBarIconOffsetX = 4.dp
|
||||
val SearchBarMinWidth = 360.dp
|
||||
val SearchBarMaxWidth = 720.dp
|
||||
const val AnimationDelayMillis = 100
|
||||
@@ -1,163 +0,0 @@
|
||||
/*
|
||||
* Copyright 2022 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.google.accompanist.systemuicontroller
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.content.ContextWrapper
|
||||
import android.view.View
|
||||
import android.view.Window
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.Stable
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.platform.LocalView
|
||||
import androidx.compose.ui.window.DialogWindowProvider
|
||||
import androidx.core.view.ViewCompat
|
||||
import androidx.core.view.WindowCompat
|
||||
import androidx.core.view.WindowInsetsCompat
|
||||
import androidx.core.view.WindowInsetsControllerCompat
|
||||
|
||||
/**
|
||||
* A class which provides easy-to-use utilities for updating the System UI bar
|
||||
* colors within Jetpack Compose.
|
||||
*/
|
||||
@Stable
|
||||
interface SystemUiController {
|
||||
|
||||
/**
|
||||
* Control for the behavior of the system bars. This value should be one of the
|
||||
* [WindowInsetsControllerCompat] behavior constants:
|
||||
* [WindowInsetsControllerCompat.BEHAVIOR_SHOW_BARS_BY_TOUCH] (Deprecated),
|
||||
* [WindowInsetsControllerCompat.BEHAVIOR_DEFAULT] and
|
||||
* [WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE].
|
||||
*/
|
||||
var systemBarsBehavior: Int
|
||||
|
||||
/**
|
||||
* Property which holds the status bar visibility. If set to true, show the status bar,
|
||||
* otherwise hide the status bar.
|
||||
*/
|
||||
var isStatusBarVisible: Boolean
|
||||
|
||||
/**
|
||||
* Property which holds the navigation bar visibility. If set to true, show the navigation bar,
|
||||
* otherwise hide the navigation bar.
|
||||
*/
|
||||
var isNavigationBarVisible: Boolean
|
||||
|
||||
/**
|
||||
* Property which holds the status & navigation bar visibility. If set to true, show both bars,
|
||||
* otherwise hide both bars.
|
||||
*/
|
||||
var isSystemBarsVisible: Boolean
|
||||
get() = isNavigationBarVisible && isStatusBarVisible
|
||||
set(value) {
|
||||
isStatusBarVisible = value
|
||||
isNavigationBarVisible = value
|
||||
}
|
||||
|
||||
/**
|
||||
* Property which holds whether the status bar icons + content are 'dark' or not.
|
||||
*/
|
||||
var statusBarDarkContentEnabled: Boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* Remembers a [SystemUiController] for the given [window].
|
||||
*
|
||||
* If no [window] is provided, an attempt to find the correct [Window] is made.
|
||||
*
|
||||
* First, if the [LocalView]'s parent is a [DialogWindowProvider], then that dialog's [Window] will
|
||||
* be used.
|
||||
*
|
||||
* Second, we attempt to find [Window] for the [Activity] containing the [LocalView].
|
||||
*
|
||||
* If none of these are found (such as may happen in a preview), then the functionality of the
|
||||
* returned [SystemUiController] will be degraded, but won't throw an exception.
|
||||
*/
|
||||
@Composable
|
||||
fun rememberSystemUiController(
|
||||
window: Window? = findWindow(),
|
||||
): SystemUiController {
|
||||
val view = LocalView.current
|
||||
return remember(view, window) { AndroidSystemUiController(view, window) }
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun findWindow(): Window? =
|
||||
(LocalView.current.parent as? DialogWindowProvider)?.window
|
||||
?: LocalView.current.context.findWindow()
|
||||
|
||||
private tailrec fun Context.findWindow(): Window? =
|
||||
when (this) {
|
||||
is Activity -> window
|
||||
is ContextWrapper -> baseContext.findWindow()
|
||||
else -> null
|
||||
}
|
||||
|
||||
/**
|
||||
* A helper class for setting the navigation and status bar colors for a [View], gracefully
|
||||
* degrading behavior based upon API level.
|
||||
*
|
||||
* Typically you would use [rememberSystemUiController] to remember an instance of this.
|
||||
*/
|
||||
internal class AndroidSystemUiController(
|
||||
private val view: View,
|
||||
window: Window?,
|
||||
) : SystemUiController {
|
||||
private val windowInsetsController = window?.let {
|
||||
WindowCompat.getInsetsController(it, view)
|
||||
}
|
||||
|
||||
override var systemBarsBehavior: Int
|
||||
get() = windowInsetsController?.systemBarsBehavior ?: 0
|
||||
set(value) {
|
||||
windowInsetsController?.systemBarsBehavior = value
|
||||
}
|
||||
|
||||
override var isStatusBarVisible: Boolean
|
||||
get() {
|
||||
return ViewCompat.getRootWindowInsets(view)
|
||||
?.isVisible(WindowInsetsCompat.Type.statusBars()) == true
|
||||
}
|
||||
set(value) {
|
||||
if (value) {
|
||||
windowInsetsController?.show(WindowInsetsCompat.Type.statusBars())
|
||||
} else {
|
||||
windowInsetsController?.hide(WindowInsetsCompat.Type.statusBars())
|
||||
}
|
||||
}
|
||||
|
||||
override var isNavigationBarVisible: Boolean
|
||||
get() {
|
||||
return ViewCompat.getRootWindowInsets(view)
|
||||
?.isVisible(WindowInsetsCompat.Type.navigationBars()) == true
|
||||
}
|
||||
set(value) {
|
||||
if (value) {
|
||||
windowInsetsController?.show(WindowInsetsCompat.Type.navigationBars())
|
||||
} else {
|
||||
windowInsetsController?.hide(WindowInsetsCompat.Type.navigationBars())
|
||||
}
|
||||
}
|
||||
|
||||
override var statusBarDarkContentEnabled: Boolean
|
||||
get() = windowInsetsController?.isAppearanceLightStatusBars == true
|
||||
set(value) {
|
||||
windowInsetsController?.isAppearanceLightStatusBars = value
|
||||
}
|
||||
}
|
||||
@@ -635,8 +635,7 @@ data class WebViewError(
|
||||
fun rememberWebViewState(
|
||||
url: String,
|
||||
additionalHttpHeaders: Map<String, String> = emptyMap(),
|
||||
): WebViewState =
|
||||
// Rather than using .apply {} here we will recreate the state, this prevents
|
||||
): WebViewState = // Rather than using .apply {} here we will recreate the state, this prevents
|
||||
// a recomposition loop when the webview updates the url itself.
|
||||
remember {
|
||||
WebViewState(
|
||||
|
||||
@@ -21,7 +21,8 @@ import android.content.Context
|
||||
import android.os.StrictMode
|
||||
import androidx.appcompat.app.AppCompatDelegate
|
||||
import androidx.appcompat.content.res.AppCompatResources
|
||||
import androidx.collection.LruCache
|
||||
import androidx.compose.runtime.Composer
|
||||
import androidx.compose.runtime.tooling.ComposeStackTraceMode
|
||||
import androidx.lifecycle.ProcessLifecycleOwner
|
||||
import androidx.lifecycle.coroutineScope
|
||||
import coil3.EventListener
|
||||
@@ -29,15 +30,26 @@ import coil3.SingletonImageLoader
|
||||
import coil3.asImage
|
||||
import coil3.gif.AnimatedImageDecoder
|
||||
import coil3.gif.GifDecoder
|
||||
import coil3.network.ConnectivityChecker
|
||||
import coil3.network.ktor3.KtorNetworkFetcherFactory
|
||||
import coil3.request.ErrorResult
|
||||
import coil3.request.ImageRequest
|
||||
import coil3.request.allowRgb565
|
||||
import coil3.request.crossfade
|
||||
import coil3.serviceLoaderEnabled
|
||||
import coil3.util.DebugLogger
|
||||
import com.hippo.ehviewer.client.EhCookieStore
|
||||
import com.ehviewer.core.database.SearchDatabase
|
||||
import com.ehviewer.core.files.deleteContent
|
||||
import com.ehviewer.core.ui.util.initSETConnection
|
||||
import com.ehviewer.core.util.isAtLeastO
|
||||
import com.ehviewer.core.util.isAtLeastP
|
||||
import com.ehviewer.core.util.isAtLeastS
|
||||
import com.ehviewer.core.util.isAtLeastSExtension7
|
||||
import com.ehviewer.core.util.launchIO
|
||||
import com.ehviewer.core.util.logcat
|
||||
import com.ehviewer.core.util.withUIContext
|
||||
import com.hippo.ehviewer.client.EhTagDatabase
|
||||
import com.hippo.ehviewer.client.data.GalleryDetail
|
||||
import com.hippo.ehviewer.coil.AnimatedWebPDecoder
|
||||
import com.hippo.ehviewer.coil.CropBorderInterceptor
|
||||
import com.hippo.ehviewer.coil.DetectBorderInterceptor
|
||||
import com.hippo.ehviewer.coil.DownloadThumbInterceptor
|
||||
@@ -45,31 +57,25 @@ import com.hippo.ehviewer.coil.HardwareBitmapInterceptor
|
||||
import com.hippo.ehviewer.coil.MapExtraInfoInterceptor
|
||||
import com.hippo.ehviewer.coil.MergeInterceptor
|
||||
import com.hippo.ehviewer.coil.QrCodeInterceptor
|
||||
import com.hippo.ehviewer.cronet.cronetHttpClient
|
||||
import com.hippo.ehviewer.dailycheck.checkDawn
|
||||
import com.hippo.ehviewer.dao.SearchDatabase
|
||||
import com.hippo.ehviewer.download.DownloadManager
|
||||
import com.hippo.ehviewer.download.DownloadsFilterMode
|
||||
import com.hippo.ehviewer.ktbuilder.diskCache
|
||||
import com.hippo.ehviewer.ktbuilder.imageLoader
|
||||
import com.hippo.ehviewer.ktor.Cronet
|
||||
import com.hippo.ehviewer.legacy.cleanObsoleteCache
|
||||
import com.hippo.ehviewer.ktor.configureClient
|
||||
import com.hippo.ehviewer.ktor.configureCommon
|
||||
import com.hippo.ehviewer.ui.keepNoMediaFileStatus
|
||||
import com.hippo.ehviewer.ui.lockObserver
|
||||
import com.hippo.ehviewer.ui.screen.detailCache
|
||||
import com.hippo.ehviewer.ui.tools.dataStateFlow
|
||||
import com.hippo.ehviewer.ui.tools.initSETConnection
|
||||
import com.hippo.ehviewer.util.AppConfig
|
||||
import com.hippo.ehviewer.util.Crash
|
||||
import com.hippo.ehviewer.util.CrashHandler
|
||||
import com.hippo.ehviewer.util.FavouriteStatusRouter
|
||||
import com.hippo.ehviewer.util.FileUtils
|
||||
import com.hippo.ehviewer.util.isAtLeastO
|
||||
import com.hippo.ehviewer.util.isAtLeastP
|
||||
import com.hippo.ehviewer.util.isAtLeastS
|
||||
import eu.kanade.tachiyomi.util.lang.launchIO
|
||||
import eu.kanade.tachiyomi.util.lang.withUIContext
|
||||
import eu.kanade.tachiyomi.util.system.logcat
|
||||
import com.hippo.ehviewer.util.OSUtils
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.plugins.cookies.HttpCookies
|
||||
import io.ktor.client.engine.okhttp.OkHttp
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import logcat.AndroidLogcatLogger
|
||||
@@ -83,64 +89,63 @@ import splitties.init.appCtx
|
||||
private val lifecycle = ProcessLifecycleOwner.get().lifecycle
|
||||
private val lifecycleScope = lifecycle.coroutineScope
|
||||
|
||||
class EhApplication :
|
||||
Application(),
|
||||
SingletonImageLoader.Factory {
|
||||
override fun onCreate() {
|
||||
class EhApplication : Application(), SingletonImageLoader.Factory {
|
||||
override fun onCreate() = with(lifecycleScope) {
|
||||
initSETConnection()
|
||||
// Initialize Settings on first access
|
||||
lifecycleScope.launchIO {
|
||||
val mode = Settings.theme
|
||||
launchIO {
|
||||
val mode = Settings.theme.value
|
||||
if (!isAtLeastS) {
|
||||
withUIContext {
|
||||
AppCompatDelegate.setDefaultNightMode(mode)
|
||||
}
|
||||
}
|
||||
if (!LogcatLogger.isInstalled && Settings.saveCrashLog) {
|
||||
LogcatLogger.install(AndroidLogcatLogger(LogPriority.VERBOSE))
|
||||
LogcatLogger.loggers += AndroidLogcatLogger(LogPriority.VERBOSE)
|
||||
Settings.saveCrashLog.valueFlow().collect {
|
||||
if (it) {
|
||||
LogcatLogger.install()
|
||||
} else {
|
||||
LogcatLogger.uninstall()
|
||||
}
|
||||
}
|
||||
}
|
||||
lifecycle.addObserver(lockObserver)
|
||||
val handler = Thread.getDefaultUncaughtExceptionHandler()
|
||||
Thread.setDefaultUncaughtExceptionHandler { t, e ->
|
||||
try {
|
||||
if (Settings.saveCrashLog) {
|
||||
Crash.saveCrashLog(e)
|
||||
}
|
||||
} catch (ignored: Throwable) {
|
||||
}
|
||||
handler?.uncaughtException(t, e)
|
||||
}
|
||||
CrashHandler.install()
|
||||
super.onCreate()
|
||||
System.loadLibrary("ehviewer")
|
||||
lifecycleScope.launchIO {
|
||||
launch { EhTagDatabase }
|
||||
launch {
|
||||
FavouriteStatusRouter.collect { info ->
|
||||
detailCache[info.gid]?.apply {
|
||||
favoriteSlot = info.favoriteSlot
|
||||
favoriteName = info.favoriteName
|
||||
favoriteNote = info.favoriteNote
|
||||
}
|
||||
}
|
||||
}
|
||||
launchIO {
|
||||
EhTagDatabase.launchUpdate()
|
||||
launch { EhDB }
|
||||
dataStateFlow.value
|
||||
launch { dataStateFlow.value }
|
||||
launch { OSUtils.totalMemory }
|
||||
launch {
|
||||
if (DownloadManager.labelList.isNotEmpty() && Settings.downloadFilterMode.key !in Settings.prefs) {
|
||||
if (DownloadManager.labelList.isNotEmpty() && Settings.downloadFilterMode !in Settings.snapshot()) {
|
||||
Settings.downloadFilterMode.value = DownloadsFilterMode.CUSTOM.flag
|
||||
}
|
||||
initialized = true
|
||||
DownloadManager.readMetadataFromLocal()
|
||||
}
|
||||
launch {
|
||||
FileUtils.cleanupDirectory(AppConfig.externalCrashDir)
|
||||
FileUtils.cleanupDirectory(AppConfig.externalParseErrorDir)
|
||||
}
|
||||
launch {
|
||||
cleanupDownload()
|
||||
}
|
||||
if (Settings.requestNews) {
|
||||
launch {
|
||||
checkDawn()
|
||||
}
|
||||
}
|
||||
launch {
|
||||
cleanObsoleteCache()
|
||||
launch { cleanupDownload() }
|
||||
if (Settings.requestNews.value) {
|
||||
launch { checkDawn() }
|
||||
}
|
||||
}
|
||||
if (BuildConfig.DEBUG) {
|
||||
StrictMode.enableDefaults()
|
||||
Composer.setDiagnosticStackTraceMode(ComposeStackTraceMode.SourceInformation)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,30 +163,32 @@ class EhApplication :
|
||||
}
|
||||
|
||||
private fun clearTempDir() {
|
||||
var dir = AppConfig.tempDir
|
||||
if (null != dir) {
|
||||
FileUtils.deleteContent(dir)
|
||||
}
|
||||
dir = AppConfig.externalTempDir
|
||||
if (null != dir) {
|
||||
FileUtils.deleteContent(dir)
|
||||
}
|
||||
AppConfig.tempDir.deleteContent()
|
||||
AppConfig.externalTempDir?.deleteContent()
|
||||
}
|
||||
|
||||
override fun newImageLoader(context: Context) = context.imageLoader {
|
||||
interceptorCoroutineContext(Dispatchers.Default)
|
||||
components {
|
||||
serviceLoaderEnabled(false)
|
||||
add(KtorNetworkFetcherFactory { ktorClient })
|
||||
add(
|
||||
KtorNetworkFetcherFactory(
|
||||
httpClient = { ktorClient },
|
||||
connectivityChecker = { ConnectivityChecker.ONLINE },
|
||||
),
|
||||
)
|
||||
add(MergeInterceptor)
|
||||
add(DownloadThumbInterceptor)
|
||||
if (isAtLeastO) {
|
||||
add(HardwareBitmapInterceptor)
|
||||
} else {
|
||||
allowRgb565(true)
|
||||
}
|
||||
add(CropBorderInterceptor)
|
||||
add(DetectBorderInterceptor)
|
||||
add(QrCodeInterceptor)
|
||||
add(MapExtraInfoInterceptor)
|
||||
add(AnimatedWebPDecoder.Factory)
|
||||
if (isAtLeastP) {
|
||||
add(AnimatedImageDecoder.Factory(false))
|
||||
} else {
|
||||
@@ -206,38 +213,34 @@ class EhApplication :
|
||||
}
|
||||
|
||||
companion object {
|
||||
@Volatile
|
||||
var initialized = false
|
||||
private set
|
||||
|
||||
val ktorClient by lazy {
|
||||
HttpClient(Cronet) {
|
||||
engine {
|
||||
client = cronetHttpClient
|
||||
if (isAtLeastSExtension7 && Settings.enableCronet.value) {
|
||||
HttpClient(Cronet) {
|
||||
engine { configureClient(Settings.enableQuic.value) }
|
||||
configureCommon()
|
||||
}
|
||||
install(HttpCookies) {
|
||||
storage = EhCookieStore
|
||||
} else {
|
||||
HttpClient(OkHttp) {
|
||||
engine { configureClient() }
|
||||
configureCommon()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val noRedirectKtorClient by lazy {
|
||||
HttpClient(ktorClient.engine) {
|
||||
followRedirects = false
|
||||
install(HttpCookies) {
|
||||
storage = EhCookieStore
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val galleryDetailCache by lazy {
|
||||
LruCache<Long, GalleryDetail>(25).also {
|
||||
lifecycleScope.launch {
|
||||
FavouriteStatusRouter.globalFlow.collect { (gid, slot) -> it[gid]?.favoriteSlot = slot }
|
||||
}
|
||||
configureCommon(redirect = false)
|
||||
}
|
||||
}
|
||||
|
||||
val imageCache by lazy {
|
||||
diskCache {
|
||||
directory(appCtx.cacheDir.toOkioPath() / "image_cache")
|
||||
maxSizeBytes(Settings.readCacheSize.coerceIn(320, 5120).toLong() * 1024 * 1024)
|
||||
maxSizeBytes(Settings.readCacheSize.value.coerceIn(320, 5120).toLong() * 1024 * 1024)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -17,22 +17,24 @@ package com.hippo.ehviewer
|
||||
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import arrow.fx.coroutines.release
|
||||
import arrow.fx.coroutines.resource
|
||||
import com.hippo.ehviewer.client.data.BaseGalleryInfo
|
||||
import com.hippo.ehviewer.dao.DownloadArtist
|
||||
import com.hippo.ehviewer.dao.DownloadDirname
|
||||
import com.hippo.ehviewer.dao.DownloadInfo
|
||||
import com.hippo.ehviewer.dao.DownloadLabel
|
||||
import com.hippo.ehviewer.dao.EhDatabase
|
||||
import com.hippo.ehviewer.dao.Filter
|
||||
import com.hippo.ehviewer.dao.HistoryInfo
|
||||
import com.hippo.ehviewer.dao.LocalFavoriteInfo
|
||||
import com.hippo.ehviewer.dao.ProgressInfo
|
||||
import com.hippo.ehviewer.dao.QuickSearch
|
||||
import com.hippo.ehviewer.dao.Schema17to18
|
||||
import arrow.fx.coroutines.resourceScope
|
||||
import com.ehviewer.core.data.model.asEntity
|
||||
import com.ehviewer.core.database.EhDatabase
|
||||
import com.ehviewer.core.database.Schema17to18
|
||||
import com.ehviewer.core.database.model.DownloadArtist
|
||||
import com.ehviewer.core.database.model.DownloadDirname
|
||||
import com.ehviewer.core.database.model.DownloadInfo
|
||||
import com.ehviewer.core.database.model.DownloadLabel
|
||||
import com.ehviewer.core.database.model.Filter
|
||||
import com.ehviewer.core.database.model.GalleryEntity
|
||||
import com.ehviewer.core.database.model.HistoryInfo
|
||||
import com.ehviewer.core.database.model.LocalFavoriteInfo
|
||||
import com.ehviewer.core.database.model.ProgressInfo
|
||||
import com.ehviewer.core.database.model.QuickSearch
|
||||
import com.ehviewer.core.files.sendTo
|
||||
import com.ehviewer.core.model.GalleryInfo
|
||||
import com.hippo.ehviewer.download.DownloadManager
|
||||
import com.hippo.ehviewer.util.sendTo
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import okio.Path
|
||||
import okio.Path.Companion.toOkioPath
|
||||
@@ -44,15 +46,15 @@ object EhDB {
|
||||
addMigrations(Schema17to18())
|
||||
}
|
||||
|
||||
private suspend fun putGalleryInfo(galleryInfo: BaseGalleryInfo) {
|
||||
suspend fun putGalleryInfo(galleryInfo: GalleryEntity) {
|
||||
db.galleryDao().upsert(galleryInfo)
|
||||
}
|
||||
|
||||
private suspend fun deleteGalleryInfo(galleryInfo: BaseGalleryInfo) {
|
||||
private suspend fun deleteGalleryInfo(galleryInfo: GalleryEntity) {
|
||||
runCatching { db.galleryDao().delete(galleryInfo) }
|
||||
}
|
||||
|
||||
suspend fun updateGalleryInfo(galleryInfoList: List<BaseGalleryInfo>) {
|
||||
suspend fun updateGalleryInfo(galleryInfoList: List<GalleryEntity>) {
|
||||
db.galleryDao().update(galleryInfoList)
|
||||
}
|
||||
|
||||
@@ -146,6 +148,8 @@ object EhDB {
|
||||
dao.fill(raw.position)
|
||||
}
|
||||
|
||||
suspend fun searchDownloadLabel(keyword: String, limit: Int) = db.downloadLabelDao().search("%$keyword%", limit)
|
||||
|
||||
suspend fun putDownloadArtist(gid: Long, artists: List<DownloadArtist>) {
|
||||
if (artists.isNotEmpty()) {
|
||||
val dao = db.downloadArtistDao()
|
||||
@@ -154,12 +158,12 @@ object EhDB {
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun removeLocalFavorites(galleryInfo: BaseGalleryInfo) {
|
||||
suspend fun removeLocalFavorites(galleryInfo: GalleryInfo) {
|
||||
db.localFavoritesDao().deleteByKey(galleryInfo.gid)
|
||||
deleteGalleryInfo(galleryInfo)
|
||||
deleteGalleryInfo(galleryInfo.asEntity())
|
||||
}
|
||||
|
||||
suspend fun removeLocalFavorites(galleryInfoList: Collection<BaseGalleryInfo>) {
|
||||
suspend fun removeLocalFavorites(galleryInfoList: Collection<GalleryInfo>) {
|
||||
galleryInfoList.forEach {
|
||||
removeLocalFavorites(it)
|
||||
}
|
||||
@@ -170,12 +174,12 @@ object EhDB {
|
||||
return dao.contains(gid)
|
||||
}
|
||||
|
||||
suspend fun putLocalFavorites(galleryInfo: BaseGalleryInfo) {
|
||||
putGalleryInfo(galleryInfo)
|
||||
suspend fun putLocalFavorites(galleryInfo: GalleryInfo) {
|
||||
putGalleryInfo(galleryInfo.asEntity())
|
||||
db.localFavoritesDao().upsert(LocalFavoriteInfo(galleryInfo.gid))
|
||||
}
|
||||
|
||||
suspend fun putLocalFavorites(galleryInfoList: Collection<BaseGalleryInfo>) {
|
||||
suspend fun putLocalFavorites(galleryInfoList: Collection<GalleryInfo>) {
|
||||
galleryInfoList.forEach {
|
||||
putLocalFavorites(it)
|
||||
}
|
||||
@@ -221,8 +225,8 @@ object EhDB {
|
||||
|
||||
fun searchLocalFav(keyword: String) = db.localFavoritesDao().joinListLazy("*$keyword*")
|
||||
|
||||
suspend fun putHistoryInfo(galleryInfo: BaseGalleryInfo) {
|
||||
putGalleryInfo(galleryInfo)
|
||||
suspend fun putHistoryInfo(galleryInfo: GalleryInfo) {
|
||||
putGalleryInfo(galleryInfo.asEntity())
|
||||
db.historyDao().upsert(HistoryInfo(galleryInfo.gid))
|
||||
}
|
||||
|
||||
@@ -239,7 +243,7 @@ object EhDB {
|
||||
dao.insertOrIgnore(historyInfoList)
|
||||
}
|
||||
|
||||
suspend fun deleteHistoryInfo(galleryInfo: BaseGalleryInfo) {
|
||||
suspend fun deleteHistoryInfo(galleryInfo: GalleryEntity) {
|
||||
val dao = db.historyDao()
|
||||
dao.deleteByKey(galleryInfo.gid)
|
||||
deleteGalleryInfo(galleryInfo)
|
||||
@@ -279,52 +283,52 @@ object EhDB {
|
||||
dbFile.toOkioPath() sendTo file
|
||||
}
|
||||
|
||||
suspend fun importDB(context: Context, uri: Uri) {
|
||||
suspend fun importDB(context: Context, uri: Uri) = resourceScope {
|
||||
val tempDBName = "tmp.db"
|
||||
resource {
|
||||
val oldDB = resource {
|
||||
context.deleteDatabase(tempDBName)
|
||||
roomDb<EhDatabase>(tempDBName) {
|
||||
createFromInputStream { context.contentResolver.openInputStream(uri) }
|
||||
addMigrations(Schema17to18())
|
||||
}
|
||||
} release {
|
||||
it.close()
|
||||
} release { db ->
|
||||
db.close()
|
||||
context.deleteDatabase(tempDBName)
|
||||
} use { oldDB ->
|
||||
db.galleryDao().insertOrIgnore(oldDB.galleryDao().list())
|
||||
}
|
||||
|
||||
db.progressDao().insertOrIgnore(oldDB.progressDao().list())
|
||||
db.galleryDao().insertOrIgnore(oldDB.galleryDao().list())
|
||||
db.progressDao().insertOrIgnore(oldDB.progressDao().list())
|
||||
|
||||
val downloadLabelList = oldDB.downloadLabelDao().list()
|
||||
DownloadManager.addDownloadLabel(downloadLabelList)
|
||||
val downloadLabelList = oldDB.downloadLabelDao().list()
|
||||
DownloadManager.addDownloadLabel(downloadLabelList)
|
||||
|
||||
oldDB.downloadDirnameDao().list().let {
|
||||
importDownloadDirname(it)
|
||||
}
|
||||
oldDB.downloadDirnameDao().list().let {
|
||||
importDownloadDirname(it)
|
||||
}
|
||||
|
||||
val downloadInfoList = oldDB.downloadsDao().joinList().asReversed()
|
||||
DownloadManager.addDownload(downloadInfoList)
|
||||
val downloadInfoList = oldDB.downloadsDao().joinList().asReversed()
|
||||
DownloadManager.addDownload(downloadInfoList)
|
||||
|
||||
val historyInfoList = oldDB.historyDao().list()
|
||||
importHistoryInfo(historyInfoList)
|
||||
val historyInfoList = oldDB.historyDao().list()
|
||||
importHistoryInfo(historyInfoList)
|
||||
|
||||
val quickSearchList = oldDB.quickSearchDao().list()
|
||||
val currentQuickSearchList = db.quickSearchDao().list()
|
||||
val offset = currentQuickSearchList.size
|
||||
val importList = quickSearchList.filter { newQS ->
|
||||
currentQuickSearchList.none { it.name == newQS.name }
|
||||
}.onEachIndexed { index, q -> q.position = index + offset }
|
||||
importQuickSearch(importList)
|
||||
val quickSearchList = oldDB.quickSearchDao().list()
|
||||
val currentQuickSearchList = db.quickSearchDao().list()
|
||||
val offset = currentQuickSearchList.size
|
||||
val importList = quickSearchList.filter { newQS ->
|
||||
currentQuickSearchList.none { it.name == newQS.name }
|
||||
}.onEachIndexed { index, q ->
|
||||
q.id = null
|
||||
q.position = index + offset
|
||||
}
|
||||
importQuickSearch(importList)
|
||||
|
||||
oldDB.localFavoritesDao().list().let {
|
||||
importLocalFavorites(it)
|
||||
}
|
||||
oldDB.localFavoritesDao().list().let {
|
||||
importLocalFavorites(it)
|
||||
}
|
||||
|
||||
val filterList = oldDB.filterDao().list()
|
||||
val currentFilterList = db.filterDao().list()
|
||||
filterList.forEach {
|
||||
if (it !in currentFilterList) addFilter(it)
|
||||
}
|
||||
oldDB.filterDao().list().forEach {
|
||||
addFilter(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,8 +9,9 @@ import androidx.compose.runtime.Stable
|
||||
import androidx.compose.runtime.State
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.remember
|
||||
import com.hippo.ehviewer.client.CHROME_MOBILE_USER_AGENT
|
||||
import com.hippo.ehviewer.client.CHROME_USER_AGENT
|
||||
import com.ehviewer.core.preferences.DataStorePreferences
|
||||
import com.ehviewer.core.preferences.PrefDelegate
|
||||
import com.ehviewer.core.preferences.edit
|
||||
import com.hippo.ehviewer.client.EhCookieStore
|
||||
import com.hippo.ehviewer.client.data.FavListUrlBuilder
|
||||
import com.hippo.ehviewer.download.DownloadsFilterMode
|
||||
@@ -27,47 +28,12 @@ import kotlinx.coroutines.flow.conflate
|
||||
import kotlinx.coroutines.flow.debounce
|
||||
import kotlinx.coroutines.flow.flatMapMerge
|
||||
import kotlinx.coroutines.flow.map
|
||||
import splitties.preferences.BoolPref
|
||||
import splitties.preferences.DataStorePreferences
|
||||
import splitties.preferences.FloatPref
|
||||
import splitties.preferences.IntPref
|
||||
import splitties.preferences.LongPref
|
||||
import splitties.preferences.PrefDelegate
|
||||
import splitties.preferences.StringOrNullPref
|
||||
import splitties.preferences.StringPref
|
||||
import splitties.preferences.StringSetOrNullPref
|
||||
import splitties.preferences.StringSetPref
|
||||
import splitties.preferences.edit
|
||||
|
||||
@Suppress("IMPLICIT_CAST_TO_ANY", "UNCHECKED_CAST")
|
||||
fun <T> PrefDelegate<T>.getValue(): T = when (this) {
|
||||
is BoolPref -> value
|
||||
is IntPref -> value
|
||||
is FloatPref -> value
|
||||
is LongPref -> value
|
||||
is StringPref -> value
|
||||
is StringOrNullPref -> value
|
||||
is StringSetPref -> value
|
||||
is StringSetOrNullPref -> value
|
||||
} as T
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
fun <T> PrefDelegate<T>.setValue(newValue: T) = when (this) {
|
||||
is BoolPref -> value = newValue as Boolean
|
||||
is IntPref -> value = newValue as Int
|
||||
is FloatPref -> value = newValue as Float
|
||||
is LongPref -> value = newValue as Long
|
||||
is StringPref -> value = newValue as String
|
||||
is StringOrNullPref -> value = newValue as? String
|
||||
is StringSetPref -> value = newValue as Set<String?>
|
||||
is StringSetOrNullPref -> value = newValue as? Set<String?>
|
||||
}
|
||||
|
||||
@Stable
|
||||
@Composable
|
||||
inline fun <R, T> PrefDelegate<T>.collectAsState(crossinline transform: @DisallowComposableCalls (T) -> R): State<R> {
|
||||
val flow = remember { valueFlow().map { transform(it) } }
|
||||
val init = getValue()
|
||||
val init = value
|
||||
return flow.collectAsState(transform(init))
|
||||
}
|
||||
|
||||
@@ -75,32 +41,28 @@ inline fun <R, T> PrefDelegate<T>.collectAsState(crossinline transform: @Disallo
|
||||
@Composable
|
||||
fun <T> PrefDelegate<T>.collectAsState(): State<T> {
|
||||
val flow = remember { valueFlow() }
|
||||
val init = getValue()
|
||||
val init = value
|
||||
return flow.collectAsState(init)
|
||||
}
|
||||
|
||||
@Stable
|
||||
@Composable
|
||||
fun <T> PrefDelegate<T>.asMutableState(): MutableState<T> {
|
||||
val flow = remember { valueFlow() }
|
||||
val init = getValue()
|
||||
val readOnly = flow.collectAsState(init)
|
||||
val readOnly = collectAsState()
|
||||
return remember {
|
||||
object : MutableState<T> {
|
||||
override var value: T
|
||||
get() = readOnly.value
|
||||
set(value) {
|
||||
setValue(value)
|
||||
this@asMutableState.value = value
|
||||
}
|
||||
override fun component1() = TODO()
|
||||
override fun component2() = TODO()
|
||||
override fun component1() = readOnly.value
|
||||
override fun component2() = this@asMutableState::value::set
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
object Settings : DataStorePreferences(null) {
|
||||
private const val KEY_SHOW_TAG_TRANSLATIONS = "show_tag_translations"
|
||||
|
||||
@Suppress("ktlint:standard:backing-property-naming")
|
||||
private val _favFlow = MutableSharedFlow<Unit>()
|
||||
val favChangesFlow = _favFlow.debounce(1000)
|
||||
@@ -108,71 +70,85 @@ object Settings : DataStorePreferences(null) {
|
||||
var favCount by intArrayPref("fav_count", 10).emitTo(_favFlow)
|
||||
var favCloudCount by intPref("fav_cloud", 0).emitTo(_favFlow)
|
||||
|
||||
// Eh
|
||||
val gallerySite = intPref("gallery_site_2", 0).observed(::updateWhenGallerySiteChanges)
|
||||
val defaultFavSlot = intPref("default_favorite_slot", -2)
|
||||
val theme = intPref("theme_2", -1).observed(::updateWhenThemeChanges)
|
||||
val blackDarkTheme = boolPref("black_dark_theme", false)
|
||||
val harmonizeCategoryColor = boolPref("harmonize_category_color", true)
|
||||
val launchPage = intPref("launch_page_2", 0)
|
||||
val listMode = intPref("list_mode_2", 0)
|
||||
val listThumbSize = intPref("list_tile_size", 40)
|
||||
val detailSize = intPref("detail_size_2", 0)
|
||||
val thumbColumns = intPref("thumb_columns", 3)
|
||||
val listThumbSize = intPref("list_tile_size", 40)
|
||||
val showGalleryPages = boolPref("show_gallery_pages", true)
|
||||
val showVoteStatus = boolPref("show_vote_status", false)
|
||||
val showComments = boolPref("show_gallery_comments", true)
|
||||
val commentThreshold = intPref("comment_threshold", -100)
|
||||
val showTagTranslations = boolPref("show_tag_translations", false).observed(::updateWhenTagTranslationChanges)
|
||||
val meteredNetworkWarning = boolPref("cellular_network_warning", false)
|
||||
val showJpnTitle = boolPref("show_jpn_title", false)
|
||||
val requestNews = boolPref("request_news", false).observed { updateWhenRequestNewsChanges() }
|
||||
val hideHvEvents = boolPref("hide_hv_events", false)
|
||||
|
||||
// Download
|
||||
val mediaScan = boolPref("media_scan", false).observed(::updateWhenKeepMediaStatusChanges)
|
||||
val multiThreadDownload = intPref("download_thread_2", 3)
|
||||
val downloadDelay = intPref("download_delay_3", 1000)
|
||||
val timeoutSpeed = intPref("timeout_speed_level", 6)
|
||||
val preloadImage = intPref("preload_image_2", 5)
|
||||
val downloadOriginImage = boolPref("download_origin_image", false)
|
||||
val saveAsCbz = boolPref("save_as_cbz", false)
|
||||
val archiveMetadata = boolPref("archive_metadata", true)
|
||||
|
||||
// Privacy
|
||||
val security = boolPref("require_unlock", false)
|
||||
val securityDelay = intPref("require_unlock_delay", 0)
|
||||
val enabledSecurity = boolPref("enable_secure", false)
|
||||
|
||||
// Advanced
|
||||
val saveParseErrorBody = boolPref("save_parse_error_body", true)
|
||||
val saveCrashLog = boolPref("save_crash_log", true)
|
||||
val readCacheSize = intPref("read_cache_size_2", 640)
|
||||
val enableCronet = boolPref("enable_cronet", true)
|
||||
val enableQuic = boolPref("enable_quic", true)
|
||||
val hardwareBitmapThreshold = intPref("hardware_bitmap_threshold", 16384)
|
||||
val preloadThumbAggressively = boolPref("preload_thumb_aggressively", false)
|
||||
val animateItems = boolPref("animate_items", true)
|
||||
val desktopSite = boolPref("desktop_site", true)
|
||||
|
||||
// About
|
||||
val backupBeforeUpdate = boolPref("backup_before_update", false)
|
||||
val useCIUpdateChannel = boolPref("ci_update_channel", AppConfig.isSnapshot)
|
||||
val updateIntervalDays = intPref("update_interval_days", 7)
|
||||
|
||||
// Misc
|
||||
val languageFilter = intPref("language_filter", -1)
|
||||
val downloadSortMode = intPref("download_sort_mode", SortMode.Default.flag)
|
||||
val downloadFilterMode = intPref("download_filter_mode", DownloadsFilterMode.Default.flag)
|
||||
val hasSignedIn = boolPref("has_signed_in", EhCookieStore.hasSignedIn())
|
||||
val meteredNetworkWarning = boolPref("cellular_network_warning", false)
|
||||
val blackDarkTheme = boolPref("black_dark_theme", false)
|
||||
val needSignIn = boolPref("need_sign_in", true)
|
||||
val gridView = boolPref("grid_view", false)
|
||||
val showGalleryPages = boolPref("show_gallery_pages", true)
|
||||
val qSSaveProgress = boolPref("qs_save_progress", true)
|
||||
val security = boolPref("require_unlock", false)
|
||||
val animateItems = boolPref("animate_items", true)
|
||||
val displayName = stringOrNullPref("display_name", null)
|
||||
val recentDownloadLabel = stringOrNullPref("recent_download_label", null)
|
||||
val displayName = stringOrNullPref("display_name")
|
||||
val avatar = stringOrNullPref("avatar")
|
||||
val recentDownloadLabel = stringOrNullPref("recent_download_label")
|
||||
|
||||
var downloadScheme by stringOrNullPref("image_scheme", null)
|
||||
var downloadAuthority by stringOrNullPref("image_authority", null)
|
||||
var downloadPath by stringOrNullPref("image_path", null)
|
||||
var downloadQuery by stringOrNullPref("image_query", null)
|
||||
var downloadFragment by stringOrNullPref("image_fragment", null)
|
||||
var archivePasswds by stringSetOrNullPref("archive_passwds")
|
||||
var downloadDelay by intPref("download_delay_3", 1000)
|
||||
var gallerySite by intPref("gallery_site_2", 0).observed { updateWhenGallerySiteChanges() }
|
||||
var multiThreadDownload by intPref("download_thread_2", 3)
|
||||
var preloadImage by intPref("preload_image_2", 5)
|
||||
var downloadTimeout by intPref("download_timeout", 60)
|
||||
var theme by intPref("theme_2", -1).observed { updateWhenThemeChanges() }
|
||||
var thumbResolution by intPref("thumb_resolution_2", 0)
|
||||
var readCacheSize by intPref("read_cache_size_2", 640)
|
||||
var launchPage by intPref("launch_page_2", 0)
|
||||
var commentThreshold by intPref("comment_threshold", -101)
|
||||
var hardwareBitmapThreshold by intPref("hardware_bitmap_threshold", 16384)
|
||||
var showComments by boolPref("show_gallery_comments", true)
|
||||
var requestNews by boolPref("request_news", false).observed { updateWhenRequestNewsChanges() }
|
||||
var hideHvEvents by boolPref("hide_hv_events", false)
|
||||
var showJpnTitle by boolPref("show_jpn_title", false)
|
||||
var showTagTranslations by boolPref(KEY_SHOW_TAG_TRANSLATIONS, false).observed { updateWhenTagTranslationChanges() }
|
||||
var downloadScheme by stringOrNullPref("image_scheme")
|
||||
var downloadAuthority by stringOrNullPref("image_authority")
|
||||
var downloadPath by stringOrNullPref("image_path")
|
||||
var downloadQuery by stringOrNullPref("image_query")
|
||||
var downloadFragment by stringOrNullPref("image_fragment")
|
||||
var archivePasswds by stringSetPref("archive_passwds")
|
||||
var appLinkVerifyTip by boolPref("app_link_verify_tip", false)
|
||||
var enabledSecurity by boolPref("enable_secure", false)
|
||||
var backupBeforeUpdate by boolPref("backup_before_update", true)
|
||||
var useCIUpdateChannel by boolPref("ci_update_channel", AppConfig.isSnapshot)
|
||||
var mediaScan by boolPref("media_scan", false).observed { updateWhenKeepMediaStatusChanges() }
|
||||
var hasDefaultDownloadLabel by boolPref("has_default_download_label", false)
|
||||
var saveParseErrorBody by boolPref("save_parse_error_body", true)
|
||||
var saveCrashLog by boolPref("save_crash_log", true)
|
||||
var removeImageFiles by boolPref("include_pic", true)
|
||||
var needSignIn by boolPref("need_sign_in", true)
|
||||
var harmonizeCategoryColor by boolPref("harmonize_category_color", true)
|
||||
var preloadThumbAggressively by boolPref("preload_thumb_aggressively", false)
|
||||
var downloadOriginImage by boolPref("download_origin_image", false)
|
||||
var saveAsCbz by boolPref("save_as_cbz", false)
|
||||
var archiveMetadata by boolPref("archive_metadata", true)
|
||||
var desktopSite by boolPref("desktop_site", false)
|
||||
var recentFavCat by intPref("recent_fav_cat", FavListUrlBuilder.FAV_CAT_LOCAL)
|
||||
var defaultFavSlot by intPref("default_favorite_slot", -2)
|
||||
var securityDelay by intPref("require_unlock_delay", 0)
|
||||
var clipboardTextHashCode by intPref("clipboard_text_hash_code", 0)
|
||||
var requestNewsTime by intPref("request_news_time", 0).observed { updateWhenRequestNewsChanges() }
|
||||
var updateIntervalDays by intPref("update_interval_days", 7)
|
||||
var lastDawnDays by intPref("last_dawn_days", 0)
|
||||
var recentToplist by stringPref("recent_toplist", "11")
|
||||
var defaultDownloadLabel by stringOrNullPref("default_download_label", null)
|
||||
var defaultDownloadLabel by stringOrNullPref("default_download_label")
|
||||
var lastUpdateTime by longPref("last_update_time", BuildConfig.COMMIT_TIME)
|
||||
|
||||
// Reader
|
||||
@@ -182,8 +158,8 @@ object Settings : DataStorePreferences(null) {
|
||||
val colorFilterMode = intPref("color_filter_mode", 0)
|
||||
val customBrightness = boolPref("pref_custom_brightness_key", false)
|
||||
val customBrightnessValue = intPref("custom_brightness_value", 0)
|
||||
val readingMode = intPref("pref_default_reading_mode_key", ReadingModeType.LEFT_TO_RIGHT.flagValue)
|
||||
val orientationMode = intPref("pref_default_orientation_type_key", OrientationType.FREE.flagValue)
|
||||
val readingMode = intPref("pref_default_reading_mode_key", ReadingModeType.DEFAULT.prefValue)
|
||||
val orientationMode = intPref("pref_default_orientation_type_key", OrientationType.DEFAULT.prefValue)
|
||||
val showReaderSeekbar = boolPref("pref_show_reader_seekbar", true)
|
||||
val showPageNumber = boolPref("pref_show_page_number_key", true)
|
||||
val readerTheme = intPref("pref_reader_theme_key", 1)
|
||||
@@ -194,7 +170,6 @@ object Settings : DataStorePreferences(null) {
|
||||
val readerLongTapAction = boolPref("reader_long_tap", true)
|
||||
val pageTransitions = boolPref("pref_enable_transitions_key", true)
|
||||
val readWithVolumeKeys = boolPref("reader_volume_keys", false)
|
||||
val readWithVolumeKeysInterval = intPref("reader_volume_keys_interval", 0)
|
||||
val readWithVolumeKeysInverted = boolPref("reader_volume_keys_inverted", false)
|
||||
val grayScale = boolPref("pref_grayscale", false)
|
||||
val invertedColors = boolPref("pref_inverted_colors", false)
|
||||
@@ -211,47 +186,40 @@ object Settings : DataStorePreferences(null) {
|
||||
val showNavigationOverlayOnStart = boolPref("reader_navigation_overlay_on_start", false)
|
||||
val stripExtraneousAds = boolPref("strip_extraneous_ads", false)
|
||||
|
||||
val userAgent
|
||||
get() = if (desktopSite) CHROME_USER_AGENT else CHROME_MOBILE_USER_AGENT
|
||||
|
||||
init {
|
||||
if ("CN" == Locale.getDefault().country) {
|
||||
edit {
|
||||
if (KEY_SHOW_TAG_TRANSLATIONS !in prefs) showTagTranslations = true
|
||||
edit { pref ->
|
||||
if ("CN" == Locale.getDefault().country) {
|
||||
if (showTagTranslations !in pref) pref[showTagTranslations] = true
|
||||
}
|
||||
if (pref[downloadFilterMode] == DownloadsFilterMode.ARTIST.flag && pref[recentDownloadLabel] == null) {
|
||||
pref[recentDownloadLabel] = ""
|
||||
}
|
||||
}
|
||||
if (downloadFilterMode.value == DownloadsFilterMode.ARTIST.flag && recentDownloadLabel.value == null) {
|
||||
recentDownloadLabel.value = ""
|
||||
}
|
||||
}
|
||||
|
||||
interface Delegate<R> {
|
||||
val flowGetter: () -> Flow<Unit>
|
||||
fun changesFlow(): Flow<Unit>
|
||||
operator fun getValue(thisRef: Any?, prop: KProperty<*>?): R
|
||||
operator fun setValue(thisRef: Any?, prop: KProperty<*>?, value: R)
|
||||
}
|
||||
|
||||
private fun intArrayPref(key: String, count: Int) = object : Delegate<IntArray> {
|
||||
override val flowGetter: () -> Flow<Unit> = { _value.asFlow().flatMapMerge { it.changesFlow() }.conflate() }
|
||||
|
||||
@Suppress("ktlint:standard:backing-property-naming")
|
||||
private var _value = (0 until count).map { intPref("${key}_$it", 0) }.toTypedArray()
|
||||
override fun getValue(thisRef: Any?, prop: KProperty<*>?): IntArray = _value.map { it.value }.toIntArray()
|
||||
private val delegates = Array(count) { intPref("${key}_$it", 0) }
|
||||
override fun changesFlow(): Flow<Unit> = delegates.asFlow().flatMapMerge { it.changesFlow() }.conflate()
|
||||
override fun getValue(thisRef: Any?, prop: KProperty<*>?) = IntArray(delegates.size) { delegates[it].value }
|
||||
override fun setValue(thisRef: Any?, prop: KProperty<*>?, value: IntArray) {
|
||||
check(value.size == count)
|
||||
edit { value.zip(_value) { v, d -> d.value = v } }
|
||||
edit { pref -> value.zip(delegates) { v, d -> pref[d] = v } }
|
||||
}
|
||||
}
|
||||
|
||||
private fun stringArrayPref(key: String, count: Int, defMetaValue: String) = object : Delegate<Array<String>> {
|
||||
override val flowGetter: () -> Flow<Unit> = { _value.asFlow().flatMapMerge { it.changesFlow() }.conflate() }
|
||||
|
||||
@Suppress("ktlint:standard:backing-property-naming")
|
||||
private var _value = (0 until count).map { stringPref("${key}_$it", "$defMetaValue $it") }.toTypedArray()
|
||||
override fun getValue(thisRef: Any?, prop: KProperty<*>?): Array<String> = _value.map { it.value }.toTypedArray()
|
||||
private val delegates = Array(count) { stringPref("${key}_$it", "$defMetaValue $it") }
|
||||
override fun changesFlow(): Flow<Unit> = delegates.asFlow().flatMapMerge { it.changesFlow() }.conflate()
|
||||
override fun getValue(thisRef: Any?, prop: KProperty<*>?) = Array(delegates.size) { delegates[it].value }
|
||||
override fun setValue(thisRef: Any?, prop: KProperty<*>?, value: Array<String>) {
|
||||
check(value.size == count)
|
||||
edit { value.zip(_value) { v, d -> d.value = v } }
|
||||
edit { pref -> value.zip(delegates) { v, d -> pref[d] = v } }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,74 +2,73 @@ package com.hippo.ehviewer
|
||||
|
||||
import android.app.UiModeManager
|
||||
import androidx.appcompat.app.AppCompatDelegate
|
||||
import arrow.core.Either.Companion.catch
|
||||
import com.ehviewer.core.preferences.PrefDelegate
|
||||
import com.ehviewer.core.util.isAtLeastS
|
||||
import com.ehviewer.core.util.logcat
|
||||
import com.ehviewer.core.util.withIOContext
|
||||
import com.hippo.ehviewer.client.EhEngine
|
||||
import com.hippo.ehviewer.client.EhTagDatabase
|
||||
import com.hippo.ehviewer.dailycheck.updateDailyCheckWork
|
||||
import com.hippo.ehviewer.ui.keepNoMediaFileStatus
|
||||
import com.hippo.ehviewer.util.isAtLeastS
|
||||
import eu.kanade.tachiyomi.util.lang.launchIO
|
||||
import eu.kanade.tachiyomi.util.system.logcat
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
import kotlinx.coroutines.flow.drop
|
||||
import kotlinx.coroutines.flow.emitAll
|
||||
import kotlinx.coroutines.launch
|
||||
import splitties.init.appCtx
|
||||
import splitties.preferences.PrefDelegate
|
||||
import splitties.systemservices.uiModeManager
|
||||
|
||||
private val collectScope = CoroutineScope(Dispatchers.Main + SupervisorJob())
|
||||
fun <T, R : PrefDelegate<T>> R.observed(func: (Unit) -> Unit) = apply { collectScope.launch { changesFlow().collect(func) } }
|
||||
fun <T, R : Settings.Delegate<T>> R.observed(func: (Unit) -> Unit) = apply { collectScope.launch { flowGetter().collect(func) } }
|
||||
fun <T, R : Settings.Delegate<T>> R.emitTo(flow: MutableSharedFlow<Unit>) = apply { collectScope.launch { flowGetter().collect { flow.emit(Unit) } } }
|
||||
fun <T, R : PrefDelegate<T>> R.emitTo(flow: MutableSharedFlow<Unit>) = apply { collectScope.launch { changesFlow().collect { flow.emit(Unit) } } }
|
||||
private const val TAG = "SettingsCollector"
|
||||
|
||||
fun updateWhenKeepMediaStatusChanges() {
|
||||
collectScope.launchIO {
|
||||
runCatching {
|
||||
keepNoMediaFileStatus()
|
||||
private val collectScope = CoroutineScope(Dispatchers.Main + SupervisorJob())
|
||||
fun <T, R : PrefDelegate<T>> R.observed(func: suspend (T) -> Unit) = apply { collectScope.launch { valueFlow().drop(1).collectLatest(func) } }
|
||||
fun <T, R : Settings.Delegate<T>> R.emitTo(flow: MutableSharedFlow<Unit>) = apply { collectScope.launch { flow.emitAll(changesFlow()) } }
|
||||
fun <T, R : PrefDelegate<T>> R.emitTo(flow: MutableSharedFlow<Unit>) = apply { collectScope.launch { flow.emitAll(changesFlow()) } }
|
||||
|
||||
suspend fun updateWhenKeepMediaStatusChanges(mediaScan: Boolean) {
|
||||
withIOContext {
|
||||
catch {
|
||||
keepNoMediaFileStatus(mediaScan = mediaScan)
|
||||
}.onLeft {
|
||||
logcat(TAG, it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun updateWhenThemeChanges() {
|
||||
collectScope.launch {
|
||||
delay(100) // Avoid recompose being cancelled
|
||||
if (isAtLeastS) {
|
||||
val mode = when (Settings.theme) {
|
||||
AppCompatDelegate.MODE_NIGHT_NO -> UiModeManager.MODE_NIGHT_NO
|
||||
AppCompatDelegate.MODE_NIGHT_YES -> UiModeManager.MODE_NIGHT_YES
|
||||
else -> UiModeManager.MODE_NIGHT_AUTO
|
||||
}
|
||||
uiModeManager.setApplicationNightMode(mode)
|
||||
suspend fun updateWhenThemeChanges(theme: Int) {
|
||||
delay(100) // Avoid recompose being cancelled
|
||||
if (isAtLeastS) {
|
||||
val mode = when (theme) {
|
||||
AppCompatDelegate.MODE_NIGHT_NO -> UiModeManager.MODE_NIGHT_NO
|
||||
AppCompatDelegate.MODE_NIGHT_YES -> UiModeManager.MODE_NIGHT_YES
|
||||
else -> UiModeManager.MODE_NIGHT_AUTO
|
||||
}
|
||||
AppCompatDelegate.setDefaultNightMode(Settings.theme)
|
||||
uiModeManager.setApplicationNightMode(mode)
|
||||
}
|
||||
AppCompatDelegate.setDefaultNightMode(theme)
|
||||
}
|
||||
|
||||
fun updateWhenRequestNewsChanges() {
|
||||
updateDailyCheckWork(appCtx)
|
||||
}
|
||||
|
||||
fun updateWhenGallerySiteChanges() {
|
||||
suspend fun updateWhenGallerySiteChanges(gallerySite: Int) {
|
||||
if (Settings.hasSignedIn.value) {
|
||||
collectScope.launchIO {
|
||||
runCatching {
|
||||
EhEngine.getUConfig()
|
||||
}.onFailure {
|
||||
logcat(it)
|
||||
}
|
||||
catch {
|
||||
EhEngine.getUConfig(gallerySite)
|
||||
}.onLeft {
|
||||
logcat(TAG, it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun updateWhenTagTranslationChanges() {
|
||||
collectScope.launchIO {
|
||||
runCatching {
|
||||
EhTagDatabase.update()
|
||||
}.onFailure {
|
||||
logcat(it)
|
||||
}
|
||||
fun updateWhenTagTranslationChanges(enabled: Boolean) {
|
||||
if (enabled) {
|
||||
EhTagDatabase.launchUpdate()
|
||||
}
|
||||
}
|
||||
@@ -15,35 +15,38 @@
|
||||
*/
|
||||
package com.hippo.ehviewer.client
|
||||
|
||||
import com.hippo.ehviewer.client.data.GalleryInfo
|
||||
import com.hippo.ehviewer.client.data.GalleryPreview
|
||||
import com.hippo.ehviewer.client.data.NormalGalleryPreview
|
||||
import com.ehviewer.core.model.GalleryInfo
|
||||
|
||||
// Normal Preview: https://*.hath.network/cm/[timed token]/[gid]-[index].jpg
|
||||
// ExHentai Large Preview: https://s.exhentai.org/t/***
|
||||
// E-Hentai Large Preview: https://ehgt.org/***
|
||||
// Normal Preview (v2): https://*.hath.network/c(m|1|2)/[timed token]/[gid]-[index].(jpg|webp)
|
||||
// ExHentai Large Preview (v1 Cover): https://s.exhentai.org/t/***
|
||||
// E-Hentai Large Preview (v1 Cover): https://ehgt.org/***
|
||||
// ExHentai v2 Cover: https://s.exhentai.org/**.webp
|
||||
// E-Hentai v2 Cover: https://ehgt.org/**.webp
|
||||
|
||||
private const val URL_PREFIX_THUMB_E = "https://ehgt.org/"
|
||||
private const val URL_PREFIX_THUMB_EX = "https://s.exhentai.org/t/"
|
||||
private const val NORMAL_PREVIEW_PREFIX = "$"
|
||||
private val NormalPreviewKeyRegex = Regex("/(\\d+-\\d+)\\.jpg$")
|
||||
const val URL_PREFIX_THUMB_E = "https://ehgt.org/"
|
||||
const val URL_PREFIX_THUMB_EX = "https://s.exhentai.org/"
|
||||
private const val URL_PREFIX_V1_THUMB_EX = URL_PREFIX_THUMB_EX + "t/"
|
||||
private val V2PreviewKeyRegex = Regex("/c([m12]/)[^/]+/(\\d+-\\d+)")
|
||||
|
||||
fun getImageKey(gid: Long, index: Int): String = "image:$gid:$index"
|
||||
|
||||
fun getThumbKey(url: String): String = url.removePrefix(thumbPrefix)
|
||||
fun getThumbKey(url: String): String = url.removePrefix(URL_PREFIX_THUMB_E).removePrefix(URL_PREFIX_V1_THUMB_EX).removePrefix(URL_PREFIX_THUMB_EX)
|
||||
|
||||
val String.isNormalPreviewKey
|
||||
get() = startsWith(NORMAL_PREVIEW_PREFIX)
|
||||
|
||||
val GalleryPreview.imageKey
|
||||
get() = if (this is NormalGalleryPreview) {
|
||||
NormalPreviewKeyRegex.find(url)?.run { NORMAL_PREVIEW_PREFIX + groupValues[1] }
|
||||
} else {
|
||||
getThumbKey(url)
|
||||
}
|
||||
fun getV2PreviewKey(url: String) = "$".plus(
|
||||
V2PreviewKeyRegex.find(url)?.let {
|
||||
it.groupValues[1] + it.groupValues[2]
|
||||
} ?: url,
|
||||
)
|
||||
|
||||
val GalleryInfo.thumbUrl
|
||||
get() = thumbPrefix + EhUtils.handleThumbUrlResolution(thumbKey!!)
|
||||
get() = keyToUrl(thumbKey!!)
|
||||
|
||||
private val thumbPrefix
|
||||
get() = if (EhUtils.isExHentai) URL_PREFIX_THUMB_EX else URL_PREFIX_THUMB_E
|
||||
fun keyToUrl(key: String) = if (key.startsWith("https:")) {
|
||||
key
|
||||
} else {
|
||||
if (key.endsWith("webp")) {
|
||||
if (EhUtils.isExHentai) URL_PREFIX_THUMB_EX else URL_PREFIX_THUMB_E
|
||||
} else {
|
||||
if (EhUtils.isExHentai) URL_PREFIX_V1_THUMB_EX else URL_PREFIX_THUMB_E
|
||||
} + key
|
||||
}
|
||||
@@ -13,11 +13,12 @@ object EhCookieStore : CookiesStorage {
|
||||
|
||||
fun hasSignedIn(): Boolean = getCookies(EhUrl.HOST_E)?.run {
|
||||
containsKey(KEY_IPB_MEMBER_ID) && containsKey(KEY_IPB_PASS_HASH)
|
||||
} ?: false
|
||||
} == true
|
||||
|
||||
const val KEY_IPB_MEMBER_ID = "ipb_member_id"
|
||||
const val KEY_IPB_PASS_HASH = "ipb_pass_hash"
|
||||
const val KEY_IGNEOUS = "igneous"
|
||||
private const val KEY_HATH_PERKS = "hath_perks"
|
||||
private const val KEY_CONTENT_WARNING = "nw"
|
||||
private const val CONTENT_WARNING_NOT_SHOW = "1"
|
||||
private const val KEY_UTMP_NAME = "__utmp"
|
||||
@@ -33,6 +34,10 @@ object EhCookieStore : CookiesStorage {
|
||||
)
|
||||
}
|
||||
|
||||
fun getUserId() = getCookies(EhUrl.HOST_E)?.get(KEY_IPB_MEMBER_ID)
|
||||
|
||||
fun getHathPerks() = getCookies(EhUrl.HOST_E)?.get(KEY_HATH_PERKS)?.substringBefore('-')
|
||||
|
||||
fun getIdentityCookies(): List<Pair<String, String?>> {
|
||||
val eCookies = getCookies(EhUrl.HOST_E)
|
||||
val exCookies = getCookies(EhUrl.HOST_EX)
|
||||
@@ -46,9 +51,9 @@ object EhCookieStore : CookiesStorage {
|
||||
)
|
||||
}
|
||||
|
||||
fun flush() = manager.flush()
|
||||
fun isCloudflareBypassed() = getCookies(EhUrl.HOST_E)?.containsKey("cf_clearance") == true
|
||||
|
||||
fun getCookieHeader(url: String): String? = manager.getCookie(url)
|
||||
fun flush() = manager.flush()
|
||||
|
||||
// See https://github.com/Ehviewer-Overhauled/Ehviewer/issues/873
|
||||
override suspend fun addCookie(requestUrl: Url, cookie: Cookie) {
|
||||
|
||||
@@ -21,14 +21,21 @@ import arrow.core.partially2
|
||||
import arrow.core.right
|
||||
import arrow.fx.coroutines.parMap
|
||||
import arrow.fx.coroutines.parZip
|
||||
import com.ehviewer.core.i18n.R
|
||||
import com.ehviewer.core.model.BaseGalleryInfo
|
||||
import com.ehviewer.core.model.GalleryInfo
|
||||
import com.ehviewer.core.util.logcat
|
||||
import com.hippo.ehviewer.EhDB
|
||||
import com.hippo.ehviewer.R
|
||||
import com.hippo.ehviewer.Settings
|
||||
import com.hippo.ehviewer.client.data.BaseGalleryInfo
|
||||
import com.hippo.ehviewer.client.data.FavListUrlBuilder
|
||||
import com.hippo.ehviewer.client.data.GalleryInfo
|
||||
import com.hippo.ehviewer.client.data.fillInfo
|
||||
import com.hippo.ehviewer.client.data.filterComments
|
||||
import com.hippo.ehviewer.client.exception.CloudflareBypassException
|
||||
import com.hippo.ehviewer.client.exception.EhException
|
||||
import com.hippo.ehviewer.client.exception.InsufficientFundsException
|
||||
import com.hippo.ehviewer.client.exception.InsufficientGpException
|
||||
import com.hippo.ehviewer.client.exception.IpBannedException
|
||||
import com.hippo.ehviewer.client.exception.NoHathClientException
|
||||
import com.hippo.ehviewer.client.exception.NoHitsFoundException
|
||||
import com.hippo.ehviewer.client.exception.NotLoggedInException
|
||||
import com.hippo.ehviewer.client.exception.ParseException
|
||||
@@ -36,11 +43,10 @@ import com.hippo.ehviewer.client.parser.ArchiveParser
|
||||
import com.hippo.ehviewer.client.parser.EventPaneParser
|
||||
import com.hippo.ehviewer.client.parser.FavParserResult
|
||||
import com.hippo.ehviewer.client.parser.FavoritesParser
|
||||
import com.hippo.ehviewer.client.parser.ForumsParser
|
||||
import com.hippo.ehviewer.client.parser.GalleryApiParser
|
||||
import com.hippo.ehviewer.client.parser.GalleryDetailParser
|
||||
import com.hippo.ehviewer.client.parser.GalleryListParser
|
||||
import com.hippo.ehviewer.client.parser.GalleryNotAvailableParser
|
||||
import com.hippo.ehviewer.client.parser.GalleryMultiPageViewerPTokenParser
|
||||
import com.hippo.ehviewer.client.parser.GalleryPageParser
|
||||
import com.hippo.ehviewer.client.parser.GalleryTokenApiParser
|
||||
import com.hippo.ehviewer.client.parser.HomeParser
|
||||
@@ -58,10 +64,11 @@ import com.hippo.ehviewer.util.AppConfig
|
||||
import com.hippo.ehviewer.util.ReadableTime
|
||||
import com.hippo.ehviewer.util.bodyAsUtf8Text
|
||||
import com.hippo.ehviewer.util.ensureSuccess
|
||||
import eu.kanade.tachiyomi.util.system.logcat
|
||||
import io.ktor.client.statement.HttpResponse
|
||||
import io.ktor.client.statement.HttpStatement
|
||||
import io.ktor.client.statement.bodyAsChannel
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.client.statement.request
|
||||
import io.ktor.http.Url
|
||||
import io.ktor.utils.io.pool.DirectByteBufferPool
|
||||
import io.ktor.utils.io.pool.useInstance
|
||||
import io.ktor.utils.io.readAvailable
|
||||
@@ -79,7 +86,6 @@ import moe.tarsin.coroutines.removeAllSuspend
|
||||
import moe.tarsin.coroutines.runSuspendCatching
|
||||
import okio.buffer
|
||||
import okio.sink
|
||||
import org.jsoup.Jsoup
|
||||
import splitties.init.appCtx
|
||||
|
||||
// https://ehwiki.org/wiki/API#Basics
|
||||
@@ -87,7 +93,9 @@ private const val MAX_REQUEST_SIZE = 25
|
||||
private const val MAX_SEQUENTIAL_REQUESTS = 5
|
||||
private const val REQUEST_INTERVAL = 5000L
|
||||
|
||||
fun Either<String, ByteBuffer>.saveParseError(e: Throwable) {
|
||||
private val Url.isLogin get() = segments.firstOrNull() == "bounce_login.php"
|
||||
|
||||
private fun Either<String, ByteBuffer>.saveParseError(e: Throwable) {
|
||||
val dir = AppConfig.externalParseErrorDir ?: return
|
||||
val file = File(dir, ReadableTime.getFilenamableTime() + ".txt")
|
||||
file.sink().buffer().use { sink ->
|
||||
@@ -99,45 +107,50 @@ fun Either<String, ByteBuffer>.saveParseError(e: Throwable) {
|
||||
}
|
||||
}
|
||||
|
||||
fun rethrowExactly(status: HttpStatusCode, body: Either<String, ByteBuffer>, e: Throwable): Nothing {
|
||||
private fun rethrowExactly(response: HttpResponse, body: Either<String, ByteBuffer>, e: Throwable): Nothing {
|
||||
// Don't translate coroutine cancellation
|
||||
if (e is CancellationException) throw e
|
||||
|
||||
if (response.headers["cf-mitigated"] == "challenge") throw CloudflareBypassException()
|
||||
|
||||
// Check sad panda (without panda)
|
||||
val empty = body.fold(
|
||||
{ it.isEmpty() },
|
||||
{ !it.hasRemaining() },
|
||||
)
|
||||
if (empty) {
|
||||
if (EhUtils.isExHentai) {
|
||||
throw EhException("Sad Panda\n(without panda)")
|
||||
val message = if (EhUtils.isExHentai) {
|
||||
"Sad Panda\n(without panda)"
|
||||
} else {
|
||||
throw EhException("IP banned")
|
||||
appCtx.getString(R.string.error_ip_banned)
|
||||
}
|
||||
throw IpBannedException(message)
|
||||
}
|
||||
|
||||
// Check Gallery Not Available
|
||||
body.onLeft {
|
||||
if ("Gallery Not Available - " in it) {
|
||||
val error = GalleryNotAvailableParser.parse(it)
|
||||
if (!error.isNullOrBlank()) {
|
||||
throw EhException(error)
|
||||
// Check parse error thrown by rust
|
||||
val cause = e.cause
|
||||
if (e is ParseException && cause is RuntimeException) {
|
||||
when (val message = cause.message) {
|
||||
"0" -> throw NoHitsFoundException()
|
||||
"1" -> throw EhException(R.string.gallery_list_empty_hit_subscription)
|
||||
"2" -> throw NotLoggedInException()
|
||||
"3" -> throw NoHathClientException()
|
||||
"4" -> throw InsufficientFundsException()
|
||||
is String if message.isNotEmpty() -> {
|
||||
when (message[0]) {
|
||||
'5' -> throw IpBannedException(message.substring(1))
|
||||
'6' -> throw EhException(message.substring(1))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check bad response code
|
||||
status.ensureSuccess()
|
||||
response.status.ensureSuccess()
|
||||
|
||||
if (e is ParseException || e is SerializationException) {
|
||||
body.onLeft { if ("<" !in it) throw EhException(it) }
|
||||
when (val message = e.cause?.message) {
|
||||
"No hits found!" -> throw NoHitsFoundException()
|
||||
"No watched tags!" -> throw EhException(R.string.gallery_list_empty_hit_subscription)
|
||||
"Not logged in!" -> throw NotLoggedInException()
|
||||
is String -> if (message.startsWith("Your IP address")) throw EhException(message)
|
||||
}
|
||||
if (Settings.saveParseErrorBody) body.saveParseError(e)
|
||||
body.onLeft { if ("<" !in it) throw IpBannedException(it) }
|
||||
if (Settings.saveParseErrorBody.value) body.saveParseError(e)
|
||||
throw EhException(appCtx.getString(R.string.error_parse_error), e)
|
||||
}
|
||||
|
||||
@@ -145,18 +158,20 @@ fun rethrowExactly(status: HttpStatusCode, body: Either<String, ByteBuffer>, e:
|
||||
throw e
|
||||
}
|
||||
|
||||
val httpContentPool = DirectByteBufferPool(8, 0x80000)
|
||||
private val httpContentPool = DirectByteBufferPool(8, 0x80000)
|
||||
|
||||
suspend inline fun <T> HttpStatement.fetchUsingAsText(crossinline block: suspend String.() -> T) = executeSafely { response ->
|
||||
private suspend inline fun <T> HttpStatement.fetchUsingAsText(crossinline block: suspend String.() -> T) = executeSafely { response ->
|
||||
if (response.request.url.isLogin) throw NotLoggedInException()
|
||||
val body = response.bodyAsUtf8Text()
|
||||
runSuspendCatching {
|
||||
block(body)
|
||||
}.onFailure {
|
||||
rethrowExactly(response.status, body.left(), it)
|
||||
rethrowExactly(response, body.left(), it)
|
||||
}.getOrThrow()
|
||||
}
|
||||
|
||||
suspend inline fun <T> HttpStatement.fetchUsingAsByteBuffer(crossinline block: suspend ByteBuffer.() -> T) = executeSafely { response ->
|
||||
private suspend inline fun <T> HttpStatement.fetchUsingAsByteBuffer(crossinline block: suspend ByteBuffer.() -> T) = executeSafely { response ->
|
||||
if (response.request.url.isLogin) throw NotLoggedInException()
|
||||
httpContentPool.useInstance { buffer ->
|
||||
with(response.bodyAsChannel()) { while (!isClosedForRead) readAvailable(buffer) }
|
||||
buffer.flip()
|
||||
@@ -164,26 +179,35 @@ suspend inline fun <T> HttpStatement.fetchUsingAsByteBuffer(crossinline block: s
|
||||
block(buffer)
|
||||
}.onFailure {
|
||||
buffer.rewind()
|
||||
rethrowExactly(response.status, buffer.right(), it)
|
||||
rethrowExactly(response, buffer.right(), it)
|
||||
}.getOrThrow()
|
||||
}
|
||||
}
|
||||
|
||||
object EhEngine {
|
||||
suspend fun getOriginalImageUrl(url: String, referer: String?) = noRedirectEhRequest(url, referer).executeSafely { response ->
|
||||
val location = response.headers["Location"] ?: throw InsufficientFundsException()
|
||||
location.takeIf { "bounce_login" !in it } ?: throw NotLoggedInException()
|
||||
response.headers["Location"]?.takeUnless { Url(it).isLogin } ?: throw InsufficientGpException()
|
||||
}
|
||||
|
||||
suspend fun getTorrentList(url: String, gid: Long, token: String): TorrentResult {
|
||||
suspend fun getPTokenListFromMpv(gid: Long, token: String) = ehRequest(EhUrl.getGalleryMultiPageViewerUrl(gid, token), EhUrl.referer)
|
||||
.fetchUsingAsText(GalleryMultiPageViewerPTokenParser::parse)
|
||||
|
||||
suspend fun getTorrentList(gid: Long, token: String): TorrentResult {
|
||||
val url = EhUrl.getTorrentUrl(gid, token)
|
||||
val referer = EhUrl.getGalleryDetailUrl(gid, token)
|
||||
return ehRequest(url, referer).fetchUsingAsByteBuffer(TorrentParser::parse)
|
||||
}
|
||||
|
||||
suspend fun getArchiveList(url: String, gid: Long, token: String): ArchiveParser.Result {
|
||||
suspend fun getTorrentKey() = EhCookieStore.getUserId()?.let { id ->
|
||||
val key = ehRequest(EhUrl.URL_HOME).fetchUsingAsText(HomeParser::parseTorrentKey)
|
||||
"${id}x$key"
|
||||
}
|
||||
|
||||
suspend fun getArchiveList(gid: Long, token: String): ArchiveParser.Result {
|
||||
val url = EhUrl.getArchiveUrl(gid, token)
|
||||
val funds = if (EhUtils.isExHentai) getFunds() else null
|
||||
return ehRequest(url, EhUrl.getGalleryDetailUrl(gid, token))
|
||||
.fetchUsingAsText(ArchiveParser::parse.partially2(funds))
|
||||
.fetchUsingAsByteBuffer(ArchiveParser::parse.partially2(funds))
|
||||
}
|
||||
|
||||
suspend fun getImageLimits() = parZip(
|
||||
@@ -195,14 +219,15 @@ object EhEngine {
|
||||
private suspend fun getFunds() = ehRequest(EhUrl.URL_FUNDS).fetchUsingAsText(HomeParser::parseFunds)
|
||||
|
||||
suspend fun getNews(parse: Boolean) = ehRequest(EhUrl.URL_NEWS, EhUrl.REFERER_E)
|
||||
.fetchUsingAsText { if (parse) EventPaneParser.parse(this) else null }
|
||||
.fetchUsingAsByteBuffer { if (parse) EventPaneParser.parse(this) else null }
|
||||
|
||||
suspend fun getProfile(): ProfileParser.Result {
|
||||
val url = ehRequest(EhUrl.URL_FORUMS).fetchUsingAsText(ForumsParser::parse)
|
||||
return ehRequest(url, EhUrl.URL_FORUMS).fetchUsingAsText(ProfileParser::parse)
|
||||
val url = ehRequest(EhUrl.URL_FORUMS).fetchUsingAsByteBuffer(ProfileParser::parseProfileUrl)
|
||||
return ehRequest(url, EhUrl.URL_FORUMS).fetchUsingAsByteBuffer(ProfileParser::parse)
|
||||
}
|
||||
|
||||
suspend fun getUConfig(url: String = EhUrl.uConfigUrl) {
|
||||
suspend fun getUConfig(gallerySite: Int) {
|
||||
val url = EhUrl.getUConfigUrl(gallerySite)
|
||||
runSuspendCatching {
|
||||
ehRequest(url).fetchUsingAsByteBuffer(UserConfigParser::parse)
|
||||
}.onFailure { throwable ->
|
||||
@@ -225,18 +250,19 @@ object EhEngine {
|
||||
.apply { galleryInfoList.fillInfo(url, true) }
|
||||
.takeUnless { it.galleryInfoList.isEmpty() } ?: GalleryListParser.emptyResult
|
||||
|
||||
suspend fun getGalleryDetail(url: String) = ehRequest(url, EhUrl.referer).fetchUsingAsText {
|
||||
val eventPane = EventPaneParser.parse(this)
|
||||
if (eventPane != null) {
|
||||
suspend fun getGalleryDetail(url: String) = ehRequest(url, EhUrl.referer).fetchUsingAsByteBuffer(GalleryDetailParser::parse).run {
|
||||
event?.let {
|
||||
Settings.lastDawnDays = today
|
||||
showEventNotification(eventPane)
|
||||
showEventNotification(it)
|
||||
}
|
||||
detail.apply {
|
||||
// Fill info for database
|
||||
fillInfo()
|
||||
filterComments()
|
||||
}
|
||||
GalleryDetailParser.parse(this)
|
||||
}
|
||||
|
||||
suspend fun getPreviewList(url: String) = ehRequest(url, EhUrl.referer).fetchUsingAsText {
|
||||
GalleryDetailParser.parsePreviewList(this)
|
||||
}
|
||||
suspend fun getPreviewList(url: String) = ehRequest(url, EhUrl.referer).fetchUsingAsByteBuffer(GalleryDetailParser::parsePreviews)
|
||||
|
||||
suspend fun getFavorites(url: String) = ehRequest(url, EhUrl.referer).fetchUsingAsByteBuffer(FavoritesParser::parse)
|
||||
.apply { galleryInfoList.fillInfo(url) }
|
||||
@@ -270,14 +296,7 @@ object EhEngine {
|
||||
// Ktor does not handle POST redirect, we need to do it manually
|
||||
// https://youtrack.jetbrains.com/issue/KTOR-478
|
||||
val location = response.headers["Location"] ?: url
|
||||
ehRequest(location, url).fetchUsingAsText {
|
||||
val document = Jsoup.parse(this)
|
||||
val elements = document.select("#chd + p")
|
||||
if (elements.size > 0) {
|
||||
throw EhException(elements[0].text())
|
||||
}
|
||||
GalleryDetailParser.parseComments(document)
|
||||
}
|
||||
ehRequest(location, url).fetchUsingAsByteBuffer(GalleryDetailParser::parseComments)
|
||||
}
|
||||
|
||||
suspend fun modifyFavorites(gid: Long, token: String, dstCat: Int = -1, note: String = "") {
|
||||
@@ -298,16 +317,15 @@ object EhEngine {
|
||||
}.executeSafely { }
|
||||
}
|
||||
|
||||
suspend fun getFavoriteNote(gid: Long, token: String) =
|
||||
ehRequest(EhUrl.getAddFavorites(gid, token), EhUrl.getGalleryDetailUrl(gid, token))
|
||||
.fetchUsingAsText(FavoritesParser::parseNote)
|
||||
suspend fun getFavoriteNote(gid: Long, token: String) = ehRequest(EhUrl.getAddFavorites(gid, token), EhUrl.getGalleryDetailUrl(gid, token))
|
||||
.fetchUsingAsText(FavoritesParser::parseNote)
|
||||
|
||||
suspend fun downloadArchive(gid: Long, token: String, or: String, res: String, isHAtH: Boolean): String? {
|
||||
val url = EhUrl.getDownloadArchive(gid, token, or)
|
||||
suspend fun downloadArchive(gid: Long, token: String, res: String, isHath: Boolean): String? {
|
||||
val url = EhUrl.getArchiveUrl(gid, token)
|
||||
val referer = EhUrl.getGalleryDetailUrl(gid, token)
|
||||
val request = ehRequest(url, referer, EhUrl.origin) {
|
||||
formBody {
|
||||
if (isHAtH) {
|
||||
if (isHath) {
|
||||
append("hathdl_xres", res)
|
||||
} else {
|
||||
append("dltype", res)
|
||||
@@ -319,12 +337,12 @@ object EhEngine {
|
||||
}
|
||||
}
|
||||
}
|
||||
var result = request.fetchUsingAsText(ArchiveParser::parseArchiveUrl)
|
||||
if (!isHAtH) {
|
||||
var result = request.fetchUsingAsByteBuffer(ArchiveParser::parseArchiveUrl)
|
||||
if (!isHath) {
|
||||
if (result == null) {
|
||||
// Wait for the server to prepare archives
|
||||
delay(1000)
|
||||
result = request.fetchUsingAsText(ArchiveParser::parseArchiveUrl)
|
||||
result = request.fetchUsingAsByteBuffer(ArchiveParser::parseArchiveUrl)
|
||||
if (result == null) throw EhException("Archive unavailable")
|
||||
}
|
||||
return result
|
||||
@@ -379,54 +397,51 @@ object EhEngine {
|
||||
}
|
||||
}.fetchUsingAsText(String::parseAs)
|
||||
|
||||
suspend fun fillGalleryListByApi(galleryInfoList: List<GalleryInfo>, referer: String? = null) =
|
||||
galleryInfoList.chunked(MAX_REQUEST_SIZE).chunked(MAX_SEQUENTIAL_REQUESTS).forEachIndexed { index, chunk ->
|
||||
if (index != 0) {
|
||||
delay(REQUEST_INTERVAL)
|
||||
}
|
||||
chunk.parMap {
|
||||
ehRequest(EhUrl.apiUrl, referer, EhUrl.origin) {
|
||||
jsonBody {
|
||||
put("method", "gdata")
|
||||
array("gidlist") {
|
||||
it.forEach {
|
||||
addJsonArray {
|
||||
add(it.gid)
|
||||
add(it.token)
|
||||
}
|
||||
suspend fun fillGalleryListByApi(galleryInfoList: List<GalleryInfo>, referer: String? = null) = galleryInfoList.chunked(MAX_REQUEST_SIZE).chunked(MAX_SEQUENTIAL_REQUESTS).forEachIndexed { index, chunk ->
|
||||
if (index != 0) {
|
||||
delay(REQUEST_INTERVAL)
|
||||
}
|
||||
chunk.parMap {
|
||||
ehRequest(EhUrl.apiUrl, referer, EhUrl.origin) {
|
||||
jsonBody {
|
||||
put("method", "gdata")
|
||||
array("gidlist") {
|
||||
it.forEach { info ->
|
||||
addJsonArray {
|
||||
add(info.gid)
|
||||
add(info.token)
|
||||
}
|
||||
}
|
||||
put("namespace", 1)
|
||||
}
|
||||
}.fetchUsingAsText { GalleryApiParser.parse(this, it) }
|
||||
}
|
||||
put("namespace", 1)
|
||||
}
|
||||
}.fetchUsingAsText { GalleryApiParser.parse(this, it) }
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun voteComment(apiUid: Long, apiKey: String?, gid: Long, token: String, commentId: Long, commentVote: Int): VoteCommentResult =
|
||||
ehRequest(EhUrl.apiUrl, EhUrl.referer, EhUrl.origin) {
|
||||
jsonBody {
|
||||
put("method", "votecomment")
|
||||
put("apiuid", apiUid)
|
||||
put("apikey", requireNotNull(apiKey))
|
||||
put("gid", gid)
|
||||
put("token", token)
|
||||
put("comment_id", commentId)
|
||||
put("comment_vote", commentVote)
|
||||
}
|
||||
}.fetchUsingAsText(String::parseAs)
|
||||
suspend fun voteComment(apiUid: Long, apiKey: String?, gid: Long, token: String, commentId: Long, commentVote: Int): VoteCommentResult = ehRequest(EhUrl.apiUrl, EhUrl.referer, EhUrl.origin) {
|
||||
jsonBody {
|
||||
put("method", "votecomment")
|
||||
put("apiuid", apiUid)
|
||||
put("apikey", requireNotNull(apiKey))
|
||||
put("gid", gid)
|
||||
put("token", token)
|
||||
put("comment_id", commentId)
|
||||
put("comment_vote", commentVote)
|
||||
}
|
||||
}.fetchUsingAsText(String::parseAs)
|
||||
|
||||
suspend fun voteTag(apiUid: Long, apiKey: String?, gid: Long, token: String, tags: String, vote: Int) =
|
||||
ehRequest(EhUrl.apiUrl, EhUrl.referer, EhUrl.origin) {
|
||||
jsonBody {
|
||||
put("method", "taggallery")
|
||||
put("apiuid", apiUid)
|
||||
put("apikey", requireNotNull(apiKey))
|
||||
put("gid", gid)
|
||||
put("token", token)
|
||||
put("tags", tags)
|
||||
put("vote", vote)
|
||||
}
|
||||
}.fetchUsingAsText(VoteTagParser::parse)
|
||||
suspend fun voteTag(apiUid: Long, apiKey: String?, gid: Long, token: String, tags: String, vote: Int) = ehRequest(EhUrl.apiUrl, EhUrl.referer, EhUrl.origin) {
|
||||
jsonBody {
|
||||
put("method", "taggallery")
|
||||
put("apiuid", apiUid)
|
||||
put("apikey", requireNotNull(apiKey))
|
||||
put("gid", gid)
|
||||
put("token", token)
|
||||
put("tags", tags)
|
||||
put("vote", vote)
|
||||
}
|
||||
}.fetchUsingAsByteBuffer(VoteTagParser::parse)
|
||||
|
||||
suspend fun getGalleryToken(gid: Long, gtoken: String, page: Int) = ehRequest(EhUrl.apiUrl, EhUrl.referer, EhUrl.origin) {
|
||||
jsonBody {
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package com.hippo.ehviewer.client
|
||||
|
||||
import arrow.core.memoize
|
||||
import com.ehviewer.core.database.model.Filter
|
||||
import com.ehviewer.core.database.model.FilterMode
|
||||
import com.ehviewer.core.model.GalleryInfo
|
||||
import com.hippo.ehviewer.EhDB
|
||||
import com.hippo.ehviewer.client.data.GalleryInfo
|
||||
import com.hippo.ehviewer.dao.Filter
|
||||
import com.hippo.ehviewer.dao.FilterMode
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.async
|
||||
@@ -55,8 +55,8 @@ object EhFilter : CoroutineScope {
|
||||
suspend fun needTags() = filters.await().any { it.enable && (it.mode == FilterMode.TAG || it.mode == FilterMode.TAG_NAMESPACE) }
|
||||
suspend fun filterTitle(info: GalleryInfo) = anyActive(FilterMode.TITLE) { info.title.orEmpty().contains(it.text, true) }
|
||||
suspend fun filterUploader(info: GalleryInfo) = anyActive(FilterMode.UPLOADER) { it.text == info.uploader }
|
||||
suspend fun filterTag(info: GalleryInfo) = info.simpleTags?.any { tag -> anyActive(FilterMode.TAG) { matchTag(tag, it.text.lowercase()) } } ?: false
|
||||
suspend fun filterTagNamespace(info: GalleryInfo) = info.simpleTags?.any { tag -> anyActive(FilterMode.TAG_NAMESPACE) { matchTagNamespace(tag, it.text.lowercase()) } } ?: false
|
||||
suspend fun filterTag(info: GalleryInfo) = info.simpleTags?.any { tag -> anyActive(FilterMode.TAG) { matchTag(tag, it.text.lowercase()) } } == true
|
||||
suspend fun filterTagNamespace(info: GalleryInfo) = info.simpleTags?.any { tag -> anyActive(FilterMode.TAG_NAMESPACE) { matchTagNamespace(tag, it.text.lowercase()) } } == true
|
||||
suspend fun filterCommenter(commenter: String) = anyActive(FilterMode.COMMENTER) { it.text == commenter }
|
||||
suspend fun filterComment(comment: String) = anyActive(FilterMode.COMMENT) { regex(it).containsMatchIn(comment) }
|
||||
}
|
||||
@@ -15,13 +15,11 @@
|
||||
*/
|
||||
package com.hippo.ehviewer.client
|
||||
|
||||
import com.hippo.ehviewer.BuildConfig
|
||||
import com.ehviewer.core.util.logcat
|
||||
import com.hippo.ehviewer.EhApplication.Companion.ktorClient
|
||||
import com.hippo.ehviewer.EhApplication.Companion.noRedirectKtorClient
|
||||
import com.hippo.ehviewer.Settings
|
||||
import com.hippo.ehviewer.util.bodyAsUtf8Text
|
||||
import com.hippo.ehviewer.util.ensureSuccess
|
||||
import eu.kanade.tachiyomi.util.system.logcat
|
||||
import io.ktor.client.request.HttpRequestBuilder
|
||||
import io.ktor.client.request.forms.FormDataContent
|
||||
import io.ktor.client.request.header
|
||||
@@ -30,19 +28,17 @@ import io.ktor.client.request.setBody
|
||||
import io.ktor.client.statement.HttpResponse
|
||||
import io.ktor.client.statement.HttpStatement
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.http.HttpHeaders
|
||||
import io.ktor.http.HttpMethod
|
||||
import io.ktor.http.ParametersBuilder
|
||||
import io.ktor.http.content.TextContent
|
||||
import io.ktor.http.userAgent
|
||||
import kotlinx.coroutines.cancel
|
||||
import kotlinx.io.Source
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.JsonArrayBuilder
|
||||
import kotlinx.serialization.json.JsonObjectBuilder
|
||||
import kotlinx.serialization.json.buildJsonArray
|
||||
import kotlinx.serialization.json.buildJsonObject
|
||||
import kotlinx.serialization.json.okio.decodeFromBufferedSource
|
||||
import okio.BufferedSource
|
||||
import kotlinx.serialization.json.io.decodeFromSource
|
||||
|
||||
inline fun JsonObjectBuilder.array(name: String, builder: JsonArrayBuilder.() -> Unit) = put(name, buildJsonArray(builder))
|
||||
|
||||
@@ -51,23 +47,17 @@ suspend inline fun <reified T> HttpStatement.executeAndParseAs() = executeSafely
|
||||
it.bodyAsUtf8Text().parseAs<T>()
|
||||
}
|
||||
|
||||
inline fun <reified T> BufferedSource.parseAs(): T = json.decodeFromBufferedSource(this)
|
||||
inline fun <reified T> Source.parseAs(): T = json.decodeFromSource(this)
|
||||
inline fun <reified T> String.parseAs(): T = json.decodeFromString(this)
|
||||
|
||||
val json = Json { ignoreUnknownKeys = true }
|
||||
|
||||
const val CHROME_USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${BuildConfig.CHROME_VERSION}.0.0.0 Safari/537.36"
|
||||
const val CHROME_MOBILE_USER_AGENT = "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${BuildConfig.CHROME_VERSION}.0.0.0 Mobile Safari/537.36"
|
||||
const val CHROME_ACCEPT = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
|
||||
const val CHROME_ACCEPT_LANGUAGE = "en-US,en;q=0.5"
|
||||
val json = Json {
|
||||
ignoreUnknownKeys = true
|
||||
explicitNulls = false
|
||||
}
|
||||
|
||||
fun HttpRequestBuilder.applyEhConfig(referer: String?, origin: String?) {
|
||||
method = HttpMethod.Get
|
||||
header("Referer", referer)
|
||||
header("Origin", origin)
|
||||
userAgent(Settings.userAgent)
|
||||
header(HttpHeaders.Accept, CHROME_ACCEPT)
|
||||
header(HttpHeaders.AcceptLanguage, CHROME_ACCEPT_LANGUAGE)
|
||||
}
|
||||
|
||||
suspend inline fun ehRequest(
|
||||
|
||||
@@ -19,36 +19,39 @@ import android.content.Context
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.setValue
|
||||
import arrow.core.Either
|
||||
import com.ehviewer.core.files.metadataOrNull
|
||||
import com.ehviewer.core.files.read
|
||||
import com.ehviewer.core.files.write
|
||||
import com.ehviewer.core.model.TagNamespace
|
||||
import com.ehviewer.core.util.logcat
|
||||
import com.hippo.ehviewer.EhApplication.Companion.ktorClient
|
||||
import com.hippo.ehviewer.R
|
||||
import com.hippo.ehviewer.client.data.TagNamespace
|
||||
import com.hippo.ehviewer.util.AppConfig
|
||||
import com.hippo.ehviewer.util.FileUtils
|
||||
import com.hippo.ehviewer.util.copyTo
|
||||
import com.hippo.ehviewer.util.bodyAsUtf8Text
|
||||
import com.hippo.ehviewer.util.ensureSuccess
|
||||
import com.hippo.ehviewer.util.sha1
|
||||
import eu.kanade.tachiyomi.util.system.logcat
|
||||
import io.ktor.client.HttpClient
|
||||
import com.hippo.ehviewer.util.utf8
|
||||
import io.ktor.client.request.prepareGet
|
||||
import io.ktor.client.statement.bodyAsChannel
|
||||
import java.io.File
|
||||
import java.nio.charset.StandardCharsets
|
||||
import kotlin.time.Clock
|
||||
import kotlin.time.Duration.Companion.days
|
||||
import kotlin.time.Instant
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import kotlinx.io.Source
|
||||
import kotlinx.io.writeString
|
||||
import kotlinx.serialization.json.io.encodeToSink
|
||||
import kotlinx.serialization.json.jsonArray
|
||||
import kotlinx.serialization.json.jsonObject
|
||||
import kotlinx.serialization.json.jsonPrimitive
|
||||
import moe.tarsin.coroutines.runSuspendCatching
|
||||
import okio.BufferedSource
|
||||
import okio.Path.Companion.toOkioPath
|
||||
import okio.buffer
|
||||
import okio.source
|
||||
import splitties.init.appCtx
|
||||
|
||||
object EhTagDatabase : CoroutineScope {
|
||||
override val coroutineContext = Dispatchers.IO + Job()
|
||||
private const val NAMESPACE_PREFIX = "n"
|
||||
private val PREFIXES = arrayOf(
|
||||
"-",
|
||||
@@ -58,181 +61,140 @@ object EhTagDatabase : CoroutineScope {
|
||||
)
|
||||
private lateinit var tagGroups: Map<String, Map<String, String>>
|
||||
private val updateLock = Mutex()
|
||||
override val coroutineContext = Dispatchers.IO + Job()
|
||||
|
||||
var initialized by mutableStateOf(false)
|
||||
|
||||
fun getTranslation(prefix: String? = NAMESPACE_PREFIX, tag: String?): String? = tagGroups[prefix]?.get(tag)?.trim()?.ifEmpty { null }
|
||||
|
||||
private fun internalSuggestFlow(
|
||||
tags: Map<String, String>,
|
||||
keyword: String,
|
||||
translate: Boolean,
|
||||
exactly: Boolean,
|
||||
): Flow<Pair<String?, String>> = flow {
|
||||
if (exactly) {
|
||||
tags[keyword]?.let {
|
||||
emit(Pair(it.takeIf { translate }, keyword))
|
||||
}
|
||||
// Copied from https://github.com/EhTagTranslation/EhSyringe/blob/c0cf88da04081072492829673aca34380b14dae6/src/plugin/suggest.ts#L37
|
||||
// TODO: Optimize
|
||||
private val nsScore = mapOf(
|
||||
TagNamespace.Location.prefix to 10f,
|
||||
TagNamespace.Other.prefix to 10f,
|
||||
TagNamespace.Female.prefix to 9f,
|
||||
TagNamespace.Male.prefix to 8.5f,
|
||||
TagNamespace.Mixed.prefix to 8f,
|
||||
TagNamespace.Parody.prefix to 3.3f,
|
||||
TagNamespace.Character.prefix to 2.8f,
|
||||
TagNamespace.Artist.prefix to 2.5f,
|
||||
TagNamespace.Cosplayer.prefix to 2.4f,
|
||||
TagNamespace.Group.prefix to 2.2f,
|
||||
TagNamespace.Language.prefix to 2f,
|
||||
)
|
||||
|
||||
data class Tag(val tag: String, val hint: String?, val score: Float)
|
||||
|
||||
context(_: Context)
|
||||
fun suggestion(rawKeyword: String, expectTranslate: Boolean): List<Tag> {
|
||||
val keyword = PREFIXES.fold(rawKeyword) { kwd, pfx -> kwd.removePrefix(pfx) }
|
||||
if (!initialized || keyword.isEmpty()) return emptyList()
|
||||
val translate = expectTranslate && translatable
|
||||
val prefix = rawKeyword.dropLast(keyword.length)
|
||||
val ns = keyword.substringBefore(':')
|
||||
val tag = keyword.drop(ns.length + 1)
|
||||
val nsPrefix = TagNamespace.from(ns)?.prefix ?: ns
|
||||
val tags = tagGroups[nsPrefix.takeIf { tag.isNotEmpty() && it != NAMESPACE_PREFIX }]
|
||||
return if (tags != null) {
|
||||
lookup(tags, tag, translate).map { it.copy(tag = "$prefix$nsPrefix:${it.tag}") }
|
||||
} else {
|
||||
if (translate) {
|
||||
tags.forEach { (tag, hint) ->
|
||||
if (tag != keyword &&
|
||||
(tag.containsIgnoreSpace(keyword) || hint.containsIgnoreSpace(keyword))
|
||||
) {
|
||||
emit(Pair(hint, tag))
|
||||
}
|
||||
tagGroups.asSequence().flatMap { (nsPrefix, tags) ->
|
||||
if (nsPrefix != NAMESPACE_PREFIX) {
|
||||
lookup(tags, keyword, translate).map { it.copy(tag = "$prefix$nsPrefix:${it.tag}", score = it.score / (nsScore[nsPrefix] ?: 1f)) }
|
||||
} else {
|
||||
lookup(tags, keyword, translate).map { it.copy(tag = "$prefix${it.tag}:", score = 0f) }
|
||||
}
|
||||
}
|
||||
}.toMutableList().apply { sortBy { it.score } }
|
||||
}
|
||||
|
||||
private fun lookup(tags: Map<String, String>, keyword: String, translate: Boolean) = if (translate) {
|
||||
tags.asSequence().mapNotNull { (tag, hint) ->
|
||||
val score = if (tag.startsWith(keyword, ignoreCase = true)) {
|
||||
tag.length
|
||||
} else {
|
||||
tags.keys.forEach { tag ->
|
||||
if (tag != keyword && tag.containsIgnoreSpace(keyword)) {
|
||||
emit(Pair(null, tag))
|
||||
if (tag.indexOf(" $keyword", ignoreCase = true) != -1) {
|
||||
tag.length * 2
|
||||
} else {
|
||||
when (hint.indexOf(keyword, ignoreCase = true)) {
|
||||
-1 -> 0
|
||||
0 -> hint.length
|
||||
else -> hint.length * 2
|
||||
}
|
||||
}
|
||||
}
|
||||
if (score != 0) Tag(tag, hint, score.toFloat()) else null
|
||||
}
|
||||
}
|
||||
|
||||
// Construct a cold flow for tag database suggestions
|
||||
fun suggestFlow(
|
||||
keyword: String,
|
||||
translate: Boolean,
|
||||
exactly: Boolean = false,
|
||||
): Flow<Pair<String?, String>> = flow {
|
||||
var mKeyword = keyword
|
||||
PREFIXES.forEach { mKeyword = mKeyword.removePrefix(it) }
|
||||
val prefix = keyword.dropLast(mKeyword.length)
|
||||
val namespace = mKeyword.substringBefore(':')
|
||||
val tagKeyword = mKeyword.drop(namespace.length + 1)
|
||||
val namespacePrefix = TagNamespace(namespace).toPrefix() ?: namespace
|
||||
val tags = tagGroups[namespacePrefix.takeIf { tagKeyword.isNotEmpty() && it != NAMESPACE_PREFIX }]
|
||||
tags?.let {
|
||||
internalSuggestFlow(it, tagKeyword, translate, exactly).collect { (hint, tag) ->
|
||||
emit(Pair(hint, "$prefix$namespacePrefix:$tag"))
|
||||
}
|
||||
} ?: tagGroups.forEach { (namespacePrefix, tags) ->
|
||||
if (namespacePrefix != NAMESPACE_PREFIX) {
|
||||
internalSuggestFlow(tags, mKeyword, translate, exactly).collect { (hint, tag) ->
|
||||
emit(Pair(hint, "$prefix$namespacePrefix:$tag"))
|
||||
}
|
||||
} else {
|
||||
tags.keys.asSequence().mapNotNull { tag ->
|
||||
val score = if (tag.startsWith(keyword, ignoreCase = true)) {
|
||||
tag.length
|
||||
} else {
|
||||
internalSuggestFlow(tags, mKeyword, translate, exactly).collect { (hint, namespacePrefix) ->
|
||||
emit(Pair(hint, "$prefix$namespacePrefix:"))
|
||||
if (tag.indexOf(" $keyword", ignoreCase = true) != -1) {
|
||||
tag.length * 2
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
if (score != 0) Tag(tag, null, score.toFloat()) else null
|
||||
}
|
||||
}
|
||||
|
||||
private fun String.removeSpace(): String = replace(" ", "")
|
||||
private fun metadata(context: Context): Array<String> = context.resources.getStringArray(R.array.tag_translation_metadata)
|
||||
|
||||
private fun String.containsIgnoreSpace(other: String, ignoreCase: Boolean = true): Boolean =
|
||||
removeSpace().contains(other.removeSpace(), ignoreCase)
|
||||
context(ctx: Context)
|
||||
val translatable
|
||||
get() = ctx.resources.getBoolean(R.bool.tag_translatable)
|
||||
|
||||
private fun getMetadata(context: Context): Array<String>? = context.resources.getStringArray(R.array.tag_translation_metadata)
|
||||
.takeIf { it.size == 4 }
|
||||
|
||||
fun isTranslatable(context: Context): Boolean = context.resources.getBoolean(R.bool.tag_translatable)
|
||||
|
||||
private fun getFileContent(file: File): String? = runCatching {
|
||||
file.source().buffer().use { it.readString(StandardCharsets.UTF_8) }
|
||||
}.getOrNull()
|
||||
|
||||
private fun checkData(sha1: String?, data: File): Boolean = sha1 != null && sha1 == data.toOkioPath().sha1()
|
||||
|
||||
private suspend fun save(client: HttpClient, url: String, file: File) {
|
||||
runCatching {
|
||||
client.prepareGet(url).executeSafely {
|
||||
it.status.ensureSuccess()
|
||||
it.bodyAsChannel().copyTo(file)
|
||||
}
|
||||
}.onFailure {
|
||||
file.delete()
|
||||
}.getOrThrow()
|
||||
private suspend fun fetch(url: String) = ktorClient.prepareGet(url).executeSafely {
|
||||
it.status.ensureSuccess()
|
||||
it.bodyAsUtf8Text()
|
||||
}
|
||||
|
||||
suspend fun update() {
|
||||
fun launchUpdate() = launch {
|
||||
updateLock.withLock {
|
||||
updateInternal()
|
||||
}
|
||||
}
|
||||
|
||||
private fun issueUpdateInMemoryData(file: File? = null) {
|
||||
val dataFile = file ?: getMetadata(appCtx)?.let { metadata ->
|
||||
val dataName = metadata[2]
|
||||
val dir = AppConfig.getFilesDir("tag-translations")
|
||||
File(dir, dataName).takeIf { it.exists() }
|
||||
} ?: return
|
||||
tagGroups = dataFile.source().buffer().use(BufferedSource::parseAs)
|
||||
initialized = true
|
||||
}
|
||||
|
||||
init {
|
||||
launch {
|
||||
updateLock.withLock {
|
||||
runSuspendCatching {
|
||||
issueUpdateInMemoryData()
|
||||
}.onFailure {
|
||||
logcat(it)
|
||||
}
|
||||
updateInternal()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun updateInternal() {
|
||||
getMetadata(appCtx)?.let { metadata ->
|
||||
val sha1Name = metadata[0]
|
||||
val sha1Url = metadata[1]
|
||||
val dataName = metadata[2]
|
||||
val dataUrl = metadata[3]
|
||||
|
||||
val dir = AppConfig.getFilesDir("tag-translations")
|
||||
checkNotNull(dir)
|
||||
val sha1File = File(dir, sha1Name)
|
||||
val dataFile = File(dir, dataName)
|
||||
|
||||
runSuspendCatching {
|
||||
// Check current sha1 and current data
|
||||
val sha1 = getFileContent(sha1File)
|
||||
if (!checkData(sha1, dataFile)) {
|
||||
FileUtils.delete(sha1File)
|
||||
FileUtils.delete(dataFile)
|
||||
}
|
||||
|
||||
// Save new sha1
|
||||
val tempSha1File = File(dir, "$sha1Name.tmp")
|
||||
save(ktorClient, sha1Url, tempSha1File)
|
||||
val tempSha1 = getFileContent(tempSha1File)
|
||||
|
||||
// Check new sha1 and current sha1
|
||||
if (tempSha1 == sha1) {
|
||||
// The data is the same
|
||||
FileUtils.delete(tempSha1File)
|
||||
return
|
||||
}
|
||||
|
||||
// Save new data
|
||||
val tempDataFile = File(dir, "$dataName.tmp")
|
||||
save(ktorClient, dataUrl, tempDataFile)
|
||||
|
||||
// Check new sha1 and new data
|
||||
if (!checkData(tempSha1, tempDataFile)) {
|
||||
FileUtils.delete(tempSha1File)
|
||||
FileUtils.delete(tempDataFile)
|
||||
return
|
||||
}
|
||||
|
||||
// Replace current sha1 and current data with new sha1 and new data
|
||||
FileUtils.delete(sha1File)
|
||||
FileUtils.delete(dataFile)
|
||||
tempSha1File.renameTo(sha1File)
|
||||
tempDataFile.renameTo(dataFile)
|
||||
|
||||
// Read new EhTagDatabase
|
||||
issueUpdateInMemoryData(dataFile)
|
||||
atomicallyUpdate()
|
||||
}.onFailure {
|
||||
logcat(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun atomicallyUpdate() {
|
||||
val (sha1Name, sha1Url, dataName, dataUrl) = metadata(appCtx)
|
||||
val workdir = AppConfig.tagTranslationsDir
|
||||
val sha1File = workdir / sha1Name
|
||||
val dataFile = workdir / dataName
|
||||
|
||||
if (!initialized) {
|
||||
Either.catch {
|
||||
tagGroups = dataFile.read(Source::parseAs)
|
||||
initialized = true
|
||||
|
||||
// Perform only one automatic update (on app startup) per day
|
||||
val lastModified = Instant.fromEpochMilliseconds(dataFile.metadataOrNull()!!.lastModifiedAtMillis!!)
|
||||
if (Clock.System.now() - lastModified < 1.days) return
|
||||
}.onLeft {
|
||||
logcat(it)
|
||||
}
|
||||
}
|
||||
|
||||
val currentSha1 = if (initialized) {
|
||||
Either.catch { sha1File.utf8() }.getOrNull()
|
||||
} else {
|
||||
null
|
||||
}
|
||||
val newSha1 = fetch(sha1Url)
|
||||
if (newSha1 == currentSha1) return
|
||||
|
||||
val rawData = fetch(dataUrl)
|
||||
tagGroups = json.parseToJsonElement(rawData).jsonObject["data"]!!.jsonArray.associate { e ->
|
||||
val data = e.jsonObject
|
||||
val namespace = data["namespace"]!!.jsonPrimitive.content
|
||||
val prefix = if (namespace == "rows") NAMESPACE_PREFIX else TagNamespace.from(namespace)!!.prefix!!
|
||||
prefix to data["data"]!!.jsonObject.mapValues { it.value.jsonObject["name"]!!.jsonPrimitive.content }
|
||||
}
|
||||
initialized = true
|
||||
sha1File.write { writeString(newSha1) }
|
||||
dataFile.write { json.encodeToSink(tagGroups, this) }
|
||||
}
|
||||
}
|
||||
@@ -53,66 +53,64 @@ object EhUrl {
|
||||
const val ORIGIN_E = REFERER_E
|
||||
|
||||
val domain: String
|
||||
get() = when (Settings.gallerySite) {
|
||||
get() = when (Settings.gallerySite.value) {
|
||||
SITE_E -> DOMAIN_E
|
||||
SITE_EX -> DOMAIN_EX
|
||||
else -> DOMAIN_E
|
||||
}
|
||||
|
||||
val apiUrl: String
|
||||
get() = when (Settings.gallerySite) {
|
||||
get() = when (Settings.gallerySite.value) {
|
||||
SITE_E -> API_E
|
||||
SITE_EX -> API_EX
|
||||
else -> API_E
|
||||
}
|
||||
|
||||
val referer: String
|
||||
get() = when (Settings.gallerySite) {
|
||||
get() = when (Settings.gallerySite.value) {
|
||||
SITE_E -> REFERER_E
|
||||
SITE_EX -> REFERER_EX
|
||||
else -> REFERER_E
|
||||
}
|
||||
|
||||
val origin: String
|
||||
get() = when (Settings.gallerySite) {
|
||||
get() = when (Settings.gallerySite.value) {
|
||||
SITE_E -> ORIGIN_E
|
||||
SITE_EX -> ORIGIN_EX
|
||||
else -> ORIGIN_E
|
||||
}
|
||||
|
||||
val uConfigUrl: String
|
||||
get() = when (Settings.gallerySite) {
|
||||
SITE_E -> URL_UCONFIG_E
|
||||
SITE_EX -> URL_UCONFIG_EX
|
||||
else -> URL_UCONFIG_E
|
||||
}
|
||||
fun getUConfigUrl(gallerySite: Int = Settings.gallerySite.value) = when (gallerySite) {
|
||||
SITE_E -> URL_UCONFIG_E
|
||||
SITE_EX -> URL_UCONFIG_EX
|
||||
else -> URL_UCONFIG_E
|
||||
}
|
||||
|
||||
val myTagsUrl: String
|
||||
get() = when (Settings.gallerySite) {
|
||||
get() = when (Settings.gallerySite.value) {
|
||||
SITE_E -> URL_MY_TAGS_E
|
||||
SITE_EX -> URL_MY_TAGS_EX
|
||||
else -> URL_MY_TAGS_E
|
||||
}
|
||||
|
||||
val popularUrl: String
|
||||
get() = when (Settings.gallerySite) {
|
||||
get() = when (Settings.gallerySite.value) {
|
||||
SITE_E -> URL_POPULAR_E
|
||||
SITE_EX -> URL_POPULAR_EX
|
||||
else -> URL_POPULAR_E
|
||||
}
|
||||
|
||||
val imageSearchUrl: String
|
||||
get() = when (Settings.gallerySite) {
|
||||
get() = when (Settings.gallerySite.value) {
|
||||
SITE_E -> URL_IMAGE_SEARCH_E
|
||||
SITE_EX -> URL_IMAGE_SEARCH_EX
|
||||
else -> URL_IMAGE_SEARCH_E
|
||||
}
|
||||
|
||||
fun getGalleryDetailUrl(gid: Long, token: String, index: Int = 0, allComment: Boolean = false) =
|
||||
ehUrl(listOf("g", "$gid", token)) {
|
||||
if (index != 0) addQueryParameter("p", "$index")
|
||||
if (allComment) addQueryParameter("hc", "1")
|
||||
}.buildString()
|
||||
fun getGalleryDetailUrl(gid: Long, token: String, index: Int = 0, allComment: Boolean = false) = ehUrl(listOf("g", "$gid", token)) {
|
||||
if (index != 0) addQueryParameter("p", "$index")
|
||||
if (allComment) addQueryParameter("hc", "1")
|
||||
}.buildString()
|
||||
|
||||
fun getGalleryMultiPageViewerUrl(gid: Long, token: String) = ehUrl(listOf("mpv", "$gid", token)).buildString()
|
||||
|
||||
@@ -126,13 +124,22 @@ object EhUrl {
|
||||
addQueryParameter("act", "addfav")
|
||||
}.buildString()
|
||||
|
||||
fun getDownloadArchive(gid: Long, token: String, or: String) = ehUrl("archiver.php") {
|
||||
fun getArchiveUrl(gid: Long, token: String) = ehUrl("archiver.php") {
|
||||
addQueryParameter("gid", "$gid")
|
||||
addQueryParameter("token", token)
|
||||
addQueryParameter("or", or)
|
||||
}.buildString()
|
||||
|
||||
fun getTorrentUrl(gid: Long, token: String) = ehUrl("gallerytorrents.php") {
|
||||
addQueryParameter("gid", "$gid")
|
||||
addQueryParameter("t", token)
|
||||
}.buildString()
|
||||
|
||||
fun getTagDefinitionUrl(tag: String) = "https://ehwiki.org/wiki/" + tag.replace(' ', '_')
|
||||
|
||||
fun getTrackerUrl(gid: Long, key: String?) = ehUrl(
|
||||
path = listOfNotNull("$gid", key, "announce"),
|
||||
host = "ehtracker.org",
|
||||
).buildString()
|
||||
}
|
||||
|
||||
inline fun ehUrl(path: String? = null, host: String = EhUrl.domain, builder: ParametersBuilder.() -> Unit) = ehUrl(
|
||||
|
||||
@@ -15,17 +15,31 @@
|
||||
*/
|
||||
package com.hippo.ehviewer.client
|
||||
|
||||
import android.app.DownloadManager
|
||||
import android.content.ActivityNotFoundException
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Environment
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.ReadOnlyComposable
|
||||
import androidx.compose.runtime.Stable
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.toArgb
|
||||
import androidx.core.graphics.ColorUtils
|
||||
import androidx.core.net.toUri
|
||||
import arrow.core.memoize
|
||||
import com.ehviewer.core.model.GalleryDetail
|
||||
import com.ehviewer.core.model.GalleryInfo
|
||||
import com.ehviewer.core.util.withUIContext
|
||||
import com.hippo.ehviewer.Settings
|
||||
import com.hippo.ehviewer.client.data.GalleryInfo
|
||||
import kotlin.math.abs
|
||||
import com.hippo.ehviewer.client.parser.Archive
|
||||
import com.hippo.ehviewer.spider.SpiderDen
|
||||
import com.hippo.ehviewer.util.AppConfig
|
||||
import com.hippo.ehviewer.util.FileUtils
|
||||
import com.hippo.ehviewer.util.addTextToClipboard
|
||||
import com.materialkolor.hct.Hct
|
||||
import com.materialkolor.ktx.from
|
||||
import com.materialkolor.ktx.toColor
|
||||
import splitties.systemservices.downloadManager
|
||||
|
||||
object EhUtils {
|
||||
const val NONE = -1 // Use it for homepage
|
||||
@@ -84,7 +98,10 @@ object EhUtils {
|
||||
private val CATEGORY_STRINGS = CATEGORY_VALUES.entries.map { (k, v) -> v to k }
|
||||
|
||||
val isExHentai: Boolean
|
||||
get() = Settings.gallerySite == EhUrl.SITE_EX
|
||||
get() = Settings.gallerySite.value == EhUrl.SITE_EX
|
||||
|
||||
val isMpvAvailable
|
||||
get() = EhCookieStore.getHathPerks()?.contains('q') == true
|
||||
|
||||
fun getCategory(type: String?): Int {
|
||||
for (entry in CATEGORY_STRINGS) {
|
||||
@@ -99,64 +116,54 @@ object EhUtils {
|
||||
|
||||
fun getCategory(type: Int): String = CATEGORY_VALUES.getOrDefault(type, CATEGORY_VALUES[UNKNOWN])!![0]
|
||||
|
||||
private fun differenceDegrees(a: Float, b: Float): Float = 180.0f - abs(abs(a - b) - 180.0f)
|
||||
fun invCategory(category: Int): Int = category.inv() and ALL_CATEGORY
|
||||
|
||||
private fun sanitizeDegreesDouble(degrees: Float): Float {
|
||||
val deg = degrees % 360.0f
|
||||
return if (deg < 0) deg + 360.0f else deg
|
||||
}
|
||||
|
||||
private fun rotationDirection(from: Float, to: Float): Float {
|
||||
val increasingDifference = sanitizeDegreesDouble(to - from)
|
||||
return if (increasingDifference <= 180.0) 1.0f else -1.0f
|
||||
}
|
||||
|
||||
val harmonizeWithRole = { primaryContainer: Int, src: Int ->
|
||||
val fromHct = FloatArray(3).apply { ColorUtils.colorToM3HCT(src, this) }
|
||||
val toHct = FloatArray(3).apply { ColorUtils.colorToM3HCT(primaryContainer, this) }
|
||||
val differenceDegrees = differenceDegrees(fromHct[0], toHct[0])
|
||||
val rotationDegrees = minOf(differenceDegrees * 0.5f, 15.0f)
|
||||
val outputHue = sanitizeDegreesDouble(fromHct[0] + rotationDegrees * rotationDirection(fromHct[0], toHct[0]))
|
||||
ColorUtils.M3HCTToColor(outputHue, toHct[1], toHct[2])
|
||||
val mergeColor = { primaryContainer: Color, src: Color ->
|
||||
val fromHct = Hct.from(src)
|
||||
val toHct = Hct.from(primaryContainer)
|
||||
Hct.from(fromHct.hue, toHct.chroma, toHct.tone).toColor()
|
||||
}.memoize()
|
||||
|
||||
@Stable
|
||||
@ReadOnlyComposable
|
||||
@Composable
|
||||
fun getCategoryColor(category: Int): Color {
|
||||
val primary = when (category) {
|
||||
DOUJINSHI -> BG_COLOR_DOUJINSHI
|
||||
MANGA -> BG_COLOR_MANGA
|
||||
ARTIST_CG -> BG_COLOR_ARTIST_CG
|
||||
GAME_CG -> BG_COLOR_GAME_CG
|
||||
WESTERN -> BG_COLOR_WESTERN
|
||||
NON_H -> BG_COLOR_NON_H
|
||||
IMAGE_SET -> BG_COLOR_IMAGE_SET
|
||||
COSPLAY -> BG_COLOR_COSPLAY
|
||||
ASIAN_PORN -> BG_COLOR_ASIAN_PORN
|
||||
MISC -> BG_COLOR_MISC
|
||||
else -> BG_COLOR_UNKNOWN
|
||||
}.toInt()
|
||||
val color = if (Settings.harmonizeCategoryColor) {
|
||||
val primaryContainer = MaterialTheme.colorScheme.primaryContainer.toArgb()
|
||||
harmonizeWithRole(primaryContainer, primary)
|
||||
val primary = Color(
|
||||
when (category) {
|
||||
DOUJINSHI -> BG_COLOR_DOUJINSHI
|
||||
MANGA -> BG_COLOR_MANGA
|
||||
ARTIST_CG -> BG_COLOR_ARTIST_CG
|
||||
GAME_CG -> BG_COLOR_GAME_CG
|
||||
WESTERN -> BG_COLOR_WESTERN
|
||||
NON_H -> BG_COLOR_NON_H
|
||||
IMAGE_SET -> BG_COLOR_IMAGE_SET
|
||||
COSPLAY -> BG_COLOR_COSPLAY
|
||||
ASIAN_PORN -> BG_COLOR_ASIAN_PORN
|
||||
MISC -> BG_COLOR_MISC
|
||||
else -> BG_COLOR_UNKNOWN
|
||||
}.toInt(),
|
||||
)
|
||||
return if (Settings.harmonizeCategoryColor.value) {
|
||||
val primaryContainer = MaterialTheme.colorScheme.primaryContainer
|
||||
mergeColor(primaryContainer, primary)
|
||||
} else {
|
||||
primary
|
||||
}
|
||||
return Color(color)
|
||||
}
|
||||
|
||||
val categoryTextColor = Color(0xffe6e0e9)
|
||||
|
||||
val favoriteIconColor = Color(0xffff3040)
|
||||
|
||||
fun signOut() {
|
||||
EhCookieStore.removeAllCookies()
|
||||
Settings.displayName.value = null
|
||||
Settings.hasSignedIn.value = false
|
||||
Settings.gallerySite = EhUrl.SITE_E
|
||||
Settings.needSignIn = true
|
||||
Settings.gallerySite.value = EhUrl.SITE_E
|
||||
Settings.needSignIn.value = true
|
||||
}
|
||||
|
||||
fun getSuitableTitle(gi: GalleryInfo): String = if (Settings.showJpnTitle) {
|
||||
fun getSuitableTitle(gi: GalleryInfo): String = if (Settings.showJpnTitle.value) {
|
||||
if (gi.titleJpn.isNullOrEmpty()) gi.title else gi.titleJpn
|
||||
} else {
|
||||
if (gi.title.isNullOrEmpty()) gi.titleJpn else gi.title
|
||||
@@ -172,19 +179,34 @@ object EhUtils {
|
||||
return title.substringBeforeLast('|').trim().ifEmpty { null }
|
||||
}
|
||||
|
||||
fun handleThumbUrlResolution(url: String): String {
|
||||
val resolution = when (Settings.thumbResolution) {
|
||||
0 -> return url
|
||||
1 -> "250"
|
||||
2 -> "300"
|
||||
else -> return url
|
||||
}
|
||||
val index1 = url.lastIndexOf('_')
|
||||
val index2 = url.lastIndexOf('.')
|
||||
return if (index1 >= 0 && index2 >= 0 && index1 < index2) {
|
||||
url.substring(0, index1 + 1) + resolution + url.substring(index2)
|
||||
} else {
|
||||
url
|
||||
context(ctx: Context)
|
||||
suspend fun downloadArchive(galleryDetail: GalleryDetail, archive: Archive) {
|
||||
val gid = galleryDetail.gid
|
||||
EhEngine.downloadArchive(gid, galleryDetail.token, archive.res, archive.isHath)?.let {
|
||||
val uri = it.toUri()
|
||||
val intent = Intent().apply {
|
||||
action = Intent.ACTION_VIEW
|
||||
setDataAndType(uri, "application/zip")
|
||||
}
|
||||
val name = "$gid-${getSuitableTitle(galleryDetail)}.zip"
|
||||
try {
|
||||
ctx.startActivity(intent)
|
||||
withUIContext { addTextToClipboard(name, true) }
|
||||
} catch (_: ActivityNotFoundException) {
|
||||
val r = DownloadManager.Request(uri)
|
||||
r.setDestinationInExternalPublicDir(
|
||||
Environment.DIRECTORY_DOWNLOADS,
|
||||
AppConfig.APP_DIRNAME + "/" + FileUtils.sanitizeFilename(name),
|
||||
)
|
||||
r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
|
||||
downloadManager.enqueue(r)
|
||||
}
|
||||
if (Settings.archiveMetadata.value) {
|
||||
SpiderDen(galleryDetail).apply {
|
||||
initDownloadDir()
|
||||
writeComicInfo()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.hippo.ehviewer.client.data
|
||||
|
||||
interface AbstractDownloadInfo {
|
||||
var state: Int
|
||||
var legacy: Int
|
||||
var time: Long
|
||||
var label: String?
|
||||
var speed: Long
|
||||
var remaining: Long
|
||||
var finished: Int
|
||||
var downloaded: Int
|
||||
var total: Int
|
||||
}
|
||||
@@ -15,24 +15,27 @@
|
||||
*/
|
||||
package com.hippo.ehviewer.client.data
|
||||
|
||||
import android.os.Parcelable
|
||||
import com.hippo.ehviewer.client.EhUrl
|
||||
import com.hippo.ehviewer.client.addQueryParameter
|
||||
import com.hippo.ehviewer.client.addQueryParameterIfNotBlank
|
||||
import com.hippo.ehviewer.client.ehUrl
|
||||
import kotlinx.parcelize.Parcelize
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Parcelize
|
||||
@Serializable
|
||||
data class FavListUrlBuilder(
|
||||
val favCat: Int = FAV_CAT_ALL,
|
||||
val keyword: String? = null,
|
||||
var jumpTo: String? = null,
|
||||
var prev: String? = null,
|
||||
var next: String? = null,
|
||||
) : Parcelable {
|
||||
private var jumpTo: String? = null,
|
||||
private var prev: String? = null,
|
||||
private var next: String? = null,
|
||||
) {
|
||||
val isLocal
|
||||
get() = favCat == FAV_CAT_LOCAL
|
||||
|
||||
fun setIndex(index: String?, isNext: Boolean) {
|
||||
next = index.takeIf { isNext }
|
||||
prev = index.takeUnless { isNext }
|
||||
jumpTo = null
|
||||
}
|
||||
|
||||
fun build() = ehUrl(EhUrl.FAV_PATH) {
|
||||
|
||||
@@ -15,9 +15,14 @@
|
||||
*/
|
||||
package com.hippo.ehviewer.client.data
|
||||
|
||||
import android.os.Parcelable
|
||||
import com.hippo.ehviewer.client.data.GalleryInfo.Companion.S_LANGS
|
||||
import kotlinx.parcelize.Parcelize
|
||||
import androidx.compose.ui.util.fastFlatMap
|
||||
import com.ehviewer.core.model.GalleryDetail
|
||||
import com.ehviewer.core.model.GalleryInfo
|
||||
import com.ehviewer.core.model.GalleryTagGroup
|
||||
import com.ehviewer.core.model.PowerStatus
|
||||
import com.hippo.ehviewer.EhDB
|
||||
import com.hippo.ehviewer.Settings
|
||||
import com.hippo.ehviewer.client.EhFilter
|
||||
|
||||
private val LANGUAGES = arrayOf(
|
||||
"English",
|
||||
@@ -36,30 +41,27 @@ private val LANGUAGES = arrayOf(
|
||||
"Dutch",
|
||||
)
|
||||
|
||||
@Parcelize
|
||||
data class GalleryDetail(
|
||||
val galleryInfo: BaseGalleryInfo = BaseGalleryInfo(),
|
||||
var apiUid: Long = -1L,
|
||||
var apiKey: String? = null,
|
||||
var torrentCount: Int = 0,
|
||||
var torrentUrl: String? = null,
|
||||
var archiveUrl: String? = null,
|
||||
var parent: String? = null,
|
||||
var newerVersions: List<BaseGalleryInfo> = emptyList(),
|
||||
var visible: String? = null,
|
||||
var language: String? = null,
|
||||
var size: String? = null,
|
||||
var favoriteCount: Int = 0,
|
||||
var ratingCount: Int = 0,
|
||||
val tags: List<GalleryTagGroup>,
|
||||
var comments: GalleryCommentList,
|
||||
val previewPages: Int,
|
||||
val previewList: List<GalleryPreview>,
|
||||
) : GalleryInfo by galleryInfo,
|
||||
Parcelable {
|
||||
fun fillInfo() {
|
||||
val index = LANGUAGES.indexOf(language)
|
||||
if (index != -1) simpleLanguage = S_LANGS[index]
|
||||
simpleTags = tags.flatten()
|
||||
suspend fun GalleryDetail.fillInfo() {
|
||||
val index = LANGUAGES.indexOf(language)
|
||||
if (index != -1) simpleLanguage = GalleryInfo.S_LANGS[index]
|
||||
simpleTags = tagGroups.fastFlatMap(GalleryTagGroup::tags).map { (text, power, _) ->
|
||||
if (power == PowerStatus.Weak) "_$text" else text
|
||||
}
|
||||
if (favoriteSlot == GalleryInfo.NOT_FAVORITED && EhDB.containLocalFavorites(gid)) {
|
||||
favoriteSlot = GalleryInfo.LOCAL_FAVORITED
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun GalleryDetail.filterComments() {
|
||||
comments = with(comments) {
|
||||
val scoreThreshold = Settings.commentThreshold.value
|
||||
copy(
|
||||
comments = comments.filter {
|
||||
it.uploader ||
|
||||
it.score > scoreThreshold &&
|
||||
!EhFilter.filterCommenter(it.user.orEmpty()) &&
|
||||
!EhFilter.filterComment(it.comment)
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -15,30 +15,25 @@
|
||||
*/
|
||||
package com.hippo.ehviewer.client.data
|
||||
|
||||
import android.os.Parcelable
|
||||
import androidx.annotation.IntDef
|
||||
import com.ehviewer.core.database.model.QuickSearch
|
||||
import com.ehviewer.core.model.GalleryInfo
|
||||
import com.hippo.ehviewer.Settings
|
||||
import com.hippo.ehviewer.client.EhUrl
|
||||
import com.hippo.ehviewer.client.EhUtils
|
||||
import com.hippo.ehviewer.client.addQueryParameter
|
||||
import com.hippo.ehviewer.client.addQueryParameterIfNotBlank
|
||||
import com.hippo.ehviewer.client.ehUrl
|
||||
import com.hippo.ehviewer.dao.QuickSearch
|
||||
import com.hippo.ehviewer.ui.main.AdvanceTable
|
||||
import com.hippo.ehviewer.util.toIntOrDefault
|
||||
import java.io.UnsupportedEncodingException
|
||||
import java.net.URLDecoder
|
||||
import kotlinx.parcelize.Parcelize
|
||||
import io.ktor.http.Parameters
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Parcelize
|
||||
@Serializable
|
||||
data class ListUrlBuilder(
|
||||
@get:Mode @Mode
|
||||
var mode: Int = MODE_NORMAL,
|
||||
private var mPrev: String? = null,
|
||||
var mNext: String? = null,
|
||||
// Reset to null after initial loading
|
||||
var mJumpTo: String? = null,
|
||||
private var mRange: Int = 0,
|
||||
private var prev: String? = null,
|
||||
private var next: String? = null,
|
||||
private var jumpTo: String? = null,
|
||||
private var range: Int = 0,
|
||||
var category: Int = EhUtils.NONE,
|
||||
private var mKeyword: String? = null,
|
||||
var hash: String? = null,
|
||||
@@ -47,24 +42,31 @@ data class ListUrlBuilder(
|
||||
var minRating: Int = -1,
|
||||
var pageFrom: Int = -1,
|
||||
var pageTo: Int = -1,
|
||||
var imagePath: String? = null,
|
||||
) : Parcelable {
|
||||
) {
|
||||
|
||||
fun setIndex(index: String, isNext: Boolean = true) {
|
||||
mNext = index.takeIf { isNext }
|
||||
mPrev = index.takeUnless { isNext }
|
||||
mRange = 0
|
||||
next = index.takeIf { isNext }
|
||||
prev = index.takeUnless { isNext }
|
||||
range = 0
|
||||
jumpTo = null
|
||||
}
|
||||
|
||||
fun setJumpTo(jumpTo: Int) {
|
||||
mJumpTo = jumpTo.takeUnless { it == 0 }?.toString()
|
||||
var page: Int
|
||||
get() = (jumpTo?.toInt() ?: 0) + 1
|
||||
set(value) {
|
||||
jumpTo = (value - 1).takeUnless { it == 0 }?.toString()
|
||||
}
|
||||
|
||||
fun setRange(to: Int) {
|
||||
range = to
|
||||
prev = null
|
||||
next = null
|
||||
jumpTo = null
|
||||
}
|
||||
|
||||
fun setRange(range: Int) {
|
||||
mRange = range
|
||||
mPrev = null
|
||||
mNext = null
|
||||
mJumpTo = null
|
||||
fun setSeek(date: String) {
|
||||
setIndex("2", true)
|
||||
jumpTo = date
|
||||
}
|
||||
|
||||
var keyword: String?
|
||||
@@ -73,18 +75,18 @@ data class ListUrlBuilder(
|
||||
mKeyword = keyword
|
||||
}
|
||||
|
||||
constructor(q: QuickSearch) : this() {
|
||||
mode = q.mode
|
||||
this.category = q.category
|
||||
mKeyword = q.keyword
|
||||
advanceSearch = q.advanceSearch
|
||||
minRating = q.minRating
|
||||
pageFrom = q.pageFrom
|
||||
pageTo = q.pageTo
|
||||
mNext = q.name.substringAfterLast('@', "").ifEmpty { null }
|
||||
}
|
||||
constructor(q: QuickSearch) : this(
|
||||
mode = q.mode,
|
||||
category = q.category,
|
||||
mKeyword = q.keyword,
|
||||
advanceSearch = q.advanceSearch,
|
||||
minRating = q.minRating,
|
||||
pageFrom = q.pageFrom,
|
||||
pageTo = q.pageTo,
|
||||
next = q.name.substringAfterLast('@', "").ifEmpty { null },
|
||||
)
|
||||
|
||||
fun toQuickSearch(name: String): QuickSearch = QuickSearch(
|
||||
fun toQuickSearch(name: String) = QuickSearch(
|
||||
name = name,
|
||||
mode = mode,
|
||||
category = category,
|
||||
@@ -121,140 +123,34 @@ data class ListUrlBuilder(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param query xxx=yyy&mmm=nnn
|
||||
*/
|
||||
constructor(query: String?) : this() {
|
||||
// TODO page
|
||||
if (query.isNullOrEmpty()) {
|
||||
return
|
||||
}
|
||||
val queries = query.split('&')
|
||||
var category = 0
|
||||
var keyword: String? = null
|
||||
var enableAdvanceSearch = false
|
||||
var advanceSearch = 0
|
||||
var enableMinRating = false
|
||||
var minRating = -1
|
||||
var enablePage = false
|
||||
var pageFrom = -1
|
||||
var pageTo = -1
|
||||
for (str in queries) {
|
||||
val index = str.indexOf('=')
|
||||
if (index < 0) {
|
||||
continue
|
||||
constructor(params: Parameters) : this(
|
||||
prev = params["prev"],
|
||||
next = params["next"],
|
||||
jumpTo = params["seek"],
|
||||
range = params["range"]?.toIntOrNull() ?: 0,
|
||||
category = params["f_cats"]?.toIntOrNull()?.let(EhUtils::invCategory) ?: EhUtils.NONE,
|
||||
mKeyword = params["f_search"],
|
||||
) {
|
||||
if (params["advsearch"] == "1") {
|
||||
advanceSearch = 0
|
||||
if (params["f_sh"] == "on") {
|
||||
advanceSearch = advanceSearch or AdvanceTable.SH
|
||||
}
|
||||
val key = str.substring(0, index)
|
||||
val value = str.substring(index + 1)
|
||||
when (key) {
|
||||
"f_cats" -> {
|
||||
val cats = value.toIntOrDefault(EhUtils.ALL_CATEGORY)
|
||||
category = category or (cats.inv() and EhUtils.ALL_CATEGORY)
|
||||
}
|
||||
|
||||
"f_doujinshi" -> if ("1" == value) {
|
||||
category = category or EhUtils.DOUJINSHI
|
||||
}
|
||||
|
||||
"f_manga" -> if ("1" == value) {
|
||||
category = category or EhUtils.MANGA
|
||||
}
|
||||
|
||||
"f_artistcg" -> if ("1" == value) {
|
||||
category = category or EhUtils.ARTIST_CG
|
||||
}
|
||||
|
||||
"f_gamecg" -> if ("1" == value) {
|
||||
category = category or EhUtils.GAME_CG
|
||||
}
|
||||
|
||||
"f_western" -> if ("1" == value) {
|
||||
category = category or EhUtils.WESTERN
|
||||
}
|
||||
|
||||
"f_non-h" -> if ("1" == value) {
|
||||
category = category or EhUtils.NON_H
|
||||
}
|
||||
|
||||
"f_imageset" -> if ("1" == value) {
|
||||
category = category or EhUtils.IMAGE_SET
|
||||
}
|
||||
|
||||
"f_cosplay" -> if ("1" == value) {
|
||||
category = category or EhUtils.COSPLAY
|
||||
}
|
||||
|
||||
"f_asianporn" -> if ("1" == value) {
|
||||
category = category or EhUtils.ASIAN_PORN
|
||||
}
|
||||
|
||||
"f_misc" -> if ("1" == value) {
|
||||
category = category or EhUtils.MISC
|
||||
}
|
||||
|
||||
"f_search" -> try {
|
||||
keyword = URLDecoder.decode(value, "utf-8")
|
||||
} catch (e: UnsupportedEncodingException) {
|
||||
// Ignore
|
||||
} catch (_: IllegalArgumentException) {
|
||||
}
|
||||
|
||||
"advsearch" -> if ("1" == value) {
|
||||
enableAdvanceSearch = true
|
||||
}
|
||||
|
||||
"f_sh" -> if ("on" == value) {
|
||||
advanceSearch = advanceSearch or AdvanceTable.SH
|
||||
}
|
||||
|
||||
"f_sto" -> if ("on" == value) {
|
||||
advanceSearch = advanceSearch or AdvanceTable.STO
|
||||
}
|
||||
|
||||
"f_sfl" -> if ("on" == value) {
|
||||
advanceSearch = advanceSearch or AdvanceTable.SFL
|
||||
}
|
||||
|
||||
"f_sfu" -> if ("on" == value) {
|
||||
advanceSearch = advanceSearch or AdvanceTable.SFU
|
||||
}
|
||||
|
||||
"f_sft" -> if ("on" == value) {
|
||||
advanceSearch = advanceSearch or AdvanceTable.SFT
|
||||
}
|
||||
|
||||
"f_sr" -> if ("on" == value) {
|
||||
enableMinRating = true
|
||||
}
|
||||
|
||||
"f_srdd" -> minRating = value.toIntOrDefault(-1)
|
||||
"f_sp" -> if ("on" == value) {
|
||||
enablePage = true
|
||||
}
|
||||
|
||||
"f_spf" -> pageFrom = value.toIntOrDefault(-1)
|
||||
"f_spt" -> pageTo = value.toIntOrDefault(-1)
|
||||
"f_shash" -> hash = value
|
||||
if (params["f_sto"] == "on") {
|
||||
advanceSearch = advanceSearch or AdvanceTable.STO
|
||||
}
|
||||
}
|
||||
this.category = category
|
||||
mKeyword = keyword
|
||||
if (enableAdvanceSearch) {
|
||||
this.advanceSearch = advanceSearch
|
||||
if (enableMinRating) {
|
||||
this.minRating = minRating
|
||||
} else {
|
||||
this.minRating = -1
|
||||
if (params["f_sfl"] == "on") {
|
||||
advanceSearch = advanceSearch or AdvanceTable.SFL
|
||||
}
|
||||
if (enablePage) {
|
||||
this.pageFrom = pageFrom
|
||||
this.pageTo = pageTo
|
||||
} else {
|
||||
this.pageFrom = -1
|
||||
this.pageTo = -1
|
||||
if (params["f_sfu"] == "on") {
|
||||
advanceSearch = advanceSearch or AdvanceTable.SFU
|
||||
}
|
||||
} else {
|
||||
this.advanceSearch = -1
|
||||
if (params["f_sft"] == "on") {
|
||||
advanceSearch = advanceSearch or AdvanceTable.SFT
|
||||
}
|
||||
minRating = params["f_srdd"]?.toIntOrNull() ?: -1
|
||||
pageFrom = params["f_spf"]?.toIntOrNull() ?: -1
|
||||
pageTo = params["f_spt"]?.toIntOrNull() ?: -1
|
||||
}
|
||||
}
|
||||
|
||||
@@ -262,7 +158,7 @@ data class ListUrlBuilder(
|
||||
MODE_NORMAL, MODE_SUBSCRIPTION -> ehUrl(EhUrl.WATCHED_PATH.takeIf { mode == MODE_SUBSCRIPTION }) {
|
||||
val category = if (!Settings.hasSignedIn.value && category <= 0) EhUtils.NON_H else category
|
||||
if (category > 0) {
|
||||
addQueryParameter("f_cats", (category.inv() and EhUtils.ALL_CATEGORY).toString())
|
||||
addQueryParameter("f_cats", "${EhUtils.invCategory(category)}")
|
||||
}
|
||||
val query = mKeyword?.let { keyword ->
|
||||
if (language == -1 || "gid:" in keyword || "l:" in keyword || "language:" in keyword) {
|
||||
@@ -277,10 +173,10 @@ data class ListUrlBuilder(
|
||||
}
|
||||
}
|
||||
addQueryParameterIfNotBlank("f_search", query)
|
||||
addQueryParameterIfNotBlank("prev", mPrev)
|
||||
addQueryParameterIfNotBlank("next", mNext)
|
||||
addQueryParameterIfNotBlank("seek", mJumpTo)
|
||||
addQueryParameterIfNotBlank("range", mRange.takeIf { it > 0 }?.toString())
|
||||
addQueryParameterIfNotBlank("prev", prev)
|
||||
addQueryParameterIfNotBlank("next", next)
|
||||
addQueryParameterIfNotBlank("seek", jumpTo)
|
||||
addQueryParameterIfNotBlank("range", range.takeIf { it > 0 }?.toString())
|
||||
// Advance search
|
||||
if (advanceSearch > 0 || minRating > 0 || pageFrom > 0 || pageTo > 0) {
|
||||
addQueryParameter("advsearch", "1")
|
||||
@@ -301,12 +197,10 @@ data class ListUrlBuilder(
|
||||
}
|
||||
// Set min star
|
||||
if (minRating > 0) {
|
||||
addQueryParameter("f_sr", "on")
|
||||
addQueryParameter("f_srdd", "$minRating")
|
||||
}
|
||||
// Pages
|
||||
if (pageFrom > 0 || pageTo > 0) {
|
||||
addQueryParameter("f_sp", "on")
|
||||
addQueryParameterIfNotBlank(
|
||||
"f_spf",
|
||||
pageFrom.takeIf { it > 0 }?.toString(),
|
||||
@@ -322,10 +216,10 @@ data class ListUrlBuilder(
|
||||
MODE_UPLOADER, MODE_TAG -> {
|
||||
val path = if (mode == MODE_UPLOADER) "uploader" else "tag"
|
||||
ehUrl(listOf(path, requireNotNull(mKeyword))) {
|
||||
addQueryParameterIfNotBlank("prev", mPrev)
|
||||
addQueryParameterIfNotBlank("next", mNext)
|
||||
addQueryParameterIfNotBlank("seek", mJumpTo)
|
||||
addQueryParameterIfNotBlank("range", mRange.takeIf { it > 0 }?.toString())
|
||||
addQueryParameterIfNotBlank("prev", prev)
|
||||
addQueryParameterIfNotBlank("next", next)
|
||||
addQueryParameterIfNotBlank("seek", jumpTo)
|
||||
addQueryParameterIfNotBlank("range", range.takeIf { it > 0 }?.toString())
|
||||
}.buildString()
|
||||
}
|
||||
|
||||
@@ -333,27 +227,14 @@ data class ListUrlBuilder(
|
||||
MODE_IMAGE_SEARCH -> ehUrl {
|
||||
addQueryParameter("f_shash", requireNotNull(hash))
|
||||
}.buildString()
|
||||
MODE_TOPLIST -> {
|
||||
ehUrl("toplist.php", EhUrl.DOMAIN_E) {
|
||||
addQueryParameter("tl", requireNotNull(mKeyword))
|
||||
addQueryParameterIfNotBlank("p", mJumpTo)
|
||||
}.buildString()
|
||||
}
|
||||
MODE_TOPLIST -> ehUrl("toplist.php", EhUrl.DOMAIN_E) {
|
||||
addQueryParameter("tl", requireNotNull(mKeyword))
|
||||
addQueryParameterIfNotBlank("p", jumpTo)
|
||||
}.buildString()
|
||||
|
||||
else -> throw IllegalStateException("Unexpected value: $mode")
|
||||
}
|
||||
|
||||
@IntDef(
|
||||
MODE_NORMAL,
|
||||
MODE_UPLOADER,
|
||||
MODE_TAG,
|
||||
MODE_WHATS_HOT,
|
||||
MODE_IMAGE_SEARCH,
|
||||
MODE_SUBSCRIPTION,
|
||||
MODE_TOPLIST,
|
||||
)
|
||||
@Retention(AnnotationRetention.SOURCE)
|
||||
private annotation class Mode
|
||||
companion object {
|
||||
const val MODE_NORMAL = 0x0
|
||||
const val MODE_UPLOADER = 0x1
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
package com.hippo.ehviewer.client.data
|
||||
|
||||
@JvmInline
|
||||
value class TagNamespace(val value: String) {
|
||||
fun toPrefix(): String? = prefixMap[this]
|
||||
|
||||
companion object {
|
||||
val Artist = TagNamespace("artist")
|
||||
val Cosplayer = TagNamespace("cosplayer")
|
||||
val Character = TagNamespace("character")
|
||||
val Female = TagNamespace("female")
|
||||
val Group = TagNamespace("group")
|
||||
val Language = TagNamespace("language")
|
||||
val Male = TagNamespace("male")
|
||||
val Mixed = TagNamespace("mixed")
|
||||
val Other = TagNamespace("other")
|
||||
val Parody = TagNamespace("parody")
|
||||
val Reclass = TagNamespace("reclass")
|
||||
private val prefixMap = mapOf(
|
||||
Artist to "a",
|
||||
Cosplayer to "cos",
|
||||
Character to "c",
|
||||
Female to "f",
|
||||
Group to "g",
|
||||
Language to "l",
|
||||
Male to "m",
|
||||
Mixed to "x",
|
||||
Other to "o",
|
||||
Parody to "p",
|
||||
Reclass to "r",
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.hippo.ehviewer.client.exception
|
||||
|
||||
import com.ehviewer.core.i18n.R
|
||||
|
||||
class CloudflareBypassException : EhException(R.string.cloudflare_bypass_failed)
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.hippo.ehviewer.client.exception
|
||||
|
||||
import androidx.annotation.StringRes
|
||||
import com.ehviewer.core.i18n.R
|
||||
|
||||
sealed class FatalException : EhException {
|
||||
constructor(message: String) : super(message)
|
||||
constructor(@StringRes messageId: Int) : super(messageId)
|
||||
}
|
||||
|
||||
class IpBannedException(message: String) : FatalException(message)
|
||||
class InsufficientGpException : FatalException(R.string.insufficient_gp)
|
||||
class QuotaExceededException : FatalException(R.string.error_509)
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
package com.hippo.ehviewer.client.exception
|
||||
|
||||
import com.hippo.ehviewer.R
|
||||
import com.ehviewer.core.i18n.R
|
||||
|
||||
class InsufficientFundsException : EhException(R.string.insufficient_funds)
|
||||
+3
-1
@@ -15,4 +15,6 @@
|
||||
*/
|
||||
package com.hippo.ehviewer.client.exception
|
||||
|
||||
class NoHAtHClientException(detailMessage: String?) : EhException(detailMessage)
|
||||
import com.ehviewer.core.i18n.R
|
||||
|
||||
class NoHathClientException() : EhException(R.string.download_archive_failure_no_hath)
|
||||
@@ -1,5 +1,5 @@
|
||||
package com.hippo.ehviewer.client.exception
|
||||
|
||||
import com.hippo.ehviewer.R
|
||||
import com.ehviewer.core.i18n.R
|
||||
|
||||
class NoHitsFoundException : EhException(R.string.gallery_list_empty_hit)
|
||||
@@ -1,5 +1,5 @@
|
||||
package com.hippo.ehviewer.client.exception
|
||||
|
||||
import com.hippo.ehviewer.R
|
||||
import com.ehviewer.core.i18n.R
|
||||
|
||||
class NotLoggedInException : EhException(R.string.sign_in_first)
|
||||
@@ -1,5 +0,0 @@
|
||||
package com.hippo.ehviewer.client.exception
|
||||
|
||||
import com.hippo.ehviewer.R
|
||||
|
||||
class QuotaExceededException : EhException(R.string.error_509)
|
||||
@@ -15,76 +15,43 @@
|
||||
*/
|
||||
package com.hippo.ehviewer.client.parser
|
||||
|
||||
import com.hippo.ehviewer.client.exception.InsufficientFundsException
|
||||
import com.hippo.ehviewer.client.exception.NoHAtHClientException
|
||||
import eu.kanade.tachiyomi.util.system.logcat
|
||||
import org.jsoup.Jsoup
|
||||
import arrow.core.Either.Companion.catch
|
||||
import arrow.core.getOrElse
|
||||
import arrow.core.partially3
|
||||
import com.hippo.ehviewer.client.exception.ParseException
|
||||
import java.nio.ByteBuffer
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
object ArchiveParser {
|
||||
private val PATTERN_ARCHIVE_URL =
|
||||
Regex("<strong>(.*)</strong>.*<a href=\"([^\"]*)\">Click Here To Start Downloading</a>")
|
||||
private val PATTERN_CURRENT_FUNDS =
|
||||
Regex("<p>([\\d,]+) GP \\[[^]]*] ([\\d,]+) Credits \\[[^]]*]</p>")
|
||||
private val PATTERN_HATH_FORM =
|
||||
Regex("<form id=\"hathdl_form\" action=\"[^\"]*?or=([^=\"]*?)\" method=\"post\">")
|
||||
private val PATTERN_HATH_ARCHIVE =
|
||||
Regex("<p><a href=\"[^\"]*\" onclick=\"return do_hathdl\\('([0-9]+|org)'\\)\">([^<]+)</a></p>\\s*<p>([\\w. ]+)</p>\\s*<p>([\\w. ]+)</p>")
|
||||
private const val ERROR_NEED_HATH_CLIENT =
|
||||
"You must have a H@H client assigned to your account to use this feature."
|
||||
private const val ERROR_INSUFFICIENT_FUNDS =
|
||||
"You do not have enough funds to download this archive."
|
||||
|
||||
fun parse(body: String, maybeFunds: Funds?): Result {
|
||||
val m = PATTERN_HATH_FORM.find(body)!!
|
||||
val paramOr = m.groupValues[1]
|
||||
val archiveList = ArrayList<Archive>()
|
||||
Jsoup.parse(body).select("#db>div>div").forEach { element ->
|
||||
if (element.childrenSize() > 0 && !element.attr("style").contains("color:#CCCCCC")) {
|
||||
runCatching {
|
||||
val res = element.selectFirst("form>input")!!.attr("value")
|
||||
val name = element.selectFirst("form>div>input")!!.attr("value")
|
||||
val size = element.selectFirst("p>strong")!!.text()
|
||||
val cost = element.selectFirst("div>strong")!!.text().replace(",", "")
|
||||
Archive(res, name, size, cost, false)
|
||||
}.onSuccess {
|
||||
archiveList.add(it)
|
||||
}.onFailure {
|
||||
logcat(it)
|
||||
}
|
||||
}
|
||||
fun parse(body: ByteBuffer, maybeFunds: Funds?) = catch {
|
||||
if (maybeFunds != null) {
|
||||
val archives = unmarshalParsingAs<List<Archive>>(body, ::parseArchives)
|
||||
Result(archives, maybeFunds)
|
||||
} else {
|
||||
unmarshalParsingAs<Result>(body, ::parseArchives.partially3(true))
|
||||
}
|
||||
PATTERN_HATH_ARCHIVE.findAll(body).forEach { matchResult ->
|
||||
val (res, name, size, cost) = matchResult.groupValues.slice(1..4)
|
||||
.map { ParserUtils.trim(it) }
|
||||
val item = Archive(res, name, size, cost, true)
|
||||
archiveList.add(item)
|
||||
}
|
||||
val funds = maybeFunds ?: PATTERN_CURRENT_FUNDS.find(body)!!.groupValues.run {
|
||||
val fundsGP = ParserUtils.parseInt(get(1), 0)
|
||||
val fundsC = ParserUtils.parseInt(get(2), 0)
|
||||
Funds("%,d".format(fundsGP), "%,d".format(fundsC))
|
||||
}
|
||||
return Result(paramOr, archiveList, funds)
|
||||
}.getOrElse {
|
||||
throw ParseException("Can't parse archive list", it)
|
||||
}
|
||||
|
||||
fun parseArchiveUrl(body: String): String? {
|
||||
if (body.contains(ERROR_NEED_HATH_CLIENT)) {
|
||||
throw NoHAtHClientException("No H@H client")
|
||||
} else if (body.contains(ERROR_INSUFFICIENT_FUNDS)) {
|
||||
throw InsufficientFundsException()
|
||||
}
|
||||
return Jsoup.parse(body).selectFirst("#continue>a[href]")
|
||||
?.let { it.attr("href") + "?start=1" }
|
||||
// TODO: Check more errors
|
||||
fun parseArchiveUrl(body: ByteBuffer) = catch {
|
||||
unmarshalParsingAs<String?>(body, ::parseArchiveUrl)
|
||||
}.getOrElse {
|
||||
throw ParseException("Can't parse archive url", it)
|
||||
}
|
||||
|
||||
data class Result(val paramOr: String, val archiveList: List<Archive>, val funds: Funds)
|
||||
@Serializable
|
||||
data class Result(val archiveList: List<Archive>, val funds: Funds)
|
||||
}
|
||||
|
||||
@Serializable
|
||||
class Archive(
|
||||
val res: String,
|
||||
val name: String,
|
||||
val size: String,
|
||||
val cost: String,
|
||||
val isHAtH: Boolean,
|
||||
val isHath: Boolean,
|
||||
)
|
||||
|
||||
private external fun parseArchives(body: ByteBuffer, size: Int = body.limit(), parseFunds: Boolean = false): Int
|
||||
private external fun parseArchiveUrl(body: ByteBuffer, size: Int = body.limit()): Int
|
||||
@@ -17,8 +17,10 @@
|
||||
*/
|
||||
package com.hippo.ehviewer.client.parser
|
||||
|
||||
import org.jsoup.Jsoup
|
||||
import java.nio.ByteBuffer
|
||||
|
||||
object EventPaneParser {
|
||||
fun parse(body: String): String? = Jsoup.parse(body).getElementById("eventpane")?.html()
|
||||
private external fun parseEventPane(body: ByteBuffer, size: Int = body.limit()): Int
|
||||
|
||||
fun parse(body: ByteBuffer): String? = unmarshalParsingAs(body, ::parseEventPane)
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.hippo.ehviewer.client.parser
|
||||
|
||||
import com.ehviewer.core.util.unescapeXml
|
||||
import com.hippo.ehviewer.client.exception.ParseException
|
||||
import com.hippo.ehviewer.util.unescapeXml
|
||||
import java.nio.ByteBuffer
|
||||
import kotlinx.serialization.Serializable
|
||||
import moe.tarsin.coroutines.runSuspendCatching
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
/*
|
||||
* Copyright 2016 Hippo Seven
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.hippo.ehviewer.client.parser
|
||||
|
||||
import com.hippo.ehviewer.client.EhUrl
|
||||
import com.hippo.ehviewer.client.exception.NotLoggedInException
|
||||
import com.hippo.ehviewer.util.ExceptionUtils
|
||||
import org.jsoup.Jsoup
|
||||
|
||||
object ForumsParser {
|
||||
fun parse(body: String): String = runCatching {
|
||||
val d = Jsoup.parse(body, EhUrl.URL_FORUMS)
|
||||
val userlinks = d.getElementById("userlinks")
|
||||
val child = userlinks!!.child(0).child(0).child(0)
|
||||
child.attr("href")
|
||||
}.getOrElse {
|
||||
ExceptionUtils.throwIfFatal(it)
|
||||
throw NotLoggedInException()
|
||||
}
|
||||
}
|
||||
@@ -15,11 +15,11 @@
|
||||
*/
|
||||
package com.hippo.ehviewer.client.parser
|
||||
|
||||
import com.ehviewer.core.model.GalleryInfo
|
||||
import com.ehviewer.core.util.unescapeXml
|
||||
import com.hippo.ehviewer.client.EhUtils.getCategory
|
||||
import com.hippo.ehviewer.client.data.GalleryInfo
|
||||
import com.hippo.ehviewer.client.getThumbKey
|
||||
import com.hippo.ehviewer.client.parseAs
|
||||
import com.hippo.ehviewer.util.unescapeXml
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
|
||||
@@ -15,464 +15,40 @@
|
||||
*/
|
||||
package com.hippo.ehviewer.client.parser
|
||||
|
||||
import arrow.core.unzip
|
||||
import com.hippo.ehviewer.EhDB
|
||||
import com.hippo.ehviewer.Settings
|
||||
import com.hippo.ehviewer.client.EhFilter
|
||||
import com.hippo.ehviewer.client.EhUtils
|
||||
import com.hippo.ehviewer.client.EhUtils.getCategory
|
||||
import com.hippo.ehviewer.client.data.BaseGalleryInfo
|
||||
import com.hippo.ehviewer.client.data.GalleryComment
|
||||
import com.hippo.ehviewer.client.data.GalleryCommentList
|
||||
import com.hippo.ehviewer.client.data.GalleryDetail
|
||||
import com.hippo.ehviewer.client.data.GalleryInfo.Companion.LOCAL_FAVORITED
|
||||
import com.hippo.ehviewer.client.data.GalleryInfo.Companion.NOT_FAVORITED
|
||||
import com.hippo.ehviewer.client.data.GalleryPreview
|
||||
import com.hippo.ehviewer.client.data.GalleryTagGroup
|
||||
import com.hippo.ehviewer.client.data.LargeGalleryPreview
|
||||
import com.hippo.ehviewer.client.data.NormalGalleryPreview
|
||||
import com.hippo.ehviewer.client.exception.EhException
|
||||
import com.hippo.ehviewer.client.exception.OffensiveException
|
||||
import arrow.core.Either
|
||||
import arrow.core.getOrElse
|
||||
import com.ehviewer.core.model.GalleryCommentList
|
||||
import com.ehviewer.core.model.GalleryDetail
|
||||
import com.ehviewer.core.model.GalleryPreviewList
|
||||
import com.hippo.ehviewer.client.exception.ParseException
|
||||
import com.hippo.ehviewer.client.exception.PiningException
|
||||
import com.hippo.ehviewer.client.getThumbKey
|
||||
import com.hippo.ehviewer.util.ExceptionUtils
|
||||
import com.hippo.ehviewer.util.toEpochMillis
|
||||
import com.hippo.ehviewer.util.toFloatOrDefault
|
||||
import com.hippo.ehviewer.util.toIntOrDefault
|
||||
import com.hippo.ehviewer.util.trimAnd
|
||||
import com.hippo.ehviewer.util.unescapeXml
|
||||
import eu.kanade.tachiyomi.util.system.logcat
|
||||
import kotlinx.datetime.LocalDateTime
|
||||
import kotlinx.datetime.format.MonthNames
|
||||
import kotlinx.datetime.format.char
|
||||
import org.jsoup.Jsoup
|
||||
import org.jsoup.nodes.Document
|
||||
import org.jsoup.nodes.Element
|
||||
import org.jsoup.nodes.Node
|
||||
import org.jsoup.select.Elements
|
||||
import org.jsoup.select.NodeTraversor
|
||||
import org.jsoup.select.NodeVisitor
|
||||
import java.nio.ByteBuffer
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.cbor.CborArray
|
||||
|
||||
object GalleryDetailParser {
|
||||
private val PATTERN_ERROR = Regex("<div class=\"d\">\n<p>([^<]+)</p>")
|
||||
private val PATTERN_DETAIL = Regex(
|
||||
"var gid = (\\d+);.+?var token = \"([a-f0-9]+)\";.+?var apiuid = ([\\-\\d]+);.+?var apikey = \"([a-f0-9]+)\";",
|
||||
RegexOption.DOT_MATCHES_ALL,
|
||||
)
|
||||
private val PATTERN_TORRENT =
|
||||
Regex("<a[^<>]*onclick=\"return popUp\\('([^']+)'[^)]+\\)\">Torrent Download \\((\\d+)\\)</a>")
|
||||
private val PATTERN_ARCHIVE =
|
||||
Regex("<a[^<>]*onclick=\"return popUp\\('([^']+)'[^)]+\\)\">Archive Download</a>")
|
||||
private val PATTERN_COVER = Regex("width:(\\d+)px; height:(\\d+)px.+?url\\((.+?)\\)")
|
||||
private val PATTERN_PAGES =
|
||||
Regex("<tr><td[^<>]*>Length:</td><td[^<>]*>([\\d,]+) pages</td></tr>")
|
||||
private val PATTERN_PREVIEW_PAGES =
|
||||
Regex("<td[^>]+><a[^>]+>([\\d,]+)</a></td><td[^>]+>(?:<a[^>]+>)?>(?:</a>)?</td>")
|
||||
private val PATTERN_NORMAL_PREVIEW =
|
||||
Regex("<div class=\"gdtm\"[^<>]*><div[^<>]*width:(\\d+)[^<>]*height:(\\d+)[^<>]*\\((.+?)\\)[^<>]*-(\\d+)px[^<>]*><a[^<>]*href=\"(.+?)\"[^<>]*><img alt=\"([\\d,]+)\"")
|
||||
private val PATTERN_LARGE_PREVIEW =
|
||||
Regex("<div class=\"gdtl\".+?<a href=\"(.+?)\"><img alt=\"([\\d,]+)\".+?src=\"(.+?)\"")
|
||||
private val PATTERN_NEWER_DATE = Regex(", added (.+?)<br />")
|
||||
private val PATTERN_FAVORITE_SLOT =
|
||||
Regex("/fav.png\\); background-position:0px -(\\d+)px")
|
||||
private val EMPTY_GALLERY_COMMENT_LIST = GalleryCommentList(emptyList(), false)
|
||||
|
||||
// dd MMMM yyyy, HH:mm
|
||||
private val WEB_COMMENT_DATE_FORMAT = LocalDateTime.Format {
|
||||
dayOfMonth()
|
||||
char(' ')
|
||||
monthName(MonthNames.ENGLISH_FULL)
|
||||
char(' ')
|
||||
year()
|
||||
chars(", ")
|
||||
hour()
|
||||
char(':')
|
||||
minute()
|
||||
}
|
||||
private const val OFFENSIVE_STRING =
|
||||
"<p>(And if you choose to ignore this warning, you lose all rights to complain about it in the future.)</p>"
|
||||
private const val PINING_STRING = "<p>This gallery is pining for the fjords.</p>"
|
||||
|
||||
suspend fun parse(body: String): GalleryDetail {
|
||||
if (body.contains(OFFENSIVE_STRING)) {
|
||||
throw OffensiveException()
|
||||
}
|
||||
if (body.contains(PINING_STRING)) {
|
||||
throw PiningException()
|
||||
}
|
||||
|
||||
// Error info
|
||||
PATTERN_ERROR.find(body)?.run { throw EhException(groupValues[1]) }
|
||||
val document = Jsoup.parse(body)
|
||||
val galleryDetail = GalleryDetail(
|
||||
tags = parseTagGroups(document),
|
||||
comments = parseComments(document),
|
||||
previewPages = parsePreviewPages(body),
|
||||
previewList = parsePreviewList(body).first,
|
||||
)
|
||||
parseDetail(galleryDetail, document, body)
|
||||
|
||||
// Fill info for database
|
||||
galleryDetail.fillInfo()
|
||||
return galleryDetail
|
||||
fun parse(body: ByteBuffer) = Either.catch {
|
||||
unmarshalParsingAs<Result>(body, ::nativeParse)
|
||||
}.getOrElse {
|
||||
throw ParseException("Failed to parse gallery detail", it)
|
||||
}
|
||||
|
||||
private suspend fun parseDetail(gd: GalleryDetail, d: Document, body: String) {
|
||||
PATTERN_DETAIL.find(body)?.apply {
|
||||
gd.gid = groupValues[1].toLongOrNull() ?: -1L
|
||||
gd.token = groupValues[2]
|
||||
gd.apiUid = groupValues[3].toLongOrNull() ?: -1L
|
||||
gd.apiKey = groupValues[4]
|
||||
} ?: throw ParseException("Can't parse gallery detail")
|
||||
if (gd.gid == -1L) {
|
||||
throw ParseException("Can't parse gallery detail")
|
||||
}
|
||||
PATTERN_TORRENT.find(body)?.run {
|
||||
gd.torrentUrl = groupValues[1].trim().unescapeXml()
|
||||
gd.torrentCount = groupValues[2].toIntOrNull() ?: 0
|
||||
}
|
||||
PATTERN_ARCHIVE.find(body)?.run {
|
||||
gd.archiveUrl = groupValues[1].trim().unescapeXml()
|
||||
}
|
||||
try {
|
||||
val gm = d.getElementsByClass("gm")[0]
|
||||
// Thumb url
|
||||
gm.getElementById("gd1")?.child(0)?.attr("style")?.trim()?.let {
|
||||
gd.thumbKey = getThumbKey(PATTERN_COVER.find(it)!!.groupValues[3])
|
||||
}
|
||||
|
||||
gd.title = gm.getElementById("gn")?.text()?.trim()
|
||||
gd.titleJpn = gm.getElementById("gj")?.text()?.trim()
|
||||
|
||||
// Category
|
||||
try {
|
||||
val gdc = gm.getElementById("gdc")!!
|
||||
var ce = gdc.getElementsByClass("cn").first()
|
||||
if (ce == null) {
|
||||
ce = gdc.getElementsByClass("cs").first()
|
||||
}
|
||||
gd.category = getCategory(ce!!.text())
|
||||
} catch (e: Throwable) {
|
||||
ExceptionUtils.throwIfFatal(e)
|
||||
gd.category = EhUtils.UNKNOWN
|
||||
}
|
||||
|
||||
// Uploader
|
||||
val gdn = gm.getElementById("gdn")
|
||||
if (null != gdn) {
|
||||
gd.disowned = gdn.attr("style").contains("opacity:0.5")
|
||||
gd.uploader = gdn.text().trim()
|
||||
} else {
|
||||
gd.uploader = ""
|
||||
}
|
||||
val gdd = gm.getElementById("gdd")
|
||||
gd.posted = ""
|
||||
gd.parent = ""
|
||||
gd.visible = ""
|
||||
gd.size = ""
|
||||
gd.pages = 0
|
||||
gd.favoriteCount = 0
|
||||
val es = gdd!!.child(0).child(0).children()
|
||||
es.forEach {
|
||||
parseDetailInfo(gd, it)
|
||||
}
|
||||
|
||||
// Rating count
|
||||
val ratingCount = gm.getElementById("rating_count")
|
||||
if (null != ratingCount) {
|
||||
gd.ratingCount = ratingCount.text().trim().toIntOrDefault(0)
|
||||
} else {
|
||||
gd.ratingCount = 0
|
||||
}
|
||||
|
||||
// Rating
|
||||
val ratingLabel = gm.getElementById("rating_label")
|
||||
if (null != ratingLabel) {
|
||||
val ratingStr = ratingLabel.text().trim()
|
||||
if ("Not Yet Rated" == ratingStr) {
|
||||
gd.rating = -1.0f
|
||||
} else {
|
||||
val index = ratingStr.indexOf(' ')
|
||||
if (index == -1 || index >= ratingStr.length) {
|
||||
gd.rating = 0f
|
||||
} else {
|
||||
gd.rating = ratingStr.substring(index + 1).toFloatOrDefault(0f)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
gd.rating = -1.0f
|
||||
}
|
||||
|
||||
// isFavorited
|
||||
val gdf = gm.getElementById("gdf")
|
||||
if (gdf != null) {
|
||||
val favoriteName = gdf.text().trim()
|
||||
if (favoriteName != "Add to Favorites") {
|
||||
gd.favoriteName = gdf.text().trim()
|
||||
PATTERN_FAVORITE_SLOT.find(body)?.run {
|
||||
gd.favoriteSlot = ((groupValues[1].toIntOrNull() ?: 2) - 2) / 19
|
||||
}
|
||||
}
|
||||
}
|
||||
if (gd.favoriteSlot == NOT_FAVORITED && EhDB.containLocalFavorites(gd.gid)) {
|
||||
gd.favoriteSlot = LOCAL_FAVORITED
|
||||
}
|
||||
} catch (e: Throwable) {
|
||||
ExceptionUtils.throwIfFatal(e)
|
||||
throw ParseException("Can't parse gallery detail")
|
||||
}
|
||||
|
||||
// newer version
|
||||
d.getElementById("gnd")?.run {
|
||||
val dates = PATTERN_NEWER_DATE.findAll(body).map { it.groupValues[1] }.toList()
|
||||
gd.newerVersions = select("a").mapIndexedNotNull { index, element ->
|
||||
val result = GalleryDetailUrlParser.parse(element.attr("href"))
|
||||
result?.run {
|
||||
BaseGalleryInfo(gid, token, element.text().trim(), posted = dates[index])
|
||||
}
|
||||
}
|
||||
}
|
||||
fun parseComments(body: ByteBuffer) = Either.catch {
|
||||
unmarshalParsingAs<GalleryCommentList>(body, ::nativeParseComments)
|
||||
}.getOrElse {
|
||||
throw ParseException("Failed to parse comments", it)
|
||||
}
|
||||
|
||||
private fun parseDetailInfo(gd: GalleryDetail, e: Element) {
|
||||
val es = e.children()
|
||||
if (es.size < 2) {
|
||||
return
|
||||
}
|
||||
val key = es[0].text().trim()
|
||||
val value = es[1].ownText().trim()
|
||||
if (key.startsWith("Posted")) {
|
||||
gd.posted = value
|
||||
} else if (key.startsWith("Parent")) {
|
||||
val a = es[1].children().first()
|
||||
if (a != null) {
|
||||
gd.parent = a.attr("href")
|
||||
}
|
||||
} else if (key.startsWith("Visible")) {
|
||||
gd.visible = value
|
||||
} else if (key.startsWith("Language")) {
|
||||
gd.language = value
|
||||
} else if (key.startsWith("File Size")) {
|
||||
gd.size = value
|
||||
} else if (key.startsWith("Length")) {
|
||||
val index = value.indexOf(' ')
|
||||
if (index >= 0) {
|
||||
gd.pages = value.substring(0, index).toIntOrDefault(1)
|
||||
} else {
|
||||
gd.pages = 1
|
||||
}
|
||||
} else if (key.startsWith("Favorited")) {
|
||||
when (value) {
|
||||
"Never" -> gd.favoriteCount = 0
|
||||
"Once" -> gd.favoriteCount = 1
|
||||
else -> {
|
||||
val index = value.indexOf(' ')
|
||||
if (index == -1) {
|
||||
gd.favoriteCount = 0
|
||||
} else {
|
||||
gd.favoriteCount = value.substring(0, index).toIntOrDefault(0)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
fun parsePreviews(body: ByteBuffer) = Either.catch {
|
||||
unmarshalParsingAs<GalleryPreviewList>(body, ::nativeParsePreviews)
|
||||
}.getOrElse {
|
||||
throw ParseException("Failed to parse previews", it)
|
||||
}
|
||||
|
||||
private fun parseTagGroup(element: Element): GalleryTagGroup? = try {
|
||||
var nameSpace = element.child(0).text()
|
||||
// Remove last ':'
|
||||
nameSpace = nameSpace.substring(0, nameSpace.length - 1)
|
||||
val group = GalleryTagGroup(nameSpace)
|
||||
val tags = element.child(1).children()
|
||||
tags.forEach {
|
||||
var tag = it.text()
|
||||
// Sometimes parody tag is followed with '|' and english translate, just remove them
|
||||
val index = tag.indexOf('|')
|
||||
if (index >= 0) {
|
||||
tag = tag.substring(0, index).trim()
|
||||
}
|
||||
if (it.className() == "gtw") {
|
||||
tag = "_$tag" // weak tag
|
||||
}
|
||||
group.add(tag)
|
||||
}
|
||||
if (group.size > 0) group else null
|
||||
} catch (e: Throwable) {
|
||||
ExceptionUtils.throwIfFatal(e)
|
||||
logcat(e)
|
||||
null
|
||||
}
|
||||
@Serializable
|
||||
@CborArray
|
||||
class Result(val detail: GalleryDetail, val event: String?)
|
||||
|
||||
/**
|
||||
* Parse tag groups with html parser
|
||||
*/
|
||||
private fun parseTagGroups(document: Document): List<GalleryTagGroup> = try {
|
||||
val taglist = document.getElementById("taglist")!!
|
||||
val tagGroups = taglist.child(0).child(0).children()
|
||||
parseTagGroups(tagGroups)
|
||||
} catch (e: Throwable) {
|
||||
ExceptionUtils.throwIfFatal(e)
|
||||
logcat(e)
|
||||
emptyList()
|
||||
}
|
||||
|
||||
private fun parseTagGroups(trs: Elements): List<GalleryTagGroup> = try {
|
||||
trs.mapNotNull { parseTagGroup(it) }
|
||||
} catch (e: Throwable) {
|
||||
ExceptionUtils.throwIfFatal(e)
|
||||
logcat(e)
|
||||
emptyList()
|
||||
}
|
||||
|
||||
private suspend fun parseComment(element: Element): GalleryComment? {
|
||||
return try {
|
||||
// Id
|
||||
val a = element.previousElementSibling()
|
||||
val name = a!!.attr("name")
|
||||
val id = name trimAnd { substring(1).toInt().toLong() }
|
||||
// Editable, vote up and vote down
|
||||
val c4 = element.getElementsByClass("c4").first()
|
||||
var voteUpAble = false
|
||||
var voteUpEd = false
|
||||
var voteDownAble = false
|
||||
var voteDownEd = false
|
||||
var editable = false
|
||||
var uploader = false
|
||||
if (null != c4) {
|
||||
if ("Uploader Comment" == c4.text()) {
|
||||
uploader = true
|
||||
}
|
||||
for (e in c4.children()) {
|
||||
when (e.text()) {
|
||||
"Vote+" -> {
|
||||
voteUpAble = true
|
||||
voteUpEd = e.attr("style").trim().isNotEmpty()
|
||||
}
|
||||
|
||||
"Vote-" -> {
|
||||
voteDownAble = true
|
||||
voteDownEd = e.attr("style").trim().isNotEmpty()
|
||||
}
|
||||
|
||||
"Edit" -> editable = true
|
||||
}
|
||||
}
|
||||
}
|
||||
// Vote state
|
||||
val c7 = element.getElementsByClass("c7").first()
|
||||
val voteState = c7?.text()?.trim()
|
||||
// Score
|
||||
val c5 = element.getElementsByClass("c5").first()
|
||||
val score = c5?.children()?.first()?.text()?.trim()?.toIntOrDefault(0) ?: 0
|
||||
// time
|
||||
val c3 = element.getElementsByClass("c3").first()
|
||||
val temp = c3!!.ownText()
|
||||
val (timeStr, user) = if (temp.endsWith(':')) {
|
||||
temp.substring("Posted on ".length, temp.length - " by:".length) to c3.child(0).text()
|
||||
} else {
|
||||
temp.substring("Posted on ".length) to null
|
||||
}
|
||||
val time = WEB_COMMENT_DATE_FORMAT.parse(timeStr).toEpochMillis()
|
||||
// comment
|
||||
val c6 = element.getElementsByClass("c6").first()
|
||||
// fix underline support
|
||||
for (e in c6!!.children()) {
|
||||
if ("span" == e.tagName() && "text-decoration:underline;" == e.attr("style")) {
|
||||
e.tagName("u")
|
||||
}
|
||||
}
|
||||
val commentHtml = c6.html()
|
||||
// filter comment
|
||||
if (!uploader) {
|
||||
val sEhFilter = EhFilter
|
||||
if (score <= Settings.commentThreshold || sEhFilter.filterCommenter(user.orEmpty()) || sEhFilter.filterComment(commentHtml)) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
// last edited
|
||||
val c8 = element.getElementsByClass("c8").first()
|
||||
val lastEdited = c8?.children()?.first()?.run { WEB_COMMENT_DATE_FORMAT.parse(text()).toEpochMillis() } ?: 0
|
||||
GalleryComment(
|
||||
id, score, editable, voteUpAble, voteUpEd, voteDownAble,
|
||||
voteDownEd, uploader, voteState, time, user, commentHtml, lastEdited,
|
||||
)
|
||||
} catch (e: Throwable) {
|
||||
ExceptionUtils.throwIfFatal(e)
|
||||
logcat(e)
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse comments with html parser
|
||||
*/
|
||||
suspend fun parseComments(document: Document): GalleryCommentList {
|
||||
// Disable pretty print to get comments in raw html
|
||||
document.outputSettings().prettyPrint(false)
|
||||
return try {
|
||||
val cdiv = document.getElementById("cdiv")!!
|
||||
val c1s = cdiv.getElementsByClass("c1")
|
||||
val list = c1s.mapNotNull { parseComment(it) }
|
||||
val chd = cdiv.getElementById("chd")
|
||||
var hasMore = false
|
||||
NodeTraversor.traverse(
|
||||
object : NodeVisitor {
|
||||
override fun head(node: Node, depth: Int) {
|
||||
if (node is Element && node.text() == "click to show all") {
|
||||
hasMore = true
|
||||
}
|
||||
}
|
||||
|
||||
override fun tail(node: Node, depth: Int) {}
|
||||
},
|
||||
chd!!,
|
||||
)
|
||||
GalleryCommentList(list, hasMore)
|
||||
} catch (e: Throwable) {
|
||||
ExceptionUtils.throwIfFatal(e)
|
||||
logcat(e)
|
||||
EMPTY_GALLERY_COMMENT_LIST
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse preview pages with regular expressions
|
||||
*/
|
||||
fun parsePreviewPages(body: String): Int = PATTERN_PREVIEW_PAGES.find(body)?.groupValues?.get(1)?.toIntOrNull()
|
||||
?: throw ParseException("Parse preview page count error")
|
||||
|
||||
/**
|
||||
* Parse pages with regular expressions
|
||||
*/
|
||||
fun parsePages(body: String): Int = PATTERN_PAGES.find(body)?.groupValues?.get(1)?.toIntOrNull()
|
||||
?: throw ParseException("Parse pages error")
|
||||
|
||||
fun parsePreviewList(body: String): Pair<List<GalleryPreview>, List<String>> = runCatching { parseNormalPreview(body) }.getOrElse { parseLargePreview(body) }
|
||||
|
||||
private fun parseLargePreview(body: String): Pair<List<GalleryPreview>, List<String>> {
|
||||
check(PATTERN_LARGE_PREVIEW.containsMatchIn(body))
|
||||
return PATTERN_LARGE_PREVIEW.findAll(body).unzip {
|
||||
val position = it.groupValues[2].toInt() - 1
|
||||
val url = it.groupValues[3].trim()
|
||||
val pageUrl = it.groupValues[1].trim()
|
||||
LargeGalleryPreview(url, position) to pageUrl
|
||||
}.run {
|
||||
first.toList() to second.toList()
|
||||
}
|
||||
}
|
||||
|
||||
private fun parseNormalPreview(body: String): Pair<List<GalleryPreview>, List<String>> {
|
||||
check(PATTERN_NORMAL_PREVIEW.containsMatchIn(body))
|
||||
return PATTERN_NORMAL_PREVIEW.findAll(body).unzip {
|
||||
val position = it.groupValues[6].toInt() - 1
|
||||
val url = it.groupValues[3].trim()
|
||||
val xOffset = it.groupValues[4].toIntOrNull() ?: 0
|
||||
val width = it.groupValues[1].toInt()
|
||||
val height = it.groupValues[2].toInt()
|
||||
val pageUrl = it.groupValues[5].trim()
|
||||
NormalGalleryPreview(url, position, xOffset, width, height) to pageUrl
|
||||
}.run {
|
||||
first.toList() to second.toList()
|
||||
}
|
||||
}
|
||||
private external fun nativeParse(body: ByteBuffer, size: Int = body.limit()): Int
|
||||
private external fun nativeParseComments(body: ByteBuffer, size: Int = body.limit()): Int
|
||||
private external fun nativeParsePreviews(body: ByteBuffer, size: Int = body.limit()): Int
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.hippo.ehviewer.client.parser
|
||||
|
||||
import com.hippo.ehviewer.client.data.BaseGalleryInfo
|
||||
import com.ehviewer.core.model.BaseGalleryInfo
|
||||
import com.hippo.ehviewer.client.exception.ParseException
|
||||
import java.nio.ByteBuffer
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@@ -15,124 +15,56 @@
|
||||
*/
|
||||
package com.hippo.ehviewer.client.parser
|
||||
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import arrow.core.Either
|
||||
import com.hippo.ehviewer.client.EhUrl
|
||||
import com.hippo.ehviewer.client.EhUtils
|
||||
import com.hippo.ehviewer.client.data.ListUrlBuilder
|
||||
import eu.kanade.tachiyomi.util.system.logcat
|
||||
import java.io.UnsupportedEncodingException
|
||||
import java.net.MalformedURLException
|
||||
import java.net.URL
|
||||
import java.net.URLDecoder
|
||||
import java.nio.charset.StandardCharsets
|
||||
import io.ktor.http.Url
|
||||
import io.ktor.http.decodeURLQueryComponent
|
||||
|
||||
object GalleryListUrlParser {
|
||||
private val VALID_HOSTS = arrayOf(EhUrl.DOMAIN_EX, EhUrl.DOMAIN_E)
|
||||
private const val PATH_NORMAL = "/"
|
||||
private const val PATH_UPLOADER = "/uploader/"
|
||||
private const val PATH_TAG = "/tag/"
|
||||
private const val PATH_TOPLIST = "/toplist.php"
|
||||
fun parse(urlStr: String): ListUrlBuilder? {
|
||||
val url = try {
|
||||
URL(urlStr)
|
||||
} catch (e: MalformedURLException) {
|
||||
fun parse(urlStr: String) = Either.catch {
|
||||
val url = Url(urlStr)
|
||||
if (url.host != EhUrl.DOMAIN_E && url.host != EhUrl.DOMAIN_EX) {
|
||||
return null
|
||||
}
|
||||
if (url.host !in VALID_HOSTS) {
|
||||
return null
|
||||
}
|
||||
val path = url.path ?: return null
|
||||
return if (PATH_NORMAL == path || path.isEmpty()) {
|
||||
ListUrlBuilder(url.query)
|
||||
} else if (path.startsWith(PATH_UPLOADER)) {
|
||||
parseUploader(path)
|
||||
} else if (path.startsWith(PATH_TAG)) {
|
||||
parseTag(path)
|
||||
} else if (path.startsWith(PATH_TOPLIST)) {
|
||||
parseToplist(urlStr)
|
||||
} else if (path.startsWith("/")) {
|
||||
val category = try {
|
||||
path.substring(1).toInt()
|
||||
} catch (e: NumberFormatException) {
|
||||
return null
|
||||
val segments = url.segments
|
||||
if (segments.isEmpty()) {
|
||||
ListUrlBuilder(url.parameters).apply {
|
||||
url.parameters["f_shash"]?.let {
|
||||
mode = ListUrlBuilder.MODE_IMAGE_SEARCH
|
||||
hash = it
|
||||
}
|
||||
}
|
||||
ListUrlBuilder(url.query).also { it.category = category }
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
when (val head = segments[0]) {
|
||||
"uploader", "tag" -> ListUrlBuilder(url.parameters).apply {
|
||||
mode = if (head == "uploader") ListUrlBuilder.MODE_UPLOADER else ListUrlBuilder.MODE_TAG
|
||||
keyword = segments[1].decodeURLQueryComponent(plusIsSpace = true)
|
||||
}
|
||||
|
||||
// TODO get page
|
||||
private fun parseUploader(path: String): ListUrlBuilder? {
|
||||
var uploader: String?
|
||||
val prefixLength = PATH_UPLOADER.length
|
||||
val index = path.indexOf('/', prefixLength)
|
||||
uploader = if (index < 0) {
|
||||
path.substring(prefixLength)
|
||||
} else {
|
||||
path.substring(prefixLength, index)
|
||||
}
|
||||
uploader = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
URLDecoder.decode(uploader, StandardCharsets.UTF_8)
|
||||
} else {
|
||||
try {
|
||||
URLDecoder.decode(uploader, StandardCharsets.UTF_8.displayName())
|
||||
} catch (e: UnsupportedEncodingException) {
|
||||
logcat(e)
|
||||
return null
|
||||
"toplist.php" -> {
|
||||
val tl = url.parameters["tl"]
|
||||
if (tl != null && tl in arrayOf("11", "12", "13", "15")) {
|
||||
ListUrlBuilder(
|
||||
mode = ListUrlBuilder.MODE_TOPLIST,
|
||||
jumpTo = url.parameters["p"],
|
||||
mKeyword = tl,
|
||||
)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
else -> {
|
||||
val category = EhUtils.getCategory(head)
|
||||
if (category != EhUtils.UNKNOWN) {
|
||||
ListUrlBuilder(url.parameters).also { it.category = category }
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (uploader.isNullOrEmpty()) {
|
||||
return null
|
||||
}
|
||||
val builder = ListUrlBuilder()
|
||||
builder.mode = ListUrlBuilder.MODE_UPLOADER
|
||||
builder.keyword = uploader
|
||||
return builder
|
||||
}
|
||||
|
||||
// TODO get page
|
||||
private fun parseTag(path: String): ListUrlBuilder? {
|
||||
var tag: String?
|
||||
val prefixLength = PATH_TAG.length
|
||||
val index = path.indexOf('/', prefixLength)
|
||||
tag = if (index < 0) {
|
||||
path.substring(prefixLength)
|
||||
} else {
|
||||
path.substring(prefixLength, index)
|
||||
}
|
||||
tag = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
URLDecoder.decode(tag, StandardCharsets.UTF_8)
|
||||
} else {
|
||||
try {
|
||||
URLDecoder.decode(tag, StandardCharsets.UTF_8.displayName())
|
||||
} catch (e: UnsupportedEncodingException) {
|
||||
logcat(e)
|
||||
return null
|
||||
}
|
||||
}
|
||||
if (tag.isNullOrEmpty()) {
|
||||
return null
|
||||
}
|
||||
val builder = ListUrlBuilder()
|
||||
builder.mode = ListUrlBuilder.MODE_TAG
|
||||
builder.keyword = tag
|
||||
return builder
|
||||
}
|
||||
|
||||
// TODO get page
|
||||
private fun parseToplist(path: String): ListUrlBuilder? {
|
||||
val uri = Uri.parse(path)
|
||||
if (uri == null || uri.getQueryParameter("tl").isNullOrEmpty()) {
|
||||
return null
|
||||
}
|
||||
val tl = uri.getQueryParameter("tl")!!.toInt()
|
||||
if (tl > 15 || tl < 11) {
|
||||
return null
|
||||
}
|
||||
val builder = ListUrlBuilder()
|
||||
builder.mode = ListUrlBuilder.MODE_TOPLIST
|
||||
builder.keyword = tl.toString()
|
||||
return builder
|
||||
}
|
||||
}.getOrNull()
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
/*
|
||||
* Copyright 2022 Tarsin Norbin
|
||||
*
|
||||
* This file is part of EhViewer
|
||||
*
|
||||
* EhViewer is free software: you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* EhViewer is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with EhViewer.
|
||||
* If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.hippo.ehviewer.client.parser
|
||||
|
||||
import com.hippo.ehviewer.util.ExceptionUtils
|
||||
import eu.kanade.tachiyomi.util.system.logcat
|
||||
import org.jsoup.Jsoup
|
||||
|
||||
object GalleryNotAvailableParser {
|
||||
fun parse(body: String): String? = runCatching {
|
||||
val e = Jsoup.parse(body).getElementsByClass("d").first()
|
||||
e!!.child(0).html().replace("<br>", "\n")
|
||||
}.getOrElse {
|
||||
ExceptionUtils.throwIfFatal(it)
|
||||
logcat(it)
|
||||
null
|
||||
}
|
||||
}
|
||||
@@ -15,8 +15,8 @@
|
||||
*/
|
||||
package com.hippo.ehviewer.client.parser
|
||||
|
||||
import com.ehviewer.core.util.unescapeXml
|
||||
import com.hippo.ehviewer.client.exception.ParseException
|
||||
import com.hippo.ehviewer.util.unescapeXml
|
||||
|
||||
object GalleryPageParser {
|
||||
private val PATTERN_IMAGE_URL = Regex("<img[^>]*src=\"([^\"]+)\" style")
|
||||
|
||||
@@ -15,9 +15,10 @@
|
||||
*/
|
||||
package com.hippo.ehviewer.client.parser
|
||||
|
||||
import arrow.core.Either
|
||||
import arrow.core.getOrElse
|
||||
import com.hippo.ehviewer.client.exception.EhException
|
||||
import com.hippo.ehviewer.client.parseAs
|
||||
import com.hippo.ehviewer.util.ExceptionUtils
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@@ -32,11 +33,10 @@ object GalleryTokenApiParser {
|
||||
* ]
|
||||
* }
|
||||
*/
|
||||
fun parse(body: String): String = runCatching {
|
||||
fun parse(body: String): String = Either.catch {
|
||||
body.parseAs<Result>().tokenList[0].token
|
||||
}.getOrElse {
|
||||
ExceptionUtils.throwIfFatal(it)
|
||||
throw EhException(body.parseAs<Error>().error)
|
||||
throw EhException(body.parseAs<VoteTagResult>().error)
|
||||
}
|
||||
|
||||
@Serializable
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
package com.hippo.ehviewer.client.parser
|
||||
|
||||
import android.os.Parcelable
|
||||
import com.hippo.ehviewer.client.exception.InsufficientFundsException
|
||||
import com.hippo.ehviewer.client.exception.ParseException
|
||||
import java.nio.ByteBuffer
|
||||
import kotlinx.parcelize.Parcelize
|
||||
import kotlin.random.Random
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
object HomeParser {
|
||||
private val PATTERN_FUNDS = Regex("Available: ([\\d,]+) Credits.*Available: ([\\d,]+) kGP", RegexOption.DOT_MATCHES_ALL)
|
||||
private val TorrentKeyRegex = Regex("Your current key is: <[^>]*>([^<]*)<")
|
||||
private val FundsRegex = Regex("Available: ([\\d,]+) Credits.*Available: ([\\d,]+) kGP", RegexOption.DOT_MATCHES_ALL)
|
||||
private const val INSUFFICIENT_FUNDS = "Insufficient funds."
|
||||
|
||||
fun parse(body: ByteBuffer) = runCatching {
|
||||
@@ -21,24 +21,25 @@ object HomeParser {
|
||||
}
|
||||
}
|
||||
|
||||
fun parseTorrentKey(body: String) = TorrentKeyRegex.find(body)?.run { groupValues[1] }
|
||||
?: throw ParseException("Parse torrent key error")
|
||||
|
||||
fun parseFunds(body: String): Funds {
|
||||
PATTERN_FUNDS.find(body)?.groupValues?.run {
|
||||
val fundsC = ParserUtils.parseInt(get(1), 0)
|
||||
val fundsGP = ParserUtils.parseInt(get(2), 0) * 1000
|
||||
return Funds("%,d+".format(fundsGP), "%,d".format(fundsC))
|
||||
FundsRegex.find(body)?.run {
|
||||
val fundsC = ParserUtils.parseInt(groupValues[1], 0)
|
||||
val fundsGP = ParserUtils.parseInt(groupValues[2], 0)
|
||||
return Funds(fundsGP, fundsC)
|
||||
}
|
||||
throw ParseException("Parse funds error")
|
||||
}
|
||||
|
||||
@Parcelize
|
||||
data class Result(val limits: Limits, val funds: Funds) : Parcelable
|
||||
data class Result(val limits: Limits, val funds: Funds, private val id: Int = Random.nextInt())
|
||||
}
|
||||
|
||||
@Parcelize
|
||||
data class Funds(val gp: String, val credit: String) : Parcelable
|
||||
|
||||
@Parcelize
|
||||
@Serializable
|
||||
data class Limits(val current: Int, val maximum: Int, val resetCost: Int) : Parcelable
|
||||
data class Funds(val gp: Int, val credit: Int)
|
||||
|
||||
@Serializable
|
||||
data class Limits(val current: Int, val maximum: Int, val resetCost: Int)
|
||||
|
||||
private external fun parseLimit(body: ByteBuffer, limit: Int = body.limit()): Int
|
||||
@@ -15,11 +15,9 @@
|
||||
*/
|
||||
package com.hippo.ehviewer.client.parser
|
||||
|
||||
import com.hippo.ehviewer.util.toEpochMillis
|
||||
import com.hippo.ehviewer.util.toIntOrDefault
|
||||
import com.hippo.ehviewer.util.toLocalDateTime
|
||||
import com.hippo.ehviewer.util.toLongOrDefault
|
||||
import com.hippo.ehviewer.util.unescapeXml
|
||||
import com.ehviewer.core.util.toEpochMillis
|
||||
import com.ehviewer.core.util.toIntOrDefault
|
||||
import com.ehviewer.core.util.toLocalDateTime
|
||||
import kotlinx.datetime.LocalDateTime
|
||||
import kotlinx.datetime.format.char
|
||||
|
||||
@@ -30,7 +28,7 @@ object ParserUtils {
|
||||
char('-')
|
||||
monthNumber()
|
||||
char('-')
|
||||
dayOfMonth()
|
||||
day()
|
||||
char(' ')
|
||||
hour()
|
||||
char(':')
|
||||
@@ -41,9 +39,5 @@ object ParserUtils {
|
||||
|
||||
fun parseDate(str: String): Long = formatter.parse(str).toEpochMillis()
|
||||
|
||||
fun trim(str: String?): String = str?.unescapeXml()?.trim() ?: ""
|
||||
|
||||
fun parseInt(str: String?, defValue: Int): Int = trim(str).replace(",", "").toIntOrDefault(defValue)
|
||||
|
||||
fun parseLong(str: String?, defValue: Long): Long = trim(str).replace(",", "").toLongOrDefault(defValue)
|
||||
fun parseInt(str: String, defValue: Int): Int = str.replace(",", "").toIntOrDefault(defValue)
|
||||
}
|
||||
@@ -15,37 +15,28 @@
|
||||
*/
|
||||
package com.hippo.ehviewer.client.parser
|
||||
|
||||
import com.hippo.ehviewer.client.EhUrl
|
||||
import arrow.core.Either.Companion.catch
|
||||
import arrow.core.getOrElse
|
||||
import com.hippo.ehviewer.client.exception.ParseException
|
||||
import com.hippo.ehviewer.util.ExceptionUtils
|
||||
import eu.kanade.tachiyomi.util.system.logcat
|
||||
import org.jsoup.Jsoup
|
||||
import java.nio.ByteBuffer
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
object ProfileParser {
|
||||
fun parse(body: String): Result = runCatching {
|
||||
val d = Jsoup.parse(body)
|
||||
val profilename = d.getElementById("profilename")
|
||||
val displayName = profilename!!.child(0).text()
|
||||
val avatar = runCatching {
|
||||
val avatar =
|
||||
profilename.nextElementSibling()!!.nextElementSibling()!!.child(0).attr("src")
|
||||
if (avatar.isEmpty()) {
|
||||
null
|
||||
} else if (!avatar.startsWith("http")) {
|
||||
EhUrl.URL_FORUMS + avatar
|
||||
} else {
|
||||
avatar
|
||||
}
|
||||
}.getOrElse {
|
||||
ExceptionUtils.throwIfFatal(it)
|
||||
logcat { "No avatar" }
|
||||
null
|
||||
}
|
||||
Result(displayName, avatar)
|
||||
fun parse(body: ByteBuffer) = catch {
|
||||
unmarshalParsingAs<Result>(body, ::parseProfile)
|
||||
}.getOrElse {
|
||||
ExceptionUtils.throwIfFatal(it)
|
||||
throw ParseException("Parse forums error")
|
||||
throw ParseException("Failed to parse profile", it)
|
||||
}
|
||||
|
||||
class Result(val displayName: String?, val avatar: String?)
|
||||
fun parseProfileUrl(body: ByteBuffer) = catch {
|
||||
unmarshalParsingAs<String>(body, ::parseProfileUrl)
|
||||
}.getOrElse {
|
||||
throw ParseException("Failed to parse profile url", it)
|
||||
}
|
||||
|
||||
@Serializable
|
||||
data class Result(val displayName: String, val avatar: String?)
|
||||
|
||||
private external fun parseProfile(body: ByteBuffer, size: Int = body.limit()): Int
|
||||
private external fun parseProfileUrl(body: ByteBuffer, size: Int = body.limit()): Int
|
||||
}
|
||||
@@ -17,9 +17,18 @@
|
||||
*/
|
||||
package com.hippo.ehviewer.client.parser
|
||||
|
||||
import com.hippo.ehviewer.client.parseAs
|
||||
import arrow.core.Either.Companion.catch
|
||||
import arrow.core.getOrElse
|
||||
import com.ehviewer.core.model.GalleryTagGroup
|
||||
import com.hippo.ehviewer.client.exception.ParseException
|
||||
import java.nio.ByteBuffer
|
||||
|
||||
object VoteTagParser {
|
||||
// {"error":"The tag \"neko\" is not allowed. Use character:neko or artist:neko"}
|
||||
fun parse(body: String): String? = runCatching { body.parseAs<Error>().error }.getOrNull()
|
||||
fun parse(body: ByteBuffer) = catch {
|
||||
unmarshalParsingAs<List<GalleryTagGroup>>(body, ::nativeParse)
|
||||
}.getOrElse {
|
||||
throw ParseException("Failed to parse vote tag result", it)
|
||||
}
|
||||
|
||||
private external fun nativeParse(body: ByteBuffer, size: Int = body.limit()): Int
|
||||
}
|
||||
+4
-1
@@ -3,4 +3,7 @@ package com.hippo.ehviewer.client.parser
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class Error(val error: String)
|
||||
data class VoteTagResult(
|
||||
val error: String?,
|
||||
val tagpane: String?,
|
||||
)
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.hippo.ehviewer.coil
|
||||
|
||||
import coil3.ImageLoader
|
||||
import coil3.asImage
|
||||
import coil3.decode.ByteBufferMetadata
|
||||
import coil3.decode.ContentMetadata
|
||||
import coil3.decode.DecodeResult
|
||||
import coil3.decode.DecodeUtils
|
||||
import coil3.decode.Decoder
|
||||
import coil3.decode.ImageSource
|
||||
import coil3.fetch.SourceFetchResult
|
||||
import coil3.gif.isAnimatedWebP
|
||||
import coil3.request.Options
|
||||
import java.io.FileInputStream
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.channels.FileChannel
|
||||
import okio.FileSystem
|
||||
|
||||
class AnimatedWebPDecoder(private val source: ByteBuffer) : Decoder {
|
||||
override suspend fun decode() = DecodeResult(AnimatedWebPDrawable(source).asImage(), false)
|
||||
|
||||
object Factory : Decoder.Factory {
|
||||
override fun create(
|
||||
result: SourceFetchResult,
|
||||
options: Options,
|
||||
imageLoader: ImageLoader,
|
||||
) = if (DecodeUtils.isAnimatedWebP(result.source.source())) {
|
||||
result.source.toByteBufferOrNull()?.let { AnimatedWebPDecoder(it) }
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun ImageSource.toByteBufferOrNull(): ByteBuffer? {
|
||||
if (fileSystem === FileSystem.SYSTEM) {
|
||||
val file = fileOrNull()
|
||||
if (file != null) {
|
||||
return file.toFile().inputStream().mapReadOnly()
|
||||
}
|
||||
}
|
||||
return when (val metadata = metadata) {
|
||||
is ContentMetadata -> metadata.assetFileDescriptor.createInputStream().mapReadOnly()
|
||||
is ByteBufferMetadata -> metadata.byteBuffer
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
private fun FileInputStream.mapReadOnly(): ByteBuffer = channel.use { it.map(FileChannel.MapMode.READ_ONLY, 0, it.size()) }
|
||||
@@ -0,0 +1,151 @@
|
||||
package com.hippo.ehviewer.coil
|
||||
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.Canvas
|
||||
import android.graphics.ColorFilter
|
||||
import android.graphics.Paint
|
||||
import android.graphics.PixelFormat
|
||||
import android.graphics.drawable.Animatable
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.os.SystemClock
|
||||
import androidx.core.graphics.createBitmap
|
||||
import com.ehviewer.core.util.logcat
|
||||
import java.nio.ByteBuffer
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.cancelAndJoin
|
||||
import kotlinx.coroutines.ensureActive
|
||||
import kotlinx.coroutines.job
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runBlocking
|
||||
|
||||
// Hold a reference to the buffer as it's used by the decoder
|
||||
@Suppress("CanBeParameter")
|
||||
class AnimatedWebPDrawable(private val source: ByteBuffer) : Drawable(), Animatable {
|
||||
private val decodeScope = CoroutineScope(Dispatchers.IO.limitedParallelism(1))
|
||||
private val paint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG or Paint.FILTER_BITMAP_FLAG)
|
||||
private val decoder = nativeCreateDecoder(source)
|
||||
private val width: Int
|
||||
private val height: Int
|
||||
private val loopCount: Int
|
||||
|
||||
private var decodeJob: Job? = null
|
||||
private var loopsCompleted = 0
|
||||
private var timeToShowNextFrame = 0L
|
||||
private var currentFrame: Frame
|
||||
private var nextFrame: Frame
|
||||
|
||||
init {
|
||||
check(decoder != 0L) { "Failed to create decoder" }
|
||||
val packed = nativeGetImageInfo(decoder)
|
||||
width = (packed shr 40).toInt()
|
||||
height = (packed shr 16 and 0xFFFFFF).toInt()
|
||||
loopCount = (packed and 0xFFFF).toInt()
|
||||
val bitmap = createBitmap(width, height)
|
||||
val timestamp = nativeDecodeNextFrame(decoder, false, bitmap)
|
||||
check(timestamp != 0) {
|
||||
nativeDestroyDecoder(decoder)
|
||||
"Failed to decode first frame"
|
||||
}
|
||||
currentFrame = Frame(bitmap, timestamp)
|
||||
nextFrame = Frame(createBitmap(width, height), 0)
|
||||
}
|
||||
|
||||
override fun getIntrinsicWidth() = width
|
||||
|
||||
override fun getIntrinsicHeight() = height
|
||||
|
||||
private val runnable = Runnable {
|
||||
timeToShowNextFrame = SystemClock.uptimeMillis() + nextFrame.timestamp - currentFrame.timestamp
|
||||
invalidateSelf()
|
||||
}
|
||||
|
||||
private fun decodeNextFrame(reset: Boolean) = decodeScope.launch {
|
||||
val timestamp = nativeDecodeNextFrame(decoder, reset, nextFrame.bitmap)
|
||||
ensureActive()
|
||||
check(timestamp != 0) {
|
||||
decodeJob = null
|
||||
"Failed to decode next frame"
|
||||
}
|
||||
if (timestamp <= currentFrame.timestamp) {
|
||||
if (reset) loopsCompleted = 0 else loopsCompleted++
|
||||
currentFrame.timestamp = 0
|
||||
}
|
||||
nextFrame.timestamp = timestamp
|
||||
nextFrame.bitmap.prepareToDraw()
|
||||
}.apply {
|
||||
invokeOnCompletion { cause ->
|
||||
when (cause) {
|
||||
null -> if (reset) {
|
||||
runnable.run()
|
||||
} else {
|
||||
scheduleSelf(runnable, timeToShowNextFrame)
|
||||
}
|
||||
!is CancellationException -> logcat(cause)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun draw(canvas: Canvas) {
|
||||
if (decodeJob?.isCompleted == true && isVisible) {
|
||||
decodeJob = if (loopCount == 0 || loopsCompleted < loopCount) {
|
||||
currentFrame = nextFrame.also { nextFrame = currentFrame }
|
||||
decodeNextFrame(false)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
canvas.drawBitmap(currentFrame.bitmap, null, bounds, paint)
|
||||
}
|
||||
|
||||
@Deprecated("Deprecated in Java")
|
||||
override fun getOpacity(): Int = PixelFormat.TRANSLUCENT
|
||||
|
||||
override fun setAlpha(alpha: Int) {
|
||||
if (alpha != paint.alpha) {
|
||||
paint.alpha = alpha
|
||||
invalidateSelf()
|
||||
}
|
||||
}
|
||||
|
||||
override fun getAlpha(): Int = paint.alpha
|
||||
|
||||
override fun setColorFilter(colorFilter: ColorFilter?) {
|
||||
if (colorFilter != paint.colorFilter) {
|
||||
paint.colorFilter = colorFilter
|
||||
invalidateSelf()
|
||||
}
|
||||
}
|
||||
|
||||
override fun getColorFilter(): ColorFilter? = paint.colorFilter
|
||||
|
||||
override fun isRunning() = decodeJob != null
|
||||
|
||||
override fun start() {
|
||||
if (decodeJob == null) {
|
||||
decodeJob = decodeNextFrame(true)
|
||||
}
|
||||
}
|
||||
|
||||
override fun stop() {
|
||||
decodeJob?.cancel()
|
||||
decodeJob = null
|
||||
unscheduleSelf(runnable)
|
||||
}
|
||||
|
||||
fun dispose() {
|
||||
runBlocking {
|
||||
decodeScope.coroutineContext.job.cancelAndJoin()
|
||||
}
|
||||
nativeDestroyDecoder(decoder)
|
||||
}
|
||||
}
|
||||
|
||||
private class Frame(val bitmap: Bitmap, var timestamp: Int)
|
||||
|
||||
private external fun nativeCreateDecoder(source: ByteBuffer): Long
|
||||
private external fun nativeGetImageInfo(decoder: Long): Long
|
||||
private external fun nativeDecodeNextFrame(decoder: Long, reset: Boolean, bitmap: Bitmap): Int
|
||||
private external fun nativeDestroyDecoder(decoder: Long)
|
||||
@@ -12,14 +12,11 @@ import coil3.asImage
|
||||
import coil3.intercept.Interceptor
|
||||
import coil3.request.ImageResult
|
||||
import coil3.request.SuccessResult
|
||||
import com.ehviewer.core.util.isAtLeastQ
|
||||
import com.ehviewer.core.util.logcat
|
||||
import com.hippo.ehviewer.image.copyBitmapToAHB
|
||||
import com.hippo.ehviewer.util.isAtLeastQ
|
||||
import eu.kanade.tachiyomi.util.system.logcat
|
||||
import moe.tarsin.coroutines.runSuspendCatching
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.O)
|
||||
private const val FORMAT = HardwareBuffer.RGBA_8888
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.O)
|
||||
private const val USAGE = HardwareBuffer.USAGE_CPU_WRITE_RARELY or HardwareBuffer.USAGE_GPU_SAMPLED_IMAGE
|
||||
|
||||
@@ -42,8 +39,14 @@ object CropBorderInterceptor : Interceptor {
|
||||
val meetHardwareThreshold = maxOf(w, h) <= chain.request.hardwareThreshold
|
||||
val bitmap = when {
|
||||
isAtLeastQ && meetHardwareThreshold -> runSuspendCatching {
|
||||
val format = when (val config = src.config) {
|
||||
Bitmap.Config.ARGB_8888 -> HardwareBuffer.RGBA_8888
|
||||
Bitmap.Config.RGB_565 -> HardwareBuffer.RGB_565
|
||||
Bitmap.Config.RGBA_F16 -> HardwareBuffer.RGBA_FP16
|
||||
else -> error("Unsupported bitmap config: $config")
|
||||
}
|
||||
resourceScope {
|
||||
val buffer = autoCloseable { HardwareBuffer.create(w, h, FORMAT, 1, USAGE) }
|
||||
val buffer = autoCloseable { HardwareBuffer.create(w, h, format, 1, USAGE) }
|
||||
copyBitmapToAHB(src, buffer, x, y)
|
||||
Bitmap.wrapHardwareBuffer(buffer, src.colorSpace)
|
||||
}
|
||||
|
||||
@@ -1,41 +1,64 @@
|
||||
package com.hippo.ehviewer.coil
|
||||
|
||||
import coil3.Extras
|
||||
import coil3.getExtra
|
||||
import coil3.intercept.Interceptor
|
||||
import coil3.request.ImageRequest
|
||||
import coil3.request.ImageResult
|
||||
import coil3.request.SuccessResult
|
||||
import com.ehviewer.core.database.model.DownloadInfo
|
||||
import com.ehviewer.core.files.delete
|
||||
import com.ehviewer.core.files.isDirectory
|
||||
import com.ehviewer.core.files.isFile
|
||||
import com.ehviewer.core.files.sendTo
|
||||
import com.ehviewer.core.files.toUri
|
||||
import com.hippo.ehviewer.EhApplication.Companion.imageCache
|
||||
import com.hippo.ehviewer.EhDB
|
||||
import com.hippo.ehviewer.client.getThumbKey
|
||||
import com.hippo.ehviewer.download.downloadLocation
|
||||
import com.hippo.ehviewer.spider.DownloadInfoMagics.decodeMagicRequestOrUrl
|
||||
import com.hippo.ehviewer.util.sendTo
|
||||
import com.hippo.files.isDirectory
|
||||
import com.hippo.files.isFile
|
||||
import com.hippo.files.toUri
|
||||
|
||||
private val downloadInfoKey = Extras.Key<DownloadInfo?>(default = null)
|
||||
|
||||
fun ImageRequest.Builder.downloadInfo(info: DownloadInfo) = apply {
|
||||
extras[downloadInfoKey] = info
|
||||
}
|
||||
|
||||
val ImageRequest.downloadInfo: DownloadInfo?
|
||||
get() = getExtra(downloadInfoKey)
|
||||
|
||||
object DownloadThumbInterceptor : Interceptor {
|
||||
private const val THUMB_FILE = "thumb.jpg"
|
||||
override suspend fun intercept(chain: Interceptor.Chain): ImageResult {
|
||||
val magicOrUrl = chain.request.data as? String
|
||||
if (magicOrUrl != null) {
|
||||
val (url, location) = decodeMagicRequestOrUrl(magicOrUrl)
|
||||
if (location != null) {
|
||||
val thumb = downloadLocation / location / THUMB_FILE
|
||||
if (thumb.isFile) {
|
||||
val new = chain.request.newBuilder().data(thumb.toUri()).build()
|
||||
val result = chain.withRequest(new).proceed()
|
||||
if (result is SuccessResult) return result
|
||||
}
|
||||
val new = chain.request.newBuilder().data(url).build()
|
||||
val result = chain.withRequest(new).proceed()
|
||||
if (result is SuccessResult && thumb.parent?.isDirectory == true) {
|
||||
// Accessing the recreated file immediately after deleting it throws
|
||||
// FileNotFoundException, so we just overwrite the existing file.
|
||||
val key = requireNotNull(chain.request.memoryCacheKey)
|
||||
imageCache.read(key) {
|
||||
data sendTo thumb
|
||||
}
|
||||
}
|
||||
return result
|
||||
val info = chain.request.downloadInfo
|
||||
if (info != null && !info.dirname.isNullOrBlank()) {
|
||||
val thumbKey = getThumbKey(chain.request.data as String)
|
||||
if (info.thumbKey != thumbKey) {
|
||||
info.thumbKey = thumbKey
|
||||
EhDB.putGalleryInfo(info.galleryInfo)
|
||||
}
|
||||
val dir = downloadLocation / info.dirname!!
|
||||
val format = thumbKey.substringAfterLast('.', "")
|
||||
check(format.isNotBlank())
|
||||
val thumb = dir / "thumb.$format"
|
||||
val v1Thumb = dir / "thumb.jpg"
|
||||
if (thumb.isFile) {
|
||||
val new = chain.request.newBuilder().data(thumb.toUri()).build()
|
||||
val result = chain.withRequest(new).proceed()
|
||||
if (result is SuccessResult) {
|
||||
if (thumb != v1Thumb) v1Thumb.delete()
|
||||
return result
|
||||
}
|
||||
}
|
||||
val result = chain.proceed()
|
||||
if (result is SuccessResult && dir.isDirectory) {
|
||||
// Accessing the recreated file immediately after deleting it throws
|
||||
// FileNotFoundException, so we just overwrite the existing file.
|
||||
val key = requireNotNull(chain.request.memoryCacheKey)
|
||||
imageCache.read(key) {
|
||||
data sendTo thumb
|
||||
}
|
||||
if (thumb != v1Thumb) v1Thumb.delete()
|
||||
}
|
||||
return result
|
||||
}
|
||||
return chain.proceed()
|
||||
}
|
||||
|
||||
@@ -1,52 +1,61 @@
|
||||
package com.hippo.ehviewer.coil
|
||||
|
||||
import android.graphics.Color
|
||||
import android.graphics.drawable.ColorDrawable
|
||||
import coil3.asImage
|
||||
import coil3.decode.DecodeResult
|
||||
import coil3.decode.Decoder
|
||||
import coil3.network.NetworkHeaders
|
||||
import coil3.network.httpHeaders
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.paging.compose.LazyPagingItems
|
||||
import coil3.decode.BlackholeDecoder
|
||||
import coil3.request.CachePolicy
|
||||
import coil3.request.ImageRequest
|
||||
import coil3.size.Size
|
||||
import com.hippo.ehviewer.Settings
|
||||
import com.hippo.ehviewer.client.CHROME_ACCEPT
|
||||
import com.hippo.ehviewer.client.CHROME_ACCEPT_LANGUAGE
|
||||
import com.hippo.ehviewer.client.data.GalleryInfo
|
||||
import com.hippo.ehviewer.client.data.GalleryPreview
|
||||
import com.hippo.ehviewer.client.data.NormalGalleryPreview
|
||||
import com.hippo.ehviewer.client.imageKey
|
||||
import com.hippo.ehviewer.spider.DownloadInfoMagics.encodeMagicRequestOrUrl
|
||||
import io.ktor.http.HttpHeaders
|
||||
|
||||
private val header = NetworkHeaders.Builder().apply {
|
||||
add(HttpHeaders.UserAgent, Settings.userAgent)
|
||||
add(HttpHeaders.Accept, CHROME_ACCEPT)
|
||||
add(HttpHeaders.AcceptLanguage, CHROME_ACCEPT_LANGUAGE)
|
||||
}.build()
|
||||
import coil3.size.SizeResolver
|
||||
import com.ehviewer.core.database.model.DownloadInfo
|
||||
import com.ehviewer.core.model.GalleryInfo
|
||||
import com.ehviewer.core.model.GalleryPreview
|
||||
import com.ehviewer.core.model.V2GalleryPreview
|
||||
import com.hippo.ehviewer.client.getThumbKey
|
||||
import com.hippo.ehviewer.client.getV2PreviewKey
|
||||
import com.hippo.ehviewer.client.thumbUrl
|
||||
import com.hippo.ehviewer.download.DownloadManager
|
||||
import com.hippo.ehviewer.ktbuilder.execute
|
||||
|
||||
// Load in original size so the memory cache can be reused for preload requests
|
||||
fun ImageRequest.Builder.ehUrl(info: GalleryInfo) = apply {
|
||||
val key = info.thumbKey!!
|
||||
data(encodeMagicRequestOrUrl(info))
|
||||
data(info.thumbUrl)
|
||||
size(SizeResolver.ORIGINAL)
|
||||
val downloadInfo = (info as? DownloadInfo) ?: DownloadManager.getDownloadInfo(info.gid)
|
||||
if (downloadInfo != null) {
|
||||
downloadInfo(downloadInfo)
|
||||
}
|
||||
memoryCacheKey(key)
|
||||
diskCacheKey(key)
|
||||
httpHeaders(header)
|
||||
}
|
||||
|
||||
// Load in original size so the memory cache can be reused for preload requests
|
||||
fun ImageRequest.Builder.ehPreview(preview: GalleryPreview) = apply {
|
||||
data(preview.url)
|
||||
memoryCacheKey(preview.imageKey)
|
||||
diskCacheKey(preview.imageKey)
|
||||
if (preview is NormalGalleryPreview) size(Size.ORIGINAL)
|
||||
httpHeaders(header)
|
||||
with(preview) {
|
||||
val key = if (this is V2GalleryPreview) getV2PreviewKey(url) else getThumbKey(url)
|
||||
data(url)
|
||||
size(SizeResolver.ORIGINAL)
|
||||
memoryCacheKey(key)
|
||||
diskCacheKey(key)
|
||||
}
|
||||
}
|
||||
|
||||
private val stubImage = ColorDrawable(Color.BLACK).asImage(true)
|
||||
private val stubResult = DecodeResult(stubImage, false)
|
||||
private val stubFactory = Decoder { stubResult }
|
||||
|
||||
fun ImageRequest.Builder.justDownload() = apply {
|
||||
memoryCachePolicy(CachePolicy.DISABLED)
|
||||
decoderFactory { _, _, _ -> stubFactory }
|
||||
decoderFactory(BlackholeDecoder.Factory())
|
||||
}
|
||||
|
||||
@Composable
|
||||
inline fun <T : Any> PrefetchAround(data: LazyPagingItems<T>, index: Int, distance: Int, crossinline f: (T) -> ImageRequest) {
|
||||
data.peek((index - distance).coerceAtLeast(0))?.let { fetchBefore ->
|
||||
LaunchedEffect(fetchBefore) {
|
||||
f(fetchBefore).execute()
|
||||
}
|
||||
}
|
||||
data.peek((index + distance).coerceAtMost(data.itemCount - 1))?.let { fetchAhead ->
|
||||
LaunchedEffect(fetchAhead) {
|
||||
f(fetchAhead).execute()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,84 +1,67 @@
|
||||
package com.hippo.ehviewer.coil
|
||||
|
||||
import arrow.core.merge
|
||||
import arrow.fx.coroutines.raceN
|
||||
import coil3.decode.DataSource
|
||||
import coil3.imageLoader
|
||||
import coil3.intercept.Interceptor
|
||||
import coil3.memory.MemoryCache
|
||||
import coil3.request.ImageResult
|
||||
import com.hippo.ehviewer.Settings
|
||||
import com.hippo.ehviewer.client.isNormalPreviewKey
|
||||
import kotlin.coroutines.resume
|
||||
import kotlin.coroutines.resumeWithException
|
||||
import coil3.request.SuccessResult
|
||||
import kotlinx.coroutines.CancellableContinuation
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.cancel
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.buffer
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.receiveAsFlow
|
||||
import kotlinx.coroutines.flow.shareIn
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.suspendCancellableCoroutine
|
||||
import moe.tarsin.coroutines.Counter
|
||||
import moe.tarsin.coroutines.Tracker
|
||||
import moe.tarsin.coroutines.counter
|
||||
import moe.tarsin.coroutines.runSuspendCatching
|
||||
import moe.tarsin.coroutines.use
|
||||
|
||||
typealias F = suspend () -> Unit
|
||||
|
||||
suspend inline fun <T> CancellableContinuation<T>.evalAndResume(f: suspend () -> T) = runSuspendCatching { f() }.takeIf { isActive }?.let(::resumeWith)
|
||||
|
||||
class TightRope : CoroutineScope, Counter by counter() {
|
||||
override val coroutineContext = Dispatchers.IO + Job()
|
||||
val actions = Channel<F>()
|
||||
val flow = actions.receiveAsFlow().map(F::invoke).buffer(0).shareIn(this, SharingStarted.WhileSubscribed(stopTimeoutMillis = 200))
|
||||
suspend inline fun <R> sendAndAwait(crossinline block: suspend () -> R) = raceN(
|
||||
{ flow.collect {} },
|
||||
{ suspendCancellableCoroutine { cont -> launch { actions.send { cont.evalAndResume(block) } } } },
|
||||
).merge()
|
||||
}
|
||||
|
||||
object TightRopeTracker : Tracker<TightRope, String>() {
|
||||
override fun new() = TightRope()
|
||||
override fun free(e: TightRope) = e.cancel()
|
||||
}
|
||||
|
||||
object MergeInterceptor : Interceptor {
|
||||
private val pendingContinuationMap: HashMap<String, MutableList<CancellableContinuation<Unit>>> = hashMapOf()
|
||||
private val pendingContinuationMapLock = Any()
|
||||
private val notifyScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
|
||||
private val EMPTY_LIST = mutableListOf<CancellableContinuation<Unit>>()
|
||||
|
||||
private fun triggerSuccessor(key: String) {
|
||||
notifyScope.launch {
|
||||
synchronized(pendingContinuationMapLock) {
|
||||
// Wake up a pending continuation to continue executing task
|
||||
val successor = pendingContinuationMap[key]?.removeFirstOrNull()?.apply {
|
||||
resume(Unit) { _, _, _ -> triggerSuccessor(key) }
|
||||
}
|
||||
// If no successor, delete this entry from hashmap
|
||||
successor ?: pendingContinuationMap.remove(key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun triggerFailure(key: String, e: Throwable) {
|
||||
notifyScope.launch {
|
||||
synchronized(pendingContinuationMapLock) {
|
||||
pendingContinuationMap.remove(key)?.forEach { it.resumeWithException(e) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun intercept(chain: Interceptor.Chain): ImageResult {
|
||||
val req = chain.request
|
||||
val key = req.memoryCacheKey?.takeIf { it.isNormalPreviewKey || Settings.preloadThumbAggressively } ?: return chain.proceed()
|
||||
|
||||
suspendCancellableCoroutine { continuation ->
|
||||
synchronized(pendingContinuationMapLock) {
|
||||
val existPendingContinuations = pendingContinuationMap[key]
|
||||
if (existPendingContinuations == null) {
|
||||
pendingContinuationMap[key] = EMPTY_LIST
|
||||
continuation.resume(Unit) { _, _, _ ->
|
||||
triggerSuccessor(key)
|
||||
}
|
||||
} else {
|
||||
if (existPendingContinuations === EMPTY_LIST) pendingContinuationMap[key] = mutableListOf()
|
||||
pendingContinuationMap[key]!!.apply {
|
||||
add(continuation)
|
||||
continuation.invokeOnCancellation { remove(continuation) }
|
||||
}
|
||||
val key = req.memoryCacheKey
|
||||
return if (key != null) {
|
||||
val cacheKey = MemoryCache.Key(key, req.memoryCacheKeyExtras)
|
||||
val cached = req.context.imageLoader.memoryCache!![cacheKey] != null
|
||||
val result = TightRopeTracker.use(key) { sendAndAwait { chain.proceed() } }
|
||||
when (result) {
|
||||
is SuccessResult if !cached && result.dataSource == DataSource.MEMORY_CACHE -> {
|
||||
result.copy(dataSource = DataSource.MEMORY)
|
||||
}
|
||||
else -> result
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return chain.proceed().apply {
|
||||
// Wake all pending continuations shared with the same memory key since we have written it to memory cache
|
||||
notifyScope.launch {
|
||||
synchronized(pendingContinuationMapLock) {
|
||||
pendingContinuationMap.remove(key)?.forEach { it.resume(Unit) }
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e: CancellationException) {
|
||||
triggerSuccessor(key)
|
||||
throw e
|
||||
} catch (e: Throwable) {
|
||||
// Wake all pending continuations since this request is to be failed
|
||||
triggerFailure(key, e)
|
||||
throw e
|
||||
} else {
|
||||
chain.proceed()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package com.hippo.ehviewer.cronet
|
||||
|
||||
import com.hippo.ehviewer.Settings
|
||||
import java.io.File
|
||||
import org.chromium.net.CronetEngine
|
||||
import splitties.init.appCtx
|
||||
|
||||
val cronetHttpClient: CronetEngine = CronetEngine.Builder(appCtx).apply {
|
||||
enableBrotli(true)
|
||||
setUserAgent(Settings.userAgent)
|
||||
|
||||
// Cache Quic hint only since the real cache mechanism should on Ktor layer
|
||||
val cache = File(appCtx.cacheDir, "http_cache").apply { mkdirs() }
|
||||
setStoragePath(cache.path)
|
||||
enableHttpCache(CronetEngine.Builder.HTTP_CACHE_DISK_NO_HTTP, 4096)
|
||||
|
||||
addQuicHint("e-hentai.org", 443, 443)
|
||||
addQuicHint("forums.e-hentai.org", 443, 443)
|
||||
addQuicHint("exhentai.org", 443, 443)
|
||||
}.build()
|
||||
@@ -3,12 +3,12 @@ package com.hippo.ehviewer.dailycheck
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.text.style.URLSpan
|
||||
import androidx.core.app.NotificationChannelCompat
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.core.app.NotificationManagerCompat
|
||||
import androidx.core.app.PendingIntentCompat
|
||||
import androidx.core.net.toUri
|
||||
import androidx.core.text.getSpans
|
||||
import androidx.core.text.parseAsHtml
|
||||
import androidx.work.Constraints
|
||||
@@ -19,15 +19,15 @@ import androidx.work.PeriodicWorkRequest
|
||||
import androidx.work.PeriodicWorkRequestBuilder
|
||||
import androidx.work.WorkManager
|
||||
import androidx.work.WorkerParameters
|
||||
import com.ehviewer.core.util.logcat
|
||||
import com.ehviewer.core.util.withIOContext
|
||||
import com.hippo.ehviewer.R
|
||||
import com.hippo.ehviewer.Settings
|
||||
import com.hippo.ehviewer.client.EhEngine
|
||||
import eu.kanade.tachiyomi.util.lang.withIOContext
|
||||
import eu.kanade.tachiyomi.util.system.logcat
|
||||
import java.time.Duration
|
||||
import java.util.concurrent.TimeUnit
|
||||
import kotlin.time.Clock
|
||||
import kotlin.time.Duration.Companion.days
|
||||
import kotlinx.datetime.Clock
|
||||
import kotlinx.datetime.LocalDateTime
|
||||
import kotlinx.datetime.LocalTime
|
||||
import kotlinx.datetime.TimeZone
|
||||
@@ -66,7 +66,7 @@ private fun getDailyCheckWorkRequest(): PeriodicWorkRequest {
|
||||
private const val WORK_NAME = "DailyCheckWork"
|
||||
|
||||
fun updateDailyCheckWork(context: Context) {
|
||||
if (Settings.requestNews) {
|
||||
if (Settings.requestNews.value) {
|
||||
WorkManager.getInstance(context).enqueueUniquePeriodicWork(
|
||||
WORK_NAME,
|
||||
ExistingPeriodicWorkPolicy.CANCEL_AND_REENQUEUE,
|
||||
@@ -93,7 +93,7 @@ suspend fun checkDawn() = runCatching {
|
||||
|
||||
@SuppressLint("MissingPermission")
|
||||
fun showEventNotification(html: String) {
|
||||
if (Settings.hideHvEvents && html.contains("You have encountered a monster!")) {
|
||||
if (Settings.hideHvEvents.value && html.contains("You have encountered a monster!")) {
|
||||
return
|
||||
}
|
||||
val notificationManager = NotificationManagerCompat.from(appCtx)
|
||||
@@ -110,7 +110,7 @@ fun showEventNotification(html: String) {
|
||||
.setStyle(NotificationCompat.BigTextStyle())
|
||||
val urls = text.getSpans<URLSpan>(0, text.length)
|
||||
if (urls.isNotEmpty()) {
|
||||
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(urls.first().url))
|
||||
val intent = Intent(Intent.ACTION_VIEW, urls.first().url.toUri())
|
||||
val pi = PendingIntentCompat.getActivity(appCtx, 0, intent, 0, false)
|
||||
msg.setContentIntent(pi)
|
||||
}
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
package com.hippo.ehviewer.dao
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.ForeignKey
|
||||
import com.hippo.ehviewer.client.data.BaseGalleryInfo
|
||||
import kotlinx.datetime.Clock
|
||||
|
||||
@Entity(tableName = "HISTORY", foreignKeys = [ForeignKey(BaseGalleryInfo::class, ["GID"], ["GID"])])
|
||||
class HistoryInfo(gid: Long, time: Long = Clock.System.now().toEpochMilliseconds()) : TimeInfo(gid, time)
|
||||
@@ -22,19 +22,31 @@ import androidx.compose.runtime.DisallowComposableCalls
|
||||
import androidx.compose.runtime.Stable
|
||||
import androidx.compose.runtime.State
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateListOf
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.toMutableStateList
|
||||
import androidx.compose.ui.util.lerp
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.core.util.size
|
||||
import arrow.fx.coroutines.parMapNotNull
|
||||
import com.ehviewer.core.data.model.asEntity
|
||||
import com.ehviewer.core.database.model.DownloadArtist
|
||||
import com.ehviewer.core.database.model.DownloadInfo
|
||||
import com.ehviewer.core.database.model.DownloadLabel
|
||||
import com.ehviewer.core.files.delete
|
||||
import com.ehviewer.core.files.find
|
||||
import com.ehviewer.core.files.toOkioPath
|
||||
import com.ehviewer.core.files.toUri
|
||||
import com.ehviewer.core.model.BaseGalleryInfo
|
||||
import com.ehviewer.core.model.GalleryInfo
|
||||
import com.ehviewer.core.preferences.edit
|
||||
import com.ehviewer.core.util.logcat
|
||||
import com.ehviewer.core.util.mapNotNull
|
||||
import com.hippo.ehviewer.EhDB
|
||||
import com.hippo.ehviewer.Settings
|
||||
import com.hippo.ehviewer.client.data.BaseGalleryInfo
|
||||
import com.hippo.ehviewer.client.data.GalleryInfo
|
||||
import com.hippo.ehviewer.dao.DownloadArtist
|
||||
import com.hippo.ehviewer.dao.DownloadInfo
|
||||
import com.hippo.ehviewer.dao.DownloadLabel
|
||||
import com.hippo.ehviewer.image.Image
|
||||
import com.hippo.ehviewer.client.exception.FatalException
|
||||
import com.hippo.ehviewer.spider.COMIC_INFO_FILE
|
||||
import com.hippo.ehviewer.spider.SpeedTracker
|
||||
import com.hippo.ehviewer.spider.SpiderQueen
|
||||
import com.hippo.ehviewer.spider.SpiderQueen.Companion.SPIDER_INFO_FILENAME
|
||||
import com.hippo.ehviewer.spider.SpiderQueen.OnSpiderListener
|
||||
@@ -44,17 +56,10 @@ import com.hippo.ehviewer.spider.readCompatFromPath
|
||||
import com.hippo.ehviewer.spider.toSimpleTags
|
||||
import com.hippo.ehviewer.util.AppConfig
|
||||
import com.hippo.ehviewer.util.insertWith
|
||||
import com.hippo.ehviewer.util.mapNotNull
|
||||
import com.hippo.ehviewer.util.runAssertingNotMainThread
|
||||
import com.hippo.files.delete
|
||||
import com.hippo.files.find
|
||||
import com.hippo.files.toOkioPath
|
||||
import com.hippo.files.toUri
|
||||
import eu.kanade.tachiyomi.util.system.logcat
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.asSharedFlow
|
||||
import kotlinx.coroutines.flow.transform
|
||||
@@ -65,15 +70,12 @@ import logcat.LogPriority
|
||||
import okio.Path
|
||||
import okio.Path.Companion.toOkioPath
|
||||
import okio.Path.Companion.toPath
|
||||
import splitties.preferences.edit
|
||||
|
||||
object DownloadManager : OnSpiderListener, CoroutineScope {
|
||||
override val coroutineContext = Dispatchers.IO + Job()
|
||||
|
||||
// All download info list
|
||||
private val allInfoList = runAssertingNotMainThread {
|
||||
(EhDB.getAllDownloadInfo() as MutableList).apply { sortWith(comparator()) }
|
||||
}
|
||||
private val allInfoList = runAssertingNotMainThread { EhDB.getAllDownloadInfo() as MutableList }
|
||||
|
||||
val downloadInfoList: List<DownloadInfo>
|
||||
get() = allInfoList
|
||||
@@ -82,7 +84,10 @@ object DownloadManager : OnSpiderListener, CoroutineScope {
|
||||
private val mAllInfoMap = allInfoList.associateBy { it.gid } as MutableMap<Long, DownloadInfo>
|
||||
|
||||
// All labels without default label
|
||||
val labelList = runAssertingNotMainThread { EhDB.getAllDownloadLabelList() }.toMutableStateList()
|
||||
// Create the SnapshotStateList first in case the database query is time-consuming
|
||||
val labelList = mutableStateListOf<DownloadLabel>().apply {
|
||||
addAll(runAssertingNotMainThread { EhDB.getAllDownloadLabelList() })
|
||||
}
|
||||
|
||||
// Store download info wait to start
|
||||
private val mWaitList = ArrayDeque<DownloadInfo>()
|
||||
@@ -95,6 +100,21 @@ object DownloadManager : OnSpiderListener, CoroutineScope {
|
||||
|
||||
private val mutex = Mutex()
|
||||
|
||||
private val sortMutex = Mutex()
|
||||
private val sortMode
|
||||
get() = SortMode.from(Settings.downloadSortMode.value)
|
||||
|
||||
var isInitialized by mutableStateOf(false)
|
||||
private set
|
||||
|
||||
init {
|
||||
launch {
|
||||
val mode = sortMode
|
||||
if (mode != SortMode.Default) sortDownloads(mode)
|
||||
isInitialized = true
|
||||
}
|
||||
}
|
||||
|
||||
fun containLabel(label: String?): Boolean {
|
||||
if (label == null) {
|
||||
return false
|
||||
@@ -176,11 +196,13 @@ object DownloadManager : OnSpiderListener, CoroutineScope {
|
||||
}
|
||||
} else {
|
||||
// It is new download info
|
||||
info = DownloadInfo(galleryInfo, galleryInfo.downloadDirname())
|
||||
info = DownloadInfo(galleryInfo.asEntity(), galleryInfo.downloadDirname())
|
||||
info.label = label
|
||||
info.state = DownloadInfo.STATE_WAIT
|
||||
// Add to all download list and map
|
||||
allInfoList.insertWith(info, comparator())
|
||||
sortMutex.withLock {
|
||||
allInfoList.insertWith(info, sortMode.comparator())
|
||||
}
|
||||
mAllInfoMap[galleryInfo.gid] = info
|
||||
|
||||
// Add to wait list
|
||||
@@ -195,7 +217,7 @@ object DownloadManager : OnSpiderListener, CoroutineScope {
|
||||
ensureDownload()
|
||||
|
||||
// Add it to history
|
||||
EhDB.putHistoryInfo(info.galleryInfo)
|
||||
EhDB.putHistoryInfo(info)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -237,10 +259,12 @@ object DownloadManager : OnSpiderListener, CoroutineScope {
|
||||
}.collectAsState(transform(info)).value
|
||||
|
||||
suspend fun startAllDownload() {
|
||||
val updateList = allInfoList.filter {
|
||||
when (it.state) {
|
||||
DownloadInfo.STATE_NONE, DownloadInfo.STATE_FAILED -> true
|
||||
else -> false
|
||||
val updateList = sortMutex.withLock {
|
||||
allInfoList.filter {
|
||||
when (it.state) {
|
||||
DownloadInfo.STATE_NONE, DownloadInfo.STATE_FAILED -> true
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
}
|
||||
if (updateList.isNotEmpty()) {
|
||||
@@ -254,8 +278,8 @@ object DownloadManager : OnSpiderListener, CoroutineScope {
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun addDownload(downloadInfoList: List<DownloadInfo>) {
|
||||
val comparator = comparator()
|
||||
suspend fun addDownload(downloadInfoList: List<DownloadInfo>) = sortMutex.withLock {
|
||||
val comparator = sortMode.comparator()
|
||||
downloadInfoList.forEach { info ->
|
||||
if (containDownloadInfo(info.gid)) return@forEach
|
||||
|
||||
@@ -285,11 +309,13 @@ object DownloadManager : OnSpiderListener, CoroutineScope {
|
||||
}
|
||||
|
||||
suspend fun restoreDownload(galleryInfo: BaseGalleryInfo, dirname: String) {
|
||||
val info = DownloadInfo(galleryInfo, dirname)
|
||||
val info = DownloadInfo(galleryInfo.asEntity(), dirname)
|
||||
info.state = DownloadInfo.STATE_NONE
|
||||
|
||||
// Add to all download list and map
|
||||
allInfoList.insertWith(info, comparator())
|
||||
sortMutex.withLock {
|
||||
allInfoList.insertWith(info, sortMode.comparator())
|
||||
}
|
||||
mAllInfoMap[galleryInfo.gid] = info
|
||||
|
||||
// Save to
|
||||
@@ -309,16 +335,6 @@ object DownloadManager : OnSpiderListener, CoroutineScope {
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun stopCurrentDownload() {
|
||||
val info = stopCurrentDownloadInternal()
|
||||
if (info != null) {
|
||||
// Update listener
|
||||
mutableNotifyFlow.emit(info)
|
||||
// Ensure download
|
||||
ensureDownload()
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun stopRangeDownload(gidList: LongArray) {
|
||||
stopRangeDownloadInternal(gidList)
|
||||
|
||||
@@ -358,7 +374,9 @@ object DownloadManager : OnSpiderListener, CoroutineScope {
|
||||
EhDB.removeDownloadInfo(info)
|
||||
|
||||
// Remove all list and map
|
||||
allInfoList.remove(info)
|
||||
sortMutex.withLock {
|
||||
allInfoList.remove(info)
|
||||
}
|
||||
mAllInfoMap.remove(info.gid)
|
||||
|
||||
// Update listener
|
||||
@@ -381,7 +399,9 @@ object DownloadManager : OnSpiderListener, CoroutineScope {
|
||||
mAllInfoMap.remove(gid)
|
||||
}
|
||||
EhDB.removeDownloadInfo(list)
|
||||
allInfoList.removeAll(list.toSet())
|
||||
sortMutex.withLock {
|
||||
allInfoList.removeAll(list.toSet())
|
||||
}
|
||||
|
||||
// Update listener
|
||||
list.forEach { mutableNotifyFlow.emit(it) }
|
||||
@@ -390,7 +410,7 @@ object DownloadManager : OnSpiderListener, CoroutineScope {
|
||||
ensureDownload()
|
||||
}
|
||||
|
||||
fun sortDownloads(mode: SortMode) {
|
||||
suspend fun sortDownloads(mode: SortMode) = sortMutex.withLock {
|
||||
allInfoList.sortWith(mode.comparator())
|
||||
}
|
||||
|
||||
@@ -509,9 +529,11 @@ object DownloadManager : OnSpiderListener, CoroutineScope {
|
||||
val new = exist.copy(label = to)
|
||||
labelList.add(index, new)
|
||||
EhDB.updateDownloadLabel(new)
|
||||
allInfoList.forEach {
|
||||
if (it.label == from) {
|
||||
it.label = to
|
||||
sortMutex.withLock {
|
||||
allInfoList.forEach {
|
||||
if (it.label == from) {
|
||||
it.label = to
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -527,21 +549,25 @@ object DownloadManager : OnSpiderListener, CoroutineScope {
|
||||
}
|
||||
removeAt(index)
|
||||
}
|
||||
allInfoList.forEach {
|
||||
if (it.label == label) {
|
||||
it.label = null
|
||||
sortMutex.withLock {
|
||||
allInfoList.forEach {
|
||||
if (it.label == label) {
|
||||
it.label = null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun readMetadataFromLocal() {
|
||||
val list = allInfoList.mapNotNull {
|
||||
val updateGallery = it.pages == 0 || it.simpleTags == null
|
||||
val updateArtist = it.artistInfoList.isEmpty()
|
||||
if (updateGallery || updateArtist) {
|
||||
Triple(it, updateGallery, updateArtist)
|
||||
} else {
|
||||
null
|
||||
val list = sortMutex.withLock {
|
||||
allInfoList.mapNotNull {
|
||||
val updateGallery = it.pages == 0 || it.simpleTags == null
|
||||
val updateArtist = it.artistInfoList.isEmpty()
|
||||
if (updateGallery || updateArtist) {
|
||||
Triple(it, updateGallery, updateArtist)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
}.parMapNotNull(concurrency = 5) { (info, updateGallery, updateArtist) ->
|
||||
info.downloadDir?.run {
|
||||
@@ -582,13 +608,14 @@ object DownloadManager : OnSpiderListener, CoroutineScope {
|
||||
}
|
||||
}
|
||||
|
||||
val galleryInfoList = mutableListOf<BaseGalleryInfo>()
|
||||
list.forEach { (info, artists) ->
|
||||
info?.let {
|
||||
galleryInfoList.add(it)
|
||||
}
|
||||
artists?.let { (gid, updateList) ->
|
||||
EhDB.putDownloadArtist(gid, updateList)
|
||||
val galleryInfoList = buildList {
|
||||
list.forEach { (info, artists) ->
|
||||
info?.let {
|
||||
add(it)
|
||||
}
|
||||
artists?.let { (gid, updateList) ->
|
||||
EhDB.putDownloadArtist(gid, updateList)
|
||||
}
|
||||
}
|
||||
}
|
||||
if (galleryInfoList.isNotEmpty()) EhDB.updateGalleryInfo(galleryInfoList)
|
||||
@@ -606,9 +633,9 @@ object DownloadManager : OnSpiderListener, CoroutineScope {
|
||||
}
|
||||
}
|
||||
|
||||
override fun onGet509(index: Int) {
|
||||
override fun onFatal(error: FatalException) {
|
||||
launch {
|
||||
mDownloadListener?.onGet509()
|
||||
mDownloadListener?.onFatal(error)
|
||||
stopAllDownload()
|
||||
}
|
||||
}
|
||||
@@ -683,19 +710,11 @@ object DownloadManager : OnSpiderListener, CoroutineScope {
|
||||
}
|
||||
}
|
||||
|
||||
override fun onGetImageSuccess(index: Int, image: Image?) {
|
||||
// Ignore
|
||||
}
|
||||
|
||||
override fun onGetImageFailure(index: Int, error: String?) {
|
||||
// Ignore
|
||||
}
|
||||
|
||||
interface DownloadListener {
|
||||
/**
|
||||
* Get 509 error
|
||||
* Fatal error
|
||||
*/
|
||||
fun onGet509()
|
||||
fun onFatal(error: FatalException)
|
||||
|
||||
/**
|
||||
* Start download
|
||||
@@ -719,28 +738,24 @@ object DownloadManager : OnSpiderListener, CoroutineScope {
|
||||
}
|
||||
|
||||
private val mSpeedReminder = object {
|
||||
private val mContentLengthMap = SparseLongArray()
|
||||
private val mReceivedSizeMap = SparseLongArray()
|
||||
private val contentLengthMap = SparseLongArray()
|
||||
private val receivedSizeMap = SparseLongArray()
|
||||
private val tracker = SpeedTracker()
|
||||
private val mutex = Mutex()
|
||||
private var mBytesRead = 0L
|
||||
private var oldSpeed = -1L
|
||||
private var currentJob: Job? = null
|
||||
fun start() {
|
||||
if (currentJob == null) {
|
||||
currentJob = launch {
|
||||
while (true) {
|
||||
runInternal()
|
||||
}
|
||||
tracker.speedFlow().collect(::updateSpeed)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun stop() {
|
||||
mBytesRead = 0
|
||||
oldSpeed = -1
|
||||
tracker.reset()
|
||||
mutex.withLock {
|
||||
mContentLengthMap.clear()
|
||||
mReceivedSizeMap.clear()
|
||||
contentLengthMap.clear()
|
||||
receivedSizeMap.clear()
|
||||
}
|
||||
currentJob?.cancel()
|
||||
currentJob = null
|
||||
@@ -748,48 +763,43 @@ object DownloadManager : OnSpiderListener, CoroutineScope {
|
||||
|
||||
suspend fun onDownload(index: Int, contentLength: Long, receivedSize: Long, bytesRead: Int) {
|
||||
mutex.withLock {
|
||||
mContentLengthMap.put(index, contentLength)
|
||||
mReceivedSizeMap.put(index, receivedSize)
|
||||
contentLengthMap.put(index, contentLength)
|
||||
receivedSizeMap.put(index, receivedSize)
|
||||
}
|
||||
mBytesRead += bytesRead.toLong()
|
||||
tracker.track(bytesRead)
|
||||
}
|
||||
|
||||
suspend fun onDone(index: Int) {
|
||||
mutex.withLock {
|
||||
mContentLengthMap.delete(index)
|
||||
mReceivedSizeMap.delete(index)
|
||||
contentLengthMap.delete(index)
|
||||
receivedSizeMap.delete(index)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun onFinish() {
|
||||
mutex.withLock {
|
||||
mContentLengthMap.clear()
|
||||
mReceivedSizeMap.clear()
|
||||
contentLengthMap.clear()
|
||||
receivedSizeMap.clear()
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun runInternal() {
|
||||
private suspend fun updateSpeed(speed: Long) {
|
||||
mCurrentTask?.let { info ->
|
||||
var newSpeed = mBytesRead / 2
|
||||
if (oldSpeed != -1L) {
|
||||
newSpeed = lerp(oldSpeed, newSpeed, 0.75f)
|
||||
}
|
||||
oldSpeed = newSpeed
|
||||
info.speed = newSpeed
|
||||
info.speed = speed
|
||||
|
||||
// Calculate remaining
|
||||
if (info.total <= 0) {
|
||||
info.remaining = -1
|
||||
} else if (newSpeed == 0L) {
|
||||
} else if (speed == 0L) {
|
||||
info.remaining = 300L * 24L * 60L * 60L * 1000L // 300 days
|
||||
} else {
|
||||
var downloadingCount = 0
|
||||
var downloadingContentLengthSum = 0L
|
||||
var totalSize = 0L
|
||||
mutex.withLock {
|
||||
for (i in 0..<mContentLengthMap.size()) {
|
||||
val contentLength = mContentLengthMap.valueAt(i)
|
||||
val receivedSize = mReceivedSizeMap.valueAt(i)
|
||||
for (i in 0..<contentLengthMap.size) {
|
||||
val contentLength = contentLengthMap.valueAt(i)
|
||||
val receivedSize = receivedSizeMap.valueAt(i)
|
||||
downloadingCount++
|
||||
downloadingContentLengthSum += contentLength
|
||||
totalSize += contentLength - receivedSize
|
||||
@@ -797,20 +807,16 @@ object DownloadManager : OnSpiderListener, CoroutineScope {
|
||||
}
|
||||
if (downloadingCount != 0) {
|
||||
totalSize += downloadingContentLengthSum * (info.total - info.downloaded - downloadingCount) / downloadingCount
|
||||
info.remaining = totalSize / newSpeed * 1000
|
||||
info.remaining = totalSize / speed * 1000
|
||||
}
|
||||
}
|
||||
mDownloadListener?.onDownload(info)
|
||||
mutableNotifyFlow.emit(info)
|
||||
}
|
||||
mBytesRead = 0
|
||||
delay(2000)
|
||||
}
|
||||
}
|
||||
|
||||
private const val TAG = "DownloadManager"
|
||||
|
||||
private fun comparator() = SortMode.from(Settings.downloadSortMode.value).comparator()
|
||||
}
|
||||
|
||||
var downloadLocation: Path
|
||||
@@ -839,4 +845,4 @@ var downloadLocation: Path
|
||||
|
||||
val DownloadInfo.downloadDir get() = dirname?.let { downloadLocation / it }
|
||||
val DownloadInfo.archiveFile get() = downloadDir?.run { find("$gid.cbz") ?: find("$gid.zip") }
|
||||
val GalleryInfo.tempDownloadDir get() = AppConfig.getTempDir("$gid")?.toOkioPath()
|
||||
val GalleryInfo.tempDownloadDir get() = AppConfig.externalTempPersistDir?.let { it / "$gid" }
|
||||
@@ -16,8 +16,10 @@
|
||||
package com.hippo.ehviewer.download
|
||||
|
||||
import android.Manifest
|
||||
import android.app.ForegroundServiceStartNotAllowedException
|
||||
import android.app.PendingIntent
|
||||
import android.app.Service
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Bundle
|
||||
@@ -26,17 +28,23 @@ import androidx.core.app.ActivityCompat
|
||||
import androidx.core.app.NotificationChannelCompat
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.core.app.NotificationManagerCompat
|
||||
import androidx.core.app.ServiceCompat
|
||||
import com.hippo.ehviewer.R
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.savedstate.serialization.decodeFromSavedState
|
||||
import androidx.savedstate.serialization.encodeToSavedState
|
||||
import com.ehviewer.core.database.model.DownloadInfo
|
||||
import com.ehviewer.core.i18n.R
|
||||
import com.ehviewer.core.model.BaseGalleryInfo
|
||||
import com.ehviewer.core.util.isAtLeastS
|
||||
import com.ehviewer.core.util.logcat
|
||||
import com.ehviewer.core.util.unsafeLazy
|
||||
import com.hippo.ehviewer.client.EhUtils
|
||||
import com.hippo.ehviewer.client.data.BaseGalleryInfo
|
||||
import com.hippo.ehviewer.dao.DownloadInfo
|
||||
import com.hippo.ehviewer.client.exception.FatalException
|
||||
import com.hippo.ehviewer.client.exception.InsufficientGpException
|
||||
import com.hippo.ehviewer.client.exception.IpBannedException
|
||||
import com.hippo.ehviewer.client.exception.QuotaExceededException
|
||||
import com.hippo.ehviewer.ui.MainActivity
|
||||
import com.hippo.ehviewer.util.FileUtils
|
||||
import com.hippo.ehviewer.util.ReadableTime
|
||||
import com.hippo.ehviewer.util.getParcelableExtraCompat
|
||||
import com.hippo.ehviewer.util.unsafeLazy
|
||||
import eu.kanade.tachiyomi.util.system.logcat
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
@@ -57,7 +65,7 @@ class DownloadService :
|
||||
private val notifyManager by unsafeLazy { NotificationManagerCompat.from(this) }
|
||||
private val downloadingNotification by unsafeLazy { initDownloadingNotification() }
|
||||
private val downloadedNotification by lazy { initDownloadedNotification() }
|
||||
private val error509Notification by lazy { init509Notification() }
|
||||
private val fatalNotification by lazy { initFatalNotification() }
|
||||
private val channelId by unsafeLazy { "$packageName.download" }
|
||||
|
||||
override fun onCreate() {
|
||||
@@ -65,12 +73,18 @@ class DownloadService :
|
||||
NotificationChannelCompat.Builder(channelId, NotificationManagerCompat.IMPORTANCE_LOW)
|
||||
.setName(getString(R.string.download_service)).build(),
|
||||
)
|
||||
downloadingNotification.builder.run {
|
||||
downloadingNotification.builder.runCatching {
|
||||
setContentTitle(getString(R.string.download_service))
|
||||
.setContentText(null)
|
||||
.setSubText(null)
|
||||
.setProgress(0, 0, true)
|
||||
startForeground(ID_DOWNLOADING, build())
|
||||
}.onFailure {
|
||||
if (isAtLeastS && it is ForegroundServiceStartNotAllowedException) {
|
||||
logcat(it)
|
||||
} else {
|
||||
throw it
|
||||
}
|
||||
}
|
||||
launch {
|
||||
deferredMgr.await().setDownloadListener(this@DownloadService)
|
||||
@@ -95,11 +109,9 @@ class DownloadService :
|
||||
private suspend fun handleIntent(intent: Intent?) {
|
||||
when (intent?.action) {
|
||||
ACTION_START -> {
|
||||
val gi = intent.getParcelableExtraCompat<BaseGalleryInfo>(KEY_GALLERY_INFO)
|
||||
val gi = intent.getBundleExtra(KEY_GALLERY_INFO) ?: return
|
||||
val label = intent.getStringExtra(KEY_LABEL)
|
||||
if (gi != null) {
|
||||
deferredMgr.await().startDownload(gi, label)
|
||||
}
|
||||
deferredMgr.await().startDownload(decodeFromSavedState(gi), label)
|
||||
}
|
||||
|
||||
ACTION_START_RANGE -> {
|
||||
@@ -109,47 +121,11 @@ class DownloadService :
|
||||
}
|
||||
}
|
||||
|
||||
ACTION_START_ALL -> {
|
||||
deferredMgr.await().startAllDownload()
|
||||
}
|
||||
|
||||
ACTION_STOP -> {
|
||||
val gid = intent.getLongExtra(KEY_GID, -1)
|
||||
if (gid != -1L) {
|
||||
deferredMgr.await().stopDownload(gid)
|
||||
}
|
||||
}
|
||||
|
||||
ACTION_STOP_CURRENT -> deferredMgr.await().stopCurrentDownload()
|
||||
|
||||
ACTION_STOP_RANGE -> {
|
||||
val gidList = intent.getLongArrayExtra(KEY_GID_LIST)
|
||||
if (gidList != null) {
|
||||
deferredMgr.await().stopRangeDownload(gidList)
|
||||
}
|
||||
}
|
||||
|
||||
ACTION_START_ALL -> deferredMgr.await().startAllDownload()
|
||||
ACTION_STOP_ALL -> deferredMgr.await().stopAllDownload()
|
||||
|
||||
ACTION_DELETE -> {
|
||||
val gid = intent.getLongExtra(KEY_GID, -1)
|
||||
if (gid != -1L) {
|
||||
deferredMgr.await().deleteDownload(gid)
|
||||
}
|
||||
}
|
||||
|
||||
ACTION_DELETE_RANGE -> {
|
||||
val gidList = intent.getLongArrayExtra(KEY_GID_LIST)
|
||||
if (gidList != null) {
|
||||
deferredMgr.await().deleteRangeDownload(gidList)
|
||||
}
|
||||
}
|
||||
|
||||
ACTION_CLEAR -> {
|
||||
clear()
|
||||
checkStopSelf()
|
||||
}
|
||||
ACTION_CLEAR -> clear()
|
||||
}
|
||||
checkStopSelf()
|
||||
}
|
||||
|
||||
override fun onBind(intent: Intent) = null
|
||||
@@ -165,7 +141,7 @@ class DownloadService :
|
||||
.setAutoCancel(false)
|
||||
.setCategory(NotificationCompat.CATEGORY_PROGRESS)
|
||||
.addAction(
|
||||
R.drawable.v_pause_x24,
|
||||
com.hippo.ehviewer.R.drawable.v_pause_x24,
|
||||
getString(R.string.stat_download_action_stop_all),
|
||||
piStopAll,
|
||||
)
|
||||
@@ -200,24 +176,27 @@ class DownloadService :
|
||||
.apply { launch { run() } }
|
||||
}
|
||||
|
||||
private fun init509Notification(): NotificationHandler {
|
||||
private fun initFatalNotification(): NotificationHandler {
|
||||
val builder = NotificationCompat.Builder(applicationContext, channelId)
|
||||
.setSmallIcon(R.drawable.ic_baseline_warning_24)
|
||||
.setContentTitle(getString(R.string.stat_509_alert_title))
|
||||
.setContentText(getString(R.string.stat_509_alert_text))
|
||||
.setStyle(
|
||||
NotificationCompat.BigTextStyle().bigText(getString(R.string.stat_509_alert_text)),
|
||||
)
|
||||
.setSmallIcon(com.hippo.ehviewer.R.drawable.ic_baseline_warning_24)
|
||||
.setAutoCancel(true)
|
||||
.setOngoing(false)
|
||||
.setCategory(NotificationCompat.CATEGORY_ERROR)
|
||||
return NotificationHandler(this, notifyManager, builder, ID_509)
|
||||
return NotificationHandler(this, notifyManager, builder, ID_FATAL)
|
||||
.apply { launch { run() } }
|
||||
}
|
||||
|
||||
override fun onGet509() {
|
||||
error509Notification.run {
|
||||
override fun onFatal(error: FatalException) {
|
||||
val (title, text) = when (error) {
|
||||
is IpBannedException -> R.string.error_ip_banned to error.message
|
||||
is InsufficientGpException -> R.string.insufficient_funds to error.message
|
||||
is QuotaExceededException -> R.string.stat_509_alert_title to getString(R.string.stat_509_alert_text)
|
||||
}
|
||||
fatalNotification.run {
|
||||
builder.setWhen(System.currentTimeMillis())
|
||||
.setContentTitle(getString(title))
|
||||
.setContentText(text)
|
||||
.setStyle(NotificationCompat.BigTextStyle().bigText(text))
|
||||
show()
|
||||
}
|
||||
}
|
||||
@@ -246,7 +225,7 @@ class DownloadService :
|
||||
|
||||
override fun onDownload(info: DownloadInfo) {
|
||||
val speed = info.speed.coerceAtLeast(0)
|
||||
val speedText = FileUtils.humanReadableByteCount(speed, false) + "/s"
|
||||
val speedText = FileUtils.humanReadableByteCount(speed) + "/s"
|
||||
val remaining = info.remaining
|
||||
val text = if (remaining >= 0) {
|
||||
val interval = ReadableTime.getShortTimeInterval(remaining)
|
||||
@@ -372,7 +351,6 @@ class DownloadService :
|
||||
private fun checkStopSelf() {
|
||||
launch {
|
||||
if (deferredMgr.await().isIdle) {
|
||||
ServiceCompat.stopForeground(this@DownloadService, ServiceCompat.STOP_FOREGROUND_REMOVE)
|
||||
stopSelf()
|
||||
}
|
||||
}
|
||||
@@ -431,12 +409,7 @@ class DownloadService :
|
||||
const val ACTION_START = "start"
|
||||
const val ACTION_START_RANGE = "start_range"
|
||||
const val ACTION_START_ALL = "start_all"
|
||||
const val ACTION_STOP = "stop"
|
||||
const val ACTION_STOP_RANGE = "stop_range"
|
||||
const val ACTION_STOP_CURRENT = "stop_current"
|
||||
const val ACTION_STOP_ALL = "stop_all"
|
||||
const val ACTION_DELETE = "delete"
|
||||
const val ACTION_DELETE_RANGE = "delete_range"
|
||||
const val ACTION_CLEAR = "clear"
|
||||
const val KEY_GALLERY_INFO = "gallery_info"
|
||||
const val KEY_LABEL = "label"
|
||||
@@ -446,7 +419,7 @@ class DownloadService :
|
||||
const val ACTION_CLEAR_DOWNLOAD_SERVICE = "clear_download_service"
|
||||
private const val ID_DOWNLOADING = 1
|
||||
private const val ID_DOWNLOADED = 2
|
||||
private const val ID_509 = 3
|
||||
private const val ID_FATAL = 3
|
||||
private const val DELAY = 1000L // 1s
|
||||
private val sItemStateArray = LongSparseArray<Boolean>()
|
||||
private val sItemTitleArray = LongSparseArray<String>()
|
||||
@@ -454,6 +427,23 @@ class DownloadService :
|
||||
private var sFinishedCount = 0
|
||||
private var sDownloadedCount = 0
|
||||
|
||||
context(ctx: Context)
|
||||
inline fun startService(action: String, intentBuilder: Intent.() -> Unit = {}) {
|
||||
val intent = Intent(ctx, DownloadService::class.java).setAction(action).apply(intentBuilder)
|
||||
ContextCompat.startForegroundService(ctx, intent)
|
||||
}
|
||||
|
||||
context(_: Context)
|
||||
fun startDownload(info: BaseGalleryInfo, label: String? = null) = startService(ACTION_START) {
|
||||
putExtra(KEY_GALLERY_INFO, encodeToSavedState(info))
|
||||
label?.let { putExtra(KEY_LABEL, it) }
|
||||
}
|
||||
|
||||
context(_: Context)
|
||||
fun startRangeDownload(gidList: LongArray) = startService(ACTION_START_RANGE) {
|
||||
putExtra(KEY_GID_LIST, gidList)
|
||||
}
|
||||
|
||||
fun clear() {
|
||||
sFailedCount = 0
|
||||
sFinishedCount = 0
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.hippo.ehviewer.download
|
||||
|
||||
import com.hippo.ehviewer.dao.DownloadInfo
|
||||
import com.hippo.ehviewer.dao.artists
|
||||
import com.ehviewer.core.database.model.DownloadInfo
|
||||
import com.ehviewer.core.database.model.artists
|
||||
|
||||
enum class DownloadsFilterMode(
|
||||
val flag: Int,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.hippo.ehviewer.download
|
||||
|
||||
import com.ehviewer.core.database.model.DownloadInfo
|
||||
import com.hippo.ehviewer.client.EhUtils
|
||||
import com.hippo.ehviewer.dao.DownloadInfo
|
||||
|
||||
data class SortMode(val field: Field, val order: Order, val groupByDownloadLabel: Boolean = false) {
|
||||
val flag
|
||||
@@ -11,7 +11,6 @@ data class SortMode(val field: Field, val order: Order, val groupByDownloadLabel
|
||||
GID(0),
|
||||
TITLE(1),
|
||||
TIME(2),
|
||||
LABEL(3),
|
||||
PAGES(4),
|
||||
}
|
||||
|
||||
@@ -23,13 +22,16 @@ data class SortMode(val field: Field, val order: Order, val groupByDownloadLabel
|
||||
fun comparator(): Comparator<DownloadInfo> {
|
||||
var comparator = when (field) {
|
||||
Field.GID -> compareBy { it.gid }
|
||||
Field.TITLE, Field.LABEL -> compareBy { EhUtils.getSuitableTitle(it) }
|
||||
Field.TITLE -> compareBy { EhUtils.getSuitableTitle(it) }
|
||||
Field.TIME -> compareBy { it.time }
|
||||
Field.PAGES -> compareBy<DownloadInfo> { it.pages }.thenBy { EhUtils.getSuitableTitle(it) }
|
||||
Field.PAGES -> compareBy<DownloadInfo> { it.pages }
|
||||
}
|
||||
if (order == Order.DESC) {
|
||||
comparator = comparator.reversed()
|
||||
}
|
||||
if (field == Field.PAGES) {
|
||||
comparator = comparator.thenByDescending { it.time }
|
||||
}
|
||||
if (groupByDownloadLabel) {
|
||||
comparator = compareBy<DownloadInfo> { it.label }.then(comparator)
|
||||
}
|
||||
@@ -38,15 +40,16 @@ data class SortMode(val field: Field, val order: Order, val groupByDownloadLabel
|
||||
|
||||
companion object {
|
||||
fun from(flag: Int): SortMode {
|
||||
val field = flag and 0xF shr 1
|
||||
val order = flag and 0x1
|
||||
val fieldValue = flag and 0xF shr 1
|
||||
val orderValue = flag and 0x1
|
||||
val groupByDownloadLabel = flag shr 4 == 1
|
||||
require(field in Field.entries.indices && order in Order.entries.indices)
|
||||
return SortMode(Field.entries[field], Order.entries[order], groupByDownloadLabel)
|
||||
val field = Field.entries.find { it.value == fieldValue } ?: Default.field
|
||||
val order = Order.entries.find { it.value == orderValue } ?: Default.order
|
||||
return SortMode(field, order, groupByDownloadLabel)
|
||||
}
|
||||
val Default = SortMode(Field.TIME, Order.DESC)
|
||||
val All = listOf(
|
||||
SortMode(Field.TIME, Order.DESC),
|
||||
Default,
|
||||
SortMode(Field.TIME, Order.ASC),
|
||||
SortMode(Field.GID, Order.DESC),
|
||||
SortMode(Field.GID, Order.ASC),
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2023 Tarsin Norbin
|
||||
* Copyright 2023-2024 Tarsin Norbin
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,11 +15,12 @@
|
||||
*/
|
||||
package com.hippo.ehviewer.gallery
|
||||
|
||||
import android.os.ParcelFileDescriptor
|
||||
import com.hippo.ehviewer.Settings
|
||||
import arrow.autoCloseScope
|
||||
import com.ehviewer.core.files.openFileDescriptor
|
||||
import com.ehviewer.core.util.logcat
|
||||
import com.hippo.ehviewer.Settings.archivePasswds
|
||||
import com.hippo.ehviewer.image.ByteBufferSource
|
||||
import com.hippo.ehviewer.image.Image
|
||||
import com.hippo.ehviewer.image.ImageSource
|
||||
import com.hippo.ehviewer.image.byteBufferSource
|
||||
import com.hippo.ehviewer.jni.closeArchive
|
||||
import com.hippo.ehviewer.jni.extractToByteBuffer
|
||||
import com.hippo.ehviewer.jni.extractToFd
|
||||
@@ -29,139 +30,59 @@ import com.hippo.ehviewer.jni.openArchive
|
||||
import com.hippo.ehviewer.jni.providePassword
|
||||
import com.hippo.ehviewer.jni.releaseByteBuffer
|
||||
import com.hippo.ehviewer.util.FileUtils
|
||||
import com.hippo.ehviewer.util.displayPath
|
||||
import com.hippo.files.openFileDescriptor
|
||||
import com.hippo.files.toUri
|
||||
import eu.kanade.tachiyomi.util.system.logcat
|
||||
import java.nio.ByteBuffer
|
||||
import kotlinx.coroutines.CoroutineStart
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.currentCoroutineContext
|
||||
import kotlinx.coroutines.ensureActive
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.sync.Semaphore
|
||||
import kotlinx.coroutines.sync.withPermit
|
||||
import moe.tarsin.coroutines.NamedMutex
|
||||
import moe.tarsin.coroutines.withLock
|
||||
import com.hippo.ehviewer.util.displayName
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import moe.tarsin.kt.install
|
||||
import okio.Path
|
||||
|
||||
typealias PasswdInvalidator = (String) -> Boolean
|
||||
typealias PasswdProvider = suspend (PasswdInvalidator) -> String
|
||||
|
||||
class ArchivePageLoader(
|
||||
private val file: Path,
|
||||
suspend inline fun <T> useArchivePageLoader(
|
||||
file: Path,
|
||||
gid: Long = 0,
|
||||
startPage: Int = 0,
|
||||
private val hasAds: Boolean = false,
|
||||
passwdProvider: PasswdProvider? = null,
|
||||
) : PageLoader2(gid, startPage) {
|
||||
private lateinit var pfd: ParcelFileDescriptor
|
||||
private val hostJob = async(start = CoroutineStart.LAZY) {
|
||||
logcat(DEBUG_TAG) { "Open archive ${file.toUri().displayPath}" }
|
||||
pfd = file.openFileDescriptor("r")
|
||||
val size = openArchive(pfd.fd, pfd.statSize, gid == 0L || file.name.endsWith(".zip"))
|
||||
if (size != 0 && passwdProvider != null && needPassword()) {
|
||||
archivePasswds?.forEach {
|
||||
it ?: return@forEach
|
||||
if (providePassword(it)) return@async size
|
||||
}
|
||||
val toAdd = passwdProvider { providePassword(it) }
|
||||
archivePasswds = archivePasswds?.toMutableSet()?.apply { add(toAdd) } ?: setOf(toAdd)
|
||||
hasAds: Boolean = false,
|
||||
crossinline passwdProvider: PasswdProvider,
|
||||
crossinline block: suspend (PageLoader) -> T,
|
||||
) = autoCloseScope {
|
||||
coroutineScope {
|
||||
val pfd = install(file.openFileDescriptor("r"))
|
||||
val size = install(
|
||||
{ openArchive(pfd.fd, pfd.statSize, gid == 0L || file.name.endsWith(".zip")) },
|
||||
{ _, _ -> closeArchive() },
|
||||
)
|
||||
check(size > 0) { "Archive have no content!" }
|
||||
if (needPassword() && archivePasswds.none(::providePassword)) {
|
||||
archivePasswds += passwdProvider(::providePassword)
|
||||
}
|
||||
size
|
||||
}
|
||||
val loader = install(
|
||||
object : PageLoader(this, gid, startPage, size, hasAds) {
|
||||
override val title by lazy { FileUtils.getNameFromFilename(file.displayName)!! }
|
||||
|
||||
override var size = 0
|
||||
private set
|
||||
override fun getImageExtension(index: Int) = getExtension(index)
|
||||
|
||||
override fun start() {
|
||||
super.start()
|
||||
hostJob.start()
|
||||
}
|
||||
|
||||
override fun stop() {
|
||||
super.stop()
|
||||
closeArchive()
|
||||
pfd.close()
|
||||
logcat(DEBUG_TAG) { "Close archive ${file.toUri().displayPath} successfully!" }
|
||||
}
|
||||
|
||||
private val mJobMap = hashMapOf<Int, Job>()
|
||||
private val mWorkerMutex = NamedMutex<Int>()
|
||||
private val mSemaphore = Semaphore(4)
|
||||
|
||||
override fun onRequest(index: Int) {
|
||||
synchronized(mJobMap) {
|
||||
val current = mJobMap[index]
|
||||
if (current?.isActive != true) {
|
||||
mJobMap[index] = launch {
|
||||
mWorkerMutex.withLock(index) {
|
||||
mSemaphore.withPermit {
|
||||
doRealWork(index)
|
||||
}
|
||||
override fun save(index: Int, file: Path) = runCatching {
|
||||
file.openFileDescriptor("w").use {
|
||||
extractToFd(index, it.fd)
|
||||
}
|
||||
}.getOrElse {
|
||||
logcat(it)
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun openSource(index: Int): ImageSource {
|
||||
val buffer = extractToByteBuffer(index)
|
||||
checkNotNull(buffer) { "Extract archive content $index failed!" }
|
||||
check(buffer.isDirect)
|
||||
return byteBufferSource(buffer) { releaseByteBuffer(buffer) }
|
||||
}
|
||||
|
||||
override fun prefetchPages(pages: List<Int>, bounds: IntRange) = Unit
|
||||
|
||||
override fun onRequest(index: Int, force: Boolean, orgImg: Boolean) = notifySourceReady(index)
|
||||
},
|
||||
)
|
||||
block(loader)
|
||||
}
|
||||
|
||||
private fun mayBeAd(index: Int) = index > size - 10
|
||||
|
||||
private suspend fun doRealWork(index: Int) {
|
||||
val buffer = extractToByteBuffer(index) ?: return notifyPageFailed(index, null)
|
||||
check(buffer.isDirect)
|
||||
val src = object : ByteBufferSource {
|
||||
override val source: ByteBuffer = buffer
|
||||
|
||||
override fun close() {
|
||||
releaseByteBuffer(buffer)
|
||||
}
|
||||
}
|
||||
runCatching {
|
||||
currentCoroutineContext().ensureActive()
|
||||
}.onFailure {
|
||||
src.close()
|
||||
throw it
|
||||
}
|
||||
val image = Image.decode(src, hasAds && Settings.stripExtraneousAds.value && mayBeAd(index)) ?: return notifyPageFailed(index, null)
|
||||
runCatching {
|
||||
currentCoroutineContext().ensureActive()
|
||||
}.onFailure {
|
||||
image.recycle()
|
||||
throw it
|
||||
}
|
||||
notifyPageSucceed(index, image)
|
||||
}
|
||||
|
||||
override fun onForceRequest(index: Int, orgImg: Boolean) {
|
||||
onRequest(index)
|
||||
}
|
||||
|
||||
override suspend fun awaitReady(): Boolean {
|
||||
size = hostJob.await()
|
||||
return super.awaitReady() && isReady
|
||||
}
|
||||
|
||||
override val isReady: Boolean
|
||||
get() = size != 0
|
||||
|
||||
override val title by lazy {
|
||||
FileUtils.getNameFromFilename(file.name)!!
|
||||
}
|
||||
|
||||
override fun getImageExtension(index: Int): String = getExtension(index)
|
||||
|
||||
override fun save(index: Int, file: Path): Boolean = runCatching {
|
||||
file.openFileDescriptor("w").use {
|
||||
extractToFd(index, it.fd)
|
||||
}
|
||||
}.getOrElse {
|
||||
logcat(it)
|
||||
false
|
||||
}
|
||||
|
||||
override fun prefetchPages(pages: List<Int>, bounds: Pair<Int, Int>) = Unit
|
||||
}
|
||||
|
||||
private const val DEBUG_TAG = "ArchivePageLoader"
|
||||
@@ -1,107 +1,59 @@
|
||||
/*
|
||||
* Copyright 2016 Hippo Seven
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.hippo.ehviewer.gallery
|
||||
|
||||
import arrow.autoCloseScope
|
||||
import com.ehviewer.core.model.GalleryInfo
|
||||
import com.hippo.ehviewer.client.EhUtils
|
||||
import com.hippo.ehviewer.client.data.GalleryInfo
|
||||
import com.hippo.ehviewer.image.Image
|
||||
import com.hippo.ehviewer.spider.SpiderQueen
|
||||
import com.hippo.ehviewer.spider.SpiderQueen.Companion.obtainSpiderQueen
|
||||
import com.hippo.ehviewer.spider.SpiderQueen.Companion.releaseSpiderQueen
|
||||
import com.hippo.ehviewer.spider.SpiderQueen.OnSpiderListener
|
||||
import com.hippo.ehviewer.util.hasAds
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import moe.tarsin.kt.install
|
||||
import okio.Path
|
||||
|
||||
class EhPageLoader(private val mGalleryInfo: GalleryInfo, startPage: Int) :
|
||||
PageLoader2(mGalleryInfo.gid, startPage),
|
||||
OnSpiderListener {
|
||||
private lateinit var mSpiderQueen: SpiderQueen
|
||||
override fun start() {
|
||||
super.start()
|
||||
if (!::mSpiderQueen.isInitialized) {
|
||||
mSpiderQueen = obtainSpiderQueen(mGalleryInfo, SpiderQueen.MODE_READ)
|
||||
mSpiderQueen.addOnSpiderListener(this)
|
||||
suspend inline fun <T> useEhPageLoader(
|
||||
info: GalleryInfo,
|
||||
startPage: Int,
|
||||
crossinline block: suspend (PageLoader) -> T,
|
||||
) = autoCloseScope {
|
||||
coroutineScope {
|
||||
val queen = install(
|
||||
{ obtainSpiderQueen(info, SpiderQueen.MODE_READ) },
|
||||
{ queen, _ -> releaseSpiderQueen(queen, SpiderQueen.MODE_READ) },
|
||||
)
|
||||
queen.awaitReady()
|
||||
val loader = install(
|
||||
object : PageLoader(this, info.gid, startPage, queen.size, info.hasAds) {
|
||||
override val title by lazy { EhUtils.getSuitableTitle(info) }
|
||||
|
||||
override fun getImageExtension(index: Int) = queen.getExtension(index)
|
||||
|
||||
override fun save(index: Int, file: Path) = queen.save(index, file)
|
||||
|
||||
override fun openSource(index: Int) = queen.spiderDen.getImageSource(index)
|
||||
|
||||
override fun prefetchPages(pages: List<Int>, bounds: IntRange) = queen.preloadPages(pages, bounds)
|
||||
|
||||
override fun onRequest(index: Int, force: Boolean, orgImg: Boolean) = queen.request(index, force, orgImg)
|
||||
},
|
||||
).apply {
|
||||
val listener = object : OnSpiderListener {
|
||||
override fun onPageDownload(index: Int, contentLength: Long, receivedSize: Long, bytesRead: Int) {
|
||||
if (contentLength > 0) {
|
||||
notifyPagePercent(index, receivedSize.toFloat() / contentLength)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onPageReady(index: Int) = notifySourceReady(index)
|
||||
|
||||
override fun onPageFailure(index: Int, error: String?, finished: Int, downloaded: Int, total: Int) = notifyPageFailed(index, error)
|
||||
}
|
||||
install(
|
||||
{ queen.addOnSpiderListener(listener) },
|
||||
{ _, _ -> queen.removeOnSpiderListener(listener) },
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override fun stop() {
|
||||
super.stop()
|
||||
mSpiderQueen.removeOnSpiderListener(this)
|
||||
releaseSpiderQueen(mSpiderQueen, SpiderQueen.MODE_READ)
|
||||
}
|
||||
|
||||
override val title by lazy {
|
||||
EhUtils.getSuitableTitle(mGalleryInfo)
|
||||
}
|
||||
|
||||
override fun getImageExtension(index: Int): String? = mSpiderQueen.getExtension(index)
|
||||
|
||||
override fun save(index: Int, file: Path): Boolean = mSpiderQueen.save(index, file)
|
||||
|
||||
override val size: Int
|
||||
get() = mSpiderQueen.size
|
||||
|
||||
override fun onRequest(index: Int) {
|
||||
mSpiderQueen.request(index)
|
||||
}
|
||||
|
||||
override fun onForceRequest(index: Int, orgImg: Boolean) {
|
||||
mSpiderQueen.forceRequest(index, orgImg)
|
||||
}
|
||||
|
||||
override suspend fun awaitReady(): Boolean = super.awaitReady() && mSpiderQueen.awaitReady()
|
||||
|
||||
override val isReady: Boolean
|
||||
get() = ::mSpiderQueen.isInitialized && mSpiderQueen.isReady
|
||||
|
||||
override fun onGetPages(pages: Int) {}
|
||||
|
||||
override fun onGet509(index: Int) {}
|
||||
|
||||
override fun onPageDownload(
|
||||
index: Int,
|
||||
contentLength: Long,
|
||||
receivedSize: Long,
|
||||
bytesRead: Int,
|
||||
) {
|
||||
if (contentLength > 0) {
|
||||
notifyPagePercent(index, receivedSize.toFloat() / contentLength)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onPageSuccess(index: Int, finished: Int, downloaded: Int, total: Int) {}
|
||||
override fun onPageFailure(
|
||||
index: Int,
|
||||
error: String?,
|
||||
finished: Int,
|
||||
downloaded: Int,
|
||||
total: Int,
|
||||
) {
|
||||
notifyPageFailed(index, error)
|
||||
}
|
||||
|
||||
override fun onFinish(finished: Int, downloaded: Int, total: Int) {}
|
||||
override fun onGetImageSuccess(index: Int, image: Image?) {
|
||||
notifyPageSucceed(index, image!!)
|
||||
}
|
||||
|
||||
override fun onGetImageFailure(index: Int, error: String?) {
|
||||
notifyPageFailed(index, error)
|
||||
}
|
||||
|
||||
override fun prefetchPages(pages: List<Int>, bounds: Pair<Int, Int>) {
|
||||
mSpiderQueen.preloadPages(pages, bounds)
|
||||
block(loader)
|
||||
}
|
||||
}
|
||||
@@ -36,8 +36,12 @@ val PageStatus.progressObserved
|
||||
|
||||
sealed interface PageStatus {
|
||||
data object Queued : PageStatus
|
||||
data class Blocked(val ad: Image) : PageStatus
|
||||
data class Ready(val image: Image) : PageStatus
|
||||
data class Error(val message: String?) : PageStatus
|
||||
data class Loading(val progress: MutableStateFlow<Float>) : PageStatus
|
||||
|
||||
@JvmInline value class Blocked(val ad: Image) : PageStatus
|
||||
|
||||
@JvmInline value class Ready(val image: Image) : PageStatus
|
||||
|
||||
@JvmInline value class Error(val message: String?) : PageStatus
|
||||
|
||||
@JvmInline value class Loading(val progress: MutableStateFlow<Float>) : PageStatus
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
package com.hippo.ehviewer.gallery
|
||||
|
||||
import androidx.collection.SieveCache
|
||||
import androidx.collection.mutableIntObjectMapOf
|
||||
import arrow.fx.coroutines.ExitCase
|
||||
import arrow.fx.coroutines.bracketCase
|
||||
import com.ehviewer.core.util.isAtLeastO
|
||||
import com.ehviewer.core.util.withNonCancellableContext
|
||||
import com.hippo.ehviewer.EhDB
|
||||
import com.hippo.ehviewer.Settings
|
||||
import com.hippo.ehviewer.image.Image
|
||||
import com.hippo.ehviewer.image.ImageSource
|
||||
import com.hippo.ehviewer.util.FileUtils
|
||||
import com.hippo.ehviewer.util.OSUtils
|
||||
import com.hippo.ehviewer.util.detectAds
|
||||
import com.hippo.ehviewer.util.displayString
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock
|
||||
import kotlin.concurrent.atomics.AtomicInt
|
||||
import kotlin.concurrent.read
|
||||
import kotlin.concurrent.write
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.sync.Semaphore
|
||||
import kotlinx.coroutines.sync.withPermit
|
||||
import moe.tarsin.coroutines.NamedMutex
|
||||
import moe.tarsin.coroutines.withLock
|
||||
import okio.Path
|
||||
|
||||
private val progressScope = CoroutineScope(Dispatchers.IO)
|
||||
private const val MAX_CACHE_SIZE = 512 * 1024 * 1024
|
||||
private const val MIN_CACHE_SIZE = 256 * 1024 * 1024
|
||||
|
||||
abstract class PageLoader(val scope: CoroutineScope, val gid: Long, startPage: Int, val size: Int, val hasAds: Boolean = false) : AutoCloseable {
|
||||
var startPage = startPage.coerceIn(0, size - 1)
|
||||
|
||||
private val jobs = mutableIntObjectMapOf<Job>()
|
||||
private val mutex = NamedMutex<Int>()
|
||||
private val semaphore = Semaphore(4)
|
||||
|
||||
private val cache = SieveCache<Int, Image>(
|
||||
maxSize = if (isAtLeastO) {
|
||||
(OSUtils.totalMemory / 8).toInt().coerceIn(MIN_CACHE_SIZE, MAX_CACHE_SIZE)
|
||||
} else {
|
||||
(OSUtils.appMaxMemory / 3 * 2).toInt()
|
||||
},
|
||||
sizeOf = { _, v -> v.allocationSize.toInt() },
|
||||
onEntryRemoved = { k, o, n, _ -> if (o.unpin()) n ?: notifyPageWait(k) },
|
||||
)
|
||||
|
||||
private suspend fun atomicallyDecodeAndUpdate(index: Int) {
|
||||
bracketCase(
|
||||
{ openSource(index) },
|
||||
{ src ->
|
||||
withNonCancellableContext {
|
||||
notifyPageSucceed(index, Image.decode(src, hasAds && detectAds(index, size)))
|
||||
}
|
||||
},
|
||||
{ src, case -> if (case !is ExitCase.Completed) src.close() },
|
||||
)
|
||||
}
|
||||
|
||||
private val lock = ReentrantReadWriteLock()
|
||||
|
||||
val pages = (0 until size).map { Page(it) }
|
||||
|
||||
private val prefetchPageCount = Settings.preloadImage.value
|
||||
|
||||
fun restart() {
|
||||
lock.write { cache.evictAll() }
|
||||
pages.forEach(Page::reset)
|
||||
}
|
||||
|
||||
private val prevIndex = AtomicInt(-1)
|
||||
|
||||
fun retryPage(index: Int, orgImg: Boolean = false) {
|
||||
notifyPageWait(index)
|
||||
lock.write { cache.remove(index) }
|
||||
onRequest(index, true, orgImg)
|
||||
}
|
||||
|
||||
protected abstract fun prefetchPages(pages: List<Int>, bounds: IntRange)
|
||||
|
||||
protected abstract fun onRequest(index: Int, force: Boolean = false, orgImg: Boolean = false)
|
||||
|
||||
fun notifyPageWait(index: Int) {
|
||||
pages[index].reset()
|
||||
}
|
||||
|
||||
fun notifyPagePercent(index: Int, percent: Float) {
|
||||
pages[index].statusFlow.update {
|
||||
when (it) {
|
||||
is PageStatus.Loading -> it.apply { progress.update { percent } }
|
||||
else -> PageStatus.Loading(MutableStateFlow(percent))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun notifyPageSucceed(index: Int, image: Image, replaceCache: Boolean = true) {
|
||||
if (replaceCache) lock.write { cache[index] = image }
|
||||
pages[index].statusFlow.update { if (image.hasQrCode) PageStatus.Blocked(image) else PageStatus.Ready(image) }
|
||||
}
|
||||
|
||||
fun notifyPageFailed(index: Int, error: String?) {
|
||||
pages[index].statusFlow.update { PageStatus.Error(error) }
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
lock.write { cache.evictAll() }
|
||||
if (gid != 0L) {
|
||||
progressScope.launch {
|
||||
EhDB.putReadProgress(gid, startPage)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract val title: String
|
||||
|
||||
protected abstract fun getImageExtension(index: Int): String?
|
||||
|
||||
fun getImageFilename(index: Int): String? = getImageExtension(index)?.let {
|
||||
FileUtils.sanitizeFilename("$title - ${index + 1}.${it.lowercase()}")
|
||||
}
|
||||
|
||||
fun request(index: Int) {
|
||||
val prefetchRange = if (index >= prevIndex.load()) {
|
||||
index + 1..(index + prefetchPageCount).coerceAtMost(size - 1)
|
||||
} else {
|
||||
index - 1 downTo (index - prefetchPageCount).coerceAtLeast(0)
|
||||
}
|
||||
prevIndex.store(index)
|
||||
val image = lock.read { cache[index] }
|
||||
if (image != null) {
|
||||
notifyPageSucceed(index, image, false)
|
||||
} else {
|
||||
notifyPageWait(index)
|
||||
onRequest(index)
|
||||
}
|
||||
|
||||
// Prefetch to disk
|
||||
val pagesAbsent = prefetchRange.filter {
|
||||
when (pages[it].status) {
|
||||
PageStatus.Queued, is PageStatus.Error -> true
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
val start = if (prefetchRange.step > 0) prefetchRange.first else prefetchRange.last
|
||||
val end = if (prefetchRange.step > 0) prefetchRange.last else prefetchRange.first
|
||||
prefetchPages(pagesAbsent, start - 5..end + 5)
|
||||
}
|
||||
|
||||
fun cancelRequest(index: Int) {
|
||||
jobs[index]?.cancel()
|
||||
}
|
||||
|
||||
abstract fun save(index: Int, file: Path): Boolean
|
||||
|
||||
fun notifySourceReady(index: Int) = synchronized(jobs) {
|
||||
if (jobs[index]?.isActive != true) {
|
||||
jobs[index] = scope.launch {
|
||||
try {
|
||||
mutex.withLock(index) {
|
||||
semaphore.withPermit {
|
||||
atomicallyDecodeAndUpdate(index)
|
||||
}
|
||||
}
|
||||
} catch (e: Throwable) {
|
||||
if (e is CancellationException) {
|
||||
notifyPageFailed(index, null)
|
||||
throw e
|
||||
} else {
|
||||
notifyPageFailed(index, e.displayString())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
abstract fun openSource(index: Int): ImageSource
|
||||
}
|
||||
@@ -1,70 +0,0 @@
|
||||
/*
|
||||
* Copyright 2016 Hippo Seven
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.hippo.ehviewer.gallery
|
||||
|
||||
import com.hippo.ehviewer.EhDB
|
||||
import com.hippo.ehviewer.util.FileUtils
|
||||
import eu.kanade.tachiyomi.ui.reader.loader.PageLoader
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.CoroutineStart
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.cancel
|
||||
import kotlinx.coroutines.launch
|
||||
import okio.Path
|
||||
|
||||
abstract class PageLoader2(private val gid: Long, var startPage: Int) :
|
||||
PageLoader(),
|
||||
CoroutineScope {
|
||||
override val coroutineContext = Dispatchers.IO + Job()
|
||||
private val progressJob = async(start = CoroutineStart.LAZY) {
|
||||
if (startPage == -1) {
|
||||
startPage = EhDB.getReadProgress(gid)
|
||||
}
|
||||
}
|
||||
|
||||
override fun start() {
|
||||
progressJob.start()
|
||||
}
|
||||
|
||||
override suspend fun awaitReady(): Boolean {
|
||||
progressJob.await()
|
||||
return super.awaitReady() && startPage != -1
|
||||
}
|
||||
|
||||
override fun stop() {
|
||||
super.stop()
|
||||
if (gid != 0L) {
|
||||
launch {
|
||||
EhDB.putReadProgress(gid, startPage)
|
||||
this@PageLoader2.cancel()
|
||||
}
|
||||
} else {
|
||||
cancel()
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract val title: String
|
||||
|
||||
protected abstract fun getImageExtension(index: Int): String?
|
||||
|
||||
fun getImageFilename(index: Int): String? = getImageExtension(index)?.let {
|
||||
FileUtils.sanitizeFilename("$title - ${index + 1}.${it.lowercase()}")
|
||||
}
|
||||
|
||||
abstract fun save(index: Int, file: Path): Boolean
|
||||
}
|
||||
@@ -23,17 +23,25 @@ import androidx.compose.ui.unit.IntSize
|
||||
import arrow.core.Either
|
||||
import arrow.core.left
|
||||
import arrow.core.right
|
||||
import arrow.fx.coroutines.ExitCase
|
||||
import arrow.fx.coroutines.bracketCase
|
||||
import coil3.BitmapImage
|
||||
import coil3.DrawableImage
|
||||
import coil3.Image as CoilImage
|
||||
import coil3.imageLoader
|
||||
import coil3.request.CachePolicy
|
||||
import coil3.request.ErrorResult
|
||||
import coil3.request.SuccessResult
|
||||
import coil3.request.allowHardware
|
||||
import coil3.size.Dimension
|
||||
import coil3.size.Precision
|
||||
import coil3.size.Size
|
||||
import coil3.size.SizeResolver
|
||||
import com.ehviewer.core.files.openFileDescriptor
|
||||
import com.ehviewer.core.files.toUri
|
||||
import com.ehviewer.core.util.isAtLeastP
|
||||
import com.ehviewer.core.util.isAtLeastU
|
||||
import com.hippo.ehviewer.Settings
|
||||
import com.hippo.ehviewer.coil.AnimatedWebPDrawable
|
||||
import com.hippo.ehviewer.coil.BitmapImageWithExtraInfo
|
||||
import com.hippo.ehviewer.coil.detectQrCode
|
||||
import com.hippo.ehviewer.coil.hardwareThreshold
|
||||
@@ -42,17 +50,22 @@ import com.hippo.ehviewer.jni.isGif
|
||||
import com.hippo.ehviewer.jni.mmap
|
||||
import com.hippo.ehviewer.jni.munmap
|
||||
import com.hippo.ehviewer.jni.rewriteGifSource
|
||||
import com.hippo.ehviewer.ktbuilder.execute
|
||||
import com.hippo.ehviewer.ktbuilder.imageRequest
|
||||
import com.hippo.ehviewer.util.isAtLeastP
|
||||
import com.hippo.ehviewer.util.isAtLeastU
|
||||
import com.hippo.files.openFileDescriptor
|
||||
import com.hippo.files.toUri
|
||||
import eu.kanade.tachiyomi.util.system.logcat
|
||||
import java.nio.ByteBuffer
|
||||
import kotlin.concurrent.atomics.AtomicInt
|
||||
import kotlin.concurrent.atomics.decrementAndFetch
|
||||
import kotlin.concurrent.atomics.updateAndFetch
|
||||
import okio.Path
|
||||
import splitties.init.appCtx
|
||||
|
||||
class Image private constructor(image: CoilImage, private val src: ImageSource) {
|
||||
val refcnt = AtomicInt(1)
|
||||
|
||||
fun pin() = refcnt.updateAndFetch { if (it != 0) it + 1 else 0 } != 0
|
||||
|
||||
fun unpin() = (refcnt.decrementAndFetch() == 0).also { if (it) recycle() }
|
||||
|
||||
val intrinsicSize = with(image) { IntSize(width, height) }
|
||||
val allocationSize = image.size
|
||||
val hasQrCode = when (image) {
|
||||
@@ -65,12 +78,12 @@ class Image private constructor(image: CoilImage, private val src: ImageSource)
|
||||
else -> image
|
||||
}
|
||||
|
||||
var isRecyclable = true
|
||||
|
||||
@Synchronized
|
||||
fun recycle() {
|
||||
when (val image = innerImage ?: return) {
|
||||
is DrawableImage -> src.close()
|
||||
private fun recycle() {
|
||||
when (val image = innerImage!!) {
|
||||
is DrawableImage -> {
|
||||
(image.drawable as? AnimatedWebPDrawable)?.dispose()
|
||||
src.close()
|
||||
}
|
||||
is BitmapImage -> image.bitmap.recycle()
|
||||
}
|
||||
innerImage = null
|
||||
@@ -78,62 +91,56 @@ class Image private constructor(image: CoilImage, private val src: ImageSource)
|
||||
|
||||
companion object {
|
||||
private val targetWidth = appCtx.resources.displayMetrics.widthPixels * 3
|
||||
private val sizeResolver = SizeResolver(Size(targetWidth, Dimension.Undefined))
|
||||
|
||||
private suspend fun Either<ByteBufferSource, PathSource>.decodeCoil(checkExtraneousAds: Boolean): CoilImage {
|
||||
val req = appCtx.imageRequest {
|
||||
onLeft { data(it.source) }
|
||||
onRight { data(it.source.toUri()) }
|
||||
size(Dimension(targetWidth), Dimension.Undefined)
|
||||
precision(Precision.INEXACT)
|
||||
allowHardware(false)
|
||||
hardwareThreshold(Settings.hardwareBitmapThreshold)
|
||||
maybeCropBorder(Settings.cropBorder.value)
|
||||
detectQrCode(checkExtraneousAds)
|
||||
memoryCachePolicy(CachePolicy.DISABLED)
|
||||
val request = with(appCtx) {
|
||||
imageRequest {
|
||||
onLeft { data(it.source) }
|
||||
onRight { data(it.source.toUri()) }
|
||||
size(sizeResolver)
|
||||
precision(Precision.INEXACT)
|
||||
allowHardware(false)
|
||||
hardwareThreshold(Settings.hardwareBitmapThreshold.value)
|
||||
maybeCropBorder(Settings.cropBorder.value)
|
||||
detectQrCode(checkExtraneousAds)
|
||||
memoryCachePolicy(CachePolicy.DISABLED)
|
||||
}
|
||||
}
|
||||
return when (val result = appCtx.imageLoader.execute(req)) {
|
||||
return when (val result = request.execute()) {
|
||||
is SuccessResult -> result.image
|
||||
is ErrorResult -> throw result.throwable
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun decode(src: ImageSource, checkExtraneousAds: Boolean = false): Image? {
|
||||
return runCatching {
|
||||
val image = when (src) {
|
||||
is PathSource -> {
|
||||
if (isAtLeastP && !isAtLeastU) {
|
||||
src.source.openFileDescriptor("rw").use {
|
||||
val fd = it.fd
|
||||
if (isGif(fd)) {
|
||||
val buffer = mmap(fd)!!
|
||||
val source = object : ByteBufferSource {
|
||||
override val source = buffer
|
||||
override fun close() {
|
||||
munmap(buffer)
|
||||
src.close()
|
||||
}
|
||||
}
|
||||
return decode(source, checkExtraneousAds)
|
||||
}
|
||||
suspend fun decode(src: ImageSource, checkExtraneousAds: Boolean = false): Image {
|
||||
val image = when (src) {
|
||||
is PathSource -> {
|
||||
if (isAtLeastP && !isAtLeastU) {
|
||||
src.source.openFileDescriptor("rw").use {
|
||||
val fd = it.fd
|
||||
if (isGif(fd)) {
|
||||
return bracketCase(
|
||||
{ mmap(fd)!! },
|
||||
{ buffer -> decode(byteBufferSource(buffer) { munmap(buffer).also { src.close() } }, checkExtraneousAds) },
|
||||
{ buffer, case -> if (case !is ExitCase.Completed) munmap(buffer) },
|
||||
)
|
||||
}
|
||||
}
|
||||
src.right().decodeCoil(checkExtraneousAds)
|
||||
}
|
||||
src.right().decodeCoil(checkExtraneousAds)
|
||||
}
|
||||
|
||||
is ByteBufferSource -> {
|
||||
if (isAtLeastP && !isAtLeastU) {
|
||||
rewriteGifSource(src.source)
|
||||
}
|
||||
src.left().decodeCoil(checkExtraneousAds)
|
||||
is ByteBufferSource -> {
|
||||
if (isAtLeastP && !isAtLeastU) {
|
||||
rewriteGifSource(src.source)
|
||||
}
|
||||
src.left().decodeCoil(checkExtraneousAds)
|
||||
}
|
||||
Image(image, src).apply {
|
||||
if (innerImage is BitmapImage) src.close()
|
||||
}
|
||||
}.onFailure {
|
||||
src.close()
|
||||
logcat(it)
|
||||
}.getOrNull()
|
||||
}
|
||||
return Image(image, src).apply {
|
||||
if (innerImage is BitmapImage) src.close()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -149,6 +156,11 @@ interface ByteBufferSource : ImageSource {
|
||||
val source: ByteBuffer
|
||||
}
|
||||
|
||||
inline fun byteBufferSource(buffer: ByteBuffer, crossinline release: () -> Unit) = object : ByteBufferSource {
|
||||
override val source = buffer
|
||||
override fun close() = release()
|
||||
}
|
||||
|
||||
external fun detectBorder(bitmap: Bitmap): IntArray
|
||||
external fun hasQrCode(bitmap: Bitmap): Boolean
|
||||
external fun copyBitmapToAHB(src: Bitmap, dst: HardwareBuffer, x: Int, y: Int)
|
||||
@@ -5,16 +5,20 @@ import coil3.ImageLoader
|
||||
import coil3.disk.DiskCache
|
||||
import coil3.imageLoader
|
||||
import coil3.request.ImageRequest
|
||||
import com.hippo.ehviewer.client.data.GalleryInfo
|
||||
import com.hippo.ehviewer.client.data.GalleryPreview
|
||||
import com.ehviewer.core.model.GalleryInfo
|
||||
import com.ehviewer.core.model.GalleryPreview
|
||||
import com.hippo.ehviewer.coil.ehPreview
|
||||
import com.hippo.ehviewer.coil.ehUrl
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
inline fun Context.imageRequest(preview: GalleryPreview, builder: ImageRequest.Builder.() -> Unit = {}) = ImageRequest.Builder(this).ehPreview(preview).apply(builder).build()
|
||||
inline fun Context.imageRequest(info: GalleryInfo, builder: ImageRequest.Builder.() -> Unit = {}) = ImageRequest.Builder(this).ehUrl(info).apply(builder).build()
|
||||
inline fun Context.imageRequest(builder: ImageRequest.Builder.() -> Unit = {}) = ImageRequest.Builder(this).apply(builder).build()
|
||||
context(ctx: Context)
|
||||
inline fun imageRequest(preview: GalleryPreview, builder: ImageRequest.Builder.() -> Unit = {}) = ImageRequest.Builder(ctx).ehPreview(preview).apply(builder).build()
|
||||
context(ctx: Context)
|
||||
inline fun imageRequest(info: GalleryInfo, builder: ImageRequest.Builder.() -> Unit = {}) = ImageRequest.Builder(ctx).ehUrl(info).apply(builder).build()
|
||||
context(ctx: Context)
|
||||
inline fun imageRequest(builder: ImageRequest.Builder.() -> Unit = {}) = ImageRequest.Builder(ctx).apply(builder).build()
|
||||
inline fun diskCache(builder: DiskCache.Builder.() -> Unit) = DiskCache.Builder().apply(builder).build()
|
||||
inline fun Context.imageLoader(builder: ImageLoader.Builder.() -> Unit) = ImageLoader.Builder(this).apply(builder).build()
|
||||
fun ImageRequest.launchIn(scope: CoroutineScope) = scope.launch { context.imageLoader.execute(this@launchIn) }
|
||||
suspend fun ImageRequest.execute() = context.imageLoader.execute(this)
|
||||
fun ImageRequest.executeIn(scope: CoroutineScope) = scope.launch { execute() }
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.hippo.ehviewer.ktor
|
||||
|
||||
import com.hippo.ehviewer.BuildConfig
|
||||
import com.hippo.ehviewer.Settings
|
||||
import com.hippo.ehviewer.client.EhCookieStore
|
||||
import io.ktor.client.HttpClientConfig
|
||||
import io.ktor.client.engine.HttpClientEngineConfig
|
||||
import io.ktor.client.plugins.HttpTimeout
|
||||
import io.ktor.client.plugins.HttpTimeoutConfig
|
||||
import io.ktor.client.plugins.api.createClientPlugin
|
||||
import io.ktor.client.plugins.cookies.HttpCookies
|
||||
import io.ktor.client.plugins.defaultRequest
|
||||
import io.ktor.client.request.header
|
||||
import io.ktor.http.HttpHeaders
|
||||
import io.ktor.http.userAgent
|
||||
import io.ktor.util.appendIfNameAbsent
|
||||
|
||||
const val CHROME_MOBILE_USER_AGENT = "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${BuildConfig.CHROME_VERSION}.0.0.0 Mobile Safari/537.36"
|
||||
private const val CHROME_USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${BuildConfig.CHROME_VERSION}.0.0.0 Safari/537.36"
|
||||
private const val CHROME_ACCEPT = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"
|
||||
private const val CHROME_ACCEPT_LANGUAGE = "en-US,en;q=0.9"
|
||||
|
||||
fun <T : HttpClientEngineConfig> HttpClientConfig<T>.configureCommon(redirect: Boolean = true) = apply {
|
||||
install(HttpCookies) {
|
||||
storage = EhCookieStore
|
||||
}
|
||||
install(HttpTimeout) {
|
||||
reset()
|
||||
requestTimeoutMillis = 10_000
|
||||
}
|
||||
install(UserAgent)
|
||||
defaultRequest {
|
||||
headers.appendIfNameAbsent(HttpHeaders.Accept, CHROME_ACCEPT)
|
||||
header(HttpHeaders.AcceptLanguage, CHROME_ACCEPT_LANGUAGE)
|
||||
}
|
||||
followRedirects = redirect
|
||||
}
|
||||
|
||||
private val UserAgent = createClientPlugin("UserAgent") {
|
||||
onRequest { request, _ ->
|
||||
val userAgent = if (Settings.desktopSite.value) {
|
||||
CHROME_USER_AGENT
|
||||
} else {
|
||||
CHROME_MOBILE_USER_AGENT
|
||||
}
|
||||
request.userAgent(userAgent)
|
||||
}
|
||||
}
|
||||
|
||||
fun HttpTimeoutConfig.reset() = apply {
|
||||
requestTimeoutMillis = HttpTimeoutConfig.INFINITE_TIMEOUT_MS
|
||||
connectTimeoutMillis = HttpTimeoutConfig.INFINITE_TIMEOUT_MS
|
||||
socketTimeoutMillis = HttpTimeoutConfig.INFINITE_TIMEOUT_MS
|
||||
}
|
||||
@@ -1,9 +1,12 @@
|
||||
@file:RequiresExtension(extension = Build.VERSION_CODES.S, version = 7)
|
||||
|
||||
package com.hippo.ehviewer.ktor
|
||||
|
||||
import io.ktor.client.engine.HttpClientEngine
|
||||
import android.os.Build
|
||||
import androidx.annotation.RequiresExtension
|
||||
import io.ktor.client.engine.HttpClientEngineFactory
|
||||
import splitties.init.appCtx
|
||||
|
||||
object Cronet : HttpClientEngineFactory<CronetConfig> {
|
||||
override fun create(block: CronetConfig.() -> Unit): HttpClientEngine =
|
||||
CronetEngine(CronetConfig().apply(block))
|
||||
override fun create(block: CronetConfig.() -> Unit) = CronetEngine(CronetConfig(appCtx).apply(block))
|
||||
}
|
||||
@@ -1,13 +1,13 @@
|
||||
@file:RequiresExtension(extension = Build.VERSION_CODES.S, version = 7)
|
||||
|
||||
package com.hippo.ehviewer.ktor
|
||||
|
||||
import android.content.Context
|
||||
import android.net.http.HttpEngine
|
||||
import android.os.Build
|
||||
import androidx.annotation.RequiresExtension
|
||||
import io.ktor.client.engine.HttpClientEngineConfig
|
||||
import org.chromium.net.CronetEngine
|
||||
|
||||
class CronetConfig : HttpClientEngineConfig() {
|
||||
var client: CronetEngine? = null
|
||||
|
||||
fun config(context: Context, block: CronetEngine.Builder.() -> Unit) {
|
||||
client = CronetEngine.Builder(context).apply(block).build()
|
||||
}
|
||||
class CronetConfig(val context: Context) : HttpClientEngineConfig() {
|
||||
var config: HttpEngine.Builder.() -> Unit = {}
|
||||
}
|
||||
@@ -1,7 +1,21 @@
|
||||
@file:RequiresExtension(extension = Build.VERSION_CODES.S, version = 7)
|
||||
|
||||
package com.hippo.ehviewer.ktor
|
||||
|
||||
import android.net.http.HttpEngine
|
||||
import android.net.http.HttpException
|
||||
import android.net.http.UploadDataProvider
|
||||
import android.net.http.UploadDataSink
|
||||
import android.net.http.UrlRequest
|
||||
import android.net.http.UrlResponseInfo
|
||||
import android.os.Build
|
||||
import androidx.annotation.RequiresExtension
|
||||
import com.ehviewer.core.preferences.edit
|
||||
import com.hippo.ehviewer.Settings
|
||||
import com.hippo.ehviewer.util.restartApplication
|
||||
import io.ktor.client.engine.HttpClientEngineBase
|
||||
import io.ktor.client.engine.callContext
|
||||
import io.ktor.client.plugins.HttpTimeoutCapability
|
||||
import io.ktor.client.request.HttpRequestData
|
||||
import io.ktor.client.request.HttpResponseData
|
||||
import io.ktor.http.HttpHeaders
|
||||
@@ -27,17 +41,18 @@ import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.channels.consumeEach
|
||||
import kotlinx.coroutines.job
|
||||
import kotlinx.coroutines.suspendCancellableCoroutine
|
||||
import org.chromium.net.CronetException
|
||||
import org.chromium.net.UrlRequest
|
||||
import org.chromium.net.UrlResponseInfo
|
||||
import org.chromium.net.apihelpers.UploadDataProviders
|
||||
|
||||
class CronetEngine(override val config: CronetConfig) : HttpClientEngineBase("Cronet") {
|
||||
// Limit thread to 1 since we are async & non-blocking
|
||||
override val dispatcher = Dispatchers.Default.limitedParallelism(1)
|
||||
|
||||
override val supportedCapabilities = setOf(HttpTimeoutCapability)
|
||||
|
||||
private val executor = dispatcher.asExecutor()
|
||||
private val pool = DirectByteBufferPool(32)
|
||||
private val client = config.client ?: error("Cronet client is not configured")
|
||||
private val client by lazy {
|
||||
with(config) { HttpEngine.Builder(context).apply(config).build() }
|
||||
}
|
||||
|
||||
@InternalAPI
|
||||
override suspend fun execute(data: HttpRequestData) = executeHttpRequest(callContext(), data)
|
||||
@@ -48,7 +63,7 @@ class CronetEngine(override val config: CronetConfig) : HttpClientEngineBase("Cr
|
||||
) = suspendCancellableCoroutine { continuation ->
|
||||
val requestTime = GMTDate()
|
||||
|
||||
val callback = object : UrlRequest.Callback() {
|
||||
val callback = object : UrlRequest.Callback {
|
||||
val chunkChan = Channel<ByteBuffer>()
|
||||
override fun onRedirectReceived(request: UrlRequest, info: UrlResponseInfo, newLocationUrl: String) {
|
||||
continuation.resume(
|
||||
@@ -87,16 +102,27 @@ class CronetEngine(override val config: CronetConfig) : HttpClientEngineBase("Cr
|
||||
chunkChan.close()
|
||||
}
|
||||
|
||||
override fun onFailed(request: UrlRequest, info: UrlResponseInfo?, error: CronetException) {
|
||||
override fun onFailed(request: UrlRequest, info: UrlResponseInfo?, error: HttpException) {
|
||||
// Cronet may crash on some devices, fuck xiaomi
|
||||
// https://github.com/FooIbar/EhViewer/issues/1826
|
||||
val message = error.message.orEmpty()
|
||||
if ("ERR_FILE_NOT_FOUND" in message || "ERR_PROXY_CONNECTION_FAILED" in message) {
|
||||
Settings.edit(true) {
|
||||
it[enableCronet] = false
|
||||
}
|
||||
with(config.context) { restartApplication() }
|
||||
}
|
||||
if (continuation.isActive) {
|
||||
continuation.resumeWithException(error)
|
||||
} else {
|
||||
chunkChan.close(error)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCanceled(request: UrlRequest, info: UrlResponseInfo?) = Unit
|
||||
}
|
||||
|
||||
client.newUrlRequestBuilder(data.url.toString(), callback, executor).apply {
|
||||
client.newUrlRequestBuilder(data.url.toString(), executor, callback).apply {
|
||||
setHttpMethod(data.method.value)
|
||||
data.headers.flattenForEach { key, value -> addHeader(key, value) }
|
||||
data.body.contentType?.let { addHeader(HttpHeaders.ContentType, it.toString()) }
|
||||
@@ -117,7 +143,7 @@ private fun UrlResponseInfo.toHttpResponseData(
|
||||
statusCode = HttpStatusCode.fromValue(httpStatusCode),
|
||||
requestTime = requestTime,
|
||||
headers = headers {
|
||||
allHeaders.forEach { (key, value) ->
|
||||
headers.asMap.forEach { (key, value) ->
|
||||
appendAll(key, value)
|
||||
}
|
||||
},
|
||||
@@ -133,6 +159,17 @@ private fun UrlResponseInfo.toHttpResponseData(
|
||||
|
||||
private fun OutgoingContent.toUploadDataProvider() = when (this) {
|
||||
is OutgoingContent.NoContent -> null
|
||||
is OutgoingContent.ByteArrayContent -> UploadDataProviders.create(bytes())
|
||||
is OutgoingContent.ByteArrayContent -> object : UploadDataProvider() {
|
||||
val buffer = ByteBuffer.wrap(bytes()).slice()
|
||||
override fun getLength() = buffer.limit().toLong()
|
||||
override fun read(uploadDataSink: UploadDataSink, byteBuffer: ByteBuffer) {
|
||||
byteBuffer.put(buffer)
|
||||
uploadDataSink.onReadSucceeded(false)
|
||||
}
|
||||
override fun rewind(uploadDataSink: UploadDataSink) {
|
||||
buffer.position(0)
|
||||
uploadDataSink.onRewindSucceeded()
|
||||
}
|
||||
}
|
||||
else -> error("UnsupportedContentType $this")
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
@file:RequiresExtension(extension = Build.VERSION_CODES.S, version = 7)
|
||||
|
||||
package com.hippo.ehviewer.ktor
|
||||
|
||||
import android.net.http.HttpEngine
|
||||
import android.os.Build
|
||||
import androidx.annotation.RequiresExtension
|
||||
import java.io.File
|
||||
|
||||
fun CronetConfig.configureClient(enableQuic: Boolean) {
|
||||
config = {
|
||||
setEnableBrotli(true)
|
||||
|
||||
// Cache Quic hint only since the real cache mechanism should on Ktor layer
|
||||
val cache = File(context.cacheDir, "http_cache").apply { mkdirs() }
|
||||
setStoragePath(cache.path)
|
||||
setEnableHttpCache(HttpEngine.Builder.HTTP_CACHE_DISK_NO_HTTP, 4096)
|
||||
|
||||
setEnableQuic(enableQuic)
|
||||
if (enableQuic) {
|
||||
addQuicHint("e-hentai.org", 443, 443)
|
||||
addQuicHint("forums.e-hentai.org", 443, 443)
|
||||
addQuicHint("exhentai.org", 443, 443)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.hippo.ehviewer.ktor
|
||||
|
||||
import com.ehviewer.core.util.isAtLeastQ
|
||||
import io.ktor.client.engine.okhttp.OkHttpConfig
|
||||
import okhttp3.AsyncDns
|
||||
import okhttp3.android.AndroidAsyncDns
|
||||
|
||||
fun OkHttpConfig.configureClient() {
|
||||
config {
|
||||
if (isAtLeastQ) {
|
||||
dns(AsyncDns.toDns(AndroidAsyncDns.IPv4, AndroidAsyncDns.IPv6))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
package com.hippo.ehviewer.legacy
|
||||
|
||||
import java.io.File
|
||||
import splitties.init.appCtx
|
||||
|
||||
private val OBSOLETE_CACHE_DIRS = arrayOf(
|
||||
"image",
|
||||
"thumb",
|
||||
"gallery_image",
|
||||
"spider_info",
|
||||
)
|
||||
|
||||
fun cleanObsoleteCache() {
|
||||
appCtx.deleteDatabase("hosts.db")
|
||||
val dir = appCtx.cacheDir
|
||||
for (subdir in OBSOLETE_CACHE_DIRS) {
|
||||
val file = File(dir, subdir)
|
||||
if (file.exists()) {
|
||||
file.deleteRecursively()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,38 +1,36 @@
|
||||
package com.hippo.ehviewer.legacy
|
||||
|
||||
import com.ehviewer.core.files.read
|
||||
import com.hippo.ehviewer.spider.SpiderInfo
|
||||
import java.io.InputStream
|
||||
import okio.buffer
|
||||
import okio.source
|
||||
import kotlinx.io.readLineStrict
|
||||
import okio.Path
|
||||
|
||||
fun readLegacySpiderInfo(inputStream: InputStream): SpiderInfo {
|
||||
val source = inputStream.source().buffer()
|
||||
fun read(): String = source.readUtf8LineStrict()
|
||||
fun Path.readLegacySpiderInfo() = read {
|
||||
fun read() = readLineStrict()
|
||||
fun readInt() = read().toInt()
|
||||
fun readLong() = read().toLong()
|
||||
|
||||
fun readInt(): Int = read().toInt()
|
||||
|
||||
fun readLong(): Long = read().toLong()
|
||||
repeat(2) { read() } // We assert that only info v2
|
||||
read() // Skip version, we assert it's v2
|
||||
read() // Skip startPage
|
||||
val gid = readLong()
|
||||
val token = read()
|
||||
read()
|
||||
val previewPages = readInt()
|
||||
read() // Skip mode
|
||||
read() // Skip previewPages
|
||||
val previewPerPage = readInt()
|
||||
val pages = read().toInt()
|
||||
val pTokenMap = hashMapOf<Int, String>()
|
||||
val info = SpiderInfo(gid, token, pages, pTokenMap, previewPages, previewPerPage)
|
||||
runCatching {
|
||||
while (true) {
|
||||
val line = read()
|
||||
val pos = line.indexOf(" ")
|
||||
if (pos > 0) {
|
||||
val index = line.substring(0, pos).toInt()
|
||||
val pToken = line.substring(pos + 1)
|
||||
if (pToken.isNotEmpty()) {
|
||||
pTokenMap[index] = pToken
|
||||
val pages = readInt()
|
||||
SpiderInfo(gid, token, pages, previewPerPage = previewPerPage).apply {
|
||||
runCatching {
|
||||
while (true) {
|
||||
val line = read()
|
||||
val pos = line.indexOf(" ")
|
||||
if (pos > 0) {
|
||||
val index = line.substring(0, pos).toInt()
|
||||
val pToken = line.substring(pos + 1)
|
||||
if (pToken.isNotEmpty()) {
|
||||
pTokenMap[index] = pToken
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return info
|
||||
}
|
||||
@@ -15,10 +15,8 @@
|
||||
*/
|
||||
package com.hippo.ehviewer.shortcuts
|
||||
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.content.ContextCompat
|
||||
import com.hippo.ehviewer.download.DownloadService
|
||||
|
||||
/**
|
||||
@@ -27,8 +25,8 @@ import com.hippo.ehviewer.download.DownloadService
|
||||
class ShortcutsActivity : AppCompatActivity() {
|
||||
public override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
when (intent?.action) {
|
||||
DownloadService.ACTION_START_ALL, DownloadService.ACTION_STOP_ALL -> ContextCompat.startForegroundService(this, Intent(this, DownloadService::class.java).setAction(intent.action))
|
||||
when (val action = intent?.action) {
|
||||
DownloadService.ACTION_START_ALL, DownloadService.ACTION_STOP_ALL -> DownloadService.startService(action)
|
||||
}
|
||||
finish()
|
||||
}
|
||||
|
||||
@@ -1,12 +1,24 @@
|
||||
package com.hippo.ehviewer.spider
|
||||
|
||||
import com.ehviewer.core.database.util.SimpleTagsConverter
|
||||
import com.ehviewer.core.files.read
|
||||
import com.ehviewer.core.files.write
|
||||
import com.ehviewer.core.model.GalleryDetail
|
||||
import com.ehviewer.core.model.GalleryInfo
|
||||
import com.ehviewer.core.model.GalleryTag
|
||||
import com.ehviewer.core.model.PowerStatus
|
||||
import com.ehviewer.core.model.TagNamespace
|
||||
import com.ehviewer.core.model.TagNamespace.Artist
|
||||
import com.ehviewer.core.model.TagNamespace.Character
|
||||
import com.ehviewer.core.model.TagNamespace.Cosplayer
|
||||
import com.ehviewer.core.model.TagNamespace.Female
|
||||
import com.ehviewer.core.model.TagNamespace.Group
|
||||
import com.ehviewer.core.model.TagNamespace.Location
|
||||
import com.ehviewer.core.model.TagNamespace.Male
|
||||
import com.ehviewer.core.model.TagNamespace.Mixed
|
||||
import com.ehviewer.core.model.TagNamespace.Other
|
||||
import com.ehviewer.core.model.TagNamespace.Parody
|
||||
import com.hippo.ehviewer.client.EhUrl
|
||||
import com.hippo.ehviewer.client.data.GalleryDetail
|
||||
import com.hippo.ehviewer.client.data.GalleryInfo
|
||||
import com.hippo.ehviewer.client.data.SimpleTagsConverter
|
||||
import com.hippo.ehviewer.client.data.TagNamespace
|
||||
import com.hippo.files.openInputStream
|
||||
import com.hippo.files.openOutputStream
|
||||
import kotlinx.serialization.KSerializer
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
@@ -14,12 +26,12 @@ import kotlinx.serialization.descriptors.PrimitiveKind
|
||||
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
|
||||
import kotlinx.serialization.encoding.Decoder
|
||||
import kotlinx.serialization.encoding.Encoder
|
||||
import net.devrieze.xmlutil.serialization.kxio.decodeFromSource
|
||||
import net.devrieze.xmlutil.serialization.kxio.encodeToSink
|
||||
import nl.adaptivity.xmlutil.XmlDeclMode
|
||||
import nl.adaptivity.xmlutil.core.XmlVersion
|
||||
import nl.adaptivity.xmlutil.newWriter
|
||||
import nl.adaptivity.xmlutil.serialization.XML
|
||||
import nl.adaptivity.xmlutil.serialization.XmlElement
|
||||
import nl.adaptivity.xmlutil.xmlStreaming
|
||||
import okio.Path
|
||||
|
||||
const val COMIC_INFO_FILE = "ComicInfo.xml"
|
||||
@@ -42,18 +54,17 @@ fun GalleryInfo.getComicInfo(): ComicInfo {
|
||||
val otherTags = mutableListOf<String>()
|
||||
with(TagNamespace) {
|
||||
when (this@getComicInfo) {
|
||||
is GalleryDetail -> tags.forEach { tagList ->
|
||||
val list = tagList.filterNot { it == TAG_ORIGINAL || it.startsWith('_') }
|
||||
when (val ns = TagNamespace(tagList.groupName)) {
|
||||
is GalleryDetail -> tagGroups.forEach { group ->
|
||||
val list = group.tags.filterNot { (text, power, _) -> text == TAG_ORIGINAL || power == PowerStatus.Weak }.map(GalleryTag::text)
|
||||
when (val ns = group.namespace) {
|
||||
Artist, Cosplayer -> artists.addAll(list)
|
||||
Group -> groups.addAll(list)
|
||||
Character -> characters.addAll(list)
|
||||
Parody -> parodies.addAll(list)
|
||||
Other -> otherTags.addAll(list)
|
||||
Female, Male, Mixed -> ns.toPrefix()?.let { prefix ->
|
||||
Location, Other -> otherTags.addAll(list)
|
||||
Female, Male, Mixed -> ns.prefix.let { prefix ->
|
||||
list.forEach { tag -> otherTags.add("$prefix:$tag") }
|
||||
}
|
||||
|
||||
else -> Unit
|
||||
}
|
||||
}
|
||||
@@ -61,13 +72,13 @@ fun GalleryInfo.getComicInfo(): ComicInfo {
|
||||
else -> simpleTags?.forEach { tagString ->
|
||||
val (namespace, tag) = tagString.split(':', limit = 2)
|
||||
.takeIf { it.size == 2 } ?: return@forEach // Ignore temp tags that don't have namespace
|
||||
when (val ns = TagNamespace(namespace)) {
|
||||
when (val ns = from(namespace)) {
|
||||
Artist, Cosplayer -> artists.add(tag)
|
||||
Group -> groups.add(tag)
|
||||
Character -> characters.add(tag)
|
||||
Parody -> if (tag != TAG_ORIGINAL) parodies.add(tag)
|
||||
Other -> otherTags.add(tag)
|
||||
Female, Male, Mixed -> ns.toPrefix()?.let { otherTags.add("$it:$tag") }
|
||||
Location, Other -> otherTags.add(tag)
|
||||
Female, Male, Mixed -> ns.prefix.let { otherTags.add("$it:$tag") }
|
||||
else -> Unit
|
||||
}
|
||||
}
|
||||
@@ -96,19 +107,11 @@ fun ComicInfo.toSimpleTags() = listOfNotNull(
|
||||
teams,
|
||||
).flatten().ifEmpty { null }
|
||||
|
||||
fun ComicInfo.write(file: Path) {
|
||||
file.openOutputStream().bufferedWriter().use {
|
||||
xmlStreaming.newWriter(it).use { writer ->
|
||||
xml.encodeToWriter(writer, ComicInfo.serializer(), this)
|
||||
}
|
||||
}
|
||||
}
|
||||
fun writeComicInfo(info: ComicInfo, file: Path) = file.write { xml.encodeToSink(this, info) }
|
||||
|
||||
fun readComicInfo(file: Path): ComicInfo? = runCatching {
|
||||
file.openInputStream().bufferedReader().use {
|
||||
xmlStreaming.newReader(it).use { reader ->
|
||||
xml.decodeFromReader(ComicInfo.serializer(), reader)
|
||||
}
|
||||
file.read {
|
||||
xml.decodeFromSource<ComicInfo>(this)
|
||||
}
|
||||
}.getOrNull()
|
||||
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
package com.hippo.ehviewer.spider
|
||||
|
||||
import com.hippo.ehviewer.client.data.GalleryInfo
|
||||
import com.hippo.ehviewer.client.thumbUrl
|
||||
import com.hippo.ehviewer.dao.DownloadInfo
|
||||
|
||||
object DownloadInfoMagics {
|
||||
private const val DOWNLOAD_INFO_DIRNAME_URL_MAGIC = "$"
|
||||
private const val DOWNLOAD_INFO_DIRNAME_URL_SEPARATOR = "|"
|
||||
|
||||
fun encodeMagicRequestOrUrl(info: GalleryInfo): String {
|
||||
val url = info.thumbUrl
|
||||
val location = (info as? DownloadInfo)?.dirname
|
||||
return if (location.isNullOrBlank()) {
|
||||
url
|
||||
} else {
|
||||
DOWNLOAD_INFO_DIRNAME_URL_MAGIC + url + DOWNLOAD_INFO_DIRNAME_URL_SEPARATOR + location
|
||||
}
|
||||
}
|
||||
|
||||
fun decodeMagicRequestOrUrl(encoded: String): Pair<String, String?> = if (encoded.startsWith(DOWNLOAD_INFO_DIRNAME_URL_MAGIC)) {
|
||||
val (a, b) = encoded.removePrefix(DOWNLOAD_INFO_DIRNAME_URL_MAGIC).split(DOWNLOAD_INFO_DIRNAME_URL_SEPARATOR)
|
||||
a to b
|
||||
} else {
|
||||
encoded to null
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
package com.hippo.ehviewer.spider
|
||||
|
||||
import androidx.collection.MutableObjectIntMap
|
||||
import androidx.collection.mutableObjectIntMapOf
|
||||
import arrow.fx.coroutines.fixedRate
|
||||
import com.hippo.ehviewer.Settings
|
||||
import com.hippo.ehviewer.client.executeSafely
|
||||
import com.hippo.ehviewer.ktor.reset
|
||||
import com.hippo.ehviewer.util.LowSpeedException
|
||||
import com.hippo.ehviewer.util.ensureSuccess
|
||||
import io.ktor.client.plugins.ConnectTimeoutException
|
||||
import io.ktor.client.plugins.onDownload
|
||||
import io.ktor.client.plugins.timeout
|
||||
import io.ktor.client.request.HttpRequestBuilder
|
||||
import io.ktor.client.statement.HttpResponse
|
||||
import io.ktor.client.statement.HttpStatement
|
||||
import kotlin.concurrent.atomics.AtomicReference
|
||||
import kotlin.math.pow
|
||||
import kotlin.math.roundToInt
|
||||
import kotlin.time.Clock
|
||||
import kotlin.time.Duration
|
||||
import kotlin.time.Duration.Companion.milliseconds
|
||||
import kotlin.time.Duration.Companion.seconds
|
||||
import kotlin.time.Instant
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.mapNotNull
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import kotlinx.io.IOException
|
||||
|
||||
class SpeedTracker(val window: Duration = 1.seconds) {
|
||||
private val mutex = Mutex()
|
||||
private val received = mutableObjectIntMapOf<Instant>()
|
||||
private var start: Instant? = null
|
||||
|
||||
suspend fun reset() = mutex.withLock {
|
||||
received.clear()
|
||||
start = null
|
||||
}
|
||||
|
||||
suspend fun track(bytes: Int) = Clock.System.now().let { now ->
|
||||
mutex.withLock {
|
||||
start = start ?: now
|
||||
received[now] = bytes
|
||||
}
|
||||
}
|
||||
|
||||
fun speedFlow(sample: Duration = 1.seconds) = fixedRate(sample).mapNotNull {
|
||||
mutex.withLock {
|
||||
start?.let { start ->
|
||||
val now = Clock.System.now()
|
||||
val passed = now - start
|
||||
val window = passed.coerceIn(10.milliseconds, window)
|
||||
val cutoff = now - window
|
||||
received.removeIf { time, _ -> time < cutoff }
|
||||
(received.fold(0) { total, _, v -> total + v } / (window / 1.seconds)).toLong()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private typealias OnTimeout = (IOException) -> Unit
|
||||
|
||||
suspend inline fun <R> timeoutBySpeed(
|
||||
url: String,
|
||||
crossinline request: suspend (HttpRequestBuilder.() -> Unit) -> HttpStatement,
|
||||
crossinline l: suspend (Long, Long, Int) -> Unit,
|
||||
crossinline f: suspend (HttpResponse) -> R,
|
||||
noinline t: OnTimeout = { throw it },
|
||||
) = coroutineScope {
|
||||
val onTimeout = AtomicReference<OnTimeout?>(t).let { { e: IOException -> it.exchange(null)?.invoke(e) } }
|
||||
val watchdog = launch {
|
||||
val timeout = 10.seconds
|
||||
delay(timeout)
|
||||
onTimeout(ConnectTimeoutException(url, timeout.inWholeMilliseconds))
|
||||
}
|
||||
val tracker = SpeedTracker(2.seconds)
|
||||
request {
|
||||
var prev = 0L
|
||||
onDownload { done, total ->
|
||||
val bytesRead = (done - prev).toInt()
|
||||
if (done == total!!) {
|
||||
tracker.reset()
|
||||
} else {
|
||||
tracker.track(bytesRead)
|
||||
}
|
||||
l(total, done, bytesRead)
|
||||
prev = done
|
||||
}
|
||||
timeout { reset() }
|
||||
}.executeSafely { resp ->
|
||||
watchdog.cancel()
|
||||
resp.status.ensureSuccess()
|
||||
val speedWatchdog = launch {
|
||||
val timeoutSpeed = speedLevelToSpeed(Settings.timeoutSpeed.value) * 1024L
|
||||
tracker.speedFlow().collect { speed ->
|
||||
if (speed < timeoutSpeed) {
|
||||
onTimeout(LowSpeedException(url, speed))
|
||||
}
|
||||
}
|
||||
}
|
||||
try {
|
||||
f(resp)
|
||||
} finally {
|
||||
speedWatchdog.cancel()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const val MIN_SPEED_LEVEL = 3
|
||||
|
||||
fun speedLevelToSpeed(level: Int) = if (level == MIN_SPEED_LEVEL) 0 else 2f.pow(level).roundToInt()
|
||||
|
||||
inline fun <T, R> MutableObjectIntMap<T>.fold(initial: R, operation: (acc: R, T, Int) -> R): R {
|
||||
var accumulator = initial
|
||||
forEach { key, value -> accumulator = operation(accumulator, key, value) }
|
||||
return accumulator
|
||||
}
|
||||
@@ -20,44 +20,42 @@ import arrow.fx.coroutines.autoCloseable
|
||||
import arrow.fx.coroutines.closeable
|
||||
import arrow.fx.coroutines.parMap
|
||||
import arrow.fx.coroutines.resourceScope
|
||||
import com.ehviewer.core.database.model.DownloadArtist
|
||||
import com.ehviewer.core.files.delete
|
||||
import com.ehviewer.core.files.exists
|
||||
import com.ehviewer.core.files.find
|
||||
import com.ehviewer.core.files.isDirectory
|
||||
import com.ehviewer.core.files.list
|
||||
import com.ehviewer.core.files.mkdirs
|
||||
import com.ehviewer.core.files.moveTo
|
||||
import com.ehviewer.core.files.openFileDescriptor
|
||||
import com.ehviewer.core.files.sendTo
|
||||
import com.ehviewer.core.model.GalleryDetail
|
||||
import com.ehviewer.core.model.GalleryInfo
|
||||
import com.ehviewer.core.util.logcat
|
||||
import com.hippo.ehviewer.EhApplication.Companion.imageCache as sCache
|
||||
import com.hippo.ehviewer.EhDB
|
||||
import com.hippo.ehviewer.Settings
|
||||
import com.hippo.ehviewer.client.EhEngine
|
||||
import com.hippo.ehviewer.client.EhUtils.getSuitableTitle
|
||||
import com.hippo.ehviewer.client.data.GalleryDetail
|
||||
import com.hippo.ehviewer.client.data.GalleryInfo
|
||||
import com.hippo.ehviewer.client.ehRequest
|
||||
import com.hippo.ehviewer.client.executeSafely
|
||||
import com.hippo.ehviewer.client.getImageKey
|
||||
import com.hippo.ehviewer.coil.read
|
||||
import com.hippo.ehviewer.coil.suspendEdit
|
||||
import com.hippo.ehviewer.dao.DownloadArtist
|
||||
import com.hippo.ehviewer.download.DownloadManager
|
||||
import com.hippo.ehviewer.download.downloadLocation
|
||||
import com.hippo.ehviewer.download.tempDownloadDir
|
||||
import com.hippo.ehviewer.image.PathSource
|
||||
import com.hippo.ehviewer.jni.archiveFdBatch
|
||||
import com.hippo.ehviewer.util.FileUtils
|
||||
import com.hippo.ehviewer.util.sendTo
|
||||
import com.hippo.ehviewer.util.copyTo
|
||||
import com.hippo.ehviewer.util.sha1
|
||||
import com.hippo.files.delete
|
||||
import com.hippo.files.exists
|
||||
import com.hippo.files.find
|
||||
import com.hippo.files.isDirectory
|
||||
import com.hippo.files.list
|
||||
import com.hippo.files.mkdirs
|
||||
import com.hippo.files.moveTo
|
||||
import com.hippo.files.openFileDescriptor
|
||||
import com.hippo.files.openOutputStream
|
||||
import eu.kanade.tachiyomi.util.system.logcat
|
||||
import io.ktor.client.plugins.onDownload
|
||||
import io.ktor.client.statement.HttpResponse
|
||||
import io.ktor.client.statement.bodyAsChannel
|
||||
import io.ktor.client.statement.request
|
||||
import io.ktor.http.isSuccess
|
||||
import io.ktor.utils.io.copyTo
|
||||
import kotlin.io.path.readText
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock
|
||||
import kotlin.concurrent.read
|
||||
import kotlin.concurrent.write
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import okio.Path
|
||||
|
||||
@@ -67,13 +65,14 @@ class SpiderDen(val info: GalleryInfo) {
|
||||
private set
|
||||
|
||||
private var tempDownloadDir: Path? = null
|
||||
private val saveAsCbz = Settings.saveAsCbz
|
||||
private val saveAsCbz = Settings.saveAsCbz.value
|
||||
private val archiveName = "$gid.cbz"
|
||||
|
||||
private val lock = ReentrantReadWriteLock()
|
||||
|
||||
// Search in both directories to maintain compatibility
|
||||
private val fileCache by lazy {
|
||||
(tempDownloadDir?.list().orEmpty() + downloadDir?.list().orEmpty())
|
||||
.associateBy { it.name } as MutableMap
|
||||
listOfNotNull(tempDownloadDir, downloadDir).map(Path::list).flatten().associateBy { it.name } as MutableMap
|
||||
}
|
||||
|
||||
private val imageDir
|
||||
@@ -102,14 +101,12 @@ class SpiderDen(val info: GalleryInfo) {
|
||||
|
||||
private fun containInCache(index: Int): Boolean {
|
||||
val key = getImageKey(gid, index)
|
||||
return sCache.read(key) { true } ?: false
|
||||
return sCache.read(key) {} != null
|
||||
}
|
||||
|
||||
private fun findImageFile(index: Int, temp: Boolean = false) = synchronized(fileCache) {
|
||||
private fun findImageFile(index: Int, temp: Boolean = false) = lock.read {
|
||||
val head = perFilename(index)
|
||||
fileCache.entries.firstOrNull { (name) ->
|
||||
name.startsWith(head) && temp == name.endsWith(TEMP_SUFFIX)
|
||||
}?.value
|
||||
fileCache.entries.firstOrNull { (name) -> name.startsWith(head) && temp == name.endsWith(TEMP_SUFFIX) }?.value
|
||||
}
|
||||
|
||||
private fun containInDownloadDir(index: Int): Boolean = findImageFile(index) != null
|
||||
@@ -122,8 +119,7 @@ class SpiderDen(val info: GalleryInfo) {
|
||||
val extension = metadata.toFile().readText()
|
||||
val file = dir.findDownloadFileForIndex(index, extension)
|
||||
data sendTo file
|
||||
true
|
||||
} ?: false
|
||||
} != null
|
||||
}.onFailure {
|
||||
logcat(it)
|
||||
}.getOrDefault(false)
|
||||
@@ -148,11 +144,9 @@ class SpiderDen(val info: GalleryInfo) {
|
||||
return sCache.remove(key)
|
||||
}
|
||||
|
||||
private fun removeTempFile(index: Int) = findImageFile(index, temp = true)?.run {
|
||||
delete()
|
||||
synchronized(fileCache) {
|
||||
fileCache.remove(name)
|
||||
}
|
||||
private fun removeTempFile(index: Int) = findImageFile(index, temp = true)?.let { file ->
|
||||
file.delete()
|
||||
lock.write { fileCache.remove(file.name) }
|
||||
}
|
||||
|
||||
fun removeIntermediateFiles(index: Int) {
|
||||
@@ -160,11 +154,9 @@ class SpiderDen(val info: GalleryInfo) {
|
||||
removeTempFile(index)
|
||||
}
|
||||
|
||||
private fun Path.findDownloadFileForIndex(index: Int, extension: String): Path {
|
||||
private fun Path.findDownloadFileForIndex(index: Int, extension: String) = with(lock) {
|
||||
val name = perFilename(index, extension)
|
||||
return synchronized(fileCache) {
|
||||
fileCache.getOrPut(name) { resolve(name) }
|
||||
}
|
||||
read { fileCache[name] } ?: resolve(name).also { write { fileCache[name] = it } }
|
||||
}
|
||||
|
||||
suspend fun makeHttpCallAndSaveImage(
|
||||
@@ -172,19 +164,12 @@ class SpiderDen(val info: GalleryInfo) {
|
||||
url: String,
|
||||
referer: String?,
|
||||
notifyProgress: (Long, Long, Int) -> Unit,
|
||||
) = ehRequest(url, referer) {
|
||||
var prev = 0L
|
||||
onDownload { done, total ->
|
||||
notifyProgress(total!!, done, (done - prev).toInt())
|
||||
prev = done
|
||||
}
|
||||
}.executeSafely {
|
||||
if (it.status.isSuccess()) {
|
||||
saveFromHttpResponse(index, it)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
) = timeoutBySpeed(
|
||||
url,
|
||||
{ ehRequest(url, referer, builder = it) },
|
||||
notifyProgress,
|
||||
{ resp -> check(saveFromHttpResponse(index, resp)) },
|
||||
)
|
||||
|
||||
private suspend inline fun saveResponseMeta(
|
||||
index: Int,
|
||||
@@ -196,8 +181,9 @@ class SpiderDen(val info: GalleryInfo) {
|
||||
fops(tempFile)
|
||||
val file = resolve(tempFile.name.removeSuffix(TEMP_SUFFIX))
|
||||
file.delete()
|
||||
tempFile.moveTo(file)
|
||||
synchronized(fileCache) {
|
||||
lock.write { fileCache.remove(file.name) }
|
||||
tempFile moveTo file
|
||||
lock.write {
|
||||
fileCache.remove(tempFile.name)
|
||||
fileCache[file.name] = file
|
||||
}
|
||||
@@ -219,12 +205,12 @@ class SpiderDen(val info: GalleryInfo) {
|
||||
val url = response.request.url.toString()
|
||||
val extension = MimeTypeMap.getFileExtensionFromUrl(url).ifEmpty { "jpg" }
|
||||
return saveResponseMeta(index, extension) { outFile ->
|
||||
outFile.openOutputStream().use {
|
||||
response.bodyAsChannel().copyTo(it.channel)
|
||||
response.bodyAsChannel().copyTo(outFile)
|
||||
FileHashRegex.find(url)?.let {
|
||||
val expected = it.groupValues[1]
|
||||
val actual = outFile.sha1()
|
||||
check(expected == actual) { "File hash mismatch: expected $expected, but got $actual\nURL: $url" }
|
||||
}
|
||||
val expected = FileHashRegex.findAll(url).last().groupValues[1]
|
||||
val actual = outFile.sha1()
|
||||
check(expected == actual) { "File hash mismatch: expected $expected, but got $actual\nURL: $url" }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -256,11 +242,11 @@ class SpiderDen(val info: GalleryInfo) {
|
||||
|
||||
fun getExtension(index: Int): String? {
|
||||
val key = getImageKey(gid, index)
|
||||
return sCache.read(key) { metadata.toNioPath().readText() }
|
||||
return sCache.read(key) { metadata.toFile().readText() }
|
||||
?: findImageFile(index)?.name.let { FileUtils.getExtensionFromFilename(it) }
|
||||
}
|
||||
|
||||
fun getImageSource(index: Int): PathSource? {
|
||||
fun getImageSource(index: Int): PathSource {
|
||||
if (mode == SpiderQueen.MODE_READ) {
|
||||
val key = getImageKey(gid, index)
|
||||
val snapshot = sCache.openSnapshot(key)
|
||||
@@ -268,19 +254,19 @@ class SpiderDen(val info: GalleryInfo) {
|
||||
return object : PathSource, AutoCloseable by snapshot {
|
||||
override val source = snapshot.data
|
||||
override val type by lazy {
|
||||
snapshot.metadata.toNioPath().readText()
|
||||
snapshot.metadata.toFile().readText()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
val source = findImageFile(index) ?: return null
|
||||
val source = requireNotNull(findImageFile(index)) { "Source $index not found!" }
|
||||
return object : PathSource {
|
||||
override val source = source
|
||||
override val type by lazy {
|
||||
FileUtils.getExtensionFromFilename(source.name)!!
|
||||
}
|
||||
|
||||
override fun close() {}
|
||||
override fun close() = Unit
|
||||
}
|
||||
}
|
||||
|
||||
@@ -315,8 +301,7 @@ class SpiderDen(val info: GalleryInfo) {
|
||||
return archived
|
||||
}
|
||||
|
||||
suspend fun exportAsCbz(file: Path) =
|
||||
downloadDir!!.find(archiveName)?.sendTo(file) ?: archiveTo(file)
|
||||
suspend fun exportAsCbz(file: Path) = downloadDir!!.find(archiveName)?.sendTo(file) ?: archiveTo(file)
|
||||
|
||||
private suspend fun archiveTo(file: Path) = resourceScope {
|
||||
val comicInfo = closeable {
|
||||
@@ -330,7 +315,7 @@ class SpiderDen(val info: GalleryInfo) {
|
||||
}
|
||||
val pages = info.pages
|
||||
val (fdBatch, names) = (0 until pages).parMap { idx ->
|
||||
val f = autoCloseable { getImageSource(idx) ?: throw CancellationException("Image #$idx not found") }
|
||||
val f = autoCloseable { getImageSource(idx) }
|
||||
closeable { f.source.openFileDescriptor("r") }.fd to perFilename(idx, f.type)
|
||||
}.run { plus(comicInfo.fd to COMIC_INFO_FILE) }.unzip()
|
||||
val arcFd = closeable { file.openFileDescriptor("rw") }
|
||||
@@ -349,19 +334,15 @@ class SpiderDen(val info: GalleryInfo) {
|
||||
suspend fun writeComicInfo(fetchMetadata: Boolean = true) {
|
||||
downloadDir?.run {
|
||||
resolve(COMIC_INFO_FILE).also {
|
||||
runCatching {
|
||||
if (info !is GalleryDetail && fetchMetadata) {
|
||||
EhEngine.fillGalleryListByApi(listOf(info))
|
||||
if (info !is GalleryDetail && fetchMetadata) {
|
||||
EhEngine.fillGalleryListByApi(listOf(info))
|
||||
}
|
||||
info.getComicInfo().apply {
|
||||
writeComicInfo(this, it)
|
||||
DownloadManager.getDownloadInfo(gid)?.let { downloadInfo ->
|
||||
downloadInfo.artistInfoList = DownloadArtist.from(gid, penciller.orEmpty())
|
||||
EhDB.putDownloadArtist(gid, downloadInfo.artistInfoList)
|
||||
}
|
||||
info.getComicInfo().apply {
|
||||
write(it)
|
||||
DownloadManager.getDownloadInfo(gid)?.let { downloadInfo ->
|
||||
downloadInfo.artistInfoList = DownloadArtist.from(gid, penciller.orEmpty())
|
||||
EhDB.putDownloadArtist(gid, downloadInfo.artistInfoList)
|
||||
}
|
||||
}
|
||||
}.onFailure {
|
||||
logcat(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -370,7 +351,7 @@ class SpiderDen(val info: GalleryInfo) {
|
||||
|
||||
private const val TEMP_SUFFIX = ".tmp"
|
||||
private val FileNameRegex = Regex("^\\d{8}\\.\\w{3,4}")
|
||||
private val FileHashRegex = Regex("/([0-9a-f]{40})(?:-\\d+){3}-\\w+")
|
||||
private val FileHashRegex = Regex("/h/([0-9a-f]{40})")
|
||||
|
||||
fun perFilename(index: Int, extension: String = ""): String = "%08d.%s".format(index + 1, extension)
|
||||
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
package com.hippo.ehviewer.spider
|
||||
|
||||
import com.ehviewer.core.files.read
|
||||
import com.ehviewer.core.files.write
|
||||
import com.ehviewer.core.util.logcat
|
||||
import com.hippo.ehviewer.coil.edit
|
||||
import com.hippo.ehviewer.coil.read
|
||||
import com.hippo.ehviewer.ktbuilder.diskCache
|
||||
import com.hippo.ehviewer.legacy.readLegacySpiderInfo
|
||||
import com.hippo.files.openInputStream
|
||||
import com.hippo.files.openOutputStream
|
||||
import eu.kanade.tachiyomi.util.system.logcat
|
||||
import kotlinx.io.readByteArray
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.cbor.Cbor
|
||||
import kotlinx.serialization.decodeFromByteArray
|
||||
@@ -26,8 +27,6 @@ class SpiderInfo(
|
||||
|
||||
val pTokenMap: MutableMap<Int, String> = hashMapOf(),
|
||||
|
||||
var previewPages: Int = -1,
|
||||
|
||||
var previewPerPage: Int = -1,
|
||||
)
|
||||
|
||||
@@ -35,16 +34,12 @@ private val cbor = Cbor {
|
||||
ignoreUnknownKeys = true
|
||||
}
|
||||
|
||||
fun SpiderInfo.write(file: Path) {
|
||||
file.openOutputStream().use {
|
||||
it.write(cbor.encodeToByteArray(this))
|
||||
}
|
||||
}
|
||||
fun SpiderInfo.write(file: Path) = file.write { write(cbor.encodeToByteArray(contextOf<SpiderInfo>())) }
|
||||
|
||||
fun SpiderInfo.saveToCache() {
|
||||
runSuspendCatching {
|
||||
spiderInfoCache.edit(gid.toString()) {
|
||||
data.toFile().writeBytes(cbor.encodeToByteArray(this@saveToCache))
|
||||
write(data)
|
||||
}
|
||||
}.onFailure {
|
||||
logcat(it)
|
||||
@@ -60,18 +55,14 @@ private val spiderInfoCache by lazy {
|
||||
|
||||
fun readFromCache(gid: Long): SpiderInfo? = spiderInfoCache.read(gid.toString()) {
|
||||
runCatching {
|
||||
cbor.decodeFromByteArray<SpiderInfo>(data.toFile().readBytes())
|
||||
data.read { cbor.decodeFromByteArray<SpiderInfo>(readByteArray()) }
|
||||
}.onFailure {
|
||||
logcat(it)
|
||||
}.getOrNull()
|
||||
}
|
||||
|
||||
fun readCompatFromPath(file: Path): SpiderInfo? = runCatching {
|
||||
file.openInputStream().use {
|
||||
cbor.decodeFromByteArray<SpiderInfo>(it.readBytes())
|
||||
}
|
||||
file.read { cbor.decodeFromByteArray<SpiderInfo>(readByteArray()) }
|
||||
}.getOrNull() ?: runCatching {
|
||||
file.openInputStream().use { readLegacySpiderInfo(it) }
|
||||
file.readLegacySpiderInfo()
|
||||
}.getOrNull()
|
||||
|
||||
const val TOKEN_FAILED = "failed"
|
||||
@@ -17,52 +17,44 @@
|
||||
package com.hippo.ehviewer.spider
|
||||
|
||||
import androidx.annotation.IntDef
|
||||
import androidx.collection.LongSparseArray
|
||||
import androidx.collection.set
|
||||
import arrow.core.Either
|
||||
import arrow.core.getOrElse
|
||||
import arrow.core.partially1
|
||||
import com.hippo.ehviewer.R
|
||||
import com.ehviewer.core.files.find
|
||||
import com.ehviewer.core.i18n.R
|
||||
import com.ehviewer.core.model.GalleryInfo
|
||||
import com.ehviewer.core.model.GalleryPreview
|
||||
import com.ehviewer.core.util.logcat
|
||||
import com.hippo.ehviewer.Settings
|
||||
import com.hippo.ehviewer.client.EhEngine
|
||||
import com.hippo.ehviewer.client.EhUrl
|
||||
import com.hippo.ehviewer.client.EhUrl.getGalleryDetailUrl
|
||||
import com.hippo.ehviewer.client.EhUrl.getGalleryMultiPageViewerUrl
|
||||
import com.hippo.ehviewer.client.EhUrl.referer
|
||||
import com.hippo.ehviewer.client.data.GalleryInfo
|
||||
import com.hippo.ehviewer.client.data.hasAds
|
||||
import com.hippo.ehviewer.client.ehRequest
|
||||
import com.hippo.ehviewer.client.EhUtils
|
||||
import com.hippo.ehviewer.client.exception.FatalException
|
||||
import com.hippo.ehviewer.client.exception.QuotaExceededException
|
||||
import com.hippo.ehviewer.client.fetchUsingAsText
|
||||
import com.hippo.ehviewer.client.parser.GalleryDetailParser.parsePages
|
||||
import com.hippo.ehviewer.client.parser.GalleryDetailParser.parsePreviewList
|
||||
import com.hippo.ehviewer.client.parser.GalleryDetailParser.parsePreviewPages
|
||||
import com.hippo.ehviewer.client.parser.GalleryMultiPageViewerPTokenParser
|
||||
import com.hippo.ehviewer.client.parser.GalleryPageUrlParser
|
||||
import com.hippo.ehviewer.image.Image
|
||||
import com.hippo.ehviewer.util.displayString
|
||||
import com.hippo.files.find
|
||||
import eu.kanade.tachiyomi.util.system.logcat
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
import kotlin.concurrent.atomics.AtomicInt
|
||||
import kotlin.concurrent.atomics.decrementAndFetch
|
||||
import kotlin.concurrent.atomics.incrementAndFetch
|
||||
import kotlin.time.Duration.Companion.milliseconds
|
||||
import kotlin.time.Duration.Companion.seconds
|
||||
import kotlin.time.TimeSource
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.CoroutineStart
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.TimeoutCancellationException
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.cancel
|
||||
import kotlinx.coroutines.currentCoroutineContext
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.ensureActive
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.Semaphore
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import kotlinx.coroutines.sync.withPermit
|
||||
import kotlinx.coroutines.withTimeout
|
||||
import moe.tarsin.coroutines.runSuspendCatching
|
||||
import okio.FileNotFoundException
|
||||
import okio.Path
|
||||
import splitties.init.appCtx
|
||||
|
||||
@@ -70,13 +62,13 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
|
||||
override val coroutineContext = Dispatchers.IO + Job()
|
||||
|
||||
@Volatile
|
||||
lateinit var mPageStateArray: IntArray
|
||||
lateinit var mSpiderInfo: SpiderInfo
|
||||
lateinit var pageStates: IntArray
|
||||
lateinit var spiderInfo: SpiderInfo
|
||||
|
||||
val mSpiderDen: SpiderDen = SpiderDen(galleryInfo)
|
||||
val spiderDen: SpiderDen = SpiderDen(galleryInfo)
|
||||
private val mPageStateLock = Any()
|
||||
private val mDownloadedPages = AtomicInteger(0)
|
||||
private val mFinishedPages = AtomicInteger(0)
|
||||
private val mDownloadedPages = AtomicInt(0)
|
||||
private val mFinishedPages = AtomicInt(0)
|
||||
private val mSpiderListeners: MutableList<OnSpiderListener> = ArrayList()
|
||||
|
||||
private var mReadReference = 0
|
||||
@@ -96,9 +88,9 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
|
||||
}
|
||||
}
|
||||
|
||||
fun notifyGet509(index: Int) {
|
||||
fun notifyFatal(error: FatalException) {
|
||||
synchronized(mSpiderListeners) {
|
||||
mSpiderListeners.forEach { it.onGet509(index) }
|
||||
mSpiderListeners.forEach { it.onFatal(error) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,9 +112,9 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
|
||||
mSpiderListeners.forEach {
|
||||
it.onPageSuccess(
|
||||
index,
|
||||
mFinishedPages.get(),
|
||||
mDownloadedPages.get(),
|
||||
mPageStateArray.size,
|
||||
mFinishedPages.load(),
|
||||
mDownloadedPages.load(),
|
||||
pageStates.size,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -134,9 +126,9 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
|
||||
it.onPageFailure(
|
||||
index,
|
||||
error,
|
||||
mFinishedPages.get(),
|
||||
mDownloadedPages.get(),
|
||||
mPageStateArray.size,
|
||||
mFinishedPages.load(),
|
||||
mDownloadedPages.load(),
|
||||
pageStates.size,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -146,52 +138,45 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
|
||||
synchronized(mSpiderListeners) {
|
||||
mSpiderListeners.forEach {
|
||||
it.onFinish(
|
||||
mFinishedPages.get(),
|
||||
mDownloadedPages.get(),
|
||||
mPageStateArray.size,
|
||||
mFinishedPages.load(),
|
||||
mDownloadedPages.load(),
|
||||
pageStates.size,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun notifyGetImageSuccess(index: Int, image: Image) {
|
||||
private fun notifyPageReady(index: Int) {
|
||||
synchronized(mSpiderListeners) {
|
||||
mSpiderListeners.forEach {
|
||||
it.onGetImageSuccess(index, image)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun notifyGetImageFailure(index: Int, error: String) {
|
||||
synchronized(mSpiderListeners) {
|
||||
mSpiderListeners.forEach {
|
||||
it.onGetImageFailure(index, error)
|
||||
it.onPageReady(index)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var downloadMode = false
|
||||
val isReady
|
||||
get() = this::mSpiderInfo.isInitialized && this::mPageStateArray.isInitialized
|
||||
|
||||
private val isReady
|
||||
get() = this::spiderInfo.isInitialized && this::pageStates.isInitialized
|
||||
|
||||
private val updateLock = Mutex()
|
||||
|
||||
private suspend fun updateMode() {
|
||||
if (!awaitReady()) return
|
||||
private suspend fun updateMode() = runSuspendCatching {
|
||||
awaitReady()
|
||||
updateLock.withLock {
|
||||
val mode: Int = if (mDownloadReference > 0) {
|
||||
MODE_DOWNLOAD
|
||||
} else {
|
||||
MODE_READ
|
||||
}
|
||||
mSpiderDen.setMode(mode)
|
||||
spiderDen.setMode(mode)
|
||||
|
||||
// Update download page
|
||||
val intoDownloadMode = mode == MODE_DOWNLOAD
|
||||
if (intoDownloadMode && !downloadMode) {
|
||||
// Clear download state
|
||||
synchronized(mPageStateLock) {
|
||||
val temp: IntArray = mPageStateArray
|
||||
val temp: IntArray = pageStates
|
||||
var i = 0
|
||||
val n = temp.size
|
||||
while (i < n) {
|
||||
@@ -201,8 +186,8 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
|
||||
}
|
||||
i++
|
||||
}
|
||||
mDownloadedPages.lazySet(0)
|
||||
mFinishedPages.lazySet(0)
|
||||
mDownloadedPages.store(0)
|
||||
mFinishedPages.store(0)
|
||||
}
|
||||
mWorkerScope.enterDownloadMode()
|
||||
}
|
||||
@@ -226,20 +211,25 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
|
||||
check(!(mReadReference < 0 || mDownloadReference < 0)) { "Mode reference < 0" }
|
||||
}
|
||||
|
||||
private val prepareJob = async { doPrepare() }
|
||||
private val archiveJob = launch(start = CoroutineStart.LAZY) { mSpiderDen.archive() }
|
||||
private val prepareScope = CoroutineScope(coroutineContext + SupervisorJob())
|
||||
private val prepareJob = prepareScope.async { doPrepare() }
|
||||
private val archiveJob = launch(start = CoroutineStart.LAZY) { spiderDen.archive() }
|
||||
|
||||
private suspend fun doPrepare() {
|
||||
mSpiderDen.initDownloadDirIfExist()
|
||||
mSpiderInfo = readSpiderInfoFromLocal() ?: readSpiderInfoFromInternet() ?: return
|
||||
mPageStateArray = IntArray(mSpiderInfo.pages)
|
||||
notifyGetPages(mSpiderInfo.pages)
|
||||
spiderDen.initDownloadDirIfExist()
|
||||
val pages = Either.catch {
|
||||
spiderInfo = readSpiderInfoFromLocal() ?: readSpiderInfoFromInternet()
|
||||
spiderInfo.pages
|
||||
}.getOrElse {
|
||||
logcat(it)
|
||||
galleryInfo.pages
|
||||
}
|
||||
check(pages > 0)
|
||||
pageStates = IntArray(pages)
|
||||
notifyGetPages(pages)
|
||||
}
|
||||
|
||||
suspend fun awaitReady(): Boolean {
|
||||
prepareJob.await()
|
||||
return isReady
|
||||
}
|
||||
suspend fun awaitReady() = prepareJob.await()
|
||||
|
||||
private fun stop() {
|
||||
val queenScope = this
|
||||
@@ -247,9 +237,13 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
|
||||
if (archiveJob.isActive) {
|
||||
archiveJob.join()
|
||||
}
|
||||
if (!mSpiderDen.postArchive()) {
|
||||
if (!spiderDen.postArchive()) {
|
||||
if (mWorkerScope.isDownloadMode) {
|
||||
mSpiderDen.writeComicInfo()
|
||||
runCatching {
|
||||
spiderDen.writeComicInfo()
|
||||
}.onFailure {
|
||||
logcat(it)
|
||||
}
|
||||
}
|
||||
runCatching {
|
||||
writeSpiderInfoToLocal()
|
||||
@@ -262,34 +256,26 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
|
||||
}
|
||||
|
||||
val size
|
||||
get() = mPageStateArray.size
|
||||
get() = pageStates.size
|
||||
|
||||
val error: String?
|
||||
get() = null
|
||||
|
||||
fun forceRequest(index: Int, orgImg: Boolean) {
|
||||
request(index, true, orgImg)
|
||||
}
|
||||
|
||||
fun request(index: Int) {
|
||||
request(index, false)
|
||||
}
|
||||
|
||||
private fun getPageState(index: Int): Int {
|
||||
synchronized(mPageStateLock) {
|
||||
return if (index >= 0 && index < mPageStateArray.size) {
|
||||
mPageStateArray[index]
|
||||
return if (index >= 0 && index < pageStates.size) {
|
||||
pageStates[index]
|
||||
} else {
|
||||
STATE_NONE
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun preloadPages(pages: List<Int>, pair: Pair<Int, Int>) {
|
||||
fun preloadPages(pages: List<Int>, pair: IntRange) {
|
||||
mWorkerScope.updateRAList(pages, pair)
|
||||
}
|
||||
|
||||
private fun request(index: Int, force: Boolean, orgImg: Boolean = false) {
|
||||
fun request(index: Int, force: Boolean, orgImg: Boolean = false) {
|
||||
// Get page state
|
||||
val state = getPageState(index)
|
||||
|
||||
@@ -306,7 +292,7 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
|
||||
return if (STATE_FINISHED != state) {
|
||||
false
|
||||
} else {
|
||||
mSpiderDen.saveToPath(index, file)
|
||||
spiderDen.saveToPath(index, file)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -315,11 +301,11 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
|
||||
return if (STATE_FINISHED != state) {
|
||||
null
|
||||
} else {
|
||||
mSpiderDen.getExtension(index)
|
||||
spiderDen.getExtension(index)
|
||||
}
|
||||
}
|
||||
|
||||
private fun readSpiderInfoFromLocal(): SpiderInfo? = mSpiderDen.downloadDir?.run {
|
||||
private fun readSpiderInfoFromLocal(): SpiderInfo? = spiderDen.downloadDir?.run {
|
||||
find(SPIDER_INFO_FILENAME)?.let { file ->
|
||||
readCompatFromPath(file)?.takeIf {
|
||||
it.gid == galleryInfo.gid && it.token == galleryInfo.token
|
||||
@@ -328,51 +314,36 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
|
||||
}
|
||||
?: readFromCache(galleryInfo.gid)?.takeIf { it.gid == galleryInfo.gid && it.token == galleryInfo.token }
|
||||
|
||||
private fun readPreviews(body: String, index: Int, spiderInfo: SpiderInfo) {
|
||||
spiderInfo.previewPages = parsePreviewPages(body)
|
||||
val (previewList, pageUrlList) = parsePreviewList(body)
|
||||
if (previewList.isNotEmpty()) {
|
||||
if (index == 0) {
|
||||
spiderInfo.previewPerPage = previewList.size
|
||||
} else {
|
||||
spiderInfo.previewPerPage = previewList[0].position / index
|
||||
}
|
||||
private fun readPreviews(previews: List<GalleryPreview>, index: Int, spiderInfo: SpiderInfo) {
|
||||
if (index == 0) {
|
||||
spiderInfo.previewPerPage = previews.size
|
||||
} else {
|
||||
spiderInfo.previewPerPage = previews[0].position / index
|
||||
}
|
||||
pageUrlList.forEach {
|
||||
val result = GalleryPageUrlParser.parse(it)
|
||||
if (result != null) {
|
||||
spiderInfo.pTokenMap[result.page] = result.pToken
|
||||
previews.forEach {
|
||||
if (it.pToken.isNotEmpty()) {
|
||||
spiderInfo.pTokenMap[it.position] = it.pToken
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun readSpiderInfoFromInternet(): SpiderInfo? = runSuspendCatching {
|
||||
ehRequest(
|
||||
getGalleryDetailUrl(galleryInfo.gid, galleryInfo.token, 0, false),
|
||||
referer,
|
||||
).fetchUsingAsText {
|
||||
val pages = parsePages(this)
|
||||
val spiderInfo = SpiderInfo(galleryInfo.gid, galleryInfo.token, pages)
|
||||
readPreviews(this, 0, spiderInfo)
|
||||
spiderInfo
|
||||
}
|
||||
}.onFailure {
|
||||
logcat(it)
|
||||
}.getOrNull()
|
||||
private suspend fun readSpiderInfoFromInternet() = EhEngine.getPreviewList(
|
||||
getGalleryDetailUrl(galleryInfo.gid, galleryInfo.token),
|
||||
).run {
|
||||
val spiderInfo = SpiderInfo(galleryInfo.gid, galleryInfo.token, total)
|
||||
readPreviews(previews, 0, spiderInfo)
|
||||
spiderInfo
|
||||
}
|
||||
|
||||
private val isMpvAvailable = EhUtils.isMpvAvailable
|
||||
|
||||
suspend fun getPTokenFromMultiPageViewer(index: Int): String? {
|
||||
val spiderInfo = mSpiderInfo
|
||||
val url = getGalleryMultiPageViewerUrl(
|
||||
galleryInfo.gid,
|
||||
galleryInfo.token,
|
||||
)
|
||||
if (!isMpvAvailable) return null
|
||||
return runSuspendCatching {
|
||||
ehRequest(url, referer).fetchUsingAsText {
|
||||
GalleryMultiPageViewerPTokenParser.parse(this).forEachIndexed { index, s ->
|
||||
spiderInfo.pTokenMap[index] = s
|
||||
}
|
||||
spiderInfo.pTokenMap[index]
|
||||
EhEngine.getPTokenListFromMpv(galleryInfo.gid, galleryInfo.token).forEachIndexed { index, s ->
|
||||
spiderInfo.pTokenMap[index] = s
|
||||
}
|
||||
spiderInfo.pTokenMap[index]
|
||||
}.getOrElse {
|
||||
logcat(it)
|
||||
null
|
||||
@@ -380,27 +351,15 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
|
||||
}
|
||||
|
||||
suspend fun getPTokenFromInternet(index: Int): String? {
|
||||
val spiderInfo = mSpiderInfo
|
||||
|
||||
// Check previewIndex
|
||||
var previewIndex: Int
|
||||
previewIndex = if (spiderInfo.previewPerPage >= 0) {
|
||||
val previewIndex = if (spiderInfo.previewPerPage > 0) {
|
||||
index / spiderInfo.previewPerPage
|
||||
} else {
|
||||
0
|
||||
}
|
||||
if (spiderInfo.previewPages > 0) {
|
||||
previewIndex = previewIndex.coerceAtMost(spiderInfo.previewPages - 1)
|
||||
}
|
||||
val url = getGalleryDetailUrl(
|
||||
galleryInfo.gid,
|
||||
galleryInfo.token,
|
||||
previewIndex,
|
||||
false,
|
||||
)
|
||||
val url = getGalleryDetailUrl(galleryInfo.gid, galleryInfo.token, previewIndex)
|
||||
return runSuspendCatching {
|
||||
ehRequest(url, referer).fetchUsingAsText {
|
||||
readPreviews(this, previewIndex, spiderInfo)
|
||||
EhEngine.getPreviewList(url).run {
|
||||
readPreviews(previews, previewIndex, spiderInfo)
|
||||
spiderInfo.pTokenMap[index]
|
||||
}
|
||||
}.getOrElse {
|
||||
@@ -412,26 +371,25 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
|
||||
@Synchronized
|
||||
private fun writeSpiderInfoToLocal() {
|
||||
if (!isReady) return
|
||||
mSpiderDen.downloadDir?.run { mSpiderInfo.write(resolve(SPIDER_INFO_FILENAME)) }
|
||||
mSpiderInfo.saveToCache()
|
||||
spiderDen.downloadDir?.run { spiderInfo.write(resolve(SPIDER_INFO_FILENAME)) }
|
||||
spiderInfo.saveToCache()
|
||||
}
|
||||
|
||||
private fun isStateDone(state: Int): Boolean = state == STATE_FINISHED || state == STATE_FAILED
|
||||
|
||||
fun updatePageState(index: Int, @State state: Int, error: String? = null) {
|
||||
var oldState: Int
|
||||
synchronized<Unit>(mPageStateLock) {
|
||||
oldState = mPageStateArray[index]
|
||||
mPageStateArray[index] = state
|
||||
val oldState = pageStates[index]
|
||||
pageStates[index] = state
|
||||
if (!isStateDone(oldState) && isStateDone(state)) {
|
||||
mDownloadedPages.incrementAndGet()
|
||||
mDownloadedPages.incrementAndFetch()
|
||||
} else if (isStateDone(oldState) && !isStateDone(state)) {
|
||||
mDownloadedPages.decrementAndGet()
|
||||
mDownloadedPages.decrementAndFetch()
|
||||
}
|
||||
if (oldState != STATE_FINISHED && state == STATE_FINISHED) {
|
||||
mFinishedPages.incrementAndGet()
|
||||
mFinishedPages.incrementAndFetch()
|
||||
} else if (oldState == STATE_FINISHED && state != STATE_FINISHED) {
|
||||
mFinishedPages.decrementAndGet()
|
||||
mFinishedPages.decrementAndFetch()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -441,8 +399,8 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
|
||||
} else if (state == STATE_FINISHED) {
|
||||
notifyPageSuccess(index)
|
||||
}
|
||||
if (mDownloadedPages.get() == size) {
|
||||
if (mFinishedPages.get() == size) archiveJob.start()
|
||||
if (mDownloadedPages.load() == size) {
|
||||
if (mFinishedPages.load() == size) archiveJob.start()
|
||||
notifyAllPageDownloaded()
|
||||
}
|
||||
}
|
||||
@@ -455,14 +413,13 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
|
||||
@Retention(AnnotationRetention.SOURCE)
|
||||
annotation class State
|
||||
interface OnSpiderListener {
|
||||
fun onGetPages(pages: Int)
|
||||
fun onGet509(index: Int)
|
||||
fun onPageDownload(index: Int, contentLength: Long, receivedSize: Long, bytesRead: Int)
|
||||
fun onPageSuccess(index: Int, finished: Int, downloaded: Int, total: Int)
|
||||
fun onPageFailure(index: Int, error: String?, finished: Int, downloaded: Int, total: Int)
|
||||
fun onFinish(finished: Int, downloaded: Int, total: Int)
|
||||
fun onGetImageSuccess(index: Int, image: Image?)
|
||||
fun onGetImageFailure(index: Int, error: String?)
|
||||
fun onGetPages(pages: Int) {}
|
||||
fun onFatal(error: FatalException) {}
|
||||
fun onPageDownload(index: Int, contentLength: Long, receivedSize: Long, bytesRead: Int) {}
|
||||
fun onPageSuccess(index: Int, finished: Int, downloaded: Int, total: Int) {}
|
||||
fun onPageFailure(index: Int, error: String?, finished: Int, downloaded: Int, total: Int) {}
|
||||
fun onFinish(finished: Int, downloaded: Int, total: Int) {}
|
||||
fun onPageReady(index: Int) {}
|
||||
}
|
||||
|
||||
companion object {
|
||||
@@ -473,11 +430,11 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
|
||||
const val STATE_FINISHED = 2
|
||||
const val STATE_FAILED = 3
|
||||
const val SPIDER_INFO_FILENAME = ".ehviewer"
|
||||
private val sQueenMap = LongSparseArray<SpiderQueen>()
|
||||
private val sQueenMap = mutableMapOf<Long, SpiderQueen>()
|
||||
|
||||
fun obtainSpiderQueen(galleryInfo: GalleryInfo, @Mode mode: Int): SpiderQueen {
|
||||
val gid = galleryInfo.gid
|
||||
return (sQueenMap[gid] ?: SpiderQueen(galleryInfo).also { sQueenMap[gid] = it }).apply {
|
||||
return (sQueenMap.getOrPut(gid) { SpiderQueen(galleryInfo) }).apply {
|
||||
setMode(mode)
|
||||
launch { updateMode() }
|
||||
}
|
||||
@@ -497,13 +454,12 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
|
||||
}
|
||||
|
||||
private val mWorkerScope = object {
|
||||
private val mFetcherJobMap = hashMapOf<Int, Job>()
|
||||
private val mSemaphore = Semaphore(Settings.multiThreadDownload)
|
||||
private val jobs = hashMapOf<Int, Job>()
|
||||
private val semaphore = Semaphore(Settings.multiThreadDownload.value)
|
||||
private val pTokenLock = Mutex()
|
||||
private var showKey: String? = null
|
||||
private val showKeyLock = Mutex()
|
||||
private val mDownloadDelay = Settings.downloadDelay.milliseconds
|
||||
private val downloadTimeout = Settings.downloadTimeout.seconds
|
||||
private val downloadDelay = Settings.downloadDelay.value.milliseconds
|
||||
private var lastRequestTime = TimeSource.Monotonic.markNow()
|
||||
var isDownloadMode = false
|
||||
private set
|
||||
@@ -515,16 +471,16 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
|
||||
isDownloadMode = true
|
||||
}
|
||||
|
||||
fun updateRAList(list: List<Int>, cancelBounds: Pair<Int, Int> = 0 to Int.MAX_VALUE) {
|
||||
fun updateRAList(list: List<Int>, aliveBound: IntRange = 0..Int.MAX_VALUE) {
|
||||
if (isDownloadMode) return
|
||||
synchronized(mFetcherJobMap) {
|
||||
mFetcherJobMap.forEach { (i, job) ->
|
||||
if (i < cancelBounds.first || i > cancelBounds.second) {
|
||||
synchronized(jobs) {
|
||||
jobs.forEach { (i, job) ->
|
||||
if (i !in aliveBound) {
|
||||
job.cancel()
|
||||
}
|
||||
}
|
||||
list.forEach {
|
||||
if (mFetcherJobMap[it]?.isActive != true) {
|
||||
if (pageStates[it] != STATE_FINISHED && jobs[it]?.isActive != true) {
|
||||
doLaunchDownloadJob(it, false)
|
||||
}
|
||||
}
|
||||
@@ -532,15 +488,13 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
|
||||
}
|
||||
|
||||
private fun doLaunchDownloadJob(index: Int, force: Boolean, orgImg: Boolean = false) {
|
||||
val state = mPageStateArray[index]
|
||||
if (!force && state == STATE_FINISHED) return
|
||||
val currentJob = mFetcherJobMap[index]
|
||||
val currentJob = jobs[index]
|
||||
val skipHath = force && !orgImg && currentJob?.isActive == true
|
||||
if (force) currentJob?.cancel(CancellationException(FORCE_RETRY))
|
||||
if (currentJob?.isActive != true) {
|
||||
mFetcherJobMap[index] = launch {
|
||||
jobs[index] = launch {
|
||||
runCatching {
|
||||
mSemaphore.withPermit {
|
||||
semaphore.withPermit {
|
||||
doInJob(index, force, orgImg, skipHath)
|
||||
}
|
||||
}.onFailure {
|
||||
@@ -561,39 +515,37 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
|
||||
|
||||
fun launch(index: Int, force: Boolean = false, orgImg: Boolean) {
|
||||
check(index in 0 until size)
|
||||
val state = pageStates[index]
|
||||
if (!force && state == STATE_FINISHED) return notifyPageReady(index)
|
||||
if (!isDownloadMode) {
|
||||
synchronized(mFetcherJobMap) { doLaunchDownloadJob(index, force, orgImg) }
|
||||
synchronized(jobs) { doLaunchDownloadJob(index, force, orgImg) }
|
||||
}
|
||||
launch {
|
||||
jobs[index]?.join()
|
||||
if (pageStates[index] == STATE_FINISHED) notifyPageReady(index)
|
||||
}
|
||||
if (force) decoder.cancel(index)
|
||||
decoder.launch(index)
|
||||
}
|
||||
|
||||
private suspend fun doInJob(index: Int, force: Boolean, orgImg: Boolean, skipHath: Boolean) {
|
||||
suspend fun getPToken(index: Int): String? {
|
||||
if (index !in 0 until size) return null
|
||||
return mSpiderInfo.pTokenMap[index].takeIf { it != TOKEN_FAILED }
|
||||
?: getPTokenFromInternet(index)
|
||||
?: getPTokenFromInternet(index)
|
||||
if (!isReady || index !in 0 until size) return null
|
||||
return spiderInfo.pTokenMap[index]
|
||||
?: getPTokenFromMultiPageViewer(index)
|
||||
?: getPTokenFromInternet(index)
|
||||
// Preview size may changed, so try to get pToken twice
|
||||
?: getPTokenFromInternet(index)
|
||||
}
|
||||
val previousPToken: String?
|
||||
val pToken: String
|
||||
pTokenLock.withLock {
|
||||
if (force) {
|
||||
if (mSpiderInfo.pTokenMap[index] == TOKEN_FAILED) {
|
||||
mSpiderInfo.pTokenMap.remove(index)
|
||||
}
|
||||
} else if (index in mSpiderDen) {
|
||||
if (!force && index in spiderDen) {
|
||||
return updatePageState(index, STATE_FINISHED)
|
||||
}
|
||||
pToken = getPToken(index)
|
||||
?: return updatePageState(index, STATE_FAILED, PTOKEN_FAILED_MESSAGE).also {
|
||||
mSpiderInfo.pTokenMap[index] = TOKEN_FAILED
|
||||
}
|
||||
pToken = getPToken(index) ?: return updatePageState(index, STATE_FAILED, pTokenFailedMessage)
|
||||
previousPToken = getPToken(index - 1)
|
||||
|
||||
// The lock for delay should be acquired before anything else to maintain FIFO order
|
||||
delay(mDownloadDelay - lastRequestTime.elapsedNow())
|
||||
delay(downloadDelay - lastRequestTime.elapsedNow())
|
||||
lastRequestTime = TimeSource.Monotonic.markNow()
|
||||
}
|
||||
updatePageState(index, STATE_DOWNLOADING)
|
||||
@@ -602,9 +554,9 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
|
||||
var originImageUrl: String? = null
|
||||
var error: String? = null
|
||||
var forceHtml = false
|
||||
val original = Settings.downloadOriginImage || orgImg
|
||||
val original = Settings.downloadOriginImage.value || orgImg
|
||||
runSuspendCatching {
|
||||
repeat(2) { retries ->
|
||||
repeat(3) { retries ->
|
||||
var imageUrl: String? = null
|
||||
var localShowKey: String?
|
||||
|
||||
@@ -628,7 +580,7 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
|
||||
if (imageUrl == null) {
|
||||
runSuspendCatching {
|
||||
EhEngine.getGalleryPageApi(
|
||||
mSpiderInfo.gid,
|
||||
galleryInfo.gid,
|
||||
index,
|
||||
pToken,
|
||||
localShowKey,
|
||||
@@ -650,123 +602,58 @@ class SpiderQueen private constructor(val galleryInfo: GalleryInfo) : CoroutineS
|
||||
return@repeat
|
||||
}
|
||||
|
||||
val targetImageUrl: String?
|
||||
val referer: String?
|
||||
|
||||
if (original && originImageUrl != null) {
|
||||
val (targetImageUrl, referer) = if (original && originImageUrl != null) {
|
||||
if (retries == 1 && skipHathKey != null) {
|
||||
originImageUrl += "?nl=$skipHathKey"
|
||||
}
|
||||
val pageUrl = EhUrl.getPageUrl(mSpiderInfo.gid, index, pToken)
|
||||
targetImageUrl = EhEngine.getOriginalImageUrl(originImageUrl!!, pageUrl)
|
||||
referer = EhUrl.referer
|
||||
val pageUrl = EhUrl.getPageUrl(galleryInfo.gid, index, pToken)
|
||||
EhEngine.getOriginalImageUrl(originImageUrl!!, pageUrl) to referer
|
||||
} else {
|
||||
// Original image url won't change, so only set forceHtml in this case
|
||||
forceHtml = true
|
||||
targetImageUrl = imageUrl
|
||||
referer = null
|
||||
imageUrl to null
|
||||
}
|
||||
checkNotNull(targetImageUrl)
|
||||
|
||||
repeat(2) { times ->
|
||||
runCatching {
|
||||
logcat(WORKER_DEBUG_TAG) { "Start download image $index attempt #$times" }
|
||||
val success = withTimeout(downloadTimeout) {
|
||||
mSpiderDen.makeHttpCallAndSaveImage(
|
||||
index,
|
||||
targetImageUrl,
|
||||
referer,
|
||||
this@SpiderQueen::notifyPageDownload.partially1(index),
|
||||
)
|
||||
}
|
||||
|
||||
check(success)
|
||||
logcat(WORKER_DEBUG_TAG) { "Download image $index succeed" }
|
||||
updatePageState(index, STATE_FINISHED)
|
||||
return
|
||||
}.onFailure {
|
||||
mSpiderDen.removeIntermediateFiles(index)
|
||||
logcat(WORKER_DEBUG_TAG) { "Download image $index attempt #$times failed" }
|
||||
error = when (it) {
|
||||
is TimeoutCancellationException -> ERROR_TIMEOUT
|
||||
is CancellationException -> throw it
|
||||
else -> it.displayString()
|
||||
}
|
||||
runCatching {
|
||||
logcat(WORKER_DEBUG_TAG) { "Start download image $index" }
|
||||
spiderDen.makeHttpCallAndSaveImage(
|
||||
index,
|
||||
targetImageUrl,
|
||||
referer,
|
||||
this@SpiderQueen::notifyPageDownload.partially1(index),
|
||||
)
|
||||
logcat(WORKER_DEBUG_TAG) { "Download image $index succeed" }
|
||||
updatePageState(index, STATE_FINISHED)
|
||||
return
|
||||
}.onFailure {
|
||||
spiderDen.removeIntermediateFiles(index)
|
||||
logcat(WORKER_DEBUG_TAG) { "Download image $index failed" }
|
||||
when (it) {
|
||||
is CancellationException, is FileNotFoundException -> throw it
|
||||
}
|
||||
error = it.displayString()
|
||||
}
|
||||
}
|
||||
}.onFailure {
|
||||
when (it) {
|
||||
is QuotaExceededException -> notifyGet509(index)
|
||||
// TODO: Check IP ban
|
||||
}
|
||||
if (it is FatalException) notifyFatal(it)
|
||||
error = it.displayString()
|
||||
}
|
||||
updatePageState(index, STATE_FAILED, error)
|
||||
}
|
||||
|
||||
private val decoder = object {
|
||||
private val mSemaphore = Semaphore(4)
|
||||
private val mDecodeJobMap = hashMapOf<Int, Job>()
|
||||
|
||||
fun cancel(index: Int) {
|
||||
synchronized(mDecodeJobMap) {
|
||||
mDecodeJobMap.remove(index)?.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
fun launch(index: Int) {
|
||||
synchronized(mDecodeJobMap) {
|
||||
val currentJob = mDecodeJobMap[index]
|
||||
if (currentJob?.isActive != true) {
|
||||
mDecodeJobMap[index] = launch {
|
||||
runCatching {
|
||||
mSemaphore.withPermit {
|
||||
mFetcherJobMap[index]?.join()
|
||||
if (mPageStateArray[index] == STATE_FINISHED) {
|
||||
doInJob(index)
|
||||
}
|
||||
}
|
||||
}.onFailure {
|
||||
if (mPageStateArray[index] == STATE_FINISHED) {
|
||||
notifyGetImageFailure(index, DECODE_ERROR)
|
||||
}
|
||||
if (it is CancellationException) {
|
||||
throw it
|
||||
}
|
||||
}
|
||||
}
|
||||
if (error == "Invalid page.") {
|
||||
pTokenLock.withLock {
|
||||
spiderInfo.pTokenMap.remove(index)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun mayBeAd(index: Int) = index > size - 10
|
||||
|
||||
private val hasAds = galleryInfo.hasAds
|
||||
|
||||
private suspend fun doInJob(index: Int) {
|
||||
val src = mSpiderDen.getImageSource(index) ?: return
|
||||
val image = Image.decode(src, hasAds && Settings.stripExtraneousAds.value && mayBeAd(index))
|
||||
checkNotNull(image)
|
||||
runCatching {
|
||||
currentCoroutineContext().ensureActive()
|
||||
}.onFailure {
|
||||
image.recycle()
|
||||
throw it
|
||||
}
|
||||
notifyGetImageSuccess(index, image)
|
||||
}
|
||||
updatePageState(index, STATE_FAILED, error)
|
||||
}
|
||||
}
|
||||
private val pTokenFailedMessage = appCtx.getString(R.string.error_get_ptoken_error)
|
||||
}
|
||||
|
||||
private val PTOKEN_FAILED_MESSAGE = appCtx.getString(R.string.error_get_ptoken_error)
|
||||
private val ERROR_TIMEOUT = appCtx.getString(R.string.error_timeout)
|
||||
private val DECODE_ERROR = appCtx.getString(R.string.error_decoding_failed)
|
||||
private val URL_509_PATTERN = Regex("\\.org/.+/509s?\\.gif")
|
||||
private val Url509Regex = Regex("https://(?:ehgt\\.org/|exhentai\\.org/im)g/509s?\\.gif")
|
||||
private const val FORCE_RETRY = "Force retry"
|
||||
private const val WORKER_DEBUG_TAG = "SpiderQueenWorker"
|
||||
|
||||
private fun check509(url: String) {
|
||||
if (URL_509_PATTERN in url) throw QuotaExceededException()
|
||||
if (Url509Regex.matches(url)) throw QuotaExceededException()
|
||||
}
|
||||
Loaded 100 of 469 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user