summaryrefslogtreecommitdiffstats
path: root/app/src/main/java/app/fedilab/android/helper/Helper.java
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/app/fedilab/android/helper/Helper.java')
-rw-r--r--app/src/main/java/app/fedilab/android/helper/Helper.java105
1 files changed, 84 insertions, 21 deletions
diff --git a/app/src/main/java/app/fedilab/android/helper/Helper.java b/app/src/main/java/app/fedilab/android/helper/Helper.java
index 30f4d4907..688464427 100644
--- a/app/src/main/java/app/fedilab/android/helper/Helper.java
+++ b/app/src/main/java/app/fedilab/android/helper/Helper.java
@@ -56,11 +56,13 @@ import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.provider.OpenableColumns;
import android.text.Html;
+import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.style.ClickableSpan;
+import android.text.style.ImageSpan;
import android.text.style.URLSpan;
import android.util.DisplayMetrics;
import android.util.Log;
@@ -82,6 +84,7 @@ import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
+import android.widget.RadioButton;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
@@ -89,6 +92,7 @@ import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
+import androidx.appcompat.widget.AppCompatTextView;
import androidx.appcompat.widget.SwitchCompat;
import androidx.browser.customtabs.CustomTabsIntent;
import androidx.core.app.NotificationCompat;
@@ -262,6 +266,8 @@ public class Helper {
public static final String REDIRECT_CONTENT = "urn:ietf:wg:oauth:2.0:oob";
public static final String REDIRECT_CONTENT_WEB = "mastalab://backtomastalab";
public static final int EXTERNAL_STORAGE_REQUEST_CODE = 84;
+ public static final int EXTERNAL_STORAGE_REQUEST_CODE_MEDIA_SAVE = 85;
+ public static final int EXTERNAL_STORAGE_REQUEST_CODE_MEDIA_SHARE = 86;
//Thekinrar's API: https://instances.social/api/doc/
public static final String THEKINRAR_SECRET_TOKEN = "jGj9gW3z9ptyIpB8CMGhAlTlslcemMV6AgoiImfw3vPP98birAJTHOWiu5ZWfCkLvcaLsFZw9e3Pb7TIwkbIyrj3z6S7r2oE6uy6EFHvls3YtapP8QKNZ980p9RfzTb4";
@@ -1639,12 +1645,7 @@ public class Helper {
public static <K, V> Map<K, V> sortByValue(Map<K, V> map) {
List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());
- Collections.sort(list, new Comparator<Object>() {
- @SuppressWarnings("unchecked")
- public int compare(Object o2, Object o1) {
- return ((Comparable<V>) ((Map.Entry<K, V>) (o1)).getValue()).compareTo(((Map.Entry<K, V>) (o2)).getValue());
- }
- });
+ Collections.sort(list, (Comparator<Object>) (o2, o1) -> ((Comparable<V>) ((Map.Entry<K, V>) (o1)).getValue()).compareTo(((Map.Entry<K, V>) (o2)).getValue()));
Map<K, V> result = new LinkedHashMap<>();
for (Map.Entry<K, V> entry : list) {
@@ -1970,7 +1971,8 @@ public class Helper {
activity.startActivity(myIntent);
activity.finish(); //User is logged out to get a new token
} else {
- Account.makeAccountNameEmoji(new WeakReference<>(activity), account);
+
+ makeEmojis(activity, username, account.getDisplayNameSpan(), account.getEmojis());
username.setText(String.format("@%s", account.getUsername() + "@" + account.getInstance()));
displayedName.setText(account.getDisplayNameSpan(), TextView.BufferType.SPANNABLE);
loadGiF(activity, account, profilePicture);
@@ -2078,7 +2080,9 @@ public class Helper {
*/
public static SpannableString clickableElementsDescription(final Context context, String fullContent) {
-
+ if (fullContent == null) {
+ return new SpannableString("");
+ }
SpannableString spannableString;
SpannableString spannableStringT = new SpannableString(fullContent);
Pattern aLink = Pattern.compile("(<\\s?a\\s?href=\"https?://([\\da-z.-]+\\.[a-z.]{2,10})/(@[/\\w._-]*)\"\\s?[^.]*<\\s?/\\s?a\\s?>)");
@@ -2654,7 +2658,8 @@ public class Helper {
public static List<String> restoreArrayFromString(String serializedArray) {
Gson gson = new Gson();
try {
- Type listType = new TypeToken<ArrayList<String>>() {}.getType();
+ Type listType = new TypeToken<ArrayList<String>>() {
+ }.getType();
return gson.fromJson(serializedArray, listType);
} catch (Exception e) {
return null;
@@ -4200,12 +4205,12 @@ public class Helper {
fw.append(',');
fw.append("COMMENT");
fw.append('\n');
- Iterator it = domainBlocked.entrySet().iterator();
+ Iterator<Map.Entry<String, String>> it = domainBlocked.entrySet().iterator();
while (it.hasNext()) {
- Map.Entry pair = (Map.Entry) it.next();
- fw.append((String) pair.getKey());
+ Map.Entry<String, String> pair = it.next();
+ fw.append(pair.getKey());
fw.append(',');
- fw.append((String) pair.getValue());
+ fw.append(pair.getValue());
fw.append('\n');
it.remove();
}
@@ -4254,16 +4259,16 @@ public class Helper {
linearLayout_main.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
linearLayout_main.setOrientation(LinearLayout.VERTICAL);
- Iterator it = instances.entrySet().iterator();
+ Iterator<Map.Entry<String, String>> it = instances.entrySet().iterator();
while (it.hasNext()) {
- Map.Entry pair = (Map.Entry) it.next();
+ Map.Entry<String, String> pair = it.next();
View item = inflater.inflate(R.layout.muted_instance_popup, new LinearLayout(context), false);
CheckBox checkBox = item.findViewById(R.id.popup_domain);
- checkBox.setText(pair.getKey().toString().trim());
+ checkBox.setText(pair.getKey().trim());
item.findViewById(R.id.popup_comment).setVisibility(View.GONE);
if (pair.getValue() != null) {
TextView textView = item.findViewById(R.id.comment_text);
- textView.setText(pair.getValue().toString());
+ textView.setText(pair.getValue());
textView.setVisibility(View.VISIBLE);
}
linearLayout_main.addView(item);
@@ -4284,15 +4289,15 @@ public class Helper {
if (domainBlocked.size() > 0) {
new Thread() {
public void run() {
- Iterator it1 = domainBlocked.entrySet().iterator();
+ Iterator<Map.Entry<String, String>> it1 = domainBlocked.entrySet().iterator();
while (it1.hasNext()) {
- Map.Entry pair = (Map.Entry) it1.next();
+ Map.Entry<String, String> pair = it1.next();
try {
sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
- new PostActionAsyncTask(context, API.StatusAction.BLOCK_DOMAIN, pair.getKey().toString().trim(), ((MutedInstanceActivity) context)).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
+ new PostActionAsyncTask(context, API.StatusAction.BLOCK_DOMAIN, pair.getKey().trim(), ((MutedInstanceActivity) context)).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
it1.remove();
}
}
@@ -4399,6 +4404,62 @@ public class Helper {
return null;
}
+ public static void makeEmojis(Context context, View customTextView, SpannableString contentSpan, List<Emojis> emojis) {
+
+ SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
+ boolean disableAnimatedEmoji = sharedpreferences.getBoolean(Helper.SET_DISABLE_ANIMATED_EMOJI, false);
+
+ if (emojis != null && emojis.size() > 0) {
+ for (final Emojis emoji : emojis) {
+ Glide.with(context)
+ .asDrawable()
+ .load(disableAnimatedEmoji ? emoji.getStatic_url() : emoji.getUrl())
+ .into(new CustomTarget<Drawable>() {
+ @Override
+ public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
+ final String targetedEmoji = ":" + emoji.getShortcode() + ":";
+ if (contentSpan != null && contentSpan.toString().contains(targetedEmoji)) {
+ //emojis can be used several times so we have to loop
+ for (int startPosition = -1; (startPosition = contentSpan.toString().indexOf(targetedEmoji, startPosition + 1)) != -1; startPosition++) {
+ final int endPosition = startPosition + targetedEmoji.length();
+ if (endPosition <= contentSpan.toString().length() && endPosition >= startPosition) {
+ ImageSpan imageSpan;
+ try {
+ resource.setBounds(0, 0, (int) Helper.convertDpToPixel(20, context), (int) Helper.convertDpToPixel(20, context));
+ resource.setVisible(true, true);
+ imageSpan = new ImageSpan(resource);
+ contentSpan.setSpan(
+ imageSpan, startPosition,
+ endPosition, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
+ if (customTextView instanceof CustomTextView) {
+ ((CustomTextView) customTextView).setText(contentSpan, TextView.BufferType.SPANNABLE);
+ } else if (customTextView instanceof RadioButton) {
+ ((RadioButton) customTextView).setText(contentSpan, TextView.BufferType.SPANNABLE);
+ } else if (customTextView instanceof CheckBox) {
+ ((CheckBox) customTextView).setText(contentSpan, TextView.BufferType.SPANNABLE);
+ } else if (customTextView instanceof AppCompatTextView) {
+ ((AppCompatTextView) customTextView).setText(contentSpan, TextView.BufferType.SPANNABLE);
+ } else if (customTextView instanceof TextView) {
+ ((TextView) customTextView).setText(contentSpan, TextView.BufferType.SPANNABLE);
+ }
+
+ } catch (Exception ignored) {
+ }
+ }
+ }
+ }
+ }
+
+ @Override
+ public void onLoadCleared(@Nullable Drawable placeholder) {
+
+ }
+ });
+
+ }
+ }
+ }
+
//Event Type
public enum EventStreaming {
UPDATE,
@@ -4407,6 +4468,7 @@ public class Helper {
NONE
}
+
public enum NotifType {
FOLLLOW,
MENTION,
@@ -4418,7 +4480,6 @@ public class Helper {
TOOT
}
-
public static class CacheTask extends AsyncTask<Void, Void, Void> {
private float cacheSize;
private WeakReference<Context> contextReference;
@@ -4505,4 +4566,6 @@ public class Helper {
.show();
}
}
+
+
}