cleaner preference handling in ApiRequest
make json data debugging optional
This commit is contained in:
parent
b5af258ee7
commit
16fd6cebd7
@ -1,8 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="org.fox.ttrss"
|
||||
android:versionCode="12"
|
||||
android:versionName="0.1.11">
|
||||
android:versionCode="13"
|
||||
android:versionName="0.1.12">
|
||||
<uses-sdk android:minSdkVersion="8" />
|
||||
<!-- <supports-screens android:smallScreens="false" android:normalScreens="false" /> -->
|
||||
|
||||
|
@ -9,6 +9,7 @@
|
||||
<string name="login_login">Log in</string>
|
||||
<string name="logout">Log out</string>
|
||||
<string name="login">Login</string>
|
||||
<string name="debugging">Debugging</string>
|
||||
<string name="password">Password</string>
|
||||
<string name="default_url">http://example.domain/tt-rss/</string>
|
||||
<string name="authentication">Authentication</string>
|
||||
@ -20,6 +21,7 @@
|
||||
<string name="theme_dark">Dark</string>
|
||||
<string name="preferences">Preferences</string>
|
||||
<string name="theme_light">Light</string>
|
||||
<string name="connection">Connection</string>
|
||||
<string name="login_api_disabled">Login failed: API disabled.</string>
|
||||
<string name="login_no_data">Login failed: no data received.</string>
|
||||
<string name="login_wrong_password">Login failed: username or password incorrect.</string>
|
||||
@ -42,4 +44,5 @@
|
||||
<string name="error_no_feeds">No feeds to display.</string>
|
||||
<string name="error_invalid_object">Error: invalid object received.</string>
|
||||
<string name="blank"></string>
|
||||
<string name="transport_debugging">Log sent and received data</string>
|
||||
</resources>
|
||||
|
@ -3,14 +3,13 @@
|
||||
<PreferenceScreen
|
||||
xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<PreferenceCategory android:title="@string/authentication">
|
||||
<PreferenceCategory android:title="@string/connection">
|
||||
|
||||
<EditTextPreference android:title="@string/login" android:key="login" android:singleLine="true"></EditTextPreference>
|
||||
<EditTextPreference android:title="@string/password" android:key="password" android:singleLine="true" android:password="true"></EditTextPreference>
|
||||
<EditTextPreference android:key="ttrss_url" android:title="@string/ttrss_url" android:singleLine="true" textUri="true" android:hint="@string/default_url"></EditTextPreference>
|
||||
<CheckBoxPreference android:defaultValue="false" android:title="@string/ssl_trust_any" android:key="ssl_trust_any" />
|
||||
|
||||
</PreferenceCategory>
|
||||
</PreferenceCategory>
|
||||
|
||||
<PreferenceCategory android:title="@string/look_and_feel">
|
||||
<ListPreference
|
||||
@ -22,5 +21,9 @@
|
||||
|
||||
<CheckBoxPreference android:title="@string/sort_feeds_by_unread" android:key="sort_feeds_by_unread"/>
|
||||
</PreferenceCategory>
|
||||
|
||||
|
||||
<PreferenceCategory android:title="@string/debugging">
|
||||
<CheckBoxPreference android:defaultValue="false" android:title="@string/transport_debugging" android:key="transport_debugging" />
|
||||
</PreferenceCategory>
|
||||
|
||||
</PreferenceScreen>
|
@ -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<HashMap<String,String>, 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<HashMap<String,String>, Integer, JsonE
|
||||
|
||||
String requestStr = gson.toJson(new HashMap<String,String>(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<HashMap<String,String>, Integer, JsonE
|
||||
response += s;
|
||||
}
|
||||
|
||||
Log.d(TAG, "<<< " + response);
|
||||
if (m_transportDebugging) Log.d(TAG, "<<< " + response);
|
||||
|
||||
JsonParser parser = new JsonParser();
|
||||
|
||||
|
@ -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<String,String> map = new HashMap<String,String>() {
|
||||
{
|
||||
@ -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<List<Feed>>() {}.getType();
|
||||
final List<Feed> 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<List<Feed>>() {}.getType();
|
||||
final List<Feed> 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<Feed> {
|
||||
private ArrayList<Feed> items;
|
||||
|
||||
|
@ -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();
|
||||
|
||||
|
@ -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<String,String> map = new HashMap<String,String>() {
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user