Add color support to UIUtils.applyMarkup (#667)

This commit is contained in:
Jordane Quincy 2019-10-07 16:14:48 +02:00 committed by Synced Synapse
parent f7ed2f3bc3
commit 1ea54b8d7f
8 changed files with 175 additions and 11 deletions

View File

@ -210,7 +210,7 @@ public class FavouritesListFragment extends AbstractListFragment implements Swip
}
void bindView(FavouriteType.DetailsFavourite favouriteDetail) {
titleView.setText(favouriteDetail.title);
titleView.setText(UIUtils.applyMarkup(context, favouriteDetail.title));
@StringRes final int typeRes;
switch (favouriteDetail.type) {

View File

@ -751,7 +751,7 @@ public class NowPlayingFragment extends Fragment
break;
}
mediaTitle.setText(title);
mediaTitle.setText(UIUtils.applyMarkup(getContext(), title));
mediaTitle.post(UIUtils.getMarqueeToggleableAction(mediaTitle));
mediaUndertitle.setText(underTitle);

View File

@ -718,7 +718,7 @@ public class PlaylistFragment extends Fragment
break;
}
viewHolder.title.setText(title);
viewHolder.title.setText(UIUtils.applyMarkup(getContext(), title));
viewHolder.details.setText(details);
viewHolder.duration.setText((duration > 0) ? UIUtils.formatTime(duration) : "");
viewHolder.position = position;

View File

@ -540,7 +540,7 @@ public class RemoteFragment extends Fragment
break;
}
nowPlayingTitle.setText(title);
nowPlayingTitle.setText(UIUtils.applyMarkup(getContext(), title));
nowPlayingDetails.setText(underTitle);
fastForwardButton.setImageResource(currentFastForwardIcon);

View File

@ -389,7 +389,7 @@ public class PVRChannelsListFragment extends Fragment
viewHolder.channelGroupId = channelGroupDetails.channelgroupid;
viewHolder.channelGroupName = channelGroupDetails.label;
viewHolder.titleView.setText(viewHolder.channelGroupName);
viewHolder.titleView.setText(UIUtils.applyMarkup(getContext(), viewHolder.channelGroupName));
return convertView;
}
}

View File

@ -165,7 +165,7 @@ public class NowPlayingPanel extends SlidingUpPanelLayout {
}
public void setTitle(String title) {
this.title.setText(title);
this.title.setText(UIUtils.applyMarkup(getContext(), title));
}
public void setDetails(String details) {

View File

@ -23,6 +23,7 @@ import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Vibrator;
@ -31,8 +32,10 @@ import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.TextViewCompat;
import android.support.v7.app.AlertDialog;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.TextAppearanceSpan;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
@ -652,8 +655,8 @@ public class UIUtils {
/**
* Replaces some BBCode-ish tagged text with styled spans.
* <p>
* Recognizes and styles CR, B, I, UPPERCASE, LOWERCASE and CAPITALIZE; recognizes
* and strips out LIGHT and COLOR. This is very strict/dumb, it only recognizes
* Recognizes and styles COLOR, CR, B, I, UPPERCASE, LOWERCASE and CAPITALIZE; recognizes
* and strips out LIGHT. This is very strict/dumb, it only recognizes
* uppercase tags with no spaces around them.
*
* @param context Activity context needed to resolve the style resources
@ -681,7 +684,8 @@ public class UIUtils {
Nestable italic = new Nestable();
Nestable light = new Nestable();
Nestable color = new Nestable();
Pattern colorTag = Pattern.compile("^\\[COLOR [^\\]]+\\]");
Pattern colorTag = Pattern.compile("^\\[COLOR ([^\\]]+)\\]");
String colorName = "white";
for (int i = start, length = src.length(); i < length;) {
String s = src.substring(i);
int nextTag = s.indexOf('[');
@ -770,8 +774,17 @@ public class UIUtils {
}
i += 8;
} else if (s.startsWith("[/COLOR]")) {
color.end();
if (color.imbalanced()) {
if(color.end()) {
int colorId = context.getResources().getIdentifier(colorName, "color", context.getPackageName());
ForegroundColorSpan foregroundColorSpan;
try{
foregroundColorSpan = new ForegroundColorSpan(context.getResources().getColor(colorId));
}catch (Resources.NotFoundException nfe){
foregroundColorSpan = new ForegroundColorSpan(Color.WHITE);
}
sb.setSpan(foregroundColorSpan, color.index, sb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
else if (color.imbalanced()) {
sb.append("[/COLOR]");
}
i += 8;
@ -779,6 +792,8 @@ public class UIUtils {
Matcher m = colorTag.matcher(s);
if (m.find()) {
color.start();
colorName = m.group(1);
color.index = sb.length();
i += m.end();
} else {
sb.append('[');

View File

@ -136,4 +136,153 @@
<color name="host_status_available">#ff669900</color>
<color name="host_status_unavailable">#ffcc0000</color>
<!-- CSS colors -->
<color name="aliceblue">#f0f8ff</color>
<color name="antiquewhite">#faebd7</color>
<color name="aqua">#00ffff</color>
<color name="aquamarine">#7fffd4</color>
<color name="azure">#f0ffff</color>
<color name="beige">#f5f5dc</color>
<color name="bisque">#ffe4c4</color>
<color name="black">#000000</color>
<color name="blanchedalmond">#ffebcd</color>
<color name="blue">#0000ff</color>
<color name="blueviolet">#8a2be2</color>
<color name="brown">#a52a2a</color>
<color name="burlywood">#deb887</color>
<color name="cadetblue">#5f9ea0</color>
<color name="chartreuse">#7fff00</color>
<color name="chocolate">#d2691e</color>
<color name="coral">#ff7f50</color>
<color name="cornflowerblue">#6495ed</color>
<color name="cornsilk">#fff8dc</color>
<color name="crimson">#dc143c</color>
<color name="cyan">#00ffff</color>
<color name="darkblue">#00008b</color>
<color name="darkcyan">#008b8b</color>
<color name="darkgoldenrod">#b8860b</color>
<color name="darkgray">#a9a9a9</color>
<color name="darkgreen">#006400</color>
<color name="darkgrey">#a9a9a9</color>
<color name="darkkhaki">#bdb76b</color>
<color name="darkmagenta">#8b008b</color>
<color name="darkolivegreen">#556b2f</color>
<color name="darkorange">#ff8c00</color>
<color name="darkorchid">#9932cc</color>
<color name="darkred">#8b0000</color>
<color name="darksalmon">#e9967a</color>
<color name="darkseagreen">#8fbc8f</color>
<color name="darkslateblue">#483d8b</color>
<color name="darkslategray">#2f4f4f</color>
<color name="darkslategrey">#2f4f4f</color>
<color name="darkturquoise">#00ced1</color>
<color name="darkviolet">#9400d3</color>
<color name="deeppink">#ff1493</color>
<color name="deepskyblue">#00bfff</color>
<color name="dimgray">#696969</color>
<color name="dimgrey">#696969</color>
<color name="dodgerblue">#1e90ff</color>
<color name="firebrick">#b22222</color>
<color name="floralwhite">#fffaf0</color>
<color name="forestgreen">#228b22</color>
<color name="fuchsia">#ff00ff</color>
<color name="gainsboro">#dcdcdc</color>
<color name="ghostwhite">#f8f8ff</color>
<color name="gold">#ffd700</color>
<color name="goldenrod">#daa520</color>
<color name="gray">#808080</color>
<color name="green">#008000</color>
<color name="greenyellow">#adff2f</color>
<color name="grey">#808080</color>
<color name="honeydew">#f0fff0</color>
<color name="hotpink">#ff69b4</color>
<color name="indianred">#cd5c5c</color>
<color name="indigo">#4b0082</color>
<color name="ivory">#fffff0</color>
<color name="khaki">#f0e68c</color>
<color name="lavender">#e6e6fa</color>
<color name="lavenderblush">#fff0f5</color>
<color name="lawngreen">#7cfc00</color>
<color name="lemonchiffon">#fffacd</color>
<color name="lightblue">#add8e6</color>
<color name="lightcoral">#f08080</color>
<color name="lightcyan">#e0ffff</color>
<color name="lightgoldenrodyellow">#fafad2</color>
<color name="lightgray">#d3d3d3</color>
<color name="lightgreen">#90ee90</color>
<color name="lightgrey">#d3d3d3</color>
<color name="lightpink">#ffb6c1</color>
<color name="lightsalmon">#ffa07a</color>
<color name="lightseagreen">#20b2aa</color>
<color name="lightskyblue">#87cefa</color>
<color name="lightslategray">#778899</color>
<color name="lightslategrey">#778899</color>
<color name="lightsteelblue">#b0c4de</color>
<color name="lightyellow">#ffffe0</color>
<color name="lime">#00ff00</color>
<color name="limegreen">#32cd32</color>
<color name="linen">#faf0e6</color>
<color name="magenta">#ff00ff</color>
<color name="maroon">#800000</color>
<color name="mediumaquamarine">#66cdaa</color>
<color name="mediumblue">#0000cd</color>
<color name="mediumorchid">#ba55d3</color>
<color name="mediumpurple">#9370db</color>
<color name="mediumseagreen">#3cb371</color>
<color name="mediumslateblue">#7b68ee</color>
<color name="mediumspringgreen">#00fa9a</color>
<color name="mediumturquoise">#48d1cc</color>
<color name="mediumvioletred">#c71585</color>
<color name="midnightblue">#191970</color>
<color name="mintcream">#f5fffa</color>
<color name="mistyrose">#ffe4e1</color>
<color name="moccasin">#ffe4b5</color>
<color name="navajowhite">#ffdead</color>
<color name="navy">#000080</color>
<color name="oldlace">#fdf5e6</color>
<color name="olive">#808000</color>
<color name="olivedrab">#6b8e23</color>
<color name="orange">#ffa500</color>
<color name="orangered">#ff4500</color>
<color name="orchid">#da70d6</color>
<color name="palegoldenrod">#eee8aa</color>
<color name="palegreen">#98fb98</color>
<color name="paleturquoise">#afeeee</color>
<color name="palevioletred">#db7093</color>
<color name="papayawhip">#ffefd5</color>
<color name="peachpuff">#ffdab9</color>
<color name="peru">#cd853f</color>
<color name="pink">#ffc0cb</color>
<color name="plum">#dda0dd</color>
<color name="powderblue">#b0e0e6</color>
<color name="purple">#800080</color>
<color name="red">#ff0000</color>
<color name="rosybrown">#bc8f8f</color>
<color name="royalblue">#4169e1</color>
<color name="saddlebrown">#8b4513</color>
<color name="salmon">#fa8072</color>
<color name="sandybrown">#f4a460</color>
<color name="seagreen">#2e8b57</color>
<color name="seashell">#fff5ee</color>
<color name="sienna">#a0522d</color>
<color name="silver">#c0c0c0</color>
<color name="skyblue">#87ceeb</color>
<color name="slateblue">#6a5acd</color>
<color name="slategray">#708090</color>
<color name="slategrey">#708090</color>
<color name="snow">#fffafa</color>
<color name="springgreen">#00ff7f</color>
<color name="steelblue">#4682b4</color>
<color name="tan">#d2b48c</color>
<color name="teal">#008080</color>
<color name="thistle">#d8bfd8</color>
<color name="tomato">#ff6347</color>
<color name="turquoise">#40e0d0</color>
<color name="violet">#ee82ee</color>
<color name="wheat">#f5deb3</color>
<!-- Already defined in section "General colors" : <color name="white">#ffffff</color>-->
<color name="whitesmoke">#f5f5f5</color>
<color name="yellow">#ffff00</color>
<color name="yellowgreen">#9acd32</color>
</resources>