summaryrefslogtreecommitdiff
path: root/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineDownloadService.java
blob: ec1c353df9e530b019bdea0a891f97533b5826cd (plain)
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
package org.fox.ttrss.offline;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.graphics.BitmapFactory;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.provider.BaseColumns;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;

import org.fox.ttrss.ApiRequest;
import org.fox.ttrss.BuildConfig;
import org.fox.ttrss.CommonActivity;
import org.fox.ttrss.OnlineActivity;
import org.fox.ttrss.R;
import org.fox.ttrss.types.Article;
import org.fox.ttrss.types.Feed;
import org.fox.ttrss.types.FeedCategory;
import org.fox.ttrss.util.DatabaseHelper;
import org.fox.ttrss.util.ImageCacheService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;

import androidx.core.app.NotificationCompat;

public class OfflineDownloadService extends Service {

	private final String TAG = this.getClass().getSimpleName();

	// enable downloading read articles in debug configuration for testing
	private static boolean OFFLINE_DEBUG_READ = false;

	public static final int NOTIFY_DOWNLOADING = 1;
	public static final int NOTIFY_DOWNLOAD_SUCCESS = 2;

	public static final int PI_GENERIC = 0;
	public static final int PI_CANCEL = 1;
	public static final int PI_SUCCESS = 2;

	//public static final String INTENT_ACTION_SUCCESS = "org.fox.ttrss.intent.action.DownloadComplete";
	public static final String INTENT_ACTION_CANCEL = "org.fox.ttrss.intent.action.Cancel";
	public static final String INTENT_ACTION_SWITCH_OFFLINE = "org.fox.ttrss.intent.action.SwitchOffline";

	private static final int OFFLINE_SYNC_SEQ = 50;
	private static final int OFFLINE_SYNC_MAX = OFFLINE_SYNC_SEQ * 10;
	
	private int m_articleOffset = 0;
	private String m_sessionId;
	private NotificationManager m_nmgr;
	
	private boolean m_batchMode = false;
	private boolean m_downloadInProgress = false;
	private boolean m_downloadImages = false;
	private int m_syncMax;
	private SharedPreferences m_prefs;
	private boolean m_canProceed = true;
	
	private final IBinder m_binder = new LocalBinder();
	private DatabaseHelper m_databaseHelper;

	public class LocalBinder extends Binder {
        OfflineDownloadService getService() {
            return OfflineDownloadService.this;
        }
    }
	
    @Override
    public IBinder onBind(Intent intent) {
        return m_binder;
    }
    
	@Override
	public void onCreate() {
		super.onCreate();
		m_nmgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

		m_prefs = PreferenceManager
						.getDefaultSharedPreferences(getApplicationContext());
 
		m_downloadImages = m_prefs.getBoolean("offline_image_cache_enabled", false);
		m_syncMax = Integer.parseInt(m_prefs.getString("offline_sync_max", String.valueOf(OFFLINE_SYNC_MAX)));
		
		initDatabase();
	}
	
	@SuppressWarnings("deprecation")
	private void updateNotification(String msg, int progress, int max, boolean showProgress, boolean isError) {
		Intent intent = new Intent(this, OnlineActivity.class);
		
		PendingIntent contentIntent = PendingIntent.getActivity(this, PI_GENERIC,
                intent, 0);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
                .setContentText(msg)
                .setContentTitle(getString(R.string.notify_downloading_title))
                .setContentIntent(contentIntent)
                .setWhen(System.currentTimeMillis())
				.setSmallIcon(R.drawable.ic_cloud_download)
                .setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(),
                        R.drawable.ic_launcher))
                .setOngoing(!isError)
                .setOnlyAlertOnce(true);

		if (showProgress) builder.setProgress(max, progress, max == 0);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

			intent = new Intent(this, OnlineActivity.class);
			intent.setAction(INTENT_ACTION_CANCEL);

			PendingIntent cancelIntent = PendingIntent.getActivity(this, PI_CANCEL, intent, 0);

            builder.setCategory(Notification.CATEGORY_PROGRESS)
                    .setVibrate(new long[0])
                    .setVisibility(Notification.VISIBILITY_PUBLIC)
                    .setColor(0x88b0f0)
                    .setGroup("org.fox.ttrss")
					.addAction(R.drawable.ic_launcher, getString(R.string.cancel), cancelIntent);
        }

		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
			builder.setChannelId(CommonActivity.NOTIFICATION_CHANNEL_NORMAL);
		}

        m_nmgr.notify(NOTIFY_DOWNLOADING, builder.build());
	}

	@SuppressWarnings("deprecation")
	private void notifyDownloadComplete() {
		Intent intent = new Intent(this, OnlineActivity.class);

		if (m_articleOffset > 0) {
			intent.setAction(INTENT_ACTION_SWITCH_OFFLINE);
		}

		PendingIntent contentIntent = PendingIntent.getActivity(this, PI_SUCCESS,
				intent, PendingIntent.FLAG_UPDATE_CURRENT);

		NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
				.setContentIntent(contentIntent)
				.setWhen(System.currentTimeMillis())
				.setSmallIcon(R.drawable.ic_notification)
				.setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(),
						R.drawable.ic_launcher))
				.setOnlyAlertOnce(true)
				.setPriority(Notification.PRIORITY_HIGH)
				.setDefaults(Notification.DEFAULT_ALL)
				.setAutoCancel(true);

		if (m_articleOffset > 0) {
			builder
					.setContentTitle(getString(R.string.dialog_offline_success))
					.setContentText(getString(R.string.offline_tap_to_switch));
		} else {
			builder
					.setContentTitle(getString(R.string.offline_switch_failed))
					.setContentText(getString(R.string.offline_no_articles));

		}

		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
			builder.setCategory(Notification.CATEGORY_MESSAGE)
					.setVibrate(new long[0])
					.setVisibility(Notification.VISIBILITY_PUBLIC)
					.setColor(0x88b0f0)
					.setGroup("org.fox.ttrss");
		}

		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
			builder.setChannelId(CommonActivity.NOTIFICATION_CHANNEL_PRIORITY);
		}

		m_nmgr.notify(NOTIFY_DOWNLOAD_SUCCESS, builder.build());
	}

	private void updateNotification(int msgResId, int progress, int max, boolean showProgress, boolean isError) {
		updateNotification(getString(msgResId), progress, max, showProgress, isError);
	}

	private void downloadFailed() {
        //m_nmgr.cancel(NOTIFY_DOWNLOADING);
        
        // TODO send notification to activity?
        
        m_downloadInProgress = false;
        stopSelf();
	}
	
	private boolean isCacheServiceRunning() {
	    ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
	    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
	        if ("org.fox.ttrss.util.ImageCacheService".equals(service.service.getClassName())) {
	            return true;
	        }
	    }
	    return false;
	}
	
	public void downloadComplete() {
		m_downloadInProgress = false;
		
        // if cache service is running, it will send a finished intent on its own
        if (!isCacheServiceRunning()) {
            m_nmgr.cancel(NOTIFY_DOWNLOADING);

            if (m_batchMode) {
            	
            	SharedPreferences localPrefs = getSharedPreferences("localprefs", Context.MODE_PRIVATE);
				SharedPreferences.Editor editor = localPrefs.edit();
				editor.putBoolean("offline_mode_active", true);
				editor.apply();
            	
            } else {

            	/*Intent intent = new Intent();
            	intent.setAction(INTENT_ACTION_SUCCESS);
            	intent.addCategory(Intent.CATEGORY_DEFAULT);
            	sendBroadcast(intent);*/

				notifyDownloadComplete();
            }
        }

        stopSelf();
	}
	
	private void initDatabase() {
		m_databaseHelper = DatabaseHelper.getInstance(this);
	}
	
	/* private synchronized SQLiteDatabase getReadableDb() {
		return m_readableDb;
	} */
	
	private synchronized SQLiteDatabase getDatabase() {
		return m_databaseHelper.getWritableDatabase();
	}
	
	@SuppressWarnings("unchecked")
	private void downloadArticles() {
		Log.d(TAG, "offline: downloading articles... offset=" + m_articleOffset);
		
		updateNotification(getString(R.string.notify_downloading_articles, m_articleOffset), m_articleOffset, m_syncMax, true, false);
		
		OfflineArticlesRequest req = new OfflineArticlesRequest(this);
		
		@SuppressWarnings("serial")
		HashMap<String,String> map = new HashMap<String,String>();
		map.put("op", "getHeadlines");
		map.put("sid", m_sessionId);
		map.put("feed_id", "-4");

		if (BuildConfig.DEBUG && OFFLINE_DEBUG_READ) {
			map.put("view_mode", "all_articles");
		} else {
			map.put("view_mode", "unread");
		}
		map.put("show_content", "true");
		map.put("skip", String.valueOf(m_articleOffset));
		map.put("limit", String.valueOf(OFFLINE_SYNC_SEQ));
		
		req.execute(map);
	}
	
	private void downloadFeeds() {

		updateNotification(R.string.notify_downloading_feeds, 0, 0, true, false);
		
		getDatabase().execSQL("DELETE FROM feeds;");
		
		ApiRequest req = new ApiRequest(getApplicationContext()) {
			@Override
			protected JsonElement doInBackground(HashMap<String, String>... params) {
				JsonElement content = super.doInBackground(params);

				if (content != null) {

					try {
						Type listType = new TypeToken<List<Feed>>() {}.getType();
						List<Feed> feeds = new Gson().fromJson(content, listType);
						
						SQLiteStatement stmtInsert = getDatabase().compileStatement("INSERT INTO feeds " +
								"(" + BaseColumns._ID + ", title, feed_url, has_icon, cat_id) " +
								"VALUES (?, ?, ?, ?, ?);");
						
						for (Feed feed : feeds) {
							stmtInsert.bindLong(1, feed.id);
							stmtInsert.bindString(2, feed.title);
							stmtInsert.bindString(3, feed.feed_url);
							stmtInsert.bindLong(4, feed.has_icon ? 1 : 0);
							stmtInsert.bindLong(5, feed.cat_id);
	
							stmtInsert.execute();
						}
	
						stmtInsert.close();
	
						Log.d(TAG, "offline: done downloading feeds");
						
						m_articleOffset = 0;
						
						getDatabase().execSQL("DELETE FROM articles;");
					} catch (Exception e) {
						e.printStackTrace();
						updateNotification(getErrorMessage(), 0, 0, false, true);
						downloadFailed();
					}
				}
				
				return content;
			}
			
			@Override
			protected void onPostExecute(JsonElement content) {
				if (content != null) {
					if (m_canProceed) { 
						downloadArticles();
					} else {
						downloadFailed();
					}
				} else {
					updateNotification(getErrorMessage(), 0, 0, false, true);
					downloadFailed();
				}
			}

		};
		
		@SuppressWarnings("serial")
		HashMap<String,String> map = new HashMap<String,String>();
		map.put("op", "getFeeds");
		map.put("sid", m_sessionId);
		map.put("cat_id", "-3");

		if (!BuildConfig.DEBUG && OFFLINE_DEBUG_READ) {
			map.put("unread_only", "true");
		}
		
		req.execute(map);
	}

	private void downloadCategories() {

		updateNotification(R.string.notify_downloading_categories, 0, 0, true, false);
		
		getDatabase().execSQL("DELETE FROM categories;");
		
		ApiRequest req = new ApiRequest(getApplicationContext()) {
			protected JsonElement doInBackground(HashMap<String, String>... params) {
				JsonElement content = super.doInBackground(params);
				
				if (content != null) {
					try {
						Type listType = new TypeToken<List<FeedCategory>>() {}.getType();
						List<FeedCategory> cats = new Gson().fromJson(content, listType);
						
						SQLiteStatement stmtInsert = getDatabase().compileStatement("INSERT INTO categories " +
								"(" + BaseColumns._ID + ", title) " +
								"VALUES (?, ?);");
						
						for (FeedCategory cat : cats) {
							stmtInsert.bindLong(1, cat.id);
							stmtInsert.bindString(2, cat.title);

							stmtInsert.execute();
						}

						stmtInsert.close();

						Log.d(TAG, "offline: done downloading categories");
						
					} catch (Exception e) {
						e.printStackTrace();
						updateNotification(getErrorMessage(), 0, 0, false, true);
						downloadFailed();
					}
				}
			
				return content;
			}
			@Override
			protected void onPostExecute(JsonElement content) {
				if (content != null) {
					if (m_canProceed) { 
						downloadFeeds();
					} else {
						downloadFailed();
					}
				} else {
					updateNotification(getErrorMessage(), 0, 0, false, true);
					downloadFailed();
				}
			}

		};
		
		@SuppressWarnings("serial")
		HashMap<String,String> map = new HashMap<String,String>();
		map.put("op", "getCategories");
		map.put("sid", m_sessionId);
		//put("cat_id", "-3");

		if (!BuildConfig.DEBUG && OFFLINE_DEBUG_READ) {
			map.put("unread_only", "true");
		}
		
		req.execute(map);
	}

	
	@Override
	public void onDestroy() {
		super.onDestroy();
		m_nmgr.cancel(NOTIFY_DOWNLOADING);

		m_canProceed = false;
		Log.d(TAG, "onDestroy");

	}

	public class OfflineArticlesRequest extends ApiRequest {
		List<Article> m_articles;
		
		public OfflineArticlesRequest(Context context) {
			super(context);
		}

		@Override
		protected JsonElement doInBackground(HashMap<String, String>... params) {
			JsonElement content = super.doInBackground(params);
			
			if (content != null) {

				try {
					Type listType = new TypeToken<List<Article>>() {}.getType();
					m_articles = new Gson().fromJson(content, listType);
	
					SQLiteStatement stmtInsert = getDatabase().compileStatement("INSERT INTO articles " +
							"(" + BaseColumns._ID + ", unread, marked, published, score, updated, is_updated, title, link, feed_id, tags, content, author) " +
							"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
	
					for (Article article : m_articles) {
	
						String tagsString = "";
						
						for (String t : article.tags) {
							tagsString += t + ", ";
						}
						
						tagsString = tagsString.replaceAll(", $", "");
						
						int index = 1;
						stmtInsert.bindLong(index++, article.id);
						stmtInsert.bindLong(index++, article.unread ? 1 : 0);
						stmtInsert.bindLong(index++, article.marked ? 1 : 0);
						stmtInsert.bindLong(index++, article.published ? 1 : 0);
						stmtInsert.bindLong(index++, article.score);
						stmtInsert.bindLong(index++, article.updated);
						stmtInsert.bindLong(index++, article.is_updated ? 1 : 0);
						stmtInsert.bindString(index++, article.title);
						stmtInsert.bindString(index++, article.link);
						stmtInsert.bindLong(index++, article.feed_id);
						stmtInsert.bindString(index++, tagsString); // comma-separated tags
						stmtInsert.bindString(index++, article.content);
						stmtInsert.bindString(index++, article.author != null ? article.author : "");
						
						if (m_downloadImages) {
							Document doc = Jsoup.parse(article.content);
							
							if (doc != null) {
								Elements images = doc.select("img,source");
								
								for (Element img : images) {
									String url = img.attr("src");
									
									if (url.indexOf("://") != -1) {
										if (!ImageCacheService.isUrlCached(OfflineDownloadService.this, url)) {										
											Intent intent = new Intent(OfflineDownloadService.this,
													ImageCacheService.class);
										
											intent.putExtra("url", url);
											startService(intent);
										}
									}
								}

								Elements videos = doc.select("video");

								for (Element vid : videos) {
									String url = vid.attr("poster");

									if (url.indexOf("://") != -1) {
										if (!ImageCacheService.isUrlCached(OfflineDownloadService.this, url)) {
											Intent intent = new Intent(OfflineDownloadService.this,
													ImageCacheService.class);

											intent.putExtra("url", url);
											startService(intent);
										}
									}
								}

							}
						}
						
						try {
							stmtInsert.execute();
						} catch (Exception e) {
							e.printStackTrace();
						}
	
					}

					m_articleOffset += m_articles.size();

					Log.d(TAG, "offline: received " + m_articles.size() + " articles; canProc=" + m_canProceed);

					stmtInsert.close();

				} catch (Exception e) {
					updateNotification(R.string.offline_switch_failed, 0, 0, false, true);
					Log.d(TAG, "offline: failed: exception when loading articles");
					e.printStackTrace();
					downloadFailed();
				}
				
			}
			
			return content;
		}
		
		@Override
		protected void onPostExecute(JsonElement content) {
			if (content != null) {
				
				if (m_canProceed && m_articles != null) {
					if (m_articles.size() == OFFLINE_SYNC_SEQ && m_articleOffset < m_syncMax) {
						downloadArticles();
					} else {
						downloadComplete();
					}
				} else {
					downloadFailed();
				}

			} else {
				Log.d(TAG, "offline: failed: " + getErrorMessage());
				updateNotification(getErrorMessage(), 0, 0, false, true);
				downloadFailed();
			}
		}
	}

	@Override
	public void onStart(Intent intent, int startId) {
		try {
			if (getDatabase().isDbLockedByCurrentThread() || getDatabase().isDbLockedByOtherThreads()) {
				return;
			}
			
			m_sessionId = intent.getStringExtra("sessionId");
			m_batchMode = intent.getBooleanExtra("batchMode", false);
		
			if (!m_downloadInProgress) {
				if (m_downloadImages) ImageCacheService.cleanupCache(this, false);
			
				updateNotification(R.string.notify_downloading_init, 0, 0, true, false);
				m_downloadInProgress = true;
		
				downloadCategories();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}