From c9ce8b60a3ee61fcd32da8bde4188e33902ba658 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Mon, 5 Dec 2011 16:01:44 +0300 Subject: [PATCH] basic offline feed download --- res/menu/main_menu.xml | 18 ++++-- res/values/strings.xml | 2 +- src/org/fox/ttrss/DatabaseHelper.java | 4 +- src/org/fox/ttrss/MainActivity.java | 83 +++++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 9 deletions(-) diff --git a/res/menu/main_menu.xml b/res/menu/main_menu.xml index 0d59ecef..ab25e60c 100644 --- a/res/menu/main_menu.xml +++ b/res/menu/main_menu.xml @@ -22,6 +22,12 @@ android:showAsAction="" android:title="@string/update_feeds"/> + + - + - - + + diff --git a/res/values/strings.xml b/res/values/strings.xml index 7a1a6012..d71db759 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -90,5 +90,5 @@ Error: invalid API URL Displays full article text inline, instead of a separate panel Combined mode - + Sync offline \ No newline at end of file diff --git a/src/org/fox/ttrss/DatabaseHelper.java b/src/org/fox/ttrss/DatabaseHelper.java index d9173f2c..42e146b8 100644 --- a/src/org/fox/ttrss/DatabaseHelper.java +++ b/src/org/fox/ttrss/DatabaseHelper.java @@ -26,7 +26,7 @@ public class DatabaseHelper extends SQLiteOpenHelper { "feed_url TEXT, " + "title TEXT, " + "has_icon BOOLEAN, " + - "cat_id INTEGER, " + + "cat_id INTEGER" + ");"); db.execSQL("CREATE TABLE IF NOT EXISTS articles (" + @@ -40,7 +40,7 @@ public class DatabaseHelper extends SQLiteOpenHelper { "link TEXT, " + "feed_id INTEGER, " + "tags TEXT, " + - "content TEXT, " + + "content TEXT" + ");"); db.execSQL("CREATE VIEW feeds_unread AS SELECT feeds."+BaseColumns._ID+" AS "+BaseColumns._ID+", " + diff --git a/src/org/fox/ttrss/MainActivity.java b/src/org/fox/ttrss/MainActivity.java index 7a2c3453..011d379e 100644 --- a/src/org/fox/ttrss/MainActivity.java +++ b/src/org/fox/ttrss/MainActivity.java @@ -1,5 +1,6 @@ package org.fox.ttrss; +import java.lang.reflect.Type; import java.util.HashMap; import java.util.List; import java.util.Timer; @@ -12,10 +13,13 @@ import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageInfo; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteStatement; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Bundle; import android.preference.PreferenceManager; +import android.provider.BaseColumns; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTransaction; import android.util.Log; @@ -30,8 +34,10 @@ import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.LinearLayout; import android.widget.TextView; +import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import com.google.gson.reflect.TypeToken; public class MainActivity extends FragmentActivity implements FeedsFragment.OnFeedSelectedListener, ArticleOps, FeedCategoriesFragment.OnCatSelectedListener { private final String TAG = this.getClass().getSimpleName(); @@ -53,6 +59,9 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe private int m_isLicensed = -1; private int m_apiLevel = 0; + private SQLiteDatabase m_readableDb; + private SQLiteDatabase m_writableDb; + public void updateHeadlines() { HeadlinesFragment frag = (HeadlinesFragment)getSupportFragmentManager().findFragmentById(R.id.headlines_fragment); if (frag != null) { @@ -377,14 +386,85 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe } + initDatabase(); + if (m_sessionId != null) { loginSuccess(); } else { login(); } + } + public void initDatabase() { + DatabaseHelper dh = new DatabaseHelper(getApplicationContext()); + m_writableDb = dh.getWritableDatabase(); + m_readableDb = dh.getReadableDatabase(); + } + + public synchronized SQLiteDatabase getWritableDb() { + return m_writableDb; + } + + @SuppressWarnings("unchecked") + public void offlineSync() { + Log.d(TAG, "Starting offlineSync"); + + if (m_sessionId != null) { + + // TODO upload updated stuff here + + // Download feeds + + getWritableDb().execSQL("DELETE FROM feeds;"); + + ApiRequest req = new ApiRequest(getApplicationContext()) { + @Override + protected void onPostExecute(JsonElement content) { + if (content != null) { + + Type listType = new TypeToken>() {}.getType(); + List feeds = new Gson().fromJson(content, listType); + + SQLiteStatement stmtInsert = getWritableDb().compileStatement("INSERT INTO feeds " + + "("+BaseColumns._ID+", title, feed_url, has_icon, cat_id) " + + "VALUES (?, ?, ?, ?, ?);"); + + for (Feed feed : feeds) { + stmtInsert.bindLong(1, feed.id); + stmtInsert.bindString(2, feed.title); + stmtInsert.bindString(3, feed.feed_url); + stmtInsert.bindLong(4, feed.has_icon ? 1 : 0); + stmtInsert.bindLong(5, feed.cat_id); + + stmtInsert.execute(); + } + + Log.d(TAG, "offlineSync: done downloading feeds"); + + // TODO download fresh articles + + } else { + Log.d(TAG, "offlineSync failed: " + getErrorMessage()); + // TODO error, could not download feeds, properly report API error (toast) + } + } + }; + + HashMap map = new HashMap() { + { + put("op", "getFeeds"); + put("sid", m_sessionId); + put("cat_id", "-3"); + put("unread_only", "true"); + } + }; + + req.execute(map); + } + } + public void setLoadingStatus(int status, boolean showProgress) { TextView tv = (TextView)findViewById(R.id.loading_message); @@ -558,6 +638,9 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe case R.id.login: login(); return true; + case R.id.initiate_sync: + offlineSync(); + return true; case R.id.close_article: closeArticle(); return true;