2014-08-15 11:18:15 +00:00
|
|
|
package eu.siacs.conversations.ui.adapter;
|
|
|
|
|
2015-01-11 15:22:29 +00:00
|
|
|
import android.content.Context;
|
2015-02-15 17:48:05 +00:00
|
|
|
import android.content.res.Resources;
|
|
|
|
import android.graphics.Bitmap;
|
2015-01-11 15:22:29 +00:00
|
|
|
import android.graphics.Color;
|
|
|
|
import android.graphics.Typeface;
|
2015-02-15 17:48:05 +00:00
|
|
|
import android.graphics.drawable.BitmapDrawable;
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
import android.os.AsyncTask;
|
2015-01-12 15:09:39 +00:00
|
|
|
import android.util.Pair;
|
2015-01-11 15:22:29 +00:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.ArrayAdapter;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
2015-02-15 17:48:05 +00:00
|
|
|
import java.lang.ref.WeakReference;
|
2014-08-15 11:18:15 +00:00
|
|
|
import java.util.List;
|
2015-02-15 17:48:05 +00:00
|
|
|
import java.util.concurrent.RejectedExecutionException;
|
2014-08-15 11:18:15 +00:00
|
|
|
|
|
|
|
import eu.siacs.conversations.R;
|
|
|
|
import eu.siacs.conversations.entities.Conversation;
|
2014-10-15 17:32:12 +00:00
|
|
|
import eu.siacs.conversations.entities.Downloadable;
|
2014-08-15 11:18:15 +00:00
|
|
|
import eu.siacs.conversations.entities.Message;
|
|
|
|
import eu.siacs.conversations.ui.ConversationActivity;
|
2014-09-02 13:51:20 +00:00
|
|
|
import eu.siacs.conversations.ui.XmppActivity;
|
2014-08-15 11:18:15 +00:00
|
|
|
import eu.siacs.conversations.utils.UIHelper;
|
|
|
|
|
|
|
|
public class ConversationAdapter extends ArrayAdapter<Conversation> {
|
|
|
|
|
2014-09-02 13:51:20 +00:00
|
|
|
private XmppActivity activity;
|
2014-08-15 11:18:15 +00:00
|
|
|
|
2014-09-02 13:51:20 +00:00
|
|
|
public ConversationAdapter(XmppActivity activity,
|
2014-08-15 11:18:15 +00:00
|
|
|
List<Conversation> conversations) {
|
|
|
|
super(activity, 0, conversations);
|
|
|
|
this.activity = activity;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View getView(int position, View view, ViewGroup parent) {
|
|
|
|
if (view == null) {
|
2015-01-12 15:09:39 +00:00
|
|
|
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
|
|
|
view = inflater.inflate(R.layout.conversation_list_row,parent, false);
|
2014-08-15 11:18:15 +00:00
|
|
|
}
|
2014-10-15 17:32:12 +00:00
|
|
|
Conversation conversation = getItem(position);
|
2014-09-02 13:51:20 +00:00
|
|
|
if (this.activity instanceof ConversationActivity) {
|
|
|
|
ConversationActivity activity = (ConversationActivity) this.activity;
|
2014-10-03 13:00:29 +00:00
|
|
|
if (!activity.isConversationsOverviewHideable()) {
|
2014-10-15 17:32:12 +00:00
|
|
|
if (conversation == activity.getSelectedConversation()) {
|
2014-10-05 22:33:52 +00:00
|
|
|
view.setBackgroundColor(activity
|
|
|
|
.getSecondaryBackgroundColor());
|
2014-09-02 13:51:20 +00:00
|
|
|
} else {
|
|
|
|
view.setBackgroundColor(Color.TRANSPARENT);
|
|
|
|
}
|
2014-08-15 11:18:15 +00:00
|
|
|
} else {
|
|
|
|
view.setBackgroundColor(Color.TRANSPARENT);
|
|
|
|
}
|
|
|
|
}
|
2015-01-12 15:09:39 +00:00
|
|
|
TextView convName = (TextView) view.findViewById(R.id.conversation_name);
|
|
|
|
if (conversation.getMode() == Conversation.MODE_SINGLE || activity.useSubjectToIdentifyConference()) {
|
2014-10-15 17:32:12 +00:00
|
|
|
convName.setText(conversation.getName());
|
2014-09-22 11:23:35 +00:00
|
|
|
} else {
|
2014-12-21 20:43:58 +00:00
|
|
|
convName.setText(conversation.getJid().toBareJid().toString());
|
2014-09-22 11:23:35 +00:00
|
|
|
}
|
2015-01-12 15:09:39 +00:00
|
|
|
TextView mLastMessage = (TextView) view.findViewById(R.id.conversation_lastmsg);
|
|
|
|
TextView mTimestamp = (TextView) view.findViewById(R.id.conversation_lastupdate);
|
|
|
|
ImageView imagePreview = (ImageView) view.findViewById(R.id.conversation_lastimage);
|
2014-08-15 11:18:15 +00:00
|
|
|
|
2014-10-15 17:32:12 +00:00
|
|
|
Message message = conversation.getLatestMessage();
|
2014-08-15 11:18:15 +00:00
|
|
|
|
2014-10-15 17:32:12 +00:00
|
|
|
if (!conversation.isRead()) {
|
|
|
|
convName.setTypeface(null, Typeface.BOLD);
|
|
|
|
} else {
|
|
|
|
convName.setTypeface(null, Typeface.NORMAL);
|
|
|
|
}
|
|
|
|
|
2015-01-12 15:09:39 +00:00
|
|
|
if (message.getImageParams().width > 0
|
|
|
|
&& (message.getDownloadable() == null
|
2015-01-20 21:54:26 +00:00
|
|
|
|| message.getDownloadable().getStatus() != Downloadable.STATUS_DELETED)) {
|
2015-01-12 15:09:39 +00:00
|
|
|
mLastMessage.setVisibility(View.GONE);
|
|
|
|
imagePreview.setVisibility(View.VISIBLE);
|
|
|
|
activity.loadBitmap(message, imagePreview);
|
|
|
|
} else {
|
|
|
|
Pair<String,Boolean> preview = UIHelper.getMessagePreview(activity,message);
|
|
|
|
mLastMessage.setVisibility(View.VISIBLE);
|
|
|
|
imagePreview.setVisibility(View.GONE);
|
|
|
|
mLastMessage.setText(preview.first);
|
|
|
|
if (preview.second) {
|
|
|
|
if (conversation.isRead()) {
|
|
|
|
mLastMessage.setTypeface(null, Typeface.ITALIC);
|
2014-08-15 11:18:15 +00:00
|
|
|
} else {
|
2015-01-12 15:09:39 +00:00
|
|
|
mLastMessage.setTypeface(null,Typeface.BOLD_ITALIC);
|
2014-08-15 11:18:15 +00:00
|
|
|
}
|
2014-10-15 17:32:12 +00:00
|
|
|
} else {
|
2015-01-12 15:09:39 +00:00
|
|
|
if (conversation.isRead()) {
|
|
|
|
mLastMessage.setTypeface(null,Typeface.NORMAL);
|
|
|
|
} else {
|
|
|
|
mLastMessage.setTypeface(null,Typeface.BOLD);
|
|
|
|
}
|
2014-10-15 17:32:12 +00:00
|
|
|
}
|
2014-08-15 11:18:15 +00:00
|
|
|
}
|
|
|
|
|
2015-01-12 15:09:39 +00:00
|
|
|
mTimestamp.setText(UIHelper.readableTimeDifference(activity,conversation.getLatestMessage().getTimeSent()));
|
|
|
|
ImageView profilePicture = (ImageView) view.findViewById(R.id.conversation_image);
|
2015-02-15 17:48:05 +00:00
|
|
|
loadAvatar(conversation,profilePicture);
|
2014-08-15 11:18:15 +00:00
|
|
|
|
|
|
|
return view;
|
|
|
|
}
|
2015-02-15 17:48:05 +00:00
|
|
|
|
|
|
|
class BitmapWorkerTask extends AsyncTask<Conversation, Void, Bitmap> {
|
|
|
|
private final WeakReference<ImageView> imageViewReference;
|
|
|
|
private Conversation conversation = null;
|
|
|
|
|
|
|
|
public BitmapWorkerTask(ImageView imageView) {
|
|
|
|
imageViewReference = new WeakReference<>(imageView);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Bitmap doInBackground(Conversation... params) {
|
|
|
|
return activity.avatarService().get(params[0], activity.getPixel(56));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Bitmap bitmap) {
|
|
|
|
if (bitmap != null) {
|
|
|
|
final ImageView imageView = imageViewReference.get();
|
|
|
|
if (imageView != null) {
|
|
|
|
imageView.setImageBitmap(bitmap);
|
|
|
|
imageView.setBackgroundColor(0x00000000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void loadAvatar(Conversation conversation, ImageView imageView) {
|
|
|
|
Bitmap bm = activity.avatarService().get(conversation,activity.getPixel(56),true);
|
|
|
|
if (bm != null) {
|
|
|
|
imageView.setImageBitmap(bm);
|
|
|
|
imageView.setBackgroundColor(0x00000000);
|
|
|
|
} else if (cancelPotentialWork(conversation, imageView)) {
|
|
|
|
imageView.setBackgroundColor(0xff333333);
|
|
|
|
final BitmapWorkerTask task = new BitmapWorkerTask(imageView);
|
|
|
|
final AsyncDrawable asyncDrawable = new AsyncDrawable(activity.getResources(), null, task);
|
|
|
|
imageView.setImageDrawable(asyncDrawable);
|
|
|
|
try {
|
|
|
|
task.execute(conversation);
|
|
|
|
} catch (final RejectedExecutionException ignored) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean cancelPotentialWork(Conversation conversation, ImageView imageView) {
|
|
|
|
final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(imageView);
|
|
|
|
|
|
|
|
if (bitmapWorkerTask != null) {
|
|
|
|
final Conversation oldConversation = bitmapWorkerTask.conversation;
|
|
|
|
if (oldConversation == null || conversation != oldConversation) {
|
|
|
|
bitmapWorkerTask.cancel(true);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static BitmapWorkerTask getBitmapWorkerTask(ImageView imageView) {
|
|
|
|
if (imageView != null) {
|
|
|
|
final Drawable drawable = imageView.getDrawable();
|
|
|
|
if (drawable instanceof AsyncDrawable) {
|
|
|
|
final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
|
|
|
|
return asyncDrawable.getBitmapWorkerTask();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
static class AsyncDrawable extends BitmapDrawable {
|
|
|
|
private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference;
|
|
|
|
|
|
|
|
public AsyncDrawable(Resources res, Bitmap bitmap, BitmapWorkerTask bitmapWorkerTask) {
|
|
|
|
super(res, bitmap);
|
|
|
|
bitmapWorkerTaskReference = new WeakReference<>(bitmapWorkerTask);
|
|
|
|
}
|
|
|
|
|
|
|
|
public BitmapWorkerTask getBitmapWorkerTask() {
|
|
|
|
return bitmapWorkerTaskReference.get();
|
|
|
|
}
|
|
|
|
}
|
2015-01-12 15:09:39 +00:00
|
|
|
}
|