add placeholder code for downloading articles

This commit is contained in:
Andrew Dolgov 2011-09-09 10:02:31 +04:00
parent 261c7cfbe6
commit 1668e69307
5 changed files with 279 additions and 52 deletions

View File

@ -0,0 +1,17 @@
package org.fox.ttrss;
import java.util.List;
public class Article {
int id;
boolean unread;
boolean marked;
boolean published;
int updated;
boolean is_updated;
String title;
String link;
int feed_id;
List<String> tags;
String content;
}

View File

@ -10,14 +10,16 @@ public class DatabaseHelper extends SQLiteOpenHelper {
private final String TAG = this.getClass().getSimpleName();
public static final String DATABASE_NAME = "LocalStorage";
public static final int DATABASE_VERSION = 4;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.d(TAG, "onCreate");
db.execSQL("DROP TABLE IF EXISTS feeds;");
db.execSQL("DROP TABLE IF EXISTS articles;");
db.execSQL("CREATE TABLE IF NOT EXISTS feeds (" +
BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
@ -26,14 +28,28 @@ public class DatabaseHelper extends SQLiteOpenHelper {
"unread INTEGER, " +
"has_icon BOOLEAN, " +
"cat_id INTEGER, " +
"last_updated INTEGER)");
"last_updated INTEGER, " +
"count INTEGER" +
");");
db.execSQL("CREATE TABLE IF NOT EXISTS articles (" +
BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
"unread BOOLEAN, " +
"marked BOOLEAN, " +
"published BOOLEAN, " +
"updated INTEGER, " +
"is_updated BOOLEAN, " +
"title TEXT, " +
"link TEXT, " +
"feed_id INTEGER, " +
"tags TEXT, " +
"content TEXT" +
");");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
onCreate(db);
}
}

View File

@ -3,6 +3,8 @@ package org.fox.ttrss;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.app.Fragment;
@ -36,18 +38,29 @@ public class FeedsFragment extends Fragment implements OnItemClickListener {
// protected ArrayList<Feed> m_feeds = new ArrayList<Feed>();
protected FeedsListAdapter m_adapter;
protected SharedPreferences m_prefs;
protected String m_sessionId;
protected int m_activeFeedId;
protected long m_lastUpdate;
protected Gson m_gson = new Gson();
protected Cursor m_cursor;
private Timer m_timer;
private TimerTask m_updateTask = new TimerTask() {
@Override
public void run() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
updateSelf();
}
});
}
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
m_sessionId = m_prefs.getString("last_session_id", null);
if (savedInstanceState != null) {
m_sessionId = savedInstanceState.getString("sessionId");
m_activeFeedId = savedInstanceState.getInt("activeFeedId");
m_lastUpdate = savedInstanceState.getLong("lastUpdate");
}
@ -55,9 +68,9 @@ public class FeedsFragment extends Fragment implements OnItemClickListener {
View view = inflater.inflate(R.layout.feeds_fragment, container, false);
DatabaseHelper helper = new DatabaseHelper(getActivity());
Cursor cursor = helper.getReadableDatabase().query("feeds", null, null, null, null, null, "unread DESC");
m_cursor = helper.getReadableDatabase().query("feeds", null, null, null, null, null, "unread DESC");
m_adapter = new FeedsListAdapter(getActivity(), R.layout.feeds_row, cursor,
m_adapter = new FeedsListAdapter(getActivity(), R.layout.feeds_row, m_cursor,
new String[] { "title", "unread" }, new int[] { R.id.title, R.id.unread_counter }, 0);
ListView list = (ListView) view.findViewById(R.id.feeds);
@ -68,31 +81,32 @@ public class FeedsFragment extends Fragment implements OnItemClickListener {
list.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
}
updateSelf();
//updateSelf();
m_timer = new Timer("UpdateFeeds");
m_timer.schedule(m_updateTask, 1000L, 60*1000L);
return view;
}
protected void updateSessionId(String sessionId) {
m_sessionId = sessionId;
@Override
public void onDestroy() {
super.onDestroy();
SharedPreferences.Editor editor = m_prefs.edit();
editor.putString("last_session_id", m_sessionId);
editor.commit();
m_timer.cancel();
m_timer = null;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
}
public void initialize(String sessionId) {
m_sessionId = sessionId;
}
private void updateSelf() {
ApiRequest task = new ApiRequest(m_sessionId,
String sessionId = ((MainActivity)getActivity()).getSessionId();
ApiRequest task = new ApiRequest(sessionId,
m_prefs.getString("ttrss_url", null),
m_prefs.getString("login", null),
m_prefs.getString("password", null)) {
@ -100,6 +114,8 @@ public class FeedsFragment extends Fragment implements OnItemClickListener {
protected void onPostExecute(JsonElement result) {
if (result != null && getAuthStatus() == STATUS_OK) {
try {
((MainActivity)getActivity()).setSessionId(getSessionId());
JsonArray feeds_object = (JsonArray) result.getAsJsonArray();
Type listType = new TypeToken<List<Feed>>() {}.getType();
@ -108,26 +124,51 @@ public class FeedsFragment extends Fragment implements OnItemClickListener {
DatabaseHelper dh = new DatabaseHelper(getActivity());
SQLiteDatabase db = dh.getWritableDatabase();
db.execSQL("DELETE FROM FEEDS");
SQLiteStatement stmt = db.compileStatement("INSERT INTO feeds " +
"("+BaseColumns._ID+", title, feed_url, unread, has_icon, cat_id, last_updated) " +
"VALUES (?, ?, ?, ?, ?, ?, ?);");
/* db.execSQL("DELETE FROM FEEDS"); */
/* SQLiteStatement stmFind = db.compileStatement("SELECT "+BaseColumns._ID+" FROM feeds WHERE "+
BaseColumns._ID+" = ?"); */
SQLiteStatement stmtUpdate = db.compileStatement("UPDATE feeds SET " +
"title = ?, feed_url = ?, has_icon = ?, cat_id = ?, last_updated = ? WHERE " +
BaseColumns._ID + " = ?");
SQLiteStatement stmtInsert = db.compileStatement("INSERT INTO feeds " +
"("+BaseColumns._ID+", title, feed_url, has_icon, cat_id, last_updated) " +
"VALUES (?, ?, ?, ?, ?, ?);");
for (Feed feed : feeds) {
stmt.bindLong(1, feed.id);
stmt.bindString(2, feed.title);
stmt.bindString(3, feed.feed_url);
stmt.bindLong(4, feed.unread);
stmt.bindLong(5, 1);
stmt.bindLong(6, feed.cat_id);
stmt.bindLong(7, feed.last_updated);
stmt.execute();
}
Cursor c = db.query("feeds", new String[] { BaseColumns._ID } , BaseColumns._ID + "=?",
new String[] { String.valueOf(feed.id) }, null, null, null);
if (c.getCount() != 0) {
stmtUpdate.bindString(1, feed.title);
stmtUpdate.bindString(2, feed.feed_url);
stmtUpdate.bindLong(3, feed.has_icon ? 1 : 0);
stmtUpdate.bindLong(4, feed.cat_id);
stmtUpdate.bindLong(5, feed.last_updated);
stmtUpdate.bindLong(6, feed.id);
stmtUpdate.execute();
} else {
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.bindLong(6, feed.last_updated);
stmtInsert.execute();
}
c.close();
}
// TODO delete not returned feeds which has no data here
db.close();
m_adapter.notifyDataSetChanged();
m_cursor.requery();
m_adapter.notifyDataSetChanged();
} catch (Exception e) {
e.printStackTrace();
@ -139,20 +180,19 @@ public class FeedsFragment extends Fragment implements OnItemClickListener {
task.execute(new HashMap<String,String>() {
{
put("sid", m_sessionId);
put("sid", ((MainActivity)getActivity()).getSessionId());
put("op", "getFeeds");
put("cat_id", "-3");
put("unread_only", "true");
}
});
}
}
@Override
public void onSaveInstanceState (Bundle out) {
super.onSaveInstanceState(out);
out.putString("sessionId", m_sessionId);
out.putInt("activeFeedId", m_activeFeedId);
out.putLong("lastUpdate", m_lastUpdate);
}
@ -168,8 +208,7 @@ public class FeedsFragment extends Fragment implements OnItemClickListener {
this.context = context;
this.layout = layout;
}
}
}
@ -196,7 +235,7 @@ public class FeedsFragment extends Fragment implements OnItemClickListener {
FragmentTransaction ft = getFragmentManager().beginTransaction();
HeadlinesFragment frag = new HeadlinesFragment();
frag.initialize(m_sessionId, feedId);
frag.initialize(feedId);
ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
ft.replace(R.id.headlines_container, frag);

View File

@ -11,17 +11,12 @@ import android.view.ViewGroup;
public class HeadlinesFragment extends Fragment {
private final String TAG = this.getClass().getSimpleName();
protected String m_sessionId;
protected int m_feedId;
protected SharedPreferences m_prefs;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (savedInstanceState != null) {
m_sessionId = savedInstanceState.getString("sessionId");
}
View view = inflater.inflate(R.layout.headlines_fragment, container, false);
/* m_adapter = new FeedsListAdapter(getActivity(), R.id.feeds_row, m_feeds);
@ -43,8 +38,7 @@ public class HeadlinesFragment extends Fragment {
m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
}
public void initialize(String sessionId, int feedId) {
m_sessionId = sessionId;
public void initialize(int feedId) {
m_feedId = feedId;
}

View File

@ -1,29 +1,79 @@
package org.fox.ttrss;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.BaseColumns;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
public class MainActivity extends Activity {
private final String TAG = this.getClass().getSimpleName();
private SharedPreferences m_prefs;
private String m_themeName = "";
private boolean m_feedsOpened = false;
protected String m_sessionId;
protected int m_offset = 0;
protected int m_limit = 100;
protected String getSessionId() {
return m_sessionId;
}
protected synchronized void setSessionId(String sessionId) {
m_sessionId = sessionId;
SharedPreferences.Editor editor = m_prefs.edit();
editor.putString("last_session_id", m_sessionId);
editor.commit();
}
private Timer m_timer;
private TimerTask m_updateTask = new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
downloadArticles();
}
});
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// allow database to upgrade before we do anything else
DatabaseHelper dh = new DatabaseHelper(getApplicationContext());
SQLiteDatabase db = dh.getWritableDatabase();
db.execSQL("DELETE FROM feeds;");
db.execSQL("DELETE FROM articles;");
db.close();
m_prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
if (m_prefs.getString("theme", "THEME_DARK").equals("THEME_DARK")) {
@ -34,8 +84,11 @@ public class MainActivity extends Activity {
m_themeName = m_prefs.getString("theme", "THEME_DARK");
m_sessionId = m_prefs.getString("last_session_id", null);
if (savedInstanceState != null) {
m_feedsOpened = savedInstanceState.getBoolean("feedsOpened");
m_sessionId = savedInstanceState.getString("sessionId");
}
setContentView(R.layout.main);
@ -53,6 +106,8 @@ public class MainActivity extends Activity {
m_feedsOpened = true;
}
m_timer = new Timer("UpdateArticles");
m_timer.schedule(m_updateTask, 1000L, 5*1000L);
}
@Override
@ -60,6 +115,7 @@ public class MainActivity extends Activity {
super.onSaveInstanceState(out);
out.putBoolean("feedsOpened", m_feedsOpened);
out.putString("sessionId", m_sessionId);
}
@Override
@ -73,6 +129,14 @@ public class MainActivity extends Activity {
}
}
@Override
public void onDestroy() {
super.onDestroy();
m_timer.cancel();
m_timer = null;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
@ -92,4 +156,101 @@ public class MainActivity extends Activity {
}
}
private void downloadArticles() {
ApiRequest task = new ApiRequest(m_sessionId,
m_prefs.getString("ttrss_url", null),
m_prefs.getString("login", null),
m_prefs.getString("password", null)) {
@Override
protected void onPostExecute(JsonElement result) {
if (result != null && getAuthStatus() == STATUS_OK) {
try {
setSessionId(getSessionId());
JsonArray feeds_object = (JsonArray) result.getAsJsonArray();
Type listType = new TypeToken<List<Article>>() {}.getType();
List<Article> articles = m_gson.fromJson(feeds_object, listType);
DatabaseHelper dh = new DatabaseHelper(getApplicationContext());
SQLiteDatabase db = dh.getWritableDatabase();
/* db.execSQL("DELETE FROM articles"); */
SQLiteStatement stmtInsert = db.compileStatement("INSERT INTO articles " +
"("+BaseColumns._ID+", unread, marked, published, updated, is_updated, title, link, feed_id, tags, content) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
SQLiteStatement stmtUpdate = db.compileStatement("UPDATE articles SET " +
"unread = ?, marked = ?, published = ?, updated = ?, is_updated = ?, title = ?, link = ?, feed_id = ?, " +
"tags = ?, content = ? WHERE " + BaseColumns._ID + " = ?");
int articlesFound = 0;
for (Article article : articles) {
//Log.d(TAG, "Processing article #" + article.id);
++articlesFound;
Cursor c = db.query("articles", new String[] { BaseColumns._ID } , BaseColumns._ID + "=?",
new String[] { String.valueOf(article.id) }, null, null, null);
if (c.getCount() != 0) {
//Log.d(TAG, "article found");
} else {
//Log.d(TAG, "article not found");
stmtInsert.bindLong(1, article.id);
stmtInsert.bindLong(2, article.unread ? 1 : 0);
stmtInsert.bindLong(3, article.marked ? 1 : 0);
stmtInsert.bindLong(4, article.published ? 1 : 0);
stmtInsert.bindLong(5, article.updated);
stmtInsert.bindLong(6, article.is_updated ? 1 : 0);
stmtInsert.bindString(7, article.title);
stmtInsert.bindString(8, article.link);
stmtInsert.bindLong(9, article.feed_id);
stmtInsert.bindString(9, ""); // comma-separated tags
stmtInsert.bindString(10, article.content);
stmtInsert.execute();
}
c.close();
}
db.close();
Log.d(TAG, articlesFound + " articles processed");
if (articlesFound == m_limit && m_offset <= 300) {
m_offset += m_limit;
} else {
m_offset = 0;
m_timer.cancel();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
Log.d(TAG, "Requesting articles [offset=" + m_offset + "]");
task.execute(new HashMap<String,String>() {
{
put("sid", m_sessionId);
put("op", "getHeadlines");
put("feed_id", "-4");
put("show_content", "1");
put("limit", String.valueOf(m_limit));
put("offset", String.valueOf(m_offset));
put("view_mode", "unread");
}
});
}
}