2010-03-06 11:16:35 -05:00
|
|
|
/*
|
2010-03-12 14:35:25 -05:00
|
|
|
Yaaic - Yet Another Android IRC Client
|
2010-03-06 11:16:35 -05:00
|
|
|
|
2011-02-05 07:00:12 -05:00
|
|
|
Copyright 2009-2011 Sebastian Kaspari
|
2010-03-06 11:16:35 -05:00
|
|
|
|
|
|
|
This file is part of Yaaic.
|
|
|
|
|
|
|
|
Yaaic is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Yaaic is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Yaaic. If not, see <http://www.gnu.org/licenses/>.
|
2011-01-25 15:02:27 -05:00
|
|
|
*/
|
2010-03-06 11:16:35 -05:00
|
|
|
package org.yaaic.model;
|
|
|
|
|
2010-03-13 14:15:09 -05:00
|
|
|
import java.util.Date;
|
|
|
|
|
2011-03-17 20:30:14 -04:00
|
|
|
import org.yaaic.utils.MircColors;
|
2011-03-17 23:08:21 -04:00
|
|
|
import org.yaaic.utils.Smilies;
|
2011-03-15 17:52:20 -04:00
|
|
|
|
2010-03-06 12:58:27 -05:00
|
|
|
import android.content.Context;
|
2010-03-08 15:36:36 -05:00
|
|
|
import android.graphics.Typeface;
|
2010-03-06 12:58:27 -05:00
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.text.Spannable;
|
2010-03-06 11:52:22 -05:00
|
|
|
import android.text.SpannableString;
|
2011-03-17 20:43:10 -04:00
|
|
|
import android.text.TextUtils;
|
2010-03-06 14:52:54 -05:00
|
|
|
import android.text.style.ForegroundColorSpan;
|
2010-03-06 12:58:27 -05:00
|
|
|
import android.text.style.ImageSpan;
|
2010-03-08 15:36:36 -05:00
|
|
|
import android.widget.TextView;
|
2010-03-06 11:52:22 -05:00
|
|
|
|
2010-03-06 11:16:35 -05:00
|
|
|
/**
|
|
|
|
* A channel or server message
|
|
|
|
*
|
|
|
|
* @author Sebastian Kaspari <sebastian@yaaic.org>
|
|
|
|
*/
|
2011-03-11 16:07:39 -05:00
|
|
|
public class Message
|
|
|
|
{
|
2011-01-25 15:02:27 -05:00
|
|
|
public static final int COLOR_GREEN = 0xFF458509;
|
|
|
|
public static final int COLOR_RED = 0xFFcc0000;
|
|
|
|
public static final int COLOR_BLUE = 0xFF729fcf;
|
|
|
|
public static final int COLOR_YELLOW = 0xFFbe9b01;
|
|
|
|
public static final int COLOR_GREY = 0xFFaaaaaa;
|
|
|
|
public static final int COLOR_DEFAULT = 0xFFeeeeee;
|
|
|
|
|
2011-02-22 11:31:51 -05:00
|
|
|
/* normal message, this is the default */
|
|
|
|
public static final int TYPE_MESSAGE = 0;
|
2011-03-06 07:47:14 -05:00
|
|
|
|
2011-02-22 11:31:51 -05:00
|
|
|
/* join, part or quit */
|
|
|
|
public static final int TYPE_MISC = 1;
|
|
|
|
|
2011-03-04 10:50:59 -05:00
|
|
|
/* Some are light versions because dark colors hardly readable on
|
|
|
|
* Yaaic's dark background */
|
2010-11-18 12:52:19 -05:00
|
|
|
public static final int[] colors = {
|
2011-01-25 15:02:27 -05:00
|
|
|
0xFFffffff, // White
|
|
|
|
0xFFffff00, // Yellow
|
|
|
|
0xFFff00ff, // Fuchsia
|
|
|
|
0xFFff0000, // Red
|
|
|
|
0xFFc0c0c0, // Silver
|
|
|
|
0xFF808080, // Gray
|
|
|
|
0xFF808000, // Olive
|
2011-03-04 10:50:59 -05:00
|
|
|
0xFFC040C0, // Light Purple
|
|
|
|
0xFFC04040, // Light Maroon
|
2011-01-25 15:02:27 -05:00
|
|
|
0xFF00ffff, // Agua
|
2011-03-04 10:50:59 -05:00
|
|
|
0xFF80ff80, // Light Lime
|
2011-01-25 15:02:27 -05:00
|
|
|
0xFF008080, // Teal
|
|
|
|
0xFF008000, // Green
|
2011-03-04 10:50:59 -05:00
|
|
|
0xFF8484FF, // Light Blue
|
|
|
|
0xFF6060D0, // Light Navy
|
2011-01-25 15:02:27 -05:00
|
|
|
0xFF000000, // Black
|
2010-11-18 12:52:19 -05:00
|
|
|
};
|
2010-09-09 14:41:55 -04:00
|
|
|
|
2011-03-11 16:07:39 -05:00
|
|
|
public static final int NO_ICON = -1;
|
|
|
|
public static final int NO_TYPE = -1;
|
|
|
|
public static final int NO_COLOR = -1;
|
|
|
|
|
2011-01-25 15:02:27 -05:00
|
|
|
private final String text;
|
|
|
|
private final String sender;
|
2010-11-18 12:52:19 -05:00
|
|
|
private SpannableString canvas;
|
2011-03-14 17:32:28 -04:00
|
|
|
private long timestamp;
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2011-03-11 16:07:39 -05:00
|
|
|
private int color = NO_COLOR;
|
|
|
|
private int type = NO_ICON;
|
|
|
|
private int icon = NO_TYPE;
|
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
2011-02-22 11:31:51 -05:00
|
|
|
* Create a new message without an icon defaulting to TYPE_MESSAGE
|
2010-11-18 12:52:19 -05:00
|
|
|
*
|
|
|
|
* @param text
|
|
|
|
*/
|
|
|
|
public Message(String text)
|
|
|
|
{
|
2011-02-22 11:31:51 -05:00
|
|
|
this(text, null, TYPE_MESSAGE);
|
2010-11-18 12:52:19 -05:00
|
|
|
}
|
2011-02-22 11:31:51 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new message without an icon with a specific type
|
|
|
|
*
|
|
|
|
* @param text
|
|
|
|
* @param type Message type
|
|
|
|
*/
|
|
|
|
public Message(String text, int type)
|
|
|
|
{
|
|
|
|
this(text, null, type);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new message sent by a user, without an icon,
|
|
|
|
* defaulting to TYPE_MESSAGE
|
|
|
|
*
|
|
|
|
* @param text
|
|
|
|
* @param sender
|
|
|
|
*/
|
|
|
|
public Message(String text, String sender)
|
|
|
|
{
|
|
|
|
this(text, sender, TYPE_MESSAGE);
|
|
|
|
}
|
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
|
|
|
* Create a new message sent by a user without an icon
|
|
|
|
*
|
|
|
|
* @param text
|
|
|
|
* @param sender
|
2011-02-22 11:31:51 -05:00
|
|
|
* @param type Message type
|
2010-11-18 12:52:19 -05:00
|
|
|
*/
|
2011-02-22 11:31:51 -05:00
|
|
|
public Message(String text, String sender, int type)
|
2010-11-18 12:52:19 -05:00
|
|
|
{
|
|
|
|
this.text = text;
|
|
|
|
this.sender = sender;
|
|
|
|
this.timestamp = new Date().getTime();
|
2011-02-22 11:31:51 -05:00
|
|
|
this.type = type;
|
2010-11-18 12:52:19 -05:00
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
|
|
|
* Set the message's icon
|
|
|
|
*/
|
|
|
|
public void setIcon(int icon)
|
|
|
|
{
|
|
|
|
this.icon = icon;
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
|
|
|
* Get the message's icon
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public int getIcon()
|
|
|
|
{
|
|
|
|
return icon;
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
|
|
|
* Get the text of this message
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public String getText()
|
|
|
|
{
|
|
|
|
return text;
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2011-02-22 11:31:51 -05:00
|
|
|
/**
|
|
|
|
* Get the type of this message
|
|
|
|
*
|
|
|
|
* @return One of Message.TYPE_*
|
|
|
|
*/
|
|
|
|
public int getType()
|
|
|
|
{
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
|
|
|
* Set the color of this message
|
|
|
|
*/
|
|
|
|
public void setColor(int color)
|
|
|
|
{
|
|
|
|
this.color = color;
|
|
|
|
}
|
2011-03-14 17:32:28 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the timestamp of the message
|
|
|
|
*
|
|
|
|
* @param timestamp
|
|
|
|
*/
|
|
|
|
public void setTimestamp(long timestamp)
|
|
|
|
{
|
|
|
|
this.timestamp = timestamp;
|
|
|
|
}
|
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
|
|
|
* Associate a color with a sender name
|
|
|
|
*
|
|
|
|
* @return a color hexa
|
|
|
|
*/
|
|
|
|
private int getSenderColor()
|
|
|
|
{
|
|
|
|
/* It might be worth to use some hash table here */
|
2011-01-25 15:02:27 -05:00
|
|
|
if (sender == null) {
|
|
|
|
return COLOR_DEFAULT;
|
|
|
|
}
|
2011-03-11 16:07:39 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
int color = 0;
|
2011-03-11 16:07:39 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
for(int i = 0; i < sender.length(); i++){
|
|
|
|
color += sender.charAt(i);
|
|
|
|
}
|
2011-03-11 16:07:39 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/* we dont want color[colors.length-1] which is black */
|
|
|
|
color = color % (colors.length - 1);
|
2011-03-11 16:07:39 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
return colors[color];
|
|
|
|
}
|
2011-03-11 16:07:39 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
|
|
|
* Render message as spannable string
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public SpannableString render(Context context)
|
|
|
|
{
|
|
|
|
Settings settings = new Settings(context);
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
if (canvas == null) {
|
2011-03-11 16:07:39 -05:00
|
|
|
String prefix = hasIcon() && settings.showIcons() ? " " : "";
|
|
|
|
String nick = hasSender() ? "<" + sender + "> " : "";
|
|
|
|
String timestamp = settings.showTimestamp() ? renderTimeStamp(settings.use24hFormat()) : "";
|
2011-04-14 16:53:38 -04:00
|
|
|
|
2011-03-17 23:08:21 -04:00
|
|
|
canvas = new SpannableString(prefix + timestamp + nick);
|
2011-04-14 16:53:38 -04:00
|
|
|
SpannableString renderedText;
|
|
|
|
|
2011-03-15 22:21:48 -04:00
|
|
|
if (settings.showMircColors()) {
|
2011-03-17 23:08:21 -04:00
|
|
|
renderedText = MircColors.toSpannable(text);
|
2011-04-14 16:53:38 -04:00
|
|
|
} else {
|
|
|
|
renderedText = new SpannableString(
|
|
|
|
MircColors.removeStyleAndColors(text)
|
|
|
|
);
|
2011-03-15 22:21:48 -04:00
|
|
|
}
|
2011-04-14 16:53:38 -04:00
|
|
|
|
2011-03-17 23:08:21 -04:00
|
|
|
if (settings.showGraphicalSmilies()) {
|
|
|
|
renderedText = Smilies.toSpannable(renderedText, context);
|
2011-03-15 22:21:48 -04:00
|
|
|
}
|
2011-04-14 16:53:38 -04:00
|
|
|
|
2011-03-17 23:08:21 -04:00
|
|
|
canvas = new SpannableString(TextUtils.concat(canvas, renderedText));
|
2011-04-14 16:53:38 -04:00
|
|
|
|
2011-03-11 16:07:39 -05:00
|
|
|
if (hasSender()) {
|
2010-11-18 12:52:19 -05:00
|
|
|
int start = (prefix + timestamp).length() + 1;
|
|
|
|
int end = start + sender.length();
|
2011-03-11 16:07:39 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
if (settings.showColorsNick()) {
|
|
|
|
canvas.setSpan(new ForegroundColorSpan(getSenderColor()), start, end , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
|
|
|
}
|
|
|
|
}
|
2010-09-09 14:41:55 -04:00
|
|
|
|
2011-03-11 16:07:39 -05:00
|
|
|
if (hasIcon() && settings.showIcons()) {
|
2010-11-18 12:52:19 -05:00
|
|
|
Drawable drawable = context.getResources().getDrawable(icon);
|
|
|
|
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
|
|
|
|
canvas.setSpan(new ImageSpan(drawable, ImageSpan.ALIGN_BOTTOM), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
|
|
|
}
|
2011-04-14 16:53:38 -04:00
|
|
|
|
2011-03-11 16:07:39 -05:00
|
|
|
if (hasColor() && settings.showColors()) {
|
2011-03-17 20:23:57 -04:00
|
|
|
// Only apply the foreground color to areas that don't already have a foreground color.
|
2011-03-16 13:42:58 -04:00
|
|
|
ForegroundColorSpan[] spans = canvas.getSpans(0, canvas.length(), ForegroundColorSpan.class);
|
|
|
|
int start = 0;
|
2011-04-14 16:53:38 -04:00
|
|
|
|
2011-03-16 13:42:58 -04:00
|
|
|
for (int i = 0; i < spans.length; i++) {
|
|
|
|
canvas.setSpan(new ForegroundColorSpan(color), start, canvas.getSpanStart(spans[i]), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
|
|
|
start = canvas.getSpanEnd(spans[i]);
|
|
|
|
}
|
2011-04-14 16:53:38 -04:00
|
|
|
|
2011-03-16 13:42:58 -04:00
|
|
|
canvas.setSpan(new ForegroundColorSpan(color), start, canvas.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
2010-11-18 12:52:19 -05:00
|
|
|
}
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
return canvas;
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2011-03-11 16:07:39 -05:00
|
|
|
/**
|
|
|
|
* Does this message have a sender?
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
private boolean hasSender()
|
|
|
|
{
|
|
|
|
return sender != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Does this message have a color assigned?
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
private boolean hasColor()
|
|
|
|
{
|
|
|
|
return color != NO_COLOR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Does this message have an icon assigned?
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
private boolean hasIcon()
|
|
|
|
{
|
|
|
|
return icon != NO_ICON;
|
|
|
|
}
|
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
|
|
|
* Render message as text view
|
2011-03-11 16:07:39 -05:00
|
|
|
*
|
2010-11-18 12:52:19 -05:00
|
|
|
* @param context
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public TextView renderTextView(Context context)
|
|
|
|
{
|
|
|
|
// XXX: We should not read settings here ALWAYS for EVERY textview
|
|
|
|
Settings settings = new Settings(context);
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
TextView canvas = new TextView(context);
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
canvas.setText(this.render(context));
|
|
|
|
canvas.setTextSize(settings.getFontSize());
|
|
|
|
canvas.setTypeface(Typeface.MONOSPACE);
|
|
|
|
canvas.setTextColor(COLOR_DEFAULT);
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
return canvas;
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
/**
|
|
|
|
* Generate a timestamp
|
|
|
|
*
|
|
|
|
* @param use24hFormat
|
|
|
|
* @return
|
|
|
|
*/
|
2011-03-14 17:32:28 -04:00
|
|
|
public String renderTimeStamp(boolean use24hFormat)
|
2010-11-18 12:52:19 -05:00
|
|
|
{
|
|
|
|
Date date = new Date(timestamp);
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
int hours = date.getHours();
|
|
|
|
int minutes = date.getMinutes();
|
2010-03-13 19:07:11 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
if (!use24hFormat) {
|
|
|
|
hours = Math.abs(12 - hours);
|
|
|
|
if (hours == 12) {
|
|
|
|
hours = 0;
|
|
|
|
}
|
|
|
|
}
|
2011-01-25 15:02:27 -05:00
|
|
|
|
2010-11-18 12:52:19 -05:00
|
|
|
return "[" + (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + "] ";
|
|
|
|
}
|
2010-03-06 11:16:35 -05:00
|
|
|
}
|