summaryrefslogtreecommitdiff
path: root/src/org/fox/ttrss/FeedsActivity.java
blob: 6645a7ee215da77f147400a3e2d96b80f110bdc6 (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
package org.fox.ttrss;

import java.util.HashMap;

import org.fox.ttrss.types.Article;
import org.fox.ttrss.types.ArticleList;
import org.fox.ttrss.types.Feed;
import org.fox.ttrss.types.FeedCategory;
import org.fox.ttrss.util.AppRater;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.ShareActionProvider;

public class FeedsActivity extends OnlineActivity implements HeadlinesEventListener {
	private final String TAG = this.getClass().getSimpleName();
	
	protected SharedPreferences m_prefs;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		m_prefs = PreferenceManager
				.getDefaultSharedPreferences(getApplicationContext());

		if (m_prefs.getString("theme", "THEME_DARK").equals("THEME_DARK")) {
			setTheme(R.style.DarkTheme);
		} else {
			setTheme(R.style.LightTheme);
		}

		super.onCreate(savedInstanceState);

		setContentView(R.layout.feeds);
		
		setSmallScreen(findViewById(R.id.headlines_fragment) == null); 
		
		Intent intent = getIntent();
		
		if (savedInstanceState == null) {
			
			if (intent.getParcelableExtra("feed") != null || intent.getParcelableExtra("category") != null || 
				intent.getParcelableExtra("article") != null) {
			
				if (!isCompatMode()) {
					getActionBar().setDisplayHomeAsUpEnabled(true);
				}
				
				Feed feed = (Feed) intent.getParcelableExtra("feed");
				FeedCategory cat = (FeedCategory) intent.getParcelableExtra("category");
				Article article = (Article) intent.getParcelableExtra("article");
	
				FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
	
				if (feed != null) {
					HeadlinesFragment hf = new HeadlinesFragment(feed);
					ft.replace(R.id.feeds_fragment, hf, FRAG_HEADLINES);
					
					setTitle(feed.title);
				}
				
				if (cat != null) {
					FeedsFragment ff = new FeedsFragment(cat);
					ft.replace(R.id.feeds_fragment, ff, FRAG_FEEDS);
					
					setTitle(cat.title);
				}
				
				if (article != null) {
					Article original = GlobalState.getInstance().m_loadedArticles.findById(article.id);
					
					ArticlePager ap = new ArticlePager(original != null ? original : article);
					ft.replace(R.id.feeds_fragment, ap, FRAG_ARTICLE);
					
					ap.setSearchQuery(intent.getStringExtra("searchQuery"));
					
					setTitle(intent.getStringExtra("feedTitle"));
				}
	
				ft.commit();

			} else  {
				FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

				if (m_prefs.getBoolean("enable_cats", false)) {
					ft.replace(R.id.feeds_fragment, new FeedCategoriesFragment(), FRAG_CATS);				
				} else {
					ft.replace(R.id.feeds_fragment, new FeedsFragment(), FRAG_FEEDS);
				}
				
				ft.commit();
				
				AppRater.appLaunched(this);
			}
		}
	}
	
	@Override
	protected void initMenu() {
		super.initMenu();
		
		if (m_menu != null && m_sessionId != null) {
			Fragment ff = getSupportFragmentManager().findFragmentByTag(FRAG_FEEDS);
			Fragment cf = getSupportFragmentManager().findFragmentByTag(FRAG_CATS);
			ArticlePager af = (ArticlePager) getSupportFragmentManager().findFragmentByTag(FRAG_ARTICLE);
			HeadlinesFragment hf = (HeadlinesFragment)getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES);
			
			m_menu.setGroupVisible(R.id.menu_group_feeds, (ff != null && ff.isAdded()) || (cf != null && cf.isAdded()));
			
			m_menu.setGroupVisible(R.id.menu_group_article, af != null && af.isAdded());

			m_menu.setGroupVisible(R.id.menu_group_headlines, hf != null && hf.isAdded() && hf.getSelectedArticles().size() == 0);
			m_menu.setGroupVisible(R.id.menu_group_headlines_selection, hf != null && hf.isAdded() && hf.getSelectedArticles().size() != 0);
			
			if (isSmallScreen()) {
				m_menu.findItem(R.id.update_headlines).setVisible(hf != null && hf.isAdded());
			} else {
				m_menu.findItem(R.id.update_headlines).setVisible(false);
			}
			
			MenuItem item = m_menu.findItem(R.id.show_feeds);

			if (getUnreadOnly()) {
				item.setTitle(R.string.menu_all_feeds);
			} else {
				item.setTitle(R.string.menu_unread_feeds);
			}
		}		
	}
	
	
	public void onFeedSelected(Feed feed) {
		GlobalState.getInstance().m_loadedArticles.clear();

		if (isSmallScreen()) {
				
			Intent intent = new Intent(FeedsActivity.this, FeedsActivity.class);
			intent.putExtra("sessionId", m_sessionId);
			intent.putExtra("apiLevel", m_apiLevel);
			intent.putExtra("feed", feed);
	 	   
			startActivityForResult(intent, 0);
			
			//HeadlinesFragment hf = new HeadlinesFragment(feed);
			//ft.replace(R.id.feeds_fragment, hf, FRAG_HEADLINES);
			//ft.addToBackStack(null);
		} else {
			FragmentTransaction ft = getSupportFragmentManager()
					.beginTransaction();

			HeadlinesFragment hf = new HeadlinesFragment(feed);
			ft.replace(R.id.headlines_fragment, hf, FRAG_HEADLINES);
			
			ft.commit();		
		}
	}
	
	public void onCatSelected(FeedCategory cat, boolean openAsFeed) {

		if (!openAsFeed) {
			
			if (isSmallScreen()) {
			
				Intent intent = new Intent(FeedsActivity.this, FeedsActivity.class);
				intent.putExtra("sessionId", m_sessionId);
				intent.putExtra("apiLevel", m_apiLevel);				
				intent.putExtra("category", cat);
		 	   
				startActivityForResult(intent, 0);
				
			} else {
				FragmentTransaction ft = getSupportFragmentManager()
						.beginTransaction();

				FeedsFragment ff = new FeedsFragment(cat);
				ft.replace(R.id.feeds_fragment, ff, FRAG_FEEDS);

				ft.addToBackStack(null);
				ft.commit();
			}
		} else {
			Feed feed = new Feed(cat.id, cat.title, true);
			onFeedSelected(feed);
		}
	}
	
	public void onCatSelected(FeedCategory cat) {
		onCatSelected(cat, m_prefs.getBoolean("browse_cats_like_feeds", false));		
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.show_feeds:
			m_unreadOnly = !m_unreadOnly;
			initMenu();
			refresh();
			return true;
		case R.id.update_feeds:
			refresh();
			return true;
		default:
			Log.d(TAG, "onOptionsItemSelected, unhandled id=" + item.getItemId());
			return super.onOptionsItemSelected(item);
		}
	}
	
	@Override
	protected void loginSuccess() {
		setLoadingStatus(R.string.blank, false);
		findViewById(R.id.loading_container).setVisibility(View.GONE);
		initMenu();
	}
	
	@Override
	public void onSaveInstanceState(Bundle out) {
		super.onSaveInstanceState(out);
		
	}
	
	@Override
	public void onResume() {
		super.onResume();
		initMenu();
	}

	@Override
	public void onArticleListSelectionChange(ArticleList m_selectedArticles) {
		initMenu();		
	}

	public void onArticleSelected(Article article, boolean open) {
		if (article.unread) {
			article.unread = false;
			saveArticleUnread(article);
		}
		
		if (open) {
			HeadlinesFragment hf = (HeadlinesFragment)getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES);

			if (isSmallScreen()) {

				Intent intent = new Intent(FeedsActivity.this, FeedsActivity.class);
				intent.putExtra("sessionId", m_sessionId);
				intent.putExtra("apiLevel", m_apiLevel);
				
				intent.putExtra("feedTitle", hf.getFeed().title);
				intent.putExtra("article", article);
				intent.putExtra("searchQuery", hf.getSearchQuery());
		 	   
				startActivityForResult(intent, 0);
				
				
			} else {
				Intent intent = new Intent(FeedsActivity.this, HeadlinesActivity.class);
				intent.putExtra("sessionId", m_sessionId);
				intent.putExtra("apiLevel", m_apiLevel);
				
				intent.putExtra("feed", hf.getFeed());
				intent.putExtra("article", article);
				intent.putExtra("searchQuery", hf.getSearchQuery());
		 	   
				startActivityForResult(intent, 0);
			}
		} else {
			initMenu();
		}
	}

	@Override
	public void onArticleSelected(Article article) {
		onArticleSelected(article, true);		
	}

	@SuppressWarnings("unchecked")
	public void catchupFeed(final Feed feed) {
		super.catchupFeed(feed);
		refresh();
	}

	@Override
	public void onHeadlinesLoaded() {
		// TODO Auto-generated method stub
		
	}
}