basic offline feed download

This commit is contained in:
Andrew Dolgov 2011-12-05 16:01:44 +03:00
parent 0f9001c88f
commit c9ce8b60a3
4 changed files with 98 additions and 9 deletions

View File

@ -22,6 +22,12 @@
android:showAsAction=""
android:title="@string/update_feeds"/>
<item
android:id="@+id/initiate_sync"
android:icon="@android:drawable/ic_menu_rotate"
android:showAsAction="ifRoom|withText"
android:title="@string/initiate_sync"/>
<item
android:id="@+id/logout"
android:icon="@drawable/ic_menu_exit"
@ -61,7 +67,7 @@
android:icon="@drawable/ic_menu_rss"
android:showAsAction=""
android:title="@string/selection_toggle_published"/>
<item
android:id="@+id/selection_select_none"
android:icon="@android:drawable/ic_menu_close_clear_cancel"
@ -100,11 +106,11 @@
android:icon="@android:drawable/ic_menu_close_clear_cancel"
android:showAsAction="ifRoom"
android:title="@string/close_article"/>
<item
android:id="@+id/catchup_above"
android:icon="@drawable/ic_menu_tick"
android:title="@string/article_mark_read_above"/>
<item
android:id="@+id/catchup_above"
android:icon="@drawable/ic_menu_tick"
android:title="@string/article_mark_read_above"/>
</group>
</group>

View File

@ -90,5 +90,5 @@
<string name="error_invalid_api_url">Error: invalid API URL</string>
<string name="combined_mode_summary">Displays full article text inline, instead of a separate panel</string>
<string name="combined_mode">Combined mode</string>
<string name="initiate_sync">Sync offline</string>
</resources>

View File

@ -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+", " +

View File

@ -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<List<Feed>>() {}.getType();
List<Feed> 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<String,String> map = new HashMap<String,String>() {
{
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;