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; public class ConversationGridViewAdapter extends BaseAdapter { private final ArrayList listMessages; private final Activity activity; private final ArrayList dispatcherPositions; private final ArrayList ackPositions; private final Hashtable hash = new Hashtable<>(); public ConversationGridViewAdapter(Activity activity, ArrayList listMessages, ArrayList dispatcherPositions, ArrayList ackPositions) { this.activity = activity; this.listMessages = listMessages; this.dispatcherPositions = dispatcherPositions; this.ackPositions = ackPositions; } @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; } 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; } } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder view; LayoutInflater inflater = activity.getLayoutInflater(); if (convertView == null) { view = new ViewHolder(); 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)); convertView.setTag(view); } else { view = (ViewHolder) convertView.getTag(); } try { hash.put(position, convertView); SimpleDateFormat sdf = new SimpleDateFormat("HH:mm MMM-dd"); Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); if (listMessages.get(position).received.after(calendar.getTime())) sdf = new SimpleDateFormat("HH:mm:ss"); else sdf = new SimpleDateFormat("HH:mm MMM-dd"); 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; case 0: view.getLayoutReceived().setVisibility(View.VISIBLE); view.getLayoutSend().setVisibility(View.GONE); break; default: throw new IllegalStateException("Unexpected value: " + (Boolean.TRUE.equals(dispatcherPositions.get(position)) ? 1 : 0)); } } catch (Exception ex) { SM.Exception(ex.toString()); } return convertView; } public void setACK(String seqID) { int position = -1; int i = 0; for (Msg msg : listMessages) { if (msg.seqID.equals(seqID)) position = i; i++; } if (position > -1 && position < ackPositions.size()) { ackPositions.remove(position); ackPositions.add(position, true); } } public void changeView(String seqID) { int position = -1; int i = 0; for (Msg msg : listMessages) { if (msg.seqID.equals(seqID)) position = i; i++; } if (position != -1 && hash.size() > position) { SM.Debug("POSITON : " + position); View con = hash.get(position); ViewHolder view = null; if (con != null) { view = (ViewHolder) con.getTag(); } switch (Boolean.TRUE.equals(ackPositions.get(position)) ? 1 : 0) { case 1: if (view != null) { view.getImageAck().setVisibility(View.INVISIBLE); view.getTextViewNotACK().setVisibility(View.INVISIBLE); } break; case 0: if (view != null) { view.getImageAck().setVisibility(View.VISIBLE); view.getTextViewNotACK().setVisibility(View.VISIBLE); } break; default: throw new IllegalStateException("Unexpected value: " + (Boolean.TRUE.equals(ackPositions.get(position)) ? 1 : 0)); } } } }