From 212031e3cf86a6fbf4f1c4b882a342b4a5b736a3 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Wed, 23 Nov 2011 20:19:35 +0300 Subject: [PATCH] refresh feeds automatically --- src/org/fox/ttrss/FeedsFragment.java | 32 ++++++++++-------- src/org/fox/ttrss/MainActivity.java | 49 ++++++++++++++++++++++++++-- 2 files changed, 66 insertions(+), 15 deletions(-) diff --git a/src/org/fox/ttrss/FeedsFragment.java b/src/org/fox/ttrss/FeedsFragment.java index f3f24762..9898e10d 100644 --- a/src/org/fox/ttrss/FeedsFragment.java +++ b/src/org/fox/ttrss/FeedsFragment.java @@ -32,7 +32,7 @@ import com.google.gson.reflect.TypeToken; public class FeedsFragment extends Fragment implements OnItemClickListener { private final String TAG = this.getClass().getSimpleName(); private SharedPreferences m_prefs; - private String m_sessionId; + //private String m_sessionId; //private int m_activeFeedId; private FeedListAdapter m_adapter; private List m_feeds = new ArrayList(); @@ -69,7 +69,7 @@ public class FeedsFragment extends Fragment implements OnItemClickListener { list.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE); list.setOnItemClickListener(this); - if (m_sessionId != null) + //if (m_sessionId != null) refresh(); return view; @@ -87,7 +87,7 @@ public class FeedsFragment extends Fragment implements OnItemClickListener { m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext()); 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) { super.onSaveInstanceState(out); - out.putString("sessionId", m_sessionId); + //out.putString("sessionId", m_sessionId); //out.putInt("activeFeedId", m_activeFeedId); } @@ -114,16 +114,22 @@ public class FeedsFragment extends Fragment implements OnItemClickListener { fr.setApi(m_prefs.getString("ttrss_url", null)); - HashMap map = new HashMap() { - { - put("op", "getFeeds"); - put("sid", m_sessionId); - put("cat_id", "-3"); - put("unread_only", "true"); - } - }; + final String sessionId = ((MainActivity)getActivity()).getSessionId(); + + if (sessionId != null) { + + HashMap map = new HashMap() { + { + put("op", "getFeeds"); + put("sid", sessionId); + put("cat_id", "-3"); + put("unread_only", "true"); + } + }; - fr.execute(map); + fr.execute(map); + + } } public void setLoadingStatus(int status, boolean showProgress) { diff --git a/src/org/fox/ttrss/MainActivity.java b/src/org/fox/ttrss/MainActivity.java index f458af79..3123efa3 100644 --- a/src/org/fox/ttrss/MainActivity.java +++ b/src/org/fox/ttrss/MainActivity.java @@ -1,9 +1,10 @@ package org.fox.ttrss; import java.util.HashMap; +import java.util.Timer; +import java.util.TimerTask; import android.app.Activity; -import android.app.FragmentManager; import android.app.FragmentTransaction; import android.content.Intent; import android.content.SharedPreferences; @@ -29,10 +30,30 @@ public class MainActivity extends Activity implements FeedsFragment.OnFeedSelect private String m_sessionId; private Article m_selectedArticle; private Feed m_activeFeed; + private Timer m_refreshTimer; + private RefreshTask m_refreshTask; 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; } @@ -127,6 +148,15 @@ public class MainActivity extends Activity implements FeedsFragment.OnFeedSelect public void onDestroy() { super.onDestroy(); + if (m_refreshTask != null) { + m_refreshTask.cancel(); + m_refreshTask = null; + } + + if (m_refreshTimer != null) { + m_refreshTimer.cancel(); + m_refreshTimer = null; + } } @Override @@ -179,6 +209,21 @@ public class MainActivity extends Activity implements FeedsFragment.OnFeedSelect FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.replace(R.id.feeds_fragment, frag); 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 { JsonObject content = rv.get("content").getAsJsonObject();