From 16fd6cebd7ebda1ff159abdc8a382e621fa358c4 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Sun, 27 Nov 2011 13:18:04 +0300 Subject: [PATCH] cleaner preference handling in ApiRequest make json data debugging optional --- AndroidManifest.xml | 4 +- res/values/strings.xml | 3 + res/xml/preferences.xml | 11 ++- src/org/fox/ttrss/ApiRequest.java | 21 ++-- src/org/fox/ttrss/FeedsFragment.java | 117 ++++++++++++----------- src/org/fox/ttrss/HeadlinesFragment.java | 20 ++-- src/org/fox/ttrss/MainActivity.java | 15 +-- 7 files changed, 106 insertions(+), 85 deletions(-) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index ea8a83f1..f1825e8f 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -1,8 +1,8 @@ + android:versionCode="13" + android:versionName="0.1.12"> diff --git a/res/values/strings.xml b/res/values/strings.xml index 8e32a61c..9ffb5baf 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -9,6 +9,7 @@ Log in Log out Login + Debugging Password http://example.domain/tt-rss/ Authentication @@ -20,6 +21,7 @@ Dark Preferences Light + Connection Login failed: API disabled. Login failed: no data received. Login failed: username or password incorrect. @@ -42,4 +44,5 @@ No feeds to display. Error: invalid object received. + Log sent and received data diff --git a/res/xml/preferences.xml b/res/xml/preferences.xml index 1c102fb0..4fb2c5ec 100644 --- a/res/xml/preferences.xml +++ b/res/xml/preferences.xml @@ -3,14 +3,13 @@ - + - - + - + + + + + \ No newline at end of file diff --git a/src/org/fox/ttrss/ApiRequest.java b/src/org/fox/ttrss/ApiRequest.java index f2cdecdc..a33f07da 100644 --- a/src/org/fox/ttrss/ApiRequest.java +++ b/src/org/fox/ttrss/ApiRequest.java @@ -16,7 +16,10 @@ import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpParams; +import android.content.Context; +import android.content.SharedPreferences; import android.os.AsyncTask; +import android.preference.PreferenceManager; import android.util.Log; import com.google.gson.Gson; @@ -33,13 +36,17 @@ public class ApiRequest extends AsyncTask, Integer, JsonE private String m_api; private boolean m_trustAny = false; + private boolean m_transportDebugging = false; + private Context m_context; - protected void setApi(String api) { - m_api = api; - } + public ApiRequest(Context context) { + m_context = context; - public void setTrustAny(boolean trust) { - m_trustAny = trust; + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(m_context); + + m_api = prefs.getString("ttrss_url", null); + m_trustAny = prefs.getBoolean("ssl_trust_any", false); + m_transportDebugging = prefs.getBoolean("transport_debugging", false); } @Override @@ -49,7 +56,7 @@ public class ApiRequest extends AsyncTask, Integer, JsonE String requestStr = gson.toJson(new HashMap(params[0])); - //Log.d(TAG, ">>> (" + requestStr + ") " + m_api); + if (m_transportDebugging) Log.d(TAG, ">>> (" + requestStr + ") " + m_api); DefaultHttpClient client; @@ -83,7 +90,7 @@ public class ApiRequest extends AsyncTask, Integer, JsonE response += s; } - Log.d(TAG, "<<< " + response); + if (m_transportDebugging) Log.d(TAG, "<<< " + response); JsonParser parser = new JsonParser(); diff --git a/src/org/fox/ttrss/FeedsFragment.java b/src/org/fox/ttrss/FeedsFragment.java index 2e0a7665..a8278125 100644 --- a/src/org/fox/ttrss/FeedsFragment.java +++ b/src/org/fox/ttrss/FeedsFragment.java @@ -89,7 +89,7 @@ public class FeedsFragment extends Fragment implements OnItemClickListener { list.setOnItemClickListener(this); if (m_feeds == null || m_feeds.size() == 0) - refresh(); + refresh(false); else view.findViewById(R.id.loading_progress).setVisibility(View.GONE); @@ -129,23 +129,22 @@ public class FeedsFragment extends Fragment implements OnItemClickListener { } } - public void refresh() { - FeedsRequest req = new FeedsRequest(); + public void refresh(boolean background) { + FeedsRequest req = new FeedsRequest(getActivity().getApplicationContext()); - req.setApi(m_prefs.getString("ttrss_url", null)); - req.setTrustAny(m_prefs.getBoolean("ssl_trust_any", false)); - final String sessionId = ((MainActivity)getActivity()).getSessionId(); final boolean unreadOnly = ((MainActivity)getActivity()).getUnreadOnly(); if (sessionId != null) { - getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - setLoadingStatus(R.string.blank, true); - } - }); + if (!background) { + getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + setLoadingStatus(R.string.blank, true); + } + }); + } HashMap map = new HashMap() { { @@ -181,53 +180,57 @@ public class FeedsFragment extends Fragment implements OnItemClickListener { private class FeedsRequest extends ApiRequest { - protected void onPostExecute(JsonElement result) { - if (result != null) { - try { - JsonObject rv = result.getAsJsonObject(); - - Gson gson = new Gson(); - - int status = rv.get("status").getAsInt(); - - if (status == 0) { - JsonArray content = rv.get("content").getAsJsonArray(); - if (content != null) { - Type listType = new TypeToken>() {}.getType(); - final List feeds = gson.fromJson(content, listType); - - m_feeds.clear(); - - for (Feed f : feeds) - m_feeds.add(f); - - sortFeeds(); - - if (m_feeds.size() == 0) - setLoadingStatus(R.string.error_no_feeds, false); - else - setLoadingStatus(R.string.blank, false); - - } - } else { - MainActivity activity = (MainActivity)getActivity(); - activity.login(); - } - } catch (Exception e) { - e.printStackTrace(); - setLoadingStatus(R.string.error_invalid_object, false); - // report invalid object received - } - } else { - // report null object received, unless we've been awakened from sleep right in the right time - // so that current request failed - if (m_feeds.size() == 0) setLoadingStatus(R.string.error_no_data, false); - } - - return; - } + public FeedsRequest(Context context) { + super(context); } + protected void onPostExecute(JsonElement result) { + if (result != null) { + try { + JsonObject rv = result.getAsJsonObject(); + + Gson gson = new Gson(); + + int status = rv.get("status").getAsInt(); + + if (status == 0) { + JsonArray content = rv.get("content").getAsJsonArray(); + if (content != null) { + Type listType = new TypeToken>() {}.getType(); + final List feeds = gson.fromJson(content, listType); + + m_feeds.clear(); + + for (Feed f : feeds) + m_feeds.add(f); + + sortFeeds(); + + if (m_feeds.size() == 0) + setLoadingStatus(R.string.error_no_feeds, false); + else + setLoadingStatus(R.string.blank, false); + + } + } else { + MainActivity activity = (MainActivity)getActivity(); + activity.login(); + } + } catch (Exception e) { + e.printStackTrace(); + setLoadingStatus(R.string.error_invalid_object, false); + // report invalid object received + } + } else { + // report null object received, unless we've been awakened from sleep right in the right time + // so that current request failed + if (m_feeds.size() == 0) setLoadingStatus(R.string.error_no_data, false); + } + + return; + } + } + private class FeedListAdapter extends ArrayAdapter { private ArrayList items; diff --git a/src/org/fox/ttrss/HeadlinesFragment.java b/src/org/fox/ttrss/HeadlinesFragment.java index d6c7ae1c..0165a05c 100644 --- a/src/org/fox/ttrss/HeadlinesFragment.java +++ b/src/org/fox/ttrss/HeadlinesFragment.java @@ -123,10 +123,10 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener { } public void refresh(boolean append) { - HeadlinesRequest req = new HeadlinesRequest(); + HeadlinesRequest req = new HeadlinesRequest(getActivity().getApplicationContext()); - req.setApi(m_prefs.getString("ttrss_url", null)); - req.setTrustAny(m_prefs.getBoolean("ssl_trust_any", false)); + //req.setApi(m_prefs.getString("ttrss_url", null)); + //req.setTrustAny(m_prefs.getBoolean("ssl_trust_any", false)); final String sessionId = ((MainActivity)getActivity()).getSessionId(); final boolean showUnread = ((MainActivity)getActivity()).getUnreadArticlesOnly(); @@ -191,6 +191,10 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener { private class HeadlinesRequest extends ApiRequest { int m_offset = 0; + public HeadlinesRequest(Context context) { + super(context); + } + protected void onPostExecute(JsonElement result) { if (result != null) { try { @@ -250,9 +254,9 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener { } public void catchupArticle(final Article article) { - ApiRequest req = new ApiRequest(); - req.setApi(m_prefs.getString("ttrss_url", null)); - req.setTrustAny(m_prefs.getBoolean("ssl_trust_any", false)); + ApiRequest req = new ApiRequest(getActivity().getApplicationContext()); + //req.setApi(m_prefs.getString("ttrss_url", null)); + //req.setTrustAny(m_prefs.getBoolean("ssl_trust_any", false)); final String sessionId = ((MainActivity)getActivity()).getSessionId(); @@ -270,9 +274,7 @@ public class HeadlinesFragment extends Fragment implements OnItemClickListener { } public void setArticleMarked(final Article article) { - ApiRequest req = new ApiRequest(); - req.setApi(m_prefs.getString("ttrss_url", null)); - req.setTrustAny(m_prefs.getBoolean("ssl_trust_any", false)); + ApiRequest req = new ApiRequest(getActivity().getApplicationContext()); final String sessionId = ((MainActivity)getActivity()).getSessionId(); diff --git a/src/org/fox/ttrss/MainActivity.java b/src/org/fox/ttrss/MainActivity.java index 6212f2e4..b00ce875 100644 --- a/src/org/fox/ttrss/MainActivity.java +++ b/src/org/fox/ttrss/MainActivity.java @@ -5,6 +5,7 @@ import java.util.Timer; import java.util.TimerTask; import android.animation.LayoutTransition; +import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; @@ -53,10 +54,10 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe public synchronized void refreshFeeds() { FeedsFragment frag = (FeedsFragment) getSupportFragmentManager().findFragmentById(R.id.feeds_fragment); - Log.d(TAG, "Refreshing feeds..." + frag); + Log.d(TAG, "Refreshing feeds..."); if (frag != null) { - frag.refresh(); + frag.refresh(true); } } @@ -262,7 +263,6 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { - Log.d(TAG, "Overriding back button"); if (m_smallScreenMode) { if (m_selectedArticle != null) { @@ -450,6 +450,9 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe } private class LoginRequest extends ApiRequest { + public LoginRequest(Context context) { + super(context); + } protected void onPostExecute(JsonElement result) { if (result != null) { @@ -611,9 +614,9 @@ public class MainActivity extends FragmentActivity implements FeedsFragment.OnFe } else { - LoginRequest ar = new LoginRequest(); - ar.setApi(m_prefs.getString("ttrss_url", null)); - ar.setTrustAny(m_prefs.getBoolean("ssl_trust_any", false)); + LoginRequest ar = new LoginRequest(getApplicationContext()); + //ar.setApi(m_prefs.getString("ttrss_url", null)); + //ar.setTrustAny(m_prefs.getBoolean("ssl_trust_any", false)); HashMap map = new HashMap() { {