Add proper offline/online request interceptors

This commit is contained in:
CappielloAntonio 2021-08-31 09:34:53 +02:00
parent ac584974c6
commit 55b4d97195
9 changed files with 53 additions and 21 deletions

View file

@ -72,10 +72,12 @@ public class AlbumSongListClient {
}
private OkHttpClient getOkHttpClient() {
CacheUtil cacheUtil = new CacheUtil(context);
return new OkHttpClient.Builder()
.addInterceptor(getHttpLoggingInterceptor())
.addInterceptor(CacheUtil.offlineInterceptor)
.addNetworkInterceptor(CacheUtil.onlineInterceptor)
.addInterceptor(cacheUtil.offlineInterceptor)
.addNetworkInterceptor(cacheUtil.onlineInterceptor)
.cache(getCache())
.build();
}

View file

@ -122,10 +122,12 @@ public class BrowsingClient {
}
private OkHttpClient getOkHttpClient() {
CacheUtil cacheUtil = new CacheUtil(context);
return new OkHttpClient.Builder()
.addInterceptor(getHttpLoggingInterceptor())
.addInterceptor(CacheUtil.offlineInterceptor)
.addNetworkInterceptor(CacheUtil.onlineInterceptor)
.addInterceptor(cacheUtil.offlineInterceptor)
.addNetworkInterceptor(cacheUtil.onlineInterceptor)
.cache(getCache())
.build();
}

View file

@ -57,10 +57,12 @@ public class MediaAnnotationClient {
}
private OkHttpClient getOkHttpClient() {
CacheUtil cacheUtil = new CacheUtil(context);
return new OkHttpClient.Builder()
.addInterceptor(getHttpLoggingInterceptor())
.addInterceptor(CacheUtil.offlineInterceptor)
.addNetworkInterceptor(CacheUtil.onlineInterceptor)
.addInterceptor(cacheUtil.offlineInterceptor)
.addNetworkInterceptor(cacheUtil.onlineInterceptor)
.cache(getCache())
.build();
}

View file

@ -48,10 +48,12 @@ public class MediaLibraryScanningClient {
}
private OkHttpClient getOkHttpClient() {
CacheUtil cacheUtil = new CacheUtil(context);
return new OkHttpClient.Builder()
.addInterceptor(getHttpLoggingInterceptor())
.addInterceptor(CacheUtil.offlineInterceptor)
.addNetworkInterceptor(CacheUtil.onlineInterceptor)
.addInterceptor(cacheUtil.offlineInterceptor)
.addNetworkInterceptor(cacheUtil.onlineInterceptor)
.cache(getCache())
.build();
}

View file

@ -47,10 +47,12 @@ public class MediaRetrievalClient {
}
private OkHttpClient getOkHttpClient() {
CacheUtil cacheUtil = new CacheUtil(context);
return new OkHttpClient.Builder()
.addInterceptor(getHttpLoggingInterceptor())
.addInterceptor(CacheUtil.offlineInterceptor)
.addNetworkInterceptor(CacheUtil.onlineInterceptor)
.addInterceptor(cacheUtil.offlineInterceptor)
.addNetworkInterceptor(cacheUtil.onlineInterceptor)
.cache(getCache())
.build();
}

View file

@ -64,10 +64,12 @@ public class PlaylistClient {
}
private OkHttpClient getOkHttpClient() {
CacheUtil cacheUtil = new CacheUtil(context);
return new OkHttpClient.Builder()
.addInterceptor(getHttpLoggingInterceptor())
.addInterceptor(CacheUtil.offlineInterceptor)
.addNetworkInterceptor(CacheUtil.onlineInterceptor)
.addInterceptor(cacheUtil.offlineInterceptor)
.addNetworkInterceptor(cacheUtil.onlineInterceptor)
.cache(getCache())
.build();
}

View file

@ -47,10 +47,12 @@ public class SearchingClient {
}
private OkHttpClient getOkHttpClient() {
CacheUtil cacheUtil = new CacheUtil(context);
return new OkHttpClient.Builder()
.addInterceptor(getHttpLoggingInterceptor())
.addInterceptor(CacheUtil.offlineInterceptor)
.addNetworkInterceptor(CacheUtil.onlineInterceptor)
.addInterceptor(cacheUtil.offlineInterceptor)
.addNetworkInterceptor(cacheUtil.onlineInterceptor)
.cache(getCache())
.build();
}

View file

@ -47,10 +47,12 @@ public class SystemClient {
}
private OkHttpClient getOkHttpClient() {
CacheUtil cacheUtil = new CacheUtil(context);
return new OkHttpClient.Builder()
.addInterceptor(getHttpLoggingInterceptor())
.addInterceptor(CacheUtil.offlineInterceptor)
.addNetworkInterceptor(CacheUtil.onlineInterceptor)
.addInterceptor(cacheUtil.offlineInterceptor)
.addNetworkInterceptor(cacheUtil.onlineInterceptor)
.cache(getCache())
.build();
}

View file

@ -1,22 +1,32 @@
package com.cappielloantonio.play.subsonic.utils;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import okhttp3.Interceptor;
import okhttp3.Request;
public class CacheUtil {
public static Interceptor onlineInterceptor = chain -> {
private final Context context;
public CacheUtil(Context context) {
this.context = context;
}
public Interceptor onlineInterceptor = chain -> {
okhttp3.Response response = chain.proceed(chain.request());
int maxAge = 60;
int maxAge = 60; // 60 seconds
return response.newBuilder()
.header("Cache-Control", "public, max-age=" + maxAge)
.removeHeader("Pragma")
.build();
};
public static Interceptor offlineInterceptor = chain -> {
public Interceptor offlineInterceptor = chain -> {
Request request = chain.request();
if (!true) {
int maxStale = 60 * 60 * 24 * 30;
if (!isConnected()) {
int maxStale = 60 * 60 * 24 * 30; // 30 days (60 seconds * 60 minutes * 24 hours * 30 days)
request = request.newBuilder()
.header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
.removeHeader("Pragma")
@ -24,4 +34,10 @@ public class CacheUtil {
}
return chain.proceed(request);
};
private boolean isConnected(){
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
return (netInfo != null && netInfo.isConnected());
}
}