refresh feeds automatically

This commit is contained in:
Andrew Dolgov 2011-11-23 20:19:35 +03:00
parent 9b38d7a0bd
commit 212031e3cf
2 changed files with 66 additions and 15 deletions

View File

@ -32,7 +32,7 @@ import com.google.gson.reflect.TypeToken;
public class FeedsFragment extends Fragment implements OnItemClickListener { public class FeedsFragment extends Fragment implements OnItemClickListener {
private final String TAG = this.getClass().getSimpleName(); private final String TAG = this.getClass().getSimpleName();
private SharedPreferences m_prefs; private SharedPreferences m_prefs;
private String m_sessionId; //private String m_sessionId;
//private int m_activeFeedId; //private int m_activeFeedId;
private FeedListAdapter m_adapter; private FeedListAdapter m_adapter;
private List<Feed> m_feeds = new ArrayList<Feed>(); private List<Feed> m_feeds = new ArrayList<Feed>();
@ -69,7 +69,7 @@ public class FeedsFragment extends Fragment implements OnItemClickListener {
list.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE); list.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
list.setOnItemClickListener(this); list.setOnItemClickListener(this);
if (m_sessionId != null) //if (m_sessionId != null)
refresh(); refresh();
return view; return view;
@ -87,7 +87,7 @@ public class FeedsFragment extends Fragment implements OnItemClickListener {
m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext()); m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
m_feedSelectedListener = (OnFeedSelectedListener) activity; m_feedSelectedListener = (OnFeedSelectedListener) activity;
m_sessionId = ((MainActivity)activity).getSessionId(); //m_sessionId = ((MainActivity)activity).getSessionId();
} }
@ -95,7 +95,7 @@ public class FeedsFragment extends Fragment implements OnItemClickListener {
public void onSaveInstanceState (Bundle out) { public void onSaveInstanceState (Bundle out) {
super.onSaveInstanceState(out); super.onSaveInstanceState(out);
out.putString("sessionId", m_sessionId); //out.putString("sessionId", m_sessionId);
//out.putInt("activeFeedId", m_activeFeedId); //out.putInt("activeFeedId", m_activeFeedId);
} }
@ -114,16 +114,22 @@ public class FeedsFragment extends Fragment implements OnItemClickListener {
fr.setApi(m_prefs.getString("ttrss_url", null)); fr.setApi(m_prefs.getString("ttrss_url", null));
final String sessionId = ((MainActivity)getActivity()).getSessionId();
if (sessionId != null) {
HashMap<String,String> map = new HashMap<String,String>() { HashMap<String,String> map = new HashMap<String,String>() {
{ {
put("op", "getFeeds"); put("op", "getFeeds");
put("sid", m_sessionId); put("sid", sessionId);
put("cat_id", "-3"); put("cat_id", "-3");
put("unread_only", "true"); put("unread_only", "true");
} }
}; };
fr.execute(map); fr.execute(map);
}
} }
public void setLoadingStatus(int status, boolean showProgress) { public void setLoadingStatus(int status, boolean showProgress) {

View File

@ -1,9 +1,10 @@
package org.fox.ttrss; package org.fox.ttrss;
import java.util.HashMap; import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity; import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction; import android.app.FragmentTransaction;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
@ -29,10 +30,30 @@ public class MainActivity extends Activity implements FeedsFragment.OnFeedSelect
private String m_sessionId; private String m_sessionId;
private Article m_selectedArticle; private Article m_selectedArticle;
private Feed m_activeFeed; private Feed m_activeFeed;
private Timer m_refreshTimer;
private RefreshTask m_refreshTask;
protected MenuItem m_syncStatus; protected MenuItem m_syncStatus;
public synchronized String getSessionId() { private class RefreshTask extends TimerTask {
@Override
public void run() {
Log.d(TAG, "Refreshing feeds...");
refreshFeeds();
}
}
public synchronized void refreshFeeds() {
FeedsFragment frag = (FeedsFragment) getFragmentManager().findFragmentById(R.id.feeds_fragment);
if (frag != null) {
frag.refresh();
}
}
public String getSessionId() {
return m_sessionId; return m_sessionId;
} }
@ -127,6 +148,15 @@ public class MainActivity extends Activity implements FeedsFragment.OnFeedSelect
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
if (m_refreshTask != null) {
m_refreshTask.cancel();
m_refreshTask = null;
}
if (m_refreshTimer != null) {
m_refreshTimer.cancel();
m_refreshTimer = null;
}
} }
@Override @Override
@ -179,6 +209,21 @@ public class MainActivity extends Activity implements FeedsFragment.OnFeedSelect
FragmentTransaction ft = getFragmentManager().beginTransaction(); FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.feeds_fragment, frag); ft.replace(R.id.feeds_fragment, frag);
ft.commit(); ft.commit();
if (m_refreshTask != null) {
m_refreshTask.cancel();
m_refreshTask = null;
}
if (m_refreshTimer != null) {
m_refreshTimer.cancel();
m_refreshTimer = null;
}
m_refreshTask = new RefreshTask();
m_refreshTimer = new Timer("Refresh");
m_refreshTimer.schedule(m_refreshTask, 60*1000L, 60*1000L);
} }
} else { } else {
JsonObject content = rv.get("content").getAsJsonObject(); JsonObject content = rv.get("content").getAsJsonObject();