DisplayUtils.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Nextcloud Talk application
  3. *
  4. * @author Mario Danic
  5. * Copyright (C) 2017 Mario Danic <mario@lovelyhq.com>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package com.nextcloud.talk.utils;
  21. import android.annotation.SuppressLint;
  22. import android.content.Context;
  23. import android.content.Intent;
  24. import android.content.res.ColorStateList;
  25. import android.content.res.Resources;
  26. import android.graphics.Typeface;
  27. import android.graphics.drawable.Animatable;
  28. import android.graphics.drawable.Drawable;
  29. import android.net.Uri;
  30. import android.os.Build;
  31. import android.text.Spannable;
  32. import android.text.SpannableString;
  33. import android.text.Spanned;
  34. import android.text.TextPaint;
  35. import android.text.TextUtils;
  36. import android.text.method.LinkMovementMethod;
  37. import android.text.style.AbsoluteSizeSpan;
  38. import android.text.style.ClickableSpan;
  39. import android.text.style.ForegroundColorSpan;
  40. import android.text.style.StyleSpan;
  41. import android.util.DisplayMetrics;
  42. import android.util.Log;
  43. import android.view.View;
  44. import android.view.ViewGroup;
  45. import android.widget.TextView;
  46. import com.facebook.drawee.controller.ControllerListener;
  47. import com.facebook.drawee.view.SimpleDraweeView;
  48. import com.facebook.imagepipeline.image.ImageInfo;
  49. import com.nextcloud.talk.R;
  50. import com.nextcloud.talk.application.NextcloudTalkApplication;
  51. import java.lang.reflect.Constructor;
  52. import java.lang.reflect.InvocationTargetException;
  53. import java.lang.reflect.Method;
  54. import java.util.regex.Matcher;
  55. import java.util.regex.Pattern;
  56. import androidx.annotation.ColorInt;
  57. import androidx.annotation.ColorRes;
  58. import androidx.annotation.DrawableRes;
  59. import androidx.annotation.NonNull;
  60. import androidx.annotation.Nullable;
  61. import androidx.appcompat.widget.AppCompatDrawableManager;
  62. import androidx.core.content.ContextCompat;
  63. import androidx.core.graphics.drawable.DrawableCompat;
  64. public class DisplayUtils {
  65. private static final String TAG = "DisplayUtils";
  66. public static void setClickableString(String string, String url, TextView textView){
  67. SpannableString spannableString = new SpannableString(string);
  68. spannableString.setSpan(new ClickableSpan() {
  69. @Override
  70. public void onClick(@NonNull View widget) {
  71. Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
  72. browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  73. NextcloudTalkApplication.getSharedApplication().getApplicationContext().startActivity(browserIntent);
  74. }
  75. @Override
  76. public void updateDrawState(TextPaint ds) {
  77. super.updateDrawState(ds);
  78. ds.setUnderlineText(false);
  79. }
  80. }, 0, string.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
  81. textView.setText(spannableString);
  82. textView.setMovementMethod(LinkMovementMethod.getInstance());
  83. }
  84. private static void updateViewSize(@Nullable ImageInfo imageInfo, SimpleDraweeView draweeView) {
  85. if (imageInfo != null) {
  86. draweeView.getLayoutParams().width = imageInfo.getWidth() > 480 ? 480 : imageInfo.getWidth();
  87. draweeView.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
  88. draweeView.setAspectRatio((float) imageInfo.getWidth() / imageInfo.getHeight());
  89. }
  90. }
  91. public static ControllerListener getImageControllerListener(SimpleDraweeView draweeView) {
  92. return new ControllerListener() {
  93. @Override
  94. public void onSubmit(String id, Object callerContext) {
  95. }
  96. @Override
  97. public void onFinalImageSet(String id, @javax.annotation.Nullable Object imageInfo, @javax.annotation.Nullable Animatable animatable) {
  98. updateViewSize((ImageInfo)imageInfo, draweeView);
  99. }
  100. @Override
  101. public void onIntermediateImageSet(String id, @javax.annotation.Nullable Object imageInfo) {
  102. updateViewSize((ImageInfo) imageInfo, draweeView);
  103. }
  104. @Override
  105. public void onIntermediateImageFailed(String id, Throwable throwable) {
  106. }
  107. @Override
  108. public void onFailure(String id, Throwable throwable) {
  109. }
  110. @Override
  111. public void onRelease(String id) {
  112. }
  113. };
  114. }
  115. public static float convertDpToPixel(float dp, Context context) {
  116. Resources resources = context.getResources();
  117. DisplayMetrics metrics = resources.getDisplayMetrics();
  118. float px = dp * ((float) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
  119. return px;
  120. }
  121. // Solution inspired by https://stackoverflow.com/questions/34936590/why-isnt-my-vector-drawable-scaling-as-expected
  122. public static void useCompatVectorIfNeeded() {
  123. if (Build.VERSION.SDK_INT < 23) {
  124. try {
  125. @SuppressLint("RestrictedApi") AppCompatDrawableManager drawableManager = AppCompatDrawableManager.get();
  126. Class<?> inflateDelegateClass = Class.forName("android.support.v7.widget.AppCompatDrawableManager$InflateDelegate");
  127. Class<?> vdcInflateDelegateClass = Class.forName("android.support.v7.widget.AppCompatDrawableManager$VdcInflateDelegate");
  128. Constructor<?> constructor = vdcInflateDelegateClass.getDeclaredConstructor();
  129. constructor.setAccessible(true);
  130. Object vdcInflateDelegate = constructor.newInstance();
  131. Class<?> args[] = {String.class, inflateDelegateClass};
  132. Method addDelegate = AppCompatDrawableManager.class.getDeclaredMethod("addDelegate", args);
  133. addDelegate.setAccessible(true);
  134. addDelegate.invoke(drawableManager, "vector", vdcInflateDelegate);
  135. } catch (ClassNotFoundException | NoSuchMethodException | InstantiationException |
  136. InvocationTargetException | IllegalAccessException e) {
  137. Log.e(TAG, "Failed to use reflection to enable proper vector scaling");
  138. }
  139. }
  140. }
  141. public static Drawable getTintedDrawable(Resources res, @DrawableRes int drawableResId, @ColorRes int colorResId) {
  142. Drawable drawable = res.getDrawable(drawableResId);
  143. int color = res.getColor(colorResId);
  144. drawable.setTint(color);
  145. return drawable;
  146. }
  147. public static Spannable searchAndColor(String text, Spannable spannable, String searchText, @ColorInt int color) {
  148. if (TextUtils.isEmpty(text) || TextUtils.isEmpty(searchText)) {
  149. return spannable;
  150. }
  151. Matcher m = Pattern.compile(searchText, Pattern.CASE_INSENSITIVE | Pattern.LITERAL)
  152. .matcher(text);
  153. int textSize = NextcloudTalkApplication.getSharedApplication().getResources().getDimensionPixelSize(R.dimen
  154. .chat_text_size);
  155. while (m.find()) {
  156. int start = text.indexOf(m.group());
  157. int end = text.indexOf(m.group()) + m.group().length();
  158. spannable.setSpan(new ForegroundColorSpan(color), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  159. spannable.setSpan(new StyleSpan(Typeface.BOLD), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  160. spannable.setSpan(new AbsoluteSizeSpan(textSize), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  161. }
  162. return spannable;
  163. }
  164. public static Drawable getMessageSelector(@ColorInt int normalColor, @ColorInt int selectedColor,
  165. @ColorInt int pressedColor, @DrawableRes int shape) {
  166. Drawable vectorDrawable = ContextCompat.getDrawable(NextcloudTalkApplication.getSharedApplication()
  167. .getApplicationContext(),
  168. shape);
  169. Drawable drawable = DrawableCompat.wrap(vectorDrawable).mutate();
  170. DrawableCompat.setTintList(
  171. drawable,
  172. new ColorStateList(
  173. new int[][]{
  174. new int[]{android.R.attr.state_selected},
  175. new int[]{android.R.attr.state_pressed},
  176. new int[]{-android.R.attr.state_pressed, -android.R.attr.state_selected}
  177. },
  178. new int[]{selectedColor, pressedColor, normalColor}
  179. ));
  180. return drawable;
  181. }
  182. }