tt-rss-android/src/org/fox/ttrss/offline/OfflineActivity.java

1204 lines
33 KiB
Java
Raw Normal View History

2012-06-19 10:18:00 +00:00
package org.fox.ttrss.offline;
2012-06-19 10:18:00 +00:00
import org.fox.ttrss.DummyFragment;
import org.fox.ttrss.MainActivity;
import org.fox.ttrss.OnlineServices;
import org.fox.ttrss.OnlineServices.RelativeArticle;
2012-06-19 14:24:22 +00:00
import org.fox.ttrss.PreferencesActivity;
2012-06-19 10:18:00 +00:00
import org.fox.ttrss.util.DatabaseHelper;
2012-06-19 14:24:22 +00:00
import org.fox.ttrss.R;
import android.animation.LayoutTransition;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.NotificationManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.BaseColumns;
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;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView.AdapterContextMenuInfo;
2012-01-19 12:33:39 +00:00
import android.widget.EditText;
import android.widget.SearchView;
import android.widget.TextView;
2012-03-12 08:51:57 +00:00
import android.widget.Toast;
2012-06-19 14:24:22 +00:00
public class OfflineActivity extends FragmentActivity implements
2011-12-07 08:29:19 +00:00
OfflineServices {
private final String TAG = this.getClass().getSimpleName();
private SharedPreferences m_prefs;
private String m_themeName = "";
private Menu m_menu;
private boolean m_smallScreenMode;
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-12-07 08:29:19 +00:00
private int m_activeFeedId = 0;
private int m_selectedArticleId = 0;
2011-12-07 08:29:19 +00:00
private SQLiteDatabase m_readableDb;
private SQLiteDatabase m_writableDb;
2011-12-07 08:29:19 +00:00
2011-12-06 13:01:08 +00:00
@Override
public boolean isSmallScreen() {
return m_smallScreenMode;
}
2011-12-07 08:29:19 +00:00
private ActionMode m_headlinesActionMode;
2011-12-09 19:28:37 +00:00
private HeadlinesActionModeCallback m_headlinesActionModeCallback;
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;
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
initDatabase();
2011-12-07 08:29:19 +00:00
m_prefs = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
2012-06-19 14:24:22 +00:00
m_compatMode = android.os.Build.VERSION.SDK_INT <= 10;
if (m_prefs.getString("theme", "THEME_DARK").equals("THEME_DARK")) {
setTheme(R.style.DarkTheme);
} else {
setTheme(R.style.LightTheme);
}
super.onCreate(savedInstanceState);
2011-12-07 08:29:19 +00:00
NotificationManager nmgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nmgr.cancel(OfflineDownloadService.NOTIFY_DOWNLOADING);
m_themeName = m_prefs.getString("theme", "THEME_DARK");
2011-12-07 08:29:19 +00:00
if (savedInstanceState != null) {
m_unreadOnly = savedInstanceState.getBoolean("unreadOnly");
2011-12-07 08:29:19 +00:00
m_unreadArticlesOnly = savedInstanceState
.getBoolean("unreadArticlesOnly");
m_activeFeedId = savedInstanceState.getInt("offlineActiveFeedId");
m_selectedArticleId = savedInstanceState.getInt("offlineArticleId");
}
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;
2011-12-07 08:29:19 +00:00
setContentView(R.layout.main);
Log.d(TAG, "m_smallScreenMode=" + m_smallScreenMode);
2012-06-19 14:24:22 +00:00
Log.d(TAG, "m_compatMode=" + m_compatMode);
2012-06-19 14:24:22 +00:00
if (!m_compatMode) {
if (android.os.Build.VERSION.SDK_INT < 14 /* || android.os.Build.VERSION.SDK_INT == 15 */) {
if (!m_smallScreenMode) {
LayoutTransition transitioner = new LayoutTransition();
((ViewGroup) findViewById(R.id.main)).setLayoutTransition(transitioner);
}
}
2012-06-19 14:24:22 +00:00
m_headlinesActionModeCallback = new HeadlinesActionModeCallback();
}
initMainMenu();
2011-12-07 08:29:19 +00:00
2012-01-17 15:57:59 +00:00
findViewById(R.id.loading_container).setVisibility(View.GONE);
2012-06-19 17:06:16 +00:00
/* if (m_smallScreenMode) {
2011-12-06 06:16:41 +00:00
if (m_selectedArticleId != 0) {
findViewById(R.id.feeds_fragment).setVisibility(View.GONE);
findViewById(R.id.cats_fragment).setVisibility(View.GONE);
findViewById(R.id.headlines_fragment).setVisibility(View.GONE);
} else if (m_activeFeedId != 0) {
findViewById(R.id.feeds_fragment).setVisibility(View.GONE);
findViewById(R.id.article_fragment).setVisibility(View.GONE);
findViewById(R.id.cats_fragment).setVisibility(View.GONE);
} else {
//findViewById(R.id.headlines_fragment).setVisibility(View.GONE);
2011-12-07 08:29:19 +00:00
// findViewById(R.id.article_fragment).setVisibility(View.GONE);
2011-12-06 06:16:41 +00:00
findViewById(R.id.cats_fragment).setVisibility(View.GONE);
}
} else {
if (m_selectedArticleId == 0) {
findViewById(R.id.article_fragment).setVisibility(View.GONE);
findViewById(R.id.cats_fragment).setVisibility(View.GONE);
2011-12-07 08:29:19 +00:00
2011-12-06 06:16:41 +00:00
} else {
findViewById(R.id.feeds_fragment).setVisibility(View.GONE);
findViewById(R.id.cats_fragment).setVisibility(View.GONE);
}
2012-06-19 17:06:16 +00:00
} */
2011-12-07 08:29:19 +00:00
if (m_activeFeedId == 0) {
2012-06-19 14:24:22 +00:00
FragmentTransaction ft = getSupportFragmentManager()
2011-12-07 08:29:19 +00:00
.beginTransaction();
OfflineFeedsFragment frag = new OfflineFeedsFragment();
ft.replace(R.id.feeds_fragment, frag);
ft.commit();
}
}
2011-12-06 13:01:08 +00:00
private void initDatabase() {
DatabaseHelper dh = new DatabaseHelper(getApplicationContext());
m_writableDb = dh.getWritableDatabase();
m_readableDb = dh.getReadableDatabase();
}
2011-12-07 08:29:19 +00:00
2011-12-06 13:01:08 +00:00
@Override
public synchronized SQLiteDatabase getReadableDb() {
return m_readableDb;
}
2011-12-07 08:29:19 +00:00
2011-12-06 13:01:08 +00:00
@Override
public synchronized SQLiteDatabase getWritableDb() {
return m_writableDb;
}
2011-12-07 08:29:19 +00:00
2011-12-06 13:01:08 +00:00
private void switchOnline() {
SharedPreferences localPrefs = getSharedPreferences("localprefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = localPrefs.edit();
editor.putBoolean("offline_mode_active", false);
editor.commit();
2011-12-07 08:29:19 +00:00
Intent refresh = new Intent(this, MainActivity.class);
startActivity(refresh);
finish();
}
2011-12-07 08:29:19 +00:00
2011-12-06 13:01:08 +00:00
@Override
public int getActiveFeedId() {
return m_activeFeedId;
}
2011-12-07 08:29:19 +00:00
2011-12-06 13:01:08 +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);
if (tv != null) {
tv.setText(status);
}
2011-12-07 08:29:19 +00:00
View pb = findViewById(R.id.loading_progress);
2011-12-07 08:29:19 +00:00
if (pb != null) {
pb.setVisibility(showProgress ? View.VISIBLE : View.GONE);
}
}
2011-12-07 08:29:19 +00:00
@Override
2011-12-07 08:29:19 +00:00
public void onSaveInstanceState(Bundle out) {
super.onSaveInstanceState(out);
out.putBoolean("unreadOnly", m_unreadOnly);
out.putBoolean("unreadArticlesOnly", m_unreadArticlesOnly);
out.putInt("offlineActiveFeedId", m_activeFeedId);
out.putInt("offlineArticleId", m_selectedArticleId);
}
2011-12-07 08:29:19 +00:00
2011-12-06 13:01:08 +00:00
private void setUnreadOnly(boolean unread) {
m_unreadOnly = unread;
2011-12-07 08:29:19 +00:00
2011-12-06 09:26:19 +00:00
refreshViews();
2011-12-07 08:29:19 +00:00
/*
* if (!m_enableCats || m_activeCategory != null ) refreshFeeds(); else
* refreshCategories();
*/
}
2011-12-07 08:29:19 +00:00
2011-12-06 13:01:08 +00:00
@Override
public boolean getUnreadOnly() {
return m_unreadOnly;
}
@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) {
Intent refresh = new Intent(this, OfflineActivity.class);
startActivity(refresh);
finish();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.offline_menu, menu);
2011-12-07 08:29:19 +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);
}
return true;
}
2011-12-06 13:01:08 +00:00
private void setMenuLabel(int id, int labelId) {
MenuItem mi = m_menu.findItem(id);
2011-12-07 08:29:19 +00:00
if (mi != null) {
mi.setTitle(labelId);
}
}
2011-12-07 08:29:19 +00:00
private void goBack(boolean allowQuit) {
2011-12-07 08:29:19 +00:00
if (m_smallScreenMode) {
if (m_selectedArticleId != 0) {
closeArticle();
} else if (m_activeFeedId != 0) {
//if (m_compatMode) {
2011-12-07 08:29:19 +00:00
findViewById(R.id.main).setAnimation(
AnimationUtils.loadAnimation(this,
R.anim.slide_right));
//}
2011-12-07 08:29:19 +00:00
/*
* if (m_activeFeed != null && m_activeFeed.is_cat) {
* findViewById
* (R.id.headlines_fragment).setVisibility(View.GONE);
* findViewById(R.id.cats_fragment).setVisibility(View.VISIBLE);
*
* refreshCategories(); } else {
*/
findViewById(R.id.headlines_fragment).setVisibility(View.GONE);
findViewById(R.id.feeds_fragment).setVisibility(View.VISIBLE);
// }
2011-12-07 05:51:10 +00:00
m_activeFeedId = 0;
2012-01-19 12:33:39 +00:00
2012-06-19 14:24:22 +00:00
OfflineFeedsFragment ff = (OfflineFeedsFragment) getSupportFragmentManager()
2012-01-19 12:33:39 +00:00
.findFragmentById(R.id.feeds_fragment);
if (ff != null) {
ff.setSelectedFeedId(0);
}
2012-06-19 14:24:22 +00:00
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.headlines_fragment, new OfflineHeadlinesFragment());
ft.commit();
2011-12-07 05:51:10 +00:00
refreshViews();
2011-12-07 08:29:19 +00:00
initMainMenu();
} else if (allowQuit) {
2011-12-07 08:29:19 +00:00
finish();
}
} else {
if (m_selectedArticleId != 0) {
closeArticle();
} else if (m_activeFeedId != 0) {
findViewById(R.id.headlines_fragment).setVisibility(View.INVISIBLE);
m_activeFeedId = 0;
2012-06-19 14:24:22 +00:00
OfflineFeedsFragment ff = (OfflineFeedsFragment) getSupportFragmentManager()
2012-01-19 12:33:39 +00:00
.findFragmentById(R.id.feeds_fragment);
if (ff != null) {
ff.setSelectedFeedId(0);
}
2012-06-19 14:24:22 +00:00
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.headlines_fragment, new OfflineHeadlinesFragment());
ft.commit();
refreshViews();
initMainMenu();
} else if (allowQuit) {
2011-12-07 08:29:19 +00:00
finish();
}
}
}
@Override
public void onBackPressed() {
goBack(true);
2011-12-07 08:29:19 +00:00
}
/*
* @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if
* (keyCode == KeyEvent.KEYCODE_BACK) {
*
* if (m_smallScreenMode) { if (m_selectedArticleId != 0) { closeArticle();
* } else if (m_activeFeedId != 0) { if (m_compatMode) {
* findViewById(R.id.main).setAnimation(AnimationUtils.loadAnimation(this,
* R.anim.slide_right)); }
*/
/*
* if (m_activeFeed != null && m_activeFeed.is_cat) {
* findViewById(R.id.headlines_fragment).setVisibility(View.GONE);
* findViewById(R.id.cats_fragment).setVisibility(View.VISIBLE);
*
* refreshCategories(); } else {
*//*
* findViewById(R.id.headlines_fragment).setVisibility(View.GONE);
* findViewById(R.id.feeds_fragment).setVisibility(View.VISIBLE); //}
* m_activeFeedId = 0; refreshViews(); initMainMenu();
*
* } else { finish(); } } else { if (m_selectedArticleId != 0) {
* closeArticle(); } else { finish(); } }
*
* return false; } return super.onKeyDown(keyCode, event); }
*/
2011-12-06 13:01:08 +00:00
private Cursor getArticleById(int articleId) {
2011-12-07 08:29:19 +00:00
Cursor c = getReadableDb().query("articles", null,
BaseColumns._ID + "=?",
new String[] { String.valueOf(articleId) }, null, null, null);
2011-12-07 08:29:19 +00:00
c.moveToFirst();
2011-12-07 08:29:19 +00:00
return c;
}
2011-12-07 08:29:19 +00:00
private Cursor getFeedById(int feedId) {
Cursor c = getReadableDb().query("feeds", null,
BaseColumns._ID + "=?",
new String[] { String.valueOf(feedId) }, null, null, null);
c.moveToFirst();
return c;
}
private Intent getShareIntent(Cursor article) {
String title = article.getString(article.getColumnIndex("title"));
String link = article.getString(article.getColumnIndex("link"));
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
2012-03-06 12:32:53 +00:00
//intent.putExtra(Intent.EXTRA_SUBJECT, title);
intent.putExtra(Intent.EXTRA_TEXT, title + " " + link);
return intent;
}
2011-12-06 13:01:08 +00:00
private void shareArticle(int articleId) {
Cursor article = getArticleById(articleId);
2011-12-07 08:29:19 +00:00
2011-12-06 09:26:19 +00:00
if (article != null) {
shareArticle(article);
article.close();
}
}
2011-12-06 13:01:08 +00:00
private void shareArticle(Cursor article) {
2011-12-06 09:26:19 +00:00
if (article != null) {
Intent intent = getShareIntent(article);
2011-12-07 08:29:19 +00:00
startActivity(Intent.createChooser(intent,
getString(R.id.share_article)));
}
}
2011-12-06 09:26:19 +00:00
2011-12-06 13:01:08 +00:00
private void refreshHeadlines() {
2012-06-19 14:24:22 +00:00
OfflineHeadlinesFragment ohf = (OfflineHeadlinesFragment) getSupportFragmentManager()
2011-12-07 08:29:19 +00:00
.findFragmentById(R.id.headlines_fragment);
2011-12-06 06:59:32 +00:00
if (ohf != null) {
ohf.refresh();
}
}
2011-12-07 08:29:19 +00:00
@Override
public boolean onOptionsItemSelected(MenuItem item) {
2012-06-19 14:24:22 +00:00
final OfflineHeadlinesFragment ohf = (OfflineHeadlinesFragment) getSupportFragmentManager()
2011-12-07 08:29:19 +00:00
.findFragmentById(R.id.headlines_fragment);
2011-12-06 06:59:32 +00:00
switch (item.getItemId()) {
case android.R.id.home:
goBack(false);
2012-06-19 14:24:22 +00:00
return true;
case R.id.search:
if (ohf != 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();
ohf.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;
case R.id.preferences:
Intent intent = new Intent(this, PreferencesActivity.class);
startActivityForResult(intent, 0);
return true;
case R.id.go_online:
switchOnline();
return true;
2011-12-06 06:59:32 +00:00
case R.id.headlines_select:
if (ohf != null) {
Dialog dialog = new Dialog(this);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.headlines_select_dialog);
2011-12-07 08:29:19 +00:00
builder.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:
SQLiteStatement stmtSelectAll = getWritableDb()
.compileStatement(
"UPDATE articles SET selected = 1 WHERE feed_id = ?");
stmtSelectAll.bindLong(1, m_activeFeedId);
stmtSelectAll.execute();
stmtSelectAll.close();
break;
case 1:
SQLiteStatement stmtSelectUnread = getWritableDb()
.compileStatement(
"UPDATE articles SET selected = 1 WHERE feed_id = ? AND unread = 1");
stmtSelectUnread
.bindLong(1, m_activeFeedId);
stmtSelectUnread.execute();
stmtSelectUnread.close();
break;
case 2:
deselectAllArticles();
break;
}
refreshViews();
initMainMenu();
dialog.cancel();
}
});
2011-12-06 06:59:32 +00:00
dialog = builder.create();
dialog.show();
}
return true;
case R.id.headlines_mark_as_read:
if (m_activeFeedId != 0) {
2011-12-07 08:29:19 +00:00
SQLiteStatement stmt = getWritableDb().compileStatement(
"UPDATE articles SET unread = 0 WHERE feed_id = ?");
2011-12-06 06:59:32 +00:00
stmt.bindLong(1, m_activeFeedId);
stmt.execute();
stmt.close();
2011-12-06 09:26:19 +00:00
refreshViews();
2011-12-06 06:59:32 +00:00
}
return true;
case R.id.share_article:
shareArticle(m_selectedArticleId);
return true;
2011-12-06 06:59:32 +00:00
case R.id.toggle_marked:
if (m_selectedArticleId != 0) {
2011-12-07 08:29:19 +00:00
SQLiteStatement stmt = getWritableDb().compileStatement(
"UPDATE articles SET marked = NOT marked WHERE "
+ BaseColumns._ID + " = ?");
2011-12-06 06:59:32 +00:00
stmt.bindLong(1, m_selectedArticleId);
stmt.execute();
stmt.close();
2011-12-06 09:26:19 +00:00
refreshViews();
2011-12-06 06:59:32 +00:00
}
return true;
case R.id.selection_select_none:
2011-12-06 09:26:19 +00:00
deselectAllArticles();
return true;
2011-12-06 06:59:32 +00:00
case R.id.selection_toggle_unread:
2011-12-06 09:26:19 +00:00
if (getSelectedArticleCount() > 0 && m_activeFeedId != 0) {
2011-12-07 08:29:19 +00:00
SQLiteStatement stmt = getWritableDb()
.compileStatement(
"UPDATE articles SET unread = NOT unread WHERE selected = 1 AND feed_id = ?");
2011-12-06 09:26:19 +00:00
stmt.bindLong(1, m_activeFeedId);
stmt.execute();
stmt.close();
refreshViews();
}
return true;
2011-12-06 06:59:32 +00:00
case R.id.selection_toggle_marked:
2011-12-06 09:26:19 +00:00
if (getSelectedArticleCount() > 0 && m_activeFeedId != 0) {
2011-12-07 08:29:19 +00:00
SQLiteStatement stmt = getWritableDb()
.compileStatement(
"UPDATE articles SET marked = NOT marked WHERE selected = 1 AND feed_id = ?");
2011-12-06 09:26:19 +00:00
stmt.bindLong(1, m_activeFeedId);
stmt.execute();
stmt.close();
refreshViews();
}
return true;
2011-12-06 06:59:32 +00:00
case R.id.selection_toggle_published:
2011-12-06 09:26:19 +00:00
if (getSelectedArticleCount() > 0 && m_activeFeedId != 0) {
2011-12-07 08:29:19 +00:00
SQLiteStatement stmt = getWritableDb()
.compileStatement(
"UPDATE articles SET published = NOT published WHERE selected = 1 AND feed_id = ?");
2011-12-06 09:26:19 +00:00
stmt.bindLong(1, m_activeFeedId);
stmt.execute();
stmt.close();
refreshViews();
}
2011-12-06 06:59:32 +00:00
return true;
case R.id.toggle_published:
if (m_selectedArticleId != 0) {
2011-12-07 08:29:19 +00:00
SQLiteStatement stmt = getWritableDb().compileStatement(
"UPDATE articles SET published = NOT published WHERE "
+ BaseColumns._ID + " = ?");
2011-12-06 06:59:32 +00:00
stmt.bindLong(1, m_selectedArticleId);
stmt.execute();
stmt.close();
2011-12-06 09:26:19 +00:00
refreshViews();
2011-12-06 06:59:32 +00:00
}
return true;
case R.id.catchup_above:
2011-12-06 09:26:19 +00:00
if (m_selectedArticleId != 0 && m_activeFeedId != 0) {
2011-12-07 08:29:19 +00:00
SQLiteStatement stmt = getWritableDb().compileStatement(
"UPDATE articles SET unread = 0 WHERE updated >= "
+ "(SELECT updated FROM articles WHERE "
+ BaseColumns._ID + " = ?) AND feed_id = ?");
2011-12-06 09:26:19 +00:00
stmt.bindLong(1, m_selectedArticleId);
stmt.bindLong(2, m_activeFeedId);
stmt.execute();
stmt.close();
refreshViews();
2011-12-06 06:59:32 +00:00
}
return true;
case R.id.set_unread:
if (m_selectedArticleId != 0) {
2011-12-07 08:29:19 +00:00
SQLiteStatement stmt = getWritableDb().compileStatement(
"UPDATE articles SET unread = 1 WHERE "
+ BaseColumns._ID + " = ?");
2011-12-06 06:59:32 +00:00
stmt.bindLong(1, m_selectedArticleId);
stmt.execute();
stmt.close();
2011-12-06 09:26:19 +00:00
refreshViews();
2011-12-06 06:59:32 +00:00
}
return true;
case R.id.show_feeds:
setUnreadOnly(!getUnreadOnly());
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
return true;
default:
2011-12-07 08:29:19 +00:00
Log.d(TAG,
"onOptionsItemSelected, unhandled id=" + item.getItemId());
return super.onOptionsItemSelected(item);
}
}
2011-12-06 13:01:08 +00:00
private void refreshFeeds() {
2012-06-19 14:24:22 +00:00
OfflineFeedsFragment frag = (OfflineFeedsFragment) getSupportFragmentManager()
2011-12-07 08:29:19 +00:00
.findFragmentById(R.id.feeds_fragment);
if (frag != null) {
frag.refresh();
}
}
2011-12-07 08:29:19 +00:00
private void closeArticle() {
if (m_smallScreenMode) {
2011-12-07 08:29:19 +00:00
findViewById(R.id.main).setAnimation(
AnimationUtils.loadAnimation(this, R.anim.slide_right));
}
if (m_smallScreenMode) {
2011-12-07 08:29:19 +00:00
findViewById(R.id.article_fragment).setVisibility(View.GONE);
findViewById(R.id.headlines_fragment).setVisibility(View.VISIBLE);
} else {
findViewById(R.id.article_fragment).setVisibility(View.GONE);
findViewById(R.id.feeds_fragment).setVisibility(View.VISIBLE);
2011-12-07 08:29:19 +00:00
}
// we don't want to lose selected article in headlines so we refresh them before setting selected id to 0
refreshViews();
m_selectedArticleId = 0;
2012-06-19 14:24:22 +00:00
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.article_fragment, new DummyFragment());
ft.commit();
initMainMenu();
}
2011-12-07 08:29:19 +00:00
2011-12-06 13:01:08 +00:00
private int getSelectedArticleCount() {
2011-12-07 08:29:19 +00:00
Cursor c = getReadableDb().query("articles",
new String[] { "COUNT(*)" }, "selected = 1", null, null, null,
null);
2011-12-06 06:59:32 +00:00
c.moveToFirst();
2011-12-06 09:26:19 +00:00
int selected = c.getInt(0);
2011-12-06 06:59:32 +00:00
c.close();
2011-12-07 08:29:19 +00:00
2011-12-06 09:26:19 +00:00
return selected;
2011-12-06 06:59:32 +00:00
}
2011-12-07 08:29:19 +00:00
2011-12-06 13:01:08 +00:00
@Override
public void initMainMenu() {
if (m_menu != null) {
2011-12-07 08:29:19 +00:00
int numSelected = getSelectedArticleCount();
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);
2011-12-07 08:29:19 +00:00
m_menu.setGroupVisible(R.id.menu_group_article, false);
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_selectedArticleId != 0) {
m_menu.setGroupVisible(R.id.menu_group_article, true);
2012-01-19 12:33:39 +00:00
} else if (m_activeFeedId != 0) {
2011-12-07 08:29:19 +00:00
m_menu.setGroupVisible(R.id.menu_group_headlines, true);
2012-01-19 12:33:39 +00:00
MenuItem search = m_menu.findItem(R.id.search);
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-01-19 12:33:39 +00:00
2012-06-19 14:24:22 +00:00
@Override
public boolean onQueryTextSubmit(String query) {
OfflineHeadlinesFragment frag = (OfflineHeadlinesFragment) getSupportFragmentManager()
2012-01-19 12:33:39 +00:00
.findFragmentById(R.id.headlines_fragment);
if (frag != null) {
2012-06-19 14:24:22 +00:00
frag.setSearchQuery(query);
this.query = query;
2012-01-19 12:33:39 +00:00
}
2012-06-19 14:24:22 +00:00
return false;
2012-01-19 12:33:39 +00:00
}
2012-06-19 14:24:22 +00:00
@Override
public boolean onQueryTextChange(String newText) {
if (newText.equals("") && !newText.equals(this.query)) {
OfflineHeadlinesFragment frag = (OfflineHeadlinesFragment) getSupportFragmentManager()
.findFragmentById(R.id.headlines_fragment);
if (frag != null) {
frag.setSearchQuery(newText);
this.query = newText;
}
}
return false;
}
});
}
2012-01-19 12:33:39 +00:00
} else {
2011-12-07 08:29:19 +00:00
m_menu.setGroupVisible(R.id.menu_group_feeds, true);
}
if (numSelected == 0 && m_headlinesActionMode != null) {
m_headlinesActionMode.finish();
}
2012-06-19 14:24:22 +00:00
if (!m_compatMode) {
2012-06-19 14:24:22 +00:00
if (m_activeFeedId != 0) {
Cursor feed = getFeedById(m_activeFeedId);
if (feed != null) {
getActionBar().setTitle(feed.getString(feed.getColumnIndex("title")));
}
} else {
getActionBar().setTitle(R.string.app_name);
}
if (!m_smallScreenMode) {
getActionBar().setDisplayHomeAsUpEnabled(m_selectedArticleId != 0);
} else {
getActionBar().setDisplayHomeAsUpEnabled(m_selectedArticleId != 0 || m_activeFeedId != 0);
}
}
}
2011-12-07 08:29:19 +00:00
}
@Override
public void onPause() {
super.onPause();
2011-12-07 08:29:19 +00:00
}
2011-12-07 08:29:19 +00:00
@Override
public void onDestroy() {
super.onDestroy();
2011-12-07 08:29:19 +00:00
m_readableDb.close();
m_writableDb.close();
2011-12-07 08:29:19 +00:00
}
2011-12-06 13:01:08 +00:00
private void refreshViews() {
2011-12-06 09:26:19 +00:00
refreshFeeds();
refreshHeadlines();
}
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
OfflineHeadlinesFragment hf = (OfflineHeadlinesFragment) getSupportFragmentManager()
2011-12-07 08:29:19 +00:00
.findFragmentById(R.id.headlines_fragment);
2012-06-19 14:24:22 +00:00
OfflineFeedsFragment ff = (OfflineFeedsFragment) getSupportFragmentManager()
2011-12-07 08:29:19 +00:00
.findFragmentById(R.id.feeds_fragment);
switch (item.getItemId()) {
case R.id.article_link_copy:
2012-03-12 08:51:57 +00:00
if (m_selectedArticleId != 0) {
Cursor article = null;
if (m_selectedArticleId != 0) {
article = getArticleById(m_selectedArticleId);
} else if (info != null) {
article = hf.getArticleAtPosition(info.position);
}
2012-03-12 08:51:57 +00:00
if (article != null) {
if (android.os.Build.VERSION.SDK_INT < 11) {
@SuppressWarnings("deprecation")
android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
clipboard.setText(article.getString(article.getColumnIndex("link")));
} else {
android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
clipboard.setText(article.getString(article.getColumnIndex("link")));
}
article.close();
Toast toast = Toast.makeText(OfflineActivity.this, R.string.text_copied_to_clipboard, Toast.LENGTH_SHORT);
toast.show();
}
}
return true;
case R.id.article_link_share:
if (m_selectedArticleId != 0) {
shareArticle(m_selectedArticleId);
}
return true;
2011-12-07 08:29:19 +00:00
case R.id.browse_articles:
// TODO cat stuff
return true;
case R.id.browse_feeds:
// TODO cat stuff
return true;
case R.id.catchup_category:
// TODO cat stuff
return true;
case R.id.catchup_feed:
if (ff != null) {
int feedId = ff.getFeedIdAtPosition(info.position);
if (feedId != 0) {
SQLiteStatement stmt = getWritableDb().compileStatement(
"UPDATE articles SET unread = 0 WHERE feed_id = ?");
stmt.bindLong(1, feedId);
stmt.execute();
stmt.close();
refreshViews();
}
}
return true;
case R.id.selection_toggle_unread:
if (getSelectedArticleCount() > 0 && m_activeFeedId != 0) {
SQLiteStatement stmt = getWritableDb()
.compileStatement(
"UPDATE articles SET unread = NOT unread WHERE selected = 1 AND feed_id = ?");
stmt.bindLong(1, m_activeFeedId);
stmt.execute();
stmt.close();
refreshViews();
} else {
int articleId = hf.getArticleIdAtPosition(info.position);
if (articleId != 0) {
SQLiteStatement stmt = getWritableDb().compileStatement(
"UPDATE articles SET unread = NOT unread WHERE "
+ BaseColumns._ID + " = ?");
stmt.bindLong(1, articleId);
stmt.execute();
stmt.close();
refreshViews();
}
}
return true;
case R.id.selection_toggle_marked:
if (getSelectedArticleCount() > 0 && m_activeFeedId != 0) {
SQLiteStatement stmt = getWritableDb()
.compileStatement(
"UPDATE articles SET marked = NOT marked WHERE selected = 1 AND feed_id = ?");
stmt.bindLong(1, m_activeFeedId);
stmt.execute();
stmt.close();
refreshViews();
} else {
int articleId = hf.getArticleIdAtPosition(info.position);
if (articleId != 0) {
SQLiteStatement stmt = getWritableDb().compileStatement(
"UPDATE articles SET marked = NOT marked WHERE "
+ BaseColumns._ID + " = ?");
2011-12-06 09:26:19 +00:00
stmt.bindLong(1, articleId);
stmt.execute();
stmt.close();
refreshViews();
2011-12-07 08:29:19 +00:00
}
}
return true;
case R.id.selection_toggle_published:
if (getSelectedArticleCount() > 0 && m_activeFeedId != 0) {
SQLiteStatement stmt = getWritableDb()
.compileStatement(
"UPDATE articles SET published = NOT published WHERE selected = 1 AND feed_id = ?");
stmt.bindLong(1, m_activeFeedId);
stmt.execute();
stmt.close();
refreshViews();
} else {
int articleId = hf.getArticleIdAtPosition(info.position);
if (articleId != 0) {
SQLiteStatement stmt = getWritableDb().compileStatement(
"UPDATE articles SET published = NOT published WHERE "
+ BaseColumns._ID + " = ?");
stmt.bindLong(1, articleId);
stmt.execute();
stmt.close();
refreshViews();
}
}
return true;
case R.id.share_article:
Cursor article = hf.getArticleAtPosition(info.position);
if (article != null) {
shareArticle(article);
}
return true;
case R.id.catchup_above:
int articleId = hf.getArticleIdAtPosition(info.position);
if (articleId != 0 && m_activeFeedId != 0) {
SQLiteStatement stmt = getWritableDb().compileStatement(
"UPDATE articles SET unread = 0 WHERE updated >= "
+ "(SELECT updated FROM articles WHERE "
+ BaseColumns._ID + " = ?) AND feed_id = ?");
stmt.bindLong(1, articleId);
stmt.bindLong(2, m_activeFeedId);
stmt.execute();
stmt.close();
refreshViews();
}
return true;
default:
Log.d(TAG,
"onContextItemSelected, unhandled id=" + item.getItemId());
return super.onContextItemSelected(item);
}
}
@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
OfflineHeadlinesFragment ohf = (OfflineHeadlinesFragment) getSupportFragmentManager()
2011-12-07 08:29:19 +00:00
.findFragmentById(R.id.headlines_fragment);
int nextId = getRelativeArticleId(m_selectedArticleId,
m_activeFeedId, RelativeArticle.AFTER);
if (nextId != 0 && ohf != null) {
if (m_prefs.getBoolean("combined_mode", false)) {
ohf.setActiveArticleId(nextId);
SQLiteStatement stmt = getWritableDb()
.compileStatement(
"UPDATE articles SET unread = 0 "
+ "WHERE " + BaseColumns._ID
+ " = ?");
stmt.bindLong(1, nextId);
stmt.execute();
stmt.close();
} else {
openArticle(nextId, 0);
}
}
}
return true;
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_UP) {
2012-06-19 14:24:22 +00:00
OfflineHeadlinesFragment ohf = (OfflineHeadlinesFragment) getSupportFragmentManager()
2011-12-07 08:29:19 +00:00
.findFragmentById(R.id.headlines_fragment);
int prevId = getRelativeArticleId(m_selectedArticleId,
m_activeFeedId, RelativeArticle.BEFORE);
if (prevId != 0 && ohf != null) {
if (m_prefs.getBoolean("combined_mode", false)) {
ohf.setActiveArticleId(prevId);
SQLiteStatement stmt = getWritableDb()
.compileStatement(
"UPDATE articles SET unread = 0 "
+ "WHERE " + BaseColumns._ID
+ " = ?");
stmt.bindLong(1, prevId);
stmt.execute();
stmt.close();
} else {
openArticle(prevId, 0);
}
}
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
2011-12-06 13:01:08 +00:00
private void deselectAllArticles() {
2011-12-06 06:59:32 +00:00
getWritableDb().execSQL("UPDATE articles SET selected = 0 ");
}
2011-12-07 08:29:19 +00:00
2011-12-06 13:01:08 +00:00
@Override
2011-12-07 08:29:19 +00:00
public int getRelativeArticleId(int baseId, int feedId,
OnlineServices.RelativeArticle mode) {
Cursor c;
2011-12-07 08:29:19 +00:00
/*
* if (baseId == 0) { c = getReadableDb().query("articles", null,
* "feed_id = ?", new String[] { String.valueOf(feedId) }, null, null,
* "updated DESC LIMIT 1");
*
* if (c.moveToFirst()) { baseId = c.getInt(0); }
*
* c.close();
*
* return baseId; }
*/
if (mode == RelativeArticle.BEFORE) {
c = getReadableDb().query(
"articles",
null,
"updated > (SELECT updated FROM articles WHERE "
+ BaseColumns._ID + " = ?) AND feed_id = ?",
new String[] { String.valueOf(baseId),
String.valueOf(feedId) }, null, null,
"updated LIMIT 1");
} else {
2011-12-07 08:29:19 +00:00
c = getReadableDb().query(
"articles",
null,
"updated < (SELECT updated FROM articles WHERE "
+ BaseColumns._ID + " = ?) AND feed_id = ?",
new String[] { String.valueOf(baseId),
String.valueOf(feedId) }, null, null,
"updated DESC LIMIT 1");
}
int id = 0;
2011-12-07 08:29:19 +00:00
if (c.moveToFirst()) {
id = c.getInt(0);
}
2011-12-07 08:29:19 +00:00
c.close();
return id;
}
2011-12-07 08:29:19 +00:00
2011-12-06 13:01:08 +00:00
@Override
public void viewFeed(int feedId) {
m_activeFeedId = feedId;
2011-12-07 08:29:19 +00:00
initMainMenu();
2011-12-07 08:29:19 +00:00
if (m_smallScreenMode) {
findViewById(R.id.feeds_fragment).setVisibility(View.GONE);
findViewById(R.id.headlines_fragment).setVisibility(View.VISIBLE);
} else {
findViewById(R.id.headlines_fragment).setVisibility(View.VISIBLE);
}
2011-12-07 08:29:19 +00:00
2011-12-06 06:59:32 +00:00
deselectAllArticles();
2011-12-07 08:29:19 +00:00
2012-01-19 12:33:39 +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) {
2012-01-19 12:33:39 +00:00
SearchView sv = (SearchView) search.getActionView();
sv.setQuery("", false);
}
}
2012-06-19 14:24:22 +00:00
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
2011-12-07 08:29:19 +00:00
OfflineHeadlinesFragment frag = new OfflineHeadlinesFragment();
ft.replace(R.id.headlines_fragment, frag);
ft.commit();
2011-12-07 08:29:19 +00:00
}
2011-12-06 13:01:08 +00:00
@Override
public void openArticle(int articleId, int compatAnimation) {
m_selectedArticleId = articleId;
2011-12-07 08:29:19 +00:00
initMainMenu();
2012-06-19 14:24:22 +00:00
OfflineHeadlinesFragment hf = (OfflineHeadlinesFragment) getSupportFragmentManager()
2011-12-07 08:29:19 +00:00
.findFragmentById(R.id.headlines_fragment);
if (hf != null) {
hf.setActiveArticleId(articleId);
}
2011-12-07 08:29:19 +00:00
SQLiteStatement stmt = getWritableDb().compileStatement(
"UPDATE articles SET unread = 0 " + "WHERE " + BaseColumns._ID
+ " = ?");
stmt.bindLong(1, articleId);
stmt.execute();
stmt.close();
2011-12-07 08:29:19 +00:00
Fragment frag;
if (m_smallScreenMode) {
frag = new OfflineArticlePager(articleId);
} else {
frag = new OfflineArticleFragment(articleId);
}
2011-12-07 08:29:19 +00:00
2012-06-19 14:24:22 +00:00
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.article_fragment, frag);
ft.commit();
if (m_smallScreenMode) {
if (compatAnimation == 0)
2011-12-07 08:29:19 +00:00
findViewById(R.id.main).setAnimation(
AnimationUtils.loadAnimation(this, R.anim.slide_left));
else
2011-12-07 08:29:19 +00:00
findViewById(R.id.main).setAnimation(
AnimationUtils.loadAnimation(this, compatAnimation));
}
}
2011-12-06 13:01:08 +00:00
@Override
public int getSelectedArticleId() {
return m_selectedArticleId;
}
@Override
public void setSelectedArticleId(int articleId) {
m_selectedArticleId = articleId;
refreshViews();
}
}