From 48ee8380ac5d26fb1d292e9c9e93be6595c7eab4 Mon Sep 17 00:00:00 2001 From: tom79 Date: Mon, 2 Dec 2019 18:53:33 +0100 Subject: [PATCH] Fix timeline --- .../asynctasks/RetrieveFeedsAsyncTask.java | 30 +++++++++++++++++-- .../java/app/fedilab/android/client/API.java | 16 +++++----- .../android/client/HttpsConnection.java | 6 ++-- .../fragments/DisplayStatusFragment.java | 2 +- 4 files changed, 41 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/asynctasks/RetrieveFeedsAsyncTask.java b/app/src/main/java/app/fedilab/android/asynctasks/RetrieveFeedsAsyncTask.java index 53a885396..72658d5b5 100644 --- a/app/src/main/java/app/fedilab/android/asynctasks/RetrieveFeedsAsyncTask.java +++ b/app/src/main/java/app/fedilab/android/asynctasks/RetrieveFeedsAsyncTask.java @@ -32,6 +32,7 @@ import app.fedilab.android.client.Entities.Results; import app.fedilab.android.client.Entities.RetrieveFeedsParam; import app.fedilab.android.client.GNUAPI; import app.fedilab.android.client.PeertubeAPI; +import app.fedilab.android.fragments.DisplayStatusFragment; import app.fedilab.android.helper.FilterToots; import app.fedilab.android.interfaces.OnRetrieveFeedsInterface; import app.fedilab.android.sqlite.InstancesDAO; @@ -66,6 +67,7 @@ public class RetrieveFeedsAsyncTask extends AsyncTask { private int timelineId; private String currentfilter; private String social; + private boolean fromCahe; public RetrieveFeedsAsyncTask(Context context, FilterToots filterToots, String max_id, OnRetrieveFeedsInterface onRetrieveFeedsInterface) { this.contextReference = new WeakReference<>(context); @@ -73,16 +75,26 @@ public class RetrieveFeedsAsyncTask extends AsyncTask { this.max_id = max_id; this.listener = onRetrieveFeedsInterface; this.filterToots = filterToots; + this.fromCahe = false; } - public RetrieveFeedsAsyncTask(Context context, Type action, String max_id, OnRetrieveFeedsInterface onRetrieveFeedsInterface) { this.contextReference = new WeakReference<>(context); this.action = action; this.max_id = max_id; this.listener = onRetrieveFeedsInterface; + this.fromCahe = false; } + public RetrieveFeedsAsyncTask(Context context, Type action, String max_id, boolean fromCahe, OnRetrieveFeedsInterface onRetrieveFeedsInterface) { + this.contextReference = new WeakReference<>(context); + this.action = action; + this.max_id = max_id; + this.listener = onRetrieveFeedsInterface; + this.fromCahe = fromCahe; + } + + public RetrieveFeedsAsyncTask(Context context, Type action, String instanceName, String max_id, OnRetrieveFeedsInterface onRetrieveFeedsInterface) { this.contextReference = new WeakReference<>(context); @@ -90,6 +102,7 @@ public class RetrieveFeedsAsyncTask extends AsyncTask { this.max_id = max_id; this.listener = onRetrieveFeedsInterface; this.instanceName = instanceName; + this.fromCahe = false; } public RetrieveFeedsAsyncTask(Context context, Type action, int timelineId, String max_id, OnRetrieveFeedsInterface onRetrieveFeedsInterface) { @@ -98,6 +111,7 @@ public class RetrieveFeedsAsyncTask extends AsyncTask { this.max_id = max_id; this.listener = onRetrieveFeedsInterface; this.timelineId = timelineId; + this.fromCahe = false; } public RetrieveFeedsAsyncTask(Context context, Type action, String targetedID, String max_id, boolean showMediaOnly, boolean showPinned, OnRetrieveFeedsInterface onRetrieveFeedsInterface) { @@ -108,6 +122,7 @@ public class RetrieveFeedsAsyncTask extends AsyncTask { this.targetedID = targetedID; this.showMediaOnly = showMediaOnly; this.showPinned = showPinned; + this.fromCahe = false; } public RetrieveFeedsAsyncTask(Context context, Type action, String targetedID, String max_id, boolean showMediaOnly, boolean showPinned, boolean showReply, OnRetrieveFeedsInterface onRetrieveFeedsInterface) { @@ -119,6 +134,7 @@ public class RetrieveFeedsAsyncTask extends AsyncTask { this.showMediaOnly = showMediaOnly; this.showPinned = showPinned; this.showReply = showReply; + this.fromCahe = false; } public RetrieveFeedsAsyncTask(Context context, Type action, String tag, String targetedID, String max_id, OnRetrieveFeedsInterface onRetrieveFeedsInterface) { @@ -128,6 +144,7 @@ public class RetrieveFeedsAsyncTask extends AsyncTask { this.listener = onRetrieveFeedsInterface; this.targetedID = targetedID; this.tag = tag; + this.fromCahe = false; } public RetrieveFeedsAsyncTask(Context context, String remoteInstance, String name, String max_id, OnRetrieveFeedsInterface onRetrieveFeedsInterface) { @@ -137,6 +154,7 @@ public class RetrieveFeedsAsyncTask extends AsyncTask { this.listener = onRetrieveFeedsInterface; this.name = name; this.action = Type.REMOTE_INSTANCE; + this.fromCahe = false; } public RetrieveFeedsAsyncTask(Context context, RetrieveFeedsParam retrieveFeedsParam, OnRetrieveFeedsInterface onRetrieveFeedsInterface) { @@ -154,8 +172,12 @@ public class RetrieveFeedsAsyncTask extends AsyncTask { this.social = retrieveFeedsParam.getSocial(); this.instanceName = retrieveFeedsParam.getInstanceName(); this.remoteInstance = retrieveFeedsParam.getRemoteInstance(); + this.fromCahe = false; } + + + @Override protected Void doInBackground(Void... params) { API api = new API(this.contextReference.get()); @@ -164,7 +186,11 @@ public class RetrieveFeedsAsyncTask extends AsyncTask { return null; switch (action) { case HOME: - apiResponse = api.getHomeTimelineCache(max_id); + if (this.fromCahe) { + apiResponse = api.getHomeTimelineCache(max_id); + } else { + apiResponse = api.getHomeTimeline(max_id); + } break; case LOCAL: apiResponse = api.getPublicTimeline(true, max_id); diff --git a/app/src/main/java/app/fedilab/android/client/API.java b/app/src/main/java/app/fedilab/android/client/API.java index 3b5b7651b..af085cf0d 100644 --- a/app/src/main/java/app/fedilab/android/client/API.java +++ b/app/src/main/java/app/fedilab/android/client/API.java @@ -19,6 +19,7 @@ import android.content.Intent; import android.content.SharedPreferences; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; +import android.util.Log; import androidx.localbroadcastmanager.content.LocalBroadcastManager; @@ -33,6 +34,7 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; +import java.net.UnknownHostException; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.text.ParseException; @@ -2906,15 +2908,13 @@ public class API { } else { statuses = parseStatuses(context, new JSONArray(response)); } - } catch (HttpsConnection.HttpsConnectionException e) { + } catch (UnknownHostException e){ + if (since_id == null){ + getHomeTimelineCache(max_id); + } + } catch(HttpsConnection.HttpsConnectionException e) { setError(e.getStatusCode(), e); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } catch (KeyManagementException e) { - e.printStackTrace(); - } catch (JSONException e) { + } catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) { e.printStackTrace(); } if (apiResponse == null) diff --git a/app/src/main/java/app/fedilab/android/client/HttpsConnection.java b/app/src/main/java/app/fedilab/android/client/HttpsConnection.java index 9c7afb7a5..5bb1134e5 100644 --- a/app/src/main/java/app/fedilab/android/client/HttpsConnection.java +++ b/app/src/main/java/app/fedilab/android/client/HttpsConnection.java @@ -70,6 +70,7 @@ import app.fedilab.android.client.Entities.Error; import app.fedilab.android.helper.FileNameCleaner; import app.fedilab.android.helper.Helper; import app.fedilab.android.interfaces.OnDownloadInterface; +import okhttp3.Cache; import okhttp3.HttpUrl; import okhttp3.OkHttpClient; import okhttp3.Request; @@ -95,6 +96,7 @@ public class HttpsConnection { private Proxy proxy; private String instance; private String USER_AGENT; + private int cacheSize = 30*1024*1024; public HttpsConnection(Context context, String instance) { this.instance = instance; @@ -177,7 +179,7 @@ public class HttpsConnection { URL url = new URL(urlConnection + "?" + postData); if (Build.VERSION.SDK_INT >= 21) { - OkHttpClient.Builder builder = new OkHttpClient.Builder().connectTimeout(timeout, TimeUnit.SECONDS); + OkHttpClient.Builder builder = new OkHttpClient.Builder().connectTimeout(timeout, TimeUnit.SECONDS).cache(new Cache(context.getCacheDir(), cacheSize)); if (proxy != null) { builder.proxy(proxy); } @@ -297,7 +299,7 @@ public class HttpsConnection { public String get(String urlConnection) throws IOException, NoSuchAlgorithmException, KeyManagementException, HttpsConnectionException { if (Build.VERSION.SDK_INT >= 21) { - OkHttpClient.Builder builder = new OkHttpClient.Builder().connectTimeout(30, TimeUnit.SECONDS); + OkHttpClient.Builder builder = new OkHttpClient.Builder().connectTimeout(30, TimeUnit.SECONDS).cache(new Cache(context.getCacheDir(), cacheSize)); if (proxy != null) { builder.proxy(proxy); } diff --git a/app/src/main/java/app/fedilab/android/fragments/DisplayStatusFragment.java b/app/src/main/java/app/fedilab/android/fragments/DisplayStatusFragment.java index 6cf27fc1f..079ef4f0e 100644 --- a/app/src/main/java/app/fedilab/android/fragments/DisplayStatusFragment.java +++ b/app/src/main/java/app/fedilab/android/fragments/DisplayStatusFragment.java @@ -1076,7 +1076,7 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn if (context instanceof BaseMainActivity) { boolean remember_position_home = sharedpreferences.getBoolean(Helper.SET_REMEMBER_POSITION_HOME, true); if (remember_position_home) - asyncTask = new RetrieveFeedsAsyncTask(context, type, initialBookMark, DisplayStatusFragment.this).execute(); + asyncTask = new RetrieveFeedsAsyncTask(context, type, initialBookMark, true,DisplayStatusFragment.this).execute(); else asyncTask = new RetrieveFeedsAsyncTask(context, type, null, DisplayStatusFragment.this).execute(); }