2022-03-14 09:53:00 +00:00
|
|
|
package com.safemobile.adapters;
|
|
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Hashtable;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.BaseAdapter;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import com.safemobile.lib.Msg;
|
|
|
|
import com.safemobile.lib.R;
|
|
|
|
import com.safemobile.lib.SM;
|
|
|
|
|
2022-03-18 06:40:05 +00:00
|
|
|
public class ConversationGridViewAdapter extends BaseAdapter {
|
|
|
|
private final ArrayList<Msg> listMessages;
|
|
|
|
private final Activity activity;
|
|
|
|
private final ArrayList<Boolean> dispatcherPositions;
|
|
|
|
private final ArrayList<Boolean> ackPositions;
|
|
|
|
private final Hashtable<Integer, View> hash = new Hashtable<>();
|
|
|
|
|
|
|
|
|
|
|
|
public ConversationGridViewAdapter(Activity activity, ArrayList<Msg> listMessages, ArrayList<Boolean> dispatcherPositions, ArrayList<Boolean> ackPositions) {
|
2022-03-14 09:53:00 +00:00
|
|
|
this.activity = activity;
|
|
|
|
this.listMessages = listMessages;
|
2022-03-18 06:40:05 +00:00
|
|
|
this.dispatcherPositions = dispatcherPositions;
|
|
|
|
this.ackPositions = ackPositions;
|
2022-03-14 09:53:00 +00:00
|
|
|
}
|
2022-03-18 06:40:05 +00:00
|
|
|
|
2022-03-14 09:53:00 +00:00
|
|
|
@Override
|
|
|
|
public int getCount() {
|
|
|
|
return listMessages.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Msg getItem(int position) {
|
|
|
|
return listMessages.get(position);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getItemId(int position) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-03-18 06:40:05 +00:00
|
|
|
public static class ViewHolder {
|
|
|
|
private ImageView imgViewContact;
|
|
|
|
private TextView txtViewMsg;
|
|
|
|
private TextView txtViewDateTime;
|
|
|
|
private ImageView imgViewReceivedContact;
|
|
|
|
private TextView txtViewReceivedMsg;
|
|
|
|
private TextView txtViewReceivedDateTime;
|
|
|
|
private ImageView imageAck;
|
|
|
|
private TextView textViewNotACK;
|
|
|
|
private LinearLayout layoutSend;
|
|
|
|
private LinearLayout layoutReceived;
|
|
|
|
|
|
|
|
public ImageView getImgViewContact() {
|
|
|
|
return imgViewContact;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setImgViewContact(ImageView imgViewContact) {
|
|
|
|
this.imgViewContact = imgViewContact;
|
|
|
|
}
|
|
|
|
|
|
|
|
public TextView getTxtViewMsg() {
|
|
|
|
return txtViewMsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setTxtViewMsg(TextView txtViewMsg) {
|
|
|
|
this.txtViewMsg = txtViewMsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
public TextView getTxtViewDateTime() {
|
|
|
|
return txtViewDateTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setTxtViewDateTime(TextView txtViewDateTime) {
|
|
|
|
this.txtViewDateTime = txtViewDateTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ImageView getImgViewReceivedContact() {
|
|
|
|
return imgViewReceivedContact;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setImgViewReceivedContact(ImageView imgViewReceivedContact) {
|
|
|
|
this.imgViewReceivedContact = imgViewReceivedContact;
|
|
|
|
}
|
|
|
|
|
|
|
|
public TextView getTxtViewReceivedMsg() {
|
|
|
|
return txtViewReceivedMsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setTxtViewReceivedMsg(TextView txtViewReceivedMsg) {
|
|
|
|
this.txtViewReceivedMsg = txtViewReceivedMsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
public TextView getTxtViewReceivedDateTime() {
|
|
|
|
return txtViewReceivedDateTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setTxtViewReceivedDateTime(TextView txtViewReceivedDateTime) {
|
|
|
|
this.txtViewReceivedDateTime = txtViewReceivedDateTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ImageView getImageAck() {
|
|
|
|
return imageAck;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setImageAck(ImageView imageAck) {
|
|
|
|
this.imageAck = imageAck;
|
|
|
|
}
|
|
|
|
|
|
|
|
public TextView getTextViewNotACK() {
|
|
|
|
return textViewNotACK;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setTextViewNotACK(TextView textViewNotACK) {
|
|
|
|
this.textViewNotACK = textViewNotACK;
|
|
|
|
}
|
|
|
|
|
|
|
|
public LinearLayout getLayoutSend() {
|
|
|
|
return layoutSend;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setLayoutSend(LinearLayout layoutSend) {
|
|
|
|
this.layoutSend = layoutSend;
|
|
|
|
}
|
|
|
|
|
|
|
|
public LinearLayout getLayoutReceived() {
|
|
|
|
return layoutReceived;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setLayoutReceived(LinearLayout layoutReceived) {
|
|
|
|
this.layoutReceived = layoutReceived;
|
|
|
|
}
|
2022-03-14 09:53:00 +00:00
|
|
|
}
|
2022-03-18 06:40:05 +00:00
|
|
|
|
2022-03-14 09:53:00 +00:00
|
|
|
@Override
|
|
|
|
public View getView(int position, View convertView, ViewGroup parent) {
|
|
|
|
ViewHolder view;
|
2022-03-18 06:40:05 +00:00
|
|
|
LayoutInflater inflater = activity.getLayoutInflater();
|
|
|
|
|
|
|
|
if (convertView == null) {
|
2022-03-14 09:53:00 +00:00
|
|
|
view = new ViewHolder();
|
2022-03-18 06:40:05 +00:00
|
|
|
convertView = inflater.inflate(R.layout.row_conversation, null);
|
|
|
|
|
|
|
|
view.setImgViewContact(convertView.findViewById(R.id.imageViewSenderIco));
|
|
|
|
view.setTxtViewMsg(convertView.findViewById(R.id.textViewSendMsg));
|
|
|
|
view.setTxtViewDateTime(convertView.findViewById(R.id.textViewSendDate));
|
|
|
|
view.setImgViewReceivedContact(convertView.findViewById(R.id.imageViewReceivedIco));
|
|
|
|
view.setTxtViewReceivedMsg(convertView.findViewById(R.id.textViewReceivedMsg));
|
|
|
|
view.setTxtViewReceivedDateTime(convertView.findViewById(R.id.textViewReceivedDate));
|
|
|
|
view.setLayoutSend(convertView.findViewById(R.id.layoutSend));
|
|
|
|
view.setLayoutReceived(convertView.findViewById(R.id.layoutReceived));
|
|
|
|
view.setTextViewNotACK(convertView.findViewById(R.id.textViewNotACK));
|
|
|
|
view.setImageAck(convertView.findViewById(R.id.imageAck));
|
|
|
|
|
2022-03-14 09:53:00 +00:00
|
|
|
convertView.setTag(view);
|
2022-03-18 06:40:05 +00:00
|
|
|
} else {
|
2022-03-14 09:53:00 +00:00
|
|
|
view = (ViewHolder) convertView.getTag();
|
|
|
|
}
|
2022-03-18 06:40:05 +00:00
|
|
|
try {
|
2022-03-14 09:53:00 +00:00
|
|
|
hash.put(position, convertView);
|
2022-03-18 06:40:05 +00:00
|
|
|
|
2022-03-14 09:53:00 +00:00
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm MMM-dd");
|
|
|
|
Calendar calendar = Calendar.getInstance();
|
2022-03-18 06:40:05 +00:00
|
|
|
calendar.set(Calendar.HOUR_OF_DAY, 0);
|
2022-03-14 09:53:00 +00:00
|
|
|
calendar.set(Calendar.MINUTE, 0);
|
|
|
|
calendar.set(Calendar.SECOND, 0);
|
|
|
|
calendar.set(Calendar.MILLISECOND, 0);
|
2022-03-18 06:40:05 +00:00
|
|
|
if (listMessages.get(position).received.after(calendar.getTime()))
|
2022-03-14 09:53:00 +00:00
|
|
|
sdf = new SimpleDateFormat("HH:mm:ss");
|
|
|
|
else
|
|
|
|
sdf = new SimpleDateFormat("HH:mm MMM-dd");
|
2022-03-18 06:40:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
view.getImgViewContact().setImageResource(R.drawable.peoplegreen_large);
|
|
|
|
view.getTxtViewMsg().setText(listMessages.get(position).message);
|
|
|
|
view.getTxtViewDateTime().setText(sdf.format(listMessages.get(position).received));
|
|
|
|
view.getImgViewReceivedContact().setImageResource(listMessages.get(position).from.getLargeIcon());
|
|
|
|
view.getTxtViewReceivedMsg().setText(listMessages.get(position).message);
|
|
|
|
view.getTxtViewReceivedDateTime().setText(sdf.format(listMessages.get(position).received));
|
|
|
|
|
|
|
|
if (!ackPositions.isEmpty())
|
|
|
|
switch (Boolean.TRUE.equals(ackPositions.get(position)) ? 1 : 0) {
|
|
|
|
case 0:
|
|
|
|
// show not ack
|
|
|
|
view.getTextViewNotACK().setVisibility(View.VISIBLE);
|
|
|
|
view.getImageAck().setVisibility(View.VISIBLE);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
// show not ack
|
|
|
|
view.getTextViewNotACK().setVisibility(View.INVISIBLE);
|
|
|
|
view.getImageAck().setVisibility(View.INVISIBLE);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new IllegalStateException("Unexpected value: " + (Boolean.TRUE.equals(ackPositions.get(position)) ? 1 : 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (Boolean.TRUE.equals(dispatcherPositions.get(position)) ? 1 : 0) {
|
|
|
|
case 1:
|
|
|
|
view.getLayoutReceived().setVisibility(View.GONE);
|
|
|
|
view.getLayoutSend().setVisibility(View.VISIBLE);
|
|
|
|
break;
|
2022-03-14 09:53:00 +00:00
|
|
|
case 0:
|
2022-03-18 06:40:05 +00:00
|
|
|
view.getLayoutReceived().setVisibility(View.VISIBLE);
|
|
|
|
view.getLayoutSend().setVisibility(View.GONE);
|
2022-03-14 09:53:00 +00:00
|
|
|
break;
|
2022-03-18 06:40:05 +00:00
|
|
|
default:
|
|
|
|
throw new IllegalStateException("Unexpected value: " + (Boolean.TRUE.equals(dispatcherPositions.get(position)) ? 1 : 0));
|
|
|
|
}
|
|
|
|
} catch (Exception ex) {
|
2022-03-14 09:53:00 +00:00
|
|
|
SM.Exception(ex.toString());
|
|
|
|
}
|
|
|
|
return convertView;
|
|
|
|
}
|
2022-03-18 06:40:05 +00:00
|
|
|
|
|
|
|
public void setACK(String seqID) {
|
|
|
|
int position = -1;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
for (Msg msg : listMessages) {
|
|
|
|
if (msg.seqID.equals(seqID))
|
2022-03-14 09:53:00 +00:00
|
|
|
position = i;
|
2022-03-18 06:40:05 +00:00
|
|
|
|
2022-03-14 09:53:00 +00:00
|
|
|
i++;
|
|
|
|
}
|
2022-03-18 06:40:05 +00:00
|
|
|
|
|
|
|
if (position > -1 && position < ackPositions.size()) {
|
2022-03-14 09:53:00 +00:00
|
|
|
ackPositions.remove(position);
|
|
|
|
ackPositions.add(position, true);
|
|
|
|
}
|
|
|
|
}
|
2022-03-18 06:40:05 +00:00
|
|
|
|
|
|
|
public void changeView(String seqID) {
|
|
|
|
int position = -1;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
for (Msg msg : listMessages) {
|
|
|
|
if (msg.seqID.equals(seqID))
|
2022-03-14 09:53:00 +00:00
|
|
|
position = i;
|
2022-03-18 06:40:05 +00:00
|
|
|
|
2022-03-14 09:53:00 +00:00
|
|
|
i++;
|
|
|
|
}
|
2022-03-18 06:40:05 +00:00
|
|
|
|
|
|
|
if (position != -1 && hash.size() > position) {
|
2022-03-14 09:53:00 +00:00
|
|
|
SM.Debug("POSITON : " + position);
|
|
|
|
View con = hash.get(position);
|
2022-03-18 06:40:05 +00:00
|
|
|
ViewHolder view = null;
|
|
|
|
if (con != null) {
|
|
|
|
view = (ViewHolder) con.getTag();
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (Boolean.TRUE.equals(ackPositions.get(position)) ? 1 : 0) {
|
2022-03-14 09:53:00 +00:00
|
|
|
case 1:
|
2022-03-18 06:40:05 +00:00
|
|
|
if (view != null) {
|
|
|
|
view.getImageAck().setVisibility(View.INVISIBLE);
|
|
|
|
view.getTextViewNotACK().setVisibility(View.INVISIBLE);
|
|
|
|
}
|
2022-03-14 09:53:00 +00:00
|
|
|
break;
|
|
|
|
case 0:
|
2022-03-18 06:40:05 +00:00
|
|
|
if (view != null) {
|
|
|
|
view.getImageAck().setVisibility(View.VISIBLE);
|
|
|
|
view.getTextViewNotACK().setVisibility(View.VISIBLE);
|
|
|
|
}
|
2022-03-14 09:53:00 +00:00
|
|
|
break;
|
2022-03-18 06:40:05 +00:00
|
|
|
default:
|
|
|
|
throw new IllegalStateException("Unexpected value: " + (Boolean.TRUE.equals(ackPositions.get(position)) ? 1 : 0));
|
2022-03-14 09:53:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|