Retrieves custom emoji automatically
This commit is contained in:
parent
9686e62a1a
commit
6efbf03add
|
@ -20,13 +20,17 @@ import android.content.Intent;
|
|||
import android.content.SharedPreferences;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.os.AsyncTask;
|
||||
import android.util.Log;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
import fr.gouv.etalab.mastodon.activities.MainActivity;
|
||||
import fr.gouv.etalab.mastodon.client.API;
|
||||
import fr.gouv.etalab.mastodon.client.APIResponse;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.Account;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.Emojis;
|
||||
import fr.gouv.etalab.mastodon.helper.Helper;
|
||||
import fr.gouv.etalab.mastodon.sqlite.CustomEmojiDAO;
|
||||
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
|
||||
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
|
||||
|
||||
|
@ -68,6 +72,7 @@ public class UpdateAccountInfoAsyncTask extends AsyncTask<Void, Void, Void> {
|
|||
if( account.getUsername() != null && account.getCreated_at() != null)
|
||||
new AccountDAO(this.contextReference.get(), db).insertAccount(account);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -22,10 +22,13 @@ import android.os.AsyncTask;
|
|||
import java.lang.ref.WeakReference;
|
||||
|
||||
import fr.gouv.etalab.mastodon.client.API;
|
||||
import fr.gouv.etalab.mastodon.client.APIResponse;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.Account;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.Emojis;
|
||||
import fr.gouv.etalab.mastodon.helper.Helper;
|
||||
import fr.gouv.etalab.mastodon.interfaces.OnUpdateAccountInfoInterface;
|
||||
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
|
||||
import fr.gouv.etalab.mastodon.sqlite.CustomEmojiDAO;
|
||||
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
|
||||
|
||||
/**
|
||||
|
@ -60,6 +63,15 @@ public class UpdateAccountInfoByIDAsyncTask extends AsyncTask<Void, Void, Void>
|
|||
new AccountDAO(this.contextReference.get(), db).updateAccount(account);
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
}catch (Exception ignored){}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -1110,6 +1110,31 @@ public class API {
|
|||
return apiResponse;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves Accounts when searching (ie: via @...) *synchronously*
|
||||
*
|
||||
* @return APIResponse
|
||||
*/
|
||||
public APIResponse getCustomEmoji() {
|
||||
List<Emojis> emojis = new ArrayList<>();
|
||||
try {
|
||||
HttpsConnection httpsConnection = new HttpsConnection();
|
||||
String response = httpsConnection.get(getAbsoluteUrl("/custom_emojis"), 60, null, prefKeyOauthTokenT);
|
||||
emojis = parseEmojis(new JSONArray(response));
|
||||
apiResponse.setSince_id(httpsConnection.getSince_id());
|
||||
apiResponse.setMax_id(httpsConnection.getMax_id());
|
||||
|
||||
} catch (HttpsConnection.HttpsConnectionException e) {
|
||||
setError(e.getStatusCode(), e);
|
||||
}catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
apiResponse.setEmojis(emojis);
|
||||
return apiResponse;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse json response an unique account
|
||||
* @param resobj JSONObject
|
||||
|
@ -1242,10 +1267,7 @@ public class API {
|
|||
if( arrayTag != null){
|
||||
for(int j = 0 ; j < emojisTag.length() ; j++){
|
||||
JSONObject emojisObj = emojisTag.getJSONObject(j);
|
||||
Emojis emojis = new Emojis();
|
||||
emojis.setShortcode(emojisObj.get("shortcode").toString());
|
||||
emojis.setStatic_url(emojisObj.get("static_url").toString());
|
||||
emojis.setUrl(emojisObj.get("url").toString());
|
||||
Emojis emojis = parseEmojis(emojisObj);
|
||||
emojiList.add(emojis);
|
||||
}
|
||||
}
|
||||
|
@ -1303,6 +1325,47 @@ public class API {
|
|||
return instance;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Parse emojis
|
||||
* @param jsonArray JSONArray
|
||||
* @return List<Emojis> of emojis
|
||||
*/
|
||||
private List<Emojis> parseEmojis(JSONArray jsonArray){
|
||||
List<Emojis> emojis = new ArrayList<>();
|
||||
try {
|
||||
int i = 0;
|
||||
while (i < jsonArray.length() ) {
|
||||
JSONObject resobj = jsonArray.getJSONObject(i);
|
||||
Emojis emojis1 = parseEmojis(resobj);
|
||||
emojis.add(emojis1);
|
||||
i++;
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return emojis;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Parse json response for emoji
|
||||
* @param resobj JSONObject
|
||||
* @return Emojis
|
||||
*/
|
||||
private static Emojis parseEmojis(JSONObject resobj){
|
||||
Emojis emojis = new Emojis();
|
||||
try {
|
||||
emojis.setShortcode(resobj.get("shortcode").toString());
|
||||
emojis.setStatic_url(resobj.get("static_url").toString());
|
||||
emojis.setUrl(resobj.get("url").toString());
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return emojis;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse json response an unique account
|
||||
* @param resobj JSONObject
|
||||
|
|
|
@ -30,6 +30,7 @@ public class APIResponse {
|
|||
private List<Context> contexts = null;
|
||||
private List<Notification> notifications = null;
|
||||
private List<Relationship> relationships = null;
|
||||
private List<Emojis> emojis = null;
|
||||
private fr.gouv.etalab.mastodon.client.Entities.Error error = null;
|
||||
private String since_id, max_id;
|
||||
private Instance instance;
|
||||
|
@ -105,4 +106,12 @@ public class APIResponse {
|
|||
public void setRelationships(List<Relationship> relationships) {
|
||||
this.relationships = relationships;
|
||||
}
|
||||
|
||||
public List<Emojis> getEmojis() {
|
||||
return emojis;
|
||||
}
|
||||
|
||||
public void setEmojis(List<Emojis> emojis) {
|
||||
this.emojis = emojis;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -139,7 +139,6 @@ import fr.gouv.etalab.mastodon.client.Entities.Status;
|
|||
import fr.gouv.etalab.mastodon.client.PatchBaseImageDownloader;
|
||||
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveEmojiInterface;
|
||||
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
|
||||
import fr.gouv.etalab.mastodon.sqlite.CustomEmojiDAO;
|
||||
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
|
||||
|
||||
import static android.content.Context.DOWNLOAD_SERVICE;
|
||||
|
@ -1236,15 +1235,6 @@ public class Helper {
|
|||
.cacheOnDisk(true).resetViewBeforeLoading(true).build();
|
||||
imageLoader = ImageLoader.getInstance();
|
||||
for (final Emojis emoji : emojis) {
|
||||
boolean sameInstance;
|
||||
if( instance != null) {
|
||||
if (status.getReblog() == null)
|
||||
sameInstance = status.getUri().contains(instance);
|
||||
else
|
||||
sameInstance = status.getReblog().getUri().contains(instance);
|
||||
if (sameInstance)
|
||||
storeEmoji(context, db, emoji);
|
||||
}
|
||||
NonViewAware imageAware = new NonViewAware(new ImageSize(50, 50), ViewScaleType.CROP);
|
||||
imageLoader.displayImage(emoji.getUrl(), imageAware, options, new SimpleImageLoadingListener() {
|
||||
@Override
|
||||
|
@ -1334,21 +1324,6 @@ public class Helper {
|
|||
return spannableString;
|
||||
}
|
||||
|
||||
/**
|
||||
* Manage custom emoji to store or update them
|
||||
* @param context Context
|
||||
* @param db SQLiteDatabase
|
||||
* @param emojis Emojis
|
||||
*/
|
||||
private static void storeEmoji(Context context, SQLiteDatabase db, Emojis emojis){
|
||||
try{
|
||||
Emojis emoji_ = new CustomEmojiDAO(context, db).getEmoji(emojis.getShortcode());
|
||||
if( emoji_ == null)
|
||||
new CustomEmojiDAO(context, db).insertEmoji(emojis);
|
||||
else
|
||||
new CustomEmojiDAO(context, db).updateEmoji(emojis);
|
||||
}catch (Exception ignored){}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the account bio contents urls & tags and fills the content with ClickableSpan
|
||||
|
|
|
@ -92,6 +92,14 @@ public class CustomEmojiDAO {
|
|||
return db.delete(Sqlite.TABLE_CUSTOM_EMOJI, Sqlite.COL_SHORTCODE + " = \"" + emoji.getShortcode() + "\" AND " + Sqlite.COL_INSTANCE + " = \"" + instance+ "\"", null);
|
||||
}
|
||||
|
||||
/***
|
||||
* Remove emoji by id
|
||||
*/
|
||||
public void removeAll(){
|
||||
String instance = Helper.getLiveInstance(context);
|
||||
db.delete(Sqlite.TABLE_CUSTOM_EMOJI, Sqlite.COL_INSTANCE + " = \"" + instance + "\"", null);
|
||||
}
|
||||
|
||||
//------- GETTERS -------
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue