diff options
Diffstat (limited to '')
-rw-r--r-- | src/de/gultsch/chat/ui/ConversationFragment.java | 205 |
1 files changed, 137 insertions, 68 deletions
diff --git a/src/de/gultsch/chat/ui/ConversationFragment.java b/src/de/gultsch/chat/ui/ConversationFragment.java index 9620c16a..8be44f49 100644 --- a/src/de/gultsch/chat/ui/ConversationFragment.java +++ b/src/de/gultsch/chat/ui/ConversationFragment.java @@ -1,6 +1,9 @@ package de.gultsch.chat.ui; +import java.io.FileNotFoundException; import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; import java.util.Hashtable; import java.util.List; import java.util.Set; @@ -19,13 +22,17 @@ import android.app.AlertDialog; import android.app.Fragment; import android.content.DialogInterface; import android.content.SharedPreferences; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; import android.graphics.Typeface; import android.net.Uri; import android.os.Bundle; import android.preference.PreferenceManager; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; +import android.view.View.OnLayoutChangeListener; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.EditText; @@ -43,6 +50,7 @@ public class ConversationFragment extends Fragment { protected List<Message> messageList = new ArrayList<Message>(); protected ArrayAdapter<Message> messageListAdapter; protected Contact contact; + protected BitmapCache mBitmapCache = new BitmapCache(); private EditText chatMsg; @@ -104,13 +112,24 @@ public class ConversationFragment extends Fragment { boolean showPhoneSelfContactPicture = sharedPref.getBoolean( "show_phone_selfcontact_picture", true); - final Uri selfiUri; + Bitmap self; + if (showPhoneSelfContactPicture) { - selfiUri = PhoneHelper.getSefliUri(getActivity()); + Uri selfiUri = PhoneHelper.getSefliUri(getActivity()); + try { + self = BitmapFactory.decodeStream(getActivity() + .getContentResolver().openInputStream(selfiUri)); + } catch (FileNotFoundException e) { + self = UIHelper.getUnknownContactPicture(conversation + .getAccount().getJid(), 200); + } } else { - selfiUri = null; + self = UIHelper.getUnknownContactPicture(conversation.getAccount() + .getJid(), 200); } + final Bitmap selfBitmap = self; + messageListAdapter = new ArrayAdapter<Message>(this.getActivity() .getApplicationContext(), R.layout.message_sent, this.messageList) { @@ -136,68 +155,73 @@ public class ConversationFragment extends Fragment { public View getView(int position, View view, ViewGroup parent) { Message item = getItem(position); int type = getItemViewType(position); + ViewHolder viewHolder; if (view == null) { switch (type) { case SENT: + viewHolder = new ViewHolder(); view = (View) inflater.inflate(R.layout.message_sent, null); + viewHolder.imageView = (ImageView) view + .findViewById(R.id.message_photo); + viewHolder.messageBody = (TextView) view + .findViewById(R.id.message_body); + viewHolder.time = (TextView) view + .findViewById(R.id.message_time); + view.setTag(viewHolder); break; case RECIEVED: + viewHolder = new ViewHolder(); view = (View) inflater.inflate( R.layout.message_recieved, null); + viewHolder.imageView = (ImageView) view + .findViewById(R.id.message_photo); + viewHolder.messageBody = (TextView) view + .findViewById(R.id.message_body); + viewHolder.time = (TextView) view + .findViewById(R.id.message_time); + view.setTag(viewHolder); + break; + default: + viewHolder = null; break; } + } else { + viewHolder = (ViewHolder) view.getTag(); } - ImageView imageView = (ImageView) view - .findViewById(R.id.message_photo); if (type == RECIEVED) { if (item.getConversation().getMode() == Conversation.MODE_SINGLE) { Uri uri = item.getConversation().getProfilePhotoUri(); if (uri != null) { - imageView.setImageURI(uri); + viewHolder.imageView.setImageBitmap(mBitmapCache.get(item.getConversation().getName(), uri)); } else { - imageView.setImageBitmap(UIHelper - .getUnknownContactPicture(item - .getConversation().getName(), 200)); + viewHolder.imageView.setImageBitmap(mBitmapCache.get(item.getConversation().getName(),null)); } } else if (item.getConversation().getMode() == Conversation.MODE_MULTI) { if (item.getCounterpart() != null) { - imageView.setImageBitmap(UIHelper - .getUnknownContactPicture( - item.getCounterpart(), 200)); + viewHolder.imageView.setImageBitmap(mBitmapCache.get(item.getCounterpart(),null)); } else { - imageView.setImageBitmap(UIHelper - .getUnknownContactPicture(item - .getConversation().getName(), 200)); + viewHolder.imageView.setImageBitmap(mBitmapCache.get(item.getConversation().getName(),null)); } } } else { - if (selfiUri != null) { - imageView.setImageURI(selfiUri); - } else { - imageView.setImageBitmap(UIHelper - .getUnknownContactPicture(conversation - .getAccount().getJid(), 200)); - } + viewHolder.imageView.setImageBitmap(selfBitmap); } - TextView messageBody = (TextView) view - .findViewById(R.id.message_body); String body = item.getBody(); if (body != null) { - messageBody.setText(body.trim()); + viewHolder.messageBody.setText(body.trim()); } - TextView time = (TextView) view.findViewById(R.id.message_time); if (item.getStatus() == Message.STATUS_UNSEND) { - time.setTypeface(null, Typeface.ITALIC); - time.setText("sending\u2026"); + viewHolder.time.setTypeface(null, Typeface.ITALIC); + viewHolder.time.setText("sending\u2026"); } else { - time.setTypeface(null, Typeface.NORMAL); + viewHolder.time.setTypeface(null, Typeface.NORMAL); if ((item.getConversation().getMode() == Conversation.MODE_SINGLE) || (type != RECIEVED)) { - time.setText(UIHelper.readableTimeDifference(item - .getTimeSent())); + viewHolder.time.setText(UIHelper + .readableTimeDifference(item.getTimeSent())); } else { - time.setText(item.getCounterpart() + viewHolder.time.setText(item.getCounterpart() + " \u00B7 " + UIHelper.readableTimeDifference(item .getTimeSent())); @@ -275,24 +299,31 @@ public class ConversationFragment extends Fragment { protected void makeFingerprintWarning(int latestEncryption) { final LinearLayout fingerprintWarning = (LinearLayout) getView() .findViewById(R.id.new_fingerprint); - Set<String> knownFingerprints = conversation.getContact() - .getOtrFingerprints(); - if ((latestEncryption == Message.ENCRYPTION_OTR) - && (conversation.hasValidOtrSession() - && (conversation.getOtrSession().getSessionStatus() == SessionStatus.ENCRYPTED) && (!knownFingerprints - .contains(conversation.getOtrFingerprint())))) { - fingerprintWarning.setVisibility(View.VISIBLE); - TextView fingerprint = (TextView) getView().findViewById( - R.id.otr_fingerprint); - fingerprint.setText(conversation.getOtrFingerprint()); - fingerprintWarning.setOnClickListener(new OnClickListener() { - - @Override - public void onClick(View v) { - AlertDialog dialog = UIHelper.getVerifyFingerprintDialog((ConversationActivity) getActivity(),conversation,fingerprintWarning); - dialog.show(); - } - }); + if (conversation.getContact() != null) { + Set<String> knownFingerprints = conversation.getContact() + .getOtrFingerprints(); + if ((latestEncryption == Message.ENCRYPTION_OTR) + && (conversation.hasValidOtrSession() + && (conversation.getOtrSession().getSessionStatus() == SessionStatus.ENCRYPTED) && (!knownFingerprints + .contains(conversation.getOtrFingerprint())))) { + fingerprintWarning.setVisibility(View.VISIBLE); + TextView fingerprint = (TextView) getView().findViewById( + R.id.otr_fingerprint); + fingerprint.setText(conversation.getOtrFingerprint()); + fingerprintWarning.setOnClickListener(new OnClickListener() { + + @Override + public void onClick(View v) { + AlertDialog dialog = UIHelper + .getVerifyFingerprintDialog( + (ConversationActivity) getActivity(), + conversation, fingerprintWarning); + dialog.show(); + } + }); + } else { + fingerprintWarning.setVisibility(View.GONE); + } } else { fingerprintWarning.setVisibility(View.GONE); } @@ -300,11 +331,11 @@ public class ConversationFragment extends Fragment { protected void sendPlainTextMessage(Message message) { ConversationActivity activity = (ConversationActivity) getActivity(); - activity.xmppConnectionService.sendMessage(conversation.getAccount(), message, - null); + activity.xmppConnectionService.sendMessage(conversation.getAccount(), + message, null); chatMsg.setText(""); } - + protected void sendOtrMessage(final Message message) { ConversationActivity activity = (ConversationActivity) getActivity(); final XmppConnectionService xmppService = activity.xmppConnectionService; @@ -313,9 +344,13 @@ public class ConversationFragment extends Fragment { conversation.getAccount(), message, null); chatMsg.setText(""); } else { - Hashtable<String, Integer> presences = conversation - .getContact().getPresences(); - if (presences.size() == 0) { + Hashtable<String, Integer> presences; + if (conversation.getContact() != null) { + presences = conversation.getContact().getPresences(); + } else { + presences = null; + } + if ((presences != null) && (presences.size() == 0)) { AlertDialog.Builder builder = new AlertDialog.Builder( getActivity()); builder.setTitle("Contact is offline"); @@ -330,16 +365,15 @@ public class ConversationFragment extends Fragment { conversation.nextMessageEncryption = Message.ENCRYPTION_NONE; message.setEncryption(Message.ENCRYPTION_NONE); xmppService.sendMessage( - conversation.getAccount(), - message, null); + conversation.getAccount(), message, + null); chatMsg.setText(""); } }); builder.setNegativeButton("Cancel", null); builder.create().show(); } else if (presences.size() == 1) { - xmppService.sendMessage(conversation.getAccount(), - message, + xmppService.sendMessage(conversation.getAccount(), message, (String) presences.keySet().toArray()[0]); chatMsg.setText(""); } else { @@ -348,16 +382,51 @@ public class ConversationFragment extends Fragment { builder.setTitle("Choose Presence"); final String[] presencesArray = new String[presences.size()]; presences.keySet().toArray(presencesArray); - builder.setItems(presencesArray, new DialogInterface.OnClickListener() { - - @Override - public void onClick(DialogInterface dialog, int which) { - xmppService.sendMessage(conversation.getAccount(), message, presencesArray[which]); - chatMsg.setText(""); - } - }); + builder.setItems(presencesArray, + new DialogInterface.OnClickListener() { + + @Override + public void onClick(DialogInterface dialog, + int which) { + xmppService.sendMessage( + conversation.getAccount(), message, + presencesArray[which]); + chatMsg.setText(""); + } + }); builder.create().show(); } } } + + private static class ViewHolder { + + protected TextView time; + protected TextView messageBody; + protected ImageView imageView; + + } + + private class BitmapCache { + private HashMap<String, Bitmap> bitmaps = new HashMap<String, Bitmap>(); + public Bitmap get(String name, Uri uri) { + if (bitmaps.containsKey(name)) { + return bitmaps.get(name); + } else { + Bitmap bm; + if (uri!=null) { + try { + bm = BitmapFactory.decodeStream(getActivity() + .getContentResolver().openInputStream(uri)); + } catch (FileNotFoundException e) { + bm = UIHelper.getUnknownContactPicture(name, 200); + } + } else { + bm = UIHelper.getUnknownContactPicture(name, 200); + } + bitmaps.put(name, bm); + return bm; + } + } + } } |