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