Example usage for android.text.format DateUtils getRelativeTimeSpanString

List of usage examples for android.text.format DateUtils getRelativeTimeSpanString

Introduction

In this page you can find the example usage for android.text.format DateUtils getRelativeTimeSpanString.

Prototype

public static CharSequence getRelativeTimeSpanString(Context c, long millis) 

Source Link

Document

Convenience function to return relative time string without preposition.

Usage

From source file:Main.java

public static CharSequence getRelativeTimeSpanString(Context context, Date date) {
    if (date != null)
        return DateUtils.getRelativeTimeSpanString(context, date.getTime());
    return "";
}

From source file:edu.cmu.cylab.starslinger.view.IntroductionFragment.java

private void drawUserData(int dirId, String name, byte[] photo, TextView textViewUserName, TextView textViewKey,
        ImageView imageViewPhoto, String keyId, long keyDate) {

    StringBuilder detailStr = new StringBuilder();
    if (!CryptTools.isNullKeyId(keyId)) {
        if (keyDate > 0) {
            detailStr.append(getString(R.string.label_Key)).append(" ")
                    .append(DateUtils.getRelativeTimeSpanString(getActivity(), keyDate));
        }//ww w  . j  av  a2  s .  com
    }

    // draw name
    if (textViewUserName != null)
        textViewUserName.setText(getString(dirId) + " " + name);

    // draw keys
    if (textViewKey != null)
        textViewKey.setText(detailStr);

    // draw photo
    if (photo != null) {
        try {
            Bitmap bm = BitmapFactory.decodeByteArray(photo, 0, photo.length, null);
            imageViewPhoto.setImageBitmap(bm);
        } catch (OutOfMemoryError e) {
            imageViewPhoto.setImageDrawable(getResources().getDrawable(R.drawable.ic_silhouette));
        }
    } else {
        imageViewPhoto.setImageDrawable(getResources().getDrawable(R.drawable.ic_silhouette));
    }
}

From source file:mp.paschalis.App.java

/**
 * @param pTimestamp// w  w  w  . j a  va2  s. c  o m
 * @return
 */
public static String makeTimeStampHumanReadble(Context context, String pTimestamp) {

    try {

        SimpleDateFormat datetimeFormatter1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        java.util.Date lFromDate1 = datetimeFormatter1.parse(pTimestamp);

        CharSequence humansTime = DateUtils.getRelativeTimeSpanString(context, lFromDate1.getTime());

        return humansTime.toString();
    } catch (Exception e) {
        return pTimestamp;
    }

}

From source file:edu.cmu.cylab.starslinger.view.ComposeFragment.java

private void drawUserData(int dirId, String name, byte[] photo, TextView textViewUserName, TextView textViewKey,
        ImageView imageViewPhoto, String keyId, long keyDate) {

    StringBuilder detailStr = new StringBuilder();
    if (!CryptTools.isNullKeyId(keyId)) {
        if (keyDate > 0) {
            detailStr.append(getString(R.string.label_Key)).append(" ").append(
                    DateUtils.getRelativeTimeSpanString(getActivity().getApplicationContext(), keyDate));
        }/*w w w .  j  av  a2  s .  co m*/
    }

    // draw name
    if (textViewUserName != null) {
        textViewUserName.setText(getString(dirId) + " " + name);
    }

    // draw keys
    if (textViewKey != null) {
        textViewKey.setText(detailStr);
    }

    // draw photo
    if (photo != null) {
        try {
            Bitmap bm = BitmapFactory.decodeByteArray(photo, 0, photo.length, null);
            imageViewPhoto.setImageBitmap(bm);
        } catch (OutOfMemoryError e) {
            imageViewPhoto.setImageDrawable(getResources().getDrawable(R.drawable.ic_silhouette));
        }
    } else {
        imageViewPhoto.setImageDrawable(getResources().getDrawable(R.drawable.ic_silhouette));
    }
}

From source file:com.android.mail.browse.ConversationItemView.java

private void bind(final ConversationItemViewModel header, final ControllableActivity activity,
        final ConversationItemAreaClickListener conversationItemAreaClickListener,
        final ConversationCheckedSet set, final Folder folder, final int checkboxOrSenderImage,
        boolean swipeEnabled, final boolean importanceMarkersEnabled, final boolean showChevronsEnabled,
        final AnimatedAdapter adapter, final int backgroundOverrideResId, final Bitmap photoBitmap,
        final boolean useFullMargins, final boolean dividerEnabled) {
    mBackgroundOverrideResId = backgroundOverrideResId;
    mPhotoBitmap = photoBitmap;/*from w w w .j a va2  s  .  c  o m*/
    mConversationItemAreaClickListener = conversationItemAreaClickListener;
    mDividerEnabled = dividerEnabled;

    if (mHeader != null) {
        Utils.traceBeginSection("unbind");
        final boolean newlyBound = header.conversation.id != mHeader.conversation.id;
        // If this was previously bound to a different conversation, remove any contact photo
        // manager requests.
        if (newlyBound || (!mHeader.displayableNames.equals(header.displayableNames))) {
            mSendersImageView.getContactDrawable().unbind();
        }

        if (newlyBound) {
            // Stop the photo flip animation
            final boolean showSenders = !mChecked;
            mSendersImageView.reset(showSenders);
        }
        Utils.traceEndSection();
    }
    mCoordinates = null;
    mHeader = header;
    mActivity = activity;
    mCheckedConversationSet = set;
    if (mCheckedConversationSet != null) {
        mCheckedConversationSet.addObserver(this);
    }
    mDisplayedFolder = folder;
    mStarEnabled = folder != null && !folder.isTrash();
    mSwipeEnabled = swipeEnabled;
    mAdapter = adapter;

    Utils.traceBeginSection("drawables");
    mSendersImageView.getContactDrawable().setBitmapCache(mAdapter.getSendersImagesCache());
    mSendersImageView.getContactDrawable().setContactResolver(mAdapter.getContactResolver());
    Utils.traceEndSection();

    if (checkboxOrSenderImage == ConversationListIcon.SENDER_IMAGE) {
        mGadgetMode = ConversationItemViewCoordinates.GADGET_CONTACT_PHOTO;
    } else {
        mGadgetMode = ConversationItemViewCoordinates.GADGET_NONE;
    }

    Utils.traceBeginSection("folder displayer");
    // Initialize folder displayer.
    if (mHeader.folderDisplayer == null) {
        mHeader.folderDisplayer = new ConversationItemFolderDisplayer(mContext, mAdapter.getBidiFormatter());
    } else {
        mHeader.folderDisplayer.reset();
    }
    Utils.traceEndSection();

    final int ignoreFolderType;
    if (mDisplayedFolder.isInbox()) {
        ignoreFolderType = FolderType.INBOX;
    } else {
        ignoreFolderType = -1;
    }

    Utils.traceBeginSection("load folders");
    mHeader.folderDisplayer.loadConversationFolders(mHeader.conversation, mDisplayedFolder.folderUri,
            ignoreFolderType);
    Utils.traceEndSection();

    if (mHeader.showDateText) {
        Utils.traceBeginSection("relative time");
        mHeader.dateText = DateUtils.getRelativeTimeSpanString(mContext, mHeader.conversation.dateMs);
        Utils.traceEndSection();
    } else {
        mHeader.dateText = "";
    }

    Utils.traceBeginSection("config setup");
    mConfig = new ConversationItemViewCoordinates.Config().withGadget(mGadgetMode)
            .setUseFullMargins(useFullMargins);
    if (header.folderDisplayer.hasVisibleFolders()) {
        mConfig.showFolders();
    }
    if (header.hasBeenForwarded || header.hasBeenRepliedTo || header.isInvite) {
        mConfig.showReplyState();
    }
    if (mHeader.conversation.color != 0) {
        mConfig.showColorBlock();
    }

    // Importance markers and chevrons (personal level indicators).
    mHeader.personalLevelBitmap = null;
    final int personalLevel = mHeader.conversation.personalLevel;
    final boolean isImportant = mHeader.conversation.priority == UIProvider.ConversationPriority.IMPORTANT;
    final boolean useImportantMarkers = isImportant && importanceMarkersEnabled;
    if (showChevronsEnabled && personalLevel == UIProvider.ConversationPersonalLevel.ONLY_TO_ME) {
        mHeader.personalLevelBitmap = useImportantMarkers ? IMPORTANT_ONLY_TO_ME : ONLY_TO_ME;
    } else if (showChevronsEnabled && personalLevel == UIProvider.ConversationPersonalLevel.TO_ME_AND_OTHERS) {
        mHeader.personalLevelBitmap = useImportantMarkers ? IMPORTANT_TO_ME_AND_OTHERS : TO_ME_AND_OTHERS;
    } else if (useImportantMarkers) {
        mHeader.personalLevelBitmap = IMPORTANT;
    }
    if (mHeader.personalLevelBitmap != null) {
        mConfig.showPersonalIndicator();
    }
    Utils.traceEndSection();

    Utils.traceBeginSection("content description");
    setContentDescription();
    Utils.traceEndSection();
    requestLayout();
}

From source file:cn.suishen.email.activity.MessageViewFragmentBase.java

protected void updateHeaderView(Message message) {
    mSubjectView.setText(message.mSubject);
    final Address from = Address.unpackFirst(message.mFrom);

    // Set sender address/display name
    // Note we set " " for empty field, so TextView's won't get squashed.
    // Otherwise their height will be 0, which breaks the layout.
    if (from != null) {
        final String fromFriendly = from.toFriendly();
        final String fromAddress = from.getAddress();
        mFromNameView.setText(fromFriendly);
        mFromAddressView.setText(fromFriendly.equals(fromAddress) ? " " : fromAddress);
    } else {/*from w  w  w  . ja va2s  .  c o  m*/
        mFromNameView.setText(" ");
        mFromAddressView.setText(" ");
    }
    mDateTimeView.setText(DateUtils.getRelativeTimeSpanString(mContext, message.mTimeStamp).toString());

    // To/Cc/Bcc
    final Resources res = mContext.getResources();
    final SpannableStringBuilder ssb = new SpannableStringBuilder();
    final String friendlyTo = Address.toFriendly(Address.unpack(message.mTo));
    final String friendlyCc = Address.toFriendly(Address.unpack(message.mCc));
    final String friendlyBcc = Address.toFriendly(Address.unpack(message.mBcc));

    if (!TextUtils.isEmpty(friendlyTo)) {
        Utility.appendBold(ssb, res.getString(R.string.message_view_to_label));
        ssb.append(" ");
        ssb.append(friendlyTo);
    }
    if (!TextUtils.isEmpty(friendlyCc)) {
        ssb.append("  ");
        Utility.appendBold(ssb, res.getString(R.string.message_view_cc_label));
        ssb.append(" ");
        ssb.append(friendlyCc);
    }
    if (!TextUtils.isEmpty(friendlyBcc)) {
        ssb.append("  ");
        Utility.appendBold(ssb, res.getString(R.string.message_view_bcc_label));
        ssb.append(" ");
        ssb.append(friendlyBcc);
    }
    mAddressesView.setText(ssb);
}

From source file:com.tct.mail.ui.ConversationListFragment.java

private String getElapseTime(long time) {
    long now = System.currentTimeMillis();
    long elapseTime = now - time;
    String displayTime;//from  w  w w . ja v  a 2  s .  co m
    if (elapseTime < 0) {
        // abnormal time, this may occur when user change system time to a wrong time
        displayTime = (String) DateUtils.getRelativeTimeSpanString(mActivity.getActivityContext(), time);
    } else if (elapseTime < DateUtils.DAY_IN_MILLIS) {
        //within one day
        displayTime = (String) DateUtils.getRelativeTimeSpanString(mActivity.getActivityContext(), time);
        displayTime = mActivity.getActivityContext().getString(R.string.conversation_time_elapse_today) + ", "
                + displayTime;
    } else {
        //beyond one day
        java.text.DateFormat timeFormat = DateFormat.getTimeFormat(mActivity.getActivityContext());
        Date date = new Date(time);
        String dateText = DateUtils.formatDateTime(mActivity.getActivityContext(), time,
                DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_MONTH);
        displayTime = dateText + ", " + timeFormat.format(date);
    }

    return displayTime;
}

From source file:com.tct.mail.browse.ConversationItemView.java

private String getElapseTime() {
    long time = mHeader.conversation.dateMs;
    long now = System.currentTimeMillis();
    long elapseTime = now - time;
    String displayTime;/* w w  w  . ja v  a 2s.  c  o  m*/
    if (elapseTime < 0) {
        // abnormal time, this may occur when user change system time to a wrong time
        displayTime = (String) DateUtils.getRelativeTimeSpanString(mContext, time);
    } else if (elapseTime < DateUtils.MINUTE_IN_MILLIS) {
        // within one minute
        displayTime = mContext.getString(R.string.conversation_time_elapse_just_now);
    } else if (elapseTime < DateUtils.HOUR_IN_MILLIS) {
        //with in one hour
        int min = (int) (elapseTime / DateUtils.MINUTE_IN_MILLIS);
        displayTime = String.format(mContext.getString(R.string.conversation_time_elapse_minute), min);
    } else if (elapseTime < DateUtils.DAY_IN_MILLIS) {
        //within one day
        displayTime = (String) DateUtils.getRelativeTimeSpanString(mContext, time);
    } else {
        //beyond one day
        displayTime = (String) DateUtils.getRelativeTimeSpanString(mContext, time);
    }

    return displayTime;
}