Use different cache directories for different accounts (#1117)

To fix cache issues if you have multiple accounts.

This will make the caches available before updating unusable because a different directory is used.

Co-authored-by: qwerty287 <ndev@web.de>
Co-authored-by: M M Arif <mmarif@noreply.codeberg.org>
Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/1117
Reviewed-by: 6543 <6543@noreply.codeberg.org>
Co-authored-by: qwerty287 <qwerty287@noreply.codeberg.org>
Co-committed-by: qwerty287 <qwerty287@noreply.codeberg.org>
This commit is contained in:
qwerty287 2022-04-30 18:55:20 +02:00 committed by 6543
parent e8c223ba1d
commit bdfbbf31ef
6 changed files with 41 additions and 26 deletions

View File

@ -130,7 +130,7 @@ public class AddNewAccountActivity extends BaseActivity {
private void versionCheck(final String instanceUrl, final String loginToken) {
Call<ServerVersion> callVersion = RetrofitClient.getApiInterface(ctx, instanceUrl, "token " + loginToken).getVersion();
Call<ServerVersion> callVersion = RetrofitClient.getApiInterface(ctx, instanceUrl, "token " + loginToken, null).getVersion();
callVersion.enqueue(new Callback<>() {
@Override
@ -227,7 +227,7 @@ public class AddNewAccountActivity extends BaseActivity {
private void setupNewAccountWithToken(String instanceUrl, final String loginToken) {
Call<User> call = RetrofitClient.getApiInterface(ctx, instanceUrl, "token " + loginToken).userGetCurrent();
Call<User> call = RetrofitClient.getApiInterface(ctx, instanceUrl, "token " + loginToken, null).userGetCurrent();
call.enqueue(new Callback<>() {

View File

@ -241,7 +241,7 @@ public class LoginActivity extends BaseActivity {
if(!loginToken.equals("")) {
callVersion = RetrofitClient.getApiInterface(ctx, instanceUrl.toString(), "token " + loginToken).getVersion();
callVersion = RetrofitClient.getApiInterface(ctx, instanceUrl.toString(), "token " + loginToken, null).getVersion();
}
else {
@ -249,10 +249,10 @@ public class LoginActivity extends BaseActivity {
if (loginOTP != 0) {
callVersion = RetrofitClient.getApiInterface(ctx, instanceUrl.toString(), credential).getVersion(loginOTP);
callVersion = RetrofitClient.getApiInterface(ctx, instanceUrl.toString(), credential, null).getVersion(loginOTP);
} else {
callVersion = RetrofitClient.getApiInterface(ctx, instanceUrl.toString(), credential).getVersion();
callVersion = RetrofitClient.getApiInterface(ctx, instanceUrl.toString(), credential, null).getVersion();
}
}
@ -340,7 +340,7 @@ public class LoginActivity extends BaseActivity {
private void setupUsingExistingToken(final String loginToken) {
Call<User> call = RetrofitClient.getApiInterface(ctx, instanceUrl.toString(), "token " + loginToken).userGetCurrent();
Call<User> call = RetrofitClient.getApiInterface(ctx, instanceUrl.toString(), "token " + loginToken, null).userGetCurrent();
call.enqueue(new Callback<>() {
@ -407,11 +407,11 @@ public class LoginActivity extends BaseActivity {
Call<List<AccessToken>> call;
if(loginOTP != 0) {
call = RetrofitClient.getApiInterface(ctx, instanceUrl.toString(), credential).userGetTokens(loginOTP, loginUid, null, null);
call = RetrofitClient.getApiInterface(ctx, instanceUrl.toString(), credential, null).userGetTokens(loginOTP, loginUid, null, null);
}
else {
call = RetrofitClient.getApiInterface(ctx, instanceUrl.toString(), credential).userGetTokens(loginUid, null, null);
call = RetrofitClient.getApiInterface(ctx, instanceUrl.toString(), credential, null).userGetTokens(loginUid, null, null);
}
call.enqueue(new Callback<>() {
@ -435,12 +435,12 @@ public class LoginActivity extends BaseActivity {
Call<Void> delToken;
if(loginOTP != 0) {
delToken = RetrofitClient.getApiInterface(ctx, instanceUrl.toString(), credential)
delToken = RetrofitClient.getApiInterface(ctx, instanceUrl.toString(), credential, null)
.userDeleteAccessToken(loginOTP, loginUid, String.valueOf(t.getId()));
}
else {
delToken = RetrofitClient.getApiInterface(ctx, instanceUrl.toString(), credential).userDeleteAccessToken(loginUid, String.valueOf(t.getId()));
delToken = RetrofitClient.getApiInterface(ctx, instanceUrl.toString(), credential, null).userDeleteAccessToken(loginUid, String.valueOf(t.getId()));
}
delToken.enqueue(new Callback<>() {
@ -498,12 +498,12 @@ public class LoginActivity extends BaseActivity {
if(loginOTP != 0) {
callCreateToken = RetrofitClient.getApiInterface(ctx, instanceUrl.toString(), credential)
callCreateToken = RetrofitClient.getApiInterface(ctx, instanceUrl.toString(), credential, null)
.userCreateToken(loginOTP, loginUid, createUserToken);
}
else {
callCreateToken = RetrofitClient.getApiInterface(ctx, instanceUrl.toString(), credential)
callCreateToken = RetrofitClient.getApiInterface(ctx, instanceUrl.toString(), credential, null)
.userCreateToken(loginUid, createUserToken);
}
@ -519,7 +519,7 @@ public class LoginActivity extends BaseActivity {
if(!newToken.getSha1().equals("")) {
Call<User> call = RetrofitClient.getApiInterface(ctx, instanceUrl.toString(), "token " + newToken.getSha1()).userGetCurrent();
Call<User> call = RetrofitClient.getApiInterface(ctx, instanceUrl.toString(), "token " + newToken.getSha1(), null).userGetCurrent();
call.enqueue(new Callback<>() {

View File

@ -4,6 +4,7 @@ import android.content.Context;
import android.util.Log;
import com.squareup.picasso.OkHttp3Downloader;
import com.squareup.picasso.Picasso;
import org.mian.gitnex.activities.BaseActivity;
import org.mian.gitnex.helpers.PicassoCache;
import org.mian.gitnex.helpers.ssl.MemorizingTrustManager;
import java.io.File;
@ -14,7 +15,7 @@ import javax.net.ssl.X509TrustManager;
import okhttp3.OkHttpClient;
/**
* Author opyale
* @author opyale
*/
public class PicassoService {
@ -25,7 +26,7 @@ public class PicassoService {
private PicassoService(Context context) {
cachePath = new File(context.getCacheDir() + "/picasso_cache/");
cachePath = ((BaseActivity) context).getAccount().getPicassoCacheDir(context);
Picasso.Builder builder = new Picasso.Builder(context);
try {

View File

@ -54,7 +54,7 @@ public class RetrofitClient {
private static final Map<String, ApiInterface> apiInterfaces = new ConcurrentHashMap<>();
private static final Map<String, WebApi> webInterfaces = new ConcurrentHashMap<>();
private static Retrofit createRetrofit(Context context, String instanceUrl, boolean cacheEnabled, String token) {
private static Retrofit createRetrofit(Context context, String instanceUrl, boolean cacheEnabled, String token, File cacheFile) {
TinyDB tinyDB = TinyDB.getInstance(context);
@ -76,10 +76,10 @@ public class RetrofitClient {
.sslSocketFactory(sslContext.getSocketFactory(), memorizingTrustManager)
.hostnameVerifier(memorizingTrustManager.wrapHostnameVerifier(HttpsURLConnection.getDefaultHostnameVerifier()));
if(cacheEnabled) {
if(cacheEnabled && cacheFile != null) {
int cacheSize = FilesData.returnOnlyNumberFileSize(tinyDB.getString("cacheSizeStr", context.getString(R.string.cacheSizeDataSelectionSelectedText))) * 1024 * 1024;
Cache cache = new Cache(new File(context.getCacheDir(), "responses"), cacheSize);
Cache cache = new Cache(cacheFile, cacheSize);
okHttpClient.cache(cache).addInterceptor(chain -> {
@ -114,7 +114,8 @@ public class RetrofitClient {
public static ApiInterface getApiInterface(Context context) {
return getApiInterface(context, ((BaseActivity) context).getAccount().getAccount().getInstanceUrl(),
((BaseActivity) context).getAccount().getAuthorization());
((BaseActivity) context).getAccount().getAuthorization(),
((BaseActivity) context).getAccount().getCacheDir(context));
}
public static WebApi getWebInterface(Context context) {
@ -122,18 +123,18 @@ public class RetrofitClient {
String instanceUrl = ((BaseActivity) context).getAccount().getAccount().getInstanceUrl();
instanceUrl = instanceUrl.substring(0, instanceUrl.lastIndexOf("api/v1/"));
return getWebInterface(context, instanceUrl, ((BaseActivity) context).getAccount().getWebAuthorization());
return getWebInterface(context, instanceUrl, ((BaseActivity) context).getAccount().getWebAuthorization(), ((BaseActivity) context).getAccount().getCacheDir(context));
}
public static ApiInterface getApiInterface(Context context, String url, String token) {
public static ApiInterface getApiInterface(Context context, String url, String token, File cacheFile) {
String key = token.hashCode() + "@" + url;
if(!apiInterfaces.containsKey(key)) {
synchronized(RetrofitClient.class) {
if(!apiInterfaces.containsKey(key)) {
ApiInterface apiInterface = createRetrofit(context, url, true, token).create(ApiInterface.class);
ApiInterface apiInterface = createRetrofit(context, url, true, token, cacheFile).create(ApiInterface.class);
apiInterfaces.put(key, apiInterface);
return apiInterface;
@ -145,14 +146,14 @@ public class RetrofitClient {
}
public static WebApi getWebInterface(Context context, String url, String token) {
public static WebApi getWebInterface(Context context, String url, String token, File cacheFile) {
String key = token.hashCode() + "@" + url;
if(!webInterfaces.containsKey(key)) {
synchronized(RetrofitClient.class) {
if(!webInterfaces.containsKey(key)) {
WebApi webInterface = createRetrofit(context, url, false, token).create(WebApi.class);
WebApi webInterface = createRetrofit(context, url, false, token, cacheFile).create(WebApi.class);
webInterfaces.put(key, webInterface);
return webInterface;

View File

@ -5,6 +5,7 @@ import org.gitnex.tea4j.v2.models.User;
import org.mian.gitnex.database.api.UserAccountsApi;
import org.mian.gitnex.database.models.UserAccount;
import org.mian.gitnex.helpers.Version;
import java.io.File;
import java.io.Serializable;
import java.util.Objects;
import okhttp3.Credentials;
@ -71,4 +72,16 @@ public class AccountContext implements Serializable {
userInfo.getFullName() : userInfo.getLogin() : account.getUserName();
}
public File getCacheDir(Context context) {
assert account.getAccountName() != null;
return new File(context.getCacheDir() + "responses", account.getAccountName());
}
public File getPicassoCacheDir(Context context) {
assert account.getAccountName() != null;
return new File(context.getCacheDir() + "/picasso_cache/", account.getAccountName());
}
}

View File

@ -31,7 +31,7 @@ import retrofit2.Call;
import retrofit2.Response;
/**
* Author opyale
* @author opyale
*/
public class NotificationsWorker extends Worker {
@ -98,7 +98,7 @@ public class NotificationsWorker extends Worker {
try {
assert userAccountParameters != null;
Call<List<NotificationThread>> call = RetrofitClient
.getApiInterface(context, userAccount.getInstanceUrl(), userAccount.getToken())
.getApiInterface(context, userAccount.getInstanceUrl(), userAccount.getToken(), null)
.notifyGetList(false, Arrays.asList("unread"), null, new Date(userAccountParameters.get("previousTimestamp")), null,
null, 1);