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
|
|
|
|
2010-03-13 10:52:20 -05:00
|
|
|
Copyright 2009-2010 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/>.
|
|
|
|
*/
|
|
|
|
package org.yaaic.model;
|
|
|
|
|
2010-03-13 14:15:09 -05:00
|
|
|
import java.util.Date;
|
|
|
|
|
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;
|
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>
|
|
|
|
*/
|
|
|
|
public class Message {
|
2010-03-06 14:59:22 -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;
|
2010-03-13 15:16:32 -05:00
|
|
|
public static final int COLOR_GREY = 0xFFaaaaaa;
|
2010-03-06 14:52:54 -05:00
|
|
|
|
|
|
|
private int icon = -1;
|
2010-03-06 11:16:35 -05:00
|
|
|
private String text;
|
2010-03-06 12:58:27 -05:00
|
|
|
private SpannableString canvas;
|
2010-03-06 14:52:54 -05:00
|
|
|
private int color = -1;
|
2010-03-18 17:10:19 -04:00
|
|
|
private long timestamp;
|
2010-03-06 11:16:35 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new message without an icon
|
|
|
|
*
|
|
|
|
* @param text
|
|
|
|
*/
|
|
|
|
public Message(String text)
|
|
|
|
{
|
|
|
|
this.text = text;
|
2010-03-18 17:10:19 -04:00
|
|
|
this.timestamp = new Date().getTime();
|
2010-03-06 11:16:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-03-06 12:58:27 -05:00
|
|
|
* Set the message's icon
|
2010-03-06 11:16:35 -05:00
|
|
|
*/
|
2010-03-06 12:58:27 -05:00
|
|
|
public void setIcon(int icon)
|
2010-03-06 11:16:35 -05:00
|
|
|
{
|
|
|
|
this.icon = icon;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the message's icon
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public int getIcon()
|
|
|
|
{
|
|
|
|
return icon;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the text of this message
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public String getText()
|
|
|
|
{
|
|
|
|
return text;
|
|
|
|
}
|
2010-03-06 11:52:22 -05:00
|
|
|
|
2010-03-06 14:52:54 -05:00
|
|
|
/**
|
|
|
|
* Set the color of this message
|
|
|
|
*/
|
|
|
|
public void setColor(int color)
|
|
|
|
{
|
|
|
|
this.color = color;
|
|
|
|
}
|
|
|
|
|
2010-03-06 11:52:22 -05:00
|
|
|
/**
|
|
|
|
* Render message as spannable string
|
2010-03-06 12:58:27 -05:00
|
|
|
*
|
|
|
|
* @return
|
2010-03-06 11:52:22 -05:00
|
|
|
*/
|
2010-03-06 12:58:27 -05:00
|
|
|
public SpannableString render(Context context)
|
2010-03-06 11:52:22 -05:00
|
|
|
{
|
2010-03-13 13:56:32 -05:00
|
|
|
Settings settings = new Settings(context);
|
|
|
|
|
2010-03-06 12:58:27 -05:00
|
|
|
if (canvas == null) {
|
2010-03-31 19:07:30 -04:00
|
|
|
String prefix = icon != -1 && settings.showIcons() ? " " : "";
|
2010-03-18 17:10:19 -04:00
|
|
|
String timestamp = settings.showTimestamp() ? Message.generateTimestamp(this.timestamp, settings.use24hFormat()) : "";
|
2010-03-13 14:15:09 -05:00
|
|
|
|
|
|
|
canvas = new SpannableString(prefix + timestamp + text);
|
2010-03-13 13:56:32 -05:00
|
|
|
|
2010-03-31 19:07:30 -04:00
|
|
|
if (icon != -1 && settings.showIcons()) {
|
2010-03-06 12:58:27 -05:00
|
|
|
Drawable drawable = context.getResources().getDrawable(icon);
|
2010-03-06 13:31:24 -05:00
|
|
|
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
|
2010-03-08 18:33:35 -05:00
|
|
|
canvas.setSpan(new ImageSpan(drawable, ImageSpan.ALIGN_BOTTOM), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
2010-03-06 11:52:22 -05:00
|
|
|
}
|
2010-03-13 14:15:09 -05:00
|
|
|
if (color != -1 && settings.showColors()) {
|
2010-03-06 14:52:54 -05:00
|
|
|
canvas.setSpan(new ForegroundColorSpan(color), 0, canvas.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
|
|
|
}
|
2010-03-06 11:52:22 -05:00
|
|
|
}
|
|
|
|
|
2010-03-06 12:58:27 -05:00
|
|
|
return canvas;
|
2010-03-06 11:52:22 -05:00
|
|
|
}
|
2010-03-08 15:36:36 -05:00
|
|
|
|
2010-03-13 14:15:09 -05:00
|
|
|
/**
|
|
|
|
* Render message as text view
|
|
|
|
*
|
|
|
|
* @param context
|
|
|
|
* @return
|
|
|
|
*/
|
2010-03-08 15:36:36 -05:00
|
|
|
public TextView renderTextView(Context context)
|
|
|
|
{
|
|
|
|
TextView canvas = new TextView(context);
|
|
|
|
|
|
|
|
canvas.setText(this.render(context));
|
|
|
|
canvas.setTextSize(11);
|
|
|
|
canvas.setTypeface(Typeface.MONOSPACE);
|
|
|
|
canvas.setTextColor(0xffeeeeee);
|
|
|
|
|
|
|
|
return canvas;
|
|
|
|
}
|
2010-03-13 14:15:09 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a timestamp
|
|
|
|
*
|
|
|
|
* @param use24hFormat
|
|
|
|
* @return
|
|
|
|
*/
|
2010-03-18 17:10:19 -04:00
|
|
|
public static String generateTimestamp(long timestamp, boolean use24hFormat)
|
2010-03-13 14:15:09 -05:00
|
|
|
{
|
2010-03-18 17:10:19 -04:00
|
|
|
Date date = new Date(timestamp);
|
2010-03-13 14:15:09 -05:00
|
|
|
|
|
|
|
int hours = date.getHours();
|
2010-03-13 19:07:11 -05:00
|
|
|
int minutes = date.getMinutes();
|
|
|
|
|
2010-03-13 14:15:09 -05:00
|
|
|
if (!use24hFormat) {
|
2010-04-05 18:37:16 -04:00
|
|
|
hours = Math.abs(12 - hours);
|
|
|
|
if (hours == 12) {
|
2010-04-05 18:21:39 -04:00
|
|
|
hours = 0;
|
|
|
|
}
|
2010-03-13 14:15:09 -05:00
|
|
|
}
|
|
|
|
|
2010-03-13 19:07:11 -05:00
|
|
|
return "[" + (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + "] ";
|
2010-03-13 14:15:09 -05:00
|
|
|
}
|
2010-03-06 11:16:35 -05:00
|
|
|
}
|