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
|
package org.fox.ttrss;
import android.app.Application;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import org.fox.ttrss.types.GalleryEntry;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class GalleryModel extends AndroidViewModel {
private final String TAG = this.getClass().getSimpleName();
private MutableLiveData<List<GalleryEntry>> m_items = new MutableLiveData<>(new ArrayList<>());
private MutableLiveData<Integer> m_checkProgress = new MutableLiveData<>(Integer.valueOf(0));
private MutableLiveData<Integer> m_itemsToCheck = new MutableLiveData<>(Integer.valueOf(0));
private MutableLiveData<Boolean> m_isChecking = new MutableLiveData<>(Boolean.valueOf(false));
public GalleryModel(@NonNull Application application) {
super(application);
}
public LiveData<List<GalleryEntry>> getItems() {
return m_items;
}
private ExecutorService m_executor = Executors.newSingleThreadExecutor();
private Handler m_mainHandler = new Handler(Looper.getMainLooper());
public LiveData<Integer> getItemsToCheck() {
return m_itemsToCheck;
}
public LiveData<Integer> getCheckProgress() {
return m_checkProgress;
}
private boolean isDataUri(String src) {
try {
Uri uri = Uri.parse(src);
return "data".equalsIgnoreCase(uri.getScheme());
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public void update(List<GalleryEntry> items) {
m_items.postValue(items);
}
public LiveData<Boolean> getIsChecking() {
return m_isChecking;
}
public void collectItems(String articleText, String srcFirst) {
m_executor.execute(() -> {
Document doc = Jsoup.parse(articleText);
List<GalleryEntry> checkList = new ArrayList<>();
Log.d(TAG, "looking for srcFirst=" + srcFirst);
Elements elems = doc.select("img,video");
m_itemsToCheck.postValue(elems.size());
int currentItem = 0;
boolean firstFound = false;
m_isChecking.postValue(true);
for (Element elem : elems) {
++currentItem;
if ("video".equalsIgnoreCase(elem.tagName())) {
Element source = elem.select("source").first();
String poster = elem.attr("abs:poster");
if (source != null) {
String src = source.attr("abs:src");
Log.d(TAG, "checking vid src=" + src + " poster=" + poster);
if (src != null && src.equals(srcFirst)) {
Log.d(TAG, "first item found, vid=" + src);
firstFound = true;
GalleryEntry item = new GalleryEntry(src, GalleryEntry.GalleryEntryType.TYPE_VIDEO, poster);
checkList.add(item);
m_items.postValue(checkList);
} else {
if (!isDataUri(src)) {
checkList.add(new GalleryEntry(src, GalleryEntry.GalleryEntryType.TYPE_VIDEO, poster));
m_items.postValue(checkList);
}
}
}
} else {
String src = elem.attr("abs:src");
Log.d(TAG, "checking img src=" + src);
if (src != null && src.equals(srcFirst)) {
Log.d(TAG, "first item found, img=" + src);
firstFound = true;
GalleryEntry item = new GalleryEntry(src, GalleryEntry.GalleryEntryType.TYPE_IMAGE, null);
checkList.add(item);
m_items.postValue(checkList);
} else {
if (!isDataUri(src)) {
Log.d(TAG, "checking image with glide: " + src);
try {
Bitmap bmp = Glide.with(getApplication().getApplicationContext())
.asBitmap()
.load(src)
.skipMemoryCache(false)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(HeadlinesFragment.FLAVOR_IMG_MIN_SIZE, HeadlinesFragment.FLAVOR_IMG_MIN_SIZE)
.get();
if (bmp != null && bmp.getWidth() >= HeadlinesFragment.FLAVOR_IMG_MIN_SIZE && bmp.getHeight() >= HeadlinesFragment.FLAVOR_IMG_MIN_SIZE) {
Log.d(TAG, "image matches gallery criteria, adding...");
checkList.add(new GalleryEntry(src, GalleryEntry.GalleryEntryType.TYPE_IMAGE, null));
m_items.postValue(checkList);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
m_checkProgress.postValue(currentItem);
}
// if we didn't find it in the document, let's add insert to the list anyway so shared transition
// would hopefully work
if (!firstFound) {
checkList.add(0, new GalleryEntry(srcFirst, GalleryEntry.GalleryEntryType.TYPE_IMAGE, null));
m_items.postValue(checkList);
}
m_isChecking.postValue(false);
});
}
}
|