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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
|
package org.fox.ttrss;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.ColorStateList;
import android.graphics.Point;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.transition.Fade;
import android.transition.Transition;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.Size;
import android.util.TypedValue;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.core.view.ViewCompat;
import androidx.lifecycle.ViewModelProvider;
import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.amulyakhare.textdrawable.TextDrawable;
import com.amulyakhare.textdrawable.util.ColorGenerator;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.DrawableImageViewTarget;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.card.MaterialCardView;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.snackbar.Snackbar;
import org.fox.ttrss.glide.ProgressTarget;
import org.fox.ttrss.types.Article;
import org.fox.ttrss.types.Attachment;
import org.fox.ttrss.types.Feed;
import org.fox.ttrss.util.ArticleDiffItemCallback;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
public class HeadlinesFragment extends androidx.fragment.app.Fragment {
private boolean m_isLazyLoading;
public void notifyItemChanged(int position) {
if (m_adapter != null)
m_adapter.notifyItemChanged(position);
}
public static final int FLAVOR_IMG_MIN_SIZE = 128;
private final String TAG = this.getClass().getSimpleName();
private Feed m_feed;
private String m_searchQuery = "";
private SharedPreferences m_prefs;
private ArticleListAdapter m_adapter;
private final List<Article> m_readArticles = new ArrayList<>();
private HeadlinesEventListener m_listener;
private OnlineActivity m_activity;
private SwipeRefreshLayout m_swipeLayout;
private boolean m_compactLayoutMode = false;
private RecyclerView m_list;
private LinearLayoutManager m_layoutManager;
private HeadlinesFragmentModel m_headlinesFragmentModel;
private MediaPlayer m_mediaPlayer;
private TextureView m_activeTexture;
public void initialize(Feed feed) {
m_feed = feed;
}
public void initialize(Feed feed, boolean compactMode) {
m_feed = feed;
m_compactLayoutMode = compactMode;
}
public boolean onArticleMenuItemSelected(MenuItem item, Article article, int position) {
if (article == null) return false;
int itemId = item.getItemId();
if (itemId == R.id.article_set_labels) {
m_activity.editArticleLabels(article);
return true;
} else if (itemId == R.id.article_edit_note) {
m_activity.editArticleNote(article);
return true;
} else if (itemId == R.id.headlines_article_unread) {
Article articleClone = new Article(article);
articleClone.unread = !articleClone.unread;
m_activity.saveArticleUnread(articleClone);
return true;
} else if (itemId == R.id.headlines_article_link_copy) {
m_activity.copyToClipboard(article.link);
return true;
} else if (itemId == R.id.headlines_article_link_open) {
m_activity.openUri(Uri.parse(article.link));
if (article.unread) {
Article articleClone = new Article(article);
articleClone.unread = !articleClone.unread;
m_activity.saveArticleUnread(articleClone);
}
return true;
} else if (itemId == R.id.headlines_share_article) {
m_activity.shareArticle(article);
return true;
} else if (itemId == R.id.catchup_above) {
m_activity.confirmCatchupAbove(article);
return true;
}
Log.d(TAG, "onArticleMenuItemSelected, unhandled id=" + item.getItemId());
return false;
}
// all onContextItemSelected are invoked in sequence so we might get a context menu for headlines, etc
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
if (info != null) {
try {
Article article = Application.getArticles().get(info.position);
if (!onArticleMenuItemSelected(item, article, info.position))
return super.onContextItemSelected(item);
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
}
}
Log.d(TAG, "onContextItemSelected, unhandled id=" + item.getItemId());
return super.onContextItemSelected(item);
}
public HeadlinesFragment() {
super();
Transition fade = new Fade();
setEnterTransition(fade);
setReenterTransition(fade);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
getActivity().getMenuInflater().inflate(R.menu.context_headlines, menu);
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
Article article = m_adapter.getCurrentList().get(info.position);
menu.setHeaderTitle(article.title);
menu.findItem(R.id.article_set_labels).setEnabled(m_activity.getApiLevel() >= 1);
menu.findItem(R.id.article_edit_note).setEnabled(m_activity.getApiLevel() >= 1);
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
m_feed = savedInstanceState.getParcelable("m_feed");
m_searchQuery = savedInstanceState.getString("m_searchQuery");
m_compactLayoutMode = savedInstanceState.getBoolean("m_compactLayoutMode");
}
setRetainInstance(true);
Glide.get(getContext()).clearMemory();
}
@Override
public void onSaveInstanceState(Bundle out) {
super.onSaveInstanceState(out);
out.putParcelable("m_feed", m_feed);
out.putString("m_searchQuery", m_searchQuery);
out.putBoolean("m_compactLayoutMode", m_compactLayoutMode);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(TAG, "onCreateView");
m_headlinesFragmentModel = new ViewModelProvider(this).get(HeadlinesFragmentModel.class);
String headlineMode = m_prefs.getString("headline_mode", "HL_DEFAULT");
if ("HL_COMPACT".equals(headlineMode) || "HL_COMPACT_NOIMAGES".equals(headlineMode))
m_compactLayoutMode = true;
DisplayMetrics metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
View view = inflater.inflate(R.layout.fragment_headlines, container, false);
m_swipeLayout = view.findViewById(R.id.headlines_swipe_container);
// see below re: viewpager2
if (!(m_activity instanceof DetailActivity))
m_swipeLayout.setOnRefreshListener(() -> refresh(false));
else
m_swipeLayout.setEnabled(false);
m_list = view.findViewById(R.id.headlines_list);
registerForContextMenu(m_list);
m_layoutManager = new LinearLayoutManager(m_activity.getApplicationContext());
m_list.setLayoutManager(m_layoutManager);
m_list.setItemAnimator(new DefaultItemAnimator());
m_adapter = new ArticleListAdapter();
m_list.setAdapter(m_adapter);
if (savedInstanceState == null && Application.getArticles().isEmpty()) {
refresh(false);
}
// we disable this because default implementationof viewpager2 does not support removing/reordering/changing items
// https://stackoverflow.com/questions/69368198/delete-item-in-android-viewpager2
if (m_prefs.getBoolean("headlines_swipe_to_dismiss", true) && !(m_activity instanceof DetailActivity)) {
ItemTouchHelper swipeHelper = new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
@Override
public int getSwipeDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
int position = viewHolder.getBindingAdapterPosition();
try {
Article article = Application.getArticles().get(position);
if (article == null || article.id < 0)
return 0;
} catch (IndexOutOfBoundsException e) {
return 0;
}
return super.getSwipeDirs(recyclerView, viewHolder);
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
final int adapterPosition = viewHolder.getBindingAdapterPosition();
try {
final Article article = Application.getArticles().get(adapterPosition);
final boolean wasUnread;
if (article != null && article.id > 0) {
if (article.unread) {
wasUnread = true;
article.unread = false;
m_activity.saveArticleUnread(article);
} else {
wasUnread = false;
}
List<Article> tmpRemove = new ArrayList<>(Application.getArticles());
tmpRemove.remove(adapterPosition);
Application.getArticlesModel().update(tmpRemove);
Snackbar.make(m_list, R.string.headline_undo_row_prompt, Snackbar.LENGTH_LONG)
.setAction(getString(R.string.headline_undo_row_button), v -> {
if (wasUnread) {
article.unread = true;
m_activity.saveArticleUnread(article);
}
List<Article> tmpInsert = new ArrayList<>(Application.getArticles());
tmpInsert.add(adapterPosition, article);
Application.getArticlesModel().update(tmpInsert);
}).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
swipeHelper.attachToRecyclerView(m_list);
}
m_list.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
ArticleModel model = Application.getArticlesModel();
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
if (!m_readArticles.isEmpty() && !m_isLazyLoading && !model.isLoading() && m_prefs.getBoolean("headlines_mark_read_scroll", false)) {
Log.d(TAG, "marking articles as read, count=" + m_readArticles.size());
// since we clear the list after we send the batch to mark as read, we need to pass a cloned arraylist here,
// otherwise nothing would get marked as read when async operation completes
m_activity.setArticlesUnread(new ArrayList<>(m_readArticles), Article.UPDATE_SET_FALSE);
m_readArticles.clear();
new Handler().postDelayed(() -> m_activity.refresh(false), 100);
}
}
}
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int firstVisibleItem = m_layoutManager.findFirstVisibleItemPosition();
int lastVisibleItem = m_layoutManager.findLastVisibleItemPosition();
// Log.d(TAG, "onScrolled: FVI=" + firstVisibleItem + " LVI=" + lastVisibleItem);
if (m_prefs.getBoolean("headlines_mark_read_scroll", false)) {
for (int i = 0; i < firstVisibleItem; i++) {
try {
Article article = Application.getArticles().get(i);
if (article.unread && !m_readArticles.contains(article))
m_readArticles.add(new Article(article));
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
}
}
// Log.d(TAG, "pending to auto mark as read count=" + m_readArticles.size());
}
ArticleModel model = Application.getArticlesModel();
if (dy > 0 && !m_isLazyLoading && !model.isLoading() && model.isLazyLoadEnabled() &&
lastVisibleItem >= Application.getArticles().size() - 5) {
Log.d(TAG, "attempting to lazy load more articles (onScrolled)...");
m_isLazyLoading = true;
// this has to be dispatched delayed, consequent adapter updates are forbidden in scroll handler
new Handler().postDelayed(() -> refresh(true), 250);
}
}
});
ArticleModel model = Application.getArticlesModel();
model.getIsLoading().observe(getActivity(), isLoading -> {
Log.d(TAG, "observed headlines isLoading=" + isLoading + " lazyLoadEnabled=" + model.isLazyLoadEnabled());
if (m_swipeLayout != null)
m_swipeLayout.setRefreshing(isLoading);
});
// this gets notified on loading %
model.getLoadingProgress().observe(getActivity(), progress -> {
Log.d(TAG, "observed headlines loading progress=" + progress);
m_listener.onHeadlinesLoadingProgress(progress);
});
// this gets notified if active article changes
model.getActive().observe(getActivity(), (activeArticle) -> {
Log.d(TAG, "observed active article=" + activeArticle);
if (activeArticle != null) {
// we can't be sure scrollToArticle() below actually does anything in DetailView because our fragment might be invisible in some layouts
// so we also trigger lazy load on active article change
if (m_activity instanceof DetailActivity) {
int position = Application.getArticles().indexOf(activeArticle);
if (!m_isLazyLoading && !model.isLoading() && model.isLazyLoadEnabled() &&
position >= Application.getArticles().size() - 5) {
Log.d(TAG, "attempting to lazy load more articles (observed active article change)...");
m_isLazyLoading = true;
// this has to be dispatched delayed, consequent adapter updates are forbidden in scroll handler
new Handler().postDelayed(() -> refresh(true), 250);
}
}
scrollToArticle(activeArticle);
}
});
// this gets notified on network update
model.getUpdatesData().observe(getActivity(), lastUpdate -> {
if (lastUpdate > 0) {
List<Article> tmp = new ArrayList<>(model.getArticles().getValue());
Log.d(TAG, "observed headlines last update=" + lastUpdate + " article count=" + tmp.size());
if (m_prefs.getBoolean("headlines_mark_read_scroll", false))
tmp.add(new Article(Article.TYPE_AMR_FOOTER));
final boolean appended = model.getAppend();
m_adapter.submitList(tmp, () -> {
if (!appended)
m_list.scrollToPosition(0);
m_isLazyLoading = false;
m_listener.onHeadlinesLoaded(appended);
});
if (model.getFirstIdChanged())
Snackbar.make(getView(), R.string.headlines_row_top_changed, Snackbar.LENGTH_LONG)
.setAction(R.string.reload, v -> refresh(false)).show();
if (model.getLastError() != null && model.getLastError() != ApiCommon.ApiError.SUCCESS) {
m_isLazyLoading = false;
if (model.getLastError() == ApiCommon.ApiError.LOGIN_FAILED) {
m_activity.login();
return;
}
m_listener.onHeadlinesLoaded(appended);
if (model.getLastErrorMessage() != null) {
m_activity.toast(m_activity.getString(model.getErrorMessage()) + "\n" + model.getLastErrorMessage());
} else {
m_activity.toast(model.getErrorMessage());
}
}
}
});
// loaded articles might get modified for all sorts of reasons
model.getArticles().observe(getActivity(), articles -> {
Log.d(TAG, "observed headlines article list size=" + articles.size());
List<Article> tmp = new ArrayList<>(articles);
if (m_prefs.getBoolean("headlines_mark_read_scroll", false))
tmp.add(new Article(Article.TYPE_AMR_FOOTER));
m_adapter.submitList(tmp);
});
return view;
}
@Override
public void onResume() {
super.onResume();
Log.d(TAG, "onResume");
syncToSharedArticles();
m_activity.invalidateOptionsMenu();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
m_activity = (OnlineActivity) activity;
m_listener = (HeadlinesEventListener) activity;
}
public void refresh(final boolean append) {
ArticleModel model = Application.getArticlesModel();
// we do not support non-append refreshes while in DetailActivity because of viewpager2
if (m_activity instanceof DetailActivity && !append)
return;
if (!append) {
model.setActive(null);
model.setSelection(ArticleModel.ArticlesSelection.NONE);
}
model.startLoading(append, m_feed, m_activity.getResizeWidth());
}
static class ArticleViewHolder extends RecyclerView.ViewHolder {
public View view;
public TextView titleView;
public TextView feedTitleView;
public MaterialButton markedView;
public MaterialButton scoreView;
public MaterialButton publishedView;
public TextView excerptView;
public ImageView flavorImageView;
public ImageView flavorVideoKindView;
public TextView authorView;
public TextView dateView;
public CheckBox selectionBoxView;
public MaterialButton menuButtonView;
public ViewGroup flavorImageHolder;
public ProgressBar flavorImageLoadingBar;
public View headlineFooter;
public ImageView textImage;
public ImageView textChecked;
public View headlineHeader;
public View flavorImageOverflow;
public TextureView flavorVideoView;
public MaterialButton attachmentsView;
public TextView linkHost;
public ArticleViewHolder(View v) {
super(v);
view = v;
titleView = v.findViewById(R.id.title);
feedTitleView = v.findViewById(R.id.feed_title);
markedView = v.findViewById(R.id.marked);
scoreView = v.findViewById(R.id.score);
publishedView = v.findViewById(R.id.published);
excerptView = v.findViewById(R.id.excerpt);
flavorImageView = v.findViewById(R.id.flavor_image);
flavorVideoKindView = v.findViewById(R.id.flavor_video_kind);
authorView = v.findViewById(R.id.author);
dateView = v.findViewById(R.id.date);
selectionBoxView = v.findViewById(R.id.selected);
menuButtonView = v.findViewById(R.id.article_menu_button);
flavorImageHolder = v.findViewById(R.id.flavor_image_holder);
flavorImageLoadingBar = v.findViewById(R.id.flavor_image_progressbar);
textImage = v.findViewById(R.id.text_image);
textChecked = v.findViewById(R.id.text_checked);
headlineHeader = v.findViewById(R.id.headline_header);
flavorImageOverflow = v.findViewById(R.id.gallery_overflow);
flavorVideoView = v.findViewById(R.id.flavor_video);
attachmentsView = v.findViewById(R.id.attachments);
linkHost = v.findViewById(R.id.link_host);
}
}
private static class FlavorProgressTarget<Z> extends ProgressTarget<String, Z> {
private final ArticleViewHolder holder;
public FlavorProgressTarget(Target<Z> target, String model, ArticleViewHolder holder) {
super(target);
setModel(model);
this.holder = holder;
}
@Override
public float getGranualityPercentage() {
return 0.1f; // this matches the format string for #text below
}
@Override
protected void onConnecting() {
holder.flavorImageHolder.setVisibility(View.VISIBLE);
holder.flavorImageLoadingBar.setIndeterminate(true);
holder.flavorImageLoadingBar.setVisibility(View.VISIBLE);
}
@Override
protected void onDownloading(long bytesRead, long expectedLength) {
holder.flavorImageHolder.setVisibility(View.VISIBLE);
holder.flavorImageLoadingBar.setIndeterminate(false);
holder.flavorImageLoadingBar.setProgress((int) (100 * bytesRead / expectedLength));
}
@Override
protected void onDownloaded() {
holder.flavorImageHolder.setVisibility(View.VISIBLE);
holder.flavorImageLoadingBar.setIndeterminate(true);
}
@Override
protected void onDelivered() {
holder.flavorImageHolder.setVisibility(View.VISIBLE);
holder.flavorImageLoadingBar.setVisibility(View.INVISIBLE);
}
}
private class ArticleListAdapter extends ListAdapter<Article, ArticleViewHolder> {
public static final int VIEW_NORMAL = 0;
public static final int VIEW_AMR_FOOTER = 1;
private final ColorGenerator m_colorGenerator = ColorGenerator.DEFAULT;
private final TextDrawable.IBuilder m_drawableBuilder = TextDrawable.builder().round();
private final ColorStateList m_cslTertiary;
private final ColorStateList m_cslPrimary;
private final int m_colorSurfaceContainerLowest;
private final int m_colorSurface;
private final int m_colorPrimary;
private final int m_colorTertiary;
private final int m_colorSecondary;
private final int m_colorOnSurface;
private final int m_colorTertiaryContainer;
private final int m_colorOnTertiaryContainer;
boolean m_flavorImageEnabled;
private final int m_screenWidth;
private final int m_screenHeight;
private final int m_headlineSmallFontSize;
private final int m_headlineFontSize;
private final boolean m_enableIconTinting;
private final ConnectivityManager m_cmgr;
private boolean canShowFlavorImage() {
if (m_flavorImageEnabled) {
if (m_prefs.getBoolean("headline_images_wifi_only", false)) {
// why do i have to get this service every time instead of using a member variable :(
NetworkInfo wifi = m_cmgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifi != null)
return wifi.isConnected();
} else {
return true;
}
}
return false;
}
private int colorFromAttr(int attr) {
TypedValue tv = new TypedValue();
m_activity.getTheme().resolveAttribute(attr, tv, true);
return ContextCompat.getColor(m_activity, tv.resourceId);
}
public ArticleListAdapter() {
super(new ArticleDiffItemCallback());
Display display = m_activity.getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
m_screenHeight = size.y;
m_screenWidth = size.x;
String headlineMode = m_prefs.getString("headline_mode", "HL_DEFAULT");
m_flavorImageEnabled = "HL_DEFAULT".equals(headlineMode) || "HL_COMPACT".equals(headlineMode);
m_colorPrimary = colorFromAttr(R.attr.colorPrimary);
m_colorSecondary = colorFromAttr(R.attr.colorSecondary);
m_colorTertiary = colorFromAttr(R.attr.colorTertiary);
m_cslTertiary = ColorStateList.valueOf(m_colorTertiary);
m_cslPrimary = ColorStateList.valueOf(m_colorPrimary);
m_colorSurfaceContainerLowest = colorFromAttr(R.attr.colorSurfaceContainerLowest);
m_colorSurface = colorFromAttr(R.attr.colorSurface);
m_colorOnSurface = colorFromAttr(R.attr.colorOnSurface);
m_colorTertiaryContainer = colorFromAttr(R.attr.colorTertiaryContainer);
m_colorOnTertiaryContainer = colorFromAttr(R.attr.colorOnTertiaryContainer);
m_headlineFontSize = m_prefs.getInt("headlines_font_size_sp_int", 13);
m_headlineSmallFontSize = Math.max(10, Math.min(18, m_headlineFontSize - 2));
m_enableIconTinting = m_prefs.getBoolean("enable_icon_tinting", true);
m_cmgr = (ConnectivityManager) m_activity.getSystemService(Context.CONNECTIVITY_SERVICE);
}
@NonNull
@Override
public ArticleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
int layoutId = m_compactLayoutMode ? R.layout.headlines_row_compact : R.layout.headlines_row;
if (viewType == VIEW_AMR_FOOTER) {
layoutId = R.layout.headlines_footer;
}
View v = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
ArticleViewHolder holder = new ArticleViewHolder(v);
// set on click handlers once when view is created
holder.view.setOnClickListener(view -> {
int position = m_list.getChildAdapterPosition(view);
if (position != -1) {
Article article = m_adapter.getItem(position);
m_listener.onArticleSelected(article);
}
});
holder.view.setOnLongClickListener(view -> {
m_list.showContextMenuForChild(view);
return true;
});
// block footer clicks to make button/selection clicking easier
if (holder.headlineFooter != null) {
holder.headlineFooter.setOnClickListener(view -> {
//
});
}
if (holder.attachmentsView != null) {
holder.attachmentsView.setOnClickListener(view -> {
int position = m_list.getChildAdapterPosition(holder.view);
if (position != -1) {
Article article = m_adapter.getItem(position);
m_activity.displayAttachments(article);
}
});
}
if (holder.flavorImageView != null) {
holder.flavorImageView.setOnClickListener(view -> {
int position = m_list.getChildAdapterPosition(holder.view);
if (position != -1) {
Article article = m_adapter.getItem(position);
openGalleryForType(article, holder, holder.flavorImageView);
}
});
}
if (holder.flavorImageOverflow != null) {
holder.flavorImageOverflow.setOnClickListener(view -> {
PopupMenu popup = new PopupMenu(getContext(), holder.flavorImageOverflow);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.content_gallery_entry, popup.getMenu());
int position = m_list.getChildAdapterPosition(holder.view);
if (position != -1) {
Article article = m_adapter.getItem(position);
popup.setOnMenuItemClickListener(item -> {
Uri mediaUri = Uri.parse(article.flavorStreamUri != null ? article.flavorStreamUri :
article.flavorImageUri);
int itemId = item.getItemId();
if (itemId == R.id.article_img_open) {
m_activity.openUri(mediaUri);
return true;
} else if (itemId == R.id.article_img_copy) {
m_activity.copyToClipboard(mediaUri.toString());
return true;
} else if (itemId == R.id.article_img_share) {
m_activity.shareImageFromUri(mediaUri.toString());
return true;
} else if (itemId == R.id.article_img_share_url) {
m_activity.shareText(mediaUri.toString());
return true;
} else if (itemId == R.id.article_img_view_caption) {
m_activity.displayImageCaption(article.flavorImageUri, article.content);
return true;
}
return false;
});
popup.show();
}
});
holder.flavorImageView.setOnLongClickListener(view -> {
m_list.showContextMenuForChild(holder.view);
return true;
});
}
if (holder.menuButtonView != null) {
holder.menuButtonView.setOnClickListener(view -> {
int position = m_list.getChildAdapterPosition(holder.view);
if (position != -1) {
PopupMenu popup = new PopupMenu(getContext(), view);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.context_headlines, popup.getMenu());
popup.getMenu().findItem(R.id.article_set_labels).setEnabled(m_activity.getApiLevel() >= 1);
popup.getMenu().findItem(R.id.article_edit_note).setEnabled(m_activity.getApiLevel() >= 1);
popup.setOnMenuItemClickListener(item -> onArticleMenuItemSelected(item,
getItem(position),
m_list.getChildAdapterPosition(holder.view)));
popup.show();
}
});
}
if (holder.markedView != null) {
holder.markedView.setOnClickListener(view -> {
int position = m_list.getChildAdapterPosition(holder.view);
if (position != -1) {
Article article = new Article(getItem(position));
article.marked = !article.marked;
m_activity.saveArticleMarked(article);
}
});
}
if (holder.selectionBoxView != null) {
holder.selectionBoxView.setOnClickListener(view -> {
int position = m_list.getChildAdapterPosition(holder.view);
if (position != -1) {
Article article = new Article(getItem(position));
CheckBox cb = (CheckBox) view;
article.selected = cb.isChecked();
Application.getArticlesModel().update(article);
}
});
}
if (holder.publishedView != null) {
holder.publishedView.setOnClickListener(view -> {
int position = m_list.getChildAdapterPosition(holder.view);
if (position != -1) {
Article article = new Article(getItem(position));
article.published = !article.published;
m_activity.saveArticlePublished(article);
}
});
}
if (holder.textImage != null) {
holder.textImage.setOnClickListener(view -> {
int position = m_list.getChildAdapterPosition(holder.view);
if (position != -1) {
Article article = new Article(getItem(position));
article.selected = !article.selected;
Application.getArticlesModel().update(article);
}
});
holder.textImage.setOnLongClickListener(view -> {
int position = m_list.getChildAdapterPosition(holder.view);
if (position != -1) {
Article article = getItem(position);
openGalleryForType(article, holder, holder.textImage);
}
return true;
});
}
if (holder.scoreView != null) {
if (m_activity.getApiLevel() >= 16) {
holder.scoreView.setOnClickListener(view -> {
int position = m_list.getChildAdapterPosition(holder.view);
if (position != -1) {
final Article articleClone = new Article(getItem(position));
final EditText edit = new EditText(getActivity());
edit.setText(String.valueOf(articleClone.score));
MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(getContext())
.setTitle(R.string.score_for_this_article)
.setPositiveButton(R.string.set_score,
(dialog, which) -> {
try {
articleClone.score = Integer.parseInt(edit.getText().toString());
m_activity.saveArticleScore(articleClone);
} catch (NumberFormatException e) {
m_activity.toast(R.string.score_invalid);
e.printStackTrace();
}
})
.setNegativeButton(getString(R.string.cancel),
(dialog, which) -> {
}).setView(edit);
Dialog dialog = builder.create();
dialog.show();
}
});
} else {
holder.scoreView.setVisibility(View.GONE);
}
}
return holder;
}
@Override
public void onViewRecycled(@NonNull ArticleViewHolder holder) {
super.onViewRecycled(holder);
if (holder.flavorImageView != null)
Glide.with(HeadlinesFragment.this).clear(holder.flavorImageView);
}
@Override
// https://stackoverflow.com/questions/33176336/need-an-example-about-recyclerview-adapter-notifyitemchangedint-position-objec/50085835#50085835
public void onBindViewHolder(@NonNull final ArticleViewHolder holder, final int position, final List<Object> payloads) {
if (!payloads.isEmpty()) {
Log.d(TAG, "onBindViewHolder, payloads=" + payloads + " position=" + position);
final Article article = getItem(position);
for (final Object pobject : payloads) {
ArticleDiffItemCallback.ChangePayload payload = (ArticleDiffItemCallback.ChangePayload) pobject;
switch (payload) {
case UNREAD:
case ACTIVE:
updateUnreadView(article, holder);
break;
case MARKED:
updateMarkedView(article, holder);
break;
case SELECTED:
updateSelectedView(article, holder);
updateTextImage(article, holder);
break;
case PUBLISHED:
updatePublishedView(article, holder);
break;
case SCORE:
updateScoreView(article, holder);
break;
}
}
} else {
super.onBindViewHolder(holder, position, payloads);
}
}
private void updateUnreadView(final Article article, final ArticleViewHolder holder) {
if (m_compactLayoutMode) {
holder.view.setBackgroundColor(article.unread ? m_colorSurfaceContainerLowest : 0);
} else {
MaterialCardView card = (MaterialCardView) holder.view;
card.setCardBackgroundColor(article.unread ? m_colorSurfaceContainerLowest : m_colorSurface);
}
if (holder.titleView != null) {
holder.titleView.setTypeface(null, article.unread ? Typeface.BOLD : Typeface.NORMAL);
holder.titleView.setTextColor(article.unread ? m_colorOnSurface : m_colorPrimary);
}
updateActiveView(article, holder);
}
private void updateActiveView(final Article article, final ArticleViewHolder holder) {
if (m_activity instanceof DetailActivity) {
if (article.active) {
holder.view.setBackgroundColor(m_colorTertiaryContainer);
if (holder.titleView != null) {
holder.titleView.setTextColor(m_colorOnTertiaryContainer);
}
}
if (holder.excerptView != null) {
holder.excerptView.setTextColor(article.active ? m_colorOnTertiaryContainer : m_colorOnSurface);
}
if (holder.feedTitleView != null) {
holder.feedTitleView.setTextColor(article.active ? m_colorOnTertiaryContainer : m_colorSecondary);
}
}
}
@Override
public void onBindViewHolder(@NonNull final ArticleViewHolder holder, int position) {
Article article = getItem(position);
if (article.id == Article.TYPE_AMR_FOOTER && m_prefs.getBoolean("headlines_mark_read_scroll", false)) {
WindowManager wm = (WindowManager) m_activity.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int screenHeight = (int) (display.getHeight() * 1.5);
holder.view.setLayoutParams(new ListView.LayoutParams(ListView.LayoutParams.MATCH_PARENT, screenHeight));
}
// nothing else of interest for those below anyway
if (article.id < 0) return;
updateUnreadView(article, holder);
updateTextImage(article, holder);
updateTitleView(article, holder);
updateMarkedView(article, holder);
updateScoreView(article, holder);
updatePublishedView(article, holder);
updateAttachmentsView(article, holder);
updateLinkHost(article, holder);
updateExcerptView(article, holder);
updateAuthorView(article, holder);
updateDateView(article, holder);
updateSelectedView(article, holder);
if (!m_compactLayoutMode && holder.flavorImageHolder != null) {
// reset our view to default in case of recycling
holder.flavorImageLoadingBar.setVisibility(View.GONE);
holder.flavorImageLoadingBar.setIndeterminate(false);
holder.flavorImageView.setVisibility(View.GONE);
holder.flavorVideoKindView.setVisibility(View.GONE);
holder.flavorImageOverflow.setVisibility(View.GONE);
holder.flavorVideoView.setVisibility(View.GONE);
holder.flavorImageHolder.setVisibility(View.GONE);
if (canShowFlavorImage() && article.flavorImageUri != null && holder.flavorImageView != null) {
int maxImageHeight = (int) (m_screenHeight * 0.5f);
// we also downsample below using glide to save RAM
holder.flavorImageView.setMaxHeight(maxImageHeight);
if (m_headlinesFragmentModel.getFlavorImageSizes().containsKey(article.flavorImageUri)) {
Size size = m_headlinesFragmentModel.getFlavorImageSizes().get(article.flavorImageUri);
if (BuildConfig.DEBUG)
Log.d(TAG, "using cached resource size for " + article.flavorImageUri + " " + size.getWidth() + "x" + size.getHeight());
if (size.getWidth() > FLAVOR_IMG_MIN_SIZE && size.getHeight() > FLAVOR_IMG_MIN_SIZE) {
loadFlavorImage(article, holder, maxImageHeight);
}
} else {
if (BuildConfig.DEBUG)
Log.d(TAG, "checking resource size for " + article.flavorImageUri);
checkImageAndLoad(article, holder, maxImageHeight);
}
}
/* if (m_prefs.getBoolean("inline_video_player", false) && article.flavorImage != null &&
"video".equalsIgnoreCase(article.flavorImage.tagName()) && article.flavorStreamUri != null) {
holder.flavorVideoView.setOnLongClickListener(v -> {
releaseSurface();
openGalleryForType(article, holder, holder.flavorImageView);
return true;
});
holder.flavorImageView.setOnClickListener(view -> {
releaseSurface();
m_mediaPlayer = new MediaPlayer();
holder.flavorVideoView.setVisibility(View.VISIBLE);
final ProgressBar bar = holder.flavorImageLoadingBar;
bar.setIndeterminate(true);
bar.setVisibility(View.VISIBLE);
holder.flavorVideoView.setOnClickListener(v -> {
try {
if (m_mediaPlayer.isPlaying())
m_mediaPlayer.pause();
else
m_mediaPlayer.start();
} catch (IllegalStateException e) {
releaseSurface();
}
});
m_activeTexture = holder.flavorVideoView;
ViewGroup.LayoutParams lp = m_activeTexture.getLayoutParams();
Drawable drawable = holder.flavorImageView.getDrawable();
if (drawable != null) {
float aspect = drawable.getIntrinsicWidth() / (float) drawable.getIntrinsicHeight();
lp.height = holder.flavorImageView.getMeasuredHeight();
lp.width = (int) (lp.height * aspect);
m_activeTexture.setLayoutParams(lp);
}
holder.flavorVideoView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
try {
m_mediaPlayer.setSurface(new Surface(surface));
m_mediaPlayer.setDataSource(article.flavorStreamUri);
m_mediaPlayer.setOnPreparedListener(mp -> {
try {
bar.setVisibility(View.GONE);
mp.setLooping(true);
mp.start();
} catch (IllegalStateException e) {
e.printStackTrace();
}
});
m_mediaPlayer.prepareAsync();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
try {
m_mediaPlayer.release();
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
}
);
});
} else {
holder.flavorImageView.setOnClickListener(view -> openGalleryForType(article, holder, holder.flavorImageView));
} */
}
}
private void updateTitleView(final Article article, final ArticleViewHolder holder) {
if (holder.titleView != null) {
holder.titleView.setText(Html.fromHtml(article.title));
holder.titleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, Math.min(21, m_headlineFontSize + 3));
}
if (holder.feedTitleView != null) {
if (article.feed_title != null && m_feed != null && (m_feed.is_cat || m_feed.id < 0)) {
holder.feedTitleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, m_headlineSmallFontSize);
holder.feedTitleView.setText(article.feed_title);
} else {
holder.feedTitleView.setVisibility(View.GONE);
}
}
}
private void updateDateView(final Article article, final ArticleViewHolder holder) {
if (holder.dateView != null) {
holder.dateView.setTextSize(TypedValue.COMPLEX_UNIT_SP, m_headlineSmallFontSize);
Date d = new Date((long) article.updated * 1000);
Date now = new Date();
long half_a_year_ago = now.getTime() / 1000L - 182 * 24 * 60 * 60;
DateFormat df;
if (now.getYear() == d.getYear() && now.getMonth() == d.getMonth() && now.getDay() == d.getDay()) {
df = new SimpleDateFormat("HH:mm");
} else if (article.updated > half_a_year_ago) {
df = new SimpleDateFormat("MMM dd");
} else {
df = new SimpleDateFormat("MMM yyyy");
}
df.setTimeZone(TimeZone.getDefault());
holder.dateView.setText(df.format(d));
}
}
private void updateAuthorView(final Article article, final ArticleViewHolder holder) {
String articleAuthor = article.author != null ? article.author : "";
if (holder.authorView != null) {
holder.authorView.setTextSize(TypedValue.COMPLEX_UNIT_SP, m_headlineSmallFontSize);
if (!articleAuthor.isEmpty()) {
holder.authorView.setText(getString(R.string.author_formatted, articleAuthor));
} else {
holder.authorView.setText("");
}
}
}
private void updateExcerptView(final Article article, final ArticleViewHolder holder) {
if (holder.excerptView != null) {
if (!m_prefs.getBoolean("headlines_show_content", true)) {
holder.excerptView.setVisibility(View.GONE);
} else {
String excerpt = "";
try {
if (article.excerpt != null) {
excerpt = article.excerpt;
} else if (article.articleDoc != null) {
excerpt = article.articleDoc.text();
if (excerpt.length() > CommonActivity.EXCERPT_MAX_LENGTH)
excerpt = excerpt.substring(0, CommonActivity.EXCERPT_MAX_LENGTH) + "…";
}
} catch (Exception e) {
e.printStackTrace();
excerpt = "";
}
holder.excerptView.setTextSize(TypedValue.COMPLEX_UNIT_SP, m_headlineFontSize);
holder.excerptView.setText(excerpt);
if (!excerpt.isEmpty()) {
holder.excerptView.setVisibility(View.VISIBLE);
} else {
holder.excerptView.setVisibility(View.GONE);
}
}
}
}
private void updateLinkHost(final Article article, final ArticleViewHolder holder) {
if (holder.linkHost != null) {
if (article.isHostDistinct()) {
holder.linkHost.setTextSize(TypedValue.COMPLEX_UNIT_SP, m_headlineSmallFontSize);
holder.linkHost.setText(article.getLinkHost());
holder.linkHost.setVisibility(View.VISIBLE);
} else {
holder.linkHost.setVisibility(View.GONE);
}
}
}
private void updateAttachmentsView(final Article article, final ArticleViewHolder holder) {
if (holder.attachmentsView != null) {
if (article.attachments != null && !article.attachments.isEmpty()) {
holder.attachmentsView.setVisibility(View.VISIBLE);
} else {
holder.attachmentsView.setVisibility(View.GONE);
}
}
}
private void updateMarkedView(final Article article, final ArticleViewHolder holder) {
if (holder.markedView != null) {
holder.markedView.setIconResource(article.marked ? R.drawable.baseline_star_24 : R.drawable.baseline_star_outline_24);
if (m_enableIconTinting)
holder.markedView.setIconTint(article.marked ? m_cslTertiary : m_cslPrimary);
}
}
private void updateTextImage(final Article article, final ArticleViewHolder holder) {
if (holder.textImage != null) {
updateTextCheckedState(article, holder);
ViewCompat.setTransitionName(holder.textImage,
"gallery:" + article.flavorImageUri);
}
}
private void updateSelectedView(final Article article, final ArticleViewHolder holder) {
if (holder.selectionBoxView != null) {
holder.selectionBoxView.setChecked(article.selected);
}
}
private void updateScoreView(final Article article, final ArticleViewHolder holder) {
if (holder.scoreView != null) {
int scoreDrawable = R.drawable.baseline_trending_flat_24;
if (article.score > 0)
scoreDrawable = R.drawable.baseline_trending_up_24;
else if (article.score < 0)
scoreDrawable = R.drawable.baseline_trending_down_24;
holder.scoreView.setIconResource(scoreDrawable);
if (m_enableIconTinting) {
if (article.score > Article.SCORE_HIGH)
holder.scoreView.setIconTint(m_cslTertiary);
else
holder.scoreView.setIconTint(m_cslPrimary);
}
}
}
private void updatePublishedView(final Article article, final ArticleViewHolder holder) {
if (holder.publishedView != null) {
// otherwise we just use tinting in actionbar
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O || !m_prefs.getBoolean("enable_icon_tinting", true)) {
holder.publishedView.setIconResource(article.published ? R.drawable.rss_box : R.drawable.rss);
}
if (m_enableIconTinting)
holder.publishedView.setIconTint(article.published ? m_cslTertiary : m_cslPrimary);
}
}
private void loadFlavorImage(final Article article, final ArticleViewHolder holder, final int maxImageHeight) {
Glide.with(HeadlinesFragment.this)
.load(article.flavorImageUri)
.transition(DrawableTransitionOptions.withCrossFade())
.override(m_screenWidth, maxImageHeight)
.diskCacheStrategy(DiskCacheStrategy.DATA)
.skipMemoryCache(false)
.listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
holder.flavorImageHolder.setVisibility(View.GONE);
holder.flavorImageView.setVisibility(View.GONE);
holder.flavorImageOverflow.setVisibility(View.VISIBLE);
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
holder.flavorImageHolder.setVisibility(View.VISIBLE);
holder.flavorImageView.setVisibility(View.VISIBLE);
holder.flavorImageOverflow.setVisibility(View.VISIBLE);
adjustVideoKindView(holder, article);
return false;
}
})
.into(new DrawableImageViewTarget(holder.flavorImageView));
}
private void checkImageAndLoad(final Article article, final ArticleViewHolder holder, final int maxImageHeight) {
FlavorProgressTarget<Size> flavorProgressTarget = new FlavorProgressTarget<>(new SimpleTarget<Size>() {
@Override
public void onResourceReady(@NonNull Size resource, @Nullable com.bumptech.glide.request.transition.Transition<? super Size> transition) {
if (BuildConfig.DEBUG)
Log.d(TAG, "got resource of " + resource.getWidth() + "x" + resource.getHeight());
m_headlinesFragmentModel.getFlavorImageSizes().put(article.flavorImageUri, resource);
if (resource.getWidth() > FLAVOR_IMG_MIN_SIZE && resource.getHeight() > FLAVOR_IMG_MIN_SIZE) {
// now we can actually load the image into our drawable
loadFlavorImage(article, holder, maxImageHeight);
} else {
holder.flavorImageHolder.setVisibility(View.GONE);
holder.flavorImageView.setVisibility(View.VISIBLE);
holder.flavorImageOverflow.setVisibility(View.VISIBLE);
}
}
}, article.flavorImageUri, holder);
Glide.with(HeadlinesFragment.this)
.as(Size.class)
.load(article.flavorImageUri)
.diskCacheStrategy(DiskCacheStrategy.DATA)
.skipMemoryCache(true)
.into(flavorProgressTarget);
}
@Override
public int getItemViewType(int position) {
Article a = getItem(position);
if (a.id == Article.TYPE_AMR_FOOTER) {
return VIEW_AMR_FOOTER;
} else {
return VIEW_NORMAL;
}
}
private void updateTextCheckedState(final Article article, final ArticleViewHolder holder) {
String tmp = !article.title.isEmpty() ? article.title.substring(0, 1).toUpperCase() : "?";
if (article.selected) {
holder.textImage.setImageDrawable(m_drawableBuilder.build(" ", 0xff616161));
holder.textChecked.setVisibility(View.VISIBLE);
} else {
final Drawable textDrawable = m_drawableBuilder.build(tmp, m_colorGenerator.getColor(article.title));
holder.textImage.setImageDrawable(textDrawable);
if (!canShowFlavorImage() || article.flavorImage == null) {
holder.textImage.setImageDrawable(textDrawable);
} else {
Glide.with(HeadlinesFragment.this)
.load(article.flavorImageUri)
.transition(DrawableTransitionOptions.withCrossFade())
.placeholder(textDrawable)
.thumbnail(0.5f)
.apply(RequestOptions.circleCropTransform())
.diskCacheStrategy(DiskCacheStrategy.ALL)
.skipMemoryCache(false)
.into(holder.textImage);
}
holder.textChecked.setVisibility(View.GONE);
}
}
private void openGalleryForType(final Article article, final ArticleViewHolder holder, final View transitionView) {
//Log.d(TAG, "openGalleryForType: " + article + " " + holder + " " + transitionView);
if (article.flavorImage != null) {
if ("iframe".equalsIgnoreCase(article.flavorImage.tagName())) {
m_activity.openUri(Uri.parse(article.flavorStreamUri));
} else {
Intent intent = new Intent(m_activity, GalleryActivity.class);
intent.putExtra("firstSrc", article.flavorStreamUri != null ? article.flavorStreamUri : article.flavorImageUri);
intent.putExtra("title", article.title);
// FIXME maybe: gallery view works with document as html, it's easier to add this hack rather than
// rework it to additionally operate on separate attachment array (?)
// also, maybe consider video attachments? kinda hard to do without a poster tho (for flavor view)
String tempContent = article.content;
if (article.attachments != null) {
Document doc = new Document("");
for (Attachment a : article.attachments) {
if (a.content_type != null) {
if (a.content_type.contains("image/")) {
Element img = new Element("img").attr("src", a.content_url);
doc.appendChild(img);
}
}
}
tempContent = doc.outerHtml() + tempContent;
}
intent.putExtra("content", tempContent);
/* ActivityOptionsCompat options =
ActivityOptionsCompat.makeSceneTransitionAnimation(m_activity,
transitionView != null ? transitionView : holder.flavorImageView,
"gallery:" + (article.flavorStreamUri != null ? article.flavorStreamUri : article.flavorImageUri));
ActivityCompat.startActivity(m_activity, intent, options.toBundle()); */
startActivity(intent);
}
}
}
private void adjustVideoKindView(final ArticleViewHolder holder, final Article article) {
if (article.flavorImage != null) {
if (article.flavor_kind == Article.FLAVOR_KIND_YOUTUBE || "iframe".equalsIgnoreCase(article.flavorImage.tagName())) {
holder.flavorVideoKindView.setImageResource(R.drawable.baseline_play_circle_outline_24);
holder.flavorVideoKindView.setVisibility(View.VISIBLE);
} else if (article.flavor_kind == Article.FLAVOR_KIND_VIDEO || "video".equalsIgnoreCase(article.flavorImage.tagName())) {
holder.flavorVideoKindView.setImageResource(R.drawable.baseline_play_circle_24);
holder.flavorVideoKindView.setVisibility(View.VISIBLE);
} else {
holder.flavorVideoKindView.setVisibility(View.INVISIBLE);
}
} else {
holder.flavorVideoKindView.setVisibility(View.INVISIBLE);
}
}
}
private void releaseSurface() {
try {
if (m_mediaPlayer != null) {
m_mediaPlayer.release();
}
} catch (IllegalStateException e) {
e.printStackTrace();
}
try {
if (m_activeTexture != null) {
m_activeTexture.setVisibility(View.GONE);
}
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
public void scrollToArticle(Article article) {
int position = Application.getArticles().indexOf(article);
if (position != -1)
m_list.scrollToPosition(position);
}
public Feed getFeed() {
return m_feed;
}
@Override
public void onPause() {
super.onPause();
releaseSurface();
}
private void syncToSharedArticles() {
List<Article> tmp = new ArrayList<>(Application.getArticles());
if (m_prefs.getBoolean("headlines_mark_read_scroll", false))
tmp.add(new Article(Article.TYPE_AMR_FOOTER));
m_adapter.submitList(tmp);
}
}
|