2017-05-17 16:19:50 +02:00
|
|
|
/* Copyright 2017 Thomas Schneider
|
|
|
|
*
|
2017-07-10 10:33:24 +02:00
|
|
|
* This file is a part of Mastalab
|
2017-05-17 16:19:50 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
2017-07-10 10:33:24 +02:00
|
|
|
* Mastalab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
2017-05-17 16:19:50 +02:00
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
2017-08-04 11:11:27 +02:00
|
|
|
* You should have received a copy of the GNU General Public License along with Mastalab; if not,
|
2017-05-17 16:19:50 +02:00
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
package fr.gouv.etalab.mastodon.asynctasks;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
import android.os.AsyncTask;
|
2017-10-27 15:09:26 +02:00
|
|
|
|
|
|
|
import java.lang.ref.WeakReference;
|
2019-01-06 14:44:33 +01:00
|
|
|
import java.util.HashMap;
|
2017-10-27 15:09:26 +02:00
|
|
|
|
2017-05-17 16:19:50 +02:00
|
|
|
import fr.gouv.etalab.mastodon.client.API;
|
2017-11-24 07:13:30 +01:00
|
|
|
import fr.gouv.etalab.mastodon.client.APIResponse;
|
2017-05-17 16:19:50 +02:00
|
|
|
import fr.gouv.etalab.mastodon.client.Entities.Account;
|
2017-11-24 07:13:30 +01:00
|
|
|
import fr.gouv.etalab.mastodon.client.Entities.Emojis;
|
2019-01-05 13:00:15 +01:00
|
|
|
import fr.gouv.etalab.mastodon.client.HttpsConnection;
|
2019-01-03 10:23:31 +01:00
|
|
|
import fr.gouv.etalab.mastodon.client.PeertubeAPI;
|
2017-05-17 16:19:50 +02:00
|
|
|
import fr.gouv.etalab.mastodon.helper.Helper;
|
|
|
|
import fr.gouv.etalab.mastodon.interfaces.OnUpdateAccountInfoInterface;
|
|
|
|
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
|
2017-11-24 07:13:30 +01:00
|
|
|
import fr.gouv.etalab.mastodon.sqlite.CustomEmojiDAO;
|
2017-05-17 16:19:50 +02:00
|
|
|
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by Thomas on 17/05/2017.
|
|
|
|
* Manage the synchronization with the authenticated account and update the db not
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class UpdateAccountInfoByIDAsyncTask extends AsyncTask<Void, Void, Void> {
|
|
|
|
|
2017-10-27 15:09:26 +02:00
|
|
|
|
2017-05-17 16:19:50 +02:00
|
|
|
private OnUpdateAccountInfoInterface listener;
|
2017-10-27 15:09:26 +02:00
|
|
|
private WeakReference<Context> contextReference;
|
2019-01-03 10:23:31 +01:00
|
|
|
private UpdateAccountInfoAsyncTask.SOCIAL social;
|
2017-05-17 16:19:50 +02:00
|
|
|
|
2019-01-03 10:23:31 +01:00
|
|
|
public UpdateAccountInfoByIDAsyncTask(Context context, UpdateAccountInfoAsyncTask.SOCIAL social, OnUpdateAccountInfoInterface onUpdateAccountInfoInterface){
|
2017-10-27 15:09:26 +02:00
|
|
|
this.contextReference = new WeakReference<>(context);
|
2017-05-17 16:19:50 +02:00
|
|
|
this.listener = onUpdateAccountInfoInterface;
|
2019-01-03 10:23:31 +01:00
|
|
|
this.social = social;
|
2017-05-17 16:19:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Void doInBackground(Void... params) {
|
|
|
|
|
2017-10-27 15:09:26 +02:00
|
|
|
SharedPreferences sharedpreferences = this.contextReference.get().getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
2017-05-17 16:19:50 +02:00
|
|
|
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
|
2019-01-03 10:23:31 +01:00
|
|
|
Account account = null;
|
|
|
|
if( social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON)
|
|
|
|
account = new API(this.contextReference.get()).getAccount(userId);
|
2019-01-05 13:00:15 +01:00
|
|
|
else if( social == UpdateAccountInfoAsyncTask.SOCIAL.PEERTUBE) {
|
2019-01-12 09:26:18 +01:00
|
|
|
|
2019-01-06 14:44:33 +01:00
|
|
|
try {
|
|
|
|
account = new PeertubeAPI(this.contextReference.get()).verifyCredentials();
|
|
|
|
account.setSocial("PEERTUBE");
|
|
|
|
}catch (HttpsConnection.HttpsConnectionException exception){
|
|
|
|
if(exception.getStatusCode() == 401){
|
|
|
|
SQLiteDatabase db = Sqlite.getInstance(this.contextReference.get(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
|
|
|
String token = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null);
|
|
|
|
account = new AccountDAO(this.contextReference.get(), db).getAccountByToken(token);
|
2019-01-06 14:47:39 +01:00
|
|
|
HashMap<String, String> values = new PeertubeAPI(this.contextReference.get()).refreshToken(account.getClient_id(), account.getClient_secret(), account.getRefresh_token());
|
|
|
|
if( values != null) {
|
|
|
|
String newtoken = values.get("access_token");
|
|
|
|
String refresh_token = values.get("refresh_token");
|
|
|
|
if (newtoken != null)
|
|
|
|
account.setToken(newtoken);
|
|
|
|
if (refresh_token != null)
|
|
|
|
account.setRefresh_token(refresh_token);
|
|
|
|
new AccountDAO(this.contextReference.get(), db).updateAccount(account);
|
|
|
|
}
|
2019-01-06 14:44:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-05 13:00:15 +01:00
|
|
|
}
|
2019-01-03 10:23:31 +01:00
|
|
|
if( account == null)
|
|
|
|
return null;
|
2018-02-09 13:37:10 +01:00
|
|
|
account.setInstance(Helper.getLiveInstance(contextReference.get()));
|
2017-10-27 15:09:26 +02:00
|
|
|
SQLiteDatabase db = Sqlite.getInstance(this.contextReference.get(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
|
|
|
boolean userExists = new AccountDAO(this.contextReference.get(), db).userExist(account);
|
2017-05-17 16:19:50 +02:00
|
|
|
if( userExists) {
|
2017-10-27 15:09:26 +02:00
|
|
|
Account accountDb = new AccountDAO(this.contextReference.get(), db).getAccountByID(userId);
|
2017-05-17 16:19:50 +02:00
|
|
|
if( accountDb != null){
|
|
|
|
account.setInstance(accountDb.getInstance());
|
|
|
|
account.setToken(accountDb.getToken());
|
2017-10-27 15:09:26 +02:00
|
|
|
new AccountDAO(this.contextReference.get(), db).updateAccount(account);
|
2017-05-17 16:19:50 +02:00
|
|
|
}
|
|
|
|
}
|
2019-01-03 10:23:31 +01:00
|
|
|
if( social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON) {
|
|
|
|
try {
|
|
|
|
APIResponse response = new API(contextReference.get()).getCustomEmoji();
|
|
|
|
if (response != null && response.getEmojis() != null && response.getEmojis().size() > 0) {
|
|
|
|
new CustomEmojiDAO(contextReference.get(), db).removeAll();
|
|
|
|
for (Emojis emojis : response.getEmojis()) {
|
|
|
|
new CustomEmojiDAO(contextReference.get(), db).insertEmoji(emojis);
|
|
|
|
}
|
2017-11-24 07:13:30 +01:00
|
|
|
}
|
2019-01-03 10:23:31 +01:00
|
|
|
} catch (Exception ignored) {
|
2017-11-24 07:13:30 +01:00
|
|
|
}
|
2019-01-03 10:23:31 +01:00
|
|
|
}
|
2017-05-17 16:19:50 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Void result) {
|
|
|
|
listener.onUpdateAccountInfo(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|