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
|
package org.fox.ttrss;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.security.KeyChain;
import android.util.Log;
import android.util.TypedValue;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.ClientCertRequest;
import android.webkit.WebBackForwardList;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebView.HitTestResult;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.core.text.HtmlCompat;
import androidx.preference.PreferenceManager;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.DrawableImageViewTarget;
import org.fox.ttrss.types.Article;
import org.fox.ttrss.types.Attachment;
import org.fox.ttrss.types.Feed;
import org.jsoup.helper.StringUtil;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
// TODO: add ability to update already rendered contents from article somehow (to refresh note, etc)
public class ArticleFragment extends androidx.fragment.app.Fragment {
private final String TAG = this.getClass().getSimpleName();
private SharedPreferences m_prefs;
protected Article m_article;
private DetailActivity m_activity;
private WebView m_web;
//protected View m_fab;
protected int m_articleFontSize;
protected int m_articleSmallFontSize;
public void initialize(Article article) {
m_article = article;
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
if (v.getId() == R.id.article_content) {
HitTestResult result = ((WebView) v).getHitTestResult();
if (result.getType() == HitTestResult.IMAGE_TYPE || result.getType() == HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
menu.setHeaderTitle(result.getExtra());
getActivity().getMenuInflater().inflate(R.menu.content_gallery_entry, menu);
/* FIXME I have no idea how to do this correctly ;( */
m_activity.setLastContentImageHitTestUrl(result.getExtra());
} else {
menu.setHeaderTitle(m_article.title);
getActivity().getMenuInflater().inflate(R.menu.context_article_link, menu);
}
} else {
menu.setHeaderTitle(m_article.title);
getActivity().getMenuInflater().inflate(R.menu.context_article_link, menu);
}
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
int articleId = savedInstanceState.getInt("m_articleId");
m_article = Application.getArticlesModel().getById(articleId);
}
}
@SuppressLint({"NewApi", "SimpleDateFormat"})
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, final Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_article, container, false);
// couldn't reinitialize state properly, might as well bail out
if (m_article == null) {
m_activity.finish();
}
m_articleFontSize = m_prefs.getInt("article_font_size_sp_int", 16);
m_articleSmallFontSize = Math.max(10, Math.min(18, m_articleFontSize - 2));
TextView title = view.findViewById(R.id.title);
if (title != null) {
title.setTextSize(TypedValue.COMPLEX_UNIT_SP, Math.min(21, m_articleFontSize + 3));
String titleStr;
if (m_article.title.length() > 200)
titleStr = m_article.title.substring(0, 200) + "...";
else
titleStr = m_article.title;
title.setText(HtmlCompat.fromHtml(titleStr, HtmlCompat.FROM_HTML_MODE_LEGACY));
//title.setPaintFlags(title.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
title.setOnClickListener(v -> {
try {
m_activity.openUri(Uri.parse(m_article.link));
} catch (Exception e) {
Log.e(TAG, "opening article link", e);
m_activity.toast(R.string.error_other_error);
}
});
}
TextView comments = view.findViewById(R.id.comments);
if (comments != null) {
if (m_activity.getApiLevel() >= 4 && m_article.comments_count > 0) {
comments.setTextSize(TypedValue.COMPLEX_UNIT_SP, m_articleSmallFontSize);
String commentsTitle = getResources().getQuantityString(R.plurals.article_comments, m_article.comments_count, m_article.comments_count);
comments.setText(commentsTitle);
//comments.setPaintFlags(title.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
comments.setOnClickListener(v -> {
try {
String url = (m_article.comments_link != null && !m_article.comments_link.isEmpty()) ?
m_article.comments_link : m_article.link;
m_activity.openUri(Uri.parse(url));
} catch (Exception e) {
Log.e(TAG, "opening comments/links", e);
m_activity.toast(R.string.error_other_error);
}
});
} else {
comments.setVisibility(View.GONE);
}
}
TextView linkHost = view.findViewById(R.id.link_host);
if (linkHost != null) {
if (m_article.isHostDistinct()) {
linkHost.setText(m_article.getLinkHost());
linkHost.setVisibility(View.VISIBLE);
} else {
linkHost.setVisibility(View.GONE);
}
}
TextView note = view.findViewById(R.id.note);
View noteContainer = view.findViewById(R.id.note_container);
if (note != null && noteContainer != null) {
if (!m_article.note.isEmpty()) {
note.setTextSize(TypedValue.COMPLEX_UNIT_SP, m_articleSmallFontSize);
note.setText(m_article.note);
noteContainer.setVisibility(View.VISIBLE);
} else {
noteContainer.setVisibility(View.GONE);
}
note.setOnClickListener(view1 -> m_activity.editArticleNote(m_article));
}
TextView dv = view.findViewById(R.id.date);
if (dv != null) {
dv.setTextSize(TypedValue.COMPLEX_UNIT_SP, m_articleSmallFontSize);
Date d = new Date(m_article.updated * 1000L);
long half_a_year_ago = System.currentTimeMillis() / 1000L - 182 * 24 * 60 * 60;
DateFormat df;
if (m_article.updated < half_a_year_ago)
df = new SimpleDateFormat("MMM dd, yyyy");
else
df = new SimpleDateFormat("MMM dd, HH:mm");
dv.setText(df.format(d));
}
TextView tagv = view.findViewById(R.id.tags);
if (tagv != null) {
tagv.setTextSize(TypedValue.COMPLEX_UNIT_SP, m_articleSmallFontSize);
if (m_article.feed_title != null) {
String fTitle = m_article.feed_title;
if (m_article.author != null && !m_article.author.isEmpty()) {
fTitle += " (" + getString(R.string.author_formatted, m_article.author) + ")";
}
tagv.setText(fTitle);
} else if (m_article.tags != null) {
tagv.setText(StringUtil.join(m_article.tags, ", "));
} else {
tagv.setVisibility(View.GONE);
}
}
ImageView feedIcon = view.findViewById(R.id.feed_icon);
if (feedIcon != null) {
if (m_article.feed_id != Feed.ARCHIVED) {
String ttrssUrl = m_prefs.getString("ttrss_url", "").trim();
String feedIconUri = ttrssUrl + "/public.php?op=feed_icon&id=" + m_article.feed_id;
Glide.with(this)
.load(feedIconUri)
.diskCacheStrategy(DiskCacheStrategy.DATA)
.into(new DrawableImageViewTarget(feedIcon));
} else {
feedIcon.setVisibility(View.GONE);
}
}
m_web = view.findViewById(R.id.article_content);
m_web.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
try {
m_activity.openUri(Uri.parse(url));
return true;
} catch (Exception e) {
Log.e(TAG, "opening URL from context menu", e);
}
return false;
}
@Override
public void onReceivedClientCertRequest(WebView view, ClientCertRequest request) {
ArticleModel model = Application.getArticlesModel();
if (m_prefs.getBoolean("mtls_enabled", false) && model.getPrivateKey() != null) {
request.proceed(model.getPrivateKey(), model.getClientCertChain());
} else {
m_activity.toast(R.string.error_mtls_cert_not_available);
request.cancel();
}
}
});
m_web.setOnLongClickListener(v -> {
HitTestResult result = ((WebView) v).getHitTestResult();
if (result.getType() == HitTestResult.IMAGE_TYPE || result.getType() == HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
registerForContextMenu(m_web);
m_activity.openContextMenu(m_web);
unregisterForContextMenu(m_web);
return true;
} else {
return false;
}
});
renderContent(savedInstanceState);
return view;
}
protected void renderContent(Bundle savedInstanceState) {
if (!isAdded() || m_web == null) return;
Log.d(TAG, "renderContent: " + m_article.title);
WebSettings ws = m_web.getSettings();
ws.setSupportZoom(false);
TypedValue tvTextColor = new TypedValue();
getActivity().getTheme().resolveAttribute(R.attr.colorOnSurface, tvTextColor, true);
String textColor = String.format("#%06X", (0xFFFFFF & tvTextColor.data));
String cssOverride = "body { color : " + textColor + "; }";
TypedValue tvColorPrimary = new TypedValue();
getActivity().getTheme().resolveAttribute(R.attr.colorPrimary, tvColorPrimary, true);
String linkHexColor = String.format("#%06X", (0xFFFFFF & tvColorPrimary.data));
cssOverride += " a:link {color: " + linkHexColor + ";} a:visited { color: " + linkHexColor + ";}";
String articleContent = m_article.content;
ws.setJavaScriptEnabled(m_prefs.getBoolean("article_javascript_enabled", false));
m_web.setBackgroundColor(Color.TRANSPARENT);
ws.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
ws.setMediaPlaybackRequiresUserGesture(true);
if (m_prefs.getBoolean("justify_article_text", true)) {
cssOverride += "body { text-align : justify; } ";
}
cssOverride += " blockquote { border-left: 5px solid gray; padding-left: 10px; margin-left: 0; margin-right: 0; }";
ws.setDefaultFontSize(m_articleFontSize);
int margin8dp = CommonActivity.dpToPx(getContext(), 8);
StringBuilder content = new StringBuilder("<html>" +
"<head>" +
"<meta content=\"text/html; charset=utf-8\" http-equiv=\"content-type\">" +
"<meta name=\"viewport\" content=\"width=device-width, user-scalable=no\" />" +
"<style type=\"text/css\">" +
"body { padding : 0px; margin : " + margin8dp + "px; line-height : 1.3; word-wrap: break-word; }" +
"h1, h2, h3, h4, h5, h6 { line-height: 1; text-align: initial; }" +
"img, video, iframe { max-width : 100%; width : auto; height : auto; }" +
"img, video { object-fit : contain }" +
" table { width : 100%; }" +
cssOverride +
"</style>" +
"</head>" +
"<body dir='auto'>");
content.append(articleContent);
if (m_article.attachments != null && !m_article.attachments.isEmpty()) {
String flatContent = articleContent.replaceAll("[\r\n]", "");
boolean hasImages = flatContent.matches(".*?<img[^>+].*?");
for (Attachment a : m_article.attachments) {
if (a.content_type != null && a.content_url != null) {
try {
if (a.content_type.contains("image") &&
(!hasImages || m_article.always_display_attachments)) {
URL url = new URL(a.content_url.trim());
String strUrl = url.toString().trim();
content.append("<p><img src=\"" + strUrl.replace("\"", "\\\"") + "\"></p>");
}
} catch (MalformedURLException e) {
//
} catch (Exception e) {
Log.e(TAG, "processing image URL", e);
}
}
}
}
content.append("</body></html>");
try {
String baseUrl = null;
try {
URL url = new URL(m_article.link);
baseUrl = url.getProtocol() + "://" + url.getHost();
} catch (MalformedURLException e) {
//
}
if (savedInstanceState == null) {
m_web.loadDataWithBaseURL(baseUrl, content.toString(), "text/html", "utf-8", null);
} else {
WebBackForwardList rc = m_web.restoreState(savedInstanceState);
if (rc == null) {
// restore failed...
m_web.loadDataWithBaseURL(baseUrl, content.toString(), "text/html", "utf-8", null);
}
}
} catch (RuntimeException e) {
Log.e(TAG, "rendering article content", e);
}
}
@Override
public void onPause() {
super.onPause();
if (m_web != null) m_web.onPause();
}
@Override
public void onResume() {
super.onResume();
if (m_web != null) m_web.onResume();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
m_activity = (DetailActivity) activity;
}
@Override
public void onSaveInstanceState(Bundle out) {
super.onSaveInstanceState(out);
out.putInt("m_articleId", m_article.id);
}
}
|