fedilab-Android-App/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Status.java

706 lines
26 KiB
Java
Raw Normal View History

2017-05-05 16:36:04 +02:00
/* Copyright 2017 Thomas Schneider
*
2017-07-10 10:33:24 +02:00
* This file is a part of Mastalab
2017-05-05 16:36:04 +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-05 16:36:04 +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-05 16:36:04 +02:00
* see <http://www.gnu.org/licenses>. */
package fr.gouv.etalab.mastodon.client.Entities;
2017-12-03 10:56:54 +01:00
import android.content.*;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
2017-05-26 17:20:36 +02:00
import android.os.Parcel;
import android.os.Parcelable;
2017-12-03 10:56:54 +01:00
import android.support.annotation.Nullable;
import android.text.Html;
import android.text.Spannable;
2017-10-20 19:58:46 +02:00
import android.text.SpannableString;
2017-12-03 10:56:54 +01:00
import android.text.Spanned;
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.text.style.ImageSpan;
import android.util.Patterns;
import android.view.View;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
2017-10-20 19:58:46 +02:00
import java.util.ArrayList;
2017-05-05 16:36:04 +02:00
import java.util.Date;
import java.util.List;
2017-12-03 10:56:54 +01:00
import java.util.regex.Matcher;
import fr.gouv.etalab.mastodon.activities.HashTagActivity;
import fr.gouv.etalab.mastodon.activities.ShowAccountActivity;
import fr.gouv.etalab.mastodon.activities.WebviewActivity;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveEmojiInterface;
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
2017-05-05 16:36:04 +02:00
/**
* Created by Thomas on 23/04/2017.
2017-05-26 17:20:36 +02:00
* Manage Status (ie: toots)
2017-05-05 16:36:04 +02:00
*/
2017-05-26 17:20:36 +02:00
public class Status implements Parcelable {
2017-05-05 16:36:04 +02:00
private String id;
private String uri;
private String url;
private Account account;
private String in_reply_to_id;
private String in_reply_to_account_id;
private Status reblog;
private String content;
private String content_translated;
2017-05-05 16:36:04 +02:00
private Date created_at;
private int reblogs_count;
private int favourites_count;
private boolean reblogged;
private boolean favourited;
private boolean pinned;
2017-05-05 16:36:04 +02:00
private boolean sensitive;
private String spoiler_text;
private String visibility;
private boolean attachmentShown = false;
2017-06-07 16:52:53 +02:00
private boolean spoilerShown = false;
private ArrayList<Attachment> media_attachments;
2017-08-16 18:08:16 +02:00
private List<Status> replies;
private List<Mention> mentions;
2017-10-20 06:57:53 +02:00
private List<Emojis> emojis;
private List<Tag> tags;
private Application application;
private String language;
private boolean isTranslated = false;
2017-10-20 19:58:46 +02:00
private boolean isEmojiFound = false;
2017-12-03 10:56:54 +01:00
private boolean isClickable = false;
private boolean isTranslationShown = false;
2017-09-08 18:30:49 +02:00
private boolean isNew = false;
2017-09-18 20:32:36 +02:00
private boolean isTakingScreenShot = false;
private boolean isVisible = true;
2017-12-03 10:56:54 +01:00
private Status status;
private SpannableString contentSpan, contentSpanCW, contentSpanTranslated;
public Status(){
this.status = this;
}
2017-05-05 16:36:04 +02:00
2017-05-26 17:20:36 +02:00
protected Status(Parcel in) {
id = in.readString();
uri = in.readString();
url = in.readString();
in_reply_to_id = in.readString();
in_reply_to_account_id = in.readString();
reblog = in.readParcelable(Status.class.getClassLoader());
account = in.readParcelable(Account.class.getClassLoader());
mentions = in.readArrayList(Mention.class.getClassLoader());
2017-05-26 17:20:36 +02:00
content = in.readString();
content_translated = in.readString();
2017-05-26 17:20:36 +02:00
reblogs_count = in.readInt();
favourites_count = in.readInt();
reblogged = in.readByte() != 0;
favourited = in.readByte() != 0;
sensitive = in.readByte() != 0;
spoiler_text = in.readString();
visibility = in.readString();
language = in.readString();
2017-05-26 17:20:36 +02:00
attachmentShown = in.readByte() != 0;
2017-06-07 16:52:53 +02:00
spoilerShown = in.readByte() != 0;
isTranslated = in.readByte() != 0;
isTranslationShown = in.readByte() != 0;
2017-09-08 18:30:49 +02:00
isNew = in.readByte() != 0;
pinned = in.readByte() != 0;
2017-05-26 17:20:36 +02:00
}
2017-12-03 10:56:54 +01:00
2017-05-26 17:20:36 +02:00
public static final Creator<Status> CREATOR = new Creator<Status>() {
@Override
public Status createFromParcel(Parcel in) {
return new Status(in);
}
@Override
public Status[] newArray(int size) {
return new Status[size];
}
};
2017-05-05 16:36:04 +02:00
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUri() {
return uri;
}
public void setUri(String uri) {
this.uri = uri;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
public String getIn_reply_to_id() {
return in_reply_to_id;
}
public void setIn_reply_to_id(String in_reply_to_id) {
this.in_reply_to_id = in_reply_to_id;
}
public String getIn_reply_to_account_id() {
return in_reply_to_account_id;
}
public void setIn_reply_to_account_id(String in_reply_to_account_id) {
this.in_reply_to_account_id = in_reply_to_account_id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Status getReblog() {
return reblog;
}
public void setReblog(Status reblog) {
this.reblog = reblog;
}
public int getReblogs_count() {
return reblogs_count;
}
public void setReblogs_count(int reblogs_count) {
this.reblogs_count = reblogs_count;
}
public Date getCreated_at() {
return created_at;
}
public void setCreated_at(Date created_at) {
this.created_at = created_at;
}
public int getFavourites_count() {
return favourites_count;
}
public void setFavourites_count(int favourites_count) {
this.favourites_count = favourites_count;
}
public boolean isReblogged() {
return reblogged;
}
public void setReblogged(boolean reblogged) {
this.reblogged = reblogged;
}
public boolean isFavourited() {
return favourited;
}
public void setFavourited(boolean favourited) {
this.favourited = favourited;
}
public void setPinned(boolean pinned) { this.pinned = pinned; }
public boolean isPinned() { return pinned; }
2017-05-05 16:36:04 +02:00
public boolean isSensitive() {
return sensitive;
}
public void setSensitive(boolean sensitive) {
this.sensitive = sensitive;
}
public String getSpoiler_text() {
return spoiler_text;
}
public void setSpoiler_text(String spoiler_text) {
this.spoiler_text = spoiler_text;
}
public ArrayList<Attachment> getMedia_attachments() {
2017-05-05 16:36:04 +02:00
return media_attachments;
}
public void setMedia_attachments(ArrayList<Attachment> media_attachments) {
2017-05-05 16:36:04 +02:00
this.media_attachments = media_attachments;
}
public List<Mention> getMentions() {
return mentions;
}
public void setMentions(List<Mention> mentions) {
this.mentions = mentions;
}
public List<Tag> getTags() {
return tags;
}
public void setTags(List<Tag> tags) {
this.tags = tags;
}
public Application getApplication() {
return application;
}
public void setApplication(Application application) {
this.application = application;
}
public String getVisibility() {
return visibility;
}
public void setVisibility(String visibility) {
this.visibility = visibility;
}
public boolean isAttachmentShown() {
return attachmentShown;
}
public void setAttachmentShown(boolean attachmentShown) {
this.attachmentShown = attachmentShown;
}
2017-05-26 17:20:36 +02:00
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(id);
dest.writeString(uri);
dest.writeString(url);
dest.writeString(in_reply_to_id);
dest.writeString(in_reply_to_account_id);
dest.writeParcelable(reblog, flags);
dest.writeParcelable(account, flags);
dest.writeList(mentions);
2017-05-26 17:20:36 +02:00
dest.writeString(content);
dest.writeString(content_translated);
2017-05-26 17:20:36 +02:00
dest.writeInt(reblogs_count);
dest.writeInt(favourites_count);
dest.writeByte((byte) (reblogged ? 1 : 0));
dest.writeByte((byte) (favourited ? 1 : 0));
dest.writeByte((byte) (sensitive ? 1 : 0));
dest.writeString(spoiler_text);
dest.writeString(visibility);
dest.writeString(language);
2017-05-26 17:20:36 +02:00
dest.writeByte((byte) (attachmentShown ? 1 : 0));
2017-06-07 16:52:53 +02:00
dest.writeByte((byte) (spoilerShown ? 1 : 0));
dest.writeByte((byte) (isTranslated ? 1 : 0));
dest.writeByte((byte) (isTranslationShown ? 1 : 0));
2017-09-08 18:30:49 +02:00
dest.writeByte((byte) (isNew ? 1 : 0));
dest.writeByte((byte) (pinned ? 1 : 0));
2017-06-07 16:52:53 +02:00
}
public boolean isSpoilerShown() {
return spoilerShown;
}
public void setSpoilerShown(boolean spoilerShown) {
this.spoilerShown = spoilerShown;
2017-05-26 17:20:36 +02:00
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public boolean isTranslated() {
return isTranslated;
}
public void setTranslated(boolean translated) {
isTranslated = translated;
}
public boolean isTranslationShown() {
return isTranslationShown;
}
public void setTranslationShown(boolean translationShown) {
isTranslationShown = translationShown;
}
public String getContent_translated() {
return content_translated;
}
public void setContent_translated(String content_translated) {
this.content_translated = content_translated;
}
2017-08-16 18:08:16 +02:00
public List<Status> getReplies() {
return replies;
}
public void setReplies(List<Status> replies) {
this.replies = replies;
}
2017-09-08 18:30:49 +02:00
public boolean isNew() {
return isNew;
}
public void setNew(boolean aNew) {
isNew = aNew;
}
2017-09-18 20:32:36 +02:00
public boolean isTakingScreenShot() {
return isTakingScreenShot;
}
public void setTakingScreenShot(boolean takingScreenShot) {
isTakingScreenShot = takingScreenShot;
}
public boolean isVisible() {
return isVisible;
}
public void setVisible(boolean visible) {
isVisible = visible;
}
2017-10-20 06:54:21 +02:00
2017-10-20 06:57:53 +02:00
public List<Emojis> getEmojis() {
2017-10-20 06:54:21 +02:00
return emojis;
}
2017-10-20 06:57:53 +02:00
public void setEmojis(List<Emojis> emojis) {
2017-10-20 06:54:21 +02:00
this.emojis = emojis;
}
2017-10-20 19:58:46 +02:00
2017-12-03 10:56:54 +01:00
public boolean isEmojiFound() {
return isEmojiFound;
2017-10-20 19:58:46 +02:00
}
2017-12-03 10:56:54 +01:00
public void setEmojiFound(boolean emojiFound) {
isEmojiFound = emojiFound;
2017-10-20 19:58:46 +02:00
}
2017-12-03 10:56:54 +01:00
public void makeClickable(Context context){
2017-12-03 11:14:52 +01:00
SpannableString spannableStringContent, spannableStringCW;
2017-12-03 10:56:54 +01:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
spannableStringContent = new SpannableString(Html.fromHtml(status.getReblog() != null ?status.getReblog().getContent():status.getContent(), Html.FROM_HTML_MODE_LEGACY));
else
//noinspection deprecation
spannableStringContent = new SpannableString(Html.fromHtml(status.getReblog() != null ?status.getReblog().getContent():status.getContent()));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
spannableStringCW = new SpannableString(Html.fromHtml(status.getReblog() != null ?status.getReblog().getSpoiler_text():status.getSpoiler_text(), Html.FROM_HTML_MODE_LEGACY));
else
//noinspection deprecation
spannableStringCW = new SpannableString(Html.fromHtml(status.getReblog() != null ?status.getReblog().getSpoiler_text():status.getSpoiler_text()));
2017-12-03 11:14:52 +01:00
status.setContentSpan(treatment(context, spannableStringContent));
status.setContentSpanCW(treatment(context, spannableStringCW));
isClickable = true;
}
public void makeClickableTranslation(Context context){
SpannableString spannableStringTranslated;
2017-12-03 10:56:54 +01:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
spannableStringTranslated = new SpannableString(Html.fromHtml(status.getContent_translated(), Html.FROM_HTML_MODE_LEGACY));
else
//noinspection deprecation
spannableStringTranslated = new SpannableString(Html.fromHtml(status.getContent_translated()));
status.setContentSpanTranslated(treatment(context, spannableStringTranslated));
}
2017-12-03 11:14:52 +01:00
2017-12-03 10:56:54 +01:00
public void makeEmojis(final Context context, final OnRetrieveEmojiInterface listener){
2017-12-03 11:14:52 +01:00
final SpannableString spannableStringContent;
final SpannableString spannableStringCW;
SpannableString spannableStringTranslated = null;
2017-12-03 10:56:54 +01:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
spannableStringContent = new SpannableString(Html.fromHtml(status.getReblog() != null ?status.getReblog().getContent():status.getContent(), Html.FROM_HTML_MODE_LEGACY));
else
//noinspection deprecation
spannableStringContent = new SpannableString(Html.fromHtml(status.getReblog() != null ?status.getReblog().getContent():status.getContent()));
2017-12-03 11:14:52 +01:00
2017-12-03 10:56:54 +01:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
spannableStringCW = new SpannableString(Html.fromHtml(status.getReblog() != null ?status.getReblog().getSpoiler_text():status.getSpoiler_text(), Html.FROM_HTML_MODE_LEGACY));
else
//noinspection deprecation
spannableStringCW = new SpannableString(Html.fromHtml(status.getReblog() != null ?status.getReblog().getSpoiler_text():status.getSpoiler_text()));
2017-12-03 11:14:52 +01:00
if( status.getContent_translated() != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
spannableStringTranslated = new SpannableString(Html.fromHtml(status.getContent_translated(), Html.FROM_HTML_MODE_LEGACY));
else
//noinspection deprecation
spannableStringTranslated = new SpannableString(Html.fromHtml(status.getContent_translated()));
}
2017-12-03 10:56:54 +01:00
final List<Emojis> emojis = status.getReblog() != null ? status.getReblog().getEmojis() : status.getEmojis();
if( emojis != null && emojis.size() > 0 ) {
final int[] i = {0};
for (final Emojis emoji : emojis) {
2017-12-03 11:14:52 +01:00
final SpannableString finalSpannableStringTranslated = spannableStringTranslated;
2017-12-03 10:56:54 +01:00
Glide.with(context)
.asBitmap()
.load(emoji.getUrl())
.listener(new RequestListener<Bitmap>() {
@Override
public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
return false;
}
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
i[0]++;
if( i[0] == (emojis.size())) {
status.setContentSpan(spannableStringContent);
status.setContentSpanCW(spannableStringCW);
2017-12-03 11:14:52 +01:00
if( finalSpannableStringTranslated != null)
status.setContentSpanTranslated(finalSpannableStringTranslated);
2017-12-03 10:56:54 +01:00
listener.onRetrieveEmoji(status);
}
return false;
}
})
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
final String targetedEmoji = ":" + emoji.getShortcode() + ":";
if (spannableStringContent.toString().contains(targetedEmoji)) {
//emojis can be used several times so we have to loop
for (int startPosition = -1; (startPosition = spannableStringContent.toString().indexOf(targetedEmoji, startPosition + 1)) != -1; startPosition++) {
final int endPosition = startPosition + targetedEmoji.length();
spannableStringContent.setSpan(
new ImageSpan(context,
Bitmap.createScaledBitmap(resource, (int) Helper.convertDpToPixel(20, context),
(int) Helper.convertDpToPixel(20, context), false)), startPosition,
endPosition, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
}
}
if (spannableStringCW.toString().contains(targetedEmoji)) {
//emojis can be used several times so we have to loop
for (int startPosition = -1; (startPosition = spannableStringCW.toString().indexOf(targetedEmoji, startPosition + 1)) != -1; startPosition++) {
final int endPosition = startPosition + targetedEmoji.length();
spannableStringCW.setSpan(
new ImageSpan(context,
Bitmap.createScaledBitmap(resource, (int) Helper.convertDpToPixel(20, context),
(int) Helper.convertDpToPixel(20, context), false)), startPosition,
endPosition, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
}
}
2017-12-03 11:14:52 +01:00
if (finalSpannableStringTranslated != null && finalSpannableStringTranslated.toString().contains(targetedEmoji)) {
2017-12-03 10:56:54 +01:00
//emojis can be used several times so we have to loop
2017-12-03 11:14:52 +01:00
for (int startPosition = -1; (startPosition = finalSpannableStringTranslated.toString().indexOf(targetedEmoji, startPosition + 1)) != -1; startPosition++) {
2017-12-03 10:56:54 +01:00
final int endPosition = startPosition + targetedEmoji.length();
2017-12-03 11:14:52 +01:00
finalSpannableStringTranslated.setSpan(
2017-12-03 10:56:54 +01:00
new ImageSpan(context,
Bitmap.createScaledBitmap(resource, (int) Helper.convertDpToPixel(20, context),
(int) Helper.convertDpToPixel(20, context), false)), startPosition,
endPosition, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
}
}
i[0]++;
if( i[0] == (emojis.size())) {
status.setContentSpan(spannableStringContent);
status.setContentSpanCW(spannableStringCW);
2017-12-03 11:14:52 +01:00
if( finalSpannableStringTranslated != null)
status.setContentSpanTranslated(finalSpannableStringTranslated);
2017-12-03 10:56:54 +01:00
listener.onRetrieveEmoji(status);
}
}
});
}
}
2017-10-20 19:58:46 +02:00
}
2017-12-03 10:56:54 +01:00
private SpannableString treatment(final Context context, final SpannableString spannableString){
List<Mention> mentions = status.getReblog() != null ? status.getReblog().getMentions() : status.getMentions();
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
boolean embedded_browser = sharedpreferences.getBoolean(Helper.SET_EMBEDDED_BROWSER, true);
if( embedded_browser){
Matcher matcher;
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT)
matcher = Patterns.WEB_URL.matcher(spannableString);
else
matcher = Helper.urlPattern.matcher(spannableString);
while (matcher.find()){
int matchStart = matcher.start(1);
int matchEnd = matcher.end();
final String url = spannableString.toString().substring(matchStart, matchEnd);
spannableString.setSpan(new ClickableSpan() {
@Override
public void onClick(View textView) {
Intent intent = new Intent(context, WebviewActivity.class);
Bundle b = new Bundle();
String finalUrl = url;
if( !url.startsWith("http://") && ! url.startsWith("https://"))
finalUrl = "http://" + url;
b.putString("url", finalUrl);
intent.putExtras(b);
context.startActivity(intent);
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
}
},
matchStart, matchEnd,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
}
//Deals with mention to make them clickable
if( mentions != null && mentions.size() > 0 ) {
//Looping through accounts which are mentioned
for (final Mention mention : mentions) {
String targetedAccount = "@" + mention.getUsername();
if (spannableString.toString().contains(targetedAccount)) {
//Accounts can be mentioned several times so we have to loop
for(int startPosition = -1 ; (startPosition = spannableString.toString().indexOf(targetedAccount, startPosition + 1)) != -1 ; startPosition++){
int endPosition = startPosition + targetedAccount.length();
spannableString.setSpan(new ClickableSpan() {
@Override
public void onClick(View textView) {
Intent intent = new Intent(context, ShowAccountActivity.class);
Bundle b = new Bundle();
b.putString("accountId", mention.getId());
intent.putExtras(b);
context.startActivity(intent);
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
}
},
startPosition, endPosition,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
}
}
}
Matcher matcher = Helper.hashtagPattern.matcher(spannableString);
while (matcher.find()){
int matchStart = matcher.start(1);
int matchEnd = matcher.end();
final String tag = spannableString.toString().substring(matchStart, matchEnd);
spannableString.setSpan(new ClickableSpan() {
@Override
public void onClick(View textView) {
Intent intent = new Intent(context, HashTagActivity.class);
Bundle b = new Bundle();
b.putString("tag", tag.substring(1));
intent.putExtras(b);
context.startActivity(intent);
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
}
}, matchStart, matchEnd, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
return spannableString;
2017-10-20 19:58:46 +02:00
}
2017-12-03 10:56:54 +01:00
public SpannableString getContentSpan() {
return contentSpan;
2017-10-20 19:58:46 +02:00
}
2017-12-03 10:56:54 +01:00
public void setContentSpan(SpannableString contentSpan) {
this.contentSpan = contentSpan;
}
public SpannableString getContentSpanCW() {
return contentSpanCW;
}
public void setContentSpanCW(SpannableString contentSpanCW) {
this.contentSpanCW = contentSpanCW;
}
public SpannableString getContentSpanTranslated() {
return contentSpanTranslated;
}
public void setContentSpanTranslated(SpannableString contentSpanTranslated) {
this.contentSpanTranslated = contentSpanTranslated;
}
public boolean isClickable() {
return isClickable;
2017-10-20 19:58:46 +02:00
}
2017-05-05 16:36:04 +02:00
}