From 800c075bd168b504b3a4f4f28b3e4ee39d576c9a Mon Sep 17 00:00:00 2001
From: davigonz <dgonzalez@solidgear.es>
Date: Tue, 4 Sep 2018 16:44:58 +0200
Subject: [PATCH] Fix renewal of OAuth2 token

---
 .../android/lib/common/OwnCloudClient.java     | 18 +++---------------
 .../lib/common/OwnCloudClientFactory.java      |  3 ++-
 .../android/lib/common/http/HttpClient.java    |  4 ++++
 3 files changed, 9 insertions(+), 16 deletions(-)

diff --git a/src/com/owncloud/android/lib/common/OwnCloudClient.java b/src/com/owncloud/android/lib/common/OwnCloudClient.java
index 79d5f4d7..fa129688 100644
--- a/src/com/owncloud/android/lib/common/OwnCloudClient.java
+++ b/src/com/owncloud/android/lib/common/OwnCloudClient.java
@@ -27,7 +27,6 @@ package com.owncloud.android.lib.common;
 
 import android.accounts.AccountManager;
 import android.accounts.AccountsException;
-import android.content.Context;
 import android.net.Uri;
 
 import com.owncloud.android.lib.common.authentication.OwnCloudCredentials;
@@ -65,18 +64,11 @@ public class OwnCloudClient extends HttpClient {
     private static final String PARAM_PROTOCOL_VERSION = "http.protocol.version";
 
     private static byte[] sExhaustBuffer = new byte[1024];
-
     private static int sIntanceCounter = 0;
     private OwnCloudCredentials mCredentials = null;
     private int mInstanceNumber = 0;
-
     private Uri mBaseUri;
-
     private OwnCloudVersion mVersion = null;
-
-    /// next too attributes are a very ugly dependency, added to grant silent retry of OAuth token when needed ;
-    /// see #shouldInvalidateCredentials and #invalidateCredentials for more details
-    private Context mContext;
     private OwnCloudAccount mAccount;
 
     /**
@@ -344,10 +336,6 @@ public class OwnCloudClient extends HttpClient {
         return mVersion;
     }
 
-    public Context getContext() {
-        return mContext;
-    }
-
     public void setAccount(OwnCloudAccount account) {
         this.mAccount = account;
     }
@@ -378,7 +366,7 @@ public class OwnCloudClient extends HttpClient {
                         repeatCounter < MAX_REPEAT_COUNT_WITH_FRESH_CREDENTIALS) {
 
                     try {
-                        mAccount.loadCredentials(mContext);
+                        mAccount.loadCredentials(getContext());
                         // if mAccount.getCredentials().length() == 0 --> refresh failed
                         setCredentials(mAccount.getCredentials());
                         credentialsWereRefreshed = true;
@@ -421,7 +409,7 @@ public class OwnCloudClient extends HttpClient {
                 !(mCredentials instanceof OwnCloudCredentialsFactory.OwnCloudAnonymousCredentials));
 
         // test if have all the needed to effectively invalidate ...
-        should &= (mAccount != null && mAccount.getSavedAccount() != null && mContext != null);
+        should &= (mAccount != null && mAccount.getSavedAccount() != null && getContext() != null);
 
         return should;
     }
@@ -435,7 +423,7 @@ public class OwnCloudClient extends HttpClient {
      * @return                  'True' if invalidation was successful, 'false' otherwise.
      */
     private boolean invalidateAccountCredentials() {
-        AccountManager am = AccountManager.get(mContext);
+        AccountManager am = AccountManager.get(getContext());
         am.invalidateAuthToken(
                 mAccount.getSavedAccount().type,
                 mCredentials.getAuthToken()
diff --git a/src/com/owncloud/android/lib/common/OwnCloudClientFactory.java b/src/com/owncloud/android/lib/common/OwnCloudClientFactory.java
index 94a0c51d..0cbb56bb 100644
--- a/src/com/owncloud/android/lib/common/OwnCloudClientFactory.java
+++ b/src/com/owncloud/android/lib/common/OwnCloudClientFactory.java
@@ -154,7 +154,8 @@ public class OwnCloudClientFactory {
      * @param context   Android context where the OwnCloudClient is being created.
      * @return          A OwnCloudClient object ready to be used
      */
-    public static OwnCloudClient createOwnCloudClient(Uri uri, Context context, boolean followRedirects) {
+    public static OwnCloudClient createOwnCloudClient(Uri uri, Context context,
+                                                      boolean followRedirects) {
         OwnCloudClient client = new OwnCloudClient(uri);
 
         client.setDefaultTimeouts(DEFAULT_DATA_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT);
diff --git a/src/com/owncloud/android/lib/common/http/HttpClient.java b/src/com/owncloud/android/lib/common/http/HttpClient.java
index 1fa11633..e03906bb 100644
--- a/src/com/owncloud/android/lib/common/http/HttpClient.java
+++ b/src/com/owncloud/android/lib/common/http/HttpClient.java
@@ -59,6 +59,10 @@ public class HttpClient {
         sContext = context;
     }
 
+    public Context getContext() {
+        return sContext;
+    }
+
     public static OkHttpClient getOkHttpClient() {
         if (sOkHttpClient == null) {
             try {