1package eu.siacs.conversations.ui;
2
3import java.util.ArrayList;
4import java.util.HashMap;
5import java.util.List;
6import java.util.Set;
7
8import net.java.otr4j.session.SessionStatus;
9import eu.siacs.conversations.R;
10import eu.siacs.conversations.crypto.PgpEngine;
11import eu.siacs.conversations.entities.Contact;
12import eu.siacs.conversations.entities.Conversation;
13import eu.siacs.conversations.entities.Message;
14import eu.siacs.conversations.entities.MucOptions;
15import eu.siacs.conversations.entities.MucOptions.OnRenameListener;
16import eu.siacs.conversations.services.ImageProvider;
17import eu.siacs.conversations.services.XmppConnectionService;
18import eu.siacs.conversations.utils.UIHelper;
19import eu.siacs.conversations.xmpp.jingle.JingleConnection;
20import android.app.AlertDialog;
21import android.app.Fragment;
22import android.app.PendingIntent;
23import android.content.Context;
24import android.content.DialogInterface;
25import android.content.Intent;
26import android.content.IntentSender;
27import android.content.SharedPreferences;
28import android.content.IntentSender.SendIntentException;
29import android.graphics.Bitmap;
30import android.graphics.Typeface;
31import android.os.Bundle;
32import android.preference.PreferenceManager;
33import android.text.Editable;
34import android.text.Selection;
35import android.util.DisplayMetrics;
36import android.util.Log;
37import android.view.LayoutInflater;
38import android.view.View;
39import android.view.View.OnClickListener;
40import android.view.ViewGroup;
41import android.widget.ArrayAdapter;
42import android.widget.Button;
43import android.widget.EditText;
44import android.widget.LinearLayout;
45import android.widget.ListView;
46import android.widget.ImageButton;
47import android.widget.ImageView;
48import android.widget.TextView;
49import android.widget.Toast;
50
51public class ConversationFragment extends Fragment {
52
53 protected Conversation conversation;
54 protected ListView messagesView;
55 protected LayoutInflater inflater;
56 protected List<Message> messageList = new ArrayList<Message>();
57 protected ArrayAdapter<Message> messageListAdapter;
58 protected Contact contact;
59 protected BitmapCache mBitmapCache = new BitmapCache();
60
61 protected String queuedPqpMessage = null;
62
63 private EditText chatMsg;
64 private String pastedText = null;
65
66 protected Bitmap selfBitmap;
67
68 private boolean useSubject = true;
69
70 private IntentSender askForPassphraseIntent = null;
71
72 private OnClickListener sendMsgListener = new OnClickListener() {
73
74 @Override
75 public void onClick(View v) {
76 if (chatMsg.getText().length() < 1)
77 return;
78 Message message = new Message(conversation, chatMsg.getText()
79 .toString(), conversation.getNextEncryption());
80 if (conversation.getNextEncryption() == Message.ENCRYPTION_OTR) {
81 sendOtrMessage(message);
82 } else if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
83 sendPgpMessage(message);
84 } else {
85 sendPlainTextMessage(message);
86 }
87 }
88 };
89 protected OnClickListener clickToDecryptListener = new OnClickListener() {
90
91 @Override
92 public void onClick(View v) {
93 if (askForPassphraseIntent != null) {
94 try {
95 getActivity().startIntentSenderForResult(
96 askForPassphraseIntent,
97 ConversationActivity.REQUEST_DECRYPT_PGP, null, 0,
98 0, 0);
99 } catch (SendIntentException e) {
100 Log.d("xmppService", "couldnt fire intent");
101 }
102 }
103 }
104 };
105
106 private LinearLayout pgpInfo;
107 private LinearLayout mucError;
108 private TextView mucErrorText;
109 private OnClickListener clickToMuc = new OnClickListener() {
110
111 @Override
112 public void onClick(View v) {
113 Intent intent = new Intent(getActivity(), MucDetailsActivity.class);
114 intent.setAction(MucDetailsActivity.ACTION_VIEW_MUC);
115 intent.putExtra("uuid", conversation.getUuid());
116 startActivity(intent);
117 }
118 };
119 private ConversationActivity activity;
120
121 public void hidePgpPassphraseBox() {
122 pgpInfo.setVisibility(View.GONE);
123 }
124
125 public void updateChatMsgHint() {
126 if (conversation.getMode() == Conversation.MODE_MULTI) {
127 chatMsg.setHint(getString(R.string.send_message_to_conference));
128 } else {
129 switch (conversation.getNextEncryption()) {
130 case Message.ENCRYPTION_NONE:
131 chatMsg.setHint(getString(R.string.send_plain_text_message));
132 break;
133 case Message.ENCRYPTION_OTR:
134 chatMsg.setHint(getString(R.string.send_otr_message));
135 break;
136 case Message.ENCRYPTION_PGP:
137 chatMsg.setHint(getString(R.string.send_pgp_message));
138 break;
139 default:
140 break;
141 }
142 }
143 }
144
145 @Override
146 public View onCreateView(final LayoutInflater inflater,
147 ViewGroup container, Bundle savedInstanceState) {
148
149 final DisplayMetrics metrics = getResources().getDisplayMetrics();
150
151 this.inflater = inflater;
152
153 final View view = inflater.inflate(R.layout.fragment_conversation,
154 container, false);
155 chatMsg = (EditText) view.findViewById(R.id.textinput);
156
157 ImageButton sendButton = (ImageButton) view
158 .findViewById(R.id.textSendButton);
159 sendButton.setOnClickListener(this.sendMsgListener);
160
161 pgpInfo = (LinearLayout) view.findViewById(R.id.pgp_keyentry);
162 pgpInfo.setOnClickListener(clickToDecryptListener);
163 mucError = (LinearLayout) view.findViewById(R.id.muc_error);
164 mucError.setOnClickListener(clickToMuc);
165 mucErrorText = (TextView) view.findViewById(R.id.muc_error_msg);
166
167 messagesView = (ListView) view.findViewById(R.id.messages_view);
168
169 messageListAdapter = new ArrayAdapter<Message>(this.getActivity()
170 .getApplicationContext(), R.layout.message_sent,
171 this.messageList) {
172
173 private static final int SENT = 0;
174 private static final int RECIEVED = 1;
175
176 @Override
177 public int getViewTypeCount() {
178 return 2;
179 }
180
181 @Override
182 public int getItemViewType(int position) {
183 if (getItem(position).getStatus() <= Message.STATUS_RECIEVED) {
184 return RECIEVED;
185 } else {
186 return SENT;
187 }
188 }
189
190 private void displayStatus(ViewHolder viewHolder, Message message) {
191 String filesize = null;
192 String info = null;
193 boolean error = false;
194 if (message.getType() == Message.TYPE_IMAGE) {
195 String[] fileParams = message.getBody().split(",");
196 try {
197 long size = Long.parseLong(fileParams[0]);
198 filesize = size / 1024 + " KB";
199 } catch (NumberFormatException e) {
200 filesize = "0 KB";
201 }
202 }
203 switch (message.getStatus()) {
204 case Message.STATUS_UNSEND:
205 info = getString(R.string.sending);
206 break;
207 case Message.STATUS_OFFERED:
208 info = getString(R.string.offering);
209 break;
210 case Message.STATUS_SEND_FAILED:
211 info = getString(R.string.send_failed);
212 error = true;
213 break;
214 case Message.STATUS_SEND_REJECTED:
215 info = getString(R.string.send_rejected);
216 error = true;
217 break;
218 default:
219 if ((message.getConversation().getMode() == Conversation.MODE_MULTI)
220 && (message.getStatus() <= Message.STATUS_RECIEVED)) {
221 info = message.getCounterpart();
222 }
223 break;
224 }
225 if (error) {
226 viewHolder.time.setTextColor(0xFFe92727);
227 } else {
228 viewHolder.time.setTextColor(0xFF8e8e8e);
229 }
230 if (message.getEncryption() == Message.ENCRYPTION_NONE) {
231 viewHolder.indicator.setVisibility(View.GONE);
232 } else {
233 viewHolder.indicator.setVisibility(View.VISIBLE);
234 }
235
236 String formatedTime = UIHelper.readableTimeDifference(getContext(), message
237 .getTimeSent());
238 if (message.getStatus() <= Message.STATUS_RECIEVED) {
239 if ((filesize != null) && (info != null)) {
240 viewHolder.time.setText(filesize + " \u00B7 " + info);
241 } else if ((filesize == null) && (info != null)) {
242 viewHolder.time.setText(formatedTime + " \u00B7 "
243 + info);
244 } else if ((filesize != null) && (info == null)) {
245 viewHolder.time.setText(formatedTime + " \u00B7 "
246 + filesize);
247 } else {
248 viewHolder.time.setText(formatedTime);
249 }
250 } else {
251 if ((filesize != null) && (info != null)) {
252 viewHolder.time.setText(filesize + " \u00B7 " + info);
253 } else if ((filesize == null) && (info != null)) {
254 viewHolder.time.setText(info + " \u00B7 "
255 + formatedTime);
256 } else if ((filesize != null) && (info == null)) {
257 viewHolder.time.setText(filesize + " \u00B7 "
258 + formatedTime);
259 } else {
260 viewHolder.time.setText(formatedTime);
261 }
262 }
263 }
264
265 private void displayInfoMessage(ViewHolder viewHolder, int r) {
266 if (viewHolder.download_button != null) {
267 viewHolder.download_button.setVisibility(View.GONE);
268 }
269 viewHolder.image.setVisibility(View.GONE);
270 viewHolder.messageBody.setVisibility(View.VISIBLE);
271 viewHolder.messageBody.setText(getString(r));
272 viewHolder.messageBody.setTextColor(0xff33B5E5);
273 viewHolder.messageBody.setTypeface(null, Typeface.ITALIC);
274 }
275
276 private void displayDecryptionFailed(ViewHolder viewHolder) {
277 viewHolder.download_button.setVisibility(View.GONE);
278 viewHolder.image.setVisibility(View.GONE);
279 viewHolder.messageBody.setVisibility(View.VISIBLE);
280 viewHolder.messageBody
281 .setText(getString(R.string.decryption_failed));
282 viewHolder.messageBody.setTextColor(0xFFe92727);
283 viewHolder.messageBody.setTypeface(null, Typeface.NORMAL);
284 }
285
286 private void displayTextMessage(ViewHolder viewHolder, String text) {
287 if (viewHolder.download_button != null) {
288 viewHolder.download_button.setVisibility(View.GONE);
289 }
290 viewHolder.image.setVisibility(View.GONE);
291 viewHolder.messageBody.setVisibility(View.VISIBLE);
292 if (text != null) {
293 viewHolder.messageBody.setText(text.trim());
294 } else {
295 viewHolder.messageBody.setText("");
296 }
297 viewHolder.messageBody.setTextColor(0xff333333);
298 viewHolder.messageBody.setTypeface(null, Typeface.NORMAL);
299 }
300
301 private void displayImageMessage(ViewHolder viewHolder,
302 final Message message) {
303 if (viewHolder.download_button != null) {
304 viewHolder.download_button.setVisibility(View.GONE);
305 }
306 viewHolder.messageBody.setVisibility(View.GONE);
307 viewHolder.image.setVisibility(View.VISIBLE);
308 String[] fileParams = message.getBody().split(",");
309 if (fileParams.length == 3) {
310 double target = metrics.density * 288;
311 int w = Integer.parseInt(fileParams[1]);
312 int h = Integer.parseInt(fileParams[2]);
313 int scalledW;
314 int scalledH;
315 if (w <= h) {
316 scalledW = (int) (w / ((double) h / target));
317 scalledH = (int) target;
318 } else {
319 scalledW = (int) target;
320 scalledH = (int) (h / ((double) w / target));
321 }
322 viewHolder.image
323 .setLayoutParams(new LinearLayout.LayoutParams(
324 scalledW, scalledH));
325 }
326 activity.loadBitmap(message, viewHolder.image);
327 viewHolder.image.setOnClickListener(new OnClickListener() {
328
329 @Override
330 public void onClick(View v) {
331 Intent intent = new Intent(Intent.ACTION_VIEW);
332 intent.setDataAndType(
333 ImageProvider.getContentUri(message), "image/*");
334 startActivity(intent);
335 }
336 });
337 }
338
339 @Override
340 public View getView(int position, View view, ViewGroup parent) {
341 final Message item = getItem(position);
342 int type = getItemViewType(position);
343 ViewHolder viewHolder;
344 if (view == null) {
345 viewHolder = new ViewHolder();
346 switch (type) {
347 case SENT:
348 view = (View) inflater.inflate(R.layout.message_sent,
349 null);
350 viewHolder.contact_picture = (ImageView) view
351 .findViewById(R.id.message_photo);
352 viewHolder.contact_picture.setImageBitmap(selfBitmap);
353 break;
354 case RECIEVED:
355 view = (View) inflater.inflate(
356 R.layout.message_recieved, null);
357 viewHolder.contact_picture = (ImageView) view
358 .findViewById(R.id.message_photo);
359
360 viewHolder.download_button = (Button) view
361 .findViewById(R.id.download_button);
362
363 if (item.getConversation().getMode() == Conversation.MODE_SINGLE) {
364
365 viewHolder.contact_picture
366 .setImageBitmap(mBitmapCache.get(
367 item.getConversation().getName(
368 useSubject), item
369 .getConversation()
370 .getContact(),
371 getActivity()
372 .getApplicationContext()));
373
374 }
375 break;
376 default:
377 viewHolder = null;
378 break;
379 }
380 viewHolder.indicator = (ImageView) view
381 .findViewById(R.id.security_indicator);
382 viewHolder.image = (ImageView) view
383 .findViewById(R.id.message_image);
384 viewHolder.messageBody = (TextView) view
385 .findViewById(R.id.message_body);
386 viewHolder.time = (TextView) view
387 .findViewById(R.id.message_time);
388 view.setTag(viewHolder);
389 } else {
390 viewHolder = (ViewHolder) view.getTag();
391 }
392
393 if (type == RECIEVED) {
394 if (item.getConversation().getMode() == Conversation.MODE_MULTI) {
395 viewHolder.contact_picture.setImageBitmap(mBitmapCache
396 .get(item.getCounterpart(), null, getActivity()
397 .getApplicationContext()));
398 viewHolder.contact_picture
399 .setOnClickListener(new OnClickListener() {
400
401 @Override
402 public void onClick(View v) {
403 highlightInConference(item.getCounterpart());
404 }
405 });
406 }
407 }
408
409 if (item.getType() == Message.TYPE_IMAGE) {
410 if (item.getStatus() == Message.STATUS_RECIEVING) {
411 displayInfoMessage(viewHolder, R.string.receiving_image);
412 } else if (item.getStatus() == Message.STATUS_RECEIVED_OFFER) {
413 viewHolder.image.setVisibility(View.GONE);
414 viewHolder.messageBody.setVisibility(View.GONE);
415 viewHolder.download_button.setVisibility(View.VISIBLE);
416 viewHolder.download_button
417 .setOnClickListener(new OnClickListener() {
418
419 @Override
420 public void onClick(View v) {
421 JingleConnection connection = item
422 .getJingleConnection();
423 if (connection != null) {
424 connection.accept();
425 } else {
426 Log.d("xmppService",
427 "attached jingle connection was null");
428 }
429 }
430 });
431 } else if ((item.getEncryption() == Message.ENCRYPTION_DECRYPTED)
432 || (item.getEncryption() == Message.ENCRYPTION_NONE)) {
433 displayImageMessage(viewHolder, item);
434 } else if (item.getEncryption() == Message.ENCRYPTION_PGP) {
435 displayInfoMessage(viewHolder,
436 R.string.encrypted_message);
437 } else {
438 displayDecryptionFailed(viewHolder);
439 }
440 } else {
441 if (item.getEncryption() == Message.ENCRYPTION_PGP) {
442 displayInfoMessage(viewHolder,
443 R.string.encrypted_message);
444 } else if (item.getEncryption() == Message.ENCRYPTION_DECRYPTION_FAILED) {
445 displayDecryptionFailed(viewHolder);
446 } else {
447 displayTextMessage(viewHolder, item.getBody());
448 }
449 }
450
451 displayStatus(viewHolder, item);
452
453 return view;
454 }
455 };
456 messagesView.setAdapter(messageListAdapter);
457
458 return view;
459 }
460
461 protected void highlightInConference(String nick) {
462 String oldString = chatMsg.getText().toString().trim();
463 if (oldString.isEmpty()) {
464 chatMsg.setText(nick+": ");
465 } else {
466 chatMsg.setText(oldString+" "+nick+" ");
467 }
468 int position = chatMsg.length();
469 Editable etext = chatMsg.getText();
470 Selection.setSelection(etext, position);
471 }
472
473 protected Bitmap findSelfPicture() {
474 SharedPreferences sharedPref = PreferenceManager
475 .getDefaultSharedPreferences(getActivity()
476 .getApplicationContext());
477 boolean showPhoneSelfContactPicture = sharedPref.getBoolean(
478 "show_phone_selfcontact_picture", true);
479
480 return UIHelper.getSelfContactPicture(conversation.getAccount(), 48,
481 showPhoneSelfContactPicture, getActivity());
482 }
483
484 @Override
485 public void onStart() {
486 super.onStart();
487 this.activity = (ConversationActivity) getActivity();
488 SharedPreferences preferences = PreferenceManager
489 .getDefaultSharedPreferences(activity);
490 this.useSubject = preferences.getBoolean("use_subject_in_muc", true);
491 if (activity.xmppConnectionServiceBound) {
492 this.onBackendConnected();
493 }
494 }
495
496 @Override
497 public void onStop() {
498 super.onStop();
499 if (this.conversation != null) {
500 this.conversation.setNextMessage(chatMsg.getText().toString());
501 }
502 }
503
504 public void onBackendConnected() {
505 this.conversation = activity.getSelectedConversation();
506 if (this.conversation == null) {
507 return;
508 }
509 String oldString = conversation.getNextMessage().trim();
510 if (this.pastedText == null) {
511 this.chatMsg.setText(oldString);
512 } else {
513
514 if (oldString.isEmpty()) {
515 chatMsg.setText(pastedText);
516 } else {
517 chatMsg.setText(oldString + " " + pastedText);
518 }
519 pastedText = null;
520 }
521 int position = chatMsg.length();
522 Editable etext = chatMsg.getText();
523 Selection.setSelection(etext, position);
524 this.selfBitmap = findSelfPicture();
525 updateMessages();
526 if (activity.getSlidingPaneLayout().isSlideable()) {
527 if (!activity.shouldPaneBeOpen()) {
528 activity.getSlidingPaneLayout().closePane();
529 activity.getActionBar().setDisplayHomeAsUpEnabled(true);
530 activity.getActionBar().setTitle(
531 conversation.getName(useSubject));
532 activity.invalidateOptionsMenu();
533
534 }
535 }
536 if (conversation.getMode() == Conversation.MODE_MULTI) {
537 activity.xmppConnectionService
538 .setOnRenameListener(new OnRenameListener() {
539
540 @Override
541 public void onRename(final boolean success) {
542 activity.xmppConnectionService
543 .updateConversation(conversation);
544 getActivity().runOnUiThread(new Runnable() {
545
546 @Override
547 public void run() {
548 if (success) {
549 Toast.makeText(
550 getActivity(),
551 getString(R.string.your_nick_has_been_changed),
552 Toast.LENGTH_SHORT).show();
553 } else {
554 Toast.makeText(
555 getActivity(),
556 getString(R.string.nick_in_use),
557 Toast.LENGTH_SHORT).show();
558 }
559 }
560 });
561 }
562 });
563 }
564 }
565
566 private void decryptMessage(final Message message) {
567 PgpEngine engine = activity.xmppConnectionService.getPgpEngine();
568 if (engine != null) {
569 engine.decrypt(message, new UiCallback() {
570
571 @Override
572 public void userInputRequried(PendingIntent pi) {
573 askForPassphraseIntent = pi.getIntentSender();
574 pgpInfo.setVisibility(View.VISIBLE);
575 }
576
577 @Override
578 public void success() {
579 activity.xmppConnectionService.databaseBackend
580 .updateMessage(message);
581 updateMessages();
582 }
583
584 @Override
585 public void error(int error) {
586 message.setEncryption(Message.ENCRYPTION_DECRYPTION_FAILED);
587 // updateMessages();
588 }
589 });
590 } else {
591 pgpInfo.setVisibility(View.VISIBLE);
592 }
593 }
594
595 public void updateMessages() {
596 if (getView() == null) {
597 return;
598 }
599 ConversationActivity activity = (ConversationActivity) getActivity();
600 if (this.conversation != null) {
601 for (Message message : this.conversation.getMessages()) {
602 if ((message.getEncryption() == Message.ENCRYPTION_PGP)
603 && ((message.getStatus() == Message.STATUS_RECIEVED) || (message
604 .getStatus() == Message.STATUS_SEND))) {
605 decryptMessage(message);
606 break;
607 }
608 }
609 this.messageList.clear();
610 this.messageList.addAll(this.conversation.getMessages());
611 this.messageListAdapter.notifyDataSetChanged();
612 if (conversation.getMode() == Conversation.MODE_SINGLE) {
613 if (messageList.size() >= 1) {
614 makeFingerprintWarning(conversation.getLatestEncryption());
615 }
616 } else {
617 if (conversation.getMucOptions().getError() != 0) {
618 mucError.setVisibility(View.VISIBLE);
619 if (conversation.getMucOptions().getError() == MucOptions.ERROR_NICK_IN_USE) {
620 mucErrorText.setText(getString(R.string.nick_in_use));
621 }
622 } else {
623 mucError.setVisibility(View.GONE);
624 }
625 }
626 getActivity().invalidateOptionsMenu();
627 updateChatMsgHint();
628 int size = this.messageList.size();
629 if (size >= 1)
630 messagesView.setSelection(size - 1);
631 if (!activity.shouldPaneBeOpen()) {
632 conversation.markRead();
633 // TODO update notifications
634 UIHelper.updateNotification(getActivity(),
635 activity.getConversationList(), null, false);
636 activity.updateConversationList();
637 }
638 }
639 }
640
641 protected void makeFingerprintWarning(int latestEncryption) {
642 final LinearLayout fingerprintWarning = (LinearLayout) getView()
643 .findViewById(R.id.new_fingerprint);
644 Set<String> knownFingerprints = conversation.getContact()
645 .getOtrFingerprints();
646 if ((latestEncryption == Message.ENCRYPTION_OTR)
647 && (conversation.hasValidOtrSession()
648 && (conversation.getOtrSession().getSessionStatus() == SessionStatus.ENCRYPTED) && (!knownFingerprints
649 .contains(conversation.getOtrFingerprint())))) {
650 fingerprintWarning.setVisibility(View.VISIBLE);
651 TextView fingerprint = (TextView) getView().findViewById(
652 R.id.otr_fingerprint);
653 fingerprint.setText(conversation.getOtrFingerprint());
654 fingerprintWarning.setOnClickListener(new OnClickListener() {
655
656 @Override
657 public void onClick(View v) {
658 AlertDialog dialog = UIHelper.getVerifyFingerprintDialog(
659 (ConversationActivity) getActivity(), conversation,
660 fingerprintWarning);
661 dialog.show();
662 }
663 });
664 } else {
665 fingerprintWarning.setVisibility(View.GONE);
666 }
667 }
668
669 protected void sendPlainTextMessage(Message message) {
670 ConversationActivity activity = (ConversationActivity) getActivity();
671 activity.xmppConnectionService.sendMessage(message, null);
672 chatMsg.setText("");
673 }
674
675 protected void sendPgpMessage(final Message message) {
676 activity.pendingMessage = message;
677 final ConversationActivity activity = (ConversationActivity) getActivity();
678 final XmppConnectionService xmppService = activity.xmppConnectionService;
679 final Contact contact = message.getConversation().getContact();
680 if (activity.hasPgp()) {
681 if (contact.getPgpKeyId() != 0) {
682 xmppService.getPgpEngine().hasKey(contact, new UiCallback() {
683
684 @Override
685 public void userInputRequried(PendingIntent pi) {
686 activity.runIntent(pi,
687 ConversationActivity.REQUEST_ENCRYPT_MESSAGE);
688 }
689
690 @Override
691 public void success() {
692 activity.encryptTextMessage();
693 }
694
695 @Override
696 public void error(int error) {
697
698 }
699 });
700
701 } else {
702 showNoPGPKeyDialog(new DialogInterface.OnClickListener() {
703
704 @Override
705 public void onClick(DialogInterface dialog, int which) {
706 conversation.setNextEncryption(Message.ENCRYPTION_NONE);
707 message.setEncryption(Message.ENCRYPTION_NONE);
708 xmppService.sendMessage(message, null);
709 chatMsg.setText("");
710 }
711 });
712 }
713 }
714 }
715
716 public void showNoPGPKeyDialog(DialogInterface.OnClickListener listener) {
717 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
718 builder.setTitle(getString(R.string.no_pgp_key));
719 builder.setIconAttribute(android.R.attr.alertDialogIcon);
720 builder.setMessage(getText(R.string.contact_has_no_pgp_key));
721 builder.setNegativeButton(getString(R.string.cancel), null);
722 builder.setPositiveButton(getString(R.string.send_unencrypted),
723 listener);
724 builder.create().show();
725 }
726
727 protected void sendOtrMessage(final Message message) {
728 ConversationActivity activity = (ConversationActivity) getActivity();
729 final XmppConnectionService xmppService = activity.xmppConnectionService;
730 if (conversation.hasValidOtrSession()) {
731 activity.xmppConnectionService.sendMessage(message, null);
732 chatMsg.setText("");
733 } else {
734 activity.selectPresence(message.getConversation(),
735 new OnPresenceSelected() {
736
737 @Override
738 public void onPresenceSelected(boolean success,
739 String presence) {
740 if (success) {
741 xmppService.sendMessage(message, presence);
742 chatMsg.setText("");
743 }
744 }
745
746 @Override
747 public void onSendPlainTextInstead() {
748 message.setEncryption(Message.ENCRYPTION_NONE);
749 xmppService.sendMessage(message, null);
750 chatMsg.setText("");
751 }
752 }, "otr");
753 }
754 }
755
756 private static class ViewHolder {
757
758 protected Button download_button;
759 protected ImageView image;
760 protected ImageView indicator;
761 protected TextView time;
762 protected TextView messageBody;
763 protected ImageView contact_picture;
764
765 }
766
767 private class BitmapCache {
768 private HashMap<String, Bitmap> bitmaps = new HashMap<String, Bitmap>();
769
770 public Bitmap get(String name, Contact contact, Context context) {
771 if (bitmaps.containsKey(name)) {
772 return bitmaps.get(name);
773 } else {
774 Bitmap bm;
775 if (contact != null) {
776 bm = UIHelper
777 .getContactPicture(contact, 48, context, false);
778 } else {
779 bm = UIHelper.getContactPicture(name, 48, context, false);
780 }
781 bitmaps.put(name, bm);
782 return bm;
783 }
784 }
785 }
786
787 public void setText(String text) {
788 this.pastedText = text;
789 }
790
791 public void clearInputField() {
792 this.chatMsg.setText("");
793 }
794}