From 3071df1abec853ca639df47066e7a6010fd17033 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Wed, 10 Jun 2015 23:51:41 +0300 Subject: [PATCH] rework databasehelper as a singleton simplify offline interaction with the database --- .../java/org/fox/ttrss/CommonActivity.java | 38 +++++-------- .../java/org/fox/ttrss/OnlineActivity.java | 39 +------------ .../fox/ttrss/offline/OfflineActivity.java | 52 +++++++++--------- .../ttrss/offline/OfflineArticleFragment.java | 2 +- .../ttrss/offline/OfflineArticlePager.java | 4 +- .../ttrss/offline/OfflineDetailActivity.java | 2 +- .../ttrss/offline/OfflineDownloadService.java | 2 +- .../OfflineFeedCategoriesFragment.java | 2 +- .../ttrss/offline/OfflineFeedsFragment.java | 4 +- .../offline/OfflineHeadlinesFragment.java | 34 ++++++------ .../ttrss/offline/OfflineMasterActivity.java | 2 +- .../ttrss/offline/OfflineUploadService.java | 2 +- .../org/fox/ttrss/util/DatabaseHelper.java | 55 ++++++++++++++++++- 13 files changed, 122 insertions(+), 116 deletions(-) diff --git a/org.fox.ttrss/src/main/java/org/fox/ttrss/CommonActivity.java b/org.fox.ttrss/src/main/java/org/fox/ttrss/CommonActivity.java index 08298822..9f2fa13e 100755 --- a/org.fox.ttrss/src/main/java/org/fox/ttrss/CommonActivity.java +++ b/org.fox.ttrss/src/main/java/org/fox/ttrss/CommonActivity.java @@ -31,8 +31,10 @@ public class CommonActivity extends ActionBarActivity { public static final int EXCERPT_MAX_LENGTH = 256; public static final int EXCERPT_MAX_QUERY_LENGTH = 2048; - private SQLiteDatabase m_readableDb; - private SQLiteDatabase m_writableDb; + private DatabaseHelper m_databaseHelper; + + //private SQLiteDatabase m_readableDb; + //private SQLiteDatabase m_writableDb; private boolean m_smallScreenMode = true; private String m_theme; @@ -56,7 +58,15 @@ public class CommonActivity extends ActionBarActivity { Log.d(TAG, "m_smallScreenMode=" + smallScreen); m_smallScreenMode = smallScreen; } - + + public DatabaseHelper getDatabaseHelper() { + return m_databaseHelper; + } + + public SQLiteDatabase getDatabase() { + return m_databaseHelper.getWritableDatabase(); + } + public boolean getUnreadOnly() { return m_prefs.getBoolean("show_unread_only", true); } @@ -82,21 +92,6 @@ public class CommonActivity extends ActionBarActivity { toast.show(); } - private void initDatabase() { - DatabaseHelper dh = new DatabaseHelper(getApplicationContext()); - - m_writableDb = dh.getWritableDatabase(); - m_readableDb = dh.getReadableDatabase(); - } - - public synchronized SQLiteDatabase getReadableDb() { - return m_readableDb; - } - - public synchronized SQLiteDatabase getWritableDb() { - return m_writableDb; - } - @Override public void onResume() { super.onResume(); @@ -113,13 +108,12 @@ public class CommonActivity extends ActionBarActivity { @Override public void onDestroy() { super.onDestroy(); - - m_readableDb.close(); - m_writableDb.close(); } @Override public void onCreate(Bundle savedInstanceState) { + m_databaseHelper = DatabaseHelper.getInstance(this); + m_prefs = PreferenceManager .getDefaultSharedPreferences(getApplicationContext()); @@ -128,8 +122,6 @@ public class CommonActivity extends ActionBarActivity { } else { m_theme = m_prefs.getString("theme", CommonActivity.THEME_DEFAULT); } - - initDatabase(); super.onCreate(savedInstanceState); } diff --git a/org.fox.ttrss/src/main/java/org/fox/ttrss/OnlineActivity.java b/org.fox.ttrss/src/main/java/org/fox/ttrss/OnlineActivity.java index 2dba81a8..05b458c6 100755 --- a/org.fox.ttrss/src/main/java/org/fox/ttrss/OnlineActivity.java +++ b/org.fox.ttrss/src/main/java/org/fox/ttrss/OnlineActivity.java @@ -14,7 +14,6 @@ import android.content.IntentFilter; import android.content.SharedPreferences; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; -import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.preference.PreferenceManager; @@ -277,40 +276,6 @@ public class OnlineActivity extends CommonActivity { } } - 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 - } - - 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? - } - - return false; - } @Override public void onPause() { @@ -463,7 +428,7 @@ public class OnlineActivity extends CommonActivity { startActivityForResult(intent, 0); overridePendingTransition(0, 0); - if (hasPendingOfflineData()) + if (getDatabaseHelper().hasPendingOfflineData()) syncOfflineData(); finish(); @@ -1192,7 +1157,7 @@ public class OnlineActivity extends CommonActivity { setSessionId(null); initMenu(); - if (hasOfflineData()) { + if (getDatabaseHelper().hasOfflineData()) { AlertDialog.Builder builder = new AlertDialog.Builder( OnlineActivity.this) diff --git a/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineActivity.java b/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineActivity.java index fd14c831..bc7202bd 100755 --- a/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineActivity.java +++ b/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineActivity.java @@ -207,10 +207,10 @@ public class OfflineActivity extends CommonActivity { SQLiteStatement stmtSelectAll = null; if (isCat) { - stmtSelectAll = getWritableDb().compileStatement( + stmtSelectAll = getDatabase().compileStatement( "UPDATE articles SET selected = 1 WHERE feed_id IN (SELECT "+BaseColumns._ID+" FROM feeds WHERE cat_id = ?)"); } else { - stmtSelectAll = getWritableDb().compileStatement( + stmtSelectAll = getDatabase().compileStatement( "UPDATE articles SET selected = 1 WHERE feed_id = ?"); } @@ -224,10 +224,10 @@ public class OfflineActivity extends CommonActivity { SQLiteStatement stmtSelectUnread = null; if (isCat) { - stmtSelectUnread = getWritableDb().compileStatement( + stmtSelectUnread = getDatabase().compileStatement( "UPDATE articles SET selected = 1 WHERE feed_id IN (SELECT "+BaseColumns._ID+" FROM feeds WHERE cat_id = ?) AND unread = 1"); } else { - stmtSelectUnread = getWritableDb().compileStatement( + stmtSelectUnread = getDatabase().compileStatement( "UPDATE articles SET selected = 1 WHERE feed_id = ? AND unread = 1"); } @@ -455,7 +455,7 @@ public class OfflineActivity extends CommonActivity { if (oap != null) { int articleId = oap.getSelectedArticleId(); - SQLiteStatement stmt = getWritableDb().compileStatement( + SQLiteStatement stmt = getDatabase().compileStatement( "UPDATE articles SET modified = 1, marked = NOT marked WHERE " + BaseColumns._ID + " = ?"); stmt.bindLong(1, articleId); @@ -470,7 +470,7 @@ public class OfflineActivity extends CommonActivity { return true; */ case R.id.selection_toggle_unread: if (getSelectedArticleCount() > 0) { - SQLiteStatement stmt = getWritableDb() + SQLiteStatement stmt = getDatabase() .compileStatement( "UPDATE articles SET modified = 1, unread = NOT unread WHERE selected = 1"); stmt.execute(); @@ -481,7 +481,7 @@ public class OfflineActivity extends CommonActivity { return true; case R.id.selection_toggle_marked: if (getSelectedArticleCount() > 0) { - SQLiteStatement stmt = getWritableDb() + SQLiteStatement stmt = getDatabase() .compileStatement( "UPDATE articles SET modified = 1, marked = NOT marked WHERE selected = 1"); stmt.execute(); @@ -492,7 +492,7 @@ public class OfflineActivity extends CommonActivity { return true; case R.id.selection_toggle_published: if (getSelectedArticleCount() > 0) { - SQLiteStatement stmt = getWritableDb() + SQLiteStatement stmt = getDatabase() .compileStatement( "UPDATE articles SET modified = 1, published = NOT published WHERE selected = 1"); stmt.execute(); @@ -505,7 +505,7 @@ public class OfflineActivity extends CommonActivity { if (oap != null) { int articleId = oap.getSelectedArticleId(); - SQLiteStatement stmt = getWritableDb().compileStatement( + SQLiteStatement stmt = getDatabase().compileStatement( "UPDATE articles SET modified = 1, published = NOT published WHERE " + BaseColumns._ID + " = ?"); stmt.bindLong(1, articleId); @@ -524,12 +524,12 @@ public class OfflineActivity extends CommonActivity { SQLiteStatement stmt = null; if (isCat) { - stmt = getWritableDb().compileStatement( + stmt = getDatabase().compileStatement( "UPDATE articles SET modified = 1, unread = 0 WHERE " + "updated >= (SELECT updated FROM articles WHERE " + BaseColumns._ID + " = ?) " + "AND feed_id IN (SELECT "+BaseColumns._ID+" FROM feeds WHERE cat_id = ?)"); } else { - stmt = getWritableDb().compileStatement( + stmt = getDatabase().compileStatement( "UPDATE articles SET modified = 1, unread = 0 WHERE " + "updated >= (SELECT updated FROM articles WHERE " + BaseColumns._ID + " = ?) " + "AND feed_id = ?"); @@ -614,9 +614,9 @@ public class OfflineActivity extends CommonActivity { } protected Cursor getArticleById(int articleId) { - Cursor c = getReadableDb().query("articles", null, + Cursor c = getDatabase().query("articles", null, BaseColumns._ID + "=?", - new String[] { String.valueOf(articleId) }, null, null, null); + new String[]{String.valueOf(articleId)}, null, null, null); c.moveToFirst(); @@ -659,9 +659,9 @@ public class OfflineActivity extends CommonActivity { } protected Cursor getFeedById(int feedId) { - Cursor c = getReadableDb().query("feeds", null, + Cursor c = getDatabase().query("feeds", null, BaseColumns._ID + "=?", - new String[] { String.valueOf(feedId) }, null, null, null); + new String[]{String.valueOf(feedId)}, null, null, null); c.moveToFirst(); @@ -669,9 +669,9 @@ public class OfflineActivity extends CommonActivity { } protected Cursor getCatById(int catId) { - Cursor c = getReadableDb().query("categories", null, + Cursor c = getDatabase().query("categories", null, BaseColumns._ID + "=?", - new String[] { String.valueOf(catId) }, null, null, null); + new String[]{String.valueOf(catId)}, null, null, null); c.moveToFirst(); @@ -715,8 +715,8 @@ public class OfflineActivity extends CommonActivity { } protected int getSelectedArticleCount() { - Cursor c = getReadableDb().query("articles", - new String[] { "COUNT(*)" }, "selected = 1", null, null, null, + Cursor c = getDatabase().query("articles", + new String[]{"COUNT(*)"}, "selected = 1", null, null, null, null); c.moveToFirst(); int selected = c.getInt(0); @@ -730,12 +730,12 @@ public class OfflineActivity extends CommonActivity { Cursor c; if (isCat) { - c = getReadableDb().query("articles", + c = getDatabase().query("articles", new String[] { "COUNT(*)" }, "unread = 1 AND feed_id IN (SELECT "+BaseColumns._ID+" FROM feeds WHERE cat_id = ?)", new String[] { String.valueOf(feedId) }, null, null, null); } else { - c = getReadableDb().query("articles", + c = getDatabase().query("articles", new String[] { "COUNT(*)" }, "unread = 1 AND feed_id = ?", new String[] { String.valueOf(feedId) }, null, null, null); @@ -749,7 +749,7 @@ public class OfflineActivity extends CommonActivity { } protected void deselectAllArticles() { - getWritableDb().execSQL("UPDATE articles SET selected = 0 "); + getDatabase().execSQL("UPDATE articles SET selected = 0 "); refresh(); } @@ -780,14 +780,14 @@ public class OfflineActivity extends CommonActivity { public void catchupFeed(int feedId, boolean isCat) { if (isCat) { - SQLiteStatement stmt = getWritableDb().compileStatement( - "UPDATE articles SET modified = 1, unread = 0 WHERE feed_id IN (SELECT "+ - BaseColumns._ID+" FROM feeds WHERE cat_id = ?)"); + SQLiteStatement stmt = getDatabase().compileStatement( + "UPDATE articles SET modified = 1, unread = 0 WHERE feed_id IN (SELECT " + + BaseColumns._ID + " FROM feeds WHERE cat_id = ?)"); stmt.bindLong(1, feedId); stmt.execute(); stmt.close(); } else { - SQLiteStatement stmt = getWritableDb().compileStatement( + SQLiteStatement stmt = getDatabase().compileStatement( "UPDATE articles SET modified = 1, unread = 0 WHERE feed_id = ?"); stmt.bindLong(1, feedId); stmt.execute(); diff --git a/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineArticleFragment.java b/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineArticleFragment.java index 7be392ab..2a9daca3 100755 --- a/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineArticleFragment.java +++ b/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineArticleFragment.java @@ -126,7 +126,7 @@ public class OfflineArticleFragment extends Fragment { View view = inflater.inflate(R.layout.fragment_article, container, false); - m_cursor = m_activity.getReadableDb().query("articles LEFT JOIN feeds ON (feed_id = feeds."+BaseColumns._ID+")", + m_cursor = m_activity.getDatabase().query("articles LEFT JOIN feeds ON (feed_id = feeds."+BaseColumns._ID+")", new String[] { "articles.*", "feeds.title AS feed_title" }, "articles." + BaseColumns._ID + "=?", new String[] { String.valueOf(m_articleId) }, null, null, null); diff --git a/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineArticlePager.java b/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineArticlePager.java index a89f57f3..26bb12d7 100644 --- a/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineArticlePager.java +++ b/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineArticlePager.java @@ -66,11 +66,11 @@ public class OfflineArticlePager extends Fragment { String orderBy = (m_prefs.getBoolean("offline_oldest_first", false)) ? "updated" : "updated DESC"; if (m_searchQuery == null || m_searchQuery.equals("")) { - return m_activity.getReadableDb().query("articles LEFT JOIN feeds ON (feed_id = feeds."+BaseColumns._ID+")", + return m_activity.getDatabase().query("articles LEFT JOIN feeds ON (feed_id = feeds."+BaseColumns._ID+")", new String[] { "articles."+BaseColumns._ID, "feeds.title AS feed_title" }, feedClause, new String[] { String.valueOf(m_feedId) }, null, null, orderBy); } else { - return m_activity.getReadableDb().query("articles LEFT JOIN feeds ON (feed_id = feeds."+BaseColumns._ID+")", + return m_activity.getDatabase().query("articles LEFT JOIN feeds ON (feed_id = feeds."+BaseColumns._ID+")", new String[] { "articles."+BaseColumns._ID }, feedClause + " AND (articles.title LIKE '%' || ? || '%' OR content LIKE '%' || ? || '%')", new String[] { String.valueOf(m_feedId), m_searchQuery, m_searchQuery }, null, null, orderBy); diff --git a/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineDetailActivity.java b/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineDetailActivity.java index 568af77d..2b69bff0 100644 --- a/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineDetailActivity.java +++ b/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineDetailActivity.java @@ -145,7 +145,7 @@ public class OfflineDetailActivity extends OfflineActivity implements OfflineHea public void onArticleSelected(int articleId, boolean open) { if (!open) { - SQLiteStatement stmt = getWritableDb().compileStatement( + SQLiteStatement stmt = getDatabase().compileStatement( "UPDATE articles SET modified = 1, unread = 0 " + "WHERE " + BaseColumns._ID + " = ?"); diff --git a/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineDownloadService.java b/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineDownloadService.java index 3096490f..6d485e65 100644 --- a/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineDownloadService.java +++ b/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineDownloadService.java @@ -180,7 +180,7 @@ public class OfflineDownloadService extends Service { } private void initDatabase() { - DatabaseHelper dh = new DatabaseHelper(getApplicationContext()); + DatabaseHelper dh = DatabaseHelper.getInstance(this); m_writableDb = dh.getWritableDatabase(); m_readableDb = dh.getReadableDatabase(); } diff --git a/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineFeedCategoriesFragment.java b/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineFeedCategoriesFragment.java index e73ce6b4..90bcd93e 100755 --- a/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineFeedCategoriesFragment.java +++ b/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineFeedCategoriesFragment.java @@ -61,7 +61,7 @@ public class OfflineFeedCategoriesFragment extends BaseFeedlistFragment implemen String order = m_prefs.getBoolean("sort_feeds_by_unread", false) ? "unread DESC, title" : "title"; - return m_activity.getReadableDb().query("cats_unread", + return m_activity.getDatabase().query("cats_unread", null, unreadOnly, null, null, null, order); } diff --git a/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineFeedsFragment.java b/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineFeedsFragment.java index 7ef733b3..97f4880e 100755 --- a/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineFeedsFragment.java +++ b/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineFeedsFragment.java @@ -99,10 +99,10 @@ public class OfflineFeedsFragment extends BaseFeedlistFragment implements OnItem String order = m_prefs.getBoolean("sort_feeds_by_unread", false) ? "unread DESC, title" : "title"; if (m_catId != -1) { - return m_activity.getReadableDb().query("feeds_unread", + return m_activity.getDatabase().query("feeds_unread", null, unreadOnly + " AND cat_id = ?", new String[] { String.valueOf(m_catId) }, null, null, order); } else { - return m_activity.getReadableDb().query("feeds_unread", + return m_activity.getDatabase().query("feeds_unread", null, unreadOnly, null, null, null, order); } } diff --git a/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineHeadlinesFragment.java b/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineHeadlinesFragment.java index 076b687a..1294e8b0 100755 --- a/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineHeadlinesFragment.java +++ b/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineHeadlinesFragment.java @@ -87,7 +87,7 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis } public int getSelectedArticleCount() { - Cursor c = m_activity.getReadableDb().query("articles", + Cursor c = m_activity.getDatabase().query("articles", new String[] { "COUNT(*)" }, "selected = 1", null, null, null, null); c.moveToFirst(); int selected = c.getInt(0); @@ -116,7 +116,7 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis return true; case R.id.selection_toggle_marked: if (getSelectedArticleCount() > 0) { - SQLiteStatement stmt = m_activity.getWritableDb() + SQLiteStatement stmt = m_activity.getDatabase() .compileStatement( "UPDATE articles SET modified = 1, marked = NOT marked WHERE selected = 1"); stmt.execute(); @@ -124,7 +124,7 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis } else { int articleId = getArticleIdAtPosition(info.position); - SQLiteStatement stmt = m_activity.getWritableDb().compileStatement( + SQLiteStatement stmt = m_activity.getDatabase().compileStatement( "UPDATE articles SET modified = 1, marked = NOT marked WHERE " + BaseColumns._ID + " = ?"); stmt.bindLong(1, articleId); @@ -135,7 +135,7 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis return true; case R.id.selection_toggle_published: if (getSelectedArticleCount() > 0) { - SQLiteStatement stmt = m_activity.getWritableDb() + SQLiteStatement stmt = m_activity.getDatabase() .compileStatement( "UPDATE articles SET modified = 1, published = NOT published WHERE selected = 1"); stmt.execute(); @@ -143,7 +143,7 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis } else { int articleId = getArticleIdAtPosition(info.position); - SQLiteStatement stmt = m_activity.getWritableDb().compileStatement( + SQLiteStatement stmt = m_activity.getDatabase().compileStatement( "UPDATE articles SET modified = 1, published = NOT published WHERE " + BaseColumns._ID + " = ?"); stmt.bindLong(1, articleId); @@ -154,7 +154,7 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis return true; case R.id.selection_toggle_unread: if (getSelectedArticleCount() > 0) { - SQLiteStatement stmt = m_activity.getWritableDb() + SQLiteStatement stmt = m_activity.getDatabase() .compileStatement( "UPDATE articles SET modified = 1, unread = NOT unread WHERE selected = 1"); stmt.execute(); @@ -162,7 +162,7 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis } else { int articleId = getArticleIdAtPosition(info.position); - SQLiteStatement stmt = m_activity.getWritableDb().compileStatement( + SQLiteStatement stmt = m_activity.getDatabase().compileStatement( "UPDATE articles SET modified = 1, unread = NOT unread WHERE " + BaseColumns._ID + " = ?"); stmt.bindLong(1, articleId); @@ -186,12 +186,12 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis String updatedOperator = (m_prefs.getBoolean("offline_oldest_first", false)) ? "<" : ">"; if (m_feedIsCat) { - stmt = m_activity.getWritableDb().compileStatement( + stmt = m_activity.getDatabase().compileStatement( "UPDATE articles SET modified = 1, unread = 0 WHERE " + "updated "+updatedOperator+" (SELECT updated FROM articles WHERE " + BaseColumns._ID + " = ?) " + "AND unread = 1 AND feed_id IN (SELECT "+BaseColumns._ID+" FROM feeds WHERE cat_id = ?)"); } else { - stmt = m_activity.getWritableDb().compileStatement( + stmt = m_activity.getDatabase().compileStatement( "UPDATE articles SET modified = 1, unread = 0 WHERE " + "updated "+updatedOperator+" (SELECT updated FROM articles WHERE " + BaseColumns._ID + " = ?) " + "AND unread = 1 AND feed_id = ?"); @@ -285,7 +285,7 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis m_feedIsCat = savedInstanceState.getBoolean("feedIsCat"); m_compactLayoutMode = savedInstanceState.getBoolean("compactLayoutMode"); } else { - m_activity.getWritableDb().execSQL("UPDATE articles SET selected = 0 "); + m_activity.getDatabase().execSQL("UPDATE articles SET selected = 0 "); } if ("HL_COMPACT".equals(m_prefs.getString("headline_mode", "HL_DEFAULT"))) @@ -365,11 +365,11 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis String orderBy = (m_prefs.getBoolean("offline_oldest_first", false)) ? "updated" : "updated DESC"; if (m_searchQuery == null || m_searchQuery.equals("")) { - return m_activity.getReadableDb().query("articles LEFT JOIN feeds ON (feed_id = feeds."+BaseColumns._ID+")", + return m_activity.getDatabase().query("articles LEFT JOIN feeds ON (feed_id = feeds."+BaseColumns._ID+")", new String[] { "articles.*", "feeds.title AS feed_title" }, feedClause, new String[] { String.valueOf(m_feedId) }, null, null, orderBy); } else { - return m_activity.getReadableDb().query("articles LEFT JOIN feeds ON (feed_id = feeds."+BaseColumns._ID+")", + return m_activity.getDatabase().query("articles LEFT JOIN feeds ON (feed_id = feeds."+BaseColumns._ID+")", new String[] { "articles.*", "feeds.title AS feed_title" }, feedClause + " AND (articles.title LIKE '%' || ? || '%' OR content LIKE '%' || ? || '%')", new String[] { String.valueOf(m_feedId), m_searchQuery, m_searchQuery }, null, null, orderBy); @@ -596,7 +596,7 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis public void onClick(View view) { Log.d(TAG, "textImage : onclicked"); - SQLiteStatement stmtUpdate = m_activity.getWritableDb().compileStatement("UPDATE articles SET selected = NOT selected " + + SQLiteStatement stmtUpdate = m_activity.getDatabase().compileStatement("UPDATE articles SET selected = NOT selected " + "WHERE " + BaseColumns._ID + " = ?"); stmtUpdate.bindLong(1, articleId); @@ -651,7 +651,7 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis @Override public void onClick(View v) { - SQLiteStatement stmtUpdate = m_activity.getWritableDb().compileStatement("UPDATE articles SET modified = 1, marked = NOT marked " + + SQLiteStatement stmtUpdate = m_activity.getDatabase().compileStatement("UPDATE articles SET modified = 1, marked = NOT marked " + "WHERE " + BaseColumns._ID + " = ?"); stmtUpdate.bindLong(1, articleId); @@ -673,7 +673,7 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis @Override public void onClick(View v) { - SQLiteStatement stmtUpdate = m_activity.getWritableDb().compileStatement("UPDATE articles SET modified = 1, published = NOT published " + + SQLiteStatement stmtUpdate = m_activity.getDatabase().compileStatement("UPDATE articles SET modified = 1, published = NOT published " + "WHERE " + BaseColumns._ID + " = ?"); stmtUpdate.bindLong(1, articleId); @@ -734,7 +734,7 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis public void onClick(View view) { CheckBox cb = (CheckBox) view; - SQLiteStatement stmtUpdate = m_activity.getWritableDb().compileStatement("UPDATE articles SET selected = ? " + + SQLiteStatement stmtUpdate = m_activity.getDatabase().compileStatement("UPDATE articles SET selected = ? " + "WHERE " + BaseColumns._ID + " = ?"); stmtUpdate.bindLong(1, cb.isChecked() ? 1 : 0); @@ -897,7 +897,7 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis if (scrollState == SCROLL_STATE_IDLE && m_prefs.getBoolean("headlines_mark_read_scroll", false)) { if (!m_readArticleIds.isEmpty()) { - SQLiteStatement stmt = m_activity.getWritableDb().compileStatement( + SQLiteStatement stmt = m_activity.getDatabase().compileStatement( "UPDATE articles SET modified = 1, unread = 0 " + "WHERE " + BaseColumns._ID + " = ?"); diff --git a/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineMasterActivity.java b/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineMasterActivity.java index 52de0c28..95f6add0 100644 --- a/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineMasterActivity.java +++ b/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineMasterActivity.java @@ -321,7 +321,7 @@ public class OfflineMasterActivity extends OfflineActivity implements OfflineHea public void onArticleSelected(int articleId, boolean open) { if (!open) { - SQLiteStatement stmt = getWritableDb().compileStatement( + SQLiteStatement stmt = getDatabase().compileStatement( "UPDATE articles SET modified = 1, unread = 0 " + "WHERE " + BaseColumns._ID + " = ?"); diff --git a/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineUploadService.java b/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineUploadService.java index 54915c9e..8f9f742d 100644 --- a/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineUploadService.java +++ b/org.fox.ttrss/src/main/java/org/fox/ttrss/offline/OfflineUploadService.java @@ -89,7 +89,7 @@ public class OfflineUploadService extends IntentService { } private void initDatabase() { - DatabaseHelper dh = new DatabaseHelper(getApplicationContext()); + DatabaseHelper dh = DatabaseHelper.getInstance(this); m_writableDb = dh.getWritableDatabase(); m_readableDb = dh.getReadableDatabase(); } diff --git a/org.fox.ttrss/src/main/java/org/fox/ttrss/util/DatabaseHelper.java b/org.fox.ttrss/src/main/java/org/fox/ttrss/util/DatabaseHelper.java index 572ff62e..114f598b 100644 --- a/org.fox.ttrss/src/main/java/org/fox/ttrss/util/DatabaseHelper.java +++ b/org.fox.ttrss/src/main/java/org/fox/ttrss/util/DatabaseHelper.java @@ -1,5 +1,7 @@ package org.fox.ttrss.util; + import android.content.Context; +import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.provider.BaseColumns; @@ -11,11 +13,23 @@ public class DatabaseHelper extends SQLiteOpenHelper { private final String TAG = this.getClass().getSimpleName(); public static final String DATABASE_NAME = "OfflineStorage.db"; public static final int DATABASE_VERSION = 4; - - public DatabaseHelper(Context context) { + private static DatabaseHelper m_instance; + private Context m_context; + + private DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); + m_context = context; } - + + public static synchronized DatabaseHelper getInstance(Context context) { + + if (m_instance == null) { + m_instance = new DatabaseHelper(context.getApplicationContext()); + } + + return m_instance; + } + @Override public void onCreate(SQLiteDatabase db) { db.execSQL("DROP VIEW IF EXISTS cats_unread;"); @@ -86,4 +100,39 @@ public class DatabaseHelper extends SQLiteOpenHelper { onCreate(db); } + public boolean hasPendingOfflineData() { + try { + Cursor c = getReadableDatabase().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 + } + + return false; + } + + public boolean hasOfflineData() { + try { + Cursor c = getReadableDatabase().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? + } + + return false; + } + }