use of android.text.SpannableString in project ETSMobile-Android2 by ApplETS.
the class ETSGcmListenerService method sendNotification.
/**
* Create and show a simple notification containing the received GCM message.
*
* @param data GCM message received.
*/
private void sendNotification(Bundle data) {
SecurePreferences securePreferences = new SecurePreferences(this);
Gson gson = new Gson();
String receivedNotifString = securePreferences.getString(Constants.RECEIVED_NOTIF, "");
ArrayList<MonETSNotification> receivedNotif = gson.fromJson(receivedNotifString, new TypeToken<ArrayList<MonETSNotification>>() {
}.getType());
if (receivedNotif == null) {
receivedNotif = new ArrayList<>();
}
MonETSNotification nouvelleNotification = getMonETSNotificationFromBundle(data);
receivedNotif.add(nouvelleNotification);
int numberOfNotifications = receivedNotif.size();
Intent intent = new Intent(this, NotificationActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_ets);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.school_48).setColor(getResources().getColor(R.color.red)).setContentTitle(getString(R.string.ets)).setContentText(getString(R.string.new_notifications)).setContentIntent(pendingIntent).setLargeIcon(icon).setAutoCancel(true).setNumber(numberOfNotifications);
NotificationCompat.InboxStyle inBoxStyle = new NotificationCompat.InboxStyle();
// Sets a title for the Inbox in expanded layout
String bigContentTitle = getString(R.string.notification_content_title, numberOfNotifications + "", (numberOfNotifications == 1 ? "" : "s"), (numberOfNotifications == 1 ? "" : "s"));
inBoxStyle.setBigContentTitle(bigContentTitle);
String username = ApplicationManager.userCredentials.getUsername();
Spannable sb = new SpannableString(username);
sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, username.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
inBoxStyle.setSummaryText(sb);
securePreferences.edit().putString(Constants.RECEIVED_NOTIF, gson.toJson(receivedNotif)).commit();
int minimumIndex = receivedNotif.size() - NUMBER_OF_NOTIF_TO_DISPLAY;
minimumIndex = minimumIndex < 0 ? 0 : minimumIndex;
for (int i = receivedNotif.size() - 1; i >= minimumIndex; i--) {
inBoxStyle.addLine(receivedNotif.get(i).getNotificationTexte());
}
if (numberOfNotifications > NUMBER_OF_NOTIF_TO_DISPLAY) {
int plusOthers = (numberOfNotifications - NUMBER_OF_NOTIF_TO_DISPLAY);
String plusOthersString = getString(R.string.others_notifications, plusOthers + "", (plusOthers == 1 ? "" : "s"));
Spannable others = new SpannableString(plusOthersString);
others.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, others.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
inBoxStyle.addLine(others);
}
mBuilder.setStyle(inBoxStyle);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
use of android.text.SpannableString in project android_packages_inputmethods_LatinIME by CyanogenMod.
the class SuggestionStripLayoutHelper method getStyledSuggestedWord.
private CharSequence getStyledSuggestedWord(final SuggestedWords suggestedWords, final int indexInSuggestedWords) {
if (indexInSuggestedWords >= suggestedWords.size()) {
return null;
}
final String word = suggestedWords.getLabel(indexInSuggestedWords);
// TODO: don't use the index to decide whether this is the auto-correction/typed word, as
// this is brittle
final boolean isAutoCorrection = suggestedWords.mWillAutoCorrect && indexInSuggestedWords == SuggestedWords.INDEX_OF_AUTO_CORRECTION;
final boolean isTypedWordValid = suggestedWords.mTypedWordValid && indexInSuggestedWords == SuggestedWords.INDEX_OF_TYPED_WORD;
if (!isAutoCorrection && !isTypedWordValid) {
return word;
}
final Spannable spannedWord = new SpannableString(word);
final int options = mSuggestionStripOptions;
if ((isAutoCorrection && (options & AUTO_CORRECT_BOLD) != 0) || (isTypedWordValid && (options & VALID_TYPED_WORD_BOLD) != 0)) {
addStyleSpan(spannedWord, BOLD_SPAN);
}
if (isAutoCorrection && (options & AUTO_CORRECT_UNDERLINE) != 0) {
addStyleSpan(spannedWord, UNDERLINE_SPAN);
}
return spannedWord;
}
use of android.text.SpannableString in project android_packages_inputmethods_LatinIME by CyanogenMod.
the class SpannableStringUtils method concatWithNonParagraphSuggestionSpansOnly.
/**
* Returns a CharSequence concatenating the specified CharSequences, retaining their
* SuggestionSpans that don't have the PARAGRAPH flag, but not other spans.
*
* This code is almost entirely taken from {@link TextUtils#concat(CharSequence...)}, except
* it calls copyNonParagraphSuggestionSpansFrom instead of {@link TextUtils#copySpansFrom}.
*/
public static CharSequence concatWithNonParagraphSuggestionSpansOnly(CharSequence... text) {
if (text.length == 0) {
return "";
}
if (text.length == 1) {
return text[0];
}
boolean spanned = false;
for (int i = 0; i < text.length; i++) {
if (text[i] instanceof Spanned) {
spanned = true;
break;
}
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < text.length; i++) {
sb.append(text[i]);
}
if (!spanned) {
return sb.toString();
}
SpannableString ss = new SpannableString(sb);
int off = 0;
for (int i = 0; i < text.length; i++) {
int len = text[i].length();
if (text[i] instanceof Spanned) {
copyNonParagraphSuggestionSpansFrom((Spanned) text[i], 0, len, ss, off);
}
off += len;
}
return new SpannedString(ss);
}
use of android.text.SpannableString in project android_packages_inputmethods_LatinIME by CyanogenMod.
the class TextInfoCompatUtilsTests method testGetCharSequence.
public void testGetCharSequence() {
final SpannableString text = new SpannableString(TEST_TEXT);
text.setSpan(TEST_STYLE_SPAN, TEST_STYLE_SPAN_START, TEST_STYLE_SPAN_END, TEST_STYLE_SPAN_FLAGS);
text.setSpan(TEST_URL_SPAN_URL, TEST_URL_SPAN_START, TEST_URL_SPAN_END, TEST_URL_SPAN_FLAGS);
final TextInfo textInfo = TextInfoCompatUtils.newInstance(text, TEST_CHAR_SEQUENCE_START, TEST_CHAR_SEQUENCE_END, TEST_COOKIE, TEST_SEQUENCE_NUMBER);
final Spanned expectedSpanned = (Spanned) text.subSequence(TEST_CHAR_SEQUENCE_START, TEST_CHAR_SEQUENCE_END);
final CharSequence actualCharSequence = TextInfoCompatUtils.getCharSequenceOrString(textInfo);
// This should be valid even if TextInfo#getCharSequence is not supported.
assertTrue(TextUtils.equals(expectedSpanned, actualCharSequence));
if (TextInfoCompatUtils.isCharSequenceSupported()) {
// This is valid only if TextInfo#getCharSequence is supported.
assertTrue("should be Spanned", actualCharSequence instanceof Spanned);
assertTrue(Arrays.equals(marshall(expectedSpanned), marshall(actualCharSequence)));
}
}
use of android.text.SpannableString in project android_packages_inputmethods_LatinIME by CyanogenMod.
the class SuggestionSpanUtils method getTextWithAutoCorrectionIndicatorUnderline.
@UsedForTesting
public static CharSequence getTextWithAutoCorrectionIndicatorUnderline(final Context context, final String text, @Nonnull final Locale locale) {
if (TextUtils.isEmpty(text) || OBJ_FLAG_AUTO_CORRECTION == null) {
return text;
}
final Spannable spannable = new SpannableString(text);
final SuggestionSpan suggestionSpan = new SuggestionSpan(context, locale, new String[] {}, /* suggestions */
OBJ_FLAG_AUTO_CORRECTION, null);
spannable.setSpan(suggestionSpan, 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING);
return spannable;
}
Aggregations