Skip to content

Commit cc0106c

Browse files
dsandlerAndroid (Google) Code Review
authored andcommitted
Merge "Properly show emoji in the notification ticker." into jb-mr1-dev
2 parents 3c84c9b + b9d3664 commit cc0106c

File tree

3 files changed

+61
-12
lines changed

3 files changed

+61
-12
lines changed

packages/SystemUI/src/com/android/systemui/statusbar/phone/Ticker.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ public abstract class Ticker {
5353
private TextSwitcher mTextSwitcher;
5454
private float mIconScale;
5555

56+
public static boolean isGraphicOrEmoji(char c) {
57+
int gc = Character.getType(c);
58+
return gc != Character.CONTROL
59+
&& gc != Character.FORMAT
60+
&& gc != Character.UNASSIGNED
61+
&& gc != Character.LINE_SEPARATOR
62+
&& gc != Character.PARAGRAPH_SEPARATOR
63+
&& gc != Character.SPACE_SEPARATOR;
64+
}
65+
5666
private final class Segment {
5767
StatusBarNotification notification;
5868
Drawable icon;
@@ -68,7 +78,7 @@ StaticLayout getLayout(CharSequence substr) {
6878
}
6979

7080
CharSequence rtrim(CharSequence substr, int start, int end) {
71-
while (end > start && !TextUtils.isGraphic(substr.charAt(end-1))) {
81+
while (end > start && !isGraphicOrEmoji(substr.charAt(end-1))) {
7282
end--;
7383
}
7484
if (end > start) {
@@ -101,7 +111,7 @@ CharSequence advance() {
101111
this.first = false;
102112
int index = this.next;
103113
final int len = this.text.length();
104-
while (index < len && !TextUtils.isGraphic(this.text.charAt(index))) {
114+
while (index < len && !isGraphicOrEmoji(this.text.charAt(index))) {
105115
index++;
106116
}
107117
if (index >= len) {
@@ -136,7 +146,7 @@ CharSequence advance() {
136146
this.text = text;
137147
int index = 0;
138148
final int len = text.length();
139-
while (index < len && !TextUtils.isGraphic(text.charAt(index))) {
149+
while (index < len && !isGraphicOrEmoji(text.charAt(index))) {
140150
index++;
141151
}
142152
this.current = index;
@@ -194,7 +204,8 @@ public void addEntry(StatusBarNotification n) {
194204
final Drawable icon = StatusBarIconView.getIcon(mContext,
195205
new StatusBarIcon(n.pkg, n.user, n.notification.icon, n.notification.iconLevel, 0,
196206
n.notification.tickerText));
197-
final Segment newSegment = new Segment(n, icon, n.notification.tickerText);
207+
final CharSequence text = n.notification.tickerText;
208+
final Segment newSegment = new Segment(n, icon, text);
198209

199210
// If there's already a notification schedule for this package and id, remove it.
200211
for (int i=0; i<mSegments.size(); i++) {

tests/StatusBar/res/layout/notification_builder_test.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,12 @@
370370
android:text="long"
371371
android:tag="Oh my goodness. SOMETHING HAPPENED!!!!"
372372
/>
373+
<RadioButton
374+
android:id="@+id/text_emoji"
375+
style="@style/FieldContents"
376+
android:text="emoji"
377+
android:tag="_ Cactus _ Cactus _"
378+
/>
373379
<RadioButton
374380
android:id="@+id/text_haiku"
375381
style="@style/FieldContents"
@@ -571,6 +577,12 @@
571577
android:text="haiku"
572578
android:tag="sholes final approach\nlanding gear punted to flan\nrunway foam glistens"
573579
/>
580+
<RadioButton
581+
android:id="@+id/ticker_emoji"
582+
style="@style/FieldContents"
583+
android:text="emoji"
584+
android:tag="_ Cactus _ Cactus _"
585+
/>
574586
<RadioButton
575587
android:id="@+id/ticker_custom"
576588
style="@style/FieldContents.Disabled"

tests/StatusBar/src/com/android/statusbartest/NotificationBuilderTest.java

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import android.os.Environment;
3333
import android.os.Vibrator;
3434
import android.os.Handler;
35+
import android.text.SpannableStringBuilder;
3536
import android.text.TextUtils;
3637
import android.util.Log;
3738
import android.net.Uri;
@@ -187,6 +188,20 @@ private void sendNotification(int id) {
187188
mNM.notify(id, n);
188189
}
189190

191+
private static CharSequence subst(CharSequence in, char ch, CharSequence sub) {
192+
int i=0;
193+
SpannableStringBuilder edit = new SpannableStringBuilder(in);
194+
while (i<edit.length()) {
195+
if (edit.charAt(i) == ch) {
196+
edit.replace(i, i+1, sub);
197+
i += sub.length();
198+
} else {
199+
i ++;
200+
}
201+
}
202+
return edit;
203+
}
204+
190205
private Notification buildNotification(int id) {
191206
Notification.Builder b = new Notification.Builder(this);
192207

@@ -223,19 +238,25 @@ private Notification buildNotification(int id) {
223238
}
224239

225240
// title
226-
final String title = getRadioTag(R.id.group_title);
241+
final CharSequence title = getRadioTag(R.id.group_title);
227242
if (!TextUtils.isEmpty(title)) {
228243
b.setContentTitle(title);
229244
}
230245

231246
// text
232-
final String text = getRadioTag(R.id.group_text);
247+
final CharSequence text = getRadioTag(R.id.group_text);
233248
if (!TextUtils.isEmpty(text)) {
234-
b.setContentText(text);
249+
if (getRadioChecked(R.id.group_text) == R.id.text_emoji) {
250+
// UTF-16 for +1F335
251+
b.setContentText(subst(text,
252+
'_', "\ud83c\udf35"));
253+
} else {
254+
b.setContentText(text);
255+
}
235256
}
236257

237258
// info
238-
final String info = getRadioTag(R.id.group_info);
259+
final CharSequence info = getRadioTag(R.id.group_info);
239260
if (!TextUtils.isEmpty(info)) {
240261
b.setContentInfo(info);
241262
}
@@ -272,6 +293,11 @@ private Notification buildNotification(int id) {
272293
case R.id.ticker_haiku:
273294
b.setTicker(getRadioTag(R.id.group_ticker));
274295
break;
296+
case R.id.ticker_emoji:
297+
// UTF-16 for +1F335
298+
b.setTicker(subst(getRadioTag(R.id.group_ticker),
299+
'_', "\ud83c\udf35"));
300+
break;
275301
case R.id.ticker_custom:
276302
// TODO
277303
break;
@@ -370,19 +396,19 @@ private int getRadioChecked(int id) {
370396
return g.getCheckedRadioButtonId();
371397
}
372398

373-
private String getRadioTag(int id) {
399+
private CharSequence getRadioTag(int id) {
374400
final RadioGroup g = (RadioGroup)findViewById(id);
375401
final View v = findViewById(g.getCheckedRadioButtonId());
376-
return (String)v.getTag();
402+
return (CharSequence) v.getTag();
377403
}
378404

379405
private int getRadioInt(int id, int def) {
380-
String str = getRadioTag(id);
406+
CharSequence str = getRadioTag(id);
381407
if (TextUtils.isEmpty(str)) {
382408
return def;
383409
} else {
384410
try {
385-
return Integer.parseInt(str);
411+
return Integer.parseInt(str.toString());
386412
} catch (NumberFormatException ex) {
387413
return def;
388414
}

0 commit comments

Comments
 (0)