2011-09-08 11:28:38 +00:00
|
|
|
package org.fox.ttrss;
|
|
|
|
|
|
|
|
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 11:28:38 +00:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.preference.PreferenceManager;
|
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 16:16:46 +00:00
|
|
|
import android.widget.AbsListView;
|
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
|
|
|
|
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();
|
|
|
|
|
|
|
|
protected FeedsListAdapter m_adapter;
|
|
|
|
protected SharedPreferences m_prefs;
|
2011-09-08 12:45:08 +00:00
|
|
|
protected int m_activeFeedId;
|
2011-09-09 06:02:31 +00:00
|
|
|
protected Cursor m_cursor;
|
2011-09-09 09:23:56 +00:00
|
|
|
protected SQLiteDatabase m_db;
|
2011-09-09 06:02:31 +00:00
|
|
|
|
2011-09-09 16:36:09 +00:00
|
|
|
/* private Timer m_timer;
|
2011-09-09 06:02:31 +00:00
|
|
|
private TimerTask m_updateTask = new TimerTask() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2011-09-09 16:36:09 +00:00
|
|
|
downloadFeeds();
|
2011-09-09 06:02:31 +00:00
|
|
|
}
|
2011-09-09 16:36:09 +00:00
|
|
|
}; */
|
|
|
|
|
2011-09-08 11:28:38 +00:00
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
|
|
|
|
|
|
|
if (savedInstanceState != null) {
|
2011-09-08 12:45:08 +00:00
|
|
|
m_activeFeedId = savedInstanceState.getInt("activeFeedId");
|
2011-09-08 11:28:38 +00:00
|
|
|
}
|
2011-09-09 16:36:09 +00:00
|
|
|
|
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());
|
2011-09-09 16:36:09 +00:00
|
|
|
|
2011-09-09 09:23:56 +00:00
|
|
|
m_db = helper.getReadableDatabase();
|
2011-09-09 10:09:06 +00:00
|
|
|
m_cursor = m_db.query("feeds_unread", null, "unread > 0", null, null, null, "title");
|
2011-09-09 16:36:09 +00:00
|
|
|
|
2011-09-09 06:02:31 +00:00
|
|
|
m_adapter = new FeedsListAdapter(getActivity(), R.layout.feeds_row, m_cursor,
|
2011-09-08 16:05:00 +00:00
|
|
|
new String[] { "title", "unread" }, new int[] { R.id.title, R.id.unread_counter }, 0);
|
2011-09-09 16:36:09 +00:00
|
|
|
|
2011-09-08 11:28:38 +00:00
|
|
|
ListView list = (ListView) view.findViewById(R.id.feeds);
|
2011-09-09 16:36:09 +00:00
|
|
|
|
2011-09-08 11:28:38 +00:00
|
|
|
if (list != null) {
|
2011-09-08 12:45:08 +00:00
|
|
|
list.setAdapter(m_adapter);
|
|
|
|
list.setOnItemClickListener(this);
|
2011-09-09 09:23:56 +00:00
|
|
|
list.setEmptyView(view.findViewById(R.id.no_unread_feeds));
|
2011-09-08 16:16:46 +00:00
|
|
|
list.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
|
2011-09-08 11:28:38 +00:00
|
|
|
}
|
2011-09-08 12:45:08 +00:00
|
|
|
|
2011-09-09 16:36:09 +00:00
|
|
|
/* View pb = view.findViewById(R.id.loading_progress);
|
|
|
|
|
2011-09-09 09:23:56 +00:00
|
|
|
if (pb != null && m_cursor.getCount() == 0)
|
2011-09-09 16:36:09 +00:00
|
|
|
pb.setVisibility(View.VISIBLE); */
|
|
|
|
|
|
|
|
// m_timer = new Timer("UpdateFeeds");
|
|
|
|
// m_timer.schedule(m_updateTask, 1000L, 60*1000L);
|
2011-09-09 06:02:31 +00:00
|
|
|
|
2011-09-08 11:28:38 +00:00
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
2011-09-09 06:02:31 +00:00
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
2011-09-08 16:05:00 +00:00
|
|
|
|
2011-09-09 09:23:56 +00:00
|
|
|
m_cursor.close();
|
|
|
|
m_db.close();
|
2011-09-09 16:36:09 +00:00
|
|
|
|
|
|
|
// m_timer.cancel();
|
|
|
|
// m_timer = null;
|
2011-09-08 12:45:08 +00:00
|
|
|
}
|
2011-09-09 16:36:09 +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());
|
|
|
|
}
|
|
|
|
|
2011-09-09 16:36:09 +00:00
|
|
|
@Override
|
2011-09-08 11:28:38 +00:00
|
|
|
public void onSaveInstanceState (Bundle out) {
|
|
|
|
super.onSaveInstanceState(out);
|
2011-09-09 16:36:09 +00:00
|
|
|
|
2011-09-08 12:45:08 +00:00
|
|
|
out.putInt("activeFeedId", m_activeFeedId);
|
2011-09-08 11:28:38 +00:00
|
|
|
}
|
2011-09-09 16:36:09 +00:00
|
|
|
|
2011-09-09 07:03:14 +00:00
|
|
|
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;
|
2011-09-09 16:36:09 +00:00
|
|
|
|
2011-09-08 16:05:00 +00:00
|
|
|
public FeedsListAdapter(Context context, int layout, Cursor c,
|
|
|
|
String[] from, int[] to, int flags) {
|
|
|
|
super(context, layout, c, from, to, flags);
|
2011-09-09 16:36:09 +00:00
|
|
|
|
|
|
|
this.context = context;
|
|
|
|
this.layout = layout;
|
2011-09-09 06:02:31 +00:00
|
|
|
}
|
2011-09-09 16:36:09 +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);
|
2011-09-09 16:36:09 +00:00
|
|
|
|
2011-09-08 12:45:08 +00:00
|
|
|
if (list != null) {
|
2011-09-08 16:05:00 +00:00
|
|
|
Cursor cursor = (Cursor) list.getItemAtPosition(position);
|
2011-09-09 16:36:09 +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-09 16:36:09 +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;
|
2011-09-09 16:36:09 +00:00
|
|
|
|
2011-09-08 12:45:08 +00:00
|
|
|
FragmentTransaction ft = getFragmentManager().beginTransaction();
|
|
|
|
HeadlinesFragment frag = new HeadlinesFragment();
|
2011-09-09 16:36:09 +00:00
|
|
|
|
2011-09-09 06:02:31 +00:00
|
|
|
frag.initialize(feedId);
|
2011-09-09 16:36:09 +00:00
|
|
|
|
2011-09-08 12:45:08 +00:00
|
|
|
ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
|
|
|
|
ft.replace(R.id.headlines_container, frag);
|
|
|
|
ft.commit();
|
2011-09-09 16:36:09 +00:00
|
|
|
|
2011-09-08 12:45:08 +00:00
|
|
|
m_adapter.notifyDataSetChanged();
|
|
|
|
|
2011-09-09 09:23:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized void updateListView() {
|
|
|
|
m_cursor.requery();
|
|
|
|
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
|
|
|
}
|