tt-rss-android/org.fox.ttrss/src/main/java/org/fox/ttrss/MasterActivity.java

583 lines
18 KiB
Java
Raw Normal View History

2013-04-21 13:01:26 +00:00
package org.fox.ttrss;
2013-04-21 13:01:26 +00:00
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
2013-04-21 13:01:26 +00:00
import android.content.Intent;
import android.content.SharedPreferences;
2017-05-31 18:20:23 +00:00
import android.os.AsyncTask;
2013-04-21 13:01:26 +00:00
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
2013-04-21 13:01:26 +00:00
import android.util.Log;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
2013-04-21 13:01:26 +00:00
2017-05-31 18:20:23 +00:00
import com.bumptech.glide.Glide;
2013-11-27 14:58:25 +00:00
import com.google.gson.JsonElement;
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.widget.SmallWidgetProvider;
import java.util.Date;
import java.util.HashMap;
2015-06-10 16:40:08 +00:00
public class MasterActivity extends OnlineActivity implements HeadlinesEventListener {
2013-04-21 13:01:26 +00:00
private final String TAG = this.getClass().getSimpleName();
private static final int HEADLINES_REQUEST = 1;
protected SharedPreferences m_prefs;
protected long m_lastRefresh = 0;
protected long m_lastWidgetRefresh = 0;
2013-04-21 13:01:26 +00:00
private boolean m_feedIsSelected = false;
private boolean m_userFeedSelected = false;
private ActionBarDrawerToggle m_drawerToggle;
private DrawerLayout m_drawerLayout;
2013-04-21 13:01:26 +00:00
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
m_prefs = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
setAppTheme(m_prefs);
2015-02-12 10:09:54 +00:00
2013-04-21 13:01:26 +00:00
super.onCreate(savedInstanceState);
2015-06-10 16:40:08 +00:00
setContentView(R.layout.activity_master);
2014-10-15 19:07:52 +00:00
setSmallScreen(findViewById(R.id.sw600dp_anchor) == null);
2015-06-10 16:24:24 +00:00
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
2015-06-10 16:40:08 +00:00
Application.getInstance().load(savedInstanceState);
m_lastWidgetRefresh = new Date().getTime();
m_drawerLayout = (DrawerLayout) findViewById(R.id.headlines_drawer);
if (m_drawerLayout != null) {
m_drawerToggle = new ActionBarDrawerToggle(this, m_drawerLayout, R.string.blank, R.string.blank) {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getSupportActionBar().show();
invalidateOptionsMenu();
refresh(false);
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
if (m_prefs.getBoolean("drawer_open_on_start", true)) {
SharedPreferences.Editor editor = m_prefs.edit();
editor.putBoolean("drawer_open_on_start", false);
editor.apply();
}
invalidateOptionsMenu();
}
};
m_drawerLayout.setDrawerListener(m_drawerToggle);
m_drawerToggle.setDrawerIndicatorEnabled(true);
2014-10-31 06:06:42 +00:00
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
if (savedInstanceState == null) {
if (m_drawerLayout != null && m_prefs.getBoolean("drawer_open_on_start", true)) {
m_drawerLayout.openDrawer(Gravity.START);
}
2013-04-21 13:01:26 +00:00
final Intent i = getIntent();
boolean shortcutMode = i.getBooleanExtra("shortcut_mode", false);
Log.d(TAG, "is_shortcut_mode: " + shortcutMode);
if (shortcutMode) {
LoginRequest lr = new LoginRequest(this, false, new OnLoginFinishedListener() {
@Override
public void OnLoginSuccess() {
int feedId = i.getIntExtra("feed_id", 0);
boolean isCat = i.getBooleanExtra("feed_is_cat", false);
String feedTitle = i.getStringExtra("feed_title");
Feed tmpFeed = new Feed(feedId, feedTitle, isCat);
onFeedSelected(tmpFeed, false);
}
@Override
public void OnLoginFailed() {
login();
}
});
HashMap<String, String> map = new HashMap<String, String>() {
{
put("op", "login");
put("user", m_prefs.getString("login", "").trim());
put("password", m_prefs.getString("password", "").trim());
}
};
lr.execute(map);
}
//m_pullToRefreshAttacher.setRefreshing(true);
2013-05-28 09:17:02 +00:00
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
2015-02-12 10:09:54 +00:00
2013-05-28 09:17:02 +00:00
if (m_prefs.getBoolean("enable_cats", false)) {
2015-02-12 10:09:54 +00:00
ft.replace(R.id.feeds_fragment, new FeedCategoriesFragment(), FRAG_CATS);
2013-05-28 09:17:02 +00:00
} else {
ft.replace(R.id.feeds_fragment, new FeedsFragment(), FRAG_FEEDS);
}
if (m_prefs.getBoolean("open_fresh_on_startup", true)) {
HeadlinesFragment hf = new HeadlinesFragment();
if (BuildConfig.DEBUG) {
2017-05-31 19:51:23 +00:00
hf.initialize(new Feed(-1, "Starred articles", false));
} else {
hf.initialize(new Feed(-3, getString(R.string.fresh_articles), false));
}
ft.replace(R.id.headlines_fragment, hf, FRAG_HEADLINES);
} else if (m_drawerLayout != null) {
m_drawerLayout.openDrawer(Gravity.START);
}
2013-05-28 09:17:02 +00:00
ft.commit();
m_feedIsSelected = true;
2014-10-15 17:11:37 +00:00
checkTrial(true);
2013-05-28 09:17:02 +00:00
2013-04-21 13:01:26 +00:00
} else { // savedInstanceState != null
//m_actionbarUpEnabled = savedInstanceState.getBoolean("actionbarUpEnabled");
//m_actionbarRevertDepth = savedInstanceState.getInt("actionbarRevertDepth");
m_feedIsSelected = savedInstanceState.getBoolean("feedIsSelected");
m_userFeedSelected = savedInstanceState.getBoolean("userFeedSelected");
//m_feedWasSelected = savedInstanceState.getBoolean("feedWasSelected");
2013-04-21 13:01:26 +00:00
/* if (findViewById(R.id.sw600dp_port_anchor) != null && m_feedWasSelected && m_slidingMenu != null) {
m_slidingMenu.setBehindWidth(getScreenWidthInPixel() * 2/3);
} */
if (m_drawerLayout != null && m_feedIsSelected == false) {
m_drawerLayout.openDrawer(Gravity.START);
}
2013-04-21 13:01:26 +00:00
}
}
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
if (m_drawerToggle != null) m_drawerToggle.syncState();
}
2013-04-21 13:01:26 +00:00
@Override
protected void initMenu() {
super.initMenu();
if (m_menu != null && getSessionId() != null) {
Fragment ff = getSupportFragmentManager().findFragmentByTag(FRAG_FEEDS);
Fragment cf = getSupportFragmentManager().findFragmentByTag(FRAG_CATS);
HeadlinesFragment hf = (HeadlinesFragment)getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES);
/* if (m_drawerLayout != null) {
boolean isDrawerOpen = m_drawerLayout.isDrawerOpen(Gravity.START);
m_menu.setGroupVisible(R.id.menu_group_feeds, isDrawerOpen);
m_menu.setGroupVisible(R.id.menu_group_headlines, hf != null && hf.isAdded() && !isDrawerOpen);
} else {
m_menu.setGroupVisible(R.id.menu_group_feeds, (ff != null && ff.isAdded()) || (cf != null && cf.isAdded()));
m_menu.setGroupVisible(R.id.menu_group_headlines, hf != null && hf.isAdded());
m_menu.findItem(R.id.update_headlines).setVisible(false);
} */
m_menu.setGroupVisible(R.id.menu_group_feeds, (ff != null && ff.isAdded()) || (cf != null && cf.isAdded()));
m_menu.setGroupVisible(R.id.menu_group_headlines, hf != null && hf.isAdded());
//m_menu.findItem(R.id.headlines_toggle_sidebar).setVisible(false);
/* MenuItem item = m_menu.findItem(R.id.show_feeds);
2013-04-21 13:01:26 +00:00
if (getUnreadOnly()) {
item.setTitle(R.string.menu_all_feeds);
} else {
item.setTitle(R.string.menu_unread_feeds);
} */
2013-04-21 13:01:26 +00:00
}
}
public void onFeedSelected(Feed feed) {
onFeedSelected(feed, true);
}
public void onFeedSelected(Feed feed, final boolean selectedByUser) {
2013-04-21 13:01:26 +00:00
FeedsFragment ff = (FeedsFragment) getSupportFragmentManager().findFragmentByTag(FRAG_FEEDS);
if (ff != null && ff.isAdded()) {
ff.setSelectedfeed(feed);
}
2015-07-10 21:15:20 +00:00
2015-02-12 10:09:54 +00:00
FragmentTransaction ft = getSupportFragmentManager()
.beginTransaction();
2013-04-21 13:01:26 +00:00
2015-02-12 10:09:54 +00:00
HeadlinesFragment hf = new HeadlinesFragment();
hf.initialize(feed);
2013-04-21 13:01:26 +00:00
2015-02-12 10:09:54 +00:00
ft.replace(R.id.headlines_fragment, hf, FRAG_HEADLINES);
2015-02-12 10:09:54 +00:00
ft.commit();
2013-04-21 13:01:26 +00:00
2015-02-12 10:09:54 +00:00
m_feedIsSelected = true;
m_userFeedSelected = selectedByUser;
2013-04-21 13:01:26 +00:00
2015-02-12 10:09:54 +00:00
if (m_drawerLayout != null) {
m_drawerLayout.closeDrawers();
}
Date date = new Date();
if (date.getTime() - m_lastRefresh > 10000) {
m_lastRefresh = date.getTime();
refresh(false);
}
2013-04-21 13:01:26 +00:00
}
public void onCatSelected(FeedCategory cat, boolean openAsFeed) {
FeedCategoriesFragment fc = (FeedCategoriesFragment) getSupportFragmentManager().findFragmentByTag(FRAG_CATS);
//m_pullToRefreshAttacher.setRefreshing(true);
2013-04-21 13:01:26 +00:00
if (!openAsFeed) {
if (fc != null && fc.isAdded()) {
2013-05-28 09:17:02 +00:00
fc.setSelectedCategory(null);
}
FragmentTransaction ft = getSupportFragmentManager()
.beginTransaction();
2013-05-28 09:17:02 +00:00
FeedsFragment ff = new FeedsFragment();
ff.initialize(cat, true);
2013-05-28 09:17:02 +00:00
ft.replace(R.id.feeds_fragment, ff, FRAG_FEEDS);
2013-04-21 13:01:26 +00:00
2014-11-04 08:34:05 +00:00
ft.addToBackStack(null);
2013-05-28 09:17:02 +00:00
ft.commit();
//getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//m_actionbarUpEnabled = true;
//m_actionbarRevertDepth = m_actionbarRevertDepth + 1;
2013-04-21 13:01:26 +00:00
} else {
if (fc != null) {
fc.setSelectedCategory(cat);
}
2013-04-21 13:01:26 +00:00
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 void logout() {
super.logout();
finish();
}
2013-04-21 13:01:26 +00:00
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (m_drawerToggle != null && m_drawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case R.id.headlines_toggle_sort_order:
Dialog dialog = new Dialog(this);
String sortMode = getSortMode();
int selectedIndex = 0;
if (sortMode.equals("feed_dates")) {
selectedIndex = 1;
} else if (sortMode.equals("date_reverse")) {
selectedIndex = 2;
} else if (sortMode.equals("title")) {
selectedIndex = 3;
}
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle(getString(R.string.headlines_sort_articles_title))
.setSingleChoiceItems(
new String[] {
getString(R.string.headlines_sort_default),
getString(R.string.headlines_sort_newest_first),
getString(R.string.headlines_sort_oldest_first),
getString(R.string.headlines_sort_title)
},
selectedIndex, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
switch (which) {
case 0:
setSortMode("default");
break;
case 1:
setSortMode("feed_dates");
break;
case 2:
setSortMode("date_reverse");
break;
case 3:
setSortMode("title");
break;
}
dialog.cancel();
refresh();
}
});
dialog = builder.create();
dialog.show();
return true;
/* case R.id.show_feeds:
2013-04-21 13:01:26 +00:00
setUnreadOnly(!getUnreadOnly());
invalidateOptionsMenu();
2013-04-21 13:01:26 +00:00
refresh();
return true; */
/*case R.id.update_feeds:
//m_pullToRefreshAttacher.setRefreshing(true);
2013-04-21 13:01:26 +00:00
refresh();
return true;*/
2013-04-21 13:01:26 +00:00
default:
Log.d(TAG, "onOptionsItemSelected, unhandled id=" + item.getItemId());
return super.onOptionsItemSelected(item);
}
}
2014-11-04 08:34:05 +00:00
@Override
public void onBackPressed() {
if (m_drawerLayout != null && !m_drawerLayout.isDrawerOpen(Gravity.START) &&
(getSupportFragmentManager().getBackStackEntryCount() > 0 || m_userFeedSelected)) {
m_drawerLayout.openDrawer(Gravity.START);
} else {
try {
super.onBackPressed();
} catch (IllegalStateException e) {
// java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
e.printStackTrace();
}
}
2014-11-04 08:34:05 +00:00
}
2013-04-21 13:01:26 +00:00
@Override
protected void loginSuccess(boolean refresh) {
invalidateOptionsMenu();
2013-04-21 13:01:26 +00:00
if (refresh) refresh();
}
@Override
public void onSaveInstanceState(Bundle out) {
super.onSaveInstanceState(out);
out.putBoolean("feedIsSelected", m_feedIsSelected);
out.putBoolean("userFeedSelected", m_userFeedSelected);
2015-06-10 16:40:08 +00:00
Application.getInstance().save(out);
2013-04-21 13:01:26 +00:00
}
@Override
public void onResume() {
super.onResume();
invalidateOptionsMenu();
2015-06-11 05:32:27 +00:00
2013-04-21 13:01:26 +00:00
}
@Override
public void onArticleListSelectionChange(ArticleList m_selectedArticles) {
invalidateOptionsMenu();
2013-04-21 13:01:26 +00:00
}
/* public void openFeedArticles(Feed feed) {
2015-06-10 16:40:08 +00:00
//Application.getInstance().m_loadedArticles.clear();
2013-05-28 09:17:02 +00:00
2015-06-10 16:40:08 +00:00
Intent intent = new Intent(MasterActivity.this, DetailActivity.class);
2013-05-28 09:17:02 +00:00
intent.putExtra("feed", feed);
intent.putExtra("article", (Article)null);
intent.putExtra("searchQuery", (String)null);
2013-05-28 09:17:02 +00:00
startActivityForResult(intent, HEADLINES_REQUEST);
overridePendingTransition(R.anim.right_slide_in, 0);
} */
2013-04-21 13:01:26 +00:00
public void onArticleSelected(Article article, boolean open) {
if (open) {
HeadlinesFragment hf = (HeadlinesFragment)getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES);
2015-06-10 16:40:08 +00:00
Intent intent = new Intent(MasterActivity.this, DetailActivity.class);
2013-05-28 09:17:02 +00:00
intent.putExtra("feed", hf.getFeed());
//intent.putExtra("article", article);
2013-05-28 09:17:02 +00:00
intent.putExtra("searchQuery", hf.getSearchQuery());
//intent.putExtra("articles", (Parcelable)hf.getAllArticles());
2015-06-10 16:40:08 +00:00
Application.getInstance().tmpArticleList = hf.getAllArticles();
Application.getInstance().tmpArticle = article;
/* if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
startActivityForResult(intent, HEADLINES_REQUEST, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
} else {
startActivityForResult(intent, HEADLINES_REQUEST);
} */
// mysterious crashes somewhere in gl layer (?) on some feeds if we use activitycompat transitions here on LP so welp
startActivityForResult(intent, HEADLINES_REQUEST);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
2015-02-14 19:03:16 +00:00
2013-04-21 13:01:26 +00:00
} else {
invalidateOptionsMenu();
if (article.unread) {
article.unread = false;
saveArticleUnread(article);
}
}
2013-04-21 13:01:26 +00:00
}
@Override
public void onPause() {
super.onPause();
Date date = new Date();
if (isFinishing() || date.getTime() - m_lastWidgetRefresh > 60*1000) {
m_lastWidgetRefresh = date.getTime();
Intent updateWidgetIntent = new Intent(SmallWidgetProvider.ACTION_REQUEST_UPDATE);
sendBroadcast(updateWidgetIntent);
}
}
@Override
2013-04-21 13:01:26 +00:00
public void onArticleSelected(Article article) {
onArticleSelected(article, true);
}
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 && data != null) {
2015-06-10 16:40:08 +00:00
//Application.getInstance().m_activeArticle = null;
2015-02-10 12:50:47 +00:00
//ArrayList<Article> tmp = data.getParcelableArrayListExtra("articles");
Article article = data.getParcelableExtra("activeArticle");
2015-06-10 16:40:08 +00:00
ArticleList articles = Application.getInstance().tmpArticleList;
if (articles != null) {
HeadlinesFragment hf = (HeadlinesFragment)getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES);
if (hf != null) {
hf.setArticles(articles);
//hf.setActiveArticle(article); disable HL scrolling on resume for now
}
}
2013-04-21 13:01:26 +00:00
}
}
public void createFeedShortcut(Feed feed) {
2015-06-10 16:40:08 +00:00
final Intent shortcutIntent = new Intent(this, MasterActivity.class);
shortcutIntent.putExtra("feed_id", feed.id);
shortcutIntent.putExtra("feed_is_cat", feed.is_cat);
shortcutIntent.putExtra("feed_title", feed.title);
shortcutIntent.putExtra("shortcut_mode", true);
Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, feed.title);
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher));
intent.putExtra("duplicate", false);
sendBroadcast(intent);
toast(R.string.shortcut_has_been_placed_on_the_home_screen);
}
public void createCategoryShortcut(FeedCategory cat) {
createFeedShortcut(new Feed(cat.id, cat.title, true));
}
2013-11-27 14:58:25 +00:00
public void unsubscribeFeed(final Feed feed) {
ApiRequest req = new ApiRequest(getApplicationContext()) {
protected void onPostExecute(JsonElement result) {
refresh();
}
};
@SuppressWarnings("serial")
HashMap<String, String> map = new HashMap<String, String>() {
{
put("sid", getSessionId());
put("op", "unsubscribeFeed");
put("feed_id", String.valueOf(feed.id));
}
};
req.execute(map);
}
2013-04-21 13:01:26 +00:00
}