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

2349 lines
61 KiB
Java
Raw Normal View History

2011-09-07 05:56:46 +00:00
package org.fox.ttrss;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
2011-12-05 12:19:24 +00:00
import java.util.List;
2011-11-23 17:19:35 +00:00
import java.util.Timer;
import java.util.TimerTask;
2012-06-19 10:18:00 +00:00
import org.fox.ttrss.billing.BillingHelper;
import org.fox.ttrss.billing.BillingService;
import org.fox.ttrss.offline.OfflineActivity;
import org.fox.ttrss.offline.OfflineDownloadService;
import org.fox.ttrss.offline.OfflineUploadService;
import org.fox.ttrss.types.Article;
2012-06-19 14:32:18 +00:00
import org.fox.ttrss.types.ArticleList;
2012-06-19 10:18:00 +00:00
import org.fox.ttrss.types.Feed;
import org.fox.ttrss.types.FeedCategory;
import org.fox.ttrss.types.Label;
import org.fox.ttrss.util.AppRater;
import org.fox.ttrss.util.DatabaseHelper;
import android.animation.LayoutTransition;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
2011-09-08 10:23:44 +00:00
import android.content.Intent;
import android.content.IntentFilter;
2011-09-08 10:23:44 +00:00
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.database.Cursor;
2011-12-05 13:01:44 +00:00
import android.database.sqlite.SQLiteDatabase;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
2011-09-07 05:56:46 +00:00
import android.os.Bundle;
2011-09-08 10:23:44 +00:00
import android.preference.PreferenceManager;
2012-06-19 14:24:22 +00:00
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.ActionMode;
import android.view.KeyEvent;
2011-09-08 10:23:44 +00:00
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
2011-11-22 14:30:09 +00:00
import android.view.View;
import android.view.ViewGroup;
2012-06-19 20:44:37 +00:00
import android.view.Window;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.SearchView;
import android.widget.ShareActionProvider;
2011-11-22 14:30:09 +00:00
import android.widget.TextView;
import android.widget.Toast;
2011-09-07 05:56:46 +00:00
import com.google.gson.Gson;
import com.google.gson.JsonElement;
2011-11-22 14:30:09 +00:00
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
2012-06-19 14:24:22 +00:00
public class MainActivity extends FragmentActivity implements OnlineServices {
private final String TAG = this.getClass().getSimpleName();
2012-06-19 15:12:01 +00:00
protected final static String FRAG_HEADLINES = "headlines";
protected final static String FRAG_ARTICLE = "article";
protected final static String FRAG_FEEDS = "feeds";
protected final static String FRAG_CATS = "cats";
2011-09-08 10:23:44 +00:00
private SharedPreferences m_prefs;
private String m_themeName = "";
private String m_sessionId;
private Article m_selectedArticle;
private Feed m_activeFeed;
private FeedCategory m_activeCategory;
2011-11-23 17:19:35 +00:00
private Timer m_refreshTimer;
private RefreshTask m_refreshTask;
2011-11-24 05:59:11 +00:00
private Menu m_menu;
private boolean m_smallScreenMode;
2011-11-24 05:59:11 +00:00
private boolean m_unreadOnly = true;
private boolean m_unreadArticlesOnly = true;
2012-06-19 14:24:22 +00:00
private boolean m_compatMode = false;
private boolean m_enableCats = false;
2011-11-29 05:25:13 +00:00
private int m_apiLevel = 0;
private boolean m_isLoggingIn = false;
2011-12-05 15:14:06 +00:00
private boolean m_isOffline = false;
2012-06-20 05:49:30 +00:00
private int m_offlineModeStatus = 0;
2012-02-29 15:30:52 +00:00
private int m_selectedProduct = -1;
private long m_lastRefresh = 0;
2011-12-07 08:29:19 +00:00
2011-12-05 13:01:44 +00:00
private SQLiteDatabase m_readableDb;
private SQLiteDatabase m_writableDb;
private ActionMode m_headlinesActionMode;
2011-12-09 19:28:37 +00:00
private HeadlinesActionModeCallback m_headlinesActionModeCallback;
private NavigationListener m_navigationListener;
private NavigationAdapter m_navigationAdapter;
private ArrayList<NavigationEntry> m_navigationEntries = new ArrayList<NavigationEntry>();
private class NavigationListener implements ActionBar.OnNavigationListener {
@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
Log.d(TAG, "onNavigationItemSelected: " + itemPosition);
NavigationEntry entry = m_navigationAdapter.getItem(itemPosition);
entry._onItemSelected(itemPosition, m_navigationAdapter.getCount()-1);
return false;
}
}
private class ArticleNavigationEntry extends NavigationEntry {
public ArticleNavigationEntry(Article article) {
super(article.title);
}
@Override
public void onItemSelected() {
}
}
private class RootNavigationEntry extends NavigationEntry {
public RootNavigationEntry(String title) {
super(title);
}
@Override
public void onItemSelected() {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
m_activeFeed = null;
m_selectedArticle = null;
m_activeCategory = null;
if (m_smallScreenMode) {
if (m_enableCats) {
ft.replace(R.id.fragment_container, new FeedCategoriesFragment(), FRAG_CATS);
} else {
ft.replace(R.id.fragment_container, new FeedsFragment(), FRAG_FEEDS);
}
Fragment hf = getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES);
if (hf != null) ft.remove(hf);
Fragment af = getSupportFragmentManager().findFragmentByTag(FRAG_ARTICLE);
if (af != null) ft.remove(af);
} else {
if (m_enableCats) {
ft.replace(R.id.feeds_fragment, new FeedCategoriesFragment(), FRAG_CATS);
} else {
ft.replace(R.id.feeds_fragment, new FeedsFragment(), FRAG_FEEDS);
}
findViewById(R.id.article_fragment).setVisibility(View.GONE);
2012-06-24 10:50:14 +00:00
findViewById(R.id.feeds_fragment).setVisibility(View.VISIBLE);
ft.replace(R.id.headlines_fragment, new DummyFragment(), "");
}
ft.commit();
initMainMenu();
}
}
private class CategoryNavigationEntry extends NavigationEntry {
FeedCategory m_category = null;
public CategoryNavigationEntry(FeedCategory category) {
super(category.title);
m_category = category;
}
@Override
public void onItemSelected() {
m_selectedArticle = null;
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
if (m_smallScreenMode) {
Fragment hf = getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES);
if (hf != null) ft.remove(hf);
Fragment af = getSupportFragmentManager().findFragmentByTag(FRAG_ARTICLE);
if (af != null) ft.remove(af);
2012-06-23 13:11:41 +00:00
if (m_activeFeed.is_cat) {
FeedCategoriesFragment cats = (FeedCategoriesFragment) getSupportFragmentManager().findFragmentByTag(FRAG_CATS);
ft.show(cats);
cats.setSelectedCategory(null);
} else {
FeedsFragment feeds = (FeedsFragment) getSupportFragmentManager().findFragmentByTag(FRAG_FEEDS);
ft.show(feeds);
feeds.setSelectedFeed(null);
}
} else {
findViewById(R.id.article_fragment).setVisibility(View.GONE);
2012-06-24 10:50:14 +00:00
findViewById(R.id.feeds_fragment).setVisibility(View.VISIBLE);
ft.replace(R.id.headlines_fragment, new DummyFragment(), "");
}
ft.commit();
2012-06-23 13:11:41 +00:00
m_activeFeed = null;
refresh();
initMainMenu();
}
}
private class FeedNavigationEntry extends NavigationEntry {
Feed m_feed = null;
public FeedNavigationEntry(Feed feed) {
super(feed.title);
m_feed = feed;
}
@Override
public void onItemSelected() {
m_selectedArticle = null;
if (!m_smallScreenMode)
findViewById(R.id.article_fragment).setVisibility(View.GONE);
viewFeed(m_feed, false);
}
}
private abstract class NavigationEntry {
private String title = null;
private int timesCalled = 0;
public void _onItemSelected(int position, int size) {
Log.d(TAG, "_onItemSelected; TC=" + timesCalled + " P/S=" + position + "/" + size);
if (position == size && timesCalled == 0) {
++timesCalled;
} else {
onItemSelected();
}
}
public NavigationEntry(String title) {
this.title = title;
}
public String toString() {
return title;
}
public abstract void onItemSelected();
}
private class NavigationAdapter extends ArrayAdapter<NavigationEntry> {
public NavigationAdapter(Context context, int textViewResourceId, ArrayList<NavigationEntry> items) {
super(context, textViewResourceId, items);
}
}
2011-12-09 19:28:37 +00:00
private class HeadlinesActionModeCallback implements ActionMode.Callback {
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
deselectAllArticles();
m_headlinesActionMode = null;
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.headlines_action_menu, menu);
return true;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
onOptionsItemSelected(item);
return false;
}
};
private BroadcastReceiver m_broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context content, Intent intent) {
2011-12-07 08:29:19 +00:00
if (intent.getAction().equals(OfflineDownloadService.INTENT_ACTION_SUCCESS)) {
2012-06-20 05:49:30 +00:00
m_offlineModeStatus = 2;
switchOffline();
} else if (intent.getAction().equals(OfflineUploadService.INTENT_ACTION_SUCCESS)) {
//Log.d(TAG, "offline upload service reports success");
refresh();
2011-12-07 08:29:19 +00:00
Toast toast = Toast.makeText(MainActivity.this, R.string.offline_sync_success, Toast.LENGTH_SHORT);
toast.show();
}
2011-12-07 08:29:19 +00:00
}
};
2011-12-07 08:29:19 +00:00
public void updateHeadlines() {
2012-06-19 14:24:22 +00:00
HeadlinesFragment frag = (HeadlinesFragment) getSupportFragmentManager()
2012-06-19 15:12:01 +00:00
.findFragmentByTag(FRAG_HEADLINES);
if (frag != null) {
frag.notifyUpdated();
}
}
2011-12-07 08:29:19 +00:00
@Override
2011-11-29 05:25:13 +00:00
public int getApiLevel() {
return m_apiLevel;
}
2011-12-07 08:29:19 +00:00
private boolean hasPendingOfflineData() {
try {
Cursor c = getReadableDb().query("articles",
new String[] { "COUNT(*)" }, "modified = 1", null, null, null,
null);
if (c.moveToFirst()) {
int modified = c.getInt(0);
c.close();
return modified > 0;
}
} catch (IllegalStateException e) {
// db is closed? ugh
}
2011-12-07 08:29:19 +00:00
return false;
}
private boolean hasOfflineData() {
try {
Cursor c = getReadableDb().query("articles",
new String[] { "COUNT(*)" }, null, null, null, null, null);
if (c.moveToFirst()) {
int modified = c.getInt(0);
c.close();
return modified > 0;
}
} catch (IllegalStateException e) {
// db is closed?
}
2011-12-07 08:29:19 +00:00
return false;
}
@SuppressWarnings({ "unchecked", "serial" })
public void saveArticleUnread(final Article article) {
ApiRequest req = new ApiRequest(getApplicationContext());
2011-12-07 08:29:19 +00:00
HashMap<String, String> map = new HashMap<String, String>() {
{
put("sid", m_sessionId);
put("op", "updateArticle");
put("article_ids", String.valueOf(article.id));
put("mode", article.unread ? "1" : "0");
put("field", "2");
2011-12-07 08:29:19 +00:00
}
};
req.execute(map);
}
@SuppressWarnings({ "unchecked", "serial" })
public void saveArticleMarked(final Article article) {
ApiRequest req = new ApiRequest(getApplicationContext());
2011-12-07 08:29:19 +00:00
HashMap<String, String> map = new HashMap<String, String>() {
{
put("sid", m_sessionId);
put("op", "updateArticle");
put("article_ids", String.valueOf(article.id));
put("mode", article.marked ? "1" : "0");
put("field", "0");
2011-12-07 08:29:19 +00:00
}
};
req.execute(map);
}
@SuppressWarnings({ "unchecked", "serial" })
public void saveArticlePublished(final Article article) {
ApiRequest req = new ApiRequest(getApplicationContext());
2011-12-07 08:29:19 +00:00
HashMap<String, String> map = new HashMap<String, String>() {
{
put("sid", m_sessionId);
put("op", "updateArticle");
put("article_ids", String.valueOf(article.id));
put("mode", article.published ? "1" : "0");
put("field", "1");
2011-12-07 08:29:19 +00:00
}
};
req.execute(map);
}
2011-12-07 08:29:19 +00:00
2012-06-20 11:20:19 +00:00
@SuppressWarnings({ "unchecked", "serial" })
public void saveArticleNote(final Article article, final String note) {
ApiRequest req = new ApiRequest(getApplicationContext());
HashMap<String, String> map = new HashMap<String, String>() {
{
put("sid", m_sessionId);
put("op", "updateArticle");
put("article_ids", String.valueOf(article.id));
put("mode", "1");
put("data", note);
put("field", "3");
}
};
req.execute(map);
}
public static String articlesToIdString(ArticleList articles) {
String tmp = "";
2011-12-07 08:29:19 +00:00
for (Article a : articles)
tmp += String.valueOf(a.id) + ",";
2011-12-07 08:29:19 +00:00
return tmp.replaceAll(",$", "");
}
2011-11-29 12:40:23 +00:00
@SuppressWarnings("unchecked")
public void catchupFeed(final Feed feed) {
Log.d(TAG, "catchupFeed=" + feed);
2011-12-07 08:29:19 +00:00
2011-11-29 12:40:23 +00:00
ApiRequest req = new ApiRequest(getApplicationContext()) {
protected void onPostExecute(JsonElement result) {
refresh();
2011-11-29 12:40:23 +00:00
}
2011-12-07 08:29:19 +00:00
2011-11-29 12:40:23 +00:00
};
2011-12-07 08:29:19 +00:00
2011-11-29 12:40:23 +00:00
@SuppressWarnings("serial")
2011-12-07 08:29:19 +00:00
HashMap<String, String> map = new HashMap<String, String>() {
2011-11-29 12:40:23 +00:00
{
put("sid", m_sessionId);
put("op", "catchupFeed");
put("feed_id", String.valueOf(feed.id));
2011-12-07 08:29:19 +00:00
if (feed.is_cat)
put("is_cat", "1");
}
2011-11-29 12:40:23 +00:00
};
req.execute(map);
}
@SuppressWarnings("unchecked")
private void toggleArticlesMarked(final ArticleList articles) {
ApiRequest req = new ApiRequest(getApplicationContext());
2011-12-07 08:29:19 +00:00
@SuppressWarnings("serial")
2011-12-07 08:29:19 +00:00
HashMap<String, String> map = new HashMap<String, String>() {
{
put("sid", m_sessionId);
put("op", "updateArticle");
put("article_ids", articlesToIdString(articles));
put("mode", "2");
put("field", "0");
2011-12-07 08:29:19 +00:00
}
};
req.execute(map);
}
@SuppressWarnings("unchecked")
private void toggleArticlesUnread(final ArticleList articles) {
ApiRequest req = new ApiRequest(getApplicationContext());
2011-12-07 08:29:19 +00:00
@SuppressWarnings("serial")
2011-12-07 08:29:19 +00:00
HashMap<String, String> map = new HashMap<String, String>() {
{
put("sid", m_sessionId);
put("op", "updateArticle");
put("article_ids", articlesToIdString(articles));
put("mode", "2");
put("field", "2");
2011-12-07 08:29:19 +00:00
}
};
req.execute(map);
refresh();
}
@SuppressWarnings("unchecked")
private void toggleArticlesPublished(final ArticleList articles) {
ApiRequest req = new ApiRequest(getApplicationContext());
2011-12-07 08:29:19 +00:00
@SuppressWarnings("serial")
2011-12-07 08:29:19 +00:00
HashMap<String, String> map = new HashMap<String, String>() {
{
put("sid", m_sessionId);
put("op", "updateArticle");
put("article_ids", articlesToIdString(articles));
put("mode", "2");
put("field", "1");
2011-12-07 08:29:19 +00:00
}
};
req.execute(map);
}
2011-11-23 17:19:35 +00:00
private class RefreshTask extends TimerTask {
@Override
public void run() {
2011-12-07 08:29:19 +00:00
ConnectivityManager cm = (ConnectivityManager) getApplicationContext()
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm.getBackgroundDataSetting()) {
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
2011-12-07 08:29:19 +00:00
if (networkInfo != null && networkInfo.isAvailable()
&& networkInfo.isConnected()) {
2012-01-10 11:13:46 +00:00
runOnUiThread(new Runnable() {
@Override
public void run() {
refresh();
2012-01-10 11:13:46 +00:00
}
});
}
}
2011-11-23 17:19:35 +00:00
}
}
2011-12-07 08:29:19 +00:00
private synchronized void refresh() {
Date date = new Date();
if (m_sessionId != null && date.getTime() - m_lastRefresh > 5000) {
FeedsFragment ff = (FeedsFragment) getSupportFragmentManager()
2012-06-19 15:12:01 +00:00
.findFragmentByTag(FRAG_FEEDS);
2011-12-07 08:29:19 +00:00
if (ff != null) {
Log.d(TAG, "Refreshing feeds...");
ff.refresh(true);
}
FeedCategoriesFragment cf = (FeedCategoriesFragment) getSupportFragmentManager()
.findFragmentByTag(FRAG_CATS);
2011-12-07 08:29:19 +00:00
if (cf != null) {
Log.d(TAG, "Refreshing categories...");
cf.refresh(true);
}
m_lastRefresh = date.getTime();
2011-11-23 17:19:35 +00:00
}
}
2012-06-20 05:49:30 +00:00
/* private synchronized void refreshHeadlines() {
2011-12-07 06:37:06 +00:00
if (m_sessionId != null) {
2012-06-19 14:24:22 +00:00
HeadlinesFragment frag = (HeadlinesFragment) getSupportFragmentManager()
2012-06-19 15:12:01 +00:00
.findFragmentByTag(FRAG_HEADLINES);
2011-12-07 08:29:19 +00:00
2011-12-07 06:37:06 +00:00
Log.d(TAG, "Refreshing headlines...");
2011-12-07 08:29:19 +00:00
2011-12-07 06:37:06 +00:00
if (frag != null) {
frag.refresh(true);
}
}
2012-06-20 05:49:30 +00:00
} */
private void setUnreadOnly(boolean unread) {
2011-11-24 05:59:11 +00:00
m_unreadOnly = unread;
m_lastRefresh = 0;
refresh();
2011-11-24 05:59:11 +00:00
}
2011-12-07 08:29:19 +00:00
@Override
2011-11-24 05:59:11 +00:00
public boolean getUnreadOnly() {
return m_unreadOnly;
}
@Override
public boolean getUnreadArticlesOnly() {
return m_unreadArticlesOnly;
}
2011-11-24 05:59:11 +00:00
@Override
2011-11-23 17:19:35 +00:00
public String getSessionId() {
return m_sessionId;
}
2011-11-22 14:30:09 +00:00
@Override
2011-11-28 09:45:19 +00:00
public boolean isSmallScreen() {
return m_smallScreenMode;
}
2011-12-07 08:29:19 +00:00
@Override
public int getOrientation() {
return getWindowManager().getDefaultDisplay().getOrientation();
}
2011-09-09 16:36:09 +00:00
@Override
public void onCreate(Bundle savedInstanceState) {
initDatabase();
2011-12-07 08:29:19 +00:00
m_prefs = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
2011-09-09 16:36:09 +00:00
2012-06-19 14:24:22 +00:00
m_compatMode = android.os.Build.VERSION.SDK_INT <= 10;
2011-09-08 10:23:44 +00:00
if (m_prefs.getString("theme", "THEME_DARK").equals("THEME_DARK")) {
2011-12-05 08:45:50 +00:00
setTheme(R.style.DarkTheme);
2011-09-08 10:23:44 +00:00
} else {
2011-12-05 08:45:50 +00:00
setTheme(R.style.LightTheme);
2011-09-08 10:23:44 +00:00
}
2011-11-26 08:17:38 +00:00
super.onCreate(savedInstanceState);
2012-06-20 05:49:30 +00:00
if (OfflineDownloadService.INTENT_ACTION_CANCEL.equals(getIntent().getAction())) {
cancelOfflineSync();
}
//Log.d(TAG, "started with intent action=" + intentAction);
2012-06-19 20:44:37 +00:00
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
2011-09-08 10:23:44 +00:00
m_themeName = m_prefs.getString("theme", "THEME_DARK");
2011-12-07 08:29:19 +00:00
if (savedInstanceState != null) {
m_sessionId = savedInstanceState.getString("sessionId");
m_unreadOnly = savedInstanceState.getBoolean("unreadOnly");
m_activeFeed = savedInstanceState.getParcelable("activeFeed");
2011-12-07 08:29:19 +00:00
m_selectedArticle = savedInstanceState
.getParcelable("selectedArticle");
m_unreadArticlesOnly = savedInstanceState
.getBoolean("unreadArticlesOnly");
m_activeCategory = savedInstanceState
.getParcelable("activeCategory");
2011-11-29 05:25:13 +00:00
m_apiLevel = savedInstanceState.getInt("apiLevel");
2012-06-20 05:49:30 +00:00
m_offlineModeStatus = savedInstanceState.getInt("offlineModeStatus");
2011-09-08 10:23:44 +00:00
}
2011-12-07 08:29:19 +00:00
m_enableCats = m_prefs.getBoolean("enable_cats", false);
2011-12-07 08:29:19 +00:00
2012-06-19 14:24:22 +00:00
m_smallScreenMode = m_compatMode || (getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) !=
Configuration.SCREENLAYOUT_SIZE_XLARGE;
setContentView(R.layout.main);
2011-12-05 17:22:31 +00:00
IntentFilter filter = new IntentFilter();
filter.addAction(OfflineDownloadService.INTENT_ACTION_SUCCESS);
filter.addAction(OfflineUploadService.INTENT_ACTION_SUCCESS);
filter.addCategory(Intent.CATEGORY_DEFAULT);
2011-12-07 08:29:19 +00:00
registerReceiver(m_broadcastReceiver, filter);
2011-12-07 08:29:19 +00:00
SharedPreferences localPrefs = getSharedPreferences("localprefs", Context.MODE_PRIVATE);
m_isOffline = localPrefs.getBoolean("offline_mode_active", false);
2011-12-07 08:29:19 +00:00
2011-12-05 15:14:06 +00:00
Log.d(TAG, "m_isOffline=" + m_isOffline);
Log.d(TAG, "m_smallScreenMode=" + m_smallScreenMode);
2012-06-19 14:24:22 +00:00
Log.d(TAG, "m_compatMode=" + m_compatMode);
2011-11-23 10:55:05 +00:00
2012-06-19 14:24:22 +00:00
if (!m_compatMode) {
2012-06-19 17:06:16 +00:00
if (!m_smallScreenMode) {
findViewById(R.id.feeds_fragment).setVisibility(m_selectedArticle != null && getOrientation() % 2 != 0 ? View.GONE : View.VISIBLE);
2012-06-19 17:06:16 +00:00
findViewById(R.id.article_fragment).setVisibility(m_selectedArticle != null ? View.VISIBLE : View.GONE);
}
LayoutTransition transitioner = new LayoutTransition();
((ViewGroup) findViewById(R.id.fragment_container)).setLayoutTransition(transitioner);
2012-06-19 14:24:22 +00:00
m_navigationAdapter = new NavigationAdapter(this, android.R.layout.simple_spinner_dropdown_item, m_navigationEntries);
2012-06-19 14:24:22 +00:00
m_headlinesActionModeCallback = new HeadlinesActionModeCallback();
m_navigationListener = new NavigationListener();
getActionBar().setListNavigationCallbacks(m_navigationAdapter, m_navigationListener);
2012-06-19 14:24:22 +00:00
}
if (m_smallScreenMode) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
if (m_selectedArticle != null) {
ft.hide(getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES));
}
if (m_activeFeed != null) {
if (m_activeFeed.is_cat) {
ft.hide(getSupportFragmentManager().findFragmentByTag(FRAG_CATS));
} else {
ft.hide(getSupportFragmentManager().findFragmentByTag(FRAG_FEEDS));
}
}
ft.commit();
}
2011-12-05 12:19:24 +00:00
2011-12-05 17:22:31 +00:00
if (m_isOffline) {
2011-12-07 08:29:19 +00:00
Intent offline = new Intent(MainActivity.this,
OfflineActivity.class);
2011-12-07 05:51:10 +00:00
startActivity(offline);
finish();
2011-12-05 17:22:31 +00:00
} else {
//AppRater.showRateDialog(this, null);
AppRater.appLaunched(this);
2011-12-05 17:22:31 +00:00
if (m_sessionId != null) {
loginSuccess();
2011-11-28 18:36:13 +00:00
} else {
2011-12-05 17:22:31 +00:00
login();
2011-11-28 18:36:13 +00:00
}
}
2011-12-05 13:01:44 +00:00
}
private void initDatabase() {
2011-12-05 13:01:44 +00:00
DatabaseHelper dh = new DatabaseHelper(getApplicationContext());
m_writableDb = dh.getWritableDatabase();
m_readableDb = dh.getReadableDatabase();
}
2011-12-07 08:29:19 +00:00
2011-12-05 17:22:31 +00:00
public synchronized SQLiteDatabase getReadableDb() {
return m_readableDb;
}
2011-12-07 08:29:19 +00:00
2011-12-05 13:01:44 +00:00
public synchronized SQLiteDatabase getWritableDb() {
return m_writableDb;
2011-09-09 16:36:09 +00:00
}
2011-12-07 08:29:19 +00:00
private void switchOffline() {
2012-06-20 05:49:30 +00:00
if (m_offlineModeStatus == 2) {
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this)
.setMessage(R.string.dialog_offline_success)
.setPositiveButton(R.string.dialog_offline_go,
new Dialog.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
2011-12-07 11:59:30 +00:00
2012-06-20 05:49:30 +00:00
m_offlineModeStatus = 0;
SharedPreferences localPrefs = getSharedPreferences("localprefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = localPrefs.edit();
2011-12-07 11:59:30 +00:00
editor.putBoolean("offline_mode_active", true);
editor.commit();
Intent refresh = new Intent(
2011-12-07 08:29:19 +00:00
MainActivity.this,
OfflineActivity.class);
startActivity(refresh);
finish();
2011-12-07 08:29:19 +00:00
}
})
.setNegativeButton(R.string.dialog_cancel,
new Dialog.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
2012-06-20 05:49:30 +00:00
m_offlineModeStatus = 0;
}
});
AlertDialog dlg = builder.create();
dlg.show();
2012-06-20 05:49:30 +00:00
} else if (m_offlineModeStatus == 0) {
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setMessage(R.string.dialog_offline_switch_prompt)
.setPositiveButton(R.string.dialog_offline_go,
new Dialog.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
if (m_sessionId != null) {
Log.d(TAG, "offline: starting");
2012-06-20 05:49:30 +00:00
m_offlineModeStatus = 1;
Intent intent = new Intent(
MainActivity.this,
OfflineDownloadService.class);
intent.putExtra("sessionId", m_sessionId);
startService(intent);
}
}
})
.setNegativeButton(R.string.dialog_cancel,
new Dialog.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
//
}
});
AlertDialog dlg = builder.create();
dlg.show();
2012-06-20 05:49:30 +00:00
} else if (m_offlineModeStatus == 1) {
cancelOfflineSync();
}
2011-12-05 17:22:31 +00:00
}
2011-12-07 08:29:19 +00:00
2012-06-20 05:49:30 +00:00
private void cancelOfflineSync() {
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setMessage(R.string.dialog_offline_sync_in_progress)
.setNegativeButton(R.string.dialog_offline_sync_stop,
2012-06-20 05:49:30 +00:00
new Dialog.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
if (m_sessionId != null) {
Log.d(TAG, "offline: stopping");
m_offlineModeStatus = 0;
Intent intent = new Intent(
MainActivity.this,
OfflineDownloadService.class);
stopService(intent);
dialog.dismiss();
restart();
2012-06-20 05:49:30 +00:00
}
}
})
.setPositiveButton(R.string.dialog_offline_sync_continue,
2012-06-20 05:49:30 +00:00
new Dialog.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
restart();
2012-06-20 05:49:30 +00:00
}
});
AlertDialog dlg = builder.create();
dlg.show();
}
private void switchOfflineSuccess() {
2011-12-05 17:22:31 +00:00
logout();
2011-12-07 08:29:19 +00:00
// setLoadingStatus(R.string.blank, false);
2011-12-05 17:22:31 +00:00
2011-12-05 18:54:54 +00:00
SharedPreferences.Editor editor = m_prefs.edit();
editor.putBoolean("offline_mode_active", true);
editor.commit();
2011-12-07 08:29:19 +00:00
2011-12-07 05:51:10 +00:00
Intent offline = new Intent(MainActivity.this, OfflineActivity.class);
startActivity(offline);
2011-12-05 18:54:54 +00:00
finish();
2011-12-07 08:29:19 +00:00
2011-12-05 13:01:44 +00:00
}
2011-12-07 08:29:19 +00:00
private void setLoadingStatus(int status, boolean showProgress) {
2011-12-07 08:29:19 +00:00
TextView tv = (TextView) findViewById(R.id.loading_message);
2011-11-22 14:30:09 +00:00
if (tv != null) {
tv.setText(status);
}
2012-06-19 20:44:37 +00:00
setProgressBarIndeterminateVisibility(showProgress);
2011-11-22 14:30:09 +00:00
}
2011-12-07 08:29:19 +00:00
2011-09-08 10:23:44 +00:00
@Override
2011-12-07 08:29:19 +00:00
public void onSaveInstanceState(Bundle out) {
2011-09-08 10:23:44 +00:00
super.onSaveInstanceState(out);
out.putString("sessionId", m_sessionId);
out.putBoolean("unreadOnly", m_unreadOnly);
out.putParcelable("activeFeed", m_activeFeed);
out.putParcelable("selectedArticle", m_selectedArticle);
out.putBoolean("unreadArticlesOnly", m_unreadArticlesOnly);
out.putParcelable("activeCategory", m_activeCategory);
2011-11-29 05:25:13 +00:00
out.putInt("apiLevel", m_apiLevel);
2012-06-20 05:49:30 +00:00
out.putInt("offlineModeStatus", m_offlineModeStatus);
2011-09-08 10:23:44 +00:00
}
2011-09-09 16:36:09 +00:00
2011-09-08 10:23:44 +00:00
@Override
public void onResume() {
super.onResume();
2011-12-07 08:29:19 +00:00
boolean needRefresh = !m_prefs.getString("theme", "THEME_DARK").equals(
m_themeName)
|| m_prefs.getBoolean("enable_cats", false) != m_enableCats;
if (needRefresh) {
restart();
} else if (m_sessionId != null) {
m_refreshTask = new RefreshTask();
m_refreshTimer = new Timer("Refresh");
2011-12-07 08:29:19 +00:00
m_refreshTimer.schedule(m_refreshTask, 60 * 1000L, 120 * 1000L);
} else {
if (!m_isLoggingIn) {
login();
}
2011-11-26 06:31:14 +00:00
}
2011-09-08 10:23:44 +00:00
}
2011-09-09 16:36:09 +00:00
2011-09-08 10:23:44 +00:00
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
2011-12-07 08:29:19 +00:00
2011-11-24 05:59:11 +00:00
m_menu = menu;
2011-12-07 08:29:19 +00:00
initMainMenu();
2011-12-07 08:29:19 +00:00
MenuItem item = menu.findItem(R.id.show_feeds);
2011-12-07 08:29:19 +00:00
if (getUnreadOnly()) {
item.setTitle(R.string.menu_all_feeds);
} else {
item.setTitle(R.string.menu_unread_feeds);
}
2011-12-07 08:29:19 +00:00
/*
* item = menu.findItem(R.id.show_all_articles);
*
* if (getUnreadArticlesOnly()) {
* item.setTitle(R.string.show_all_articles); } else {
* item.setTitle(R.string.show_unread_articles); }
*/
2011-09-09 16:36:09 +00:00
2011-09-08 10:23:44 +00:00
return true;
}
private void setMenuLabel(int id, int labelId) {
2011-11-24 05:59:11 +00:00
MenuItem mi = m_menu.findItem(id);
2011-12-07 08:29:19 +00:00
2011-11-24 05:59:11 +00:00
if (mi != null) {
mi.setTitle(labelId);
}
}
2011-12-07 08:29:19 +00:00
@Override
2011-12-07 05:51:10 +00:00
public void onBackPressed() {
goBack(true);
}
private void closeCategory() {
m_activeCategory = null;
2012-06-19 15:51:47 +00:00
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
2012-06-19 19:49:45 +00:00
if (m_smallScreenMode) {
ft.replace(R.id.fragment_container, new FeedCategoriesFragment(), FRAG_CATS);
} else {
ft.replace(R.id.feeds_fragment, new FeedCategoriesFragment(), FRAG_CATS);
}
2012-06-19 15:51:47 +00:00
ft.commit();
initMainMenu();
refresh();
}
private void deselectAllArticles() {
2012-06-19 14:24:22 +00:00
HeadlinesFragment hf = (HeadlinesFragment) getSupportFragmentManager()
2012-06-19 15:12:01 +00:00
.findFragmentByTag(FRAG_HEADLINES);
if (hf != null) {
ArticleList selected = hf.getSelectedArticles();
if (selected.size() > 0) {
selected.clear();
initMainMenu();
hf.notifyUpdated();
}
}
}
private void goBack(boolean allowQuit) {
2011-12-07 08:29:19 +00:00
if (m_smallScreenMode) {
if (m_selectedArticle != null) {
closeArticle();
} else if (m_activeFeed != null) {
closeFeed();
2011-12-07 08:29:19 +00:00
} else if (m_activeCategory != null) {
closeCategory();
} else if (allowQuit) {
2011-12-07 08:29:19 +00:00
finish();
}
} else {
2011-12-07 08:29:19 +00:00
if (m_selectedArticle != null) {
closeArticle();
refresh();
} else if (m_activeFeed != null) {
2012-06-19 17:06:16 +00:00
m_activeFeed = null;
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.headlines_fragment, new DummyFragment(), "");
ft.commit();
initMainMenu();
} else if (m_activeCategory != null) {
closeCategory();
} else if (allowQuit) {
2011-12-07 08:29:19 +00:00
finish();
}
}
2011-11-28 18:36:13 +00:00
}
@SuppressWarnings("unchecked")
2011-09-08 10:23:44 +00:00
@Override
public boolean onOptionsItemSelected(MenuItem item) {
2012-06-19 14:24:22 +00:00
final HeadlinesFragment hf = (HeadlinesFragment) getSupportFragmentManager()
2012-06-19 15:12:01 +00:00
.findFragmentByTag(FRAG_HEADLINES);
2011-09-08 10:23:44 +00:00
switch (item.getItemId()) {
2012-02-29 15:30:52 +00:00
case R.id.donate:
if (true) {
CharSequence[] items = { "Silver Donation ($2)", "Gold Donation ($5)", "Platinum Donation ($10)" };
Dialog dialog = new Dialog(MainActivity.this);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
.setTitle(R.string.donate_select)
.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
m_selectedProduct = which;
}
}).setNegativeButton(R.string.dialog_close, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).setPositiveButton(R.string.donate_do, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (m_selectedProduct != -1 && m_selectedProduct < 3) {
CharSequence[] products = { "donation_silver", "donation_gold", "donation_platinum2" };
Log.d(TAG, "Selected product: " + products[m_selectedProduct]);
BillingHelper.requestPurchase(MainActivity.this, (String) products[m_selectedProduct]);
dialog.dismiss();
}
}
});
dialog = builder.create();
dialog.show();
}
return true;
case android.R.id.home:
goBack(false);
2012-06-19 14:24:22 +00:00
return true;
case R.id.search:
if (hf != null && m_compatMode) {
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();
hf.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;
2011-09-08 10:23:44 +00:00
case R.id.preferences:
2011-12-07 08:29:19 +00:00
Intent intent = new Intent(MainActivity.this,
PreferencesActivity.class);
2011-09-08 10:23:44 +00:00
startActivityForResult(intent, 0);
return true;
2011-11-24 16:11:10 +00:00
case R.id.update_feeds:
m_lastRefresh = 0;
refresh();
2011-11-24 06:06:47 +00:00
return true;
case R.id.logout:
logout();
return true;
case R.id.login:
login();
return true;
case R.id.go_offline:
switchOffline();
2011-12-05 13:01:44 +00:00
return true;
2012-06-20 11:20:19 +00:00
case R.id.article_set_note:
if (m_selectedArticle != null) {
editArticleNote(m_selectedArticle);
}
return true;
case R.id.headlines_select:
if (hf != null) {
Dialog dialog = new Dialog(this);
AlertDialog.Builder builder = new AlertDialog.Builder(this)
2011-12-07 08:29:19 +00:00
.setTitle(R.string.headlines_select_dialog)
.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) {
switch (which) {
case 0:
hf.setSelection(HeadlinesFragment.ArticlesSelection.ALL);
break;
case 1:
hf.setSelection(HeadlinesFragment.ArticlesSelection.UNREAD);
break;
case 2:
hf.setSelection(HeadlinesFragment.ArticlesSelection.NONE);
break;
}
dialog.cancel();
initMainMenu();
}
});
dialog = builder.create();
dialog.show();
}
return true;
case R.id.headlines_mark_as_read:
if (hf != null) {
ArticleList articles = hf.getUnreadArticles();
2011-12-07 08:29:19 +00:00
for (Article a : articles)
a.unread = false;
2011-12-07 08:29:19 +00:00
hf.notifyUpdated();
2011-12-07 08:29:19 +00:00
ApiRequest req = new ApiRequest(getApplicationContext());
final String articleIds = articlesToIdString(articles);
@SuppressWarnings("serial")
2011-12-07 08:29:19 +00:00
HashMap<String, String> map = new HashMap<String, String>() {
{
put("sid", m_sessionId);
put("op", "updateArticle");
put("article_ids", articleIds);
put("mode", "0");
put("field", "2");
2011-12-07 08:29:19 +00:00
}
};
req.execute(map);
refresh();
}
return true;
2011-11-25 10:46:11 +00:00
case R.id.share_article:
if (android.os.Build.VERSION.SDK_INT < 14) {
shareArticle(m_selectedArticle);
}
2011-11-25 10:46:11 +00:00
return true;
case R.id.toggle_marked:
if (m_selectedArticle != null) {
m_selectedArticle.marked = !m_selectedArticle.marked;
saveArticleMarked(m_selectedArticle);
//updateHeadlines();
}
return true;
case R.id.selection_select_none:
deselectAllArticles();
return true;
2011-11-30 08:54:42 +00:00
case R.id.selection_toggle_unread:
2011-12-07 08:29:19 +00:00
if (hf != null) {
2011-11-30 08:54:42 +00:00
ArticleList selected = hf.getSelectedArticles();
2011-12-07 08:29:19 +00:00
if (selected.size() > 0) {
2011-11-30 08:54:42 +00:00
for (Article a : selected)
a.unread = !a.unread;
2011-12-07 08:29:19 +00:00
2011-11-30 08:54:42 +00:00
toggleArticlesUnread(selected);
hf.notifyUpdated();
}
refresh();
2011-11-30 08:54:42 +00:00
}
return true;
case R.id.selection_toggle_marked:
if (hf != null) {
ArticleList selected = hf.getSelectedArticles();
2011-12-07 08:29:19 +00:00
if (selected.size() > 0) {
for (Article a : selected)
a.marked = !a.marked;
2011-12-07 08:29:19 +00:00
toggleArticlesMarked(selected);
hf.notifyUpdated();
}
}
return true;
case R.id.selection_toggle_published:
if (hf != null) {
ArticleList selected = hf.getSelectedArticles();
2011-12-07 08:29:19 +00:00
if (selected.size() > 0) {
for (Article a : selected)
a.published = !a.published;
toggleArticlesPublished(selected);
hf.notifyUpdated();
}
}
return true;
case R.id.toggle_published:
if (m_selectedArticle != null) {
m_selectedArticle.published = !m_selectedArticle.published;
saveArticlePublished(m_selectedArticle);
updateHeadlines();
}
return true;
2011-12-01 05:32:17 +00:00
case R.id.catchup_above:
if (hf != null) {
if (m_selectedArticle != null) {
ArticleList articles = hf.getAllArticles();
ArticleList tmp = new ArticleList();
for (Article a : articles) {
a.unread = false;
tmp.add(a);
if (m_selectedArticle.id == a.id)
break;
}
if (tmp.size() > 0) {
toggleArticlesUnread(tmp);
hf.notifyUpdated();
}
}
}
return true;
case R.id.set_unread:
if (m_selectedArticle != null) {
m_selectedArticle.unread = true;
saveArticleUnread(m_selectedArticle);
updateHeadlines();
}
return true;
2011-11-24 05:59:11 +00:00
case R.id.show_feeds:
setUnreadOnly(!getUnreadOnly());
2011-11-24 05:59:11 +00:00
if (getUnreadOnly()) {
item.setTitle(R.string.menu_all_feeds);
} else {
2011-11-24 05:59:11 +00:00
item.setTitle(R.string.menu_unread_feeds);
}
2011-12-07 08:29:19 +00:00
return true;
case R.id.set_labels:
if (m_selectedArticle != null) {
2012-06-20 11:20:19 +00:00
editArticleLabels(m_selectedArticle);
}
return true;
2011-09-08 10:23:44 +00:00
default:
2011-12-07 08:29:19 +00:00
Log.d(TAG,
"onOptionsItemSelected, unhandled id=" + item.getItemId());
2011-09-08 10:23:44 +00:00
return super.onOptionsItemSelected(item);
}
}
2012-06-20 11:20:19 +00:00
private void editArticleNote(final Article article) {
String note = "";
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(article.title);
final EditText topicEdit = new EditText(this);
topicEdit.setText(note);
builder.setView(topicEdit);
builder.setPositiveButton(R.string.article_set_note, new Dialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
saveArticleNote(article, topicEdit.getText().toString().trim());
article.published = true;
saveArticlePublished(article);
updateHeadlines();
}
});
builder.setNegativeButton(R.string.dialog_cancel, new Dialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
private void editArticleLabels(Article article) {
final int articleId = article.id;
ApiRequest req = new ApiRequest(getApplicationContext()) {
@Override
protected void onPostExecute(JsonElement result) {
if (result != null) {
Type listType = new TypeToken<List<Label>>() {}.getType();
final List<Label> labels = new Gson().fromJson(result, listType);
CharSequence[] items = new CharSequence[labels.size()];
final int[] itemIds = new int[labels.size()];
boolean[] checkedItems = new boolean[labels.size()];
for (int i = 0; i < labels.size(); i++) {
items[i] = labels.get(i).caption;
itemIds[i] = labels.get(i).id;
checkedItems[i] = labels.get(i).checked;
}
Dialog dialog = new Dialog(MainActivity.this);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
.setTitle(R.string.article_set_labels)
.setMultiChoiceItems(items, checkedItems, new OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, final boolean isChecked) {
final int labelId = itemIds[which];
@SuppressWarnings("serial")
HashMap<String, String> map = new HashMap<String, String>() {
{
put("sid", m_sessionId);
put("op", "setArticleLabel");
put("label_id", String.valueOf(labelId));
put("article_ids", String.valueOf(articleId));
if (isChecked) put("assign", "true");
}
};
ApiRequest req = new ApiRequest(m_context);
req.execute(map);
}
}).setPositiveButton(R.string.dialog_close, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
dialog = builder.create();
dialog.show();
}
}
};
@SuppressWarnings("serial")
HashMap<String, String> map = new HashMap<String, String>() {
{
put("sid", m_sessionId);
put("op", "getLabels");
put("article_id", String.valueOf(articleId));
}
};
req.execute(map);
}
private Intent getShareIntent(Article article) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
2012-03-06 12:32:53 +00:00
//intent.putExtra(Intent.EXTRA_SUBJECT, article.title);
intent.putExtra(Intent.EXTRA_TEXT, article.title + " " + article.link);
return intent;
}
private void shareArticle(Article article) {
2011-11-25 10:46:11 +00:00
if (article != null) {
Intent intent = getShareIntent(article);
2011-12-07 08:29:19 +00:00
startActivity(Intent.createChooser(intent,
getString(R.string.share_article)));
2011-11-25 10:46:11 +00:00
}
}
2011-12-07 08:29:19 +00:00
2011-11-28 18:36:13 +00:00
private void closeArticle() {
m_selectedArticle = null;
2012-06-19 14:24:22 +00:00
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
2012-06-19 17:06:16 +00:00
if (m_smallScreenMode) {
ft.remove(getSupportFragmentManager().findFragmentByTag(FRAG_ARTICLE));
ft.show(getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES));
} else {
findViewById(R.id.feeds_fragment).setVisibility(View.VISIBLE);
findViewById(R.id.article_fragment).setVisibility(View.GONE);
ft.replace(R.id.article_fragment, new DummyFragment(), FRAG_ARTICLE);
updateHeadlines();
2012-06-19 17:06:16 +00:00
}
ft.commit();
initMainMenu();
}
@SuppressLint({ "NewApi", "NewApi", "NewApi" })
@Override
public void initMainMenu() {
if (m_menu != null) {
2011-12-07 08:29:19 +00:00
m_menu.setGroupVisible(R.id.menu_group_feeds, false);
m_menu.setGroupVisible(R.id.menu_group_headlines, false);
m_menu.setGroupVisible(R.id.menu_group_headlines_selection, false);
m_menu.setGroupVisible(R.id.menu_group_article, false);
if (m_sessionId != null) {
2011-11-29 06:46:36 +00:00
m_menu.setGroupVisible(R.id.menu_group_logged_in, true);
m_menu.setGroupVisible(R.id.menu_group_logged_out, false);
2012-06-19 14:24:22 +00:00
HeadlinesFragment hf = (HeadlinesFragment) getSupportFragmentManager()
2012-06-19 15:12:01 +00:00
.findFragmentByTag(FRAG_HEADLINES);
2011-12-07 08:29:19 +00:00
int numSelected = 0;
if (hf != null)
numSelected = hf.getSelectedArticles().size();
2011-12-07 08:29:19 +00:00
if (numSelected != 0) {
2012-06-19 14:24:22 +00:00
if (m_compatMode) {
m_menu.setGroupVisible(R.id.menu_group_headlines_selection, true);
} else {
if (m_headlinesActionMode == null)
m_headlinesActionMode = startActionMode(m_headlinesActionModeCallback);
}
2011-12-07 08:29:19 +00:00
} else if (m_selectedArticle != null) {
m_menu.setGroupVisible(R.id.menu_group_article, true);
} else if (m_activeFeed != null) {
2011-12-07 08:29:19 +00:00
m_menu.setGroupVisible(R.id.menu_group_headlines, true);
MenuItem search = m_menu.findItem(R.id.search);
2012-01-19 10:14:54 +00:00
search.setEnabled(m_apiLevel >= 2);
2012-06-19 14:24:22 +00:00
if (!m_compatMode) {
SearchView searchView = (SearchView) search.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
private String query = "";
2012-06-19 14:24:22 +00:00
@Override
public boolean onQueryTextSubmit(String query) {
HeadlinesFragment frag = (HeadlinesFragment) getSupportFragmentManager()
2012-06-19 15:12:01 +00:00
.findFragmentByTag(FRAG_HEADLINES);
if (frag != null) {
2012-06-19 14:24:22 +00:00
frag.setSearchQuery(query);
this.query = query;
}
2012-06-19 14:24:22 +00:00
return false;
}
2012-06-19 14:24:22 +00:00
@Override
public boolean onQueryTextChange(String newText) {
if (newText.equals("") && !newText.equals(this.query)) {
HeadlinesFragment frag = (HeadlinesFragment) getSupportFragmentManager()
2012-06-19 15:12:01 +00:00
.findFragmentByTag(FRAG_HEADLINES);
2012-06-19 14:24:22 +00:00
if (frag != null) {
frag.setSearchQuery(newText);
this.query = newText;
}
}
return false;
}
});
2012-06-19 14:24:22 +00:00
}
2011-12-07 08:29:19 +00:00
} else {
m_menu.setGroupVisible(R.id.menu_group_feeds, true);
2011-11-29 06:46:36 +00:00
}
if (numSelected == 0 && m_headlinesActionMode != null) {
m_headlinesActionMode.finish();
}
2012-06-19 14:24:22 +00:00
if (!m_compatMode) {
/* if (m_activeFeed != null) {
2012-06-19 14:24:22 +00:00
getActionBar().setTitle(m_activeFeed.title);
} else if (m_activeCategory != null) {
getActionBar().setTitle(m_activeCategory.title);
} else {
getActionBar().setTitle(R.string.app_name);
} */
m_navigationAdapter.clear();
if (m_activeCategory != null || (m_activeFeed != null && m_smallScreenMode)) {
getActionBar().setDisplayShowTitleEnabled(false);
getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
m_navigationAdapter.add(new RootNavigationEntry(getString(R.string.app_name)));
if (m_activeCategory != null)
m_navigationAdapter.add(new CategoryNavigationEntry(m_activeCategory));
if (m_activeFeed != null)
m_navigationAdapter.add(new FeedNavigationEntry(m_activeFeed));
//if (m_selectedArticle != null)
// m_navigationAdapter.add(new ArticleNavigationEntry(m_selectedArticle));
getActionBar().setSelectedNavigationItem(getActionBar().getNavigationItemCount());
2012-06-19 14:24:22 +00:00
} else {
getActionBar().setDisplayShowTitleEnabled(true);
getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
getActionBar().setTitle(R.string.app_name);
}
//if (m_smallScreenMode) {
getActionBar().setDisplayHomeAsUpEnabled(m_selectedArticle != null || m_activeCategory != null || m_activeFeed != null);
//} else {
// getActionBar().setDisplayHomeAsUpEnabled(m_selectedArticle != null || m_activeCategory != null || m_activeFeed != null);
//}
2012-06-19 14:24:22 +00:00
if (android.os.Build.VERSION.SDK_INT >= 14) {
ShareActionProvider shareProvider = (ShareActionProvider) m_menu.findItem(R.id.share_article).getActionProvider();
if (m_selectedArticle != null) {
Log.d(TAG, "setting up share provider");
shareProvider.setShareIntent(getShareIntent(m_selectedArticle));
}
}
2012-06-19 14:24:22 +00:00
}
m_menu.findItem(R.id.set_labels).setEnabled(m_apiLevel >= 1);
2012-06-20 11:20:19 +00:00
m_menu.findItem(R.id.article_set_note).setEnabled(m_apiLevel >= 1);
2012-02-29 15:30:52 +00:00
m_menu.findItem(R.id.donate).setVisible(BillingHelper.isBillingSupported());
2011-11-29 06:46:36 +00:00
} else {
m_menu.setGroupVisible(R.id.menu_group_logged_in, false);
2011-12-07 08:29:19 +00:00
m_menu.setGroupVisible(R.id.menu_group_logged_out, true);
2011-11-29 06:46:36 +00:00
}
2011-12-07 08:29:19 +00:00
}
}
2011-12-07 08:29:19 +00:00
@Override
public void onPause() {
super.onPause();
2011-12-07 08:29:19 +00:00
if (m_refreshTask != null) {
m_refreshTask.cancel();
m_refreshTask = null;
}
2011-12-07 08:29:19 +00:00
if (m_refreshTimer != null) {
m_refreshTimer.cancel();
m_refreshTimer = null;
}
}
2011-12-07 08:29:19 +00:00
2011-12-05 17:22:31 +00:00
@Override
public void onDestroy() {
super.onDestroy();
2011-12-07 08:29:19 +00:00
unregisterReceiver(m_broadcastReceiver);
2011-12-07 08:29:19 +00:00
2011-12-05 17:22:31 +00:00
m_readableDb.close();
m_writableDb.close();
2011-12-07 08:29:19 +00:00
2011-12-05 17:22:31 +00:00
}
private void syncOfflineData() {
Log.d(TAG, "offlineSync: starting");
Intent intent = new Intent(
MainActivity.this,
OfflineUploadService.class);
intent.putExtra("sessionId", m_sessionId);
startService(intent);
}
2011-12-07 08:29:19 +00:00
private void loginSuccess() {
2012-01-17 15:57:59 +00:00
findViewById(R.id.loading_container).setVisibility(View.GONE);
2012-06-19 20:44:37 +00:00
setProgressBarIndeterminateVisibility(false);
2011-12-05 15:14:06 +00:00
m_isOffline = false;
2011-12-07 08:29:19 +00:00
2012-02-29 15:30:52 +00:00
startService(new Intent(MainActivity.this, BillingService.class));
initMainMenu();
2012-02-29 15:30:52 +00:00
if (m_refreshTask != null) {
m_refreshTask.cancel();
m_refreshTask = null;
}
2011-12-07 08:29:19 +00:00
if (m_refreshTimer != null) {
m_refreshTimer.cancel();
m_refreshTimer = null;
}
2011-12-07 08:29:19 +00:00
m_refreshTask = new RefreshTask();
m_refreshTimer = new Timer("Refresh");
2011-12-07 08:29:19 +00:00
m_refreshTimer.schedule(m_refreshTask, 60 * 1000L, 120 * 1000L);
}
2011-12-07 08:29:19 +00:00
2011-11-22 14:30:09 +00:00
private class LoginRequest extends ApiRequest {
public LoginRequest(Context context) {
super(context);
}
2011-12-07 08:29:19 +00:00
@SuppressWarnings("unchecked")
2011-11-22 14:30:09 +00:00
protected void onPostExecute(JsonElement result) {
m_isLoggingIn = false;
2011-11-22 14:30:09 +00:00
if (result != null) {
2011-12-07 08:29:19 +00:00
try {
2011-11-29 04:03:38 +00:00
JsonObject content = result.getAsJsonObject();
if (content != null) {
m_sessionId = content.get("session_id").getAsString();
2011-12-07 08:29:19 +00:00
2011-11-29 04:03:38 +00:00
Log.d(TAG, "Authenticated!");
2011-12-07 08:29:19 +00:00
2011-11-29 05:25:13 +00:00
ApiRequest req = new ApiRequest(m_context) {
protected void onPostExecute(JsonElement result) {
2011-12-13 12:54:01 +00:00
m_apiLevel = 0;
2011-11-29 05:25:13 +00:00
if (result != null) {
2011-12-13 12:54:01 +00:00
try {
m_apiLevel = result.getAsJsonObject()
.get("level").getAsInt();
} catch (Exception e) {
e.printStackTrace();
}
2011-11-29 05:25:13 +00:00
}
2011-12-07 08:29:19 +00:00
2011-11-29 05:25:13 +00:00
Log.d(TAG, "Received API level: " + m_apiLevel);
2011-12-07 08:29:19 +00:00
if (hasPendingOfflineData())
syncOfflineData();
2011-12-07 08:29:19 +00:00
2012-06-19 14:24:22 +00:00
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
if (m_enableCats) {
FeedCategoriesFragment frag = new FeedCategoriesFragment();
2012-06-19 17:06:16 +00:00
if (m_smallScreenMode) {
ft.replace(R.id.fragment_container, frag, FRAG_CATS);
} else {
ft.replace(R.id.feeds_fragment, frag, FRAG_CATS);
}
} else {
FeedsFragment frag = new FeedsFragment();
2012-06-19 17:06:16 +00:00
if (m_smallScreenMode) {
ft.replace(R.id.fragment_container, frag, FRAG_FEEDS);
} else {
ft.replace(R.id.feeds_fragment, frag, FRAG_FEEDS);
}
}
try {
ft.commit();
} catch (IllegalStateException e) {
e.printStackTrace();
}
loginSuccess();
2011-11-29 05:25:13 +00:00
}
};
2011-12-07 08:29:19 +00:00
@SuppressWarnings("serial")
2011-12-07 08:29:19 +00:00
HashMap<String, String> map = new HashMap<String, String>() {
2011-11-29 05:25:13 +00:00
{
put("sid", m_sessionId);
put("op", "getApiLevel");
2011-12-07 08:29:19 +00:00
}
2011-11-29 05:25:13 +00:00
};
req.execute(map);
2011-12-07 08:29:19 +00:00
2011-11-29 04:03:38 +00:00
setLoadingStatus(R.string.loading_message, true);
2011-12-07 08:29:19 +00:00
2011-11-29 04:03:38 +00:00
return;
2011-11-22 14:30:09 +00:00
}
2011-12-07 08:29:19 +00:00
2011-11-22 14:30:09 +00:00
} catch (Exception e) {
2011-12-07 08:29:19 +00:00
e.printStackTrace();
2011-11-22 14:30:09 +00:00
}
}
2011-12-07 08:29:19 +00:00
2011-11-29 04:03:38 +00:00
m_sessionId = null;
setLoadingStatus(getErrorMessage(), false);
2011-12-07 08:29:19 +00:00
if (hasOfflineData()) {
2011-12-07 08:29:19 +00:00
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this)
.setMessage(R.string.dialog_offline_prompt)
.setPositiveButton(R.string.dialog_offline_go,
new Dialog.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
switchOfflineSuccess();
}
})
.setNegativeButton(R.string.dialog_cancel,
new Dialog.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
//
}
});
AlertDialog dlg = builder.create();
dlg.show();
2011-12-07 08:29:19 +00:00
}
// m_menu.findItem(R.id.login).setVisible(true);
}
2011-11-29 04:03:38 +00:00
2011-11-22 14:30:09 +00:00
}
@Override
public void onFeedSelected(Feed feed) {
viewFeed(feed, false);
}
public Article getSelectedArticle() {
return m_selectedArticle;
}
2011-12-07 08:29:19 +00:00
public void viewFeed(Feed feed, boolean append) {
Log.d(TAG, "viewFeeed/" + feed.id);
m_activeFeed = feed;
2011-12-07 08:29:19 +00:00
if (!append) {
m_selectedArticle = null;
if (m_menu != null) {
MenuItem search = m_menu.findItem(R.id.search);
2012-06-19 14:24:22 +00:00
if (search != null && !m_compatMode) {
SearchView sv = (SearchView) search.getActionView();
sv.setQuery("", false);
}
}
2012-06-19 15:12:01 +00:00
HeadlinesFragment hf = new HeadlinesFragment(feed);
2011-12-07 08:29:19 +00:00
2012-06-19 14:24:22 +00:00
FragmentTransaction ft = getSupportFragmentManager()
2011-12-07 08:29:19 +00:00
.beginTransaction();
2012-06-19 17:06:16 +00:00
if (m_smallScreenMode) {
Fragment cats = getSupportFragmentManager().findFragmentByTag(FRAG_CATS);
if (cats != null) ft.hide(cats);
Fragment feeds = getSupportFragmentManager().findFragmentByTag(FRAG_FEEDS);
if (feeds != null) ft.hide(feeds);
ft.add(R.id.fragment_container, hf, FRAG_HEADLINES);
2012-06-19 17:06:16 +00:00
} else {
findViewById(R.id.article_fragment).setVisibility(View.GONE);
2012-06-19 17:06:16 +00:00
findViewById(R.id.headlines_fragment).setVisibility(View.VISIBLE);
ft.replace(R.id.headlines_fragment, hf, FRAG_HEADLINES);
}
ft.commit();
} else {
2012-06-19 14:24:22 +00:00
HeadlinesFragment hf = (HeadlinesFragment) getSupportFragmentManager()
2012-06-19 15:12:01 +00:00
.findFragmentByTag(FRAG_HEADLINES);
if (hf != null) {
hf.refresh(true);
}
}
initMainMenu();
}
public void viewCategory(FeedCategory cat, boolean openAsFeed) {
2011-12-07 08:29:19 +00:00
Log.d(TAG, "viewCategory");
2011-12-07 08:29:19 +00:00
if (!openAsFeed) {
2011-12-07 08:29:19 +00:00
m_activeCategory = cat;
2012-06-19 15:12:01 +00:00
FeedsFragment frag = new FeedsFragment(cat);
2011-12-07 08:29:19 +00:00
2012-06-19 14:24:22 +00:00
FragmentTransaction ft = getSupportFragmentManager()
2011-12-07 08:29:19 +00:00
.beginTransaction();
2012-06-19 17:06:16 +00:00
if (m_smallScreenMode) {
ft.replace(R.id.fragment_container, frag, FRAG_FEEDS);
} else {
ft.replace(R.id.feeds_fragment, frag, FRAG_FEEDS);
}
2011-12-07 08:29:19 +00:00
ft.commit();
2012-06-19 15:51:47 +00:00
} else {
Feed feed = new Feed(cat.id, cat.title, true);
2011-12-07 08:29:19 +00:00
if (m_menu != null) {
MenuItem search = m_menu.findItem(R.id.search);
2012-06-19 14:24:22 +00:00
if (search != null && !m_compatMode) {
SearchView sv = (SearchView) search.getActionView();
sv.setQuery("", false);
}
}
viewFeed(feed, false);
}
2011-12-07 08:29:19 +00:00
initMainMenu();
}
2011-11-28 09:45:19 +00:00
public void openArticle(Article article, int compatAnimation) {
m_selectedArticle = article;
2011-12-07 08:29:19 +00:00
2011-11-28 09:45:19 +00:00
if (article.unread) {
article.unread = false;
saveArticleUnread(article);
}
2011-12-07 08:29:19 +00:00
initMainMenu();
2011-11-28 09:45:19 +00:00
2012-06-19 14:24:22 +00:00
HeadlinesFragment hf = (HeadlinesFragment) getSupportFragmentManager()
2012-06-19 15:12:01 +00:00
.findFragmentByTag(FRAG_HEADLINES);
2011-12-07 08:29:19 +00:00
2011-11-28 09:45:19 +00:00
if (hf != null) {
hf.setActiveArticle(article);
2011-11-28 09:45:19 +00:00
}
2011-12-07 06:37:06 +00:00
Fragment frag;
if (m_smallScreenMode || m_prefs.getBoolean("tablet_article_swipe", false)) {
frag = new ArticlePager(article);
} else {
frag = new ArticleFragment(article);
}
2011-12-07 08:29:19 +00:00
2012-06-19 14:24:22 +00:00
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
2012-06-19 17:06:16 +00:00
if (m_smallScreenMode) {
ft.hide(getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES));
ft.add(R.id.fragment_container, frag, FRAG_ARTICLE);
} else {
findViewById(R.id.feeds_fragment).setVisibility(getOrientation() % 2 != 0 ? View.GONE : View.VISIBLE);
2012-06-19 17:06:16 +00:00
findViewById(R.id.article_fragment).setVisibility(View.VISIBLE);
ft.replace(R.id.article_fragment, frag, FRAG_ARTICLE);
if (getOrientation() % 2 == 0) refresh();
2012-06-19 17:06:16 +00:00
}
2011-12-07 08:29:19 +00:00
ft.commit();
}
2011-12-07 08:29:19 +00:00
2012-06-20 05:49:30 +00:00
/* private Feed getActiveFeed() {
return m_activeFeed;
}
2012-06-19 15:12:01 +00:00
private FeedCategory getActiveCategory() {
return m_activeCategory;
2012-06-20 05:49:30 +00:00
} */
2011-12-07 08:29:19 +00:00
private void logout() {
if (m_refreshTask != null) {
m_refreshTask.cancel();
m_refreshTask = null;
}
2011-12-07 08:29:19 +00:00
if (m_refreshTimer != null) {
m_refreshTimer.cancel();
m_refreshTimer = null;
}
m_sessionId = null;
2011-12-07 08:29:19 +00:00
findViewById(R.id.loading_container).setVisibility(View.VISIBLE);
2011-12-07 08:29:19 +00:00
TextView tv = (TextView) findViewById(R.id.loading_message);
if (tv != null) {
2011-12-07 08:29:19 +00:00
tv.setText(R.string.login_ready);
}
2012-02-03 10:05:57 +00:00
initMainMenu();
}
@SuppressWarnings({ "unchecked", "serial" })
2011-12-07 08:29:19 +00:00
public void login() {
logout();
2011-12-07 08:29:19 +00:00
2011-12-01 07:01:33 +00:00
if (m_prefs.getString("ttrss_url", "").trim().length() == 0) {
setLoadingStatus(R.string.login_need_configure, false);
// launch preferences
Intent intent = new Intent(MainActivity.this,
PreferencesActivity.class);
startActivityForResult(intent, 0);
2011-12-07 08:29:19 +00:00
} else {
2011-12-07 08:29:19 +00:00
LoginRequest ar = new LoginRequest(getApplicationContext());
2011-12-07 08:29:19 +00:00
HashMap<String, String> map = new HashMap<String, String>() {
{
put("op", "login");
2011-12-01 07:01:33 +00:00
put("user", m_prefs.getString("login", "").trim());
put("password", m_prefs.getString("password", "").trim());
2011-12-07 08:29:19 +00:00
}
};
2011-12-07 08:29:19 +00:00
ar.execute(map);
2011-12-07 08:29:19 +00:00
setLoadingStatus(R.string.login_in_progress, true);
m_isLoggingIn = true;
}
}
2011-12-07 08:29:19 +00:00
@Override
public boolean onContextItemSelected(MenuItem item) {
2011-12-07 08:29:19 +00:00
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
2012-06-19 14:24:22 +00:00
HeadlinesFragment hf = (HeadlinesFragment) getSupportFragmentManager()
2012-06-19 15:12:01 +00:00
.findFragmentByTag(FRAG_HEADLINES);
2012-06-19 14:24:22 +00:00
FeedsFragment ff = (FeedsFragment) getSupportFragmentManager()
2012-06-19 15:12:01 +00:00
.findFragmentByTag(FRAG_FEEDS);
2012-06-19 14:24:22 +00:00
FeedCategoriesFragment cf = (FeedCategoriesFragment) getSupportFragmentManager()
2012-06-19 15:12:01 +00:00
.findFragmentByTag(FRAG_CATS);
2011-12-07 08:29:19 +00:00
switch (item.getItemId()) {
case R.id.article_link_copy:
if (true) {
Article article = null;
if (m_selectedArticle != null) {
article = m_selectedArticle;
} else if (info != null) {
article = hf.getArticleAtPosition(info.position);
2012-03-12 08:51:57 +00:00
}
if (article != null) {
copyToClipboard(article.link);
}
2012-03-12 08:51:57 +00:00
}
return true;
case R.id.article_link_share:
if (m_selectedArticle != null) {
shareArticle(m_selectedArticle);
}
2012-06-20 11:20:19 +00:00
return true;
case R.id.set_labels:
if (true) {
Article article = null;
if (m_selectedArticle != null) {
article = m_selectedArticle;
} else if (info != null) {
article = hf.getArticleAtPosition(info.position);
}
if (article != null) {
editArticleLabels(article);
}
}
return true;
case R.id.article_set_note:
if (true) {
Article article = null;
if (m_selectedArticle != null) {
article = m_selectedArticle;
} else if (info != null) {
article = hf.getArticleAtPosition(info.position);
}
if (article != null) {
editArticleNote(article);
}
}
return true;
2011-12-07 08:29:19 +00:00
case R.id.browse_articles:
if (cf != null) {
FeedCategory cat = cf.getCategoryAtPosition(info.position);
if (cat != null) {
viewCategory(cat, true);
cf.setSelectedCategory(cat);
}
}
return true;
case R.id.browse_feeds:
if (cf != null) {
FeedCategory cat = cf.getCategoryAtPosition(info.position);
if (cat != null) {
viewCategory(cat, false);
cf.setSelectedCategory(cat);
}
}
return true;
case R.id.catchup_category:
if (cf != null) {
FeedCategory cat = cf.getCategoryAtPosition(info.position);
if (cat != null) {
catchupFeed(new Feed(cat.id, cat.title, true));
}
}
return true;
case R.id.catchup_feed:
if (ff != null) {
Feed feed = ff.getFeedAtPosition(info.position);
if (feed != null) {
catchupFeed(feed);
}
}
return true;
case R.id.selection_toggle_marked:
if (hf != null) {
ArticleList selected = hf.getSelectedArticles();
2011-12-07 08:29:19 +00:00
if (selected.size() > 0) {
for (Article a : selected)
a.marked = !a.marked;
2011-12-07 08:29:19 +00:00
toggleArticlesMarked(selected);
hf.notifyUpdated();
} else {
Article article = hf.getArticleAtPosition(info.position);
if (article != null) {
article.marked = !article.marked;
saveArticleMarked(article);
hf.notifyUpdated();
}
}
}
return true;
case R.id.selection_toggle_published:
if (hf != null) {
ArticleList selected = hf.getSelectedArticles();
2011-12-07 08:29:19 +00:00
if (selected.size() > 0) {
for (Article a : selected)
a.published = !a.published;
toggleArticlesPublished(selected);
hf.notifyUpdated();
} else {
Article article = hf.getArticleAtPosition(info.position);
if (article != null) {
article.published = !article.published;
saveArticlePublished(article);
hf.notifyUpdated();
}
}
}
return true;
case R.id.selection_toggle_unread:
2011-12-07 08:29:19 +00:00
if (hf != null) {
ArticleList selected = hf.getSelectedArticles();
2011-12-07 08:29:19 +00:00
if (selected.size() > 0) {
for (Article a : selected)
a.unread = !a.unread;
2011-12-07 08:29:19 +00:00
toggleArticlesUnread(selected);
hf.notifyUpdated();
} else {
Article article = hf.getArticleAtPosition(info.position);
if (article != null) {
article.unread = !article.unread;
saveArticleUnread(article);
hf.notifyUpdated();
}
}
}
return true;
case R.id.share_article:
if (hf != null) {
Article article = hf.getArticleAtPosition(info.position);
if (article != null)
shareArticle(article);
}
return true;
case R.id.catchup_above:
if (hf != null) {
Article article = hf.getArticleAtPosition(info.position);
if (article != null) {
2011-12-01 05:32:17 +00:00
ArticleList articles = hf.getAllArticles();
ArticleList tmp = new ArticleList();
for (Article a : articles) {
a.unread = false;
tmp.add(a);
if (article.id == a.id)
break;
}
if (tmp.size() > 0) {
toggleArticlesUnread(tmp);
hf.notifyUpdated();
}
}
}
return true;
2011-12-07 08:29:19 +00:00
/*
* case R.id.set_unread: if (hf != null) { Article article =
* hf.getArticleAtPosition(info.position); if (article != null) {
* article.unread = true; saveArticleUnread(article); } } break;
*/
default:
Log.d(TAG,
"onContextItemSelected, unhandled id=" + item.getItemId());
return super.onContextItemSelected(item);
}
}
2011-11-28 09:45:19 +00:00
@Override
public Article getRelativeArticle(Article article, RelativeArticle ra) {
2012-06-19 14:24:22 +00:00
HeadlinesFragment frag = (HeadlinesFragment) getSupportFragmentManager()
2012-06-19 15:12:01 +00:00
.findFragmentByTag(FRAG_HEADLINES);
2011-11-28 09:45:19 +00:00
if (frag != null) {
ArticleList articles = frag.getAllArticles();
for (int i = 0; i < articles.size(); i++) {
Article a = articles.get(i);
2011-12-07 08:29:19 +00:00
2011-11-28 09:45:19 +00:00
if (a.id == article.id) {
if (ra == RelativeArticle.AFTER) {
try {
2011-12-07 08:29:19 +00:00
return articles.get(i + 1);
2011-11-28 09:45:19 +00:00
} catch (IndexOutOfBoundsException e) {
return null;
}
} else {
try {
2011-12-07 08:29:19 +00:00
return articles.get(i - 1);
2011-11-28 09:45:19 +00:00
} catch (IndexOutOfBoundsException e) {
return null;
}
}
}
}
2011-12-07 08:29:19 +00:00
}
2011-11-28 09:45:19 +00:00
return null;
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
2011-12-07 08:29:19 +00:00
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
2012-06-19 14:24:22 +00:00
HeadlinesFragment hf = (HeadlinesFragment) getSupportFragmentManager()
2012-06-19 15:12:01 +00:00
.findFragmentByTag(FRAG_HEADLINES);
2011-12-07 08:29:19 +00:00
if (hf != null && m_activeFeed != null) {
Article base = hf.getActiveArticle();
2011-12-07 08:29:19 +00:00
Article next = base != null ? getRelativeArticle(base,
RelativeArticle.AFTER) : hf.getArticleAtPosition(0);
2011-12-07 08:29:19 +00:00
if (next != null) {
hf.setActiveArticle(next);
2011-12-07 08:29:19 +00:00
boolean combinedMode = m_prefs.getBoolean(
"combined_mode", false);
2011-12-07 08:29:19 +00:00
if (combinedMode || m_selectedArticle == null) {
next.unread = false;
saveArticleUnread(next);
} else {
openArticle(next, 0);
}
}
}
}
2011-12-07 08:29:19 +00:00
return true;
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_UP) {
2012-06-19 14:24:22 +00:00
HeadlinesFragment hf = (HeadlinesFragment) getSupportFragmentManager()
2012-06-19 15:12:01 +00:00
.findFragmentByTag(FRAG_HEADLINES);
2011-12-07 08:29:19 +00:00
if (hf != null && m_activeFeed != null) {
Article base = hf.getActiveArticle();
2011-12-07 08:29:19 +00:00
Article prev = base != null ? getRelativeArticle(base,
RelativeArticle.BEFORE) : hf
.getArticleAtPosition(0);
2011-12-07 08:29:19 +00:00
if (prev != null) {
hf.setActiveArticle(prev);
2011-12-07 06:58:32 +00:00
2011-12-07 08:29:19 +00:00
boolean combinedMode = m_prefs.getBoolean(
"combined_mode", false);
2011-12-07 06:37:06 +00:00
2011-12-07 08:29:19 +00:00
if (combinedMode || m_selectedArticle == null) {
prev.unread = false;
saveArticleUnread(prev);
} else {
openArticle(prev, 0);
}
}
}
2011-12-07 06:37:06 +00:00
}
2011-12-07 08:29:19 +00:00
return true;
default:
return super.dispatchKeyEvent(event);
2011-12-07 06:37:06 +00:00
}
2011-12-07 08:29:19 +00:00
}
2011-12-07 06:37:06 +00:00
2011-12-07 08:29:19 +00:00
@Override
public void onCatSelected(FeedCategory cat) {
Log.d(TAG, "onCatSelected");
boolean browse = m_prefs.getBoolean("browse_cats_like_feeds", false);
2011-12-07 06:37:06 +00:00
2011-12-07 08:29:19 +00:00
viewCategory(cat, browse && cat.id >= 0);
2011-12-07 06:37:06 +00:00
}
@Override
public void setSelectedArticle(Article article) {
m_selectedArticle = article;
updateHeadlines();
initMainMenu();
}
@Override
public void copyToClipboard(String str) {
if (android.os.Build.VERSION.SDK_INT < 11) {
@SuppressWarnings("deprecation")
android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
clipboard.setText(str);
} else {
android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
clipboard.setText(str);
}
Toast toast = Toast.makeText(MainActivity.this, R.string.text_copied_to_clipboard, Toast.LENGTH_SHORT);
toast.show();
}
private void closeFeed() {
if (m_smallScreenMode && m_activeFeed != null) {
if (m_activeFeed.is_cat) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment headlines = getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES);
FeedCategoriesFragment cats = (FeedCategoriesFragment) getSupportFragmentManager().findFragmentByTag(FRAG_CATS);
ft.show(cats);
ft.remove(headlines);
cats.setSelectedCategory(null);
//ft.replace(R.id.fragment_container, new FeedCategoriesFragment(), FRAG_CATS);
ft.commit();
} else {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment headlines = getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES);
FeedsFragment feeds = (FeedsFragment) getSupportFragmentManager().findFragmentByTag(FRAG_FEEDS);
ft.show(feeds);
ft.remove(headlines);
feeds.setSelectedFeed(null);
ft.commit();
}
}
m_activeFeed = null;
refresh();
initMainMenu();
}
@Override
public void restart() {
Intent refresh = new Intent(MainActivity.this, MainActivity.class);
refresh.putExtra("sessionId", m_sessionId);
startActivity(refresh);
finish();
}
2011-09-07 05:56:46 +00:00
}