basic offline feed download
This commit is contained in:
parent
0f9001c88f
commit
c9ce8b60a3
@ -22,6 +22,12 @@
|
|||||||
android:showAsAction=""
|
android:showAsAction=""
|
||||||
android:title="@string/update_feeds"/>
|
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
|
<item
|
||||||
android:id="@+id/logout"
|
android:id="@+id/logout"
|
||||||
android:icon="@drawable/ic_menu_exit"
|
android:icon="@drawable/ic_menu_exit"
|
||||||
@ -61,7 +67,7 @@
|
|||||||
android:icon="@drawable/ic_menu_rss"
|
android:icon="@drawable/ic_menu_rss"
|
||||||
android:showAsAction=""
|
android:showAsAction=""
|
||||||
android:title="@string/selection_toggle_published"/>
|
android:title="@string/selection_toggle_published"/>
|
||||||
|
|
||||||
<item
|
<item
|
||||||
android:id="@+id/selection_select_none"
|
android:id="@+id/selection_select_none"
|
||||||
android:icon="@android:drawable/ic_menu_close_clear_cancel"
|
android:icon="@android:drawable/ic_menu_close_clear_cancel"
|
||||||
@ -100,11 +106,11 @@
|
|||||||
android:icon="@android:drawable/ic_menu_close_clear_cancel"
|
android:icon="@android:drawable/ic_menu_close_clear_cancel"
|
||||||
android:showAsAction="ifRoom"
|
android:showAsAction="ifRoom"
|
||||||
android:title="@string/close_article"/>
|
android:title="@string/close_article"/>
|
||||||
|
|
||||||
<item
|
<item
|
||||||
android:id="@+id/catchup_above"
|
android:id="@+id/catchup_above"
|
||||||
android:icon="@drawable/ic_menu_tick"
|
android:icon="@drawable/ic_menu_tick"
|
||||||
android:title="@string/article_mark_read_above"/>
|
android:title="@string/article_mark_read_above"/>
|
||||||
</group>
|
</group>
|
||||||
</group>
|
</group>
|
||||||
|
|
||||||
|
@ -90,5 +90,5 @@
|
|||||||
<string name="error_invalid_api_url">Error: invalid API URL</string>
|
<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_summary">Displays full article text inline, instead of a separate panel</string>
|
||||||
<string name="combined_mode">Combined mode</string>
|
<string name="combined_mode">Combined mode</string>
|
||||||
|
<string name="initiate_sync">Sync offline</string>
|
||||||
</resources>
|
</resources>
|
@ -26,7 +26,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
|
|||||||
"feed_url TEXT, " +
|
"feed_url TEXT, " +
|
||||||
"title TEXT, " +
|
"title TEXT, " +
|
||||||
"has_icon BOOLEAN, " +
|
"has_icon BOOLEAN, " +
|
||||||
"cat_id INTEGER, " +
|
"cat_id INTEGER" +
|
||||||
");");
|
");");
|
||||||
|
|
||||||
db.execSQL("CREATE TABLE IF NOT EXISTS articles (" +
|
db.execSQL("CREATE TABLE IF NOT EXISTS articles (" +
|
||||||
@ -40,7 +40,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
|
|||||||
"link TEXT, " +
|
"link TEXT, " +
|
||||||
"feed_id INTEGER, " +
|
"feed_id INTEGER, " +
|
||||||
"tags TEXT, " +
|
"tags TEXT, " +
|
||||||
"content TEXT, " +
|
"content TEXT" +
|
||||||
");");
|
");");
|
||||||
|
|
||||||
db.execSQL("CREATE VIEW feeds_unread AS SELECT feeds."+BaseColumns._ID+" AS "+BaseColumns._ID+", " +
|
db.execSQL("CREATE VIEW feeds_unread AS SELECT feeds."+BaseColumns._ID+" AS "+BaseColumns._ID+", " +
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package org.fox.ttrss;
|
package org.fox.ttrss;
|
||||||
|
|
||||||
|
import java.lang.reflect.Type;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Timer;
|
import java.util.Timer;
|
||||||
@ -12,10 +13,13 @@ import android.content.DialogInterface;
|
|||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.content.pm.PackageInfo;
|
import android.content.pm.PackageInfo;
|
||||||
|
import android.database.sqlite.SQLiteDatabase;
|
||||||
|
import android.database.sqlite.SQLiteStatement;
|
||||||
import android.net.ConnectivityManager;
|
import android.net.ConnectivityManager;
|
||||||
import android.net.NetworkInfo;
|
import android.net.NetworkInfo;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
|
import android.provider.BaseColumns;
|
||||||
import android.support.v4.app.FragmentActivity;
|
import android.support.v4.app.FragmentActivity;
|
||||||
import android.support.v4.app.FragmentTransaction;
|
import android.support.v4.app.FragmentTransaction;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
@ -30,8 +34,10 @@ import android.widget.AdapterView.AdapterContextMenuInfo;
|
|||||||
import android.widget.LinearLayout;
|
import android.widget.LinearLayout;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
|
import com.google.gson.reflect.TypeToken;
|
||||||
|
|
||||||
public class MainActivity extends FragmentActivity implements FeedsFragment.OnFeedSelectedListener, ArticleOps, FeedCategoriesFragment.OnCatSelectedListener {
|
public class MainActivity extends FragmentActivity implements FeedsFragment.OnFeedSelectedListener, ArticleOps, FeedCategoriesFragment.OnCatSelectedListener {
|
||||||
private final String TAG = this.getClass().getSimpleName();
|
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_isLicensed = -1;
|
||||||
private int m_apiLevel = 0;
|
private int m_apiLevel = 0;
|
||||||
|
|
||||||
|
private SQLiteDatabase m_readableDb;
|
||||||
|
private SQLiteDatabase m_writableDb;
|
||||||
|
|
||||||
public void updateHeadlines() {
|
public void updateHeadlines() {
|
||||||
HeadlinesFragment frag = (HeadlinesFragment)getSupportFragmentManager().findFragmentById(R.id.headlines_fragment);
|
HeadlinesFragment frag = (HeadlinesFragment)getSupportFragmentManager().findFragmentById(R.id.headlines_fragment);
|
||||||
if (frag != null) {
|
if (frag != null) {
|
||||||
@ -377,14 +386,85 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
initDatabase();
|
||||||
|
|
||||||
if (m_sessionId != null) {
|
if (m_sessionId != null) {
|
||||||
loginSuccess();
|
loginSuccess();
|
||||||
} else {
|
} else {
|
||||||
login();
|
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) {
|
public void setLoadingStatus(int status, boolean showProgress) {
|
||||||
TextView tv = (TextView)findViewById(R.id.loading_message);
|
TextView tv = (TextView)findViewById(R.id.loading_message);
|
||||||
|
|
||||||
@ -558,6 +638,9 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe
|
|||||||
case R.id.login:
|
case R.id.login:
|
||||||
login();
|
login();
|
||||||
return true;
|
return true;
|
||||||
|
case R.id.initiate_sync:
|
||||||
|
offlineSync();
|
||||||
|
return true;
|
||||||
case R.id.close_article:
|
case R.id.close_article:
|
||||||
closeArticle();
|
closeArticle();
|
||||||
return true;
|
return true;
|
||||||
|
Loading…
Reference in New Issue
Block a user