use of android.text.style.StyleSpan 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.style.StyleSpan in project u2020 by JakeWharton.
the class ExternalIntentActivity method fillExtras.
private void fillExtras() {
Bundle extras = baseIntent.getExtras();
if (extras == null) {
extrasView.setText("None!");
} else {
Truss truss = new Truss();
for (String key : extras.keySet()) {
Object value = extras.get(key);
String valueString;
if (value.getClass().isArray()) {
valueString = Arrays.toString((Object[]) value);
} else {
valueString = value.toString();
}
truss.pushSpan(new StyleSpan(Typeface.BOLD));
truss.append(key).append(":\n");
truss.popSpan();
truss.append(valueString).append("\n\n");
}
extrasView.setText(truss.build());
}
}
use of android.text.style.StyleSpan in project AndroidChromium by JackyAndroid.
the class ExpandablePreferenceGroup method setGroupTitle.
/**
* Set the title for the preference group.
* @param resourceId The resource id of the text to use.
* @param count The number of entries the preference group contains.
*/
public void setGroupTitle(int resourceId, int count) {
SpannableStringBuilder spannable = new SpannableStringBuilder(getContext().getResources().getString(resourceId));
String prefCount = String.format(Locale.getDefault(), " - %d", count);
spannable.append(prefCount);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
spannable.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, spannable.length() - prefCount.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
} else {
spannable.setSpan(new TypefaceSpan("sans-serif-medium"), 0, spannable.length() - prefCount.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
// Color the first part of the title blue.
ForegroundColorSpan blueSpan = new ForegroundColorSpan(ApiCompatibilityUtils.getColor(getContext().getResources(), R.color.pref_accent_color));
spannable.setSpan(blueSpan, 0, spannable.length() - prefCount.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// Gray out the total count of items.
int gray = ApiCompatibilityUtils.getColor(getContext().getResources(), R.color.expandable_group_dark_gray);
spannable.setSpan(new ForegroundColorSpan(gray), spannable.length() - prefCount.length(), spannable.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
setTitle(spannable);
}
use of android.text.style.StyleSpan in project Etar-Calendar by Etar-Group.
the class DayView method getEventLayout.
/**
* Return the layout for a numbered event. Create it if not already existing
*/
private StaticLayout getEventLayout(StaticLayout[] layouts, int i, Event event, Paint paint, Rect r) {
if (i < 0 || i >= layouts.length) {
return null;
}
StaticLayout layout = layouts[i];
// re-layout of events at min height)
if (layout == null || r.width() != layout.getWidth()) {
SpannableStringBuilder bob = new SpannableStringBuilder();
if (event.title != null) {
// MAX - 1 since we add a space
bob.append(drawTextSanitizer(event.title.toString(), MAX_EVENT_TEXT_LEN - 1));
bob.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, bob.length(), 0);
bob.append(' ');
}
if (event.location != null) {
bob.append(drawTextSanitizer(event.location.toString(), MAX_EVENT_TEXT_LEN - bob.length()));
}
switch(event.selfAttendeeStatus) {
case Attendees.ATTENDEE_STATUS_INVITED:
paint.setColor(event.color);
break;
case Attendees.ATTENDEE_STATUS_DECLINED:
paint.setColor(mEventTextColor);
paint.setAlpha(Utils.DECLINED_EVENT_TEXT_ALPHA);
break;
// Your own events
case Attendees.ATTENDEE_STATUS_NONE:
case Attendees.ATTENDEE_STATUS_ACCEPTED:
case Attendees.ATTENDEE_STATUS_TENTATIVE:
default:
paint.setColor(mEventTextColor);
break;
}
// Leave a one pixel boundary on the left and right of the rectangle for the event
layout = new StaticLayout(bob, 0, bob.length(), new TextPaint(paint), r.width(), Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true, null, r.width());
layouts[i] = layout;
}
layout.getPaint().setAlpha(mEventsAlpha);
return layout;
}
use of android.text.style.StyleSpan in project OneSignal-Android-SDK by OneSignal.
the class GenerateNotification method createSummaryNotification.
// This summary notification will be visible instead of the normal one on pre-Android 7.0 devices.
static void createSummaryNotification(NotificationGenerationJob notifJob, OneSignalNotificationBuilder notifBuilder) {
boolean updateSummary = notifJob.restoring;
JSONObject gcmBundle = notifJob.jsonPayload;
String group = gcmBundle.optString("grp", null);
Random random = new Random();
PendingIntent summaryDeleteIntent = getNewActionPendingIntent(random.nextInt(), getNewBaseDeleteIntent(0).putExtra("summary", group));
Notification summaryNotification;
Integer summaryNotificationId = null;
String firstFullData = null;
Collection<SpannableString> summaryList = null;
OneSignalDbHelper dbHelper = OneSignalDbHelper.getInstance(currentContext);
Cursor cursor = null;
try {
SQLiteDatabase readableDb = dbHelper.getReadableDbWithRetries();
String[] retColumn = { NotificationTable.COLUMN_NAME_ANDROID_NOTIFICATION_ID, NotificationTable.COLUMN_NAME_FULL_DATA, NotificationTable.COLUMN_NAME_IS_SUMMARY, NotificationTable.COLUMN_NAME_TITLE, NotificationTable.COLUMN_NAME_MESSAGE };
String whereStr = // Where String
NotificationTable.COLUMN_NAME_GROUP_ID + " = ? AND " + NotificationTable.COLUMN_NAME_DISMISSED + " = 0 AND " + NotificationTable.COLUMN_NAME_OPENED + " = 0";
String[] whereArgs = { group };
// Make sure to omit any old existing matching android ids in-case we are replacing it.
if (!updateSummary && notifJob.getAndroidId() != -1)
whereStr += " AND " + NotificationTable.COLUMN_NAME_ANDROID_NOTIFICATION_ID + " <> " + notifJob.getAndroidId();
cursor = readableDb.query(NotificationTable.TABLE_NAME, retColumn, whereStr, whereArgs, // group by
null, // filter by row groups
null, // sort order, new to old
NotificationTable._ID + " DESC");
if (cursor.moveToFirst()) {
SpannableString spannableString;
summaryList = new ArrayList<>();
do {
if (cursor.getInt(cursor.getColumnIndex(NotificationTable.COLUMN_NAME_IS_SUMMARY)) == 1)
summaryNotificationId = cursor.getInt(cursor.getColumnIndex(NotificationTable.COLUMN_NAME_ANDROID_NOTIFICATION_ID));
else {
String title = cursor.getString(cursor.getColumnIndex(NotificationTable.COLUMN_NAME_TITLE));
if (title == null)
title = "";
else
title += " ";
// Html.fromHtml("<strong>" + line1Title + "</strong> " + gcmBundle.getString("alert"));
String msg = cursor.getString(cursor.getColumnIndex(NotificationTable.COLUMN_NAME_MESSAGE));
spannableString = new SpannableString(title + msg);
if (title.length() > 0)
spannableString.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, title.length(), 0);
summaryList.add(spannableString);
if (firstFullData == null)
firstFullData = cursor.getString(cursor.getColumnIndex(NotificationTable.COLUMN_NAME_FULL_DATA));
}
} while (cursor.moveToNext());
if (updateSummary && firstFullData != null) {
try {
gcmBundle = new JSONObject(firstFullData);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
} finally {
if (cursor != null && !cursor.isClosed())
cursor.close();
}
if (summaryNotificationId == null) {
summaryNotificationId = random.nextInt();
createSummaryIdDatabaseEntry(dbHelper, group, summaryNotificationId);
}
PendingIntent summaryContentIntent = getNewActionPendingIntent(random.nextInt(), createBaseSummaryIntent(summaryNotificationId, gcmBundle, group));
// 2 or more notifications with a group received, group them together as a single notification.
if (summaryList != null && ((updateSummary && summaryList.size() > 1) || (!updateSummary && summaryList.size() > 0))) {
int notificationCount = summaryList.size() + (updateSummary ? 0 : 1);
String summaryMessage = gcmBundle.optString("grp_msg", null);
if (summaryMessage == null)
summaryMessage = notificationCount + " new messages";
else
summaryMessage = summaryMessage.replace("$[notif_count]", "" + notificationCount);
NotificationCompat.Builder summaryBuilder = getBaseOneSignalNotificationBuilder(notifJob).compatBuilder;
if (updateSummary)
removeNotifyOptions(summaryBuilder);
// The summary is designed to fit all notifications.
// Default small and large icons are used instead of the payload options to enforce this.
summaryBuilder.setContentIntent(summaryContentIntent).setDeleteIntent(summaryDeleteIntent).setContentTitle(currentContext.getPackageManager().getApplicationLabel(currentContext.getApplicationInfo())).setContentText(summaryMessage).setNumber(notificationCount).setSmallIcon(getDefaultSmallIconId()).setLargeIcon(getDefaultLargeIcon()).setOnlyAlertOnce(updateSummary).setGroup(group).setGroupSummary(true);
if (!updateSummary)
summaryBuilder.setTicker(summaryMessage);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
// Add the latest notification to the summary
if (!updateSummary) {
String line1Title = gcmBundle.optString("title", null);
if (line1Title == null)
line1Title = "";
else
line1Title += " ";
String message = gcmBundle.optString("alert");
SpannableString spannableString = new SpannableString(line1Title + message);
if (line1Title.length() > 0)
spannableString.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, line1Title.length(), 0);
inboxStyle.addLine(spannableString);
}
for (SpannableString line : summaryList) inboxStyle.addLine(line);
inboxStyle.setBigContentTitle(summaryMessage);
summaryBuilder.setStyle(inboxStyle);
summaryNotification = summaryBuilder.build();
} else {
// First notification with this group key, post like a normal notification.
NotificationCompat.Builder summaryBuilder = notifBuilder.compatBuilder;
// We are re-using the notifBuilder from the normal notification so if a developer as an
// extender setup all the settings will carry over.
// Note: However their buttons will not carry over as we need to be setup with this new summaryNotificationId.
summaryBuilder.mActions.clear();
addNotificationActionButtons(gcmBundle, summaryBuilder, summaryNotificationId, group);
summaryBuilder.setContentIntent(summaryContentIntent).setDeleteIntent(summaryDeleteIntent).setOnlyAlertOnce(updateSummary).setGroup(group).setGroupSummary(true);
summaryNotification = summaryBuilder.build();
addXiaomiSettings(notifBuilder, summaryNotification);
}
NotificationManagerCompat.from(currentContext).notify(summaryNotificationId, summaryNotification);
}
Aggregations