summaryrefslogtreecommitdiff
path: root/src/com/android/im/app/MessageView.java
blob: 9a42cbbc48d3823586b5771f36755a6a399ef194 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
 * Copyright (C) 2008 Esmertec AG.
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.im.app;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.provider.Im;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.text.style.URLSpan;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.im.R;

public class MessageView extends LinearLayout {

    private TextView mMessage;

    private Resources mResources;

    public MessageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        mMessage = (TextView) findViewById(R.id.message);

        mResources = getResources();
    }

    public URLSpan[] getMessageLinks() {
        return mMessage.getUrls();
    }

    public void bindIncomingMessage(String contact, String body, Date date,
            Markup smileyRes, boolean scrolling) {
        CharSequence message =  formatMessage(contact, body, date, smileyRes, scrolling);
        mMessage.setText(message);
        mMessage.setTextColor(mResources.getColor(R.color.chat_msg));
    }

    public void bindOutgoingMessage(String body, Date date, Markup smileyRes, boolean scrolling) {
        String contact = mResources.getString(R.string.me);
        CharSequence message = formatMessage(contact, body, date, smileyRes, scrolling);
        mMessage.setText(message);
        mMessage.setTextColor(mResources.getColor(R.color.chat_msg));
    }

    public void bindPresenceMessage(String contact, int type, boolean isGroupChat,
            boolean scrolling) {
        CharSequence message = formatPresenceUpdates(contact, type, isGroupChat, scrolling);
        mMessage.setText(message);
        mMessage.setTextColor(mResources.getColor(R.color.chat_msg_presence));
    }

    public void bindErrorMessage(int errCode) {
        mMessage.setText(R.string.msg_sent_failed);
        mMessage.setTextColor(mResources.getColor(R.color.error));
    }

    private CharSequence formatMessage(String contact, String body,
            Date date, Markup smileyRes, boolean scrolling) {
        if (body.indexOf('\r') != -1) {
            // first convert \r\n pair to \n, then single \r to \n.
            // here we can't use HideReturnsTransformationMethod because
            // it does only 1 to 1 transformation and is unable to handle
            // the "\r\n" case.
            body = body.replace("\r\n", "\n").replace('\r', '\n');
        }

        SpannableStringBuilder buf = new SpannableStringBuilder(contact);
        buf.append(": ");
        if (scrolling) {
            buf.append(body);
        } else {
            buf.setSpan(ChatView.STYLE_BOLD, 0, buf.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

            buf.append(smileyRes.markup(body));

            if (date != null) {
                appendTimeStamp(buf, date);
            }
        }
        return buf;
    }

    private void appendTimeStamp(SpannableStringBuilder buf, Date date) {
        DateFormat format = new SimpleDateFormat(mResources.getString(R.string.time_stamp));
        String dateStr = format.format(date);
        SpannableString spanText = new SpannableString(dateStr);
        int len = spanText.length();
        spanText.setSpan(new StyleSpan(Typeface.ITALIC),
                0, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanText.setSpan(new RelativeSizeSpan(0.8f),
                0, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spanText.setSpan(new ForegroundColorSpan(
                mResources.getColor(android.R.color.darker_gray)),
                0, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        buf.append('\n');
        buf.append(spanText);
    }

    private CharSequence formatPresenceUpdates(String contact, int type,
            boolean isGroupChat, boolean scrolling) {
        String body;
        switch (type) {
            case Im.MessageType.PRESENCE_AVAILABLE:
                body = mResources.getString(isGroupChat ? R.string.contact_joined
                        : R.string.contact_online, contact);
                break;

            case Im.MessageType.PRESENCE_AWAY:
                body = mResources.getString(R.string.contact_away, contact);
                break;

            case Im.MessageType.PRESENCE_DND:
                body = mResources.getString(R.string.contact_busy, contact);
                break;

            case Im.MessageType.PRESENCE_UNAVAILABLE:
                body = mResources.getString(isGroupChat ? R.string.contact_left
                        : R.string.contact_offline, contact);
                break;

            default:
                return null;
        }

        if (scrolling) {
            return body;
        } else {
            SpannableString spanText = new SpannableString(body);
            int len = spanText.length();
            spanText.setSpan(new StyleSpan(Typeface.ITALIC),
                    0, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            spanText.setSpan(new RelativeSizeSpan((float)0.8),
                    0, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            return spanText;
        }
    }
}