Search in sources :

Example 96 with SpannableString

use of android.text.SpannableString in project NewXmPluginSDK by MiEcosystem.

the class XQProgressHorizontalDialog method setProgress.

public void setProgress(int max, int progress) {
    if (mDeterminateProgress == null || max < 0)
        return;
    mDeterminateProgress.setMax(max);
    mDeterminateProgress.setProgress(progress);
    if (mProgressPercentFormat != null) {
        double percent = (double) progress / (double) max;
        SpannableString tmp = new SpannableString(mProgressPercentFormat.format(percent));
        mProgressPercent.setText(tmp);
    } else {
        mProgressPercent.setText("");
    }
//		if (max > 1) {
//			mProgressNumber.setText("" + (progress/1024) + "K/" + (max/1024)+"K");
//		} else {
//			mProgressNumber.setText("");
//		}
}
Also used : SpannableString(android.text.SpannableString)

Example 97 with SpannableString

use of android.text.SpannableString 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);
}
Also used : JSONException(org.json.JSONException) SpannableString(android.text.SpannableString) OSUtils.getResourceString(com.onesignal.OSUtils.getResourceString) Cursor(android.database.Cursor) Notification(android.app.Notification) BigInteger(java.math.BigInteger) SpannableString(android.text.SpannableString) JSONObject(org.json.JSONObject) Random(java.util.Random) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) StyleSpan(android.text.style.StyleSpan) NotificationCompat(android.support.v4.app.NotificationCompat) PendingIntent(android.app.PendingIntent)

Example 98 with SpannableString

use of android.text.SpannableString in project android_frameworks_base by ResurrectionRemix.

the class TextView method removeSuggestionSpans.

/**
     * Removes the suggestion spans.
     */
CharSequence removeSuggestionSpans(CharSequence text) {
    if (text instanceof Spanned) {
        Spannable spannable;
        if (text instanceof Spannable) {
            spannable = (Spannable) text;
        } else {
            spannable = new SpannableString(text);
            text = spannable;
        }
        SuggestionSpan[] spans = spannable.getSpans(0, text.length(), SuggestionSpan.class);
        for (int i = 0; i < spans.length; i++) {
            spannable.removeSpan(spans[i]);
        }
    }
    return text;
}
Also used : SpannableString(android.text.SpannableString) SuggestionSpan(android.text.style.SuggestionSpan) Spanned(android.text.Spanned) Spannable(android.text.Spannable) TextPaint(android.text.TextPaint) Paint(android.graphics.Paint)

Example 99 with SpannableString

use of android.text.SpannableString in project android_frameworks_base by ResurrectionRemix.

the class LinkSpec method addLinks.

/**
     *  Applies a regex to the text of a TextView turning the matches into
     *  links.  If links are found then UrlSpans are applied to the link
     *  text match areas, and the movement method for the text is changed
     *  to LinkMovementMethod.
     *
     *  @param text TextView whose text is to be marked-up with links.
     *  @param pattern Regex pattern to be used for finding links.
     *  @param defaultScheme The default scheme to be prepended to links if the link does not
     *                       start with one of the <code>schemes</code> given.
     *  @param schemes Array of schemes (eg <code>http://</code>) to check if the link found
     *                 contains a scheme. Passing a null or empty value means prepend defaultScheme
     *                 to all links.
     *  @param matchFilter  The filter that is used to allow the client code additional control
     *                      over which pattern matches are to be converted into links.
     *  @param transformFilter Filter to allow the client code to update the link found.
     */
public static final void addLinks(@NonNull TextView text, @NonNull Pattern pattern, @Nullable String defaultScheme, @Nullable String[] schemes, @Nullable MatchFilter matchFilter, @Nullable TransformFilter transformFilter) {
    SpannableString spannable = SpannableString.valueOf(text.getText());
    boolean linksAdded = addLinks(spannable, pattern, defaultScheme, schemes, matchFilter, transformFilter);
    if (linksAdded) {
        text.setText(spannable);
        addLinkMovementMethod(text);
    }
}
Also used : SpannableString(android.text.SpannableString)

Example 100 with SpannableString

use of android.text.SpannableString in project letterpress by Pixplicity.

the class FontUtil method applyTypeface.

@NonNull
public static SpannableString applyTypeface(@NonNull final String text, @NonNull final Typeface typeface) {
    SpannableString spannableString = new SpannableString(text);
    FontSpan span = new FontSpan(typeface);
    spannableString.setSpan(span, 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return spannableString;
}
Also used : SpannableString(android.text.SpannableString) NonNull(android.support.annotation.NonNull)

Aggregations

SpannableString (android.text.SpannableString)319 Spannable (android.text.Spannable)60 TextView (android.widget.TextView)60 View (android.view.View)56 StyleSpan (android.text.style.StyleSpan)53 ForegroundColorSpan (android.text.style.ForegroundColorSpan)45 TextPaint (android.text.TextPaint)25 Spanned (android.text.Spanned)22 TextAppearanceSpan (android.text.style.TextAppearanceSpan)21 Paint (android.graphics.Paint)20 ImageView (android.widget.ImageView)19 RelativeSizeSpan (android.text.style.RelativeSizeSpan)18 Bundle (android.os.Bundle)17 TypedValue (android.util.TypedValue)17 SmallTest (android.test.suitebuilder.annotation.SmallTest)16 Intent (android.content.Intent)15 SpannableStringBuilder (android.text.SpannableStringBuilder)15 ClickableSpan (android.text.style.ClickableSpan)13 URLSpan (android.text.style.URLSpan)13 LayoutInflater (android.view.LayoutInflater)13