1package eu.siacs.conversations.ui;
2
3import android.annotation.SuppressLint;
4import android.app.ActionBar;
5import android.app.AlertDialog;
6import android.app.FragmentTransaction;
7import android.app.PendingIntent;
8import android.content.ClipData;
9import android.content.DialogInterface;
10import android.content.DialogInterface.OnClickListener;
11import android.content.Intent;
12import android.content.IntentSender.SendIntentException;
13import android.net.Uri;
14import android.os.Build;
15import android.os.Bundle;
16import android.provider.MediaStore;
17import android.support.v4.widget.SlidingPaneLayout;
18import android.support.v4.widget.SlidingPaneLayout.PanelSlideListener;
19import android.util.Log;
20import android.view.Menu;
21import android.view.MenuItem;
22import android.view.View;
23import android.widget.AdapterView;
24import android.widget.AdapterView.OnItemClickListener;
25import android.widget.ArrayAdapter;
26import android.widget.CheckBox;
27import android.widget.PopupMenu;
28import android.widget.PopupMenu.OnMenuItemClickListener;
29import android.widget.Toast;
30
31import net.java.otr4j.session.SessionStatus;
32import de.timroes.android.listview.EnhancedListView;
33
34import java.util.ArrayList;
35import java.util.Iterator;
36import java.util.List;
37
38import eu.siacs.conversations.Config;
39import eu.siacs.conversations.R;
40import eu.siacs.conversations.crypto.axolotl.AxolotlService;
41import eu.siacs.conversations.entities.Account;
42import eu.siacs.conversations.entities.Blockable;
43import eu.siacs.conversations.entities.Contact;
44import eu.siacs.conversations.entities.Conversation;
45import eu.siacs.conversations.entities.Message;
46import eu.siacs.conversations.services.XmppConnectionService;
47import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
48import eu.siacs.conversations.services.XmppConnectionService.OnConversationUpdate;
49import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate;
50import eu.siacs.conversations.ui.adapter.ConversationAdapter;
51import eu.siacs.conversations.utils.ExceptionHelper;
52import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
53
54public class ConversationActivity extends XmppActivity
55 implements OnAccountUpdate, OnConversationUpdate, OnRosterUpdate, OnUpdateBlocklist, XmppConnectionService.OnShowErrorToast {
56
57 public static final String ACTION_DOWNLOAD = "eu.siacs.conversations.action.DOWNLOAD";
58
59 public static final String VIEW_CONVERSATION = "viewConversation";
60 public static final String CONVERSATION = "conversationUuid";
61 public static final String MESSAGE = "messageUuid";
62 public static final String TEXT = "text";
63 public static final String NICK = "nick";
64
65 public static final int REQUEST_SEND_MESSAGE = 0x0201;
66 public static final int REQUEST_DECRYPT_PGP = 0x0202;
67 public static final int REQUEST_ENCRYPT_MESSAGE = 0x0207;
68 public static final int ATTACHMENT_CHOICE_CHOOSE_IMAGE = 0x0301;
69 public static final int ATTACHMENT_CHOICE_TAKE_PHOTO = 0x0302;
70 public static final int ATTACHMENT_CHOICE_CHOOSE_FILE = 0x0303;
71 public static final int ATTACHMENT_CHOICE_RECORD_VOICE = 0x0304;
72 public static final int ATTACHMENT_CHOICE_LOCATION = 0x0305;
73 private static final String STATE_OPEN_CONVERSATION = "state_open_conversation";
74 private static final String STATE_PANEL_OPEN = "state_panel_open";
75 private static final String STATE_PENDING_URI = "state_pending_uri";
76
77 private String mOpenConverstaion = null;
78 private boolean mPanelOpen = true;
79 final private List<Uri> mPendingImageUris = new ArrayList<>();
80 final private List<Uri> mPendingFileUris = new ArrayList<>();
81 private Uri mPendingGeoUri = null;
82
83 private View mContentView;
84
85 private List<Conversation> conversationList = new ArrayList<>();
86 private Conversation swipedConversation = null;
87 private Conversation mSelectedConversation = null;
88 private EnhancedListView listView;
89 private ConversationFragment mConversationFragment;
90
91 private ArrayAdapter<Conversation> listAdapter;
92
93 private Toast prepareFileToast;
94
95 private boolean mActivityPaused = false;
96 private boolean mRedirected = true;
97
98 public Conversation getSelectedConversation() {
99 return this.mSelectedConversation;
100 }
101
102 public void setSelectedConversation(Conversation conversation) {
103 this.mSelectedConversation = conversation;
104 }
105
106 public void showConversationsOverview() {
107 if (mContentView instanceof SlidingPaneLayout) {
108 SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
109 mSlidingPaneLayout.openPane();
110 }
111 }
112
113 @Override
114 protected String getShareableUri() {
115 Conversation conversation = getSelectedConversation();
116 if (conversation != null) {
117 return conversation.getAccount().getShareableUri();
118 } else {
119 return "";
120 }
121 }
122
123 public void hideConversationsOverview() {
124 if (mContentView instanceof SlidingPaneLayout) {
125 SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
126 mSlidingPaneLayout.closePane();
127 }
128 }
129
130 public boolean isConversationsOverviewHideable() {
131 if (mContentView instanceof SlidingPaneLayout) {
132 SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
133 return mSlidingPaneLayout.isSlideable();
134 } else {
135 return false;
136 }
137 }
138
139 public boolean isConversationsOverviewVisable() {
140 if (mContentView instanceof SlidingPaneLayout) {
141 SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
142 return mSlidingPaneLayout.isOpen();
143 } else {
144 return true;
145 }
146 }
147
148 @Override
149 protected void onCreate(final Bundle savedInstanceState) {
150 super.onCreate(savedInstanceState);
151 if (savedInstanceState != null) {
152 mOpenConverstaion = savedInstanceState.getString(STATE_OPEN_CONVERSATION, null);
153 mPanelOpen = savedInstanceState.getBoolean(STATE_PANEL_OPEN, true);
154 String pending = savedInstanceState.getString(STATE_PENDING_URI, null);
155 if (pending != null) {
156 mPendingImageUris.clear();
157 mPendingImageUris.add(Uri.parse(pending));
158 }
159 }
160
161 setContentView(R.layout.fragment_conversations_overview);
162
163 this.mConversationFragment = new ConversationFragment();
164 FragmentTransaction transaction = getFragmentManager().beginTransaction();
165 transaction.replace(R.id.selected_conversation, this.mConversationFragment, "conversation");
166 transaction.commit();
167
168 listView = (EnhancedListView) findViewById(R.id.list);
169 this.listAdapter = new ConversationAdapter(this, conversationList);
170 listView.setAdapter(this.listAdapter);
171
172 if (getActionBar() != null) {
173 getActionBar().setDisplayHomeAsUpEnabled(false);
174 getActionBar().setHomeButtonEnabled(false);
175 }
176
177 listView.setOnItemClickListener(new OnItemClickListener() {
178
179 @Override
180 public void onItemClick(AdapterView<?> arg0, View clickedView,
181 int position, long arg3) {
182 if (getSelectedConversation() != conversationList.get(position)) {
183 setSelectedConversation(conversationList.get(position));
184 ConversationActivity.this.mConversationFragment.reInit(getSelectedConversation());
185 }
186 hideConversationsOverview();
187 openConversation();
188 }
189 });
190
191 listView.setDismissCallback(new EnhancedListView.OnDismissCallback() {
192
193 @Override
194 public EnhancedListView.Undoable onDismiss(final EnhancedListView enhancedListView, final int position) {
195
196 final int index = listView.getFirstVisiblePosition();
197 View v = listView.getChildAt(0);
198 final int top = (v == null) ? 0 : (v.getTop() - listView.getPaddingTop());
199
200 swipedConversation = listAdapter.getItem(position);
201 listAdapter.remove(swipedConversation);
202 swipedConversation.markRead();
203 xmppConnectionService.getNotificationService().clear(swipedConversation);
204
205 final boolean formerlySelected = (getSelectedConversation() == swipedConversation);
206 if (position == 0 && listAdapter.getCount() == 0) {
207 endConversation(swipedConversation, false, true);
208 return null;
209 } else if (formerlySelected) {
210 setSelectedConversation(listAdapter.getItem(0));
211 ConversationActivity.this.mConversationFragment
212 .reInit(getSelectedConversation());
213 }
214
215 return new EnhancedListView.Undoable() {
216
217 @Override
218 public void undo() {
219 listAdapter.insert(swipedConversation, position);
220 if (formerlySelected) {
221 setSelectedConversation(swipedConversation);
222 ConversationActivity.this.mConversationFragment
223 .reInit(getSelectedConversation());
224 }
225 swipedConversation = null;
226 listView.setSelectionFromTop(index + (listView.getChildCount() < position ? 1 : 0), top);
227 }
228
229 @Override
230 public void discard() {
231 if (!swipedConversation.isRead()
232 && swipedConversation.getMode() == Conversation.MODE_SINGLE) {
233 swipedConversation = null;
234 return;
235 }
236 endConversation(swipedConversation, false, false);
237 swipedConversation = null;
238 }
239
240 @Override
241 public String getTitle() {
242 if (swipedConversation.getMode() == Conversation.MODE_MULTI) {
243 return getResources().getString(R.string.title_undo_swipe_out_muc);
244 } else {
245 return getResources().getString(R.string.title_undo_swipe_out_conversation);
246 }
247 }
248 };
249 }
250 });
251 listView.enableSwipeToDismiss();
252 listView.setSwipingLayout(R.id.swipeable_item);
253 listView.setUndoStyle(EnhancedListView.UndoStyle.SINGLE_POPUP);
254 listView.setUndoHideDelay(5000);
255 listView.setRequireTouchBeforeDismiss(false);
256
257 mContentView = findViewById(R.id.content_view_spl);
258 if (mContentView == null) {
259 mContentView = findViewById(R.id.content_view_ll);
260 }
261 if (mContentView instanceof SlidingPaneLayout) {
262 SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
263 mSlidingPaneLayout.setParallaxDistance(150);
264 mSlidingPaneLayout
265 .setShadowResource(R.drawable.es_slidingpane_shadow);
266 mSlidingPaneLayout.setSliderFadeColor(0);
267 mSlidingPaneLayout.setPanelSlideListener(new PanelSlideListener() {
268
269 @Override
270 public void onPanelOpened(View arg0) {
271 updateActionBarTitle();
272 invalidateOptionsMenu();
273 hideKeyboard();
274 if (xmppConnectionServiceBound) {
275 xmppConnectionService.getNotificationService()
276 .setOpenConversation(null);
277 }
278 closeContextMenu();
279 }
280
281 @Override
282 public void onPanelClosed(View arg0) {
283 listView.discardUndo();
284 openConversation();
285 }
286
287 @Override
288 public void onPanelSlide(View arg0, float arg1) {
289 // TODO Auto-generated method stub
290
291 }
292 });
293 }
294 }
295
296 @Override
297 public void switchToConversation(Conversation conversation) {
298 setSelectedConversation(conversation);
299 runOnUiThread(new Runnable() {
300 @Override
301 public void run() {
302 ConversationActivity.this.mConversationFragment.reInit(getSelectedConversation());
303 openConversation();
304 }
305 });
306 }
307
308 private void updateActionBarTitle() {
309 updateActionBarTitle(isConversationsOverviewHideable() && !isConversationsOverviewVisable());
310 }
311
312 private void updateActionBarTitle(boolean titleShouldBeName) {
313 final ActionBar ab = getActionBar();
314 final Conversation conversation = getSelectedConversation();
315 if (ab != null) {
316 if (titleShouldBeName && conversation != null) {
317 ab.setDisplayHomeAsUpEnabled(true);
318 ab.setHomeButtonEnabled(true);
319 if (conversation.getMode() == Conversation.MODE_SINGLE || useSubjectToIdentifyConference()) {
320 ab.setTitle(conversation.getName());
321 } else {
322 ab.setTitle(conversation.getJid().toBareJid().toString());
323 }
324 } else {
325 ab.setDisplayHomeAsUpEnabled(false);
326 ab.setHomeButtonEnabled(false);
327 ab.setTitle(R.string.app_name);
328 }
329 }
330 }
331
332 private void openConversation() {
333 this.updateActionBarTitle();
334 this.invalidateOptionsMenu();
335 if (xmppConnectionServiceBound) {
336 final Conversation conversation = getSelectedConversation();
337 xmppConnectionService.getNotificationService().setOpenConversation(conversation);
338 sendReadMarkerIfNecessary(conversation);
339 }
340 listAdapter.notifyDataSetChanged();
341 }
342
343 public void sendReadMarkerIfNecessary(final Conversation conversation) {
344 if (!mActivityPaused && conversation != null) {
345 if (!conversation.isRead()) {
346 xmppConnectionService.sendReadMarker(conversation);
347 } else {
348 xmppConnectionService.markRead(conversation);
349 }
350 }
351 }
352
353 @Override
354 public boolean onCreateOptionsMenu(Menu menu) {
355 getMenuInflater().inflate(R.menu.conversations, menu);
356 final MenuItem menuSecure = menu.findItem(R.id.action_security);
357 final MenuItem menuArchive = menu.findItem(R.id.action_archive);
358 final MenuItem menuMucDetails = menu.findItem(R.id.action_muc_details);
359 final MenuItem menuContactDetails = menu.findItem(R.id.action_contact_details);
360 final MenuItem menuAttach = menu.findItem(R.id.action_attach_file);
361 final MenuItem menuClearHistory = menu.findItem(R.id.action_clear_history);
362 final MenuItem menuAdd = menu.findItem(R.id.action_add);
363 final MenuItem menuInviteContact = menu.findItem(R.id.action_invite);
364 final MenuItem menuMute = menu.findItem(R.id.action_mute);
365 final MenuItem menuUnmute = menu.findItem(R.id.action_unmute);
366
367 if (isConversationsOverviewVisable() && isConversationsOverviewHideable()) {
368 menuArchive.setVisible(false);
369 menuMucDetails.setVisible(false);
370 menuContactDetails.setVisible(false);
371 menuSecure.setVisible(false);
372 menuInviteContact.setVisible(false);
373 menuAttach.setVisible(false);
374 menuClearHistory.setVisible(false);
375 menuMute.setVisible(false);
376 menuUnmute.setVisible(false);
377 } else {
378 menuAdd.setVisible(!isConversationsOverviewHideable());
379 if (this.getSelectedConversation() != null) {
380 if (this.getSelectedConversation().getNextEncryption(forceEncryption()) != Message.ENCRYPTION_NONE) {
381 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
382 menuSecure.setIcon(R.drawable.ic_lock_white_24dp);
383 } else {
384 menuSecure.setIcon(R.drawable.ic_action_secure);
385 }
386 }
387 if (this.getSelectedConversation().getMode() == Conversation.MODE_MULTI) {
388 menuContactDetails.setVisible(false);
389 menuAttach.setVisible(getSelectedConversation().getAccount().httpUploadAvailable());
390 menuInviteContact.setVisible(getSelectedConversation().getMucOptions().canInvite());
391 } else {
392 menuMucDetails.setVisible(false);
393 }
394 if (this.getSelectedConversation().isMuted()) {
395 menuMute.setVisible(false);
396 } else {
397 menuUnmute.setVisible(false);
398 }
399 }
400 }
401 return true;
402 }
403
404 private void selectPresenceToAttachFile(final int attachmentChoice, final int encryption) {
405 final Conversation conversation = getSelectedConversation();
406 final Account account = conversation.getAccount();
407 final OnPresenceSelected callback = new OnPresenceSelected() {
408
409 @Override
410 public void onPresenceSelected() {
411 Intent intent = new Intent();
412 boolean chooser = false;
413 String fallbackPackageId = null;
414 switch (attachmentChoice) {
415 case ATTACHMENT_CHOICE_CHOOSE_IMAGE:
416 intent.setAction(Intent.ACTION_GET_CONTENT);
417 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
418 intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,true);
419 }
420 intent.setType("image/*");
421 chooser = true;
422 break;
423 case ATTACHMENT_CHOICE_TAKE_PHOTO:
424 Uri uri = xmppConnectionService.getFileBackend().getTakePhotoUri();
425 intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
426 intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
427 mPendingImageUris.clear();
428 mPendingImageUris.add(uri);
429 break;
430 case ATTACHMENT_CHOICE_CHOOSE_FILE:
431 chooser = true;
432 intent.setType("*/*");
433 intent.addCategory(Intent.CATEGORY_OPENABLE);
434 intent.setAction(Intent.ACTION_GET_CONTENT);
435 break;
436 case ATTACHMENT_CHOICE_RECORD_VOICE:
437 intent.setAction(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
438 fallbackPackageId = "eu.siacs.conversations.voicerecorder";
439 break;
440 case ATTACHMENT_CHOICE_LOCATION:
441 intent.setAction("eu.siacs.conversations.location.request");
442 fallbackPackageId = "eu.siacs.conversations.sharelocation";
443 break;
444 }
445 if (intent.resolveActivity(getPackageManager()) != null) {
446 if (chooser) {
447 startActivityForResult(
448 Intent.createChooser(intent, getString(R.string.perform_action_with)),
449 attachmentChoice);
450 } else {
451 startActivityForResult(intent, attachmentChoice);
452 }
453 } else if (fallbackPackageId != null) {
454 startActivity(getInstallApkIntent(fallbackPackageId));
455 }
456 }
457 };
458 if ((account.httpUploadAvailable() || attachmentChoice == ATTACHMENT_CHOICE_LOCATION) && encryption != Message.ENCRYPTION_OTR) {
459 conversation.setNextCounterpart(null);
460 callback.onPresenceSelected();
461 } else {
462 selectPresence(conversation,callback);
463 }
464 }
465
466 private Intent getInstallApkIntent(final String packageId) {
467 Intent intent = new Intent(Intent.ACTION_VIEW);
468 intent.setData(Uri.parse("market://details?id="+packageId));
469 if (intent.resolveActivity(getPackageManager()) != null) {
470 return intent;
471 } else {
472 intent.setData(Uri.parse("http://play.google.com/store/apps/details?id="+packageId));
473 return intent;
474 }
475 }
476
477 public void attachFile(final int attachmentChoice) {
478 switch (attachmentChoice) {
479 case ATTACHMENT_CHOICE_LOCATION:
480 getPreferences().edit().putString("recently_used_quick_action","location").apply();
481 break;
482 case ATTACHMENT_CHOICE_RECORD_VOICE:
483 getPreferences().edit().putString("recently_used_quick_action","voice").apply();
484 break;
485 case ATTACHMENT_CHOICE_TAKE_PHOTO:
486 getPreferences().edit().putString("recently_used_quick_action","photo").apply();
487 break;
488 case ATTACHMENT_CHOICE_CHOOSE_IMAGE:
489 getPreferences().edit().putString("recently_used_quick_action","picture").apply();
490 break;
491 }
492 final Conversation conversation = getSelectedConversation();
493 final int encryption = conversation.getNextEncryption(forceEncryption());
494 if (encryption == Message.ENCRYPTION_PGP) {
495 if (hasPgp()) {
496 if (conversation.getContact().getPgpKeyId() != 0) {
497 xmppConnectionService.getPgpEngine().hasKey(
498 conversation.getContact(),
499 new UiCallback<Contact>() {
500
501 @Override
502 public void userInputRequried(PendingIntent pi,
503 Contact contact) {
504 ConversationActivity.this.runIntent(pi,attachmentChoice);
505 }
506
507 @Override
508 public void success(Contact contact) {
509 selectPresenceToAttachFile(attachmentChoice,encryption);
510 }
511
512 @Override
513 public void error(int error, Contact contact) {
514 displayErrorDialog(error);
515 }
516 });
517 } else {
518 final ConversationFragment fragment = (ConversationFragment) getFragmentManager()
519 .findFragmentByTag("conversation");
520 if (fragment != null) {
521 fragment.showNoPGPKeyDialog(false,
522 new OnClickListener() {
523
524 @Override
525 public void onClick(DialogInterface dialog,
526 int which) {
527 conversation
528 .setNextEncryption(Message.ENCRYPTION_NONE);
529 xmppConnectionService.databaseBackend
530 .updateConversation(conversation);
531 selectPresenceToAttachFile(attachmentChoice,Message.ENCRYPTION_NONE);
532 }
533 });
534 }
535 }
536 } else {
537 showInstallPgpDialog();
538 }
539 } else {
540 selectPresenceToAttachFile(attachmentChoice,encryption);
541 }
542 }
543
544 @Override
545 public boolean onOptionsItemSelected(final MenuItem item) {
546 if (item.getItemId() == android.R.id.home) {
547 showConversationsOverview();
548 return true;
549 } else if (item.getItemId() == R.id.action_add) {
550 startActivity(new Intent(this, StartConversationActivity.class));
551 return true;
552 } else if (getSelectedConversation() != null) {
553 switch (item.getItemId()) {
554 case R.id.action_attach_file:
555 attachFileDialog();
556 break;
557 case R.id.action_archive:
558 this.endConversation(getSelectedConversation());
559 break;
560 case R.id.action_contact_details:
561 switchToContactDetails(getSelectedConversation().getContact());
562 break;
563 case R.id.action_muc_details:
564 Intent intent = new Intent(this,
565 ConferenceDetailsActivity.class);
566 intent.setAction(ConferenceDetailsActivity.ACTION_VIEW_MUC);
567 intent.putExtra("uuid", getSelectedConversation().getUuid());
568 startActivity(intent);
569 break;
570 case R.id.action_invite:
571 inviteToConversation(getSelectedConversation());
572 break;
573 case R.id.action_security:
574 selectEncryptionDialog(getSelectedConversation());
575 break;
576 case R.id.action_clear_history:
577 clearHistoryDialog(getSelectedConversation());
578 break;
579 case R.id.action_mute:
580 muteConversationDialog(getSelectedConversation());
581 break;
582 case R.id.action_unmute:
583 unmuteConversation(getSelectedConversation());
584 break;
585 case R.id.action_block:
586 BlockContactDialog.show(this, xmppConnectionService, getSelectedConversation());
587 break;
588 case R.id.action_unblock:
589 BlockContactDialog.show(this, xmppConnectionService, getSelectedConversation());
590 break;
591 default:
592 break;
593 }
594 return super.onOptionsItemSelected(item);
595 } else {
596 return super.onOptionsItemSelected(item);
597 }
598 }
599
600 public void endConversation(Conversation conversation) {
601 endConversation(conversation, true, true);
602 }
603
604 public void endConversation(Conversation conversation, boolean showOverview, boolean reinit) {
605 if (showOverview) {
606 showConversationsOverview();
607 }
608 xmppConnectionService.archiveConversation(conversation);
609 if (reinit) {
610 if (conversationList.size() > 0) {
611 setSelectedConversation(conversationList.get(0));
612 this.mConversationFragment.reInit(getSelectedConversation());
613 } else {
614 setSelectedConversation(null);
615 }
616 }
617 }
618
619 @SuppressLint("InflateParams")
620 protected void clearHistoryDialog(final Conversation conversation) {
621 AlertDialog.Builder builder = new AlertDialog.Builder(this);
622 builder.setTitle(getString(R.string.clear_conversation_history));
623 View dialogView = getLayoutInflater().inflate(
624 R.layout.dialog_clear_history, null);
625 final CheckBox endConversationCheckBox = (CheckBox) dialogView
626 .findViewById(R.id.end_conversation_checkbox);
627 builder.setView(dialogView);
628 builder.setNegativeButton(getString(R.string.cancel), null);
629 builder.setPositiveButton(getString(R.string.delete_messages),
630 new OnClickListener() {
631
632 @Override
633 public void onClick(DialogInterface dialog, int which) {
634 ConversationActivity.this.xmppConnectionService.clearConversationHistory(conversation);
635 if (endConversationCheckBox.isChecked()) {
636 endConversation(conversation);
637 } else {
638 updateConversationList();
639 ConversationActivity.this.mConversationFragment.updateMessages();
640 }
641 }
642 });
643 builder.create().show();
644 }
645
646 protected void attachFileDialog() {
647 View menuAttachFile = findViewById(R.id.action_attach_file);
648 if (menuAttachFile == null) {
649 return;
650 }
651 PopupMenu attachFilePopup = new PopupMenu(this, menuAttachFile);
652 attachFilePopup.inflate(R.menu.attachment_choices);
653 if (new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION).resolveActivity(getPackageManager()) == null) {
654 attachFilePopup.getMenu().findItem(R.id.attach_record_voice).setVisible(false);
655 }
656 if (new Intent("eu.siacs.conversations.location.request").resolveActivity(getPackageManager()) == null) {
657 attachFilePopup.getMenu().findItem(R.id.attach_location).setVisible(false);
658 }
659 attachFilePopup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
660
661 @Override
662 public boolean onMenuItemClick(MenuItem item) {
663 switch (item.getItemId()) {
664 case R.id.attach_choose_picture:
665 attachFile(ATTACHMENT_CHOICE_CHOOSE_IMAGE);
666 break;
667 case R.id.attach_take_picture:
668 attachFile(ATTACHMENT_CHOICE_TAKE_PHOTO);
669 break;
670 case R.id.attach_choose_file:
671 attachFile(ATTACHMENT_CHOICE_CHOOSE_FILE);
672 break;
673 case R.id.attach_record_voice:
674 attachFile(ATTACHMENT_CHOICE_RECORD_VOICE);
675 break;
676 case R.id.attach_location:
677 attachFile(ATTACHMENT_CHOICE_LOCATION);
678 break;
679 }
680 return false;
681 }
682 });
683 attachFilePopup.show();
684 }
685
686 public void verifyOtrSessionDialog(final Conversation conversation, View view) {
687 if (!conversation.hasValidOtrSession() || conversation.getOtrSession().getSessionStatus() != SessionStatus.ENCRYPTED) {
688 Toast.makeText(this, R.string.otr_session_not_started, Toast.LENGTH_LONG).show();
689 return;
690 }
691 if (view == null) {
692 return;
693 }
694 PopupMenu popup = new PopupMenu(this, view);
695 popup.inflate(R.menu.verification_choices);
696 popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
697 @Override
698 public boolean onMenuItemClick(MenuItem menuItem) {
699 Intent intent = new Intent(ConversationActivity.this, VerifyOTRActivity.class);
700 intent.setAction(VerifyOTRActivity.ACTION_VERIFY_CONTACT);
701 intent.putExtra("contact", conversation.getContact().getJid().toBareJid().toString());
702 intent.putExtra("account", conversation.getAccount().getJid().toBareJid().toString());
703 switch (menuItem.getItemId()) {
704 case R.id.scan_fingerprint:
705 intent.putExtra("mode",VerifyOTRActivity.MODE_SCAN_FINGERPRINT);
706 break;
707 case R.id.ask_question:
708 intent.putExtra("mode",VerifyOTRActivity.MODE_ASK_QUESTION);
709 break;
710 case R.id.manual_verification:
711 intent.putExtra("mode",VerifyOTRActivity.MODE_MANUAL_VERIFICATION);
712 break;
713 }
714 startActivity(intent);
715 return true;
716 }
717 });
718 popup.show();
719 }
720
721 protected void selectEncryptionDialog(final Conversation conversation) {
722 View menuItemView = findViewById(R.id.action_security);
723 if (menuItemView == null) {
724 return;
725 }
726 PopupMenu popup = new PopupMenu(this, menuItemView);
727 final ConversationFragment fragment = (ConversationFragment) getFragmentManager()
728 .findFragmentByTag("conversation");
729 if (fragment != null) {
730 popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
731
732 @Override
733 public boolean onMenuItemClick(MenuItem item) {
734 switch (item.getItemId()) {
735 case R.id.encryption_choice_none:
736 conversation.setNextEncryption(Message.ENCRYPTION_NONE);
737 item.setChecked(true);
738 break;
739 case R.id.encryption_choice_otr:
740 conversation.setNextEncryption(Message.ENCRYPTION_OTR);
741 item.setChecked(true);
742 break;
743 case R.id.encryption_choice_pgp:
744 if (hasPgp()) {
745 if (conversation.getAccount().getKeys().has("pgp_signature")) {
746 conversation.setNextEncryption(Message.ENCRYPTION_PGP);
747 item.setChecked(true);
748 } else {
749 announcePgp(conversation.getAccount(),conversation);
750 }
751 } else {
752 showInstallPgpDialog();
753 }
754 break;
755 case R.id.encryption_choice_axolotl:
756 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(conversation.getAccount())
757 + "Enabled axolotl for Contact " + conversation.getContact().getJid());
758 conversation.setNextEncryption(Message.ENCRYPTION_AXOLOTL);
759 item.setChecked(true);
760 break;
761 default:
762 conversation.setNextEncryption(Message.ENCRYPTION_NONE);
763 break;
764 }
765 xmppConnectionService.databaseBackend.updateConversation(conversation);
766 fragment.updateChatMsgHint();
767 invalidateOptionsMenu();
768 return true;
769 }
770 });
771 popup.inflate(R.menu.encryption_choices);
772 MenuItem otr = popup.getMenu().findItem(R.id.encryption_choice_otr);
773 MenuItem none = popup.getMenu().findItem(R.id.encryption_choice_none);
774 MenuItem pgp = popup.getMenu().findItem(R.id.encryption_choice_pgp);
775 MenuItem axolotl = popup.getMenu().findItem(R.id.encryption_choice_axolotl);
776 if (conversation.getMode() == Conversation.MODE_MULTI) {
777 otr.setEnabled(false);
778 axolotl.setEnabled(false);
779 } else {
780 if (forceEncryption()) {
781 none.setVisible(false);
782 }
783 }
784 if (!conversation.getAccount().getAxolotlService().isContactAxolotlCapable(conversation.getContact())) {
785 axolotl.setEnabled(false);
786 }
787 switch (conversation.getNextEncryption(forceEncryption())) {
788 case Message.ENCRYPTION_NONE:
789 none.setChecked(true);
790 break;
791 case Message.ENCRYPTION_OTR:
792 otr.setChecked(true);
793 break;
794 case Message.ENCRYPTION_PGP:
795 pgp.setChecked(true);
796 break;
797 case Message.ENCRYPTION_AXOLOTL:
798 popup.getMenu().findItem(R.id.encryption_choice_axolotl)
799 .setChecked(true);
800 break;
801 default:
802 none.setChecked(true);
803 break;
804 }
805 popup.show();
806 }
807 }
808
809 protected void muteConversationDialog(final Conversation conversation) {
810 AlertDialog.Builder builder = new AlertDialog.Builder(this);
811 builder.setTitle(R.string.disable_notifications);
812 final int[] durations = getResources().getIntArray(
813 R.array.mute_options_durations);
814 builder.setItems(R.array.mute_options_descriptions,
815 new OnClickListener() {
816
817 @Override
818 public void onClick(final DialogInterface dialog, final int which) {
819 final long till;
820 if (durations[which] == -1) {
821 till = Long.MAX_VALUE;
822 } else {
823 till = System.currentTimeMillis() + (durations[which] * 1000);
824 }
825 conversation.setMutedTill(till);
826 ConversationActivity.this.xmppConnectionService.databaseBackend
827 .updateConversation(conversation);
828 updateConversationList();
829 ConversationActivity.this.mConversationFragment.updateMessages();
830 invalidateOptionsMenu();
831 }
832 });
833 builder.create().show();
834 }
835
836 public void unmuteConversation(final Conversation conversation) {
837 conversation.setMutedTill(0);
838 this.xmppConnectionService.databaseBackend.updateConversation(conversation);
839 updateConversationList();
840 ConversationActivity.this.mConversationFragment.updateMessages();
841 invalidateOptionsMenu();
842 }
843
844 @Override
845 public void onBackPressed() {
846 if (!isConversationsOverviewVisable()) {
847 showConversationsOverview();
848 } else {
849 moveTaskToBack(true);
850 }
851 }
852
853 @Override
854 protected void onNewIntent(final Intent intent) {
855 if (xmppConnectionServiceBound) {
856 if (intent != null && VIEW_CONVERSATION.equals(intent.getType())) {
857 handleViewConversationIntent(intent);
858 }
859 } else {
860 setIntent(intent);
861 }
862 }
863
864 @Override
865 public void onStart() {
866 super.onStart();
867 this.mRedirected = false;
868 if (this.xmppConnectionServiceBound) {
869 this.onBackendConnected();
870 }
871 if (conversationList.size() >= 1) {
872 this.onConversationUpdate();
873 }
874 }
875
876 @Override
877 public void onPause() {
878 listView.discardUndo();
879 super.onPause();
880 this.mActivityPaused = true;
881 if (this.xmppConnectionServiceBound) {
882 this.xmppConnectionService.getNotificationService().setIsInForeground(false);
883 }
884 }
885
886 @Override
887 public void onResume() {
888 super.onResume();
889 final int theme = findTheme();
890 final boolean usingEnterKey = usingEnterKey();
891 if (this.mTheme != theme || usingEnterKey != mUsingEnterKey) {
892 recreate();
893 }
894 this.mActivityPaused = false;
895 if (this.xmppConnectionServiceBound) {
896 this.xmppConnectionService.getNotificationService().setIsInForeground(true);
897 }
898
899 if (!isConversationsOverviewVisable() || !isConversationsOverviewHideable()) {
900 sendReadMarkerIfNecessary(getSelectedConversation());
901 }
902
903 }
904
905 @Override
906 public void onSaveInstanceState(final Bundle savedInstanceState) {
907 Conversation conversation = getSelectedConversation();
908 if (conversation != null) {
909 savedInstanceState.putString(STATE_OPEN_CONVERSATION,
910 conversation.getUuid());
911 }
912 savedInstanceState.putBoolean(STATE_PANEL_OPEN,
913 isConversationsOverviewVisable());
914 if (this.mPendingImageUris.size() >= 1) {
915 savedInstanceState.putString(STATE_PENDING_URI, this.mPendingImageUris.get(0).toString());
916 }
917 super.onSaveInstanceState(savedInstanceState);
918 }
919
920 @Override
921 void onBackendConnected() {
922 this.xmppConnectionService.getNotificationService().setIsInForeground(true);
923 updateConversationList();
924
925 if (mPendingConferenceInvite != null) {
926 mPendingConferenceInvite.execute(this);
927 mPendingConferenceInvite = null;
928 }
929
930 if (xmppConnectionService.getAccounts().size() == 0) {
931 if (!mRedirected) {
932 this.mRedirected = true;
933 startActivity(new Intent(this, EditAccountActivity.class));
934 finish();
935 }
936 } else if (conversationList.size() <= 0) {
937 if (!mRedirected) {
938 this.mRedirected = true;
939 Intent intent = new Intent(this, StartConversationActivity.class);
940 intent.putExtra("init",true);
941 startActivity(intent);
942 finish();
943 }
944 } else if (getIntent() != null && VIEW_CONVERSATION.equals(getIntent().getType())) {
945 handleViewConversationIntent(getIntent());
946 } else if (selectConversationByUuid(mOpenConverstaion)) {
947 if (mPanelOpen) {
948 showConversationsOverview();
949 } else {
950 if (isConversationsOverviewHideable()) {
951 openConversation();
952 }
953 }
954 this.mConversationFragment.reInit(getSelectedConversation());
955 mOpenConverstaion = null;
956 } else if (getSelectedConversation() == null) {
957 showConversationsOverview();
958 mPendingImageUris.clear();
959 mPendingFileUris.clear();
960 mPendingGeoUri = null;
961 setSelectedConversation(conversationList.get(0));
962 this.mConversationFragment.reInit(getSelectedConversation());
963 }
964
965 for(Iterator<Uri> i = mPendingImageUris.iterator(); i.hasNext(); i.remove()) {
966 attachImageToConversation(getSelectedConversation(),i.next());
967 }
968
969 for(Iterator<Uri> i = mPendingFileUris.iterator(); i.hasNext(); i.remove()) {
970 attachFileToConversation(getSelectedConversation(),i.next());
971 }
972
973 if (mPendingGeoUri != null) {
974 attachLocationToConversation(getSelectedConversation(), mPendingGeoUri);
975 mPendingGeoUri = null;
976 }
977 ExceptionHelper.checkForCrash(this, this.xmppConnectionService);
978 setIntent(new Intent());
979 }
980
981 private void handleViewConversationIntent(final Intent intent) {
982 final String uuid = intent.getStringExtra(CONVERSATION);
983 final String downloadUuid = intent.getStringExtra(MESSAGE);
984 final String text = intent.getStringExtra(TEXT);
985 final String nick = intent.getStringExtra(NICK);
986 if (selectConversationByUuid(uuid)) {
987 this.mConversationFragment.reInit(getSelectedConversation());
988 if (nick != null) {
989 this.mConversationFragment.highlightInConference(nick);
990 } else {
991 this.mConversationFragment.appendText(text);
992 }
993 hideConversationsOverview();
994 openConversation();
995 if (mContentView instanceof SlidingPaneLayout) {
996 updateActionBarTitle(true); //fixes bug where slp isn't properly closed yet
997 }
998 if (downloadUuid != null) {
999 final Message message = mSelectedConversation.findMessageWithFileAndUuid(downloadUuid);
1000 if (message != null) {
1001 mConversationFragment.messageListAdapter.startDownloadable(message);
1002 }
1003 }
1004 }
1005 }
1006
1007 private boolean selectConversationByUuid(String uuid) {
1008 if (uuid == null) {
1009 return false;
1010 }
1011 for (Conversation aConversationList : conversationList) {
1012 if (aConversationList.getUuid().equals(uuid)) {
1013 setSelectedConversation(aConversationList);
1014 return true;
1015 }
1016 }
1017 return false;
1018 }
1019
1020 @Override
1021 protected void unregisterListeners() {
1022 super.unregisterListeners();
1023 xmppConnectionService.getNotificationService().setOpenConversation(null);
1024 }
1025
1026 @SuppressLint("NewApi")
1027 private static List<Uri> extractUriFromIntent(final Intent intent) {
1028 List<Uri> uris = new ArrayList<>();
1029 Uri uri = intent.getData();
1030 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 && uri == null) {
1031 ClipData clipData = intent.getClipData();
1032 for(int i = 0; i < clipData.getItemCount(); ++i) {
1033 uris.add(clipData.getItemAt(i).getUri());
1034 }
1035 } else {
1036 uris.add(uri);
1037 }
1038 return uris;
1039 }
1040
1041 @Override
1042 protected void onActivityResult(int requestCode, int resultCode,
1043 final Intent data) {
1044 super.onActivityResult(requestCode, resultCode, data);
1045 if (resultCode == RESULT_OK) {
1046 if (requestCode == REQUEST_DECRYPT_PGP) {
1047 mConversationFragment.hideSnackbar();
1048 mConversationFragment.updateMessages();
1049 } else if (requestCode == ATTACHMENT_CHOICE_CHOOSE_IMAGE) {
1050 mPendingImageUris.clear();
1051 mPendingImageUris.addAll(extractUriFromIntent(data));
1052 if (xmppConnectionServiceBound) {
1053 for(Iterator<Uri> i = mPendingImageUris.iterator(); i.hasNext(); i.remove()) {
1054 attachImageToConversation(getSelectedConversation(),i.next());
1055 }
1056 }
1057 } else if (requestCode == ATTACHMENT_CHOICE_CHOOSE_FILE || requestCode == ATTACHMENT_CHOICE_RECORD_VOICE) {
1058 mPendingFileUris.clear();
1059 mPendingFileUris.addAll(extractUriFromIntent(data));
1060 if (xmppConnectionServiceBound) {
1061 for(Iterator<Uri> i = mPendingFileUris.iterator(); i.hasNext(); i.remove()) {
1062 attachFileToConversation(getSelectedConversation(), i.next());
1063 }
1064 }
1065 } else if (requestCode == ATTACHMENT_CHOICE_TAKE_PHOTO) {
1066 if (mPendingImageUris.size() == 1) {
1067 Uri uri = mPendingImageUris.get(0);
1068 if (xmppConnectionServiceBound) {
1069 attachImageToConversation(getSelectedConversation(), uri);
1070 mPendingImageUris.clear();
1071 }
1072 Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
1073 intent.setData(uri);
1074 sendBroadcast(intent);
1075 } else {
1076 mPendingImageUris.clear();
1077 }
1078 } else if (requestCode == ATTACHMENT_CHOICE_LOCATION) {
1079 double latitude = data.getDoubleExtra("latitude",0);
1080 double longitude = data.getDoubleExtra("longitude",0);
1081 this.mPendingGeoUri = Uri.parse("geo:"+String.valueOf(latitude)+","+String.valueOf(longitude));
1082 if (xmppConnectionServiceBound) {
1083 attachLocationToConversation(getSelectedConversation(), mPendingGeoUri);
1084 this.mPendingGeoUri = null;
1085 }
1086 }
1087 } else {
1088 mPendingImageUris.clear();
1089 mPendingFileUris.clear();
1090 }
1091 }
1092
1093 private void attachLocationToConversation(Conversation conversation, Uri uri) {
1094 if (conversation == null) {
1095 return;
1096 }
1097 xmppConnectionService.attachLocationToConversation(conversation,uri, new UiCallback<Message>() {
1098
1099 @Override
1100 public void success(Message message) {
1101 xmppConnectionService.sendMessage(message);
1102 }
1103
1104 @Override
1105 public void error(int errorCode, Message object) {
1106
1107 }
1108
1109 @Override
1110 public void userInputRequried(PendingIntent pi, Message object) {
1111
1112 }
1113 });
1114 }
1115
1116 private void attachFileToConversation(Conversation conversation, Uri uri) {
1117 if (conversation == null) {
1118 return;
1119 }
1120 prepareFileToast = Toast.makeText(getApplicationContext(),getText(R.string.preparing_file), Toast.LENGTH_LONG);
1121 prepareFileToast.show();
1122 xmppConnectionService.attachFileToConversation(conversation,uri, new UiCallback<Message>() {
1123 @Override
1124 public void success(Message message) {
1125 hidePrepareFileToast();
1126 xmppConnectionService.sendMessage(message);
1127 }
1128
1129 @Override
1130 public void error(int errorCode, Message message) {
1131 displayErrorDialog(errorCode);
1132 }
1133
1134 @Override
1135 public void userInputRequried(PendingIntent pi, Message message) {
1136
1137 }
1138 });
1139 }
1140
1141 private void attachImageToConversation(Conversation conversation, Uri uri) {
1142 if (conversation == null) {
1143 return;
1144 }
1145 prepareFileToast = Toast.makeText(getApplicationContext(),getText(R.string.preparing_image), Toast.LENGTH_LONG);
1146 prepareFileToast.show();
1147 xmppConnectionService.attachImageToConversation(conversation, uri,
1148 new UiCallback<Message>() {
1149
1150 @Override
1151 public void userInputRequried(PendingIntent pi,
1152 Message object) {
1153 hidePrepareFileToast();
1154 }
1155
1156 @Override
1157 public void success(Message message) {
1158 xmppConnectionService.sendMessage(message);
1159 }
1160
1161 @Override
1162 public void error(int error, Message message) {
1163 hidePrepareFileToast();
1164 displayErrorDialog(error);
1165 }
1166 });
1167 }
1168
1169 private void hidePrepareFileToast() {
1170 if (prepareFileToast != null) {
1171 runOnUiThread(new Runnable() {
1172
1173 @Override
1174 public void run() {
1175 prepareFileToast.cancel();
1176 }
1177 });
1178 }
1179 }
1180
1181 public void updateConversationList() {
1182 xmppConnectionService
1183 .populateWithOrderedConversations(conversationList);
1184 if (swipedConversation != null) {
1185 if (swipedConversation.isRead()) {
1186 conversationList.remove(swipedConversation);
1187 } else {
1188 listView.discardUndo();
1189 }
1190 }
1191 listAdapter.notifyDataSetChanged();
1192 }
1193
1194 public void runIntent(PendingIntent pi, int requestCode) {
1195 try {
1196 this.startIntentSenderForResult(pi.getIntentSender(), requestCode,
1197 null, 0, 0, 0);
1198 } catch (final SendIntentException ignored) {
1199 }
1200 }
1201
1202 public void encryptTextMessage(Message message) {
1203 xmppConnectionService.getPgpEngine().encrypt(message,
1204 new UiCallback<Message>() {
1205
1206 @Override
1207 public void userInputRequried(PendingIntent pi,
1208 Message message) {
1209 ConversationActivity.this.runIntent(pi,
1210 ConversationActivity.REQUEST_SEND_MESSAGE);
1211 }
1212
1213 @Override
1214 public void success(Message message) {
1215 message.setEncryption(Message.ENCRYPTION_DECRYPTED);
1216 xmppConnectionService.sendMessage(message);
1217 }
1218
1219 @Override
1220 public void error(int error, Message message) {
1221
1222 }
1223 });
1224 }
1225
1226 public boolean forceEncryption() {
1227 return getPreferences().getBoolean("force_encryption", false);
1228 }
1229
1230 public boolean useSendButtonToIndicateStatus() {
1231 return getPreferences().getBoolean("send_button_status", false);
1232 }
1233
1234 public boolean indicateReceived() {
1235 return getPreferences().getBoolean("indicate_received", false);
1236 }
1237
1238 @Override
1239 protected void refreshUiReal() {
1240 updateConversationList();
1241 if (xmppConnectionService != null && xmppConnectionService.getAccounts().size() == 0) {
1242 if (!mRedirected) {
1243 this.mRedirected = true;
1244 startActivity(new Intent(this, EditAccountActivity.class));
1245 finish();
1246 }
1247 } else if (conversationList.size() == 0) {
1248 if (!mRedirected) {
1249 this.mRedirected = true;
1250 Intent intent = new Intent(this, StartConversationActivity.class);
1251 intent.putExtra("init",true);
1252 startActivity(intent);
1253 finish();
1254 }
1255 } else {
1256 ConversationActivity.this.mConversationFragment.updateMessages();
1257 updateActionBarTitle();
1258 }
1259 }
1260
1261 @Override
1262 public void onAccountUpdate() {
1263 this.refreshUi();
1264 }
1265
1266 @Override
1267 public void onConversationUpdate() {
1268 this.refreshUi();
1269 }
1270
1271 @Override
1272 public void onRosterUpdate() {
1273 this.refreshUi();
1274 }
1275
1276 @Override
1277 public void OnUpdateBlocklist(Status status) {
1278 this.refreshUi();
1279 runOnUiThread(new Runnable() {
1280 @Override
1281 public void run() {
1282 invalidateOptionsMenu();
1283 }
1284 });
1285 }
1286
1287 public void unblockConversation(final Blockable conversation) {
1288 xmppConnectionService.sendUnblockRequest(conversation);
1289 }
1290
1291 public boolean enterIsSend() {
1292 return getPreferences().getBoolean("enter_is_send",false);
1293 }
1294
1295 @Override
1296 public void onShowErrorToast(final int resId) {
1297 runOnUiThread(new Runnable() {
1298 @Override
1299 public void run() {
1300 Toast.makeText(ConversationActivity.this,resId,Toast.LENGTH_SHORT).show();
1301 }
1302 });
1303 }
1304}