tt-rss-android/src/org/fox/ttrss/FeedsActivity.java

392 lines
12 KiB
Java
Raw Normal View History

2012-09-16 09:25:28 +00:00
package org.fox.ttrss;
2012-09-19 09:37:12 +00:00
import java.util.Date;
2012-09-16 09:25:28 +00:00
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;
2012-09-16 09:25:28 +00:00
2012-09-20 19:19:50 +00:00
import android.view.ViewGroup;
import android.animation.LayoutTransition;
2012-12-31 08:49:30 +00:00
import android.animation.ObjectAnimator;
2012-09-19 09:37:12 +00:00
import android.annotation.SuppressLint;
2012-09-16 09:25:28 +00:00
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
2012-09-16 09:25:28 +00:00
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
2012-12-31 08:49:30 +00:00
import android.widget.LinearLayout;
2012-09-16 09:25:28 +00:00
2012-09-17 12:28:32 +00:00
public class FeedsActivity extends OnlineActivity implements HeadlinesEventListener {
2012-09-16 09:25:28 +00:00
private final String TAG = this.getClass().getSimpleName();
private static final int HEADLINES_REQUEST = 1;
2012-09-16 09:25:28 +00:00
protected SharedPreferences m_prefs;
protected long m_lastRefresh = 0;
2012-09-16 09:25:28 +00:00
private boolean m_actionbarUpEnabled = false;
2012-09-19 09:37:12 +00:00
@SuppressLint("NewApi")
2012-09-16 09:25:28 +00:00
@Override
public void onCreate(Bundle savedInstanceState) {
m_prefs = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
setAppTheme(m_prefs);
2012-09-16 09:25:28 +00:00
super.onCreate(savedInstanceState);
setContentView(R.layout.feeds);
setSmallScreen(findViewById(R.id.headlines_fragment) == null);
2012-11-30 16:21:31 +00:00
GlobalState.getInstance().load(savedInstanceState);
Intent intent = getIntent();
if (savedInstanceState == null) {
if (intent.getParcelableExtra("feed") != null || intent.getParcelableExtra("category") != null ||
intent.getParcelableExtra("article") != null) {
2012-09-17 19:20:59 +00:00
if (!isCompatMode()) {
getActionBar().setDisplayHomeAsUpEnabled(true);
m_actionbarUpEnabled = true;
2012-09-17 19:20:59 +00:00
}
Feed feed = (Feed) intent.getParcelableExtra("feed");
FeedCategory cat = (FeedCategory) intent.getParcelableExtra("category");
Article article = (Article) intent.getParcelableExtra("article");
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
if (article != null) {
2012-09-17 19:20:59 +00:00
Article original = GlobalState.getInstance().m_loadedArticles.findById(article.id);
ArticlePager ap = new ArticlePager(original != null ? original : article, feed);
ft.replace(R.id.feeds_fragment, ap, FRAG_ARTICLE);
ap.setSearchQuery(intent.getStringExtra("searchQuery"));
2012-09-19 12:59:08 +00:00
setTitle(feed.title);
} else {
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);
}
}
ft.commit();
} else {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
2012-12-31 08:49:30 +00:00
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);
}
2012-12-30 23:14:01 +00:00
/* if (!isSmallScreen()) {
ft.replace(R.id.headlines_fragment, new HeadlinesFragment(new Feed(-3, "Fresh articles", false)));
} */
ft.commit();
AppRater.appLaunched(this);
2012-12-26 20:01:39 +00:00
checkTrial(true);
}
} else { // savedInstanceState != null
m_actionbarUpEnabled = savedInstanceState.getBoolean("actionbarUpEnabled");
2012-12-31 08:49:30 +00:00
if (!isSmallScreen()) {
// temporary hack because FeedsActivity doesn't track whether active feed is open
LinearLayout container = (LinearLayout) findViewById(R.id.fragment_container);
container.setWeightSum(3f);
}
if (!isCompatMode() && m_actionbarUpEnabled) {
getActionBar().setDisplayHomeAsUpEnabled(true);
2012-09-16 09:25:28 +00:00
}
}
2012-09-20 19:19:50 +00:00
if (!isCompatMode() && !isSmallScreen()) {
((ViewGroup)findViewById(R.id.headlines_fragment)).setLayoutTransition(new LayoutTransition());
((ViewGroup)findViewById(R.id.feeds_fragment)).setLayoutTransition(new LayoutTransition());
}
2012-09-16 09:25:28 +00:00
}
@Override
protected void initMenu() {
super.initMenu();
2012-09-16 15:25:47 +00:00
2012-09-19 09:55:01 +00:00
if (m_menu != null && getSessionId() != null) {
2012-09-16 09:25:28 +00:00
Fragment ff = getSupportFragmentManager().findFragmentByTag(FRAG_FEEDS);
Fragment cf = getSupportFragmentManager().findFragmentByTag(FRAG_CATS);
2012-09-16 16:46:54 +00:00
ArticlePager af = (ArticlePager) getSupportFragmentManager().findFragmentByTag(FRAG_ARTICLE);
2012-09-16 15:25:47 +00:00
HeadlinesFragment hf = (HeadlinesFragment)getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES);
m_menu.setGroupVisible(R.id.menu_group_feeds, (ff != null && ff.isAdded()) || (cf != null && cf.isAdded()));
2012-09-16 09:25:28 +00:00
m_menu.setGroupVisible(R.id.menu_group_article, af != null && af.isAdded());
2012-09-16 09:25:28 +00:00
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);
2012-09-16 15:25:47 +00:00
if (isSmallScreen()) {
m_menu.findItem(R.id.update_headlines).setVisible(hf != null && hf.isAdded());
} else {
m_menu.findItem(R.id.update_headlines).setVisible(false);
}
if (af != null) {
if (af.getSelectedArticle() != null && af.getSelectedArticle().attachments != null && af.getSelectedArticle().attachments.size() > 0) {
if (!isCompatMode()) {
m_menu.findItem(R.id.toggle_attachments).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
m_menu.findItem(R.id.toggle_attachments).setVisible(true);
} else {
if (!isCompatMode()) {
m_menu.findItem(R.id.toggle_attachments).setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
}
m_menu.findItem(R.id.toggle_attachments).setVisible(false);
}
}
2012-09-16 15:25:47 +00:00
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);
}
2012-09-16 09:25:28 +00:00
}
}
public void onFeedSelected(Feed feed) {
2012-09-17 19:20:59 +00:00
GlobalState.getInstance().m_loadedArticles.clear();
2012-09-16 09:25:28 +00:00
if (isSmallScreen()) {
Intent intent = new Intent(FeedsActivity.this, FeedsActivity.class);
intent.putExtra("feed", feed);
2012-09-19 12:59:08 +00:00
startActivityForResult(intent, 0);
2012-09-16 09:25:28 +00:00
} else {
FragmentTransaction ft = getSupportFragmentManager()
.beginTransaction();
ft.replace(R.id.headlines_fragment, new LoadingFragment(), null);
2012-09-19 09:37:12 +00:00
ft.commit();
2012-12-31 08:49:30 +00:00
if (!isCompatMode()) {
LinearLayout container = (LinearLayout) findViewById(R.id.fragment_container);
float wSum = container.getWeightSum();
if (wSum <= 2.0f) {
ObjectAnimator anim = ObjectAnimator.ofFloat(container, "weightSum", wSum, 3.0f);
anim.setDuration(200);
anim.start();
}
}
final Feed fFeed = feed;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
FragmentTransaction ft = getSupportFragmentManager()
.beginTransaction();
HeadlinesFragment hf = new HeadlinesFragment(fFeed);
ft.replace(R.id.headlines_fragment, hf, FRAG_HEADLINES);
ft.commit();
}
2012-09-20 19:00:15 +00:00
}, 10);
2012-09-19 09:37:12 +00:00
Date date = new Date();
if (date.getTime() - m_lastRefresh > 10000) {
m_lastRefresh = date.getTime();
refresh(false);
}
2012-09-16 09:25:28 +00:00
}
}
public void onCatSelected(FeedCategory cat, boolean openAsFeed) {
if (!openAsFeed) {
if (isSmallScreen()) {
Intent intent = new Intent(FeedsActivity.this, FeedsActivity.class);
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();
}
2012-09-16 09:25:28 +00:00
} 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()) {
2012-09-16 15:25:47 +00:00
case R.id.show_feeds:
setUnreadOnly(!getUnreadOnly());
2012-09-16 15:25:47 +00:00
initMenu();
refresh();
return true;
case R.id.update_feeds:
refresh();
return true;
2012-09-16 09:25:28 +00:00
default:
Log.d(TAG, "onOptionsItemSelected, unhandled id=" + item.getItemId());
return super.onOptionsItemSelected(item);
}
}
2012-09-16 09:25:28 +00:00
@Override
protected void loginSuccess(boolean refresh) {
2012-09-16 09:25:28 +00:00
setLoadingStatus(R.string.blank, false);
findViewById(R.id.loading_container).setVisibility(View.GONE);
2012-09-16 15:25:47 +00:00
initMenu();
if (refresh) refresh();
2012-09-16 09:25:28 +00:00
}
@Override
public void onSaveInstanceState(Bundle out) {
super.onSaveInstanceState(out);
out.putBoolean("actionbarUpEnabled", m_actionbarUpEnabled);
2012-11-30 16:21:31 +00:00
GlobalState.getInstance().save(out);
2012-09-16 09:25:28 +00:00
}
@Override
public void onResume() {
super.onResume();
initMenu();
2012-09-16 09:25:28 +00:00
}
@Override
public void onArticleListSelectionChange(ArticleList m_selectedArticles) {
initMenu();
}
2012-09-19 12:59:08 +00:00
public void openFeedArticles(Feed feed) {
if (isSmallScreen()) {
Intent intent = new Intent(FeedsActivity.this, FeedsActivity.class);
GlobalState.getInstance().m_activeFeed = feed;
GlobalState.getInstance().m_loadedArticles.clear();
intent.putExtra("feed", feed);
intent.putExtra("article", new Article());
startActivityForResult(intent, 0);
} else {
GlobalState.getInstance().m_loadedArticles.clear();
Intent intent = new Intent(FeedsActivity.this, HeadlinesActivity.class);
intent.putExtra("feed", feed);
intent.putExtra("article", (Article)null);
intent.putExtra("searchQuery", (String)null);
startActivityForResult(intent, HEADLINES_REQUEST);
overridePendingTransition(R.anim.right_slide_in, 0);
2012-09-19 12:59:08 +00:00
}
}
2012-09-16 09:25:28 +00:00
public void onArticleSelected(Article article, boolean open) {
if (article.unread) {
article.unread = false;
saveArticleUnread(article);
}
if (open) {
HeadlinesFragment hf = (HeadlinesFragment)getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES);
2012-09-16 09:25:28 +00:00
if (isSmallScreen()) {
//GlobalState.getInstance().m_loadedArticles.clear();
Intent intent = new Intent(FeedsActivity.this, FeedsActivity.class);
intent.putExtra("feed", hf.getFeed());
intent.putExtra("article", article);
intent.putExtra("searchQuery", hf.getSearchQuery());
startActivityForResult(intent, 0);
2012-09-16 15:25:47 +00:00
2012-09-16 09:25:28 +00:00
} else {
Intent intent = new Intent(FeedsActivity.this, HeadlinesActivity.class);
intent.putExtra("feed", hf.getFeed());
intent.putExtra("article", article);
intent.putExtra("searchQuery", hf.getSearchQuery());
2012-09-16 09:25:28 +00:00
startActivityForResult(intent, HEADLINES_REQUEST);
2012-12-30 21:53:04 +00:00
overridePendingTransition(R.anim.right_slide_in, 0);
2012-09-16 09:25:28 +00:00
}
} else {
2012-09-16 16:46:54 +00:00
initMenu();
2012-09-16 09:25:28 +00:00
}
}
@Override
public void onArticleSelected(Article article) {
onArticleSelected(article, true);
}
2012-09-16 16:46:54 +00:00
public void catchupFeed(final Feed feed) {
super.catchupFeed(feed);
refresh();
}
@Override
public void onHeadlinesLoaded(boolean appended) {
// TODO Auto-generated method stub
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == HEADLINES_REQUEST) {
GlobalState.getInstance().m_activeArticle = null;
}
}
2012-09-16 09:25:28 +00:00
}