From c033064d13a81960b1f9e83aeb18e68d1553e1e4 Mon Sep 17 00:00:00 2001 From: Thomas Date: Wed, 20 Jul 2022 17:13:07 +0200 Subject: [PATCH] Fix issue #263 - Custom emoji for Friendica --- .../android/activities/InstanceActivity.java | 21 +++++++- .../android/helper/SpannableHelper.java | 30 +++++++++++ app/src/main/res/layout/activity_instance.xml | 50 ++++++++----------- 3 files changed, 70 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/activities/InstanceActivity.java b/app/src/main/java/app/fedilab/android/activities/InstanceActivity.java index 057bc3f0..9f515608 100644 --- a/app/src/main/java/app/fedilab/android/activities/InstanceActivity.java +++ b/app/src/main/java/app/fedilab/android/activities/InstanceActivity.java @@ -17,6 +17,7 @@ package app.fedilab.android.activities; import android.content.Intent; import android.content.SharedPreferences; +import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Build; import android.os.Bundle; @@ -25,10 +26,14 @@ import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.lifecycle.ViewModelProvider; import androidx.preference.PreferenceManager; import com.bumptech.glide.Glide; +import com.bumptech.glide.request.target.CustomTarget; +import com.bumptech.glide.request.transition.Transition; import app.fedilab.android.BaseMainActivity; import app.fedilab.android.R; @@ -89,6 +94,7 @@ public class InstanceActivity extends BaseActivity { if (instanceInfo == null || instanceInfo.info == null || instanceInfo.info.description == null) { binding.maxCharContainer.setVisibility(View.VISIBLE); binding.instanceContainer.setVisibility(View.GONE); + binding.instanceContact.setVisibility(View.GONE); int val = sharedpreferences.getInt(getString(R.string.SET_MAX_INSTANCE_CHAR) + MainActivity.currentInstance, -1); if (val != -1) { binding.maxChar.setText(String.valueOf(val)); @@ -110,9 +116,20 @@ public class InstanceActivity extends BaseActivity { binding.instanceContact.hide(); } Glide.with(InstanceActivity.this) - .asBitmap() + .asDrawable() .load(instance.thumbnail) - .into(binding.backGroundImage); + .into(new CustomTarget() { + @Override + public void onResourceReady(@NonNull Drawable resource, @Nullable Transition transition) { + binding.background.setAlpha(0.2f); + binding.background.setBackground(resource); + } + + @Override + public void onLoadCleared(@Nullable Drawable placeholder) { + + } + }); binding.instanceContact.setOnClickListener(v -> { Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", instance.email, null)); diff --git a/app/src/main/java/app/fedilab/android/helper/SpannableHelper.java b/app/src/main/java/app/fedilab/android/helper/SpannableHelper.java index c9c45c7b..e786f473 100644 --- a/app/src/main/java/app/fedilab/android/helper/SpannableHelper.java +++ b/app/src/main/java/app/fedilab/android/helper/SpannableHelper.java @@ -54,6 +54,7 @@ import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Objects; @@ -89,6 +90,17 @@ public class SpannableHelper { if (text == null) { return null; } + Pattern imgPattern = Pattern.compile("]*src=\"([^\"]+)\"[^>]*>"); + Matcher matcherImg = imgPattern.matcher(text); + HashMap imagesToReplace = new LinkedHashMap<>(); + int inc = 0; + while (matcherImg.find()) { + String replacement = "[FEDI_IMG_" + inc + "]"; + imagesToReplace.put(replacement, matcherImg.group(1)); + inc++; + text = text.replaceAll(Pattern.quote(matcherImg.group()), replacement); + } + SpannableStringBuilder content; View view = viewWeakReference.get(); List mentionList = null; @@ -148,6 +160,24 @@ public class SpannableHelper { } } } + + if (imagesToReplace.size() > 0) { + for (Map.Entry entry : imagesToReplace.entrySet()) { + String key = entry.getKey(); + String url = entry.getValue(); + Matcher matcher = Pattern.compile(key, Pattern.LITERAL) + .matcher(content); + while (matcher.find()) { + CustomEmoji customEmoji = new CustomEmoji(new WeakReference<>(view)); + content.setSpan(customEmoji, matcher.start(), matcher.end(), 0); + Glide.with(view) + .asDrawable() + .load(url) + .into(customEmoji.getTarget(true)); + } + } + + } return trimSpannable(new SpannableStringBuilder(content)); } diff --git a/app/src/main/res/layout/activity_instance.xml b/app/src/main/res/layout/activity_instance.xml index a2500c21..8661017b 100644 --- a/app/src/main/res/layout/activity_instance.xml +++ b/app/src/main/res/layout/activity_instance.xml @@ -22,31 +22,19 @@ - - - - + android:layout_height="match_parent" /> - + @@ -142,7 +122,7 @@ app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent" android:layout_width="match_parent" - android:layout_height="match_parent" + android:layout_height="420dp" android:gravity="center" tools:visibility="visible" android:visibility="gone"> @@ -153,6 +133,18 @@ android:indeterminate="true" /> + + + \ No newline at end of file