From 3063d86667f1738d600308b152092ac494538336 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Fri, 22 Jun 2012 13:24:33 +0400 Subject: [PATCH] add DB stuff for offline category sync --- .../ttrss/offline/OfflineDownloadService.java | 64 +++++++++++++++++++ .../offline/OfflineHeadlinesFragment.java | 3 + src/org/fox/ttrss/util/DatabaseHelper.java | 8 ++- 3 files changed, 74 insertions(+), 1 deletion(-) diff --git a/src/org/fox/ttrss/offline/OfflineDownloadService.java b/src/org/fox/ttrss/offline/OfflineDownloadService.java index bb7d0554..98c8ce8a 100644 --- a/src/org/fox/ttrss/offline/OfflineDownloadService.java +++ b/src/org/fox/ttrss/offline/OfflineDownloadService.java @@ -11,6 +11,7 @@ import org.fox.ttrss.R.drawable; import org.fox.ttrss.R.string; import org.fox.ttrss.types.Article; import org.fox.ttrss.types.Feed; +import org.fox.ttrss.types.FeedCategory; import org.fox.ttrss.util.DatabaseHelper; import org.fox.ttrss.util.ImageCacheService; import org.jsoup.Jsoup; @@ -262,6 +263,69 @@ public class OfflineDownloadService extends Service { req.execute(map); } + + private void downloadCategories() { + + updateNotification(R.string.notify_downloading_feeds); + + getWritableDb().execSQL("DELETE FROM categories;"); + + ApiRequest req = new ApiRequest(getApplicationContext()) { + @Override + protected void onPostExecute(JsonElement content) { + if (content != null) { + + try { + Type listType = new TypeToken>() {}.getType(); + List cats = new Gson().fromJson(content, listType); + + SQLiteStatement stmtInsert = getWritableDb().compileStatement("INSERT INTO categories " + + "("+BaseColumns._ID+", title) " + + "VALUES (?, ?);"); + + for (FeedCategory cat : cats) { + stmtInsert.bindLong(1, cat.id); + stmtInsert.bindString(2, cat.title); + + stmtInsert.execute(); + } + + stmtInsert.close(); + + Log.d(TAG, "offline: done downloading categories"); + + if (m_canProceed) { + downloadFeeds(); + } else { + downloadFailed(); + } + } catch (Exception e) { + e.printStackTrace(); + updateNotification(R.string.offline_switch_error); + downloadFailed(); + } + + } else { + updateNotification(getErrorMessage()); + downloadFailed(); + } + } + + }; + + @SuppressWarnings("serial") + HashMap map = new HashMap() { + { + put("op", "getFeeds"); + put("sid", m_sessionId); + put("cat_id", "-3"); + put("unread_only", "true"); + } + }; + + req.execute(map); + } + @Override public void onDestroy() { diff --git a/src/org/fox/ttrss/offline/OfflineHeadlinesFragment.java b/src/org/fox/ttrss/offline/OfflineHeadlinesFragment.java index 46dd1e8a..b3727535 100644 --- a/src/org/fox/ttrss/offline/OfflineHeadlinesFragment.java +++ b/src/org/fox/ttrss/offline/OfflineHeadlinesFragment.java @@ -151,6 +151,9 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis list.setEmptyView(view.findViewById(R.id.no_headlines)); registerForContextMenu(list); + if (m_offlineServices.isSmallScreen()) + view.findViewById(R.id.headlines_fragment).setPadding(0, 0, 0, 0); + getActivity().setProgressBarIndeterminateVisibility(false); return view; diff --git a/src/org/fox/ttrss/util/DatabaseHelper.java b/src/org/fox/ttrss/util/DatabaseHelper.java index b8560589..345e0782 100644 --- a/src/org/fox/ttrss/util/DatabaseHelper.java +++ b/src/org/fox/ttrss/util/DatabaseHelper.java @@ -10,7 +10,7 @@ public class DatabaseHelper extends SQLiteOpenHelper { @SuppressWarnings("unused") private final String TAG = this.getClass().getSimpleName(); public static final String DATABASE_NAME = "OfflineStorage.db"; - public static final int DATABASE_VERSION = 2; + public static final int DATABASE_VERSION = 3; public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); @@ -18,6 +18,7 @@ public class DatabaseHelper extends SQLiteOpenHelper { @Override public void onCreate(SQLiteDatabase db) { + db.execSQL("DROP TABLE IF EXISTS categories;"); db.execSQL("DROP TABLE IF EXISTS feeds;"); db.execSQL("DROP TABLE IF EXISTS articles;"); db.execSQL("DROP VIEW IF EXISTS feeds_unread;"); @@ -31,6 +32,11 @@ public class DatabaseHelper extends SQLiteOpenHelper { "cat_id INTEGER" + ");"); + db.execSQL("CREATE TABLE IF NOT EXISTS categories (" + + BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + + "title TEXT" + + ");"); + db.execSQL("CREATE TABLE IF NOT EXISTS articles (" + BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + "unread BOOLEAN, " +