2011-09-08 11:28:38 +00:00
|
|
|
package org.fox.ttrss;
|
|
|
|
|
|
|
|
import java.lang.reflect.Type;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.app.Fragment;
|
2011-09-08 12:45:08 +00:00
|
|
|
import android.app.FragmentTransaction;
|
2011-09-08 11:28:38 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.SharedPreferences;
|
2011-09-08 12:56:10 +00:00
|
|
|
import android.database.Cursor;
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
2011-09-08 16:05:00 +00:00
|
|
|
import android.database.sqlite.SQLiteStatement;
|
2011-09-08 11:28:38 +00:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.preference.PreferenceManager;
|
2011-09-08 16:05:00 +00:00
|
|
|
import android.provider.BaseColumns;
|
2011-09-08 12:45:08 +00:00
|
|
|
import android.util.Log;
|
2011-09-08 11:28:38 +00:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2011-09-08 12:45:08 +00:00
|
|
|
import android.widget.AdapterView;
|
|
|
|
import android.widget.AdapterView.OnItemClickListener;
|
2011-09-08 11:28:38 +00:00
|
|
|
import android.widget.ListView;
|
2011-09-08 16:05:00 +00:00
|
|
|
import android.widget.SimpleCursorAdapter;
|
2011-09-08 11:28:38 +00:00
|
|
|
|
|
|
|
import com.google.gson.Gson;
|
2011-09-08 16:05:00 +00:00
|
|
|
import com.google.gson.JsonArray;
|
2011-09-08 11:28:38 +00:00
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
import com.google.gson.reflect.TypeToken;
|
|
|
|
|
2011-09-08 12:45:08 +00:00
|
|
|
public class FeedsFragment extends Fragment implements OnItemClickListener {
|
2011-09-08 11:28:38 +00:00
|
|
|
private final String TAG = this.getClass().getSimpleName();
|
|
|
|
|
2011-09-08 16:05:00 +00:00
|
|
|
// protected ArrayList<Feed> m_feeds = new ArrayList<Feed>();
|
2011-09-08 11:28:38 +00:00
|
|
|
protected FeedsListAdapter m_adapter;
|
|
|
|
protected SharedPreferences m_prefs;
|
|
|
|
protected String m_sessionId;
|
2011-09-08 12:45:08 +00:00
|
|
|
protected int m_activeFeedId;
|
|
|
|
protected long m_lastUpdate;
|
2011-09-08 11:28:38 +00:00
|
|
|
protected Gson m_gson = new Gson();
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
|
|
|
|
2011-09-08 16:05:00 +00:00
|
|
|
m_sessionId = m_prefs.getString("last_session_id", null);
|
|
|
|
|
2011-09-08 11:28:38 +00:00
|
|
|
if (savedInstanceState != null) {
|
2011-09-08 12:45:08 +00:00
|
|
|
m_sessionId = savedInstanceState.getString("sessionId");
|
|
|
|
m_activeFeedId = savedInstanceState.getInt("activeFeedId");
|
|
|
|
m_lastUpdate = savedInstanceState.getLong("lastUpdate");
|
2011-09-08 11:28:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
View view = inflater.inflate(R.layout.feeds_fragment, container, false);
|
|
|
|
|
2011-09-08 16:05:00 +00:00
|
|
|
DatabaseHelper helper = new DatabaseHelper(getActivity());
|
|
|
|
Cursor cursor = helper.getReadableDatabase().query("feeds", null, null, null, null, null, "unread DESC");
|
|
|
|
|
|
|
|
m_adapter = new FeedsListAdapter(getActivity(), R.layout.feeds_row, cursor,
|
|
|
|
new String[] { "title", "unread" }, new int[] { R.id.title, R.id.unread_counter }, 0);
|
2011-09-08 11:28:38 +00:00
|
|
|
|
|
|
|
ListView list = (ListView) view.findViewById(R.id.feeds);
|
|
|
|
|
|
|
|
if (list != null) {
|
2011-09-08 12:45:08 +00:00
|
|
|
list.setAdapter(m_adapter);
|
|
|
|
list.setOnItemClickListener(this);
|
2011-09-08 11:28:38 +00:00
|
|
|
}
|
2011-09-08 12:45:08 +00:00
|
|
|
|
2011-09-08 16:05:00 +00:00
|
|
|
updateSelf();
|
|
|
|
|
2011-09-08 11:28:38 +00:00
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
2011-09-08 16:05:00 +00:00
|
|
|
protected void updateSessionId(String sessionId) {
|
|
|
|
m_sessionId = sessionId;
|
|
|
|
|
|
|
|
SharedPreferences.Editor editor = m_prefs.edit();
|
|
|
|
editor.putString("last_session_id", m_sessionId);
|
|
|
|
editor.commit();
|
2011-09-08 12:45:08 +00:00
|
|
|
}
|
|
|
|
|
2011-09-08 11:28:38 +00:00
|
|
|
@Override
|
|
|
|
public void onAttach(Activity activity) {
|
2011-09-08 16:05:00 +00:00
|
|
|
super.onAttach(activity);
|
2011-09-08 11:28:38 +00:00
|
|
|
m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void initialize(String sessionId) {
|
|
|
|
m_sessionId = sessionId;
|
|
|
|
}
|
|
|
|
|
2011-09-08 16:05:00 +00:00
|
|
|
private void updateSelf() {
|
|
|
|
ApiRequest task = new ApiRequest(m_sessionId,
|
|
|
|
m_prefs.getString("ttrss_url", null),
|
|
|
|
m_prefs.getString("login", null),
|
|
|
|
m_prefs.getString("password", null)) {
|
2011-09-08 11:28:38 +00:00
|
|
|
@Override
|
|
|
|
protected void onPostExecute(JsonElement result) {
|
2011-09-08 16:05:00 +00:00
|
|
|
if (result != null && getAuthStatus() == STATUS_OK) {
|
2011-09-08 12:45:08 +00:00
|
|
|
try {
|
2011-09-08 16:05:00 +00:00
|
|
|
JsonArray feeds_object = (JsonArray) result.getAsJsonArray();
|
|
|
|
|
|
|
|
Type listType = new TypeToken<List<Feed>>() {}.getType();
|
|
|
|
List<Feed> feeds = m_gson.fromJson(feeds_object, listType);
|
2011-09-08 12:45:08 +00:00
|
|
|
|
2011-09-08 16:05:00 +00:00
|
|
|
DatabaseHelper dh = new DatabaseHelper(getActivity());
|
|
|
|
SQLiteDatabase db = dh.getWritableDatabase();
|
2011-09-08 11:28:38 +00:00
|
|
|
|
2011-09-08 16:05:00 +00:00
|
|
|
db.execSQL("DELETE FROM FEEDS");
|
2011-09-08 11:28:38 +00:00
|
|
|
|
2011-09-08 16:05:00 +00:00
|
|
|
SQLiteStatement stmt = db.compileStatement("INSERT INTO feeds " +
|
|
|
|
"("+BaseColumns._ID+", title, feed_url, unread, 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();
|
2011-09-08 11:28:38 +00:00
|
|
|
}
|
2011-09-08 16:05:00 +00:00
|
|
|
|
|
|
|
db.close();
|
|
|
|
|
|
|
|
m_adapter.notifyDataSetChanged();
|
|
|
|
|
2011-09-08 11:28:38 +00:00
|
|
|
} catch (Exception e) {
|
2011-09-08 16:05:00 +00:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2011-09-08 12:45:08 +00:00
|
|
|
}
|
2011-09-08 16:05:00 +00:00
|
|
|
|
|
|
|
}
|
2011-09-08 11:28:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
task.execute(new HashMap<String,String>() {
|
|
|
|
{
|
|
|
|
put("sid", m_sessionId);
|
|
|
|
put("op", "getFeeds");
|
2011-09-08 16:05:00 +00:00
|
|
|
put("cat_id", "-3");
|
2011-09-08 11:28:38 +00:00
|
|
|
put("unread_only", "true");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-09-08 16:05:00 +00:00
|
|
|
}
|
2011-09-08 11:28:38 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSaveInstanceState (Bundle out) {
|
|
|
|
super.onSaveInstanceState(out);
|
|
|
|
|
|
|
|
out.putString("sessionId", m_sessionId);
|
2011-09-08 12:45:08 +00:00
|
|
|
out.putInt("activeFeedId", m_activeFeedId);
|
|
|
|
out.putLong("lastUpdate", m_lastUpdate);
|
2011-09-08 11:28:38 +00:00
|
|
|
}
|
|
|
|
|
2011-09-08 16:05:00 +00:00
|
|
|
private class FeedsListAdapter extends SimpleCursorAdapter {
|
2011-09-08 11:28:38 +00:00
|
|
|
|
2011-09-08 16:05:00 +00:00
|
|
|
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);
|
2011-09-08 11:28:38 +00:00
|
|
|
|
2011-09-08 16:05:00 +00:00
|
|
|
this.context = context;
|
|
|
|
this.layout = layout;
|
2011-09-08 11:28:38 +00:00
|
|
|
}
|
|
|
|
|
2011-09-08 16:05:00 +00:00
|
|
|
|
2011-09-08 11:28:38 +00:00
|
|
|
}
|
2011-09-08 12:45:08 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onItemClick(AdapterView<?> av, View view, int position, long id) {
|
|
|
|
ListView list = (ListView)getActivity().findViewById(R.id.feeds);
|
|
|
|
|
|
|
|
if (list != null) {
|
2011-09-08 16:05:00 +00:00
|
|
|
Cursor cursor = (Cursor) list.getItemAtPosition(position);
|
2011-09-08 12:45:08 +00:00
|
|
|
|
2011-09-08 16:05:00 +00:00
|
|
|
if (cursor != null) {
|
|
|
|
int feedId = (int) cursor.getLong(0);
|
|
|
|
|
|
|
|
Log.d(TAG, "clicked on feed " + feedId);
|
2011-09-08 12:45:08 +00:00
|
|
|
|
2011-09-08 16:05:00 +00:00
|
|
|
viewFeed(feedId);
|
2011-09-08 12:45:08 +00:00
|
|
|
}
|
|
|
|
}
|
2011-09-08 16:05:00 +00:00
|
|
|
}
|
2011-09-08 12:45:08 +00:00
|
|
|
|
|
|
|
private void viewFeed(int feedId) {
|
|
|
|
m_activeFeedId = feedId;
|
|
|
|
|
|
|
|
FragmentTransaction ft = getFragmentManager().beginTransaction();
|
|
|
|
HeadlinesFragment frag = new HeadlinesFragment();
|
|
|
|
|
|
|
|
frag.initialize(m_sessionId, 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();
|
|
|
|
|
2011-09-08 16:05:00 +00:00
|
|
|
}
|
2011-09-08 12:56:10 +00:00
|
|
|
|
2011-09-08 11:28:38 +00:00
|
|
|
}
|