Example usage for android.text.format DateUtils getRelativeDateTimeString

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

Introduction

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

Prototype

public static CharSequence getRelativeDateTimeString(Context c, long time, long minResolution,
        long transitionResolution, int flags) 

Source Link

Document

Return string describing the elapsed time since startTime formatted like "[relative time/date], [time]".

Usage

From source file:org.tigase.mobile.chat.ChatAdapter.java

@Override
public void bindView(View view, Context context, Cursor cursor) {
    ViewHolder holder = (ViewHolder) view.getTag();
    if (holder == null) {
        holder = new ViewHolder();
        view.setTag(holder);/*from w w  w  .  ja v a 2 s .co m*/
        holder.nickname = (TextView) view.findViewById(R.id.chat_item_nickname);
        holder.webview = (TextView) view.findViewById(R.id.chat_item_body);
        holder.timestamp = (TextView) view.findViewById(R.id.chat_item_timestamp);
        holder.avatar = (ImageView) view.findViewById(R.id.user_avatar);
        holder.msgStatus = (ImageView) view.findViewById(R.id.msgStatus);
    }

    final int state = cursor.getInt(cursor.getColumnIndex(ChatTableMetaData.FIELD_STATE));

    if (state == ChatTableMetaData.STATE_INCOMING || state == ChatTableMetaData.STATE_INCOMING_UNREAD) {
        final BareJID account = BareJID
                .bareJIDInstance(cursor.getString(cursor.getColumnIndex(ChatTableMetaData.FIELD_ACCOUNT)));
        final BareJID jid = BareJID
                .bareJIDInstance(cursor.getString(cursor.getColumnIndex(ChatTableMetaData.FIELD_JID)));
        setAvatarForJid(holder.avatar, jid, cursor);
        JaxmppCore jaxmpp = ((MessengerApplication) context.getApplicationContext()).getMultiJaxmpp()
                .get(account);
        RosterItem ri = jaxmpp.getRoster().get(jid);
        holder.nickname.setText(ri == null ? jid.toString() : RosterDisplayTools.getDisplayName(ri));

        holder.nickname.setTextColor(context.getResources().getColor(R.color.message_his_text));
        holder.webview.setTextColor(context.getResources().getColor(R.color.message_his_text));
        holder.timestamp.setTextColor(context.getResources().getColor(R.color.message_his_text));

        view.setBackgroundColor(context.getResources().getColor(R.color.message_his_background));
        holder.msgStatus.setVisibility(View.GONE);
    } else if (state == ChatTableMetaData.STATE_OUT_NOT_SENT || state == ChatTableMetaData.STATE_OUT_SENT) {
        final BareJID jid = BareJID
                .bareJIDInstance(cursor.getString(cursor.getColumnIndex(ChatTableMetaData.FIELD_AUTHOR_JID)));
        setAvatarForJid(holder.avatar, jid, cursor);
        holder.nickname.setText(this.nickname == null ? jid.getLocalpart() : this.nickname);

        holder.nickname.setTextColor(context.getResources().getColor(R.color.message_mine_text));
        holder.webview.setTextColor(context.getResources().getColor(R.color.message_mine_text));
        holder.timestamp.setTextColor(context.getResources().getColor(R.color.message_mine_text));

        if (state == ChatTableMetaData.STATE_OUT_SENT) {
            int recpt = cursor.getInt(cursor.getColumnIndex(ChatTableMetaData.FIELD_RECEIPT_STATUS));
            if (recpt == 1) {
                holder.msgStatus.setImageResource(R.drawable.message_sent);
                holder.msgStatus.setVisibility(View.GONE);
            } else if (recpt == 2) {
                holder.msgStatus.setImageResource(R.drawable.message_delivered);
                holder.msgStatus.setVisibility(View.VISIBLE);
            } else
                holder.msgStatus.setVisibility(View.GONE);
        } else if (state == ChatTableMetaData.STATE_OUT_NOT_SENT) {
            holder.msgStatus.setImageResource(R.drawable.message_not_sent);
            holder.msgStatus.setVisibility(View.VISIBLE);
        }

        view.setBackgroundColor(context.getResources().getColor(R.color.message_mine_background));
    } else {
        holder.msgStatus.setVisibility(View.GONE);
        holder.nickname.setText("?");
    }

    // java.text.DateFormat df = DateFormat.getTimeFormat(context);
    final String txt = EscapeUtils
            .escape(cursor.getString(cursor.getColumnIndex(ChatTableMetaData.FIELD_BODY)));

    Spanned sp = Html.fromHtml(txt.replace("\n", "<br/>"));
    holder.webview.setText(sp);
    // webview.setMinimumHeight(webview.getMeasuredHeight());

    // Date t = new
    // Date(cursor.getLong(cursor.getColumnIndex(ChatTableMetaData.FIELD_TIMESTAMP)));
    // holder.timestamp.setText(df.format(t));
    long ts = cursor.getLong(cursor.getColumnIndex(ChatTableMetaData.FIELD_TIMESTAMP));
    CharSequence tsStr =
            // DateUtils.isToday(ts)
            // ? DateUtils.getRelativeTimeSpanString(ts, System.currentTimeMillis(),
            // DateUtils.MINUTE_IN_MILLIS) :
            DateUtils.getRelativeDateTimeString(mContext, ts, DateUtils.MINUTE_IN_MILLIS,
                    DateUtils.WEEK_IN_MILLIS, 0);
    holder.timestamp.setText(tsStr);
}

From source file:org.tigase.mobile.muc.MucAdapter.java

@Override
public void bindView(View view, Context context, Cursor cursor) {
    ViewHolder holder = (ViewHolder) view.getTag();
    if (holder == null) {
        holder = new ViewHolder();
        view.setTag(holder);// w  w  w. ja v a2 s .c  o m
        holder.nickname = (TextView) view.findViewById(R.id.chat_item_nickname);
        holder.body = (TextView) view.findViewById(R.id.chat_item_body);
        holder.bodySelf = (TextView) view.findViewById(R.id.chat_item_body_self);
        holder.timestamp = (TextView) view.findViewById(R.id.chat_item_timestamp);
        holder.avatar = (ImageView) view.findViewById(R.id.user_avatar);
    }

    holder.nickname.setOnClickListener(nickameClickListener);

    final int state = cursor.getInt(cursor.getColumnIndex(ChatTableMetaData.FIELD_STATE));

    // byte[] avatarData =
    // cursor.getBlob(cursor.getColumnIndex(VCardsCacheTableMetaData.FIELD_DATA));
    holder.avatar.setVisibility(View.GONE);

    // final BareJID account =
    // BareJID.bareJIDInstance(cursor.getString(cursor.getColumnIndex(ChatTableMetaData.FIELD_ACCOUNT)));
    final String nick = cursor.getString(cursor.getColumnIndex(ChatTableMetaData.FIELD_AUTHOR_NICKNAME));

    // JaxmppCore jaxmpp = ((MessengerApplication)
    // context.getApplicationContext()).getMultiJaxmpp().get(account);
    holder.nickname.setText(nick);

    final String bd = cursor.getString(cursor.getColumnIndex(ChatTableMetaData.FIELD_BODY));

    if (nick != null && nick.equals(room.getNickname())) {
        holder.nickname.setTextColor(context.getResources().getColor(R.color.mucmessage_mine_nickname));
        holder.body.setTextColor(context.getResources().getColor(R.color.mucmessage_mine_text));
        holder.bodySelf.setTextColor(context.getResources().getColor(R.color.mucmessage_mine_text));
        holder.timestamp.setTextColor(context.getResources().getColor(R.color.mucmessage_mine_text));
        view.setBackgroundColor(context.getResources().getColor(R.color.mucmessage_mine_background));
    } else {
        int colorRes = getOccupantColor(nick);

        if (bd.contains(room.getNickname())) {
            view.setBackgroundColor(context.getResources().getColor(R.color.mucmessage_his_background_marked));
        } else {
            view.setBackgroundColor(context.getResources().getColor(R.color.mucmessage_his_background));
        }

        holder.nickname.setTextColor(context.getResources().getColor(colorRes));
        holder.body.setTextColor(context.getResources().getColor(R.color.mucmessage_his_text));
        holder.bodySelf.setTextColor(context.getResources().getColor(colorRes));
        holder.timestamp.setTextColor(context.getResources().getColor(R.color.mucmessage_his_text));
    }

    // java.text.DateFormat df = DateFormat.getTimeFormat(context);

    if (bd != null && bd.startsWith("/me ")) {
        holder.body.setVisibility(View.GONE);
        holder.bodySelf.setVisibility(View.VISIBLE);
        String t = bd.substring(4);
        final String txt = EscapeUtils.escape(t);
        holder.bodySelf.setText(Html.fromHtml(
                txt.replace("\n", "<br/>").replace(room.getNickname(), "<b>" + room.getNickname() + "</b>")));

    } else {
        holder.body.setVisibility(View.VISIBLE);
        holder.bodySelf.setVisibility(View.GONE);
        final String txt = EscapeUtils.escape(bd);
        holder.body.setText(Html.fromHtml(
                txt.replace("\n", "<br/>").replace(room.getNickname(), "<b>" + room.getNickname() + "</b>")));
    }

    // webview.setMinimumHeight(webview.getMeasuredHeight());

    // Date t = new
    // Date(cursor.getLong(cursor.getColumnIndex(ChatTableMetaData.FIELD_TIMESTAMP)));
    // holder.timestamp.setText(df.format(t));
    long ts = cursor.getLong(cursor.getColumnIndex(ChatTableMetaData.FIELD_TIMESTAMP));
    CharSequence tsStr =
            // DateUtils.isToday(ts)
            // ? DateUtils.getRelativeTimeSpanString(ts, System.currentTimeMillis(),
            // DateUtils.MINUTE_IN_MILLIS) :
            DateUtils.getRelativeDateTimeString(mContext, ts, DateUtils.MINUTE_IN_MILLIS,
                    DateUtils.WEEK_IN_MILLIS, 0);
    holder.timestamp.setText(tsStr);

}

From source file:com.vuze.android.remote.activity.RcmActivity.java

@Override
public void onExtraViewVisibilityChange(final View view, int visibility) {
    {//ww  w  .j  a  v  a2 s . co  m
        if (visibility != View.VISIBLE) {
            if (pullRefreshHandler != null) {
                pullRefreshHandler.removeCallbacksAndMessages(null);
                pullRefreshHandler = null;
            }
            return;
        }

        if (pullRefreshHandler != null) {
            pullRefreshHandler.removeCallbacks(null);
            pullRefreshHandler = null;
        }
        pullRefreshHandler = new Handler(Looper.getMainLooper());

        pullRefreshHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (isFinishing()) {
                    return;
                }

                long sinceMS = System.currentTimeMillis() - lastUpdated;
                String since = DateUtils.getRelativeDateTimeString(RcmActivity.this, lastUpdated,
                        DateUtils.SECOND_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, 0).toString();
                String s = getResources().getString(R.string.last_updated, since);

                TextView tvSwipeText = (TextView) view.findViewById(R.id.swipe_text);
                tvSwipeText.setText(s);

                if (pullRefreshHandler == null) {
                    return;
                }
                pullRefreshHandler.postDelayed(this,
                        sinceMS < DateUtils.MINUTE_IN_MILLIS ? DateUtils.SECOND_IN_MILLIS
                                : sinceMS < DateUtils.HOUR_IN_MILLIS ? DateUtils.MINUTE_IN_MILLIS
                                        : DateUtils.HOUR_IN_MILLIS);
            }
        }, 0);
    }
}

From source file:com.vuze.android.remote.fragment.TorrentListFragment.java

private LastUpdatedInfo getLastUpdatedString() {
    FragmentActivity activity = getActivity();
    if (activity == null) {
        return null;
    }//from  ww  w.jav a  2s  .  c  o m
    long lastUpdated = sessionInfo == null ? 0 : sessionInfo.getLastTorrentListReceivedOn();
    long sinceMS = System.currentTimeMillis() - lastUpdated;
    String since = DateUtils.getRelativeDateTimeString(activity, lastUpdated, DateUtils.SECOND_IN_MILLIS,
            DateUtils.WEEK_IN_MILLIS, 0).toString();
    String s = activity.getResources().getString(R.string.last_updated, since);

    return new LastUpdatedInfo(sinceMS, s);
}

From source file:com.vuze.android.remote.fragment.FilesFragment.java

@Override
public void onExtraViewVisibilityChange(final View view, int visibility) {
    if (pullRefreshHandler != null) {
        pullRefreshHandler.removeCallbacksAndMessages(null);
        pullRefreshHandler = null;//from w  ww .j a v a2s .  c o m
    }
    if (visibility != View.VISIBLE) {
        return;
    }

    pullRefreshHandler = new Handler(Looper.getMainLooper());
    pullRefreshHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            FragmentActivity activity = getActivity();
            if (activity == null) {
                return;
            }
            long sinceMS = System.currentTimeMillis() - lastUpdated;
            String since = DateUtils.getRelativeDateTimeString(activity, lastUpdated,
                    DateUtils.SECOND_IN_MILLIS, DateUtils.WEEK_IN_MILLIS, 0).toString();
            String s = activity.getResources().getString(R.string.last_updated, since);

            TextView tvSwipeText = (TextView) view.findViewById(R.id.swipe_text);
            tvSwipeText.setText(s);

            if (pullRefreshHandler != null) {
                pullRefreshHandler.postDelayed(this,
                        sinceMS < DateUtils.MINUTE_IN_MILLIS ? DateUtils.SECOND_IN_MILLIS
                                : sinceMS < DateUtils.HOUR_IN_MILLIS ? DateUtils.MINUTE_IN_MILLIS
                                        : DateUtils.HOUR_IN_MILLIS);
            }
        }
    }, 0);
}