-
-
Notifications
You must be signed in to change notification settings - Fork 508
Expand file tree
/
Copy pathRedditPreparedPost.java
More file actions
899 lines (758 loc) · 26.2 KB
/
RedditPreparedPost.java
File metadata and controls
899 lines (758 loc) · 26.2 KB
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
/*******************************************************************************
* This file is part of RedReader.
*
* RedReader 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.
*
* RedReader 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 RedReader. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package org.quantumbadger.redreader.reddit.prepared;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import org.quantumbadger.redreader.R;
import org.quantumbadger.redreader.account.RedditAccount;
import org.quantumbadger.redreader.account.RedditAccountManager;
import org.quantumbadger.redreader.activities.BaseActivity;
import org.quantumbadger.redreader.cache.CacheManager;
import org.quantumbadger.redreader.cache.CacheRequest;
import org.quantumbadger.redreader.cache.CacheRequestCallbacks;
import org.quantumbadger.redreader.cache.downloadstrategy.DownloadStrategyIfNotCached;
import org.quantumbadger.redreader.common.BetterSSB;
import org.quantumbadger.redreader.common.Constants;
import org.quantumbadger.redreader.common.General;
import org.quantumbadger.redreader.common.GenericFactory;
import org.quantumbadger.redreader.common.LinkHandler;
import org.quantumbadger.redreader.common.PrefsUtility;
import org.quantumbadger.redreader.common.Priority;
import org.quantumbadger.redreader.common.RRError;
import org.quantumbadger.redreader.common.ScreenreaderPronunciation;
import org.quantumbadger.redreader.common.UriString;
import org.quantumbadger.redreader.common.datastream.SeekableInputStream;
import org.quantumbadger.redreader.common.time.TimeFormatHelper;
import org.quantumbadger.redreader.common.time.TimestampUTC;
import org.quantumbadger.redreader.image.ThumbnailScaler;
import org.quantumbadger.redreader.reddit.api.RedditPostActions;
import org.quantumbadger.redreader.reddit.kthings.RedditIdAndType;
import org.quantumbadger.redreader.views.RedditPostView;
import java.io.IOException;
import java.util.EnumSet;
import java.util.Locale;
import java.util.UUID;
public final class RedditPreparedPost implements RedditChangeDataManager.Listener {
private static final String TAG = "RedditPreparedPost";
public final RedditParsedPost src;
private final RedditChangeDataManager mChangeDataManager;
public final boolean isArchived;
public final boolean isLocked;
public final boolean canModerate;
public final boolean hasThumbnail;
public final boolean mIsProbablyAnImage;
private final boolean mShowInlinePreviews;
// TODO make it possible to turn off in-memory caching when out of memory
private volatile Bitmap thumbnailCache = null;
private ThumbnailLoadedCallback thumbnailCallback;
private int usageId = -1;
public TimestampUTC lastChange;
public final boolean showSubreddit;
private RedditPostView mBoundView = null;
// TODO too many parameters
public RedditPreparedPost(
final Context context,
final CacheManager cm,
final int listId,
final RedditParsedPost post,
final TimestampUTC timestamp,
final boolean showSubreddit,
final boolean showThumbnails,
final boolean allowHighResThumbnails,
final boolean showInlinePreviews) {
this.src = post;
this.showSubreddit = showSubreddit;
mShowInlinePreviews = showInlinePreviews;
final RedditAccount user = RedditAccountManager.getInstance(context).getDefaultAccount();
mChangeDataManager = RedditChangeDataManager.getInstance(user);
isArchived = post.isArchived();
isLocked = post.isLocked();
canModerate = post.getCanModerate();
mIsProbablyAnImage = LinkHandler.isProbablyAnImage(post.getUrl());
hasThumbnail = showThumbnails && hasThumbnail(post);
final int thumbnailWidth = General.dpToPixels(
context,
PrefsUtility.images_thumbnail_size_dp());
if(hasThumbnail && hasThumbnail(post) && !shouldShowInlinePreview()) {
downloadThumbnail(context, allowHighResThumbnails, thumbnailWidth, cm, listId);
}
lastChange = timestamp;
mChangeDataManager.update(timestamp, post.getSrc());
}
public boolean shouldShowInlinePreview() {
return mShowInlinePreviews && (src.isPreviewEnabled()
|| "gfycat.com".equals(src.getDomain())
|| "i.imgur.com".equals(src.getDomain())
|| "streamable.com".equals(src.getDomain())
|| "i.redd.it".equals(src.getDomain())
|| "v.redd.it".equals(src.getDomain()));
}
public boolean isVideoPreview() {
return src.isVideoPreview();
}
public void performAction(final BaseActivity activity, final RedditPostActions.Action action) {
RedditPostActions.INSTANCE.onActionMenuItemSelected(this, activity, action);
}
public int computeScore() {
int score = src.getScoreExcludingOwnVote();
if(isUpvoted()) {
score++;
} else if(isDownvoted()) {
score--;
}
return score;
}
public SpannableStringBuilder buildSubtitle(
final Context context,
final boolean headerMode) {
final EnumSet<PrefsUtility.AppearancePostSubtitleItem> mPostSubtitleItems;
final int mPostAgeUnits;
if(headerMode
&& PrefsUtility.appearance_post_subtitle_items_use_different_settings()) {
mPostSubtitleItems = PrefsUtility.appearance_post_header_subtitle_items();
mPostAgeUnits = PrefsUtility.appearance_post_header_age_units();
} else {
mPostSubtitleItems = PrefsUtility.appearance_post_subtitle_items();
mPostAgeUnits = PrefsUtility.appearance_post_age_units();
}
final TypedArray appearance = context.obtainStyledAttributes(new int[] {
R.attr.rrPostSubtitleBoldCol,
R.attr.rrPostSubtitleUpvoteCol,
R.attr.rrPostSubtitleDownvoteCol,
R.attr.rrFlairBackCol,
R.attr.rrFlairTextCol,
R.attr.rrGoldTextCol,
R.attr.rrGoldBackCol,
R.attr.rrLockedpostTextCol,
R.attr.rrLockedpostBackCol
});
final int boldCol;
if(headerMode) {
boldCol = Color.WHITE;
} else {
boldCol = appearance.getColor(0, 255);
}
final int rrPostSubtitleUpvoteCol = appearance.getColor(1, 255);
final int rrPostSubtitleDownvoteCol = appearance.getColor(2, 255);
final int rrFlairBackCol = appearance.getColor(3, 255);
final int rrFlairTextCol = appearance.getColor(4, 255);
final int rrGoldTextCol = appearance.getColor(5, 255);
final int rrGoldBackCol = appearance.getColor(6, 255);
final int rrLockedpostTextCol = appearance.getColor(7, 255);
final int rrLockedpostBackCol = appearance.getColor(8, 255);
appearance.recycle();
final BetterSSB postListDescSb = new BetterSSB();
final int pointsCol;
final int score = computeScore();
if(isUpvoted()) {
pointsCol = rrPostSubtitleUpvoteCol;
} else if(isDownvoted()) {
pointsCol = rrPostSubtitleDownvoteCol;
} else {
pointsCol = boldCol;
}
if(mPostSubtitleItems.contains(PrefsUtility.AppearancePostSubtitleItem.SPOILER)) {
if(src.isSpoiler()) {
postListDescSb.append(
" SPOILER ",
BetterSSB.BOLD
| BetterSSB.FOREGROUND_COLOR
| BetterSSB.BACKGROUND_COLOR,
Color.WHITE,
Color.rgb(50, 50, 50),
1f);
postListDescSb.append(" ", 0);
}
}
if(mPostSubtitleItems.contains(PrefsUtility.AppearancePostSubtitleItem.STICKY)) {
if(src.isStickied()) {
postListDescSb.append(
" STICKY ",
BetterSSB.BOLD
| BetterSSB.FOREGROUND_COLOR
| BetterSSB.BACKGROUND_COLOR,
Color.WHITE,
Color.rgb(0, 170, 0),
1f); // TODO color?
postListDescSb.append(" ", 0);
}
}
if(mPostSubtitleItems.contains(PrefsUtility.AppearancePostSubtitleItem.NSFW)) {
if(src.isNsfw()) {
postListDescSb.append(
" NSFW ",
BetterSSB.BOLD
| BetterSSB.FOREGROUND_COLOR
| BetterSSB.BACKGROUND_COLOR,
Color.WHITE,
Color.RED,
1f); // TODO color?
postListDescSb.append(" ", 0);
}
}
if(mPostSubtitleItems.contains(PrefsUtility.AppearancePostSubtitleItem.LOCKED)) {
if(src.isLocked()) {
postListDescSb.append(
" "
+ context.getString(R.string.locked)
+ " ",
BetterSSB.BOLD
| BetterSSB.FOREGROUND_COLOR
| BetterSSB.BACKGROUND_COLOR,
rrLockedpostTextCol,
rrLockedpostBackCol,
1f);
postListDescSb.append(" ", 0);
}
}
if(mPostSubtitleItems.contains(PrefsUtility.AppearancePostSubtitleItem.FLAIR)) {
if(src.getFlairText() != null) {
postListDescSb.append(
" "
+ src.getFlairText()
+ General.LTR_OVERRIDE_MARK
+ " ",
BetterSSB.BOLD
| BetterSSB.FOREGROUND_COLOR
| BetterSSB.BACKGROUND_COLOR,
rrFlairTextCol,
rrFlairBackCol,
1f);
postListDescSb.append(" ", 0);
}
}
if(mPostSubtitleItems.contains(PrefsUtility.AppearancePostSubtitleItem.COMMENTS)) {
postListDescSb.append(
String.valueOf(src.getCommentCount()),
BetterSSB.BOLD | BetterSSB.FOREGROUND_COLOR,
boldCol,
0,
1f);
postListDescSb.append(
BetterSSB.NBSP + context.getString(R.string.subtitle_comments) + " ",
0);
}
if(mPostSubtitleItems.contains(PrefsUtility.AppearancePostSubtitleItem.SCORE)) {
postListDescSb.append(
String.valueOf(score),
BetterSSB.BOLD | BetterSSB.FOREGROUND_COLOR,
pointsCol,
0,
1f);
postListDescSb.append(
BetterSSB.NBSP + context.getString(R.string.subtitle_points) + " ",
0);
}
if(mPostSubtitleItems.contains(PrefsUtility.AppearancePostSubtitleItem.UPVOTE_RATIO)) {
postListDescSb.append("(", 0);
postListDescSb.append(
src.getUpvotePercentage() + "%",
BetterSSB.BOLD | BetterSSB.FOREGROUND_COLOR,
boldCol,
0,
1f);
postListDescSb.append(
BetterSSB.NBSP + context.getString(R.string.subtitle_upvote_ratio) + ") ", 0);
}
if(mPostSubtitleItems.contains(PrefsUtility.AppearancePostSubtitleItem.GOLD)) {
if(src.getGoldAmount() > 0) {
if(mPostSubtitleItems.contains(PrefsUtility.AppearancePostSubtitleItem.SCORE)
|| mPostSubtitleItems.contains(
PrefsUtility.AppearancePostSubtitleItem.UPVOTE_RATIO)) {
postListDescSb.append(" ", 0);
}
postListDescSb.append(
" "
+ context.getString(R.string.gold)
+ BetterSSB.NBSP
+ "x"
+ src.getGoldAmount()
+ " ",
BetterSSB.FOREGROUND_COLOR | BetterSSB.BACKGROUND_COLOR,
rrGoldTextCol,
rrGoldBackCol,
1f);
postListDescSb.append(" ", 0);
}
}
if(mPostSubtitleItems.contains(PrefsUtility.AppearancePostSubtitleItem.AGE)) {
postListDescSb.append(
TimeFormatHelper.format(
src.getCreatedTimeUTC().elapsedPeriod(),
context,
R.string.time_ago,
mPostAgeUnits),
BetterSSB.BOLD | BetterSSB.FOREGROUND_COLOR,
boldCol,
0,
1f);
postListDescSb.append(" ", 0);
}
if(mPostSubtitleItems.contains(PrefsUtility.AppearancePostSubtitleItem.AUTHOR)) {
postListDescSb.append(context.getString(R.string.subtitle_by) + " ", 0);
final boolean setBackgroundColour;
final int backgroundColour; // TODO color from theme
if("moderator".equals(src.getDistinguished())) {
setBackgroundColour = true;
backgroundColour = Color.rgb(0, 170, 0);
} else if("admin".equals(src.getDistinguished())) {
setBackgroundColour = true;
backgroundColour = Color.rgb(170, 0, 0);
} else {
setBackgroundColour = false;
backgroundColour = 0;
}
if(setBackgroundColour) {
postListDescSb.append(
BetterSSB.NBSP + src.getAuthor() + BetterSSB.NBSP,
BetterSSB.BOLD
| BetterSSB.FOREGROUND_COLOR
| BetterSSB.BACKGROUND_COLOR,
Color.WHITE,
backgroundColour,
1f);
} else {
postListDescSb.append(
src.getAuthor(),
BetterSSB.BOLD | BetterSSB.FOREGROUND_COLOR,
boldCol,
0,
1f);
}
postListDescSb.append(" ", 0);
}
if(mPostSubtitleItems.contains(PrefsUtility.AppearancePostSubtitleItem.SUBREDDIT)) {
if(showSubreddit) {
postListDescSb.append(context.getString(R.string.subtitle_to) + " ", 0);
postListDescSb.append(
src.getSubreddit(),
BetterSSB.BOLD | BetterSSB.FOREGROUND_COLOR,
boldCol,
0,
1f);
postListDescSb.append(" ", 0);
}
}
if(mPostSubtitleItems.contains(PrefsUtility.AppearancePostSubtitleItem.DOMAIN)) {
postListDescSb.append("(" + src.getDomain() + ")", 0);
}
return postListDescSb.get();
}
public String buildAccessibilitySubtitle(
final Context context,
final boolean headerMode) {
final EnumSet<PrefsUtility.AppearancePostSubtitleItem> mPostSubtitleItems;
final int mPostAgeUnits;
if(headerMode
&& PrefsUtility.appearance_post_subtitle_items_use_different_settings()) {
mPostSubtitleItems = PrefsUtility.appearance_post_header_subtitle_items();
mPostAgeUnits = PrefsUtility.appearance_post_header_age_units();
} else {
mPostSubtitleItems = PrefsUtility.appearance_post_subtitle_items();
mPostAgeUnits = PrefsUtility.appearance_post_age_units();
}
final StringBuilder accessibilitySubtitle = new StringBuilder();
final int score = computeScore();
final String separator = " \n";
final boolean conciseMode = PrefsUtility.pref_accessibility_concise_mode();
// When not in concise mode, add embellishments to the subtitle for greater clarity and
// retention of familiar behaviour.
if (!conciseMode) {
accessibilitySubtitle.append(buildAccessibilityEmbellishments(context, headerMode));
}
if(mPostSubtitleItems.contains(PrefsUtility.AppearancePostSubtitleItem.COMMENTS)) {
accessibilitySubtitle
.append(context.getResources().getQuantityString(
R.plurals.accessibility_subtitle_comments_withperiod_plural,
src.getCommentCount(),
src.getCommentCount()))
.append(separator);
}
if(mPostSubtitleItems.contains(PrefsUtility.AppearancePostSubtitleItem.SCORE)) {
accessibilitySubtitle
.append(context.getResources().getQuantityString(
conciseMode
? R.plurals.
accessibility_subtitle_points_withperiod_concise_plural
: R.plurals.accessibility_subtitle_points_withperiod_plural,
score,
score))
.append(separator);
if(isUpvoted()) {
accessibilitySubtitle
.append(context.getString(
R.string.accessibility_subtitle_upvoted_withperiod))
.append(separator);
}
if(isDownvoted()) {
accessibilitySubtitle
.append(context.getString(
R.string.accessibility_subtitle_downvoted_withperiod))
.append(separator);
}
}
if(mPostSubtitleItems.contains(PrefsUtility.AppearancePostSubtitleItem.UPVOTE_RATIO)) {
accessibilitySubtitle
.append(context.getString(
conciseMode
?R.string.accessibility_subtitle_upvote_ratio_withperiod_concise
: R.string.accessibility_subtitle_upvote_ratio_withperiod,
src.getUpvotePercentage()))
.append(separator);
}
if(mPostSubtitleItems.contains(PrefsUtility.AppearancePostSubtitleItem.GOLD)) {
if(src.getGoldAmount() > 0) {
accessibilitySubtitle
.append(context.getString(
R.string.accessibility_subtitle_gold_withperiod,
src.getGoldAmount()))
.append(separator);
}
}
if(mPostSubtitleItems.contains(PrefsUtility.AppearancePostSubtitleItem.AGE)) {
accessibilitySubtitle
.append(context.getString(
R.string.accessibility_subtitle_age_withperiod,
TimeFormatHelper.format(
src.getCreatedTimeUTC().elapsedPeriod(),
context,
R.string.time_ago,
mPostAgeUnits)))
.append(separator);
}
if(mPostSubtitleItems.contains(PrefsUtility.AppearancePostSubtitleItem.SUBREDDIT)) {
if(showSubreddit) {
accessibilitySubtitle
.append(context.getString(
conciseMode
?(R.string.
accessibility_subtitle_subreddit_withperiod_concise
)
: R.string.accessibility_subtitle_subreddit_withperiod,
ScreenreaderPronunciation.getPronunciation(
context,
src.getSubreddit())))
.append(separator);
}
}
if(mPostSubtitleItems.contains(PrefsUtility.AppearancePostSubtitleItem.DOMAIN)) {
final String domain = src.getDomain().toLowerCase(Locale.US);
if(src.isSelfPost()) {
accessibilitySubtitle
.append(context.getString(
conciseMode
?R.string.accessibility_subtitle_selfpost_withperiod_concise
: R.string.accessibility_subtitle_selfpost_withperiod))
.append(separator);
} else {
accessibilitySubtitle
.append(context.getString(
conciseMode
?R.string.accessibility_subtitle_domain_withperiod_concise
: R.string.accessibility_subtitle_domain_withperiod,
ScreenreaderPronunciation.getPronunciation(context, domain)))
.append(separator);
if(src.hasSelfText()) {
accessibilitySubtitle
.append(context.getString(
conciseMode
?R.string.
accessibility_subtitle_selfpost_withperiod_concise
: R.string.
accessibility_subtitle_has_selftext_withperiod))
.append(separator);
}
}
}
if(mPostSubtitleItems.contains(PrefsUtility.AppearancePostSubtitleItem.AUTHOR)) {
@StringRes final int authorString;
if("moderator".equals(src.getDistinguished())) {
authorString = conciseMode
? R.string.accessibility_subtitle_author_moderator_withperiod_concise_post
: R.string.accessibility_subtitle_author_moderator_withperiod;
} else if("admin".equals(src.getDistinguished())) {
authorString = conciseMode
? R.string.accessibility_subtitle_author_admin_withperiod_concise_post
: R.string.accessibility_subtitle_author_admin_withperiod;
} else {
authorString = conciseMode
? R.string.accessibility_subtitle_author_withperiod_concise_post
: R.string.accessibility_subtitle_author_withperiod;
}
accessibilitySubtitle
.append(context.getString(
authorString,
ScreenreaderPronunciation.getPronunciation(
context,
src.getAuthor())))
.append(separator);
}
return accessibilitySubtitle.toString();
}
public String buildAccessibilityTitle(
final Context context,
final boolean headerMode) {
final StringBuilder a11yTitle = new StringBuilder();
// When in concise mode, add embellishments to the title for greater interruptability when
// navigating quickly.
if (PrefsUtility.pref_accessibility_concise_mode()) {
a11yTitle.append(buildAccessibilityEmbellishments(context, headerMode));
}
a11yTitle.append(src.getTitle());
// Append full stop so that subtitle doesn't become part of title
a11yTitle.append(".\n");
return a11yTitle.toString();
}
private String buildAccessibilityEmbellishments(
final Context context,
final boolean headerMode) {
final EnumSet<PrefsUtility.AppearancePostSubtitleItem> mPostSubtitleItems;
if(headerMode
&& PrefsUtility.appearance_post_subtitle_items_use_different_settings()) {
mPostSubtitleItems = PrefsUtility.appearance_post_header_subtitle_items();
} else {
mPostSubtitleItems = PrefsUtility.appearance_post_subtitle_items();
}
final StringBuilder a11yEmbellish = new StringBuilder();
final String separator = " \n";
final boolean conciseMode = PrefsUtility.pref_accessibility_concise_mode();
if (isRead()) {
a11yEmbellish
.append(
ScreenreaderPronunciation.getAccessibilityString(
context,
R.string.accessibility_post_already_read_withperiod
)
)
.append(separator);
}
if(mPostSubtitleItems.contains(PrefsUtility.AppearancePostSubtitleItem.SPOILER)) {
if(src.isSpoiler()) {
a11yEmbellish
.append(context.getString(
R.string.accessibility_subtitle_spoiler_withperiod))
.append(separator);
}
}
if(mPostSubtitleItems.contains(PrefsUtility.AppearancePostSubtitleItem.STICKY)) {
if(src.isStickied()) {
a11yEmbellish
.append(context.getString(
R.string.accessibility_subtitle_sticky_withperiod))
.append(separator);
}
}
if(mPostSubtitleItems.contains(PrefsUtility.AppearancePostSubtitleItem.NSFW)) {
if(src.isNsfw()) {
a11yEmbellish
.append(context.getString(
conciseMode
? R.string.accessibility_subtitle_nsfw_withperiod_concise
: R.string.accessibility_subtitle_nsfw_withperiod))
.append(separator);
}
}
if(mPostSubtitleItems.contains(PrefsUtility.AppearancePostSubtitleItem.LOCKED)) {
if(src.isLocked()) {
a11yEmbellish
.append(context.getString(
R.string.accessibility_subtitle_lockedpost))
.append(separator);
}
}
if(mPostSubtitleItems.contains(PrefsUtility.AppearancePostSubtitleItem.FLAIR)) {
if(src.getFlairText() != null) {
a11yEmbellish
.append(context.getString(
conciseMode
? R.string.accessibility_subtitle_flair_withperiod_concise
: R.string.accessibility_subtitle_flair_withperiod,
src.getFlairText()
+ General.LTR_OVERRIDE_MARK))
.append(separator);
}
}
return a11yEmbellish.toString();
}
// lol, reddit api
private static boolean hasThumbnail(final RedditParsedPost post) {
final UriString url = post.getThumbnailUrl();
return url != null
&& !url.value.isEmpty()
&& !url.value.equalsIgnoreCase("nsfw")
&& !url.value.equalsIgnoreCase("self")
&& !url.value.equalsIgnoreCase("default");
}
private void downloadThumbnail(
final Context context,
final boolean allowHighRes,
final int sizePixels,
final CacheManager cm,
final int listId) {
final RedditParsedPost.ImagePreviewDetails preview = allowHighRes
? src.getPreview(sizePixels, sizePixels)
: null;
final UriString uri;
if(preview != null) {
uri = preview.url;
} else {
uri = src.getThumbnailUrl();
}
final int priority = Constants.Priority.THUMBNAIL;
final int fileType = Constants.FileType.THUMBNAIL;
final RedditAccount anon = RedditAccountManager.getAnon();
cm.makeRequest(new CacheRequest(
uri,
anon,
null,
new Priority(priority, listId),
DownloadStrategyIfNotCached.INSTANCE,
fileType,
CacheRequest.DownloadQueueType.IMMEDIATE,
context,
new CacheRequestCallbacks() {
@Override
public void onDataStreamComplete(
@NonNull final GenericFactory<SeekableInputStream, IOException> factory,
final TimestampUTC timestamp,
@NonNull final UUID session,
final boolean fromCache,
@Nullable final String mimetype) {
onThumbnailStreamAvailable(factory, sizePixels);
}
@Override
public void onFailure(@NonNull final RRError error) {
if(General.isSensitiveDebugLoggingEnabled()) {
Log.e(
TAG,
"Failed to download thumbnail "
+ uri
+ " with error "
+ error,
error.t);
}
}
}));
}
// These operations are ordered so as to avoid race conditions
public Bitmap getThumbnail(
final ThumbnailLoadedCallback callback,
final int usageId) {
this.thumbnailCallback = callback;
this.usageId = usageId;
return thumbnailCache;
}
public boolean isSelf() {
return src.isSelfPost();
}
public boolean isRead() {
return mChangeDataManager.isRead(src.getIdAndType());
}
public void bind(final RedditPostView boundView) {
mBoundView = boundView;
mChangeDataManager.addListener(src.getIdAndType(), this);
}
public void unbind(final RedditPostView boundView) {
if(mBoundView == boundView) {
mBoundView = null;
mChangeDataManager.removeListener(src.getIdAndType(), this);
}
}
@Override
public void onRedditDataChange(final RedditIdAndType thingIdAndType) {
if(mBoundView != null) {
final Context context = mBoundView.getContext();
if(context != null) {
mBoundView.updateAppearance();
}
}
}
// TODO handle download failure - show red "X" or something
public interface ThumbnailLoadedCallback {
void betterThumbnailAvailable(Bitmap thumbnail, int usageId);
}
public void markAsRead(final Context context) {
final RedditAccount user =
RedditAccountManager.getInstance(context).getDefaultAccount();
RedditChangeDataManager.getInstance(user)
.markRead(TimestampUTC.now(), src.getIdAndType());
}
public boolean isUpvoted() {
return mChangeDataManager.isUpvoted(src.getIdAndType());
}
public boolean isDownvoted() {
return mChangeDataManager.isDownvoted(src.getIdAndType());
}
public int getVoteDirection() {
return isUpvoted() ? 1 : (isDownvoted() ? -1 : 0);
}
public boolean isSaved() {
return mChangeDataManager.isSaved(src.getIdAndType());
}
public boolean isHidden() {
return Boolean.TRUE.equals(mChangeDataManager.isHidden(src.getIdAndType()));
}
private void onThumbnailStreamAvailable(
final GenericFactory<SeekableInputStream, IOException> factory,
final int desiredSizePixels) {
try(SeekableInputStream seekableInputStream = factory.create()) {
final BitmapFactory.Options justDecodeBounds = new BitmapFactory.Options();
justDecodeBounds.inJustDecodeBounds = true;
BitmapFactory.decodeStream(seekableInputStream, null, justDecodeBounds);
final int width = justDecodeBounds.outWidth;
final int height = justDecodeBounds.outHeight;
int factor = 1;
while(width / (factor + 1) > desiredSizePixels
&& height / (factor + 1) > desiredSizePixels) {
factor *= 2;
}
final BitmapFactory.Options scaledOptions = new BitmapFactory.Options();
scaledOptions.inSampleSize = factor;
seekableInputStream.seek(0);
seekableInputStream.mark(0);
final Bitmap data = BitmapFactory.decodeStream(
seekableInputStream,
null,
scaledOptions);
if(data == null) {
return;
}
thumbnailCache = ThumbnailScaler.scale(data, desiredSizePixels);
if(thumbnailCache != data) {
data.recycle();
}
if(thumbnailCallback != null) {
thumbnailCallback.betterThumbnailAvailable(
thumbnailCache,
usageId);
}
} catch(final Throwable t) {
Log.e(
TAG,
"Exception while downloading thumbnail",
t);
}
}
}