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

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.res.Resources.Theme;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.BaseColumns;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.Html;
import android.util.Log;
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.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.amulyakhare.textdrawable.TextDrawable;
import com.amulyakhare.textdrawable.util.ColorGenerator;
import com.nhaarman.listviewanimations.appearance.AnimationAdapter;
import com.nhaarman.listviewanimations.appearance.simple.SwingBottomInAnimationAdapter;
import com.shamanland.fab.FloatingActionButton;

import org.fox.ttrss.Application;
import org.fox.ttrss.CommonActivity;
import org.fox.ttrss.R;
import org.jsoup.Jsoup;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.TimeZone;

public class OfflineHeadlinesFragment extends Fragment implements OnItemClickListener, AbsListView.OnScrollListener {
	public enum ArticlesSelection { ALL, NONE, UNREAD }

    private final String TAG = this.getClass().getSimpleName();
	
	private int m_feedId;
	private boolean m_feedIsCat = false;
	private int m_activeArticleId;
	private String m_searchQuery = "";
	
	private SharedPreferences m_prefs;
    private ArrayList<Integer> m_readArticleIds = new ArrayList<Integer>();
	
	private Cursor m_cursor;
	private ArticleListAdapter m_adapter;
	private AnimationAdapter m_animationAdapter;
	
	private OfflineHeadlinesEventListener m_listener;
	private OfflineActivity m_activity;
	private SwipeRefreshLayout m_swipeLayout;

    private boolean m_compactLayoutMode = false;
    private ListView m_list;
    private int m_listPreviousVisibleItem;
	
	public void initialize(int feedId, boolean isCat, boolean compactMode) {
		m_feedId = feedId;
		m_feedIsCat = isCat;
        m_compactLayoutMode = compactMode;
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		
		if (m_cursor != null && !m_cursor.isClosed()) m_cursor.close();
	}
	
	public int getSelectedArticleCount() {
		Cursor c = m_activity.getDatabase().query("articles", 
				new String[] { "COUNT(*)" }, "selected = 1", null, null, null, null);
		c.moveToFirst();
		int selected = c.getInt(0);
		c.close();
		
		return selected;
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
									ContextMenuInfo menuInfo) {

		getActivity().getMenuInflater().inflate(R.menu.context_headlines, menu);

		menu.findItem(R.id.set_labels).setVisible(false);
		menu.findItem(R.id.article_set_note).setVisible(false);
		menu.findItem(R.id.headlines_article_unread).setVisible(false); // TODO: implement

		if (m_prefs.getBoolean("offline_sort_by_feed", false)) {
			menu.findItem(R.id.catchup_above).setVisible(false);
		}

		super.onCreateContextMenu(menu, v, menuInfo);
	}

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

		int articleId = getArticleIdAtPosition(info.position);

		if (!onArticleMenuItemSelected(item, articleId))
			return super.onContextItemSelected(item);
		else
			return true;
	}

	private boolean onArticleMenuItemSelected(MenuItem item, final int articleId) {
		switch (item.getItemId()) {
			case R.id.headlines_article_link_copy:
				if (true) {
					Cursor article = m_activity.getArticleById(articleId);

					if (article != null) {
						m_activity.copyToClipboard(article.getString(article.getColumnIndex("link")));
						article.close();
					}
				}
				return true;
			case R.id.headlines_article_link_open:
				if (true) {
					Cursor article = m_activity.getArticleById(articleId);

					if (article != null) {
						m_activity.openUri(Uri.parse(article.getString(article.getColumnIndex("link"))));

						// TODO: mark article as read, set modified = 1, refresh

						article.close();
					}
				}
				return true;
			case R.id.headlines_share_article:
				m_activity.shareArticle(articleId);
				return true;
			case R.id.catchup_above:
				if (true) {
					if (m_prefs.getBoolean("confirm_headlines_catchup", true)) {

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

												catchupAbove(articleId);

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

											}
										});

						AlertDialog dlg = builder.create();
						dlg.show();
					} else {
						catchupAbove(articleId);
					}

				}
				return true;
			default:
				Log.d(TAG, "onArticleMenuItemSelected, unhandled id=" + item.getItemId());
				return false;
		}

	}

	private void catchupAbove(int articleId) {
		SQLiteStatement stmt = null;

		String updatedOperator = (m_prefs.getBoolean("offline_oldest_first", false)) ? "<" : ">";

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

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

		refresh();
	}

	@Override
	public void onResume() {
		super.onResume();
		
		if (Application.getInstance().m_selectedArticleId != 0) {
			m_activeArticleId = Application.getInstance().m_selectedArticleId;
			Application.getInstance().m_selectedArticleId = 0;
		}

		if (m_activeArticleId != 0) {
			setActiveArticleId(m_activeArticleId);
		}

		refresh();
		
		m_activity.invalidateOptionsMenu();
	}
	
	public void refresh() {
		try {
			if (!isAdded()) return;

            if (m_swipeLayout != null) m_swipeLayout.setRefreshing(true);
			
			if (m_cursor != null && !m_cursor.isClosed()) m_cursor.close();
			
			m_cursor = createCursor();
			
			if (m_cursor != null && m_adapter != null) {
				m_adapter.changeCursor(m_cursor);
				m_adapter.notifyDataSetChanged();
			}

            if (m_swipeLayout != null) m_swipeLayout.setRefreshing(false);
			
		} catch (NullPointerException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {    	
		
		if (savedInstanceState != null) {
			m_feedId = savedInstanceState.getInt("feedId");
			m_activeArticleId = savedInstanceState.getInt("activeArticleId");
			//m_selectedArticles = savedInstanceState.getParcelableArrayList("selectedArticles");
			m_searchQuery = (String) savedInstanceState.getCharSequence("searchQuery");
			m_feedIsCat = savedInstanceState.getBoolean("feedIsCat");
            m_compactLayoutMode = savedInstanceState.getBoolean("compactLayoutMode");
		} else {
			m_activity.getDatabase().execSQL("UPDATE articles SET selected = 0 ");
		}

		String headlineMode = m_prefs.getString("headline_mode", "HL_DEFAULT");

		if ("HL_COMPACT".equals(headlineMode) || "HL_COMPACT_NOIMAGES".equals(headlineMode))
			m_compactLayoutMode = true;

		View view = inflater.inflate(R.layout.fragment_headlines, container, false);

		m_swipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.headlines_swipe_container);
		
	    m_swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
			@Override
			public void onRefresh() {
				refresh();
			}
		});

		m_cursor = createCursor();
		
		m_list = (ListView)view.findViewById(R.id.headlines_list);

		FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.headlines_fab);
		fab.setVisibility(View.GONE);

        if (m_activity.isSmallScreen()) {
            View layout = inflater.inflate(R.layout.headlines_heading_spacer, m_list, false);
            m_list.addHeaderView(layout);

            m_swipeLayout.setProgressViewOffset(false, 0,
                    m_activity.getResources().getDimensionPixelSize(R.dimen.abc_action_bar_default_height_material) +
                            m_activity.getResources().getDimensionPixelSize(R.dimen.abc_action_bar_default_padding_end_material));
        }

        if (m_prefs.getBoolean("headlines_mark_read_scroll", false)) {
            WindowManager wm = (WindowManager) m_activity.getSystemService(Context.WINDOW_SERVICE);
            Display display = wm.getDefaultDisplay();
            int screenHeight = display.getHeight();

            View layout = inflater.inflate(R.layout.headlines_footer, container, false);

            layout.setLayoutParams(new ListView.LayoutParams(ListView.LayoutParams.MATCH_PARENT, screenHeight));

            m_list.addFooterView(layout, null, false);
        }


        m_adapter = new ArticleListAdapter(getActivity(), R.layout.headlines_row, m_cursor,
				new String[] { "title" }, new int[] { R.id.title }, 0);
		m_animationAdapter = new SwingBottomInAnimationAdapter(m_adapter);

		m_animationAdapter.setAbsListView(m_list);
		m_list.setAdapter(m_animationAdapter);

		m_list.setOnItemClickListener(this);
        m_list.setOnScrollListener(this);
		registerForContextMenu(m_list);

		return view;    	
	}

	public Cursor createCursor() {
		String feedClause = null;
		
		if (m_feedIsCat) {
			feedClause = "feed_id IN (SELECT "+BaseColumns._ID+" FROM feeds WHERE cat_id = ?)";
		} else {
			feedClause = "feed_id = ?";
		}
		
		String viewMode = m_activity.getViewMode();
		
		if ("adaptive".equals(viewMode)) {
			// TODO: implement adaptive			
		} else if ("marked".equals(viewMode)) {
			feedClause += "AND (marked = 1)";
		} else if ("published".equals(viewMode)) {
			feedClause += "AND (published = 1)";
		} else if ("unread".equals(viewMode)) {
			feedClause += "AND (unread = 1)";
		} else { // all_articles
			//
		}
		
		String orderBy = (m_prefs.getBoolean("offline_oldest_first", false)) ? "updated" : "updated DESC";

		if (m_prefs.getBoolean("offline_sort_by_feed", false)) {
			orderBy = "feed_title, " + orderBy;
		}

		if (m_searchQuery == null || m_searchQuery.equals("")) {
			return m_activity.getDatabase().query("articles LEFT JOIN feeds ON (feed_id = feeds."+BaseColumns._ID+")", 
					new String[] { "articles.*", "feeds.title AS feed_title" }, feedClause, 
					new String[] { String.valueOf(m_feedId) }, null, null, orderBy);
		} else {
			return m_activity.getDatabase().query("articles LEFT JOIN feeds ON (feed_id = feeds."+BaseColumns._ID+")", 
					new String[] { "articles.*", "feeds.title AS feed_title" },
					feedClause + " AND (articles.title LIKE '%' || ? || '%' OR content LIKE '%' || ? || '%')", 
					new String[] { String.valueOf(m_feedId), m_searchQuery, m_searchQuery }, null, null, orderBy);
		}
	}
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		m_listener = (OfflineHeadlinesEventListener) activity;
		m_activity = (OfflineActivity) activity;
		
		m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
	}

	@Override
	public void onItemClick(AdapterView<?> av, View view, int position, long id) {
		ListView list = (ListView)av;
		
		Log.d(TAG, "onItemClick=" + position);
		
		if (list != null) {

			int articleId = getArticleIdAtPosition(position);

            if (articleId != 0) {

                if (getActivity().findViewById(R.id.article_fragment) != null) {
                    m_activeArticleId = articleId;
                }

                m_listener.onArticleSelected(articleId);

                refresh();
            }
		}
	}

	@Override
	public void onSaveInstanceState (Bundle out) {
		super.onSaveInstanceState(out);
		
		out.putInt("feedId", m_feedId);
		out.putInt("activeArticleId", m_activeArticleId);
		//out.putParcelableArrayList("selectedArticles", m_selectedArticles);
		out.putCharSequence("searchQuery", m_searchQuery);
		out.putBoolean("feedIsCat", m_feedIsCat);

        out.putBoolean("compactLayoutMode", m_compactLayoutMode);
	}

	/* public void setLoadingStatus(int status, boolean showProgress) {
		if (getView() != null) {
			TextView tv = (TextView)getView().findViewById(R.id.loading_message);
			
			if (tv != null) {
				tv.setText(status);
			}	
		}
		
		getActivity().setProgressBarIndeterminateVisibility(showProgress);
	} */

    static class HeadlineViewHolder {
        public TextView titleView;
        public TextView feedTitleView;
        public ImageView markedView;
        public ImageView publishedView;
        public TextView excerptView;
        public ImageView flavorImageView;
        public TextView authorView;
        public TextView dateView;
        public CheckBox selectionBoxView;
        public ImageView menuButtonView;
        public ViewGroup flavorImageHolder;
        public ProgressBar flavorImageLoadingBar;
        public View headlineFooter;
        public ImageView textImage;
        public ImageView textChecked;
		public ImageView flavorVideoKindView;
		public View flavorImageOverflow;
		public View headlineHeader;
	}

    private class ArticleListAdapter extends SimpleCursorAdapter {
		public static final int VIEW_NORMAL = 0;
		public static final int VIEW_UNREAD = 1;
		public static final int VIEW_SELECTED = 2;
		public static final int VIEW_SELECTED_UNREAD = 3;
		public static final int VIEW_LOADMORE = 4;
		
		public static final int VIEW_COUNT = VIEW_LOADMORE+1;
		
		private final Integer[] origTitleColors = new Integer[VIEW_COUNT];
		private final int titleHighScoreUnreadColor;

        private ColorGenerator m_colorGenerator = ColorGenerator.DEFAULT;
        private TextDrawable.IBuilder m_drawableBuilder = TextDrawable.builder().round();
		
		public ArticleListAdapter(Context context, int layout, Cursor c,
				String[] from, int[] to, int flags) {
			super(context, layout, c, from, to, flags);
			
			Theme theme = context.getTheme();
			TypedValue tv = new TypedValue();
			theme.resolveAttribute(R.attr.headlineTitleHighScoreUnreadTextColor, tv, true);
			titleHighScoreUnreadColor = tv.data;
		}

		public int getViewTypeCount() {
			return VIEW_COUNT;
		}

		@Override
		public int getItemViewType(int position) {
			Cursor c = (Cursor) getItem(position);
			
			//Log.d(TAG, "@gIVT " + position + " " + c.getInt(0) + " vs " + m_activeArticleId);
			
			if (c.getInt(0) == m_activeArticleId && c.getInt(c.getColumnIndex("unread")) == 1) {				
				return VIEW_SELECTED_UNREAD;
			} else if (c.getInt(0) == m_activeArticleId) {
				return VIEW_SELECTED;
			} else if (c.getInt(c.getColumnIndex("unread")) == 1) {
				return VIEW_UNREAD;
			} else {
				return VIEW_NORMAL;				
			}			
		}

        private void updateTextCheckedState(HeadlineViewHolder holder, Cursor item) {
            String title = item.getString(item.getColumnIndex("title"));

            String tmp = title.length() > 0 ? title.substring(0, 1) : "?";

            boolean isChecked = item.getInt(item.getColumnIndex("selected")) == 1;

            if (isChecked) {
                holder.textImage.setImageDrawable(m_drawableBuilder.build(" ", 0xff616161));

                holder.textChecked.setVisibility(View.VISIBLE);
            } else {
                holder.textImage.setImageDrawable(m_drawableBuilder.build(tmp, m_colorGenerator.getColor(title)));

                holder.textChecked.setVisibility(View.GONE);
            }
        }

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			View v = convertView;

			final Cursor article = (Cursor)getItem(position);

            final HeadlineViewHolder holder;

			final int articleId = article.getInt(0);
			
			int headlineFontSize = Integer.parseInt(m_prefs.getString("headlines_font_size_sp", "13"));
			int headlineSmallFontSize = Math.max(10, Math.min(18, headlineFontSize - 2));
			
			if (v == null) {
                int layoutId = m_compactLayoutMode ? R.layout.headlines_row_compact : R.layout.headlines_row;

                switch (getItemViewType(position)) {
                    case VIEW_LOADMORE:
                        layoutId = R.layout.headlines_row_loadmore;
                        break;
                    case VIEW_UNREAD:
                        layoutId = m_compactLayoutMode ? R.layout.headlines_row_unread_compact : R.layout.headlines_row_unread;
                        break;
                    case VIEW_SELECTED:
                        layoutId = m_compactLayoutMode ? R.layout.headlines_row_selected_compact : R.layout.headlines_row;
                        break;
                    case VIEW_SELECTED_UNREAD:
                        layoutId = m_compactLayoutMode ? R.layout.headlines_row_selected_unread_compact : R.layout.headlines_row_unread;
                        break;
                }

                LayoutInflater vi = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(layoutId, null);

                holder = new HeadlineViewHolder();
                holder.titleView = (TextView)v.findViewById(R.id.title);

                holder.feedTitleView = (TextView)v.findViewById(R.id.feed_title);
                holder.markedView = (ImageView)v.findViewById(R.id.marked);
                holder.publishedView = (ImageView)v.findViewById(R.id.published);
                holder.excerptView = (TextView)v.findViewById(R.id.excerpt);
                holder.flavorImageView = (ImageView) v.findViewById(R.id.flavor_image);
                holder.authorView = (TextView)v.findViewById(R.id.author);
                holder.dateView = (TextView) v.findViewById(R.id.date);
                holder.selectionBoxView = (CheckBox) v.findViewById(R.id.selected);
                holder.menuButtonView = (ImageView) v.findViewById(R.id.article_menu_button);
                holder.flavorImageHolder = (ViewGroup) v.findViewById(R.id.flavorImageHolder);
                holder.flavorImageLoadingBar = (ProgressBar) v.findViewById(R.id.flavorImageLoadingBar);
                holder.headlineFooter = v.findViewById(R.id.headline_footer);
                holder.textImage = (ImageView) v.findViewById(R.id.text_image);
                holder.textChecked = (ImageView) v.findViewById(R.id.text_checked);
				holder.flavorVideoKindView = (ImageView) v.findViewById(R.id.flavor_video_kind);
				holder.headlineHeader = v.findViewById(R.id.headline_header);
				holder.flavorImageOverflow = v.findViewById(R.id.flavor_image_overflow);

                v.setTag(holder);

                // http://code.google.com/p/android/issues/detail?id=3414
                ((ViewGroup)v).setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
            } else {
                holder = (HeadlineViewHolder) v.getTag();
            }

            // block footer clicks to make button/selection clicking easier
            if (holder.headlineFooter != null) {
                holder.headlineFooter.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //
                    }
                });
            }

            if (holder.textImage != null) {
                updateTextCheckedState(holder, article);

                holder.textImage.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Log.d(TAG, "textImage : onclicked");

                        SQLiteStatement stmtUpdate = m_activity.getDatabase().compileStatement("UPDATE articles SET selected = NOT selected " +
                                "WHERE " + BaseColumns._ID + " = ?");

                        stmtUpdate.bindLong(1, articleId);
                        stmtUpdate.execute();
                        stmtUpdate.close();

                        updateTextCheckedState(holder, article);

                        refresh();

                        m_activity.invalidateOptionsMenu();
                    }
                });

            }

			if (holder.titleView != null) {
				
				holder.titleView.setText(Html.fromHtml(article.getString(article.getColumnIndex("title"))));
                holder.titleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, Math.min(21, headlineFontSize + 3));

				int scoreIndex = article.getColumnIndex("score");
				if (scoreIndex >= 0)
					adjustTitleTextView(article.getInt(scoreIndex), holder.titleView, position);
			}
			
			int feedTitleIndex = article.getColumnIndex("feed_title");
			
			if (holder.feedTitleView != null && feedTitleIndex != -1 && m_feedIsCat) {				
				String feedTitle = article.getString(feedTitleIndex);
				
				if (feedTitle.length() > 20)
					feedTitle = feedTitle.substring(0, 20) + "...";
				
				if (feedTitle.length() > 0) {
					holder.feedTitleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, headlineSmallFontSize);
					holder.feedTitleView.setText(feedTitle);					
				} else {
					holder.feedTitleView.setVisibility(View.GONE);
				}				
			} else if (holder.feedTitleView != null) {
				holder.feedTitleView.setVisibility(View.GONE);
			}
			
			if (holder.markedView != null) {
				TypedValue tv = new TypedValue();
				m_activity.getTheme().resolveAttribute(article.getInt(article.getColumnIndex("marked")) == 1 ? R.attr.ic_star : R.attr.ic_star_outline, tv, true);

				holder.markedView.setImageResource(tv.resourceId);
				
				holder.markedView.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						SQLiteStatement stmtUpdate = m_activity.getDatabase().compileStatement("UPDATE articles SET modified = 1, marked = NOT marked " +
								"WHERE " + BaseColumns._ID + " = ?");

						stmtUpdate.bindLong(1, articleId);
						stmtUpdate.execute();
						stmtUpdate.close();

						refresh();
					}
				});
			}
			
			if (holder.publishedView != null) {
				TypedValue tv = new TypedValue();
				m_activity.getTheme().resolveAttribute(article.getInt(article.getColumnIndex("published")) == 1 ? R.attr.ic_checkbox_marked : R.attr.ic_rss_box, tv, true);

				holder.publishedView.setImageResource(tv.resourceId);
				
				holder.publishedView.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        SQLiteStatement stmtUpdate = m_activity.getDatabase().compileStatement("UPDATE articles SET modified = 1, published = NOT published " +
                                "WHERE " + BaseColumns._ID + " = ?");

                        stmtUpdate.bindLong(1, articleId);
                        stmtUpdate.execute();
                        stmtUpdate.close();

                        refresh();
                    }
                });
			}

			if (holder.excerptView != null) {
				if (!m_prefs.getBoolean("headlines_show_content", true)) {
					holder.excerptView.setVisibility(View.GONE);
				} else {
                    String articleContent = article.getString(article.getColumnIndex("content"));

                    String tmp = articleContent.length() > CommonActivity.EXCERPT_MAX_QUERY_LENGTH ?
                            articleContent.substring(0, CommonActivity.EXCERPT_MAX_QUERY_LENGTH) : articleContent;

                    String excerpt = Jsoup.parse(tmp).text();

                    if (excerpt.length() > CommonActivity.EXCERPT_MAX_LENGTH) excerpt = excerpt.substring(0, CommonActivity.EXCERPT_MAX_LENGTH) + "…";

					holder.excerptView.setTextSize(TypedValue.COMPLEX_UNIT_SP, headlineFontSize);
					holder.excerptView.setText(excerpt);
				}
			}       	

			if (holder.authorView != null) {
				int authorIndex = article.getColumnIndex("author");
				if (authorIndex >= 0) {
					String author = article.getString(authorIndex);
					
					holder.authorView.setTextSize(TypedValue.COMPLEX_UNIT_SP, headlineSmallFontSize);					
					
					if (author != null && author.length() > 0) 
						holder.authorView.setText(getString(R.string.author_formatted, author));
					else
						holder.authorView.setText("");
				}
			}
			
			if (holder.dateView != null) {
				holder.dateView.setTextSize(TypedValue.COMPLEX_UNIT_SP, headlineSmallFontSize);
				
				Date d = new Date((long)article.getInt(article.getColumnIndex("updated")) * 1000);
				DateFormat df = new SimpleDateFormat("MMM dd, HH:mm");
				df.setTimeZone(TimeZone.getDefault());
				holder.dateView.setText(df.format(d));
			}

			if (holder.selectionBoxView != null) {
				holder.selectionBoxView.setChecked(article.getInt(article.getColumnIndex("selected")) == 1);
				holder.selectionBoxView.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        CheckBox cb = (CheckBox) view;

                        SQLiteStatement stmtUpdate = m_activity.getDatabase().compileStatement("UPDATE articles SET selected = ? " +
                                "WHERE " + BaseColumns._ID + " = ?");

                        stmtUpdate.bindLong(1, cb.isChecked() ? 1 : 0);
                        stmtUpdate.bindLong(2, articleId);
                        stmtUpdate.execute();
                        stmtUpdate.close();

                        refresh();

                        m_activity.invalidateOptionsMenu();

                    }
                });
			}

            if (holder.flavorImageHolder != null) {

				/* reset to default in case of convertview */
				holder.flavorImageLoadingBar.setVisibility(View.GONE);
				holder.flavorImageView.setVisibility(View.GONE);
				holder.flavorVideoKindView.setVisibility(View.GONE);
				holder.flavorImageOverflow.setVisibility(View.GONE);

				holder.headlineHeader.setBackgroundDrawable(null);

				// this is needed if our flavor image goes behind base listview element
				holder.headlineHeader.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						m_listener.onArticleSelected(articleId);
					}
				});

				holder.headlineHeader.setOnLongClickListener(new View.OnLongClickListener() {
					@Override
					public boolean onLongClick(View v) {
						m_activity.openContextMenu(v);

						return true;
					}
				});
            }
			
			if (holder.menuButtonView != null) {
				//if (m_activity.isDarkTheme())
				//	ib.setImageResource(R.drawable.ic_mailbox_collapsed_holo_dark);
				
				holder.menuButtonView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {

						PopupMenu popup = new PopupMenu(getActivity(), v);
						MenuInflater inflater = popup.getMenuInflater();
						inflater.inflate(R.menu.context_headlines, popup.getMenu());

						popup.getMenu().findItem(R.id.set_labels).setVisible(false);
						popup.getMenu().findItem(R.id.article_set_note).setVisible(false);
						popup.getMenu().findItem(R.id.headlines_article_unread).setVisible(false); // TODO: implement

						popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
							@Override
							public boolean onMenuItemClick(MenuItem item) {
								return onArticleMenuItemSelected(item, articleId);
							}
						});

						popup.show();
                    }
                });								
			}

            return v;
		}

		private void adjustTitleTextView(int score, TextView tv, int position) {
			int viewType = getItemViewType(position);
			if (origTitleColors[viewType] == null)
				// store original color
				origTitleColors[viewType] = Integer.valueOf(tv.getCurrentTextColor());

			if (score < -500) {
				tv.setPaintFlags(tv.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
			} else if (score > 500) {
				tv.setTextColor(titleHighScoreUnreadColor);
				tv.setPaintFlags(tv.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
			} else {
				tv.setTextColor(origTitleColors[viewType].intValue());
				tv.setPaintFlags(tv.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
			}
		}
	}

	public void notifyUpdated() {
		m_adapter.notifyDataSetChanged();
	}

	public void setActiveArticleId(int articleId) {
		m_activeArticleId = articleId;
		try {
			m_adapter.notifyDataSetChanged();

			ListView list = (ListView)getView().findViewById(R.id.headlines_list);
		
			Log.d(TAG, articleId + " position " + getArticleIdPosition(articleId));
			
			if (list != null) {
				list.setSelection(getArticleIdPosition(articleId));
			}
		} catch (NullPointerException e) {
			// invoked before view is created, nvm
		}
	}

	public Cursor getArticleAtPosition(int position) {
		return (Cursor) m_list.getItemAtPosition(position);
	}

	public int getArticleIdAtPosition(int position) {
		Cursor c = getArticleAtPosition(position);
		
		if (c != null) {
			int id = c.getInt(0);
			return id;
		}

        return 0;

		//return (int) m_adapter.getItemId(position + m_list.getHeaderViewsCount());
	}

	public int getActiveArticleId() {
		return m_activeArticleId;
	}

	public int getArticleIdPosition(int articleId) {
		for (int i = 0; i < m_adapter.getCount(); i++) {
			if (articleId == m_adapter.getItemId(i))
				return i + m_list.getHeaderViewsCount();
		}
		
		return -1;
	}
	
	public int getArticleCount() {
		return m_adapter.getCount();
	}

	public void setSearchQuery(String query) {
		if (!m_searchQuery.equals(query)) {
			m_searchQuery = query;
		}
	}

	public int getFeedId() {
		return m_feedId;
	}
	
	public boolean getFeedIsCat() {
		return m_feedIsCat;
	}

	public String getSearchQuery() {
		return m_searchQuery;
	}

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if (m_prefs.getBoolean("headlines_mark_read_scroll", false) && firstVisibleItem > (m_activity.isSmallScreen() ? 1 : 0)) {
            //Article a = m_articles.get(firstVisibleItem - 1);

            Cursor article = getArticleAtPosition(firstVisibleItem - 1);

            if (article != null) {
                Integer id = article.getInt(article.getColumnIndex(BaseColumns._ID));
                boolean unread = article.getInt(article.getColumnIndex("unread")) != 0;

                if (unread && !m_readArticleIds.contains(id)) {
                    m_readArticleIds.add(id);
                }
            }
        }

        if (!m_activity.isTablet()) {
            if (m_adapter.getCount() > 0) {
                if (firstVisibleItem > m_listPreviousVisibleItem) {
                    m_activity.getSupportActionBar().hide();
                } else if (firstVisibleItem < m_listPreviousVisibleItem) {
                    m_activity.getSupportActionBar().show();
                }
            } else {
                m_activity.getSupportActionBar().show();
            }

            m_listPreviousVisibleItem = firstVisibleItem;
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (scrollState == SCROLL_STATE_IDLE && m_prefs.getBoolean("headlines_mark_read_scroll", false)) {
            if (!m_readArticleIds.isEmpty()) {

                SQLiteStatement stmt = m_activity.getDatabase().compileStatement(
                        "UPDATE articles SET modified = 1, unread = 0 " + "WHERE " + BaseColumns._ID
                                + " = ?");

                for (int articleId : m_readArticleIds) {
                    stmt.bindLong(1, articleId);
                    stmt.execute();
                }

                stmt.close();
                refresh();

                m_readArticleIds.clear();
            }
        }
    }

}