attempt to sync db access
This commit is contained in:
parent
0e58bef54c
commit
550cce4116
@ -22,7 +22,6 @@ public class ArticleFragment extends Fragment {
|
||||
|
||||
protected SharedPreferences m_prefs;
|
||||
protected int m_articleId;
|
||||
protected SQLiteDatabase m_db;
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
@ -33,12 +32,9 @@ public class ArticleFragment extends Fragment {
|
||||
|
||||
View view = inflater.inflate(R.layout.article_fragment, container, false);
|
||||
|
||||
DatabaseHelper dh = new DatabaseHelper(getActivity());
|
||||
m_db = dh.getReadableDatabase();
|
||||
|
||||
Log.d(TAG, "Opening article #" + m_articleId);
|
||||
|
||||
Cursor c = m_db.query("articles", null, BaseColumns._ID + "=?",
|
||||
Cursor c = ((MainActivity)getActivity()).getReadableDb().query("articles", null, BaseColumns._ID + "=?",
|
||||
new String[] { String.valueOf(m_articleId) }, null, null, null);
|
||||
|
||||
c.moveToFirst();
|
||||
@ -72,9 +68,7 @@ public class ArticleFragment extends Fragment {
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
|
||||
m_db.close();
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -87,7 +81,7 @@ public class ArticleFragment extends Fragment {
|
||||
@Override
|
||||
public void onAttach(Activity activity) {
|
||||
super.onAttach(activity);
|
||||
m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
|
||||
m_prefs = PreferenceManager.getDefaultSharedPreferences(activity.getApplicationContext());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -5,7 +5,6 @@ import android.app.Fragment;
|
||||
import android.app.FragmentTransaction;
|
||||
import android.content.SharedPreferences;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.os.Bundle;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.util.Log;
|
||||
@ -22,7 +21,6 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener {
|
||||
private final String TAG = this.getClass().getSimpleName();
|
||||
protected int m_feedId;
|
||||
protected SharedPreferences m_prefs;
|
||||
protected SQLiteDatabase m_db;
|
||||
protected Cursor m_cursor;
|
||||
protected SimpleCursorAdapter m_adapter;
|
||||
|
||||
@ -35,10 +33,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener {
|
||||
|
||||
View view = inflater.inflate(R.layout.headlines_fragment, container, false);
|
||||
|
||||
DatabaseHelper helper = new DatabaseHelper(getActivity());
|
||||
|
||||
m_db = helper.getReadableDatabase();
|
||||
m_cursor = m_db.query("articles", null, "feed_id = ?", new String[] { String.valueOf(m_feedId) }, null, null, "updated DESC");
|
||||
m_cursor = ((MainActivity)getActivity()).getReadableDb().query("articles", null, "feed_id = ?", new String[] { String.valueOf(m_feedId) }, null, null, "updated DESC");
|
||||
|
||||
m_adapter = new SimpleCursorAdapter(getActivity(), R.layout.headlines_row, m_cursor,
|
||||
new String[] { "title", "excerpt" }, new int[] { R.id.title, R.id.excerpt }, 0);
|
||||
@ -67,7 +62,6 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener {
|
||||
super.onDestroy();
|
||||
|
||||
m_cursor.close();
|
||||
m_db.close();
|
||||
}
|
||||
|
||||
public void initialize(int feedId) {
|
||||
|
@ -49,10 +49,21 @@ public class MainActivity extends Activity {
|
||||
protected int m_maxId = 0;
|
||||
protected int m_updateMode = UPDATE_INITIAL;
|
||||
|
||||
private SQLiteDatabase m_readableDb;
|
||||
private SQLiteDatabase m_writableDb;
|
||||
|
||||
protected enum SyncStatus { SYNC_INITIAL, SYNC_ONLINE, SYNC_OFFLINE };
|
||||
|
||||
protected MenuItem m_syncStatus;
|
||||
|
||||
protected synchronized SQLiteDatabase getReadableDb() {
|
||||
return m_readableDb;
|
||||
}
|
||||
|
||||
protected synchronized SQLiteDatabase getWritableDb() {
|
||||
return m_writableDb;
|
||||
}
|
||||
|
||||
protected String getSessionId() {
|
||||
return m_sessionId;
|
||||
}
|
||||
@ -113,15 +124,24 @@ public class MainActivity extends Activity {
|
||||
|
||||
// allow database to upgrade before we do anything else
|
||||
DatabaseHelper dh = new DatabaseHelper(getApplicationContext());
|
||||
SQLiteDatabase db = dh.getWritableDatabase();
|
||||
m_writableDb = dh.getWritableDatabase();
|
||||
m_readableDb = dh.getReadableDatabase();
|
||||
|
||||
if (m_updateMode == UPDATE_INITIAL && !m_splashDisabled) {
|
||||
db.execSQL("DELETE FROM feeds;");
|
||||
db.execSQL("DELETE FROM articles;");
|
||||
m_writableDb.execSQL("DELETE FROM feeds;");
|
||||
//db.execSQL("DELETE FROM articles;");
|
||||
|
||||
if (m_maxId == 0) {
|
||||
Cursor c = m_readableDb.query("articles", new String[] { BaseColumns._ID } , null, null, null, null, BaseColumns._ID + " DESC LIMIT 1");
|
||||
if (c.getCount() == 1) {
|
||||
c.moveToFirst();
|
||||
//Log.i(TAG, "Last article # " + c.getInt(c.getColumnIndex(BaseColumns._ID)));
|
||||
m_maxId = c.getInt(c.getColumnIndex(BaseColumns._ID));
|
||||
}
|
||||
c.close();
|
||||
}
|
||||
}
|
||||
|
||||
db.close();
|
||||
|
||||
setContentView(R.layout.main);
|
||||
|
||||
LinearLayout wrapper = (LinearLayout) findViewById(R.id.headlines_wrapper);
|
||||
@ -186,6 +206,9 @@ public class MainActivity extends Activity {
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
|
||||
m_writableDb.close();
|
||||
m_readableDb.close();
|
||||
|
||||
if (m_feedsTask != null) m_feedsTask.cancel();
|
||||
if (m_articlesTask != null) m_articlesTask.cancel();
|
||||
@ -263,7 +286,7 @@ public class MainActivity extends Activity {
|
||||
put("skip", String.valueOf(m_offset));
|
||||
put("view_mode", "unread");
|
||||
|
||||
if (m_updateMode != UPDATE_INITIAL) {
|
||||
if (m_offset == 0) {
|
||||
put("since_id", String.valueOf(m_maxId));
|
||||
}
|
||||
}
|
||||
@ -281,16 +304,13 @@ public class MainActivity extends Activity {
|
||||
Type listType = new TypeToken<List<Article>>() {}.getType();
|
||||
List<Article> articles = api.m_gson.fromJson(feeds_object, listType);
|
||||
|
||||
DatabaseHelper dh = new DatabaseHelper(getApplicationContext());
|
||||
SQLiteDatabase db = dh.getWritableDatabase(); // TODO rework to m_writableDb etc to prevent crashes on rotate/recreate
|
||||
|
||||
/* db.execSQL("DELETE FROM articles"); */
|
||||
|
||||
SQLiteStatement stmtInsert = db.compileStatement("INSERT INTO articles " +
|
||||
SQLiteStatement stmtInsert = getWritableDb().compileStatement("INSERT INTO articles " +
|
||||
"("+BaseColumns._ID+", unread, marked, published, updated, is_updated, title, link, feed_id, tags, content, excerpt) " +
|
||||
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
|
||||
|
||||
SQLiteStatement stmtUpdate = db.compileStatement("UPDATE articles SET " +
|
||||
SQLiteStatement stmtUpdate = getWritableDb().compileStatement("UPDATE articles SET " +
|
||||
"unread = ?, marked = ?, published = ?, updated = ?, is_updated = ?, title = ?, link = ?, feed_id = ?, " +
|
||||
"tags = ?, content = ?, excerpt = ? WHERE " + BaseColumns._ID + " = ?");
|
||||
|
||||
@ -301,7 +321,7 @@ public class MainActivity extends Activity {
|
||||
|
||||
++articlesFound;
|
||||
|
||||
Cursor c = db.query("articles", new String[] { BaseColumns._ID } , BaseColumns._ID + "=?",
|
||||
Cursor c = getReadableDb().query("articles", new String[] { BaseColumns._ID } , BaseColumns._ID + "=?",
|
||||
new String[] { String.valueOf(article.id) }, null, null, null);
|
||||
|
||||
String excerpt = Jsoup.parse(article.content).text();
|
||||
@ -346,8 +366,6 @@ public class MainActivity extends Activity {
|
||||
c.close();
|
||||
}
|
||||
|
||||
db.close();
|
||||
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
@ -435,19 +453,16 @@ public class MainActivity extends Activity {
|
||||
Type listType = new TypeToken<List<Feed>>() {}.getType();
|
||||
List<Feed> feeds = api.m_gson.fromJson(feeds_object, listType);
|
||||
|
||||
DatabaseHelper dh = new DatabaseHelper(this);
|
||||
SQLiteDatabase db = dh.getWritableDatabase();
|
||||
|
||||
SQLiteStatement stmtUpdate = db.compileStatement("UPDATE feeds SET " +
|
||||
SQLiteStatement stmtUpdate = getWritableDb().compileStatement("UPDATE feeds SET " +
|
||||
"title = ?, feed_url = ?, has_icon = ?, cat_id = ?, last_updated = ? WHERE " +
|
||||
BaseColumns._ID + " = ?");
|
||||
|
||||
SQLiteStatement stmtInsert = db.compileStatement("INSERT INTO feeds " +
|
||||
SQLiteStatement stmtInsert = getWritableDb().compileStatement("INSERT INTO feeds " +
|
||||
"("+BaseColumns._ID+", title, feed_url, has_icon, cat_id, last_updated) " +
|
||||
"VALUES (?, ?, ?, ?, ?, ?);");
|
||||
|
||||
for (Feed feed : feeds) {
|
||||
Cursor c = db.query("feeds", new String[] { BaseColumns._ID } , BaseColumns._ID + "=?",
|
||||
Cursor c = getReadableDb().query("feeds", new String[] { BaseColumns._ID } , BaseColumns._ID + "=?",
|
||||
new String[] { String.valueOf(feed.id) }, null, null, null);
|
||||
|
||||
if (c.getCount() != 0) {
|
||||
@ -474,8 +489,6 @@ public class MainActivity extends Activity {
|
||||
|
||||
// TODO delete not returned feeds which has no data here
|
||||
|
||||
db.close();
|
||||
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
|
Loading…
Reference in New Issue
Block a user