Example usage for android.content.res Resources getQuantityString

List of usage examples for android.content.res Resources getQuantityString

Introduction

In this page you can find the example usage for android.content.res Resources getQuantityString.

Prototype

@NonNull
public String getQuantityString(@PluralsRes int id, int quantity, Object... formatArgs)
        throws NotFoundException 

Source Link

Document

Formats the string necessary for grammatically correct pluralization of the given resource ID for the given quantity, using the given arguments.

Usage

From source file:com.ubergeek42.WeechatAndroid.service.Notificator.java

private static void addMissingMessageLine(int missingMessages, Resources res, MessagingStyle style) {
    if (missingMessages > 0) {
        String s = res.getQuantityString(R.plurals.hot_messages_missing, missingMessages, missingMessages);
        style.addMessage(s, 0, "");
    }/*from   w w  w. j av  a2 s  . c  om*/
}

From source file:com.twitter.sdk.android.tweetui.TweetDateUtils.java

/**
 * This method is not thread safe. It has been modified from the original to not rely on global
 * time state. If a timestamp is in the future we return it as an absolute date string. Within
 * the same second we return 0s/* ww w.  j a v  a2s  .c om*/
 *
 * @param res resource
 * @param currentTimeMillis timestamp for offset
 * @param timestamp timestamp
 * @return the relative time string
 */
static String getRelativeTimeString(Resources res, long currentTimeMillis, long timestamp) {
    final long diff = currentTimeMillis - timestamp;
    if (diff >= 0) {
        if (diff < DateUtils.MINUTE_IN_MILLIS) { // Less than a minute ago
            final int secs = (int) (diff / 1000);
            return res.getQuantityString(R.plurals.tw__time_secs, secs, secs);
        } else if (diff < DateUtils.HOUR_IN_MILLIS) { // Less than an hour ago
            final int mins = (int) (diff / DateUtils.MINUTE_IN_MILLIS);
            return res.getQuantityString(R.plurals.tw__time_mins, mins, mins);
        } else if (diff < DateUtils.DAY_IN_MILLIS) { // Less than a day ago
            final int hours = (int) (diff / DateUtils.HOUR_IN_MILLIS);
            return res.getQuantityString(R.plurals.tw__time_hours, hours, hours);
        } else {
            final Calendar now = Calendar.getInstance();
            now.setTimeInMillis(currentTimeMillis);
            final Calendar c = Calendar.getInstance();
            c.setTimeInMillis(timestamp);
            final Date d = new Date(timestamp);

            if (now.get(Calendar.YEAR) == c.get(Calendar.YEAR)) {
                // Same year
                return RELATIVE_DATE_FORMAT.formatShortDateString(res, d);
            } else {
                // Outside of our year
                return RELATIVE_DATE_FORMAT.formatLongDateString(res, d);
            }
        }
    }
    return RELATIVE_DATE_FORMAT.formatLongDateString(res, new Date(timestamp));
}

From source file:com.markupartist.sthlmtraveling.utils.DateTimeUtil.java

public static CharSequence formatShortDuration(final Resources res, final long millis) {
    if (millis >= HOUR_IN_MILLIS) {
        final int hours = (int) ((millis + 1800000) / HOUR_IN_MILLIS);
        return res.getQuantityString(R.plurals.duration_short_hours, hours, hours);
    } else if (millis >= MINUTE_IN_MILLIS) {
        final int minutes = (int) ((millis + 30000) / MINUTE_IN_MILLIS);
        return res.getQuantityString(R.plurals.duration_short_minutes, minutes, minutes);
    } else {//from w  w w .  j  a  v a2 s .c o m
        final int seconds = (int) ((millis + 500) / SECOND_IN_MILLIS);
        return res.getQuantityString(R.plurals.duration_seconds, seconds, seconds);
    }
}

From source file:com.markupartist.sthlmtraveling.utils.DateTimeUtil.java

/**
 * Return given duration in a human-friendly format. For example, "4
 * minutes" or "1 second". Returns only largest meaningful unit of time,
 * from seconds up to hours./*from   www.  j a  va 2  s .c  om*/
 * <p/>
 * From android.text.format.DateUtils
 */
public static CharSequence formatDuration(final Resources res, final long millis) {
    if (millis >= HOUR_IN_MILLIS) {
        final int hours = (int) ((millis + 1800000) / HOUR_IN_MILLIS);
        return res.getQuantityString(R.plurals.duration_hours, hours, hours);
    } else if (millis >= MINUTE_IN_MILLIS) {
        final int minutes = (int) ((millis + 30000) / MINUTE_IN_MILLIS);
        return res.getQuantityString(R.plurals.duration_minutes, minutes, minutes);
    } else {
        final int seconds = (int) ((millis + 500) / SECOND_IN_MILLIS);
        return res.getQuantityString(R.plurals.duration_seconds, seconds, seconds);
    }
}

From source file:com.stasbar.knowyourself.timer.CountingTimerView.java

private static String getQuantityString(Resources r, @PluralsRes int resId, int quantity) {
    return r.getQuantityString(resId, quantity, quantity);
}

From source file:com.markupartist.sthlmtraveling.utils.DateTimeUtil.java

/**
 * Format duration without rounding. Seconds are still rounded though.
 *
 * @param resources a resource//from   w w w  .j  av a 2 s.co  m
 * @param millis    duration in millis
 * @return A string representing the duration
 */
public static CharSequence formatDetailedDuration(@NonNull final Resources resources, long millis) {
    int days = 0, hours = 0, minutes = 0;
    if (millis > 0) {
        days = (int) TimeUnit.MILLISECONDS.toDays(millis);
        millis -= TimeUnit.DAYS.toMillis(days);
        hours = (int) TimeUnit.MILLISECONDS.toHours(millis);
        millis -= TimeUnit.HOURS.toMillis(hours);
        minutes = (int) TimeUnit.MILLISECONDS.toMinutes(millis);
    }
    if (days > 0) {
        return resources.getQuantityString(R.plurals.duration_days, days, days);
    }
    if (hours > 0) {
        if (minutes == 0) {
            return resources.getQuantityString(R.plurals.duration_short_hours, hours, hours);
        }
        return resources.getString(R.string.duration_short_hours_minutes, hours, minutes);
    }
    return resources.getQuantityString(R.plurals.duration_short_minutes, minutes, minutes);
}

From source file:it.mb.whatshare.Dialogs.java

/**
 * Shows a dialog containing the code received by goo.gl if any, or an error
 * message stating that the pairing operation failed.
 * /*www. j a  v  a  2 s  . co  m*/
 * @param googl
 *            the pairing code retrieved by goo.gl
 * @param activity
 *            the caller activity
 */
public static void onObtainPairingCode(final String googl, final MainActivity activity) {
    new PatchedDialogFragment() {
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            AlertDialog.Builder builder = getBuilder(activity);
            if (googl != null) {
                View layout = View.inflate(getContext(), R.layout.pairing_code_dialog, null);
                TextView message = (TextView) layout.findViewById(R.id.pairingCode);
                Typeface typeFace = Typeface.createFromAsset(activity.getAssets(), CUSTOM_TYPEFACE_PATH);
                message.setTypeface(typeFace);
                message.setText(googl);
                builder.setView(layout);
            } else {
                builder.setMessage(getResources().getString(R.string.code_dialog_fail));
            }
            builder.setPositiveButton(android.R.string.ok, new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (googl != null) {
                        Resources res = getResources();
                        String howManyTotal = res.getQuantityString(R.plurals.added_device,
                                activity.getInboundDevicesCount(), activity.getInboundDevicesCount());
                        Toast.makeText(getActivity(), res.getString(R.string.device_paired, howManyTotal),
                                Toast.LENGTH_LONG).show();
                    }
                }
            });
            return builder.create();
        }
    }.show(activity.getSupportFragmentManager(), "resultCode");
}

From source file:com.ruesga.rview.misc.Formatter.java

@BindingAdapter("addedVsDeleted")
public static void toAddedVsRemoved(TextView view, FileInfo info) {
    if (info == null) {
        view.setText(null);// w  w w  .  j  av  a  2  s  .  c  om
        return;
    }

    final Resources res = view.getResources();
    String added = null;
    if (info.linesInserted != null && info.linesInserted > 0) {
        added = res.getQuantityString(R.plurals.files_added, info.linesInserted, info.linesInserted);
    }
    String deleted = null;
    if (info.linesDeleted != null && info.linesDeleted > 0) {
        deleted = res.getQuantityString(R.plurals.files_deleted, info.linesDeleted, info.linesDeleted);
    }
    if (added == null && deleted == null) {
        added = res.getQuantityString(R.plurals.files_added, 0, 0);
        deleted = res.getQuantityString(R.plurals.files_deleted, 0, 0);
    }

    String txt;
    if (added != null && deleted != null) {
        txt = res.getString(R.string.added_vs_deleted, added, deleted);
    } else if (added != null) {
        txt = added;
    } else {
        txt = deleted;
    }
    view.setText(txt);
}

From source file:org.runbuddy.tomahawk.adapters.ViewHolder.java

private static String dateToString(Resources resources, Date date) {
    String s = "";
    if (date != null) {
        long diff = System.currentTimeMillis() - date.getTime();
        if (diff < 60000) {
            s += resources.getString(R.string.time_afewseconds);
        } else if (diff < 3600000) {
            long minutes = TimeUnit.MILLISECONDS.toMinutes(diff);
            s += resources.getQuantityString(R.plurals.time_minute, (int) minutes, minutes);
        } else if (diff < 86400000) {
            long hours = TimeUnit.MILLISECONDS.toHours(diff);
            s += resources.getQuantityString(R.plurals.time_hour, (int) hours, hours);
        } else {/*from w ww.  ja va2 s  . c o  m*/
            long days = TimeUnit.MILLISECONDS.toDays(diff);
            s += resources.getQuantityString(R.plurals.time_day, (int) days, days);
        }
    }
    return s;
}

From source file:com.ubergeek42.WeechatAndroid.service.Notificator.java

@AnyThread
@Cat//w  w  w.  j a  va  2s.c  o m
public static void showHot(boolean connected, int totalHotCount, int hotBufferCount,
        List<Hotlist.HotMessage> allMessages, Hotlist.HotBuffer hotBuffer, boolean newHighlight) {
    if (!P.notificationEnable)
        return;

    // https://developer.android.com/guide/topics/ui/notifiers/notifications.html#back-compat
    boolean canMakeBundledNotifications = Build.VERSION.SDK_INT >= 24;
    Resources res = context.getResources();

    int hotCount = hotBuffer.hotCount;
    List<HotMessage> messages = hotBuffer.messages;
    String fullName = hotBuffer.fullName;
    String shortName = hotBuffer.shortName;

    if (hotCount == 0) {
        manager.cancel(fullName, NOTIFICATION_HOT_ID);
        onNotificationDismissed(fullName);
        return;
    }

    ////////////////////////////////////////////////////////////////////////////////////////////

    // (re)build the parent? summary notification. in practice, it should never be visible on
    // Lollipop and later, except for the SubText part, which appears on the right of app name

    String appName = res.getString(R.string.app_name);
    String nMessagesInNBuffers = res.getQuantityString(R.plurals.messages, totalHotCount, totalHotCount)
            + res.getQuantityString(R.plurals.in_buffers, hotBufferCount, hotBufferCount);
    Builder summary = new Builder(context, NOTIFICATION_CHANNEL_HOTLIST)
            .setContentIntent(getIntentFor(NOTIFICATION_EXTRA_BUFFER_FULL_NAME_ANY))
            .setSmallIcon(R.drawable.ic_hot).setContentTitle(appName).setContentText(nMessagesInNBuffers)
            .setGroup(GROUP_KEY).setGroupSummary(true);

    if (canMakeBundledNotifications) {
        summary.setSubText(nMessagesInNBuffers);
    } else {
        MessagingStyle style = new MessagingStyle("");
        style.setConversationTitle(nMessagesInNBuffers);
        addMissingMessageLine(totalHotCount - allMessages.size(), res, style);
        for (HotMessage message : allMessages)
            style.addMessage(message.forFullList());
        summary.setStyle(style);

        if (newHighlight)
            makeNoise(summary, res, allMessages);
    }

    manager.notify(NOTIFICATION_HOT_ID, summary.build());

    if (!canMakeBundledNotifications)
        return;

    ////////////////////////////////////////////////////////////////////////////////////////////

    String newMessageInB = res.getQuantityString(R.plurals.hot_messages, hotCount, hotCount, shortName);
    Builder builder = new Builder(context, NOTIFICATION_CHANNEL_HOTLIST)
            .setContentIntent(getIntentFor(fullName)).setSmallIcon(R.drawable.ic_hot).setContentTitle(appName)
            .setContentText(newMessageInB).setDeleteIntent(getDismissIntentFor(fullName)).setGroup(GROUP_KEY)
            .setWhen(hotBuffer.lastMessageTimestamp);

    // messages hold the latest messages, don't show the reply button if user can't see any
    if (connected && messages.size() > 0)
        builder.addAction(getAction(context, fullName));

    MessagingStyle style = new MessagingStyle("");
    style.setConversationTitle(hotCount < 2 ? shortName : shortName + " (" + String.valueOf(hotCount) + ")");
    addMissingMessageLine(hotCount - messages.size(), res, style);
    for (HotMessage message : messages)
        style.addMessage(message.forBuffer());
    builder.setStyle(style);

    if (newHighlight)
        makeNoise(builder, res, messages);
    manager.notify(fullName, NOTIFICATION_HOT_ID, builder.build());
    onNotificationFired(fullName);
}