Permalink
Browse files

Properly show emoji in the notification ticker.

Add an emoji test to StatusBarTest (working around some
difficulties in actually putting high-Unicode chars in the
layout xml).

Bug: 7378383
Change-Id: Ifce9844b26f67d2799521623e5161aa4dad69ed1
  • Loading branch information...
1 parent a1f739e commit b9d36649ca458cb5326a144fd88e26b92efba728 @dsandler dsandler committed Oct 19, 2012
@@ -53,6 +53,16 @@
private TextSwitcher mTextSwitcher;
private float mIconScale;
+ public static boolean isGraphicOrEmoji(char c) {
+ int gc = Character.getType(c);
+ return gc != Character.CONTROL
+ && gc != Character.FORMAT
+ && gc != Character.UNASSIGNED
+ && gc != Character.LINE_SEPARATOR
+ && gc != Character.PARAGRAPH_SEPARATOR
+ && gc != Character.SPACE_SEPARATOR;
+ }
+
private final class Segment {
StatusBarNotification notification;
Drawable icon;
@@ -68,7 +78,7 @@ StaticLayout getLayout(CharSequence substr) {
}
CharSequence rtrim(CharSequence substr, int start, int end) {
- while (end > start && !TextUtils.isGraphic(substr.charAt(end-1))) {
+ while (end > start && !isGraphicOrEmoji(substr.charAt(end-1))) {
end--;
}
if (end > start) {
@@ -101,7 +111,7 @@ CharSequence advance() {
this.first = false;
int index = this.next;
final int len = this.text.length();
- while (index < len && !TextUtils.isGraphic(this.text.charAt(index))) {
+ while (index < len && !isGraphicOrEmoji(this.text.charAt(index))) {
index++;
}
if (index >= len) {
@@ -136,7 +146,7 @@ CharSequence advance() {
this.text = text;
int index = 0;
final int len = text.length();
- while (index < len && !TextUtils.isGraphic(text.charAt(index))) {
+ while (index < len && !isGraphicOrEmoji(text.charAt(index))) {
index++;
}
this.current = index;
@@ -194,7 +204,8 @@ public void addEntry(StatusBarNotification n) {
final Drawable icon = StatusBarIconView.getIcon(mContext,
new StatusBarIcon(n.pkg, n.user, n.notification.icon, n.notification.iconLevel, 0,
n.notification.tickerText));
- final Segment newSegment = new Segment(n, icon, n.notification.tickerText);
+ final CharSequence text = n.notification.tickerText;
+ final Segment newSegment = new Segment(n, icon, text);
// If there's already a notification schedule for this package and id, remove it.
for (int i=0; i<mSegments.size(); i++) {
@@ -371,6 +371,12 @@
android:tag="Oh my goodness. SOMETHING HAPPENED!!!!"
/>
<RadioButton
+ android:id="@+id/text_emoji"
+ style="@style/FieldContents"
+ android:text="emoji"
+ android:tag="_ Cactus _ Cactus _"
+ />
+ <RadioButton
android:id="@+id/text_haiku"
style="@style/FieldContents"
android:text="haiku"
@@ -572,6 +578,12 @@
android:tag="sholes final approach\nlanding gear punted to flan\nrunway foam glistens"
/>
<RadioButton
+ android:id="@+id/ticker_emoji"
+ style="@style/FieldContents"
+ android:text="emoji"
+ android:tag="_ Cactus _ Cactus _"
+ />
+ <RadioButton
android:id="@+id/ticker_custom"
style="@style/FieldContents.Disabled"
android:text="custom view"
@@ -32,6 +32,7 @@
import android.os.Environment;
import android.os.Vibrator;
import android.os.Handler;
+import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.util.Log;
import android.net.Uri;
@@ -187,6 +188,20 @@ private void sendNotification(int id) {
mNM.notify(id, n);
}
+ private static CharSequence subst(CharSequence in, char ch, CharSequence sub) {
+ int i=0;
+ SpannableStringBuilder edit = new SpannableStringBuilder(in);
+ while (i<edit.length()) {
+ if (edit.charAt(i) == ch) {
+ edit.replace(i, i+1, sub);
+ i += sub.length();
+ } else {
+ i ++;
+ }
+ }
+ return edit;
+ }
+
private Notification buildNotification(int id) {
Notification.Builder b = new Notification.Builder(this);
@@ -223,19 +238,25 @@ private Notification buildNotification(int id) {
}
// title
- final String title = getRadioTag(R.id.group_title);
+ final CharSequence title = getRadioTag(R.id.group_title);
if (!TextUtils.isEmpty(title)) {
b.setContentTitle(title);
}
// text
- final String text = getRadioTag(R.id.group_text);
+ final CharSequence text = getRadioTag(R.id.group_text);
if (!TextUtils.isEmpty(text)) {
- b.setContentText(text);
+ if (getRadioChecked(R.id.group_text) == R.id.text_emoji) {
+ // UTF-16 for +1F335
+ b.setContentText(subst(text,
+ '_', "\ud83c\udf35"));
+ } else {
+ b.setContentText(text);
+ }
}
// info
- final String info = getRadioTag(R.id.group_info);
+ final CharSequence info = getRadioTag(R.id.group_info);
if (!TextUtils.isEmpty(info)) {
b.setContentInfo(info);
}
@@ -272,6 +293,11 @@ private Notification buildNotification(int id) {
case R.id.ticker_haiku:
b.setTicker(getRadioTag(R.id.group_ticker));
break;
+ case R.id.ticker_emoji:
+ // UTF-16 for +1F335
+ b.setTicker(subst(getRadioTag(R.id.group_ticker),
+ '_', "\ud83c\udf35"));
+ break;
case R.id.ticker_custom:
// TODO
break;
@@ -370,19 +396,19 @@ private int getRadioChecked(int id) {
return g.getCheckedRadioButtonId();
}
- private String getRadioTag(int id) {
+ private CharSequence getRadioTag(int id) {
final RadioGroup g = (RadioGroup)findViewById(id);
final View v = findViewById(g.getCheckedRadioButtonId());
- return (String)v.getTag();
+ return (CharSequence) v.getTag();
}
private int getRadioInt(int id, int def) {
- String str = getRadioTag(id);
+ CharSequence str = getRadioTag(id);
if (TextUtils.isEmpty(str)) {
return def;
} else {
try {
- return Integer.parseInt(str);
+ return Integer.parseInt(str.toString());
} catch (NumberFormatException ex) {
return def;
}

0 comments on commit b9d3664

Please sign in to comment.