2013-05-22 07:47:40 +02:00
|
|
|
package com.thejoshwa.ultrasonic.androidapp.view;
|
|
|
|
|
|
|
|
import android.text.method.LinkMovementMethod;
|
|
|
|
import android.text.util.Linkify;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.ArrayAdapter;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
2013-12-04 07:36:02 +01:00
|
|
|
import com.thejoshwa.ultrasonic.androidapp.R;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.activity.SubsonicTabActivity;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.domain.ChatMessage;
|
|
|
|
import com.thejoshwa.ultrasonic.androidapp.util.Util;
|
|
|
|
|
2013-05-22 07:47:40 +02:00
|
|
|
import java.text.DateFormat;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
2013-12-04 07:36:02 +01:00
|
|
|
public class ChatAdapter extends ArrayAdapter<ChatMessage>
|
|
|
|
{
|
|
|
|
|
2013-05-22 07:47:40 +02:00
|
|
|
private final SubsonicTabActivity activity;
|
|
|
|
private ArrayList<ChatMessage> messages;
|
2013-12-04 07:36:02 +01:00
|
|
|
|
|
|
|
private static final String phoneRegex = "1?\\W*([2-9][0-8][0-9])\\W*([2-9][0-9]{2})\\W*([0-9]{4})"; //you can just place your support phone here
|
|
|
|
private static final Pattern phoneMatcher = Pattern.compile(phoneRegex);
|
|
|
|
|
|
|
|
public ChatAdapter(SubsonicTabActivity activity, ArrayList<ChatMessage> messages)
|
|
|
|
{
|
|
|
|
super(activity, R.layout.chat_item, messages);
|
|
|
|
this.activity = activity;
|
|
|
|
this.messages = messages;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getCount()
|
|
|
|
{
|
2013-05-22 07:47:40 +02:00
|
|
|
return messages.size();
|
|
|
|
}
|
2013-12-04 07:36:02 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public View getView(int position, View convertView, ViewGroup parent)
|
|
|
|
{
|
2013-05-22 07:47:40 +02:00
|
|
|
ChatMessage message = this.getItem(position);
|
|
|
|
|
|
|
|
ViewHolder holder;
|
2013-12-04 07:36:02 +01:00
|
|
|
int layout;
|
|
|
|
|
|
|
|
String messageUser = message.getUsername();
|
|
|
|
Date messageTime = new java.util.Date(message.getTime());
|
|
|
|
String messageText = message.getMessage();
|
|
|
|
|
|
|
|
String me = Util.getUserName(activity, Util.getActiveServer(activity));
|
|
|
|
|
|
|
|
layout = messageUser.equals(me) ? R.layout.chat_item_reverse : R.layout.chat_item;
|
|
|
|
|
2013-05-22 07:47:40 +02:00
|
|
|
if (convertView == null)
|
|
|
|
{
|
2014-01-23 09:50:49 +01:00
|
|
|
convertView = inflateView(layout, parent);
|
|
|
|
holder = createViewHolder(layout, convertView);
|
2013-05-22 07:47:40 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
holder = (ViewHolder) convertView.getTag();
|
2014-01-23 09:50:49 +01:00
|
|
|
|
|
|
|
if (holder.layout != layout)
|
|
|
|
{
|
|
|
|
convertView = inflateView(layout, parent);
|
|
|
|
holder = createViewHolder(layout, convertView);
|
|
|
|
}
|
2013-05-22 07:47:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DateFormat timeFormat = android.text.format.DateFormat.getTimeFormat(activity);
|
|
|
|
String messageTimeFormatted = String.format("[%s]", timeFormat.format(messageTime));
|
2013-12-04 07:36:02 +01:00
|
|
|
|
|
|
|
holder.username.setText(messageUser);
|
|
|
|
holder.message.setText(messageText);
|
|
|
|
holder.time.setText(messageTimeFormatted);
|
2013-05-22 07:47:40 +02:00
|
|
|
|
|
|
|
return convertView;
|
|
|
|
}
|
2013-12-04 07:36:02 +01:00
|
|
|
|
2014-01-23 09:50:49 +01:00
|
|
|
private View inflateView(int layout, ViewGroup parent)
|
|
|
|
{
|
|
|
|
return LayoutInflater.from(activity).inflate(layout, parent, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static ViewHolder createViewHolder(int layout, View convertView)
|
|
|
|
{
|
|
|
|
ViewHolder holder = new ViewHolder();
|
|
|
|
holder.layout = layout;
|
|
|
|
|
|
|
|
TextView usernameView;
|
|
|
|
TextView timeView;
|
|
|
|
TextView messageView;
|
|
|
|
|
|
|
|
if (convertView != null)
|
|
|
|
{
|
|
|
|
usernameView = (TextView) convertView.findViewById(R.id.chat_username);
|
|
|
|
timeView = (TextView) convertView.findViewById(R.id.chat_time);
|
|
|
|
messageView = (TextView) convertView.findViewById(R.id.chat_message);
|
|
|
|
|
|
|
|
messageView.setMovementMethod(LinkMovementMethod.getInstance());
|
|
|
|
Linkify.addLinks(messageView, Linkify.EMAIL_ADDRESSES);
|
|
|
|
Linkify.addLinks(messageView, Linkify.WEB_URLS);
|
|
|
|
Linkify.addLinks(messageView, phoneMatcher, "tel:");
|
|
|
|
|
|
|
|
holder.message = messageView;
|
|
|
|
holder.username = usernameView;
|
|
|
|
holder.time = timeView;
|
|
|
|
|
|
|
|
convertView.setTag(holder);
|
|
|
|
}
|
|
|
|
|
|
|
|
return holder;
|
|
|
|
}
|
|
|
|
|
2013-05-22 07:47:40 +02:00
|
|
|
private static class ViewHolder
|
|
|
|
{
|
2013-05-29 10:21:09 +02:00
|
|
|
int layout;
|
2013-05-22 07:47:40 +02:00
|
|
|
TextView message;
|
|
|
|
TextView username;
|
|
|
|
TextView time;
|
|
|
|
}
|
|
|
|
}
|