-
-
Notifications
You must be signed in to change notification settings - Fork 510
Expand file tree
/
Copy pathRedditPostView.java
More file actions
674 lines (516 loc) · 20.1 KB
/
RedditPostView.java
File metadata and controls
674 lines (516 loc) · 20.1 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
/*******************************************************************************
* 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.views;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.UiThread;
import androidx.constraintlayout.widget.ConstraintLayout;
import org.quantumbadger.redreader.R;
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.AndroidCommon;
import org.quantumbadger.redreader.common.Constants;
import org.quantumbadger.redreader.common.DisplayUtils;
import org.quantumbadger.redreader.common.General;
import org.quantumbadger.redreader.common.GenericFactory;
import org.quantumbadger.redreader.common.Optional;
import org.quantumbadger.redreader.common.PrefsUtility;
import org.quantumbadger.redreader.common.Priority;
import org.quantumbadger.redreader.common.RRError;
import org.quantumbadger.redreader.common.SharedPrefsWrapper;
import org.quantumbadger.redreader.common.datastream.SeekableInputStream;
import org.quantumbadger.redreader.common.time.TimestampUTC;
import org.quantumbadger.redreader.fragments.PostListingFragment;
import org.quantumbadger.redreader.reddit.api.RedditPostActions;
import org.quantumbadger.redreader.reddit.prepared.RedditParsedPost;
import org.quantumbadger.redreader.reddit.prepared.RedditPreparedPost;
import org.quantumbadger.redreader.views.liststatus.ErrorView;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
public final class RedditPostView extends FlingableItemView
implements RedditPreparedPost.ThumbnailLoadedCallback {
private static final String TAG = "RedditPostView";
private static final String PROMPT_PREF_KEY = "inline_image_prompt_accepted";
private static final AtomicInteger sInlinePreviewsShownThisSession = new AtomicInteger(0);
private final AccessibilityActionManager mAccessibilityActionManager;
private RedditPreparedPost mPost = null;
private final TextView title;
private final TextView subtitle;
@NonNull private final ImageView mThumbnailView;
@NonNull private final ImageView mOverlayIcon;
@NonNull private final LinearLayout mOuterView;
@NonNull private final LinearLayout mInnerView;
@NonNull private final LinearLayout mCommentsButton;
@NonNull private final TextView mCommentsText;
@NonNull private final LinearLayout mPostErrors;
@NonNull private final FrameLayout mImagePreviewHolder;
@NonNull private final ImageView mImagePreviewImageView;
@NonNull private final ConstraintLayout mImagePreviewPlayOverlay;
@NonNull private final LinearLayout mImagePreviewOuter;
@NonNull private final LoadingSpinnerView mImagePreviewLoadingSpinner;
@NonNull private final LinearLayout mFooter;
private int mUsageId = 0;
private final Handler thumbnailHandler;
private final BaseActivity mActivity;
private final PrefsUtility.PostFlingAction mLeftFlingPref;
private final PrefsUtility.PostFlingAction mRightFlingPref;
private RedditPostActions.ActionDescriptionPair mLeftFlingAction;
private RedditPostActions.ActionDescriptionPair mRightFlingAction;
private final boolean mCommentsButtonPref;
private final int
rrPostTitleReadCol;
private final int rrPostTitleCol;
private final int mThumbnailSizePrefPixels;
@Override
protected void onSetItemFlingPosition(final float position) {
mOuterView.setTranslationX(position);
}
@NonNull
@Override
protected String getFlingLeftText() {
mLeftFlingAction = RedditPostActions.ActionDescriptionPair.from(mPost, mLeftFlingPref);
if(mLeftFlingAction != null) {
return mActivity.getString(mLeftFlingAction.getDescriptionRes());
} else {
return "Disabled";
}
}
@NonNull
@Override
protected String getFlingRightText() {
mRightFlingAction = RedditPostActions.ActionDescriptionPair.from(mPost, mRightFlingPref);
if(mRightFlingAction != null) {
return mActivity.getString(mRightFlingAction.getDescriptionRes());
} else {
return "Disabled";
}
}
@Override
protected boolean allowFlingingLeft() {
return mLeftFlingAction != null;
}
@Override
protected boolean allowFlingingRight() {
return mRightFlingAction != null;
}
@Override
protected void onFlungLeft() {
RedditPostActions.INSTANCE.onActionMenuItemSelected(
mPost,
mActivity,
mLeftFlingAction.getAction());
}
@Override
protected void onFlungRight() {
RedditPostActions.INSTANCE.onActionMenuItemSelected(
mPost,
mActivity,
mRightFlingAction.getAction());
}
public RedditPostView(
final Context context,
final PostListingFragment fragmentParent,
final BaseActivity activity,
final boolean leftHandedMode) {
super(context);
mActivity = activity;
mAccessibilityActionManager = new AccessibilityActionManager(
this,
context.getResources());
thumbnailHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(@NonNull final Message msg) {
if(mUsageId != msg.what) {
return;
}
mThumbnailView.setImageBitmap((Bitmap)msg.obj);
}
};
final float dpScale = context.getResources().getDisplayMetrics().density;
final float titleFontScale = PrefsUtility.appearance_fontscale_posts();
final float subtitleFontScale = PrefsUtility.appearance_fontscale_post_subtitles();
final View rootView =
LayoutInflater.from(context).inflate(R.layout.reddit_post, this, true);
mOuterView = Objects.requireNonNull(rootView.findViewById(R.id.reddit_post_layout_outer));
mInnerView = Objects.requireNonNull(rootView.findViewById(R.id.reddit_post_layout_inner));
mPostErrors = Objects.requireNonNull(rootView.findViewById(R.id.reddit_post_errors));
mImagePreviewHolder = Objects.requireNonNull(
rootView.findViewById(R.id.reddit_post_image_preview_holder));
mImagePreviewImageView = Objects.requireNonNull(
rootView.findViewById(R.id.reddit_post_image_preview_imageview));
mImagePreviewPlayOverlay = Objects.requireNonNull(
rootView.findViewById(R.id.reddit_post_image_preview_play_overlay));
mImagePreviewOuter = Objects.requireNonNull(
rootView.findViewById(R.id.reddit_post_image_preview_outer));
mFooter = Objects.requireNonNull(
rootView.findViewById(R.id.reddit_post_footer));
mImagePreviewLoadingSpinner = new LoadingSpinnerView(activity);
mImagePreviewHolder.addView(mImagePreviewLoadingSpinner);
mThumbnailView = Objects.requireNonNull(
rootView.findViewById(R.id.reddit_post_thumbnail_view));
mOverlayIcon = Objects.requireNonNull(
rootView.findViewById(R.id.reddit_post_overlay_icon));
title = Objects.requireNonNull(rootView.findViewById(R.id.reddit_post_title));
subtitle = Objects.requireNonNull(rootView.findViewById(R.id.reddit_post_subtitle));
mCommentsButtonPref =
PrefsUtility.appearance_post_show_comments_button();
mCommentsButton = rootView.findViewById(R.id.reddit_post_comments_button);
mCommentsText = mCommentsButton.findViewById(R.id.reddit_post_comments_text);
if(!mCommentsButtonPref) {
mInnerView.removeView(mCommentsButton);
}
if(leftHandedMode) {
final ArrayList<View> innerViewElements = new ArrayList<>(3);
for(int i = mInnerView.getChildCount() - 1; i >= 0; i--) {
innerViewElements.add(mInnerView.getChildAt(i));
mInnerView.removeViewAt(i);
}
for(int i = 0; i < innerViewElements.size(); i++) {
mInnerView.addView(innerViewElements.get(i));
}
mInnerView.setNextFocusRightId(NO_ID);
if (mCommentsButtonPref) {
mInnerView.setNextFocusLeftId(mCommentsButton.getId());
mCommentsButton.setNextFocusForwardId(R.id.reddit_post_layout_outer);
mCommentsButton.setNextFocusRightId(R.id.reddit_post_layout_outer);
mCommentsButton.setNextFocusLeftId(NO_ID);
}
}
final OnLongClickListener longClickListener = v -> {
RedditPostActions.INSTANCE.showActionMenu(mActivity, mPost);
return true;
};
switch(PrefsUtility.pref_behaviour_post_tap_action()) {
case LINK:
mOuterView.setOnClickListener(v -> fragmentParent.onPostSelected(mPost));
AndroidCommon.removeClickListeners(mThumbnailView);
AndroidCommon.removeClickListeners(mImagePreviewOuter);
AndroidCommon.removeClickListeners(title);
break;
case COMMENTS:
mOuterView.setOnClickListener(v -> fragmentParent.onPostCommentsSelected(mPost));
mThumbnailView.setOnClickListener(v -> fragmentParent.onPostSelected(mPost));
mThumbnailView.setOnLongClickListener(longClickListener);
mImagePreviewOuter.setOnClickListener(v -> fragmentParent.onPostSelected(mPost));
mImagePreviewOuter.setOnLongClickListener(longClickListener);
AndroidCommon.removeClickListeners(title);
break;
case TITLE_COMMENTS:
mOuterView.setOnClickListener(v -> fragmentParent.onPostSelected(mPost));
AndroidCommon.removeClickListeners(mThumbnailView);
AndroidCommon.removeClickListeners(mImagePreviewOuter);
title.setOnClickListener(v -> fragmentParent.onPostCommentsSelected(mPost));
title.setOnLongClickListener(longClickListener);
break;
}
mOuterView.setOnLongClickListener(longClickListener);
if(mCommentsButtonPref) {
mCommentsButton.setOnClickListener(v -> fragmentParent.onPostCommentsSelected(mPost));
}
title.setTextSize(
TypedValue.COMPLEX_UNIT_PX,
title.getTextSize() * titleFontScale);
subtitle.setTextSize(
TypedValue.COMPLEX_UNIT_PX,
subtitle.getTextSize() * subtitleFontScale);
mLeftFlingPref =
PrefsUtility.pref_behaviour_fling_post_left();
mRightFlingPref =
PrefsUtility.pref_behaviour_fling_post_right();
{
final TypedArray attr = context.obtainStyledAttributes(new int[] {
R.attr.rrPostTitleCol,
R.attr.rrPostTitleReadCol,
});
rrPostTitleCol = attr.getColor(0, 0);
rrPostTitleReadCol = attr.getColor(1, 0);
attr.recycle();
}
mThumbnailSizePrefPixels = (int)(dpScale * PrefsUtility.images_thumbnail_size_dp());
}
@UiThread
public void reset(@NonNull final RedditPreparedPost newPost) {
if(newPost != mPost) {
mThumbnailView.setImageBitmap(null);
mImagePreviewImageView.setImageBitmap(null);
mImagePreviewPlayOverlay.setVisibility(GONE);
mPostErrors.removeAllViews();
mFooter.removeAllViews();
mUsageId++;
resetSwipeState();
title.setText(newPost.src.getTitle());
if(mCommentsButtonPref) {
mCommentsText.setText(String.valueOf(newPost.src.getSrc().getNum_comments()));
}
final boolean showInlinePreview = newPost.shouldShowInlinePreview();
final boolean showThumbnail = !showInlinePreview && newPost.hasThumbnail;
if(showInlinePreview) {
downloadInlinePreview(newPost, mUsageId);
} else {
mImagePreviewLoadingSpinner.setVisibility(GONE);
mImagePreviewOuter.setVisibility(GONE);
setBottomMargin(false);
}
if(showThumbnail) {
final Bitmap thumbnail = newPost.getThumbnail(this, mUsageId);
mThumbnailView.setImageBitmap(thumbnail);
mThumbnailView.setVisibility(VISIBLE);
mThumbnailView.setMinimumWidth(mThumbnailSizePrefPixels);
General.setLayoutWidthHeight(
mThumbnailView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.MATCH_PARENT);
mInnerView.setMinimumHeight(mThumbnailSizePrefPixels);
} else {
mThumbnailView.setMinimumWidth(0);
mThumbnailView.setVisibility(GONE);
mInnerView.setMinimumHeight(General.dpToPixels(mActivity, 64));
}
}
if(mPost != null) {
mPost.unbind(this);
}
newPost.bind(this);
mPost = newPost;
updateAppearance();
}
public void updateAppearance() {
mOuterView.setBackgroundResource(
R.drawable.rr_postlist_item_selector_main);
if(mCommentsButtonPref) {
mCommentsButton.setBackgroundResource(
R.drawable.rr_postlist_commentbutton_selector_main);
}
if(mPost.isRead()) {
title.setTextColor(rrPostTitleReadCol);
} else {
title.setTextColor(rrPostTitleCol);
}
title.setContentDescription(mPost.buildAccessibilityTitle(mActivity, false));
subtitle.setText(mPost.buildSubtitle(mActivity, false));
subtitle.setContentDescription(mPost.buildAccessibilitySubtitle(mActivity, false));
boolean overlayVisible = true;
if(mPost.isSaved()) {
mOverlayIcon.setImageResource(R.drawable.star_dark);
} else if(mPost.isHidden()) {
mOverlayIcon.setImageResource(R.drawable.ic_action_cross_dark);
} else if(mPost.isUpvoted()) {
mOverlayIcon.setImageResource(R.drawable.arrow_up_bold_orangered);
} else if(mPost.isDownvoted()) {
mOverlayIcon.setImageResource(R.drawable.arrow_down_bold_periwinkle);
} else {
overlayVisible = false;
}
if(overlayVisible) {
mOverlayIcon.setVisibility(VISIBLE);
} else {
mOverlayIcon.setVisibility(GONE);
}
RedditPostActions.INSTANCE.setupAccessibilityActions(
mAccessibilityActionManager,
mPost,
mActivity,
false);
}
@Override
public void betterThumbnailAvailable(
final Bitmap thumbnail,
final int callbackUsageId) {
final Message msg = Message.obtain();
msg.obj = thumbnail;
msg.what = callbackUsageId;
thumbnailHandler.sendMessage(msg);
}
public interface PostSelectionListener {
void onPostSelected(RedditPreparedPost post);
void onPostCommentsSelected(RedditPreparedPost post);
}
private void setBottomMargin(final boolean enabled) {
final MarginLayoutParams layoutParams
= (MarginLayoutParams)mOuterView.getLayoutParams();
if(enabled) {
layoutParams.bottomMargin = General.dpToPixels(mActivity, 6);
} else {
layoutParams.bottomMargin = 0;
}
mOuterView.setLayoutParams(layoutParams);
}
private void downloadInlinePreview(
@NonNull final RedditPreparedPost post,
final int usageId) {
final Rect windowVisibleDisplayFrame
= DisplayUtils.getWindowVisibleDisplayFrame(mActivity);
final int screenWidth = Math.min(1080, Math.max(720, windowVisibleDisplayFrame.width()));
final int screenHeight = Math.min(2000, Math.max(400, windowVisibleDisplayFrame.height()));
final RedditParsedPost.ImagePreviewDetails preview
= post.src.getPreview(screenWidth, 0);
if(preview == null || preview.width < 10 || preview.height < 10) {
mImagePreviewOuter.setVisibility(GONE);
mImagePreviewLoadingSpinner.setVisibility(GONE);
setBottomMargin(false);
return;
}
final int boundedImageHeight = Math.min(
(screenHeight * 2) / 3,
(int)(((long)preview.height * screenWidth) / preview.width));
final ConstraintLayout.LayoutParams imagePreviewLayoutParams
= (ConstraintLayout.LayoutParams)mImagePreviewHolder.getLayoutParams();
imagePreviewLayoutParams.dimensionRatio = screenWidth + ":" + boundedImageHeight;
mImagePreviewHolder.setLayoutParams(imagePreviewLayoutParams);
mImagePreviewOuter.setVisibility(VISIBLE);
mImagePreviewLoadingSpinner.setVisibility(VISIBLE);
setBottomMargin(true);
CacheManager.getInstance(mActivity).makeRequest(new CacheRequest(
preview.url,
RedditAccountManager.getAnon(),
null,
new Priority(Constants.Priority.INLINE_IMAGE_PREVIEW),
DownloadStrategyIfNotCached.INSTANCE,
Constants.FileType.INLINE_IMAGE_PREVIEW,
CacheRequest.DownloadQueueType.IMMEDIATE,
mActivity,
new CacheRequestCallbacks() {
@Override
public void onDataStreamComplete(
@NonNull final GenericFactory<SeekableInputStream, IOException> stream,
final TimestampUTC timestamp,
@NonNull final UUID session,
final boolean fromCache,
@Nullable final String mimetype) {
if(usageId != mUsageId) {
return;
}
try(InputStream is = stream.create()) {
final Bitmap data = BitmapFactory.decodeStream(is);
if(data == null) {
throw new IOException("Failed to decode bitmap");
}
// Avoid a crash on badly behaving Android ROMs (where the ImageView
// crashes if an image is too big)
// Should never happen as we limit the preview size to 3000x3000
if(data.getByteCount() > 50 * 1024 * 1024) {
throw new RuntimeException("Image was too large: "
+ data.getByteCount()
+ ", preview URL was "
+ preview.url
+ " and post was "
+ post.src.getIdAndType());
}
final boolean alreadyAcceptedPrompt = General.getSharedPrefs(mActivity)
.getBoolean(PROMPT_PREF_KEY, false);
final int totalPreviewsShown
= sInlinePreviewsShownThisSession.incrementAndGet();
final boolean isVideoPreview = post.isVideoPreview();
AndroidCommon.runOnUiThread(() -> {
mImagePreviewImageView.setImageBitmap(data);
mImagePreviewLoadingSpinner.setVisibility(GONE);
if(isVideoPreview) {
mImagePreviewPlayOverlay.setVisibility(VISIBLE);
}
// Show every 8 previews, starting at the second one
if(totalPreviewsShown % 8 == 2 && !alreadyAcceptedPrompt) {
showPrefPrompt();
}
});
} catch(final Throwable t) {
onFailure(General.getGeneralErrorForFailure(
mActivity,
CacheRequest.RequestFailureType.CONNECTION,
t,
null,
preview.url,
Optional.empty()));
}
}
@Override
public void onFailure(@NonNull final RRError error) {
Log.e(TAG, "Failed to download image preview: " + error, error.t);
if(usageId != mUsageId) {
return;
}
AndroidCommon.runOnUiThread(() -> {
mImagePreviewLoadingSpinner.setVisibility(GONE);
mImagePreviewOuter.setVisibility(GONE);
final ErrorView errorView = new ErrorView(
mActivity,
error);
mPostErrors.addView(errorView);
General.setLayoutMatchWidthWrapHeight(errorView);
});
}
}
));
}
private void showPrefPrompt() {
final SharedPrefsWrapper sharedPrefs
= General.getSharedPrefs(mActivity);
LayoutInflater.from(mActivity).inflate(
R.layout.inline_images_question_view,
mFooter,
true);
final FrameLayout promptView
= mFooter.findViewById(R.id.inline_images_prompt_root);
final Button keepShowing
= mFooter.findViewById(R.id.inline_preview_prompt_keep_showing_button);
final Button turnOff
= mFooter.findViewById(R.id.inline_preview_prompt_turn_off_button);
keepShowing.setOnClickListener(v -> {
new RRAnimationShrinkHeight(promptView).start();
sharedPrefs.edit()
.putBoolean(PROMPT_PREF_KEY, true)
.apply();
});
turnOff.setOnClickListener(v -> {
final String prefPreview = mActivity.getApplicationContext()
.getString(
R.string.pref_images_inline_image_previews_key);
sharedPrefs.edit()
.putBoolean(PROMPT_PREF_KEY, true)
.putString(prefPreview, "never")
.apply();
});
}
@Nullable public RedditPreparedPost getPost() {
return mPost;
}
}