summaryrefslogtreecommitdiff
path: root/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineActivity.java
blob: 185f8fb77909f7bcc1c9e07c49ff175fa84e1f32 (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
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
package org.fox.ttrss.offline;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.BaseColumns;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;

import org.fox.ttrss.CommonActivity;
import org.fox.ttrss.OnlineActivity;
import org.fox.ttrss.PreferencesActivity;
import org.fox.ttrss.R;

import androidx.appcompat.view.ActionMode;
import androidx.appcompat.widget.Toolbar;

public class OfflineActivity extends CommonActivity {
	private final String TAG = this.getClass().getSimpleName();

	protected SharedPreferences m_prefs;
	protected Menu m_menu;
	
	private ActionMode m_headlinesActionMode;
	private HeadlinesActionModeCallback m_headlinesActionModeCallback;

	private String m_lastImageHitTestUrl;

	public String getFeedTitle(int feedId, boolean isCat) {
		try {
			SQLiteStatement stmt;

			if (isCat) {
				stmt = getDatabase().compileStatement(
						"SELECT title FROM categories " + "WHERE " + BaseColumns._ID + " = ?");
			} else {
				stmt = getDatabase().compileStatement(
						"SELECT title FROM feeds " + "WHERE " + BaseColumns._ID + " = ?");
			}

			stmt.bindLong(1, feedId);
			String title = stmt.simpleQueryForString();

			stmt.close();

			return title;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

    @SuppressLint("NewApi")
	private class HeadlinesActionModeCallback implements ActionMode.Callback {
		
		@Override
		public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
			return false;
		}
		
		@Override
		public void onDestroyActionMode(ActionMode mode) {
			m_headlinesActionMode = null;
			deselectAllArticles();
		}
		
		@Override
		public boolean onCreateActionMode(ActionMode mode, Menu menu) {
			
			 MenuInflater inflater = getMenuInflater();
	            inflater.inflate(R.menu.action_mode_headlines, menu);
			
			return true;
		}
		
		@Override
		public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
			onOptionsItemSelected(item);
			return false;
		}
	}

    @Override
	public boolean onContextItemSelected(android.view.MenuItem item) {
		/* AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
				.getMenuInfo(); */

		final OfflineArticlePager ap = (OfflineArticlePager)getSupportFragmentManager().findFragmentByTag(FRAG_ARTICLE);
		
		switch (item.getItemId()) {
		case R.id.article_img_open:
			if (getLastContentImageHitTestUrl() != null) {
				try {
					openUri(Uri.parse(getLastContentImageHitTestUrl()));
				} catch (Exception e) {
					e.printStackTrace();
					toast(R.string.error_other_error);
				}
			}			
			return true;
		case R.id.article_img_copy:
			if (getLastContentImageHitTestUrl() != null) {
				copyToClipboard(getLastContentImageHitTestUrl());
			}			
			return true;
		case R.id.article_img_share:
			if (getLastContentImageHitTestUrl() != null) {
				shareText(getLastContentImageHitTestUrl());
			}
			return true;
		case R.id.article_img_view_caption:
			if (getLastContentImageHitTestUrl() != null) {

				String content = "";
				
				Cursor article = getArticleById(ap.getSelectedArticleId());
				
				if (article != null) {
					content = article.getString(article.getColumnIndex("content"));
					article.close();
				}

				displayImageCaption(getLastContentImageHitTestUrl(), content);
            }
            return true;
		default:
			Log.d(TAG, "onContextItemSelected, unhandled id=" + item.getItemId());
			return super.onContextItemSelected(item);
		}
		
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		m_prefs = PreferenceManager
				.getDefaultSharedPreferences(getApplicationContext());

		setAppTheme(m_prefs);

		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_login);

		Toolbar toolbar = findViewById(R.id.toolbar);
		setSupportActionBar(toolbar);

		invalidateOptionsMenu();

		Intent intent = getIntent();
		
		if (intent.getExtras() != null) {
			if (intent.getBooleanExtra("initial", false)) {
				intent = new Intent(OfflineActivity.this, OfflineMasterActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
		 	   
				startActivityForResult(intent, 0);
				finish();
			}
		}
		
		/* if (savedInstanceState != null) {

		} */

		m_headlinesActionModeCallback = new HeadlinesActionModeCallback();
	}
	
	@Override
	public void onSaveInstanceState(Bundle out) {
		super.onSaveInstanceState(out);
	}
	
	protected void selectArticles(int feedId, boolean isCat, int mode) {
		switch (mode) {
		case 0:
			SQLiteStatement stmtSelectAll = null;
			
			if (isCat) {
				stmtSelectAll = getDatabase().compileStatement(
						"UPDATE articles SET selected = 1 WHERE feed_id IN (SELECT "+BaseColumns._ID+" FROM feeds WHERE cat_id = ?)");
			} else {
				stmtSelectAll = getDatabase().compileStatement(
								"UPDATE articles SET selected = 1 WHERE feed_id = ?");
			}
			
			stmtSelectAll.bindLong(1, feedId);
			stmtSelectAll.execute();
			stmtSelectAll.close();

			break;
		case 1:

			SQLiteStatement stmtSelectUnread = null;
			
			if (isCat) {
				stmtSelectUnread = getDatabase().compileStatement(
						"UPDATE articles SET selected = 1 WHERE feed_id IN (SELECT "+BaseColumns._ID+" FROM feeds WHERE cat_id = ?) AND unread = 1");
			} else {
				stmtSelectUnread = getDatabase().compileStatement(
								"UPDATE articles SET selected = 1 WHERE feed_id = ? AND unread = 1");
			}
			
			stmtSelectUnread.bindLong(1, feedId);
			stmtSelectUnread.execute();
			stmtSelectUnread.close();

			break;
		case 2:
			deselectAllArticles();
			break;
		}
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {		
		final OfflineHeadlinesFragment ohf = (OfflineHeadlinesFragment) getSupportFragmentManager()
				.findFragmentByTag(FRAG_HEADLINES);

		/* final OfflineFeedsFragment off = (OfflineFeedsFragment) getSupportFragmentManager()
				.findFragmentByTag(FRAG_FEEDS); */
		
		/* final OfflineFeedCategoriesFragment ocf = (OfflineFeedCategoriesFragment) getSupportFragmentManager()
				.findFragmentByTag(FRAG_CATS); */

		final OfflineArticlePager oap = (OfflineArticlePager) getSupportFragmentManager()
				.findFragmentByTag(FRAG_ARTICLE);

		switch (item.getItemId()) {
		/* case android.R.id.home:
			finish();
			return true; */
		/* case R.id.headlines_toggle_sidebar:
			if (true && !isSmallScreen()) {
				SharedPreferences.Editor editor = m_prefs.edit();
				editor.putBoolean("headlines_hide_sidebar", !m_prefs.getBoolean("headlines_hide_sidebar", false));
				editor.commit();
				
				if (ohf != null && ohf.isAdded()) {
					ohf.getView().setVisibility(m_prefs.getBoolean("headlines_hide_sidebar", false) ? View.GONE : View.VISIBLE);
				}
			}
			return true; */
		/*case R.id.go_online:
			switchOnline();
			return true;*/
		case R.id.search:
			if (ohf != null) {
				Dialog dialog = new Dialog(this);

				final EditText edit = new EditText(this);

				AlertDialog.Builder builder = new AlertDialog.Builder(this)
						.setTitle(R.string.search)
						.setPositiveButton(getString(R.string.search),
								new OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										
										String query = edit.getText().toString().trim();
										
										ohf.setSearchQuery(query);

									}
								})
						.setNegativeButton(getString(R.string.cancel),
								new OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										
										//

									}
								}).setView(edit);
				
				dialog = builder.create();
				dialog.show();
			}
			
			return true;
		case R.id.preferences:
			Intent intent = new Intent(this, PreferencesActivity.class);
			startActivityForResult(intent, 0);
			return true;
		case R.id.headlines_view_mode:
			if (ohf != null) {
				Dialog dialog = new Dialog(this);
				
				String viewMode = getViewMode();
				
				//Log.d(TAG, "viewMode:" + getViewMode());

				int selectedIndex = 0;
				
				if (viewMode.equals("all_articles")) {
					selectedIndex = 0;
				} else if (viewMode.equals("marked")) {
					selectedIndex = 1;
				} else if (viewMode.equals("published")) {
					selectedIndex = 2;
				} else if (viewMode.equals("unread")) {
					selectedIndex = 3;
				}
				
				AlertDialog.Builder builder = new AlertDialog.Builder(this)
						.setTitle(R.string.headlines_set_view_mode)
						.setSingleChoiceItems(
								new String[] {
										/* getString(R.string.headlines_adaptive), */
										getString(R.string.headlines_all_articles),
										getString(R.string.headlines_starred),
										getString(R.string.headlines_published),
										getString(R.string.headlines_unread) },
								selectedIndex, new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										switch (which) {
										/* case 0:
											setViewMode("adaptive");
											break; */
										case 0:
											setViewMode("all_articles");
											break;
										case 1:
											setViewMode("marked");
											break;
										case 2:
											setViewMode("published");
											break;
										case 3:
											setViewMode("unread");
											break;
										}
										dialog.cancel();

										refresh();
									}
								});

				dialog = builder.create();
				dialog.show();

			}
			return true;
		case R.id.headlines_select:
			if (ohf != null) {
				Dialog dialog = new Dialog(this);
				AlertDialog.Builder builder = new AlertDialog.Builder(this);
				builder.setTitle(R.string.headlines_select_dialog);

				builder.setSingleChoiceItems(new String[] {
						getString(R.string.headlines_select_all),
						getString(R.string.headlines_select_unread),
						getString(R.string.headlines_select_none) }, 0,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {

								selectArticles(ohf.getFeedId(), ohf.getFeedIsCat(), which);
								invalidateOptionsMenu();
								refresh();

								dialog.cancel();
							}
						});

				dialog = builder.create();
				dialog.show();
			}
			return true;
		case R.id.headlines_mark_as_read:
			if (ohf != null) {
				final int feedId = ohf.getFeedId();
				final boolean isCat = ohf.getFeedIsCat();
				
				int count = getUnreadArticleCount(feedId, isCat);
				boolean confirm = m_prefs.getBoolean("confirm_headlines_catchup", true);
				
				if (count > 0) {
					if (confirm) {
						AlertDialog.Builder builder = new AlertDialog.Builder(
								OfflineActivity.this)
								.setMessage(getResources().getQuantityString(R.plurals.mark_num_headlines_as_read, count, count))
								.setPositiveButton(R.string.catchup,
										new Dialog.OnClickListener() {
											public void onClick(DialogInterface dialog,
													int which) {
	
												catchupFeed(feedId, isCat);											
												
											}
										})
								.setNegativeButton(R.string.dialog_cancel,
										new Dialog.OnClickListener() {
											public void onClick(DialogInterface dialog,
													int which) {
		
											}
										});
		
						AlertDialog dlg = builder.create();
						dlg.show();					
					
						
					} else {
						catchupFeed(feedId, isCat);
					}				
				}
			}
			return true;
		/* case R.id.share_article:
			if (true) {
				int articleId = oap.getSelectedArticleId();
				
				shareArticle(articleId);
			}
			return true; */
		case R.id.toggle_marked:
			if (oap != null) {
				int articleId = oap.getSelectedArticleId();
				
				SQLiteStatement stmt = getDatabase().compileStatement(
						"UPDATE articles SET modified = 1, modified_marked = 1, marked = NOT marked WHERE "
								+ BaseColumns._ID + " = ?");
				stmt.bindLong(1, articleId);
				stmt.execute();
				stmt.close();
				
				refresh();
			}
			return true;
		case R.id.toggle_unread:
			if (oap != null) {
				int articleId = oap.getSelectedArticleId();

				SQLiteStatement stmt = getDatabase().compileStatement(
						"UPDATE articles SET modified = 1, unread = NOT unread WHERE "
								+ BaseColumns._ID + " = ?");
				stmt.bindLong(1, articleId);
				stmt.execute();
				stmt.close();

				refresh();
			}
			return true;
		/* case R.id.selection_select_none:
			deselectAllArticles();			
			return true; */
		case R.id.selection_toggle_unread:
			if (getSelectedArticleCount() > 0) {
				SQLiteStatement stmt = getDatabase()
						.compileStatement(
								"UPDATE articles SET modified = 1, unread = NOT unread WHERE selected = 1");
				stmt.execute();
				stmt.close();
				
				refresh();
			}
			return true;
		case R.id.selection_toggle_marked:
			if (getSelectedArticleCount() > 0) {
				SQLiteStatement stmt = getDatabase()
						.compileStatement(
								"UPDATE articles SET modified = 1, modified_marked = 1, marked = NOT marked WHERE selected = 1");
				stmt.execute();
				stmt.close();
				
				refresh();
			}
			return true;
		case R.id.selection_toggle_published:
			if (getSelectedArticleCount() > 0) {
				SQLiteStatement stmt = getDatabase()
						.compileStatement(
								"UPDATE articles SET modified = 1, modified_published = 1, published = NOT published WHERE selected = 1");
				stmt.execute();
				stmt.close();
				
				refresh();
			}
			return true;
		case R.id.toggle_published:
			if (oap != null) {
				int articleId = oap.getSelectedArticleId();
				
				SQLiteStatement stmt = getDatabase().compileStatement(
						"UPDATE articles SET modified = 1, modified_published = 1, published = NOT published WHERE "
								+ BaseColumns._ID + " = ?");
				stmt.bindLong(1, articleId);
				stmt.execute();
				stmt.close();
				
				refresh();
			}
			return true;
		case R.id.catchup_above:
			if (oap != null) {
				if (m_prefs.getBoolean("confirm_headlines_catchup", true)) {

					AlertDialog.Builder builder = new AlertDialog.Builder(
							OfflineActivity.this)
							.setMessage(R.string.confirm_catchup_above)
							.setPositiveButton(R.string.dialog_ok,
									new Dialog.OnClickListener() {
										public void onClick(DialogInterface dialog,
															int which) {

											catchupAbove(oap);

										}
									})
							.setNegativeButton(R.string.dialog_cancel,
									new Dialog.OnClickListener() {
										public void onClick(DialogInterface dialog,
															int which) {

										}
									});

					AlertDialog dlg = builder.create();
					dlg.show();
				} else {
					catchupAbove(oap);
				}
			}
			return true;
		default:
			Log.d(TAG, "onOptionsItemSelected, unhandled id=" + item.getItemId());
			return super.onOptionsItemSelected(item);
		}
	}

	private void catchupAbove(OfflineArticlePager oap) {
		int articleId = oap.getSelectedArticleId();
		int feedId = oap.getFeedId();
		boolean isCat = oap.getFeedIsCat();

		SQLiteStatement stmt = null;

		if (isCat) {
            stmt = getDatabase().compileStatement(
                    "UPDATE articles SET modified = 1, unread = 0 WHERE " +
                    "updated >= (SELECT updated FROM articles WHERE " + BaseColumns._ID + " = ?) " +
                    "AND feed_id IN (SELECT "+BaseColumns._ID+" FROM feeds WHERE cat_id = ?)");
        } else {
            stmt = getDatabase().compileStatement(
                    "UPDATE articles SET modified = 1, unread = 0 WHERE " +
                    "updated >= (SELECT updated FROM articles WHERE " + BaseColumns._ID + " = ?) " +
                    "AND feed_id = ?");
        }

		stmt.bindLong(1, articleId);
		stmt.bindLong(2, feedId);
		stmt.execute();
		stmt.close();

		refresh();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.activity_offline, menu);

		m_menu = menu;

		initMenu();
		
		return true;
	}
	
	@SuppressLint("NewApi")
	protected void initMenu() {
		if (m_menu != null) {
			m_menu.setGroupVisible(R.id.menu_group_headlines, false);
			m_menu.setGroupVisible(R.id.menu_group_article, false);
			m_menu.setGroupVisible(R.id.menu_group_feeds, false);

			OfflineHeadlinesFragment hf = (OfflineHeadlinesFragment) getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES);

			if (hf != null) {
				if (hf.getSelectedArticleCount() > 0 && m_headlinesActionMode == null) {
					m_headlinesActionMode = startSupportActionMode(m_headlinesActionModeCallback);
				} else if (hf.getSelectedArticleCount() == 0 && m_headlinesActionMode != null) { 
					m_headlinesActionMode.finish();
				}
			}
			
			OfflineArticlePager ap = (OfflineArticlePager) getSupportFragmentManager().findFragmentByTag(FRAG_ARTICLE);
			
			if (ap != null) {
				int articleId = ap.getSelectedArticleId();
				
				Cursor article = getArticleById(articleId);
				
				if (article != null) {
					boolean unread = article.getInt(article.getColumnIndex("unread")) == 1;
					boolean marked = article.getInt(article.getColumnIndex("marked")) == 1;
					boolean published = article.getInt(article.getColumnIndex("published")) == 1;

					m_menu.findItem(R.id.toggle_marked).setIcon(marked ? R.drawable.ic_star :
						R.drawable.ic_star_outline);

					m_menu.findItem(R.id.toggle_published).setIcon(published ? R.drawable.ic_checkbox_marked :
						R.drawable.ic_rss_box);

					m_menu.findItem(R.id.toggle_unread).setIcon(unread ? R.drawable.ic_email :
							R.drawable.ic_email_open);

					article.close();
				}				
			}
		}
	}
	
	public void switchOnline() {
		SharedPreferences localPrefs = getSharedPreferences("localprefs", Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = localPrefs.edit();
		editor.putBoolean("offline_mode_active", false);
		editor.apply();

		Intent refresh = new Intent(this, OnlineActivity.class);
		refresh.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_CLEAR_TASK |
				Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

		startActivity(refresh);

		finish();
	}
	
	protected Cursor getArticleById(int articleId) {
		Cursor c = getDatabase().query("articles", null,
				BaseColumns._ID + "=?",
				new String[]{String.valueOf(articleId)}, null, null, null);

		c.moveToFirst();

		return c;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {		
		if (m_prefs.getBoolean("use_volume_keys", false)) {
			OfflineArticlePager ap = (OfflineArticlePager) getSupportFragmentManager().findFragmentByTag(FRAG_ARTICLE);
			
			if (ap != null && ap.isAdded()) {			
				switch (keyCode) {
				case KeyEvent.KEYCODE_VOLUME_UP:
					ap.selectArticle(false);					
					return true;
				case KeyEvent.KEYCODE_VOLUME_DOWN:
					ap.selectArticle(true);
					return true;
				}
			}
		}
		
		return super.onKeyDown(keyCode, event);			
	}
	
	// Handle onKeyUp too to suppress beep
	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if (m_prefs.getBoolean("use_volume_keys", false)) {
					
			switch (keyCode) {
			case KeyEvent.KEYCODE_VOLUME_UP:	
			case KeyEvent.KEYCODE_VOLUME_DOWN:
				return true;
			}
		}
		
		return super.onKeyUp(keyCode, event);		
	}
	
	protected Cursor getFeedById(int feedId) {
		Cursor c = getDatabase().query("feeds", null,
				BaseColumns._ID + "=?",
				new String[]{String.valueOf(feedId)}, null, null, null);

		c.moveToFirst();

		return c;
	}

	protected Cursor getCatById(int catId) {
		Cursor c = getDatabase().query("categories", null,
				BaseColumns._ID + "=?",
				new String[]{String.valueOf(catId)}, null, null, null);

		c.moveToFirst();

		return c;
	}

	protected Intent getShareIntent(Cursor article) {
		if (article != null) {
			String title = article.getString(article.getColumnIndex("title"));
			String link = article.getString(article.getColumnIndex("link"));

			return getShareIntent(link, title);
		} else {
			return null;
		}
	}
	
	protected void shareArticle(int articleId) {

		Cursor article = getArticleById(articleId);

		if (article != null) {
			shareArticle(article);
			article.close();
		}
	}

	private void shareArticle(Cursor article) {
		if (article != null) {
			Intent intent = getShareIntent(article);
			
			startActivity(Intent.createChooser(intent,
					getString(R.string.share_article)));
		}
	}

	protected int getSelectedArticleCount() {
		Cursor c = getDatabase().query("articles",
				new String[]{"COUNT(*)"}, "selected = 1", null, null, null,
				null);
		c.moveToFirst();
		int selected = c.getInt(0);
		c.close();

		return selected;
	}

	protected int getUnreadArticleCount(int feedId, boolean isCat) {
		
		Cursor c;
		
		if (isCat) {
			c = getDatabase().query("articles",
				new String[] { "COUNT(*)" }, "unread = 1 AND feed_id IN (SELECT "+BaseColumns._ID+" FROM feeds WHERE cat_id = ?)", 
				new String[] { String.valueOf(feedId) },
				null, null, null);
		} else {
			c = getDatabase().query("articles",
				new String[] { "COUNT(*)" }, "unread = 1 AND feed_id = ?", 
				new String[] { String.valueOf(feedId) }, 
				null, null, null);
		}
		
		c.moveToFirst();
		int selected = c.getInt(0);
		c.close();

		return selected;
	}
	
	protected void deselectAllArticles() {
		getDatabase().execSQL("UPDATE articles SET selected = 0 ");
		refresh();
	}

	protected void refresh() {
		OfflineFeedsFragment ff = (OfflineFeedsFragment) getSupportFragmentManager()
				.findFragmentByTag(FRAG_FEEDS);

		if (ff != null) {
			ff.refresh();
		}

		OfflineFeedCategoriesFragment cf = (OfflineFeedCategoriesFragment) getSupportFragmentManager()
				.findFragmentByTag(FRAG_CATS);

		if (cf != null) {
			cf.refresh();
		}

		OfflineHeadlinesFragment ohf = (OfflineHeadlinesFragment) getSupportFragmentManager()
				.findFragmentByTag(FRAG_HEADLINES);

		if (ohf != null) {
			ohf.refresh();		
		}
		
		invalidateOptionsMenu();
	}

	public void catchupFeed(int feedId, boolean isCat) {
		if (isCat) {
			SQLiteStatement stmt = getDatabase().compileStatement(
					"UPDATE articles SET modified = 1, unread = 0 WHERE feed_id IN (SELECT " +
							BaseColumns._ID + " FROM feeds WHERE cat_id = ?)");
			stmt.bindLong(1, feedId);
			stmt.execute();
			stmt.close();
		} else {
			SQLiteStatement stmt = getDatabase().compileStatement(
					"UPDATE articles SET modified = 1, unread = 0 WHERE feed_id = ?");
			stmt.bindLong(1, feedId);
			stmt.execute();
			stmt.close();			
		}
		
		refresh();
	}

	public void setLastContentImageHitTestUrl(String url) {
		m_lastImageHitTestUrl = url;		
	}
	
	public String getLastContentImageHitTestUrl() {
		return m_lastImageHitTestUrl;
	}

	public void setViewMode(String viewMode) {
		SharedPreferences.Editor editor = m_prefs.edit();
		editor.putString("offline_view_mode", viewMode);
		editor.apply();
	}

    /* public boolean getOldestFirst() {
        return getSortMode().equals("oldest_first");
    }

    public String getSortMode() {
        return m_prefs.getString("offline_headlines_sort_mode", "default");
    }

    public void setSortMode(String sortMode) {
        SharedPreferences.Editor editor = m_prefs.edit();
        editor.putString("offline_headlines_sort_mode", sortMode);
        editor.commit();
    } */

	public String getViewMode() {		
		return m_prefs.getString("offline_view_mode", "adaptive");
	}

}