implement webview for content

This commit is contained in:
Andrew Dolgov 2011-09-09 20:36:09 +04:00
parent fb7054eb18
commit afc8996fff
10 changed files with 434 additions and 388 deletions

View File

@ -1,7 +1,8 @@
<LinearLayout android:id="@+id/article_fragment" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView android:paddingRight="2dip" android:layout_height="match_parent" android:background="?feedlistDivider" android:layout_width="wrap_content" android:paddingLeft="2dip"></ImageView>
<LinearLayout android:gravity="center" android:layout_height="match_parent" android:id="@+id/linearLayout1" android:layout_width="match_parent">
<TextView android:text="--- Article ---" android:id="@+id/textView1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge"></TextView>
<LinearLayout android:padding="6dip" android:id="@+id/article_fragment" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical">
<LinearLayout android:layout_margin="12dip" android:padding="6dip" android:background="#e0e0e0" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:id="@+id/article_header">
<TextView android:layout_weight="1" android:textAppearance="?android:attr/textAppearanceLarge" android:text="TextView" android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/title"></TextView>
<Button android:text="Close" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/close_button"></Button>
</LinearLayout>
<WebView android:layout_width="match_parent" android:id="@+id/content" android:layout_height="match_parent"></WebView>
</LinearLayout>

View File

@ -7,12 +7,9 @@
<ListView android:layout_weight="1"
android:layout_width="match_parent" android:id="@+id/feeds" android:layout_height="match_parent"></ListView>
</LinearLayout>
<ProgressBar android:layout_height="wrap_content"
android:layout_width="wrap_content" android:visibility="invisible"
android:layout_gravity="center" android:id="@+id/loading_progress"/>
<TextView android:id="@+id/no_unread_feeds"
android:visibility="invisible"
android:layout_gravity="center"
android:layout_gravity="center" android:color="#909090"
android:textAppearance="?android:attr/textAppearanceLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/no_unread_feeds"></TextView>
</FrameLayout>

View File

@ -4,8 +4,13 @@
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/headlines_fragment">
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<ListView android:layout_weight="1"
<ListView android:layout_weight="1" android:background="#fafafa"
android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/headlines"></ListView>
</LinearLayout>
<TextView android:id="@+id/no_headlines"
android:visibility="invisible"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/no_headlines"></TextView>
</FrameLayout>

View File

@ -19,19 +19,17 @@
android:layout_width="wrap_content" android:layout_height="match_parent"></ImageView>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/LinearLayout1" android:layout_weight="0.5">
<LinearLayout android:layout_width="match_parent" android:layout_weight="0" android:id="@+id/headlines_container" android:layout_height="match_parent"></LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_weight="1" android:background="#f0f0ff" android:id="@+id/article_container" android:layout_height="match_parent"></LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="0.5" android:id="@+id/headlines_wrapper">
<LinearLayout android:layout_width="match_parent" android:layout_weight="1" android:id="@+id/headlines_container" android:layout_height="match_parent">
</LinearLayout>
<ImageView android:paddingRight="2dip" android:layout_height="match_parent" android:background="?feedlistDivider" android:layout_width="wrap_content" android:paddingLeft="2dip"></ImageView>
<LinearLayout android:layout_weight="1" android:visibility="gone" android:layout_width="match_parent" android:background="#f0f0ff" android:id="@+id/article_container" android:layout_height="match_parent"></LinearLayout>
</LinearLayout>
</LinearLayout>
<TextView android:layout_gravity="center" android:visibility="invisible"
android:textAppearance="?android:attr/textAppearanceLarge" android:text="@string/no_unread_feeds" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/no_unread_feeds"></TextView>
<TextView android:id="@+id/no_headlines"
android:visibility="invisible"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/no_headlines"></TextView>
</FrameLayout>

View File

@ -4,15 +4,12 @@ import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.SortedMap;
import java.util.TreeMap;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import android.os.AsyncTask;
import android.util.Log;
import com.google.gson.Gson;
@ -20,7 +17,7 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
public class ApiRequest extends AsyncTask<HashMap<String,String>, Integer, JsonElement> {
public class ApiRequest {
private final String TAG = this.getClass().getSimpleName();
protected String m_sessionId;
@ -43,11 +40,11 @@ public class ApiRequest extends AsyncTask<HashMap<String,String>, Integer, JsonE
m_login = login;
m_password = password;
Log.d(TAG, "initial SID=" + sessionId);
//Log.d(TAG, "initial SID=" + sessionId);
}
protected int tryAuthenticate() {
JsonElement result = sendRequest(new HashMap<String,String>() {
JsonElement result = _sendRequest(new HashMap<String,String>() {
{
put("op", "login");
put("user", m_login);
@ -101,7 +98,7 @@ public class ApiRequest extends AsyncTask<HashMap<String,String>, Integer, JsonE
return m_authStatus;
}
protected JsonElement sendRequest(HashMap<String,String> param) {
protected JsonElement _sendRequest(HashMap<String,String> param) {
HashMap<String,String> tmp = new HashMap<String,String>(param);
@ -144,10 +141,9 @@ public class ApiRequest extends AsyncTask<HashMap<String,String>, Integer, JsonE
return null;
}
@Override
protected JsonElement doInBackground(HashMap<String, String>... params) {
public JsonElement sendRequest(HashMap<String, String> params) {
JsonElement result = sendRequest(params[0]);
JsonElement result = _sendRequest(params);
try {
JsonElement content = result.getAsJsonObject().get("content");
@ -163,7 +159,7 @@ public class ApiRequest extends AsyncTask<HashMap<String,String>, Integer, JsonE
m_authStatus = tryAuthenticate();
if (m_authStatus == STATUS_OK) {
result = sendRequest(params[0]);
result = _sendRequest(params);
return result.getAsJsonObject().get("content");
}

View File

@ -1,43 +1,83 @@
package org.fox.ttrss;
import java.util.Timer;
import org.fox.ttrss.FeedsFragment.FeedsListAdapter;
import android.app.Activity;
import android.app.Fragment;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.BaseColumns;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;
import android.webkit.WebView;
import android.widget.TextView;
public class ArticleFragment extends Fragment {
SharedPreferences m_prefs;
private final String TAG = this.getClass().getSimpleName();
protected SharedPreferences m_prefs;
protected int m_articleId;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (savedInstanceState != null) {
//
m_articleId = savedInstanceState.getInt("articleId");
}
View view = inflater.inflate(R.layout.article_fragment, container, false);
DatabaseHelper dh = new DatabaseHelper(getActivity());
SQLiteDatabase db = dh.getReadableDatabase();
Log.d(TAG, "Opening article #" + m_articleId);
Cursor c = db.query("articles", null, BaseColumns._ID + "=?",
new String[] { String.valueOf(m_articleId) }, null, null, null);
c.moveToFirst();
Log.d(TAG, "Cursor count: " + c.getCount());
TextView title = (TextView)view.findViewById(R.id.title);
if (title != null) {
title.setText(c.getString(c.getColumnIndex("title")));
}
WebView content = (WebView)view.findViewById(R.id.content);
if (content != null) {
String contentData = "<html><body>" + c.getString(c.getColumnIndex("content")) + "</body></html>";
Log.d(TAG, "content=" + contentData);
content.loadData(contentData, "text/html", "utf-8");
}
c.close();
db.close();
return view;
}
public void initialize(int articleId) {
m_articleId = articleId;
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onSaveInstanceState (Bundle out) {
public void onSaveInstanceState (Bundle out) {
super.onSaveInstanceState(out);
out.putInt("articleId", m_articleId);
}
@Override

View File

@ -3,7 +3,6 @@ import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.util.Log;
public class DatabaseHelper extends SQLiteOpenHelper {

View File

@ -1,11 +1,5 @@
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;
import android.app.FragmentTransaction;
@ -13,10 +7,8 @@ import android.content.Context;
import android.content.SharedPreferences;
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.LayoutInflater;
import android.view.View;
@ -27,54 +19,42 @@ import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
public class FeedsFragment extends Fragment implements OnItemClickListener {
private final String TAG = this.getClass().getSimpleName();
protected FeedsListAdapter m_adapter;
protected SharedPreferences m_prefs;
protected int m_activeFeedId;
protected Gson m_gson = new Gson();
protected Cursor m_cursor;
protected SQLiteDatabase m_db;
private Timer m_timer;
/* private Timer m_timer;
private TimerTask m_updateTask = new TimerTask() {
@Override
public void run() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
updateSelf();
}
});
downloadFeeds();
}
};
}; */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (savedInstanceState != null) {
m_activeFeedId = savedInstanceState.getInt("activeFeedId");
}
View view = inflater.inflate(R.layout.feeds_fragment, container, false);
DatabaseHelper helper = new DatabaseHelper(getActivity());
m_db = helper.getReadableDatabase();
m_cursor = m_db.query("feeds_unread", null, "unread > 0", null, null, null, "title");
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);
if (list != null) {
list.setAdapter(m_adapter);
list.setOnItemClickListener(this);
@ -82,13 +62,13 @@ public class FeedsFragment extends Fragment implements OnItemClickListener {
list.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
}
View pb = view.findViewById(R.id.loading_progress);
/* View pb = view.findViewById(R.id.loading_progress);
if (pb != null && m_cursor.getCount() == 0)
pb.setVisibility(View.VISIBLE);
m_timer = new Timer("UpdateFeeds");
m_timer.schedule(m_updateTask, 1000L, 60*1000L);
pb.setVisibility(View.VISIBLE); */
// m_timer = new Timer("UpdateFeeds");
// m_timer.schedule(m_updateTask, 1000L, 60*1000L);
return view;
}
@ -99,139 +79,51 @@ public class FeedsFragment extends Fragment implements OnItemClickListener {
m_cursor.close();
m_db.close();
m_timer.cancel();
m_timer = null;
// m_timer.cancel();
// m_timer = null;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
}
private void updateSelf() {
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)) {
@Override
protected void onPostExecute(JsonElement result) {
if (result != null && getAuthStatus() == STATUS_OK) {
try {
try {
((MainActivity)getActivity()).setSessionId(getSessionId());
} catch (NullPointerException e) {
//
}
JsonArray feeds_object = (JsonArray) result.getAsJsonArray();
Type listType = new TypeToken<List<Feed>>() {}.getType();
List<Feed> feeds = m_gson.fromJson(feeds_object, listType);
DatabaseHelper dh = new DatabaseHelper(getActivity());
SQLiteDatabase db = dh.getWritableDatabase();
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) {
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();
View pb = getView().findViewById(R.id.loading_progress);
if (pb != null) pb.setVisibility(View.INVISIBLE);
updateListView();
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
task.execute(new HashMap<String,String>() {
{
put("sid", ((MainActivity)getActivity()).getSessionId());
put("op", "getFeeds");
put("cat_id", "-3");
put("unread_only", "0");
}
});
}
@Override
@Override
public void onSaveInstanceState (Bundle out) {
super.onSaveInstanceState(out);
out.putInt("activeFeedId", m_activeFeedId);
}
class FeedsListAdapter extends SimpleCursorAdapter {
private Context context;
private int layout;
public FeedsListAdapter(Context context, int layout, Cursor c,
String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
this.context = context;
this.layout = layout;
this.context = context;
this.layout = layout;
}
}
@Override
public void onItemClick(AdapterView<?> av, View view, int position, long id) {
ListView list = (ListView)getActivity().findViewById(R.id.feeds);
if (list != null) {
Cursor cursor = (Cursor) list.getItemAtPosition(position);
if (cursor != null) {
int feedId = (int) cursor.getLong(0);
Log.d(TAG, "clicked on feed " + feedId);
viewFeed(feedId);
}
}
@ -239,16 +131,16 @@ public class FeedsFragment extends Fragment implements OnItemClickListener {
private void viewFeed(int feedId) {
m_activeFeedId = feedId;
FragmentTransaction ft = getFragmentManager().beginTransaction();
HeadlinesFragment frag = new HeadlinesFragment();
frag.initialize(feedId);
ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
ft.replace(R.id.headlines_container, frag);
ft.commit();
m_adapter.notifyDataSetChanged();
}

View File

@ -12,8 +12,6 @@ import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
@ -49,7 +47,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener {
list.setEmptyView(view.findViewById(R.id.no_headlines));
list.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
}
return view;
}
@ -95,19 +93,23 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ArticleFragment frag = new ArticleFragment();
//frag.initialize(articleId);
frag.initialize(articleId);
Animation a = AnimationUtils.loadAnimation(getActivity(), R.anim.test);
a.reset();
View v = getView().findViewById(R.id.headlines_container);
v.clearAnimation();
v.startAnimation(a);
/* LinearLayout v = (LinearLayout) getActivity().findViewById(R.id.headlines_container);
if (v != null) {
ObjectAnimator anim = ObjectAnimator.ofFloat(v, "weightSum", 0f, 0.5f);
anim.setDuration(1000);
anim.start();
} */
ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
ft.replace(R.id.article_container, frag);
ft.commit();
getActivity().findViewById(R.id.article_container).setVisibility(View.VISIBLE);
//m_adapter.notifyDataSetChanged();
}

View File

@ -8,6 +8,7 @@ import java.util.TimerTask;
import org.jsoup.Jsoup;
import android.animation.LayoutTransition;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Intent;
@ -22,6 +23,8 @@ import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
@ -33,9 +36,9 @@ public class MainActivity extends Activity {
private final static int UPDATE_INITIAL = 1;
private final static int UPDATE_SEQUENTIAL = 2;
private final static int UPDATE_OFFLINE = 3;
private final static int INITIAL_OFFSET_MAX = 100;
private SharedPreferences m_prefs;
private String m_themeName = "";
private boolean m_feedsOpened = false;
@ -44,47 +47,49 @@ public class MainActivity extends Activity {
protected int m_limit = 25;
protected int m_maxId = 0;
protected int m_updateMode = UPDATE_INITIAL;
protected enum SyncStatus { SYNC_INITIAL, SYNC_ONLINE, SYNC_OFFLINE };
protected MenuItem m_syncStatus;
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 UpdateTask m_updateTask;
private class UpdateTask extends TimerTask {
private Timer m_articlesTimer;
private Timer m_feedsTimer;
private ArticlesTask m_articlesTask;
private FeedsTask m_feedsTask;
private class ArticlesTask extends TimerTask {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
downloadArticles();
}
});
downloadArticles();
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
private class FeedsTask extends TimerTask {
@Override
public void run() {
downloadFeeds();
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
m_prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
if (m_prefs.getString("theme", "THEME_DARK").equals("THEME_DARK")) {
setTheme(R.style.DarkTheme);
} else {
@ -92,9 +97,9 @@ 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");
@ -103,37 +108,45 @@ public class MainActivity extends Activity {
m_updateMode = savedInstanceState.getInt("updateMode");
m_maxId = savedInstanceState.getInt("maxId");
}
// allow database to upgrade before we do anything else
// allow database to upgrade before we do anything else
DatabaseHelper dh = new DatabaseHelper(getApplicationContext());
SQLiteDatabase db = dh.getWritableDatabase();
if (m_updateMode == UPDATE_INITIAL) {
db.execSQL("DELETE FROM feeds;");
db.execSQL("DELETE FROM articles;");
}
db.close();
setContentView(R.layout.main);
if (!m_feedsOpened) {
Log.d(TAG, "Opening feeds fragment...");
FragmentTransaction ft = getFragmentManager().beginTransaction();
FeedsFragment frag = new FeedsFragment();
ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
ft.replace(R.id.feeds_container, frag, "FEEDLIST");
ft.commit();
m_feedsOpened = true;
}
scheduleNextUpdate();
}
db.close();
setContentView(R.layout.main);
LinearLayout wrapper = (LinearLayout) findViewById(R.id.headlines_wrapper);
LayoutTransition transitioner = new LayoutTransition();
wrapper.setLayoutTransition(transitioner);
m_feedsTask = new FeedsTask();
m_feedsTimer = new Timer("UpdateFeeds");
m_feedsTimer.schedule(m_feedsTask, 1000L, 60*1000L);
if (!m_feedsOpened) {
Log.d(TAG, "Opening feeds fragment...");
FragmentTransaction ft = getFragmentManager().beginTransaction();
FeedsFragment frag = new FeedsFragment();
ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
ft.replace(R.id.feeds_container, frag, "FEEDLIST");
ft.commit();
m_feedsOpened = true;
}
scheduleNextUpdate();
}
@Override
public void onSaveInstanceState (Bundle out) {
super.onSaveInstanceState(out);
@ -145,7 +158,7 @@ public class MainActivity extends Activity {
out.putInt("updateMode", m_updateMode);
out.putInt("maxId", m_maxId);
}
@Override
public void onResume() {
super.onResume();
@ -156,33 +169,33 @@ public class MainActivity extends Activity {
finish();
}
}
@Override
public void onDestroy() {
super.onDestroy();
m_timer.cancel();
m_timer = null;
m_articlesTimer.cancel();
m_articlesTimer = null;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
m_syncStatus = menu.findItem(R.id.sync_status);
switch (m_updateMode) {
case UPDATE_INITIAL:
setSyncStatus(SyncStatus.SYNC_INITIAL);
break;
setSyncStatus(SyncStatus.SYNC_INITIAL);
break;
case UPDATE_SEQUENTIAL:
setSyncStatus(SyncStatus.SYNC_ONLINE);
break;
default:
setSyncStatus(SyncStatus.SYNC_OFFLINE);
}
return true;
}
@ -202,7 +215,7 @@ public class MainActivity extends Activity {
break;
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
@ -214,129 +227,14 @@ public class MainActivity extends Activity {
return super.onOptionsItemSelected(item);
}
}
private void downloadArticles() {
ApiRequest task = new ApiRequest(m_sessionId,
private synchronized void downloadArticles() {
ApiRequest api = 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());
m_prefs.getString("password", null));
int articlesFound = 0;
try {
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, excerpt) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
SQLiteStatement stmtUpdate = db.compileStatement("UPDATE articles SET " +
"unread = ?, marked = ?, published = ?, updated = ?, is_updated = ?, title = ?, link = ?, feed_id = ?, " +
"tags = ?, content = ?, excerpt = ? WHERE " + BaseColumns._ID + " = ?");
for (Article article : articles) {
//Log.d(TAG, "Processing article #" + article.id);
m_maxId = Math.max(m_maxId, article.id);
++articlesFound;
Cursor c = db.query("articles", new String[] { BaseColumns._ID } , BaseColumns._ID + "=?",
new String[] { String.valueOf(article.id) }, null, null, null);
String excerpt = Jsoup.parse(article.content).text();
if (excerpt.length() > 250) {
excerpt = excerpt.substring(0, 250) + "...";
}
if (c.getCount() != 0) {
stmtUpdate.bindLong(1, article.unread ? 1 : 0);
stmtUpdate.bindLong(2, article.marked ? 1 : 0);
stmtUpdate.bindLong(3, article.published ? 1 : 0);
stmtUpdate.bindLong(4, article.updated);
stmtUpdate.bindLong(5, article.is_updated ? 1 : 0);
stmtUpdate.bindString(6, article.title);
stmtUpdate.bindString(7, article.link);
stmtUpdate.bindLong(8, article.feed_id);
stmtUpdate.bindString(9, ""); // comma-separated tags
stmtUpdate.bindString(10, article.content);
stmtUpdate.bindString(11, excerpt);
stmtUpdate.bindLong(12, article.id);
stmtUpdate.execute();
} 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(10, ""); // comma-separated tags
stmtInsert.bindString(11, article.content);
stmtInsert.bindString(12, excerpt);
stmtInsert.execute();
}
c.close();
}
db.close();
FeedsFragment ff = (FeedsFragment) getFragmentManager().findFragmentByTag("FEEDLIST");
if (ff != null) ff.updateListView();
} catch (Exception e) {
e.printStackTrace();
}
Log.d(TAG, articlesFound + " articles processed");
if (m_updateMode == UPDATE_INITIAL && articlesFound == m_limit && m_offset < INITIAL_OFFSET_MAX) {
m_offset += m_limit;
} else {
m_offset = 0;
if (m_updateMode == UPDATE_INITIAL) {
Log.i(TAG, "Switching to sequential mode...");
setSyncStatus(SyncStatus.SYNC_ONLINE);
m_updateMode = UPDATE_SEQUENTIAL;
}
}
scheduleNextUpdate();
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
task.execute(new HashMap<String,String>() {
JsonElement result = api.sendRequest(new HashMap<String,String>() {
{
put("sid", m_sessionId);
put("op", "getHeadlines");
@ -345,30 +243,248 @@ public class MainActivity extends Activity {
put("limit", String.valueOf(m_limit));
put("skip", String.valueOf(m_offset));
put("view_mode", "unread");
if (m_updateMode != UPDATE_INITIAL) {
put("since_id", String.valueOf(m_maxId));
}
}
});
if (result != null && api.getAuthStatus() == ApiRequest.STATUS_OK) {
try {
setSessionId(api.getSessionId());
int articlesFound = 0;
try {
JsonArray feeds_object = (JsonArray) result.getAsJsonArray();
Type listType = new TypeToken<List<Article>>() {}.getType();
List<Article> articles = api.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, excerpt) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
SQLiteStatement stmtUpdate = db.compileStatement("UPDATE articles SET " +
"unread = ?, marked = ?, published = ?, updated = ?, is_updated = ?, title = ?, link = ?, feed_id = ?, " +
"tags = ?, content = ?, excerpt = ? WHERE " + BaseColumns._ID + " = ?");
for (Article article : articles) {
//Log.d(TAG, "Processing article #" + article.id);
m_maxId = Math.max(m_maxId, article.id);
++articlesFound;
Cursor c = db.query("articles", new String[] { BaseColumns._ID } , BaseColumns._ID + "=?",
new String[] { String.valueOf(article.id) }, null, null, null);
String excerpt = Jsoup.parse(article.content).text();
if (excerpt.length() > 250) {
excerpt = excerpt.substring(0, 250) + "...";
}
if (c.getCount() != 0) {
stmtUpdate.bindLong(1, article.unread ? 1 : 0);
stmtUpdate.bindLong(2, article.marked ? 1 : 0);
stmtUpdate.bindLong(3, article.published ? 1 : 0);
stmtUpdate.bindLong(4, article.updated);
stmtUpdate.bindLong(5, article.is_updated ? 1 : 0);
stmtUpdate.bindString(6, article.title);
stmtUpdate.bindString(7, article.link);
stmtUpdate.bindLong(8, article.feed_id);
stmtUpdate.bindString(9, ""); // comma-separated tags
stmtUpdate.bindString(10, article.content);
stmtUpdate.bindString(11, excerpt);
stmtUpdate.bindLong(12, article.id);
stmtUpdate.execute();
} 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(10, ""); // comma-separated tags
stmtInsert.bindString(11, article.content);
stmtInsert.bindString(12, excerpt);
stmtInsert.execute();
}
c.close();
}
db.close();
runOnUiThread(new Runnable() {
@Override
public void run() {
FeedsFragment ff = (FeedsFragment) getFragmentManager().findFragmentByTag("FEEDLIST");
if (ff != null) ff.updateListView();
}
});
} catch (Exception e) {
e.printStackTrace();
}
Log.d(TAG, articlesFound + " articles processed");
if (m_updateMode == UPDATE_INITIAL && articlesFound == m_limit && m_offset < INITIAL_OFFSET_MAX) {
m_offset += m_limit;
} else {
m_offset = 0;
if (m_updateMode == UPDATE_INITIAL) {
Log.i(TAG, "Switching to sequential mode...");
runOnUiThread(new Runnable() {
@Override
public void run() {
setSyncStatus(SyncStatus.SYNC_ONLINE);
}
});
m_updateMode = UPDATE_SEQUENTIAL;
}
}
scheduleNextUpdate();
} catch (Exception e) {
e.printStackTrace();
}
} else if (result != null && api.getAuthStatus() != ApiRequest.STATUS_OK) {
// TODO handle error, e.g auth failed
} else if (result == null) {
// TODO could not parse result, server/network error?
}
}
private synchronized void downloadFeeds() {
ApiRequest api = new ApiRequest(m_sessionId,
m_prefs.getString("ttrss_url", null),
m_prefs.getString("login", null),
m_prefs.getString("password", null));
JsonElement result = api.sendRequest(new HashMap<String,String>() {
{
put("sid", m_sessionId);
put("op", "getFeeds");
put("cat_id", "-3");
put("unread_only", "0");
}
});
if (result != null && api.getAuthStatus() == ApiRequest.STATUS_OK) {
try {
try {
setSessionId(api.getSessionId());
} catch (NullPointerException e) {
//
}
JsonArray feeds_object = (JsonArray) result.getAsJsonArray();
Type listType = new TypeToken<List<Feed>>() {}.getType();
List<Feed> feeds = api.m_gson.fromJson(feeds_object, listType);
DatabaseHelper dh = new DatabaseHelper(this);
SQLiteDatabase db = dh.getWritableDatabase();
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) {
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();
runOnUiThread(new Runnable() {
@Override
public void run() {
/* View pb = findViewById(R.id.loading_progress);
if (pb != null) pb.setVisibility(View.INVISIBLE); */
FeedsFragment frag = (FeedsFragment)getFragmentManager().findFragmentByTag("FEEDLIST");
if (frag != null) {
frag.updateListView();
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
} else if (result != null && api.getAuthStatus() != ApiRequest.STATUS_OK) {
// TODO handle error, e.g auth failed
} else if (result == null) {
// TODO could not parse result, server/network error?
}
}
protected void scheduleNextUpdate() {
if (m_updateTask != null) m_updateTask.cancel();
m_updateTask = new UpdateTask();
if (m_updateMode == UPDATE_INITIAL) {
Log.i(TAG, "Scheduling initial update...");
m_timer = new Timer("DownloadInitial");
m_timer.schedule(m_updateTask, 1000L);
} else {
Log.i(TAG, "Scheduling sequential update...");
m_timer = new Timer("DownloadSequential");
m_timer.schedule(m_updateTask, 60*1000L);
}
if (m_articlesTask != null) m_articlesTask.cancel();
m_articlesTask = new ArticlesTask();
if (m_updateMode == UPDATE_INITIAL) {
Log.i(TAG, "Scheduling initial update...");
m_articlesTimer = new Timer("DownloadInitial");
m_articlesTimer.schedule(m_articlesTask, 1000L);
} else {
Log.i(TAG, "Scheduling sequential update...");
m_articlesTimer = new Timer("DownloadSequential");
m_articlesTimer.schedule(m_articlesTask, 60*1000L);
}
}
}