diff --git a/app/src/main/java/org/mian/gitnex/activities/AddNewAccountActivity.java b/app/src/main/java/org/mian/gitnex/activities/AddNewAccountActivity.java index e8d120f1..68ad891d 100644 --- a/app/src/main/java/org/mian/gitnex/activities/AddNewAccountActivity.java +++ b/app/src/main/java/org/mian/gitnex/activities/AddNewAccountActivity.java @@ -130,7 +130,7 @@ public class AddNewAccountActivity extends BaseActivity { private void versionCheck(final String instanceUrl, final String loginToken) { - Call callVersion = RetrofitClient.getApiInterface(ctx, instanceUrl, "token " + loginToken).getVersion(); + Call 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 call = RetrofitClient.getApiInterface(ctx, instanceUrl, "token " + loginToken).userGetCurrent(); + Call call = RetrofitClient.getApiInterface(ctx, instanceUrl, "token " + loginToken, null).userGetCurrent(); call.enqueue(new Callback<>() { diff --git a/app/src/main/java/org/mian/gitnex/activities/LoginActivity.java b/app/src/main/java/org/mian/gitnex/activities/LoginActivity.java index b1446112..aa1fab70 100644 --- a/app/src/main/java/org/mian/gitnex/activities/LoginActivity.java +++ b/app/src/main/java/org/mian/gitnex/activities/LoginActivity.java @@ -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 call = RetrofitClient.getApiInterface(ctx, instanceUrl.toString(), "token " + loginToken).userGetCurrent(); + Call call = RetrofitClient.getApiInterface(ctx, instanceUrl.toString(), "token " + loginToken, null).userGetCurrent(); call.enqueue(new Callback<>() { @@ -407,11 +407,11 @@ public class LoginActivity extends BaseActivity { Call> 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 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 call = RetrofitClient.getApiInterface(ctx, instanceUrl.toString(), "token " + newToken.getSha1()).userGetCurrent(); + Call call = RetrofitClient.getApiInterface(ctx, instanceUrl.toString(), "token " + newToken.getSha1(), null).userGetCurrent(); call.enqueue(new Callback<>() { diff --git a/app/src/main/java/org/mian/gitnex/clients/PicassoService.java b/app/src/main/java/org/mian/gitnex/clients/PicassoService.java index 9374da6d..fce5157d 100644 --- a/app/src/main/java/org/mian/gitnex/clients/PicassoService.java +++ b/app/src/main/java/org/mian/gitnex/clients/PicassoService.java @@ -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 { diff --git a/app/src/main/java/org/mian/gitnex/clients/RetrofitClient.java b/app/src/main/java/org/mian/gitnex/clients/RetrofitClient.java index 18c5c833..99410a7e 100644 --- a/app/src/main/java/org/mian/gitnex/clients/RetrofitClient.java +++ b/app/src/main/java/org/mian/gitnex/clients/RetrofitClient.java @@ -54,7 +54,7 @@ public class RetrofitClient { private static final Map apiInterfaces = new ConcurrentHashMap<>(); private static final Map 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; diff --git a/app/src/main/java/org/mian/gitnex/helpers/contexts/AccountContext.java b/app/src/main/java/org/mian/gitnex/helpers/contexts/AccountContext.java index f75ca7ba..5c2dea42 100644 --- a/app/src/main/java/org/mian/gitnex/helpers/contexts/AccountContext.java +++ b/app/src/main/java/org/mian/gitnex/helpers/contexts/AccountContext.java @@ -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()); + } + } diff --git a/app/src/main/java/org/mian/gitnex/notifications/NotificationsWorker.java b/app/src/main/java/org/mian/gitnex/notifications/NotificationsWorker.java index 13a0024b..74035c84 100644 --- a/app/src/main/java/org/mian/gitnex/notifications/NotificationsWorker.java +++ b/app/src/main/java/org/mian/gitnex/notifications/NotificationsWorker.java @@ -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> 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);