-
Notifications
You must be signed in to change notification settings - Fork 49
/
ViewUnit.java
1227 lines (1127 loc) · 50 KB
/
ViewUnit.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.mishiranu.dashchan.ui.navigator.manager;
import android.animation.Animator;
import android.animation.ArgbEvaluator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.SystemClock;
import android.text.Layout;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.BackgroundColorSpan;
import android.util.Pair;
import android.util.SparseIntArray;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import chan.content.ChanLocator;
import chan.util.StringUtils;
import com.mishiranu.dashchan.C;
import com.mishiranu.dashchan.R;
import com.mishiranu.dashchan.content.ImageLoader;
import com.mishiranu.dashchan.content.Preferences;
import com.mishiranu.dashchan.content.model.AttachmentItem;
import com.mishiranu.dashchan.content.model.GalleryItem;
import com.mishiranu.dashchan.content.model.PostItem;
import com.mishiranu.dashchan.graphics.ColorScheme;
import com.mishiranu.dashchan.text.style.LinkSpan;
import com.mishiranu.dashchan.text.style.LinkSuffixSpan;
import com.mishiranu.dashchan.ui.gallery.GalleryOverlay;
import com.mishiranu.dashchan.util.AnimationUtils;
import com.mishiranu.dashchan.util.GraphicsUtils;
import com.mishiranu.dashchan.util.ListViewUtils;
import com.mishiranu.dashchan.util.PostDateFormatter;
import com.mishiranu.dashchan.util.ResourceUtils;
import com.mishiranu.dashchan.util.ViewUtils;
import com.mishiranu.dashchan.widget.AttachmentView;
import com.mishiranu.dashchan.widget.CardView;
import com.mishiranu.dashchan.widget.CommentTextView;
import com.mishiranu.dashchan.widget.LinebreakLayout;
import com.mishiranu.dashchan.widget.PostLinearLayout;
import com.mishiranu.dashchan.widget.ThemeEngine;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
public class ViewUnit {
private final UiManager uiManager;
private final PostDateFormatter postDateFormatter;
private final int thumbnailWidth;
private final int multipleAttachmentInfoWidth;
private final int commentMaxLines;
private final int commentAdditionalHeight;
private static final float ALPHA_HIDDEN_POST = 0.2f;
private static final float ALPHA_DELETED_POST = 0.5f;
@SuppressLint("InflateParams")
ViewUnit(UiManager uiManager) {
Context context = uiManager.getContext();
this.uiManager = uiManager;
postDateFormatter = new PostDateFormatter(context);
Configuration configuration = context.getResources().getConfiguration();
float density = ResourceUtils.obtainDensity(context);
// Define header height, image width and max comment field height
View view = LayoutInflater.from(context).inflate(R.layout.list_item_post, null);
View head = view.findViewById(R.id.head);
TextView comment = view.findViewById(R.id.comment);
View bottomBar = view.findViewById(R.id.bottom_bar);
ViewUtils.applyScaleSize(head.findViewById(R.id.subject), head.findViewById(R.id.number),
head.findViewById(R.id.name), head.findViewById(R.id.index), head.findViewById(R.id.date),
head.findViewById(R.id.attachment_info), comment, bottomBar.findViewById(R.id.bottom_bar_replies),
bottomBar.findViewById(R.id.bottom_bar_expand), bottomBar.findViewById(R.id.bottom_bar_open_thread));
int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec((int) (320 * density + 0.5f), View.MeasureSpec.AT_MOST);
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
view.measure(widthMeasureSpec, heightMeasureSpec);
commentMaxLines = Preferences.getPostMaxLines();
commentAdditionalHeight = bottomBar.getMeasuredHeight();
thumbnailWidth = head.getMeasuredHeight();
int additionalAttachmentInfoWidthDp = 64; // approximately equal to thumbnail width + right padding
int minAttachmentInfoWidthDp = additionalAttachmentInfoWidthDp + 68;
int maxAttachmentInfoWidthDp = additionalAttachmentInfoWidthDp + 84;
int attachmentInfoWidthDp = configuration.smallestScreenWidthDp * minAttachmentInfoWidthDp / 320;
attachmentInfoWidthDp = Math.max(Math.min(attachmentInfoWidthDp, maxAttachmentInfoWidthDp),
minAttachmentInfoWidthDp);
attachmentInfoWidthDp -= additionalAttachmentInfoWidthDp;
multipleAttachmentInfoWidth = (int) (attachmentInfoWidthDp * density + 0.5f);
}
private final CommentTextView.LinkListener defaultLinkListener = new CommentTextView.LinkListener() {
@Override
public void onLinkClick(CommentTextView view, String chanName, Uri uri, boolean confirmed) {
UiManager.Holder holder = ListViewUtils.getViewHolder(view, UiManager.Holder.class);
uiManager.interaction().handleLinkClick(holder.getConfigurationSet(), chanName, uri, confirmed);
}
@Override
public void onLinkLongClick(CommentTextView view, String chanName, Uri uri) {
uiManager.interaction().handleLinkLongClick(uri);
}
};
private final CommentTextView.CommentListener commentListener = new CommentTextView.CommentListener() {
@Override
public void onRequestSiblingsInvalidate(CommentTextView view) {
uiManager.sendPostItemMessage(view, UiManager.Message.INVALIDATE_COMMENT_VIEW);
}
@Override
public String onPrepareToCopy(CommentTextView view, Spannable text, int start, int end) {
return uiManager.interaction().getCopyReadyComment(text, start, end);
}
};
public RecyclerView.ViewHolder createThreadViewHolder(ViewGroup parent,
UiManager.ConfigurationSet configurationSet, boolean cell) {
return new ThreadViewHolder(parent, configurationSet, uiManager, cell);
}
public void bindThreadView(RecyclerView.ViewHolder viewHolder, PostItem postItem) {
Context context = uiManager.getContext();
ColorScheme colorScheme = ThemeEngine.getColorScheme(context);
ThreadViewHolder holder = (ThreadViewHolder) viewHolder;
holder.postItem = postItem;
holder.stateSage.setVisibility(postItem.getBumpLimitReachedState(0) == PostItem.BumpLimitState.REACHED
? View.VISIBLE : View.GONE);
holder.stateSticky.setVisibility(postItem.isSticky() ? View.VISIBLE : View.GONE);
holder.stateClosed.setVisibility(postItem.isClosed() ? View.VISIBLE : View.GONE);
String subject = postItem.getSubject();
if (!StringUtils.isEmpty(subject)) {
holder.subject.setVisibility(View.VISIBLE);
holder.subject.setText(subject);
} else {
holder.subject.setVisibility(View.GONE);
}
int parentWidth = (int) (ResourceUtils.obtainDensity(holder.itemView) *
holder.itemView.getResources().getConfiguration().screenWidthDp);
CharSequence comment = postItem.getThreadCommentShort(parentWidth, holder.comment.getTextSize(), 8);
colorScheme.apply(postItem.getThreadCommentShortSpans());
if (StringUtils.isEmpty(subject) && StringUtils.isEmpty(comment)) {
// Avoid 0 height
comment = " ";
}
holder.comment.setText(comment);
holder.comment.setVisibility(holder.comment.getText().length() > 0 ? View.VISIBLE : View.GONE);
holder.description.setText(postItem.formatThreadCardDescription(context, false));
List<AttachmentItem> attachmentItems = postItem.getAttachmentItems();
if (attachmentItems != null) {
AttachmentItem attachmentItem = attachmentItems.get(0);
boolean needShowMultipleIcon = attachmentItems.size() > 1;
attachmentItem.configureAndLoad(holder.thumbnail, needShowMultipleIcon, false);
holder.thumbnailClickListener.update(0, true, GalleryOverlay.NavigatePostMode.DISABLED);
holder.thumbnailLongClickListener.update(attachmentItem);
holder.thumbnail.setSfwMode(Preferences.isSfwMode());
holder.thumbnail.setVisibility(View.VISIBLE);
} else {
ImageLoader.getInstance().cancel(holder.thumbnail);
holder.thumbnail.resetImage(null);
holder.thumbnail.setVisibility(View.GONE);
}
holder.thumbnail.setOnClickListener(holder.thumbnailClickListener);
holder.thumbnail.setOnLongClickListener(holder.thumbnailLongClickListener);
}
public void bindThreadCellView(RecyclerView.ViewHolder viewHolder, PostItem postItem, int contentHeight) {
Context context = uiManager.getContext();
ColorScheme colorScheme = ThemeEngine.getColorScheme(context);
ThreadViewHolder holder = (ThreadViewHolder) viewHolder;
holder.postItem = postItem;
List<AttachmentItem> attachmentItems = postItem.getAttachmentItems();
boolean hidden = postItem.isHidden(holder.configurationSet.hidePerformer);
((View) holder.threadContent.getParent()).setAlpha(hidden ? ALPHA_HIDDEN_POST : 1f);
String subject = postItem.getSubject();
if (!StringUtils.isEmptyOrWhitespace(subject) && !hidden) {
holder.subject.setVisibility(View.VISIBLE);
holder.subject.setText(subject.trim());
} else {
holder.subject.setVisibility(View.GONE);
}
CharSequence comment = null;
if (hidden) {
comment = postItem.getHideReason();
}
if (comment == null) {
int parentWidth = (int) (ResourceUtils.obtainDensity(holder.itemView) *
holder.itemView.getResources().getConfiguration().screenWidthDp);
comment = postItem.getThreadCommentShort(parentWidth / 2,
holder.comment.getTextSize(), attachmentItems != null ? 4 : 12);
colorScheme.apply(postItem.getThreadCommentShortSpans());
}
holder.comment.setText(comment);
holder.comment.setVisibility(StringUtils.isEmpty(comment) ? View.GONE : View.VISIBLE);
holder.description.setText(postItem.formatThreadCardDescription(context, true));
if (attachmentItems != null && !hidden) {
AttachmentItem attachmentItem = attachmentItems.get(0);
boolean needShowMultipleIcon = attachmentItems.size() > 1;
attachmentItem.configureAndLoad(holder.thumbnail, needShowMultipleIcon, false);
holder.thumbnailClickListener.update(0, true, GalleryOverlay.NavigatePostMode.DISABLED);
holder.thumbnailLongClickListener.update(attachmentItem);
holder.thumbnail.setSfwMode(Preferences.isSfwMode());
holder.thumbnail.setVisibility(View.VISIBLE);
} else {
ImageLoader.getInstance().cancel(holder.thumbnail);
holder.thumbnail.resetImage(null);
holder.thumbnail.setVisibility(View.GONE);
}
holder.thumbnail.setOnClickListener(holder.thumbnailClickListener);
holder.thumbnail.setOnLongClickListener(holder.thumbnailLongClickListener);
holder.threadContent.getLayoutParams().height = contentHeight;
}
public RecyclerView.ViewHolder createThreadHiddenView(ViewGroup parent,
UiManager.ConfigurationSet configurationSet) {
return new HiddenViewHolder(parent, configurationSet, true);
}
public void bindThreadHiddenView(RecyclerView.ViewHolder viewHolder, PostItem postItem) {
HiddenViewHolder holder = (HiddenViewHolder) viewHolder;
holder.postItem = postItem;
String description = postItem.getHideReason();
if (description == null) {
description = postItem.getSubjectOrComment();
}
holder.comment.setText(description);
}
public RecyclerView.ViewHolder createPostView(ViewGroup parent,
UiManager.ConfigurationSet configurationSet) {
return new PostViewHolder(parent, configurationSet, uiManager, thumbnailWidth);
}
public void bindPostView(RecyclerView.ViewHolder viewHolder, PostItem postItem, UiManager.DemandSet demandSet) {
ColorScheme colorScheme = ThemeEngine.getColorScheme(uiManager.getContext());
PostViewHolder holder = (PostViewHolder) viewHolder;
holder.notifyUnbind();
holder.postItem = postItem;
String chanName = postItem.getChanName();
String boardName = postItem.getBoardName();
String threadNumber = postItem.getThreadNumber();
String postNumber = postItem.getPostNumber();
boolean bumpLimitReached = false;
PostItem.BumpLimitState bumpLimitReachedState = postItem.getBumpLimitReachedState(0);
if (bumpLimitReachedState == PostItem.BumpLimitState.REACHED) {
bumpLimitReached = true;
} else if (bumpLimitReachedState == PostItem.BumpLimitState.NEED_COUNT &&
holder.configurationSet.postsProvider != null) {
int postsCount = 0;
for (PostItem itPostItem : holder.configurationSet.postsProvider) {
if (!itPostItem.isDeleted()) {
postsCount++;
}
}
bumpLimitReached = postItem.getBumpLimitReachedState(postsCount) == PostItem.BumpLimitState.REACHED;
}
holder.number.setText("#" + postNumber);
holder.states[0] = postItem.isUserPost() && Preferences.isShowMyPosts();
holder.states[1] = postItem.isOriginalPoster();
holder.states[2] = postItem.isSage() || bumpLimitReached;
holder.states[3] = !StringUtils.isEmpty(postItem.getEmail());
holder.states[4] = postItem.isSticky();
holder.states[5] = postItem.isClosed();
holder.states[6] = postItem.isCyclical();
holder.states[7] = postItem.isPosterWarned();
holder.states[8] = postItem.isPosterBanned();
for (int i = 0; i < holder.stateImages.length; i++) {
holder.stateImages[i].setVisibility(holder.states[i] ? View.VISIBLE : View.GONE);
}
viewHolder.itemView.setAlpha(postItem.isDeleted() ? ALPHA_DELETED_POST : 1f);
CharSequence name = postItem.getFullName();
colorScheme.apply(postItem.getFullNameSpans());
holder.name.setText(makeHighlightedText(demandSet.highlightText, name));
holder.date.setText(postItem.getDateTime(postDateFormatter));
String subject = postItem.getSubject();
CharSequence comment = !StringUtils.isEmpty(holder.configurationSet.repliesToPost)
? postItem.getComment(holder.configurationSet.repliesToPost) : postItem.getComment();
colorScheme.apply(postItem.getCommentSpans());
LinkSuffixSpan[] linkSuffixSpans = postItem.getLinkSuffixSpansAfterComment();
if (linkSuffixSpans != null && holder.configurationSet.userPostNumbers != null) {
boolean showMyPosts = Preferences.isShowMyPosts();
for (LinkSuffixSpan span : linkSuffixSpans) {
span.setSuffix(LinkSuffixSpan.SUFFIX_USER_POST, showMyPosts &&
holder.configurationSet.userPostNumbers.contains(span.getPostNumber()));
}
}
LinkSpan[] linkSpans = postItem.getLinkSpansAfterComment();
if (linkSpans != null) {
HashSet<String> referencesTo = postItem.getReferencesTo();
for (LinkSpan linkSpan : linkSpans) {
String linkPostNumber = linkSpan.getPostNumber();
if (linkPostNumber != null) {
boolean hidden = false;
if (referencesTo != null && referencesTo.contains(linkPostNumber)
&& holder.configurationSet.postsProvider != null) {
PostItem linkPostItem = holder.configurationSet.postsProvider.findPostItem(linkPostNumber);
hidden = linkPostItem != null && linkPostItem.isHidden(holder.configurationSet.hidePerformer);
}
linkSpan.setHidden(hidden);
}
}
}
holder.comment.setSpoilersEnabled(!Preferences.isShowSpoilers());
holder.comment.setSubjectAndComment(makeHighlightedText(demandSet.highlightText, subject),
makeHighlightedText(demandSet.highlightText, comment));
holder.comment.setReplyable(holder.configurationSet.replyable, postNumber);
holder.comment.setLinkListener(holder.configurationSet.linkListener != null
? holder.configurationSet.linkListener : defaultLinkListener, chanName, boardName, threadNumber);
holder.comment.setVisibility(subject.length() > 0 || comment.length() > 0 ? View.VISIBLE : View.GONE);
holder.comment.bindSelectionPaddingView(demandSet.lastInList ? holder.textSelectionPadding : null);
handlePostViewIcons(holder);
handlePostViewAttachments(holder);
holder.index.setText(postItem.getOrdinalIndexString());
boolean showName = holder.thumbnail.getVisibility() == View.VISIBLE ||
!postItem.isUseDefaultName() && !StringUtils.isEmpty(name);
holder.name.setVisibility(showName ? View.VISIBLE : View.GONE);
boolean showIndex = postItem.getOrdinalIndex() != PostItem.ORDINAL_INDEX_NONE;
holder.index.setVisibility(showIndex ? View.VISIBLE : View.GONE);
if (demandSet.selection == UiManager.Selection.THREADSHOT) {
holder.bottomBarReplies.setVisibility(View.GONE);
holder.bottomBarExpand.setVisibility(View.GONE);
holder.bottomBarOpenThread.setVisibility(View.GONE);
} else {
int replyCount = postItem.getPostReplyCount();
if (postItem.getPostReplyCount() > 0) {
holder.bottomBarReplies.setText(holder.itemView.getResources().getQuantityString
(R.plurals.number_replies__format, replyCount, replyCount));
holder.bottomBarReplies.setVisibility(View.VISIBLE);
} else {
holder.bottomBarReplies.setVisibility(View.GONE);
}
holder.bottomBarExpand.setVisibility(View.GONE);
holder.bottomBarOpenThread.setVisibility(demandSet.showOpenThreadButton ? View.VISIBLE : View.GONE);
}
if (holder.configurationSet.mayCollapse && commentMaxLines > 0 && !postItem.isExpanded()) {
holder.comment.setLinesLimit(commentMaxLines, commentAdditionalHeight);
} else {
holder.comment.setLinesLimit(0, 0);
}
holder.bottomBarExpand.setVisibility(View.GONE);
holder.invalidateBottomBar();
boolean viewsEnabled = demandSet.selection == UiManager.Selection.DISABLED;
holder.thumbnail.setEnabled(viewsEnabled);
holder.comment.setEnabled(viewsEnabled);
holder.head.setEnabled(viewsEnabled);
holder.bottomBarReplies.setEnabled(viewsEnabled);
holder.bottomBarReplies.setClickable(viewsEnabled);
holder.bottomBarExpand.setEnabled(viewsEnabled);
holder.bottomBarExpand.setClickable(viewsEnabled);
holder.bottomBarOpenThread.setEnabled(viewsEnabled);
holder.bottomBarOpenThread.setClickable(viewsEnabled);
if (viewsEnabled && postItem.isUnread()) {
switch (Preferences.getHighlightUnreadMode()) {
case Preferences.HIGHLIGHT_UNREAD_AUTOMATICALLY: {
holder.newPostAnimation = new NewPostAnimation(holder.layout, postItem,
colorScheme.highlightBackgroundColor);
break;
}
case Preferences.HIGHLIGHT_UNREAD_MANUALLY: {
holder.layout.setSecondaryBackgroundColor(colorScheme.highlightBackgroundColor);
break;
}
default: {
holder.layout.setSecondaryBackground(null);
break;
}
}
} else if (demandSet.selection == UiManager.Selection.SELECTED) {
holder.layout.setSecondaryBackgroundColor(colorScheme.highlightBackgroundColor);
} else {
holder.layout.setSecondaryBackground(null);
}
}
public void bindPostViewInvalidateComment(RecyclerView.ViewHolder viewHolder) {
PostViewHolder holder = (PostViewHolder) viewHolder;
holder.comment.invalidateAllSpans();
}
public RecyclerView.ViewHolder createPostHiddenView(ViewGroup parent,
UiManager.ConfigurationSet configurationSet) {
return new HiddenViewHolder(parent, configurationSet, false);
}
public void bindPostHiddenView(RecyclerView.ViewHolder viewHolder, PostItem postItem) {
HiddenViewHolder holder = (HiddenViewHolder) viewHolder;
holder.postItem = postItem;
holder.index.setText(postItem.getOrdinalIndexString());
holder.number.setText("#" + postItem.getPostNumber());
String description = postItem.getHideReason();
if (description == null) {
description = postItem.getSubjectOrComment();
}
holder.comment.setText(description);
postItem.setUnread(false);
}
private static int getPostBackgroundColor(UiManager uiManager, UiManager.ConfigurationSet configurationSet) {
ColorScheme colorScheme = ThemeEngine.getColorScheme(uiManager.getContext());
return configurationSet.isDialog ? colorScheme.dialogBackgroundColor : colorScheme.windowBackgroundColor;
}
@SuppressLint("InflateParams")
private void handlePostViewAttachments(PostViewHolder holder) {
Context context = uiManager.getContext();
PostItem postItem = holder.postItem;
List<AttachmentItem> attachmentItems = postItem.getAttachmentItems();
if (attachmentItems != null && !attachmentItems.isEmpty()) {
int size = attachmentItems.size();
if (size >= 2 && Preferences.isAllAttachments()) {
holder.thumbnail.resetImage(null);
holder.thumbnail.setVisibility(View.GONE);
holder.attachmentInfo.setVisibility(View.GONE);
ArrayList<AttachmentHolder> attachmentHolders = holder.attachmentHolders;
if (attachmentHolders == null) {
attachmentHolders = new ArrayList<>();
holder.attachmentHolders = attachmentHolders;
}
int holders = attachmentHolders.size();
if (holders < size) {
int postBackgroundColor = getPostBackgroundColor(uiManager, holder.configurationSet);
int thumbnailsScale = Preferences.getThumbnailsScale();
int textScale = Preferences.getTextScale();
for (int i = holders; i < size; i++) {
View view = LayoutInflater.from(context).inflate(R.layout.list_item_post_attachment, null);
AttachmentHolder attachmentHolder = new AttachmentHolder();
attachmentHolder.container = view;
attachmentHolder.thumbnail = view.findViewById(R.id.thumbnail);
attachmentHolder.attachmentInfo = view.findViewById(R.id.attachment_info);
attachmentHolder.thumbnail.setDrawTouching(true);
attachmentHolder.thumbnail.applyRoundedCorners(postBackgroundColor);
attachmentHolder.thumbnail.setOnClickListener(attachmentHolder.thumbnailClickListener);
attachmentHolder.thumbnail.setOnLongClickListener(attachmentHolder.thumbnailLongClickListener);
attachmentHolder.attachmentInfo.getLayoutParams().width = multipleAttachmentInfoWidth;
ViewGroup.LayoutParams thumbnailLayoutParams = attachmentHolder.thumbnail.getLayoutParams();
if (thumbnailsScale != 100) {
thumbnailLayoutParams.width = thumbnailWidth * thumbnailsScale / 100;
thumbnailLayoutParams.height = thumbnailLayoutParams.width;
} else {
thumbnailLayoutParams.width = thumbnailWidth;
thumbnailLayoutParams.height = thumbnailWidth;
}
if (textScale != 100) {
ViewUtils.applyScaleSize(attachmentHolder.attachmentInfo);
}
attachmentHolders.add(attachmentHolder);
holder.attachments.addView(view);
}
}
boolean sfwMode = Preferences.isSfwMode();
for (int i = 0; i < size; i++) {
AttachmentHolder attachmentHolder = attachmentHolders.get(i);
AttachmentItem attachmentItem = attachmentItems.get(i);
attachmentItem.configureAndLoad(attachmentHolder.thumbnail, false, false);
attachmentHolder.thumbnailClickListener.update(i, false, holder.configurationSet.isDialog
? GalleryOverlay.NavigatePostMode.MANUALLY : GalleryOverlay.NavigatePostMode.ENABLED);
attachmentHolder.thumbnailLongClickListener.update(attachmentItem);
attachmentHolder.thumbnail.setSfwMode(sfwMode);
attachmentHolder.attachmentInfo.setText(attachmentItem.getDescription(AttachmentItem.FormatMode
.THREE_LINES));
attachmentHolder.container.setVisibility(View.VISIBLE);
}
for (int i = size; i < holders; i++) {
AttachmentHolder attachmentHolder = attachmentHolders.get(i);
ImageLoader.getInstance().cancel(attachmentHolder.thumbnail);
attachmentHolder.thumbnail.resetImage(null);
attachmentHolder.container.setVisibility(View.GONE);
}
holder.attachments.setVisibility(View.VISIBLE);
holder.attachmentViewCount = size;
} else {
AttachmentItem attachmentItem = attachmentItems.get(0);
attachmentItem.configureAndLoad(holder.thumbnail, size > 1, false);
holder.thumbnailClickListener.update(0, true, holder.configurationSet.isDialog
? GalleryOverlay.NavigatePostMode.MANUALLY : GalleryOverlay.NavigatePostMode.ENABLED);
holder.thumbnailLongClickListener.update(attachmentItem);
holder.thumbnail.setSfwMode(Preferences.isSfwMode());
holder.thumbnail.setVisibility(View.VISIBLE);
holder.attachmentInfo.setText(postItem.getAttachmentsDescription(context,
AttachmentItem.FormatMode.LONG));
holder.attachmentInfo.setVisibility(View.VISIBLE);
holder.attachments.setVisibility(View.GONE);
holder.attachmentViewCount = 1;
}
} else {
ImageLoader.getInstance().cancel(holder.thumbnail);
holder.thumbnail.resetImage(null);
holder.thumbnail.setVisibility(View.GONE);
holder.attachmentInfo.setVisibility(View.GONE);
holder.attachments.setVisibility(View.GONE);
holder.attachmentViewCount = 1;
}
}
private void handlePostViewIcons(PostViewHolder holder) {
Context context = uiManager.getContext();
PostItem postItem = holder.postItem;
String chanName = postItem.getChanName();
ArrayList<Pair<Uri, String>> icons = postItem.getIcons();
if (icons != null && Preferences.isDisplayIcons()) {
if (holder.badgeImages == null) {
holder.badgeImages = new ArrayList<>();
}
int count = holder.badgeImages.size();
int add = icons.size() - count;
// Create more image views for icons
if (add > 0) {
View anchorView = count > 0 ? holder.badgeImages.get(count - 1) : holder.index;
int anchorIndex = holder.head.indexOfChild(anchorView) + 1;
float density = ResourceUtils.obtainDensity(context);
int size = (int) (12f * density);
for (int i = 0; i < add; i++) {
ImageView imageView = new ImageView(context);
holder.head.addView(imageView, anchorIndex + i, new ViewGroup.LayoutParams(size, size));
ViewUtils.applyScaleSize(imageView);
holder.badgeImages.add(imageView);
}
}
ChanLocator locator = ChanLocator.get(chanName);
for (int i = 0; i < holder.badgeImages.size(); i++) {
ImageView imageView = holder.badgeImages.get(i);
if (i < icons.size()) {
imageView.setVisibility(View.VISIBLE);
Uri uri = icons.get(i).first;
if (uri != null) {
uri = uri.isRelative() ? locator.convert(uri) : uri;
ImageLoader.getInstance().loadImage(chanName, uri, false, imageView);
} else {
ImageLoader.getInstance().cancel(imageView);
imageView.setTag(null);
imageView.setImageDrawable(null);
}
} else {
ImageLoader.getInstance().cancel(imageView);
imageView.setVisibility(View.GONE);
}
}
} else if (holder.badgeImages != null) {
for (ImageView imageView : holder.badgeImages) {
imageView.setTag(null);
imageView.setVisibility(View.GONE);
}
}
}
private CharSequence makeHighlightedText(Collection<String> highlightText, CharSequence text) {
if (!highlightText.isEmpty() && text != null) {
Locale locale = Locale.getDefault();
SpannableString spannable = new SpannableString(text);
String searchable = text.toString().toLowerCase(locale);
ColorScheme colorScheme = ThemeEngine.getColorScheme(uiManager.getContext());
for (String highlight : highlightText) {
highlight = highlight.toLowerCase(locale);
int textIndex = -1;
while ((textIndex = searchable.indexOf(highlight, textIndex + 1)) >= 0) {
spannable.setSpan(new BackgroundColorSpan(colorScheme.highlightTextColor),
textIndex, textIndex + highlight.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
}
text = spannable;
}
}
return text;
}
public void notifyUnbindListView(RecyclerView recyclerView) {
for (int i = 0; i < recyclerView.getChildCount(); i++) {
RecyclerView.ViewHolder holder = recyclerView.getChildViewHolder(recyclerView.getChildAt(i));
notifyUnbindView(holder);
}
}
public void notifyUnbindView(RecyclerView.ViewHolder holder) {
if (holder instanceof PostViewHolder) {
((PostViewHolder) holder).notifyUnbind();
}
}
public int findImageIndex(ArrayList<GalleryItem> galleryItems, PostItem postItem) {
if (galleryItems != null && postItem.hasAttachments()) {
String postNumber = postItem.getPostNumber();
for (int i = 0; i < galleryItems.size(); i++) {
if (postNumber.equals(galleryItems.get(i).postNumber)) {
return i;
}
}
}
return -1;
}
boolean handlePostForDoubleClick(final View view) {
final PostViewHolder holder = ListViewUtils.getViewHolder(view, PostViewHolder.class);
if (holder != null) {
if (holder.comment.getVisibility() != View.VISIBLE || holder.comment.isSelectionMode()) {
return false;
}
long t = SystemClock.elapsedRealtime();
long timeout = holder.comment.getPreferredDoubleTapTimeout();
if (t - holder.lastCommentClick > timeout) {
holder.lastCommentClick = t;
} else {
final RecyclerView recyclerView = (RecyclerView) view.getParent();
final int position = recyclerView.getChildAdapterPosition(view);
holder.comment.startSelection();
int padding = holder.comment.getSelectionPadding();
if (padding > 0) {
final int listHeight = recyclerView.getHeight() - recyclerView.getPaddingTop() -
recyclerView.getPaddingBottom();
recyclerView.post(() -> {
int end = holder.comment.getSelectionEnd();
if (end >= 0) {
Layout layout = holder.comment.getLayout();
int line = layout.getLineForOffset(end);
int count = layout.getLineCount();
if (count - line <= 4) {
((LinearLayoutManager) recyclerView.getLayoutManager())
.scrollToPositionWithOffset(position, listHeight - view.getHeight());
}
}
});
}
}
return true;
} else {
return false;
}
}
private final View.OnClickListener repliesBlockClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
PostViewHolder holder = ListViewUtils.getViewHolder(v, PostViewHolder.class);
PostItem postItem = holder.postItem;
uiManager.dialog().displayReplies(holder.configurationSet, postItem);
}
};
private final View.OnClickListener threadLinkBlockClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
PostViewHolder holder = ListViewUtils.getViewHolder(v, PostViewHolder.class);
PostItem postItem = holder.postItem;
String postNumber = postItem.getParentPostNumber() == null ? null : postItem.getPostNumber();
uiManager.navigator().navigatePosts(postItem.getChanName(), postItem.getBoardName(),
postItem.getThreadNumber(), postNumber, null, 0);
}
};
private final View.OnClickListener threadShowOriginalPostClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
ThreadViewHolder holder = ListViewUtils.getViewHolder(v, ThreadViewHolder.class);
uiManager.dialog().displayThread(holder.getConfigurationSet(), holder.getPostItem());
}
};
private final View.OnTouchListener headContentTouchListener = new View.OnTouchListener() {
private static final int TYPE_NONE = 0;
private static final int TYPE_BADGES = 1;
private static final int TYPE_STATES = 2;
private int type;
private float startX, startY;
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
type = TYPE_NONE;
float x = event.getX();
float y = event.getY();
PostViewHolder holder = ListViewUtils.getViewHolder(v, PostViewHolder.class);
LinebreakLayout head = holder.head;
for (int i = 0; i < head.getChildCount(); i++) {
View child = head.getChildAt(i);
if (child.getVisibility() == View.VISIBLE) {
int width = child.getWidth();
int height = child.getHeight();
int radius = (int) (Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2)) / 2f);
int centerX = child.getLeft() + child.getWidth() / 2;
int centerY = child.getTop() + child.getHeight() / 2;
int distance = (int) (Math.sqrt(Math.pow(centerX - x, 2) + Math.pow(centerY - y, 2)) / 2f);
if (distance <= radius * 3 / 2) {
startX = x;
startY = y;
// noinspection SuspiciousMethodCalls
if (holder.badgeImages != null && holder.badgeImages.contains(child)) {
type = TYPE_BADGES;
return true;
}
// noinspection SuspiciousMethodCalls
if (Arrays.asList(holder.stateImages).contains(child)) {
type = TYPE_STATES;
return true;
}
}
}
}
break;
}
case MotionEvent.ACTION_UP: {
if (type != TYPE_NONE) {
Context context = uiManager.getContext();
PostViewHolder holder = ListViewUtils.getViewHolder(v, PostViewHolder.class);
int touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
if (Math.abs(event.getX() - startX) <= touchSlop &&
Math.abs(event.getY() - startY) <= touchSlop) {
ArrayList<DialogUnit.IconData> icons = new ArrayList<>();
String emailToCopy = null;
switch (type) {
case TYPE_BADGES: {
ArrayList<Pair<Uri, String>> postIcons = holder.postItem.getIcons();
ChanLocator locator = ChanLocator.get(holder.postItem.getChanName());
for (Pair<Uri, String> postIcon : postIcons) {
Uri uri = postIcon.first;
if (uri != null) {
uri = uri.isRelative() ? locator.convert(uri) : uri;
}
icons.add(new DialogUnit.IconData(postIcon.second, uri));
}
break;
}
case TYPE_STATES: {
for (int i = 0; i < holder.states.length; i++) {
if (holder.states[i]) {
int attr = STATE_ATTRS[i];
String title;
if (attr == R.attr.iconPostEmail) {
title = holder.postItem.getEmail();
if (title.startsWith("mailto:")) {
title = title.substring(7);
}
emailToCopy = title;
} else {
title = context.getString(STATE_TEXTS.get(attr));
}
icons.add(new DialogUnit.IconData(title, attr));
}
}
break;
}
}
uiManager.dialog().showPostDescriptionDialog(icons,
holder.postItem.getChanName(), emailToCopy);
}
return true;
}
break;
}
}
return false;
}
};
private static final int[] STATE_ATTRS = {R.attr.iconPostUserPost, R.attr.iconPostOriginalPoster,
R.attr.iconPostSage, R.attr.iconPostEmail, R.attr.iconPostSticky, R.attr.iconPostClosed,
R.attr.iconPostCyclical, R.attr.iconPostWarned, R.attr.iconPostBanned};
private static final SparseIntArray STATE_TEXTS = new SparseIntArray();
static {
STATE_TEXTS.put(R.attr.iconPostUserPost, R.string.my_post);
STATE_TEXTS.put(R.attr.iconPostOriginalPoster, R.string.original_poster);
STATE_TEXTS.put(R.attr.iconPostSage, R.string.doesnt_bring_up_thread);
STATE_TEXTS.put(R.attr.iconPostSticky, R.string.sticky_thread);
STATE_TEXTS.put(R.attr.iconPostClosed, R.string.thread_is_closed);
STATE_TEXTS.put(R.attr.iconPostCyclical, R.string.cyclical_thread);
STATE_TEXTS.put(R.attr.iconPostWarned, R.string.user_is_warned);
STATE_TEXTS.put(R.attr.iconPostBanned, R.string.user_is_banned);
}
private class AttachmentHolder {
public AttachmentView thumbnail;
public final UiManager.ThumbnailClickListener thumbnailClickListener;
public final UiManager.ThumbnailLongClickListener thumbnailLongClickListener;
public View container;
public TextView attachmentInfo;
public AttachmentHolder() {
thumbnailClickListener = uiManager.interaction().createThumbnailClickListener();
thumbnailLongClickListener = uiManager.interaction().createThumbnailLongClickListener();
}
}
private static CardView createCardLayout(ViewGroup parent) {
ThemeEngine.Theme theme = ThemeEngine.getTheme(parent.getContext());
CardView cardView = new CardView(parent.getContext());
cardView.setBackgroundColor(theme.card);
FrameLayout content = new FrameLayout(cardView.getContext());
ViewUtils.setSelectableItemBackground(content);
cardView.addView(content, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
cardView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
return cardView;
}
private static class ThreadViewHolder extends RecyclerView.ViewHolder implements UiManager.Holder {
public final AttachmentView thumbnail;
public final TextView subject;
public final TextView comment;
public final TextView description;
public final ImageView stateSage;
public final ImageView stateSticky;
public final ImageView stateClosed;
public final View threadContent;
public final View showOriginalPost;
public final UiManager.ConfigurationSet configurationSet;
public final UiManager.ThumbnailClickListener thumbnailClickListener;
public final UiManager.ThumbnailLongClickListener thumbnailLongClickListener;
public PostItem postItem;
public ThreadViewHolder(ViewGroup parent, UiManager.ConfigurationSet configurationSet,
UiManager uiManager, boolean cell) {
super(createCardLayout(parent));
CardView cardView = (CardView) itemView;
ViewGroup cardContent = (ViewGroup) cardView.getChildAt(0);
LayoutInflater inflater = LayoutInflater.from(itemView.getContext());
inflater.inflate(cell ? R.layout.list_item_thread_grid : R.layout.list_item_thread, cardContent);
thumbnail = itemView.findViewById(R.id.thumbnail);
subject = itemView.findViewById(R.id.subject);
comment = itemView.findViewById(R.id.comment);
description = itemView.findViewById(R.id.thread_description);
stateSage = itemView.findViewById(R.id.state_sage);
stateSticky = itemView.findViewById(R.id.state_sticky);
stateClosed = itemView.findViewById(R.id.state_closed);
threadContent = itemView.findViewById(R.id.thread_content);
showOriginalPost = itemView.findViewById(R.id.show_original_post);
(cell ? description : showOriginalPost).setOnClickListener(uiManager.view()
.threadShowOriginalPostClickListener);
this.configurationSet = configurationSet;
thumbnailClickListener = uiManager.interaction().createThumbnailClickListener();
thumbnailLongClickListener = uiManager.interaction().createThumbnailLongClickListener();
thumbnail.setDrawTouching(true);
if (cell) {
thumbnail.setFitSquare(true);
thumbnail.applyRoundedCorners(cardView.getBackgroundColor());
ViewUtils.applyScaleSize(comment, subject, description);
} else {
if (C.API_LOLLIPOP) {
ColorStateList colors = ColorStateList.valueOf(ThemeEngine
.getTheme(itemView.getContext()).meta);
for (ImageView imageView : Arrays.asList(stateSage, stateSticky, stateClosed)) {
imageView.setImageTintList(colors);
}
}
thumbnail.applyRoundedCorners(cardView.getBackgroundColor());
ViewGroup.MarginLayoutParams thumbnailLayoutParams =
(ViewGroup.MarginLayoutParams) thumbnail.getLayoutParams();
ViewUtils.applyScaleSize(comment, subject, description,
stateSage, stateSticky, stateClosed);
if (ResourceUtils.isTablet(itemView.getResources().getConfiguration())) {
float density = ResourceUtils.obtainDensity(itemView);
description.setGravity(Gravity.START);
int thumbnailSize = (int) (72f * density);
thumbnailLayoutParams.width = thumbnailSize;
thumbnailLayoutParams.height = thumbnailSize;
int descriptionPaddingLeft = thumbnailSize + thumbnailLayoutParams.leftMargin +
thumbnailLayoutParams.rightMargin;
description.setPadding(descriptionPaddingLeft, description.getPaddingTop(),
description.getPaddingRight(), description.getPaddingBottom());
comment.setMaxLines(8);
} else {
description.setGravity(Gravity.END);
comment.setMaxLines(6);
}
int thumbnailsScale = Preferences.getThumbnailsScale();
thumbnailLayoutParams.width = thumbnailLayoutParams.width * thumbnailsScale / 100;
thumbnailLayoutParams.height = thumbnailLayoutParams.height * thumbnailsScale / 100;
}
}
@Override
public PostItem getPostItem() {
return postItem;
}
@Override
public UiManager.ConfigurationSet getConfigurationSet() {
return configurationSet;
}
@Override
public GalleryItem.GallerySet getGallerySet() {
return postItem.getThreadGallerySet();
}
}
private static class NewPostAnimation implements Runnable, ValueAnimator.AnimatorUpdateListener {
private final PostLinearLayout layout;
private final PostItem postItem;
private final ColorDrawable drawable;
private ValueAnimator animator;
private boolean applied = false;
public NewPostAnimation(PostLinearLayout layout, PostItem postItem, int color) {
this.layout = layout;
this.postItem = postItem;
drawable = new ColorDrawable(color);
layout.setSecondaryBackground(drawable);
layout.postDelayed(this, 500);
}
@Override
public void run() {
int color = drawable.getColor();
animator = ValueAnimator.ofObject(new ArgbEvaluator(), color, color & 0x00ffffff);
animator.addUpdateListener(this);
animator.setDuration(500);
animator.start();
}
@Override
public void onAnimationUpdate(ValueAnimator animation) {
if (!applied) {
applied = true;
postItem.setUnread(false);
}
drawable.setColor((int) animation.getAnimatedValue());
}
public void cancel() {
layout.removeCallbacks(this);
if (animator != null) {
animator.cancel();
animator = null;
}
}
}
private static class PostViewHolder extends RecyclerView.ViewHolder implements UiManager.Holder,
CommentTextView.RecyclerKeeper.Holder, PostLinearLayout.OnTemporaryDetachListener,
CommentTextView.LimitListener, View.OnClickListener {
public final PostLinearLayout layout;
public final LinebreakLayout head;
public final TextView number;
public final TextView name;
public final TextView index;
public final TextView date;
public final ViewGroup attachments;
public final AttachmentView thumbnail;
public final TextView attachmentInfo;
public final CommentTextView comment;
public final View textSelectionPadding;
public final View textBarPadding;
public final View bottomBar;
public final TextView bottomBarReplies;
public final TextView bottomBarExpand;
public final TextView bottomBarOpenThread;
public ArrayList<AttachmentHolder> attachmentHolders;
public int attachmentViewCount = 1;
public ArrayList<ImageView> badgeImages;
public final ImageView[] stateImages = new ImageView[STATE_ATTRS.length];
public final boolean[] states = new boolean[STATE_ATTRS.length];
public final UiManager.ConfigurationSet configurationSet;
public final UiManager.ThumbnailClickListener thumbnailClickListener;
public final UiManager.ThumbnailLongClickListener thumbnailLongClickListener;
public PostItem postItem;
public Animator expandAnimator;
public NewPostAnimation newPostAnimation;
public long lastCommentClick;