Refresh OAUTH tokens

Refs #17
This commit is contained in:
M66B
2018-08-27 16:19:56 +00:00
parent 4d7c3e9c88
commit 846a39eb78
43 changed files with 140 additions and 45 deletions

View File

@@ -19,6 +19,8 @@ package eu.faircode.email;
Copyright 2018 by Marcel Bokhorst (M66B)
*/
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
@@ -170,6 +172,30 @@ public class Helper {
}
}
static String refreshToken(Context context, String type, String name, String current) {
try {
AccountManager am = AccountManager.get(context);
Account[] accounts = am.getAccountsByType(type);
for (Account account : accounts)
if (name.equals(account.name)) {
Log.i(Helper.TAG, "Refreshing token");
am.invalidateAuthToken(type, current);
String refreshed = am.blockingGetAuthToken(account, getAuthTokenType(type), true);
Log.i(Helper.TAG, "Refreshed token");
return refreshed;
}
} catch (Throwable ex) {
Log.w(TAG, ex + "\n" + Log.getStackTraceString(ex));
}
return current;
}
static String getAuthTokenType(String type) {
if ("com.google".equals(type))
return "oauth2:https://mail.google.com/";
return null;
}
static boolean isPlayStoreInstall(Context context) {
if (false && BuildConfig.DEBUG)
return true;