some changes

This commit is contained in:
Thomas 2020-07-25 11:36:46 +02:00
parent 46c678a3e2
commit abe8812e6e
5 changed files with 32 additions and 32 deletions

View File

@ -317,11 +317,7 @@ public abstract class BaseMainActivity extends BaseActivity
} catch (Exception ignored) {
}
}
if (social == UpdateAccountInfoAsyncTask.SOCIAL.PEERTUBE || social == UpdateAccountInfoAsyncTask.SOCIAL.PIXELFED) {
new Thread(() -> {
new API(BaseMainActivity.this).refreshToken(account);
}).start();
}
//Here, the user is authenticated
appBar = findViewById(R.id.appBar);
Toolbar toolbar = findViewById(R.id.toolbar);
@ -1339,7 +1335,7 @@ public abstract class BaseMainActivity extends BaseActivity
new TimelineCacheDAO(BaseMainActivity.this, db).removeAfterDate(dateString);
});
if (Helper.isLoggedIn(BaseMainActivity.this)) {
new UpdateAccountInfoByIDAsyncTask(BaseMainActivity.this, social, BaseMainActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
new UpdateAccountInfoByIDAsyncTask(BaseMainActivity.this, account, BaseMainActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
mutedAccount = new TempMuteDAO(BaseMainActivity.this, db).getAllTimeMuted(account);
if (social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON || social == UpdateAccountInfoAsyncTask.SOCIAL.PLEROMA) {

View File

@ -153,14 +153,14 @@ public class LoginActivity extends BaseActivity {
JSONObject resobj;
try {
resobj = new JSONObject(response);
String token = resobj.get("access_token").toString();
String token = resobj.getString("access_token");
String refresh_token = null;
if (resobj.has("refresh_token"))
refresh_token = resobj.get("refresh_token").toString();
refresh_token = resobj.getString("refresh_token");
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, token);
editor.apply();
editor.commit();
//Update the account with the token;
new UpdateAccountInfoAsyncTask(LoginActivity.this, token, client_id, client_secret, refresh_token, instance, socialNetwork).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} catch (JSONException e) {
@ -701,7 +701,7 @@ public class LoginActivity extends BaseActivity {
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
public boolean onOptionsItemSelected(@NotNull MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.

View File

@ -178,14 +178,14 @@ public class WebviewConnectActivity extends BaseActivity {
JSONObject resobj;
try {
resobj = new JSONObject(response);
String token = resobj.get("access_token").toString();
String token = resobj.getString("access_token");
String refresh_token = null;
if (resobj.has("refresh_token"))
refresh_token = resobj.get("refresh_token").toString();
refresh_token = resobj.getString("refresh_token");
SharedPreferences sharedpreferences1 = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences1.edit();
editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, token);
editor.apply();
editor.commit();
//Update the account with the token;
new UpdateAccountInfoAsyncTask(WebviewConnectActivity.this, token, clientId, clientSecret, refresh_token, instance, social).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} catch (JSONException ignored) {

View File

@ -15,7 +15,6 @@
package app.fedilab.android.asynctasks;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
@ -33,6 +32,8 @@ import app.fedilab.android.sqlite.AccountDAO;
import app.fedilab.android.sqlite.CustomEmojiDAO;
import app.fedilab.android.sqlite.Sqlite;
import static app.fedilab.android.activities.BaseMainActivity.social;
/**
* Created by Thomas on 17/05/2017.
* Manage the synchronization with the authenticated account and update the db not
@ -43,24 +44,25 @@ public class UpdateAccountInfoByIDAsyncTask extends AsyncTask<Void, Void, Void>
private OnUpdateAccountInfoInterface listener;
private WeakReference<Context> contextReference;
private UpdateAccountInfoAsyncTask.SOCIAL social;
private Account accountOld;
public UpdateAccountInfoByIDAsyncTask(Context context, UpdateAccountInfoAsyncTask.SOCIAL social, OnUpdateAccountInfoInterface onUpdateAccountInfoInterface) {
public UpdateAccountInfoByIDAsyncTask(Context context, Account account, OnUpdateAccountInfoInterface onUpdateAccountInfoInterface) {
this.contextReference = new WeakReference<>(context);
this.listener = onUpdateAccountInfoInterface;
this.social = social;
this.accountOld = account;
}
@Override
protected Void doInBackground(Void... params) {
SharedPreferences sharedpreferences = this.contextReference.get().getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
String instance = sharedpreferences.getString(Helper.PREF_INSTANCE, null);
Account account = null;
if (social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON || social == UpdateAccountInfoAsyncTask.SOCIAL.PLEROMA || social == UpdateAccountInfoAsyncTask.SOCIAL.PIXELFED)
if (social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON || social == UpdateAccountInfoAsyncTask.SOCIAL.PLEROMA || social == UpdateAccountInfoAsyncTask.SOCIAL.PIXELFED) {
account = new API(this.contextReference.get()).verifyCredentials();
else if (social == UpdateAccountInfoAsyncTask.SOCIAL.PEERTUBE) {
if( social == UpdateAccountInfoAsyncTask.SOCIAL.PIXELFED){
account.setSocial("PIXELFED");
}
}else if (social == UpdateAccountInfoAsyncTask.SOCIAL.PEERTUBE) {
account = new PeertubeAPI(this.contextReference.get()).verifyCredentials();
account.setSocial("PEERTUBE");
} else if (social == UpdateAccountInfoAsyncTask.SOCIAL.GNU || social == UpdateAccountInfoAsyncTask.SOCIAL.FRIENDICA) {
@ -70,15 +72,17 @@ public class UpdateAccountInfoByIDAsyncTask extends AsyncTask<Void, Void, Void>
return null;
account.setInstance(Helper.getLiveInstance(contextReference.get()));
SQLiteDatabase db = Sqlite.getInstance(this.contextReference.get().getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
boolean userExists = new AccountDAO(this.contextReference.get(), db).userExist(account);
if (userExists) {
Account accountDb = new AccountDAO(this.contextReference.get(), db).getUniqAccount(userId, instance);
if (accountOld != null) {
account.setInstance(accountOld.getInstance());
account.setToken(accountOld.getToken());
account.setRefresh_token(accountOld.getRefresh_token());
account.setClient_id(accountOld.getClient_id());
account.setClient_secret(accountOld.getClient_secret());
new AccountDAO(this.contextReference.get(), db).updateAccountCredential(account);
}
if (accountDb != null) {
account.setInstance(accountDb.getInstance());
account.setToken(accountDb.getToken());
new AccountDAO(this.contextReference.get(), db).updateAccountCredential(account);
}
if (social == UpdateAccountInfoAsyncTask.SOCIAL.PEERTUBE || social == UpdateAccountInfoAsyncTask.SOCIAL.PIXELFED) {
new API(contextReference.get()).refreshToken(account);
}
if (social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON || social == UpdateAccountInfoAsyncTask.SOCIAL.PLEROMA) {
try {

View File

@ -2552,9 +2552,9 @@ public class API {
try {
String response;
if (account.getSocial().compareTo("PEERTUBE") == 0) {
response = new HttpsConnection(context, instance).post(getAbsoluteUrl("/users/token"), 60, params, account.getToken());
response = new HttpsConnection(context, instance).post(getAbsoluteUrl("/users/token"), 60, params, null);
} else {
response = new HttpsConnection(context, instance).post("https://" + instance + "/oauth/token", 60, params, account.getToken());
response = new HttpsConnection(context, instance).post("https://" + instance + "/oauth/token", 60, params, null);
}
JSONObject resobj = new JSONObject(response);
String newToken = resobj.getString("access_token");