Skip to content

Commit

Permalink
Properly show emoji in the notification ticker.
Browse files Browse the repository at this point in the history
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
dsandler committed Oct 19, 2012
1 parent a1f739e commit b9d3664
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ public abstract class Ticker {
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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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++) {
Expand Down
12 changes: 12 additions & 0 deletions tests/StatusBar/res/layout/notification_builder_test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,12 @@
android:text="long"
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"
Expand Down Expand Up @@ -571,6 +577,12 @@
android:text="haiku"
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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit b9d3664

Please sign in to comment.