rework databasehelper as a singleton

simplify offline interaction with the database
This commit is contained in:
Andrew Dolgov 2015-06-10 23:51:41 +03:00
parent 85fc4abbb2
commit 3071df1abe
13 changed files with 122 additions and 116 deletions

View File

@ -31,8 +31,10 @@ public class CommonActivity extends ActionBarActivity {
public static final int EXCERPT_MAX_LENGTH = 256; public static final int EXCERPT_MAX_LENGTH = 256;
public static final int EXCERPT_MAX_QUERY_LENGTH = 2048; public static final int EXCERPT_MAX_QUERY_LENGTH = 2048;
private SQLiteDatabase m_readableDb; private DatabaseHelper m_databaseHelper;
private SQLiteDatabase m_writableDb;
//private SQLiteDatabase m_readableDb;
//private SQLiteDatabase m_writableDb;
private boolean m_smallScreenMode = true; private boolean m_smallScreenMode = true;
private String m_theme; private String m_theme;
@ -57,6 +59,14 @@ public class CommonActivity extends ActionBarActivity {
m_smallScreenMode = smallScreen; m_smallScreenMode = smallScreen;
} }
public DatabaseHelper getDatabaseHelper() {
return m_databaseHelper;
}
public SQLiteDatabase getDatabase() {
return m_databaseHelper.getWritableDatabase();
}
public boolean getUnreadOnly() { public boolean getUnreadOnly() {
return m_prefs.getBoolean("show_unread_only", true); return m_prefs.getBoolean("show_unread_only", true);
} }
@ -82,21 +92,6 @@ public class CommonActivity extends ActionBarActivity {
toast.show(); 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 @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
@ -113,13 +108,12 @@ public class CommonActivity extends ActionBarActivity {
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
m_readableDb.close();
m_writableDb.close();
} }
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
m_databaseHelper = DatabaseHelper.getInstance(this);
m_prefs = PreferenceManager m_prefs = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext()); .getDefaultSharedPreferences(getApplicationContext());
@ -129,8 +123,6 @@ public class CommonActivity extends ActionBarActivity {
m_theme = m_prefs.getString("theme", CommonActivity.THEME_DEFAULT); m_theme = m_prefs.getString("theme", CommonActivity.THEME_DEFAULT);
} }
initDatabase();
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
} }

View File

@ -14,7 +14,6 @@ import android.content.IntentFilter;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager; 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 @Override
public void onPause() { public void onPause() {
@ -463,7 +428,7 @@ public class OnlineActivity extends CommonActivity {
startActivityForResult(intent, 0); startActivityForResult(intent, 0);
overridePendingTransition(0, 0); overridePendingTransition(0, 0);
if (hasPendingOfflineData()) if (getDatabaseHelper().hasPendingOfflineData())
syncOfflineData(); syncOfflineData();
finish(); finish();
@ -1192,7 +1157,7 @@ public class OnlineActivity extends CommonActivity {
setSessionId(null); setSessionId(null);
initMenu(); initMenu();
if (hasOfflineData()) { if (getDatabaseHelper().hasOfflineData()) {
AlertDialog.Builder builder = new AlertDialog.Builder( AlertDialog.Builder builder = new AlertDialog.Builder(
OnlineActivity.this) OnlineActivity.this)

View File

@ -207,10 +207,10 @@ public class OfflineActivity extends CommonActivity {
SQLiteStatement stmtSelectAll = null; SQLiteStatement stmtSelectAll = null;
if (isCat) { 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 = ?)"); "UPDATE articles SET selected = 1 WHERE feed_id IN (SELECT "+BaseColumns._ID+" FROM feeds WHERE cat_id = ?)");
} else { } else {
stmtSelectAll = getWritableDb().compileStatement( stmtSelectAll = getDatabase().compileStatement(
"UPDATE articles SET selected = 1 WHERE feed_id = ?"); "UPDATE articles SET selected = 1 WHERE feed_id = ?");
} }
@ -224,10 +224,10 @@ public class OfflineActivity extends CommonActivity {
SQLiteStatement stmtSelectUnread = null; SQLiteStatement stmtSelectUnread = null;
if (isCat) { 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"); "UPDATE articles SET selected = 1 WHERE feed_id IN (SELECT "+BaseColumns._ID+" FROM feeds WHERE cat_id = ?) AND unread = 1");
} else { } else {
stmtSelectUnread = getWritableDb().compileStatement( stmtSelectUnread = getDatabase().compileStatement(
"UPDATE articles SET selected = 1 WHERE feed_id = ? AND unread = 1"); "UPDATE articles SET selected = 1 WHERE feed_id = ? AND unread = 1");
} }
@ -455,7 +455,7 @@ public class OfflineActivity extends CommonActivity {
if (oap != null) { if (oap != null) {
int articleId = oap.getSelectedArticleId(); int articleId = oap.getSelectedArticleId();
SQLiteStatement stmt = getWritableDb().compileStatement( SQLiteStatement stmt = getDatabase().compileStatement(
"UPDATE articles SET modified = 1, marked = NOT marked WHERE " "UPDATE articles SET modified = 1, marked = NOT marked WHERE "
+ BaseColumns._ID + " = ?"); + BaseColumns._ID + " = ?");
stmt.bindLong(1, articleId); stmt.bindLong(1, articleId);
@ -470,7 +470,7 @@ public class OfflineActivity extends CommonActivity {
return true; */ return true; */
case R.id.selection_toggle_unread: case R.id.selection_toggle_unread:
if (getSelectedArticleCount() > 0) { if (getSelectedArticleCount() > 0) {
SQLiteStatement stmt = getWritableDb() SQLiteStatement stmt = getDatabase()
.compileStatement( .compileStatement(
"UPDATE articles SET modified = 1, unread = NOT unread WHERE selected = 1"); "UPDATE articles SET modified = 1, unread = NOT unread WHERE selected = 1");
stmt.execute(); stmt.execute();
@ -481,7 +481,7 @@ public class OfflineActivity extends CommonActivity {
return true; return true;
case R.id.selection_toggle_marked: case R.id.selection_toggle_marked:
if (getSelectedArticleCount() > 0) { if (getSelectedArticleCount() > 0) {
SQLiteStatement stmt = getWritableDb() SQLiteStatement stmt = getDatabase()
.compileStatement( .compileStatement(
"UPDATE articles SET modified = 1, marked = NOT marked WHERE selected = 1"); "UPDATE articles SET modified = 1, marked = NOT marked WHERE selected = 1");
stmt.execute(); stmt.execute();
@ -492,7 +492,7 @@ public class OfflineActivity extends CommonActivity {
return true; return true;
case R.id.selection_toggle_published: case R.id.selection_toggle_published:
if (getSelectedArticleCount() > 0) { if (getSelectedArticleCount() > 0) {
SQLiteStatement stmt = getWritableDb() SQLiteStatement stmt = getDatabase()
.compileStatement( .compileStatement(
"UPDATE articles SET modified = 1, published = NOT published WHERE selected = 1"); "UPDATE articles SET modified = 1, published = NOT published WHERE selected = 1");
stmt.execute(); stmt.execute();
@ -505,7 +505,7 @@ public class OfflineActivity extends CommonActivity {
if (oap != null) { if (oap != null) {
int articleId = oap.getSelectedArticleId(); int articleId = oap.getSelectedArticleId();
SQLiteStatement stmt = getWritableDb().compileStatement( SQLiteStatement stmt = getDatabase().compileStatement(
"UPDATE articles SET modified = 1, published = NOT published WHERE " "UPDATE articles SET modified = 1, published = NOT published WHERE "
+ BaseColumns._ID + " = ?"); + BaseColumns._ID + " = ?");
stmt.bindLong(1, articleId); stmt.bindLong(1, articleId);
@ -524,12 +524,12 @@ public class OfflineActivity extends CommonActivity {
SQLiteStatement stmt = null; SQLiteStatement stmt = null;
if (isCat) { if (isCat) {
stmt = getWritableDb().compileStatement( stmt = getDatabase().compileStatement(
"UPDATE articles SET modified = 1, unread = 0 WHERE " + "UPDATE articles SET modified = 1, unread = 0 WHERE " +
"updated >= (SELECT updated FROM articles WHERE " + BaseColumns._ID + " = ?) " + "updated >= (SELECT updated FROM articles WHERE " + BaseColumns._ID + " = ?) " +
"AND feed_id IN (SELECT "+BaseColumns._ID+" FROM feeds WHERE cat_id = ?)"); "AND feed_id IN (SELECT "+BaseColumns._ID+" FROM feeds WHERE cat_id = ?)");
} else { } else {
stmt = getWritableDb().compileStatement( stmt = getDatabase().compileStatement(
"UPDATE articles SET modified = 1, unread = 0 WHERE " + "UPDATE articles SET modified = 1, unread = 0 WHERE " +
"updated >= (SELECT updated FROM articles WHERE " + BaseColumns._ID + " = ?) " + "updated >= (SELECT updated FROM articles WHERE " + BaseColumns._ID + " = ?) " +
"AND feed_id = ?"); "AND feed_id = ?");
@ -614,9 +614,9 @@ public class OfflineActivity extends CommonActivity {
} }
protected Cursor getArticleById(int articleId) { protected Cursor getArticleById(int articleId) {
Cursor c = getReadableDb().query("articles", null, Cursor c = getDatabase().query("articles", null,
BaseColumns._ID + "=?", BaseColumns._ID + "=?",
new String[] { String.valueOf(articleId) }, null, null, null); new String[]{String.valueOf(articleId)}, null, null, null);
c.moveToFirst(); c.moveToFirst();
@ -659,9 +659,9 @@ public class OfflineActivity extends CommonActivity {
} }
protected Cursor getFeedById(int feedId) { protected Cursor getFeedById(int feedId) {
Cursor c = getReadableDb().query("feeds", null, Cursor c = getDatabase().query("feeds", null,
BaseColumns._ID + "=?", BaseColumns._ID + "=?",
new String[] { String.valueOf(feedId) }, null, null, null); new String[]{String.valueOf(feedId)}, null, null, null);
c.moveToFirst(); c.moveToFirst();
@ -669,9 +669,9 @@ public class OfflineActivity extends CommonActivity {
} }
protected Cursor getCatById(int catId) { protected Cursor getCatById(int catId) {
Cursor c = getReadableDb().query("categories", null, Cursor c = getDatabase().query("categories", null,
BaseColumns._ID + "=?", BaseColumns._ID + "=?",
new String[] { String.valueOf(catId) }, null, null, null); new String[]{String.valueOf(catId)}, null, null, null);
c.moveToFirst(); c.moveToFirst();
@ -715,8 +715,8 @@ public class OfflineActivity extends CommonActivity {
} }
protected int getSelectedArticleCount() { protected int getSelectedArticleCount() {
Cursor c = getReadableDb().query("articles", Cursor c = getDatabase().query("articles",
new String[] { "COUNT(*)" }, "selected = 1", null, null, null, new String[]{"COUNT(*)"}, "selected = 1", null, null, null,
null); null);
c.moveToFirst(); c.moveToFirst();
int selected = c.getInt(0); int selected = c.getInt(0);
@ -730,12 +730,12 @@ public class OfflineActivity extends CommonActivity {
Cursor c; Cursor c;
if (isCat) { 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[] { "COUNT(*)" }, "unread = 1 AND feed_id IN (SELECT "+BaseColumns._ID+" FROM feeds WHERE cat_id = ?)",
new String[] { String.valueOf(feedId) }, new String[] { String.valueOf(feedId) },
null, null, null); null, null, null);
} else { } else {
c = getReadableDb().query("articles", c = getDatabase().query("articles",
new String[] { "COUNT(*)" }, "unread = 1 AND feed_id = ?", new String[] { "COUNT(*)" }, "unread = 1 AND feed_id = ?",
new String[] { String.valueOf(feedId) }, new String[] { String.valueOf(feedId) },
null, null, null); null, null, null);
@ -749,7 +749,7 @@ public class OfflineActivity extends CommonActivity {
} }
protected void deselectAllArticles() { protected void deselectAllArticles() {
getWritableDb().execSQL("UPDATE articles SET selected = 0 "); getDatabase().execSQL("UPDATE articles SET selected = 0 ");
refresh(); refresh();
} }
@ -780,14 +780,14 @@ public class OfflineActivity extends CommonActivity {
public void catchupFeed(int feedId, boolean isCat) { public void catchupFeed(int feedId, boolean isCat) {
if (isCat) { if (isCat) {
SQLiteStatement stmt = getWritableDb().compileStatement( SQLiteStatement stmt = getDatabase().compileStatement(
"UPDATE articles SET modified = 1, unread = 0 WHERE feed_id IN (SELECT "+ "UPDATE articles SET modified = 1, unread = 0 WHERE feed_id IN (SELECT " +
BaseColumns._ID+" FROM feeds WHERE cat_id = ?)"); BaseColumns._ID + " FROM feeds WHERE cat_id = ?)");
stmt.bindLong(1, feedId); stmt.bindLong(1, feedId);
stmt.execute(); stmt.execute();
stmt.close(); stmt.close();
} else { } else {
SQLiteStatement stmt = getWritableDb().compileStatement( SQLiteStatement stmt = getDatabase().compileStatement(
"UPDATE articles SET modified = 1, unread = 0 WHERE feed_id = ?"); "UPDATE articles SET modified = 1, unread = 0 WHERE feed_id = ?");
stmt.bindLong(1, feedId); stmt.bindLong(1, feedId);
stmt.execute(); stmt.execute();

View File

@ -126,7 +126,7 @@ public class OfflineArticleFragment extends Fragment {
View view = inflater.inflate(R.layout.fragment_article, container, false); 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[] { "articles.*", "feeds.title AS feed_title" }, "articles." + BaseColumns._ID + "=?",
new String[] { String.valueOf(m_articleId) }, null, null, null); new String[] { String.valueOf(m_articleId) }, null, null, null);

View File

@ -66,11 +66,11 @@ public class OfflineArticlePager extends Fragment {
String orderBy = (m_prefs.getBoolean("offline_oldest_first", false)) ? "updated" : "updated DESC"; String orderBy = (m_prefs.getBoolean("offline_oldest_first", false)) ? "updated" : "updated DESC";
if (m_searchQuery == null || m_searchQuery.equals("")) { 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[] { "articles."+BaseColumns._ID, "feeds.title AS feed_title" }, feedClause,
new String[] { String.valueOf(m_feedId) }, null, null, orderBy); new String[] { String.valueOf(m_feedId) }, null, null, orderBy);
} else { } 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 }, new String[] { "articles."+BaseColumns._ID },
feedClause + " AND (articles.title LIKE '%' || ? || '%' OR content LIKE '%' || ? || '%')", feedClause + " AND (articles.title LIKE '%' || ? || '%' OR content LIKE '%' || ? || '%')",
new String[] { String.valueOf(m_feedId), m_searchQuery, m_searchQuery }, null, null, orderBy); new String[] { String.valueOf(m_feedId), m_searchQuery, m_searchQuery }, null, null, orderBy);

View File

@ -145,7 +145,7 @@ public class OfflineDetailActivity extends OfflineActivity implements OfflineHea
public void onArticleSelected(int articleId, boolean open) { public void onArticleSelected(int articleId, boolean open) {
if (!open) { if (!open) {
SQLiteStatement stmt = getWritableDb().compileStatement( SQLiteStatement stmt = getDatabase().compileStatement(
"UPDATE articles SET modified = 1, unread = 0 " + "WHERE " + BaseColumns._ID "UPDATE articles SET modified = 1, unread = 0 " + "WHERE " + BaseColumns._ID
+ " = ?"); + " = ?");

View File

@ -180,7 +180,7 @@ public class OfflineDownloadService extends Service {
} }
private void initDatabase() { private void initDatabase() {
DatabaseHelper dh = new DatabaseHelper(getApplicationContext()); DatabaseHelper dh = DatabaseHelper.getInstance(this);
m_writableDb = dh.getWritableDatabase(); m_writableDb = dh.getWritableDatabase();
m_readableDb = dh.getReadableDatabase(); m_readableDb = dh.getReadableDatabase();
} }

View File

@ -61,7 +61,7 @@ public class OfflineFeedCategoriesFragment extends BaseFeedlistFragment implemen
String order = m_prefs.getBoolean("sort_feeds_by_unread", false) ? "unread DESC, title" : "title"; 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); null, unreadOnly, null, null, null, order);
} }

View File

@ -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"; String order = m_prefs.getBoolean("sort_feeds_by_unread", false) ? "unread DESC, title" : "title";
if (m_catId != -1) { 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); null, unreadOnly + " AND cat_id = ?", new String[] { String.valueOf(m_catId) }, null, null, order);
} else { } else {
return m_activity.getReadableDb().query("feeds_unread", return m_activity.getDatabase().query("feeds_unread",
null, unreadOnly, null, null, null, order); null, unreadOnly, null, null, null, order);
} }
} }

View File

@ -87,7 +87,7 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis
} }
public int getSelectedArticleCount() { 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); new String[] { "COUNT(*)" }, "selected = 1", null, null, null, null);
c.moveToFirst(); c.moveToFirst();
int selected = c.getInt(0); int selected = c.getInt(0);
@ -116,7 +116,7 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis
return true; return true;
case R.id.selection_toggle_marked: case R.id.selection_toggle_marked:
if (getSelectedArticleCount() > 0) { if (getSelectedArticleCount() > 0) {
SQLiteStatement stmt = m_activity.getWritableDb() SQLiteStatement stmt = m_activity.getDatabase()
.compileStatement( .compileStatement(
"UPDATE articles SET modified = 1, marked = NOT marked WHERE selected = 1"); "UPDATE articles SET modified = 1, marked = NOT marked WHERE selected = 1");
stmt.execute(); stmt.execute();
@ -124,7 +124,7 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis
} else { } else {
int articleId = getArticleIdAtPosition(info.position); 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 " "UPDATE articles SET modified = 1, marked = NOT marked WHERE "
+ BaseColumns._ID + " = ?"); + BaseColumns._ID + " = ?");
stmt.bindLong(1, articleId); stmt.bindLong(1, articleId);
@ -135,7 +135,7 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis
return true; return true;
case R.id.selection_toggle_published: case R.id.selection_toggle_published:
if (getSelectedArticleCount() > 0) { if (getSelectedArticleCount() > 0) {
SQLiteStatement stmt = m_activity.getWritableDb() SQLiteStatement stmt = m_activity.getDatabase()
.compileStatement( .compileStatement(
"UPDATE articles SET modified = 1, published = NOT published WHERE selected = 1"); "UPDATE articles SET modified = 1, published = NOT published WHERE selected = 1");
stmt.execute(); stmt.execute();
@ -143,7 +143,7 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis
} else { } else {
int articleId = getArticleIdAtPosition(info.position); 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 " "UPDATE articles SET modified = 1, published = NOT published WHERE "
+ BaseColumns._ID + " = ?"); + BaseColumns._ID + " = ?");
stmt.bindLong(1, articleId); stmt.bindLong(1, articleId);
@ -154,7 +154,7 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis
return true; return true;
case R.id.selection_toggle_unread: case R.id.selection_toggle_unread:
if (getSelectedArticleCount() > 0) { if (getSelectedArticleCount() > 0) {
SQLiteStatement stmt = m_activity.getWritableDb() SQLiteStatement stmt = m_activity.getDatabase()
.compileStatement( .compileStatement(
"UPDATE articles SET modified = 1, unread = NOT unread WHERE selected = 1"); "UPDATE articles SET modified = 1, unread = NOT unread WHERE selected = 1");
stmt.execute(); stmt.execute();
@ -162,7 +162,7 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis
} else { } else {
int articleId = getArticleIdAtPosition(info.position); 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 " "UPDATE articles SET modified = 1, unread = NOT unread WHERE "
+ BaseColumns._ID + " = ?"); + BaseColumns._ID + " = ?");
stmt.bindLong(1, articleId); stmt.bindLong(1, articleId);
@ -186,12 +186,12 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis
String updatedOperator = (m_prefs.getBoolean("offline_oldest_first", false)) ? "<" : ">"; String updatedOperator = (m_prefs.getBoolean("offline_oldest_first", false)) ? "<" : ">";
if (m_feedIsCat) { if (m_feedIsCat) {
stmt = m_activity.getWritableDb().compileStatement( stmt = m_activity.getDatabase().compileStatement(
"UPDATE articles SET modified = 1, unread = 0 WHERE " + "UPDATE articles SET modified = 1, unread = 0 WHERE " +
"updated "+updatedOperator+" (SELECT updated FROM articles WHERE " + BaseColumns._ID + " = ?) " + "updated "+updatedOperator+" (SELECT updated FROM articles WHERE " + BaseColumns._ID + " = ?) " +
"AND unread = 1 AND feed_id IN (SELECT "+BaseColumns._ID+" FROM feeds WHERE cat_id = ?)"); "AND unread = 1 AND feed_id IN (SELECT "+BaseColumns._ID+" FROM feeds WHERE cat_id = ?)");
} else { } else {
stmt = m_activity.getWritableDb().compileStatement( stmt = m_activity.getDatabase().compileStatement(
"UPDATE articles SET modified = 1, unread = 0 WHERE " + "UPDATE articles SET modified = 1, unread = 0 WHERE " +
"updated "+updatedOperator+" (SELECT updated FROM articles WHERE " + BaseColumns._ID + " = ?) " + "updated "+updatedOperator+" (SELECT updated FROM articles WHERE " + BaseColumns._ID + " = ?) " +
"AND unread = 1 AND feed_id = ?"); "AND unread = 1 AND feed_id = ?");
@ -285,7 +285,7 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis
m_feedIsCat = savedInstanceState.getBoolean("feedIsCat"); m_feedIsCat = savedInstanceState.getBoolean("feedIsCat");
m_compactLayoutMode = savedInstanceState.getBoolean("compactLayoutMode"); m_compactLayoutMode = savedInstanceState.getBoolean("compactLayoutMode");
} else { } 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"))) 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"; String orderBy = (m_prefs.getBoolean("offline_oldest_first", false)) ? "updated" : "updated DESC";
if (m_searchQuery == null || m_searchQuery.equals("")) { 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[] { "articles.*", "feeds.title AS feed_title" }, feedClause,
new String[] { String.valueOf(m_feedId) }, null, null, orderBy); new String[] { String.valueOf(m_feedId) }, null, null, orderBy);
} else { } 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" }, new String[] { "articles.*", "feeds.title AS feed_title" },
feedClause + " AND (articles.title LIKE '%' || ? || '%' OR content LIKE '%' || ? || '%')", feedClause + " AND (articles.title LIKE '%' || ? || '%' OR content LIKE '%' || ? || '%')",
new String[] { String.valueOf(m_feedId), m_searchQuery, m_searchQuery }, null, null, orderBy); 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) { public void onClick(View view) {
Log.d(TAG, "textImage : onclicked"); 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 + " = ?"); "WHERE " + BaseColumns._ID + " = ?");
stmtUpdate.bindLong(1, articleId); stmtUpdate.bindLong(1, articleId);
@ -651,7 +651,7 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis
@Override @Override
public void onClick(View v) { 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 + " = ?"); "WHERE " + BaseColumns._ID + " = ?");
stmtUpdate.bindLong(1, articleId); stmtUpdate.bindLong(1, articleId);
@ -673,7 +673,7 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis
@Override @Override
public void onClick(View v) { 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 + " = ?"); "WHERE " + BaseColumns._ID + " = ?");
stmtUpdate.bindLong(1, articleId); stmtUpdate.bindLong(1, articleId);
@ -734,7 +734,7 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis
public void onClick(View view) { public void onClick(View view) {
CheckBox cb = (CheckBox) 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 + " = ?"); "WHERE " + BaseColumns._ID + " = ?");
stmtUpdate.bindLong(1, cb.isChecked() ? 1 : 0); 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 (scrollState == SCROLL_STATE_IDLE && m_prefs.getBoolean("headlines_mark_read_scroll", false)) {
if (!m_readArticleIds.isEmpty()) { 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 "UPDATE articles SET modified = 1, unread = 0 " + "WHERE " + BaseColumns._ID
+ " = ?"); + " = ?");

View File

@ -321,7 +321,7 @@ public class OfflineMasterActivity extends OfflineActivity implements OfflineHea
public void onArticleSelected(int articleId, boolean open) { public void onArticleSelected(int articleId, boolean open) {
if (!open) { if (!open) {
SQLiteStatement stmt = getWritableDb().compileStatement( SQLiteStatement stmt = getDatabase().compileStatement(
"UPDATE articles SET modified = 1, unread = 0 " + "WHERE " + BaseColumns._ID "UPDATE articles SET modified = 1, unread = 0 " + "WHERE " + BaseColumns._ID
+ " = ?"); + " = ?");

View File

@ -89,7 +89,7 @@ public class OfflineUploadService extends IntentService {
} }
private void initDatabase() { private void initDatabase() {
DatabaseHelper dh = new DatabaseHelper(getApplicationContext()); DatabaseHelper dh = DatabaseHelper.getInstance(this);
m_writableDb = dh.getWritableDatabase(); m_writableDb = dh.getWritableDatabase();
m_readableDb = dh.getReadableDatabase(); m_readableDb = dh.getReadableDatabase();
} }

View File

@ -1,5 +1,7 @@
package org.fox.ttrss.util; package org.fox.ttrss.util;
import android.content.Context; import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns; import android.provider.BaseColumns;
@ -11,9 +13,21 @@ public class DatabaseHelper extends SQLiteOpenHelper {
private final String TAG = this.getClass().getSimpleName(); private final String TAG = this.getClass().getSimpleName();
public static final String DATABASE_NAME = "OfflineStorage.db"; public static final String DATABASE_NAME = "OfflineStorage.db";
public static final int DATABASE_VERSION = 4; public static final int DATABASE_VERSION = 4;
private static DatabaseHelper m_instance;
private Context m_context;
public DatabaseHelper(Context context) { private DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION); 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 @Override
@ -86,4 +100,39 @@ public class DatabaseHelper extends SQLiteOpenHelper {
onCreate(db); 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;
}
} }