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.entities.Account;
41import eu.siacs.conversations.entities.Blockable;
42import eu.siacs.conversations.entities.Contact;
43import eu.siacs.conversations.entities.Conversation;
44import eu.siacs.conversations.entities.Message;
45import eu.siacs.conversations.services.XmppConnectionService;
46import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
47import eu.siacs.conversations.services.XmppConnectionService.OnConversationUpdate;
48import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate;
49import eu.siacs.conversations.ui.adapter.ConversationAdapter;
50import eu.siacs.conversations.utils.ExceptionHelper;
51import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
52
53public class ConversationActivity extends XmppActivity
54 implements OnAccountUpdate, OnConversationUpdate, OnRosterUpdate, OnUpdateBlocklist, XmppConnectionService.OnShowErrorToast {
55
56 public static final String ACTION_DOWNLOAD = "eu.siacs.conversations.action.DOWNLOAD";
57
58 public static final String VIEW_CONVERSATION = "viewConversation";
59 public static final String CONVERSATION = "conversationUuid";
60 public static final String MESSAGE = "messageUuid";
61 public static final String TEXT = "text";
62 public static final String NICK = "nick";
63
64 public static final int REQUEST_SEND_MESSAGE = 0x0201;
65 public static final int REQUEST_DECRYPT_PGP = 0x0202;
66 public static final int REQUEST_ENCRYPT_MESSAGE = 0x0207;
67 public static final int ATTACHMENT_CHOICE_CHOOSE_IMAGE = 0x0301;
68 public static final int ATTACHMENT_CHOICE_TAKE_PHOTO = 0x0302;
69 public static final int ATTACHMENT_CHOICE_CHOOSE_FILE = 0x0303;
70 public static final int ATTACHMENT_CHOICE_RECORD_VOICE = 0x0304;
71 public static final int ATTACHMENT_CHOICE_LOCATION = 0x0305;
72 private static final String STATE_OPEN_CONVERSATION = "state_open_conversation";
73 private static final String STATE_PANEL_OPEN = "state_panel_open";
74 private static final String STATE_PENDING_URI = "state_pending_uri";
75
76 private String mOpenConverstaion = null;
77 private boolean mPanelOpen = true;
78 final private List<Uri> mPendingImageUris = new ArrayList<>();
79 final private List<Uri> mPendingFileUris = new ArrayList<>();
80 private Uri mPendingGeoUri = null;
81
82 private View mContentView;
83
84 private List<Conversation> conversationList = new ArrayList<>();
85 private Conversation swipedConversation = null;
86 private Conversation mSelectedConversation = null;
87 private EnhancedListView listView;
88 private ConversationFragment mConversationFragment;
89
90 private ArrayAdapter<Conversation> listAdapter;
91
92 private Toast prepareFileToast;
93
94 private boolean mActivityPaused = false;
95 private boolean mRedirected = true;
96
97 public Conversation getSelectedConversation() {
98 return this.mSelectedConversation;
99 }
100
101 public void setSelectedConversation(Conversation conversation) {
102 this.mSelectedConversation = conversation;
103 }
104
105 public void showConversationsOverview() {
106 if (mContentView instanceof SlidingPaneLayout) {
107 SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
108 mSlidingPaneLayout.openPane();
109 }
110 }
111
112 @Override
113 protected String getShareableUri() {
114 Conversation conversation = getSelectedConversation();
115 if (conversation != null) {
116 return conversation.getAccount().getShareableUri();
117 } else {
118 return "";
119 }
120 }
121
122 public void hideConversationsOverview() {
123 if (mContentView instanceof SlidingPaneLayout) {
124 SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
125 mSlidingPaneLayout.closePane();
126 }
127 }
128
129 public boolean isConversationsOverviewHideable() {
130 if (mContentView instanceof SlidingPaneLayout) {
131 SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
132 return mSlidingPaneLayout.isSlideable();
133 } else {
134 return false;
135 }
136 }
137
138 public boolean isConversationsOverviewVisable() {
139 if (mContentView instanceof SlidingPaneLayout) {
140 SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
141 return mSlidingPaneLayout.isOpen();
142 } else {
143 return true;
144 }
145 }
146
147 @Override
148 protected void onCreate(final Bundle savedInstanceState) {
149 super.onCreate(savedInstanceState);
150 if (savedInstanceState != null) {
151 mOpenConverstaion = savedInstanceState.getString(STATE_OPEN_CONVERSATION, null);
152 mPanelOpen = savedInstanceState.getBoolean(STATE_PANEL_OPEN, true);
153 String pending = savedInstanceState.getString(STATE_PENDING_URI, null);
154 if (pending != null) {
155 mPendingImageUris.clear();
156 mPendingImageUris.add(Uri.parse(pending));
157 }
158 }
159
160 setContentView(R.layout.fragment_conversations_overview);
161
162 this.mConversationFragment = new ConversationFragment();
163 FragmentTransaction transaction = getFragmentManager().beginTransaction();
164 transaction.replace(R.id.selected_conversation, this.mConversationFragment, "conversation");
165 transaction.commit();
166
167 listView = (EnhancedListView) findViewById(R.id.list);
168 this.listAdapter = new ConversationAdapter(this, conversationList);
169 listView.setAdapter(this.listAdapter);
170
171 if (getActionBar() != null) {
172 getActionBar().setDisplayHomeAsUpEnabled(false);
173 getActionBar().setHomeButtonEnabled(false);
174 }
175
176 listView.setOnItemClickListener(new OnItemClickListener() {
177
178 @Override
179 public void onItemClick(AdapterView<?> arg0, View clickedView,
180 int position, long arg3) {
181 if (getSelectedConversation() != conversationList.get(position)) {
182 setSelectedConversation(conversationList.get(position));
183 ConversationActivity.this.mConversationFragment.reInit(getSelectedConversation());
184 }
185 hideConversationsOverview();
186 openConversation();
187 }
188 });
189
190 listView.setDismissCallback(new EnhancedListView.OnDismissCallback() {
191
192 @Override
193 public EnhancedListView.Undoable onDismiss(final EnhancedListView enhancedListView, final int position) {
194
195 final int index = listView.getFirstVisiblePosition();
196 View v = listView.getChildAt(0);
197 final int top = (v == null) ? 0 : (v.getTop() - listView.getPaddingTop());
198
199 swipedConversation = listAdapter.getItem(position);
200 listAdapter.remove(swipedConversation);
201 swipedConversation.markRead();
202 xmppConnectionService.getNotificationService().clear(swipedConversation);
203
204 final boolean formerlySelected = (getSelectedConversation() == swipedConversation);
205 if (position == 0 && listAdapter.getCount() == 0) {
206 endConversation(swipedConversation, false, true);
207 return null;
208 } else if (formerlySelected) {
209 setSelectedConversation(listAdapter.getItem(0));
210 ConversationActivity.this.mConversationFragment
211 .reInit(getSelectedConversation());
212 }
213
214 return new EnhancedListView.Undoable() {
215
216 @Override
217 public void undo() {
218 listAdapter.insert(swipedConversation, position);
219 if (formerlySelected) {
220 setSelectedConversation(swipedConversation);
221 ConversationActivity.this.mConversationFragment
222 .reInit(getSelectedConversation());
223 }
224 swipedConversation = null;
225 listView.setSelectionFromTop(index + (listView.getChildCount() < position ? 1 : 0), top);
226 }
227
228 @Override
229 public void discard() {
230 if (!swipedConversation.isRead()
231 && swipedConversation.getMode() == Conversation.MODE_SINGLE) {
232 swipedConversation = null;
233 return;
234 }
235 endConversation(swipedConversation, false, false);
236 swipedConversation = null;
237 }
238
239 @Override
240 public String getTitle() {
241 if (swipedConversation.getMode() == Conversation.MODE_MULTI) {
242 return getResources().getString(R.string.title_undo_swipe_out_muc);
243 } else {
244 return getResources().getString(R.string.title_undo_swipe_out_conversation);
245 }
246 }
247 };
248 }
249 });
250 listView.enableSwipeToDismiss();
251 listView.setSwipingLayout(R.id.swipeable_item);
252 listView.setUndoStyle(EnhancedListView.UndoStyle.SINGLE_POPUP);
253 listView.setUndoHideDelay(5000);
254 listView.setRequireTouchBeforeDismiss(false);
255
256 mContentView = findViewById(R.id.content_view_spl);
257 if (mContentView == null) {
258 mContentView = findViewById(R.id.content_view_ll);
259 }
260 if (mContentView instanceof SlidingPaneLayout) {
261 SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
262 mSlidingPaneLayout.setParallaxDistance(150);
263 mSlidingPaneLayout
264 .setShadowResource(R.drawable.es_slidingpane_shadow);
265 mSlidingPaneLayout.setSliderFadeColor(0);
266 mSlidingPaneLayout.setPanelSlideListener(new PanelSlideListener() {
267
268 @Override
269 public void onPanelOpened(View arg0) {
270 updateActionBarTitle();
271 invalidateOptionsMenu();
272 hideKeyboard();
273 if (xmppConnectionServiceBound) {
274 xmppConnectionService.getNotificationService()
275 .setOpenConversation(null);
276 }
277 closeContextMenu();
278 }
279
280 @Override
281 public void onPanelClosed(View arg0) {
282 listView.discardUndo();
283 openConversation();
284 }
285
286 @Override
287 public void onPanelSlide(View arg0, float arg1) {
288 // TODO Auto-generated method stub
289
290 }
291 });
292 }
293 }
294
295 @Override
296 public void switchToConversation(Conversation conversation) {
297 setSelectedConversation(conversation);
298 runOnUiThread(new Runnable() {
299 @Override
300 public void run() {
301 ConversationActivity.this.mConversationFragment.reInit(getSelectedConversation());
302 openConversation();
303 }
304 });
305 }
306
307 private void updateActionBarTitle() {
308 updateActionBarTitle(isConversationsOverviewHideable() && !isConversationsOverviewVisable());
309 }
310
311 private void updateActionBarTitle(boolean titleShouldBeName) {
312 final ActionBar ab = getActionBar();
313 final Conversation conversation = getSelectedConversation();
314 if (ab != null) {
315 if (titleShouldBeName && conversation != null) {
316 ab.setDisplayHomeAsUpEnabled(true);
317 ab.setHomeButtonEnabled(true);
318 if (conversation.getMode() == Conversation.MODE_SINGLE || useSubjectToIdentifyConference()) {
319 ab.setTitle(conversation.getName());
320 } else {
321 ab.setTitle(conversation.getJid().toBareJid().toString());
322 }
323 } else {
324 ab.setDisplayHomeAsUpEnabled(false);
325 ab.setHomeButtonEnabled(false);
326 ab.setTitle(R.string.app_name);
327 }
328 }
329 }
330
331 private void openConversation() {
332 this.updateActionBarTitle();
333 this.invalidateOptionsMenu();
334 if (xmppConnectionServiceBound) {
335 final Conversation conversation = getSelectedConversation();
336 xmppConnectionService.getNotificationService().setOpenConversation(conversation);
337 sendReadMarkerIfNecessary(conversation);
338 }
339 listAdapter.notifyDataSetChanged();
340 }
341
342 public void sendReadMarkerIfNecessary(final Conversation conversation) {
343 if (!mActivityPaused && conversation != null) {
344 if (!conversation.isRead()) {
345 xmppConnectionService.sendReadMarker(conversation);
346 } else {
347 xmppConnectionService.markRead(conversation);
348 }
349 }
350 }
351
352 @Override
353 public boolean onCreateOptionsMenu(Menu menu) {
354 getMenuInflater().inflate(R.menu.conversations, menu);
355 final MenuItem menuSecure = menu.findItem(R.id.action_security);
356 final MenuItem menuArchive = menu.findItem(R.id.action_archive);
357 final MenuItem menuMucDetails = menu.findItem(R.id.action_muc_details);
358 final MenuItem menuContactDetails = menu.findItem(R.id.action_contact_details);
359 final MenuItem menuAttach = menu.findItem(R.id.action_attach_file);
360 final MenuItem menuClearHistory = menu.findItem(R.id.action_clear_history);
361 final MenuItem menuAdd = menu.findItem(R.id.action_add);
362 final MenuItem menuInviteContact = menu.findItem(R.id.action_invite);
363 final MenuItem menuMute = menu.findItem(R.id.action_mute);
364 final MenuItem menuUnmute = menu.findItem(R.id.action_unmute);
365
366 if (isConversationsOverviewVisable() && isConversationsOverviewHideable()) {
367 menuArchive.setVisible(false);
368 menuMucDetails.setVisible(false);
369 menuContactDetails.setVisible(false);
370 menuSecure.setVisible(false);
371 menuInviteContact.setVisible(false);
372 menuAttach.setVisible(false);
373 menuClearHistory.setVisible(false);
374 menuMute.setVisible(false);
375 menuUnmute.setVisible(false);
376 } else {
377 menuAdd.setVisible(!isConversationsOverviewHideable());
378 if (this.getSelectedConversation() != null) {
379 if (this.getSelectedConversation().getNextEncryption(forceEncryption()) != Message.ENCRYPTION_NONE) {
380 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
381 menuSecure.setIcon(R.drawable.ic_lock_white_24dp);
382 } else {
383 menuSecure.setIcon(R.drawable.ic_action_secure);
384 }
385 }
386 if (this.getSelectedConversation().getMode() == Conversation.MODE_MULTI) {
387 menuContactDetails.setVisible(false);
388 menuAttach.setVisible(getSelectedConversation().getAccount().httpUploadAvailable());
389 menuInviteContact.setVisible(getSelectedConversation().getMucOptions().canInvite());
390 } else {
391 menuMucDetails.setVisible(false);
392 }
393 if (this.getSelectedConversation().isMuted()) {
394 menuMute.setVisible(false);
395 } else {
396 menuUnmute.setVisible(false);
397 }
398 }
399 }
400 return true;
401 }
402
403 private void selectPresenceToAttachFile(final int attachmentChoice, final int encryption) {
404 final Conversation conversation = getSelectedConversation();
405 final Account account = conversation.getAccount();
406 final OnPresenceSelected callback = new OnPresenceSelected() {
407
408 @Override
409 public void onPresenceSelected() {
410 Intent intent = new Intent();
411 boolean chooser = false;
412 String fallbackPackageId = null;
413 switch (attachmentChoice) {
414 case ATTACHMENT_CHOICE_CHOOSE_IMAGE:
415 intent.setAction(Intent.ACTION_GET_CONTENT);
416 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
417 intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,true);
418 }
419 intent.setType("image/*");
420 chooser = true;
421 break;
422 case ATTACHMENT_CHOICE_TAKE_PHOTO:
423 Uri uri = xmppConnectionService.getFileBackend().getTakePhotoUri();
424 intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
425 intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
426 mPendingImageUris.clear();
427 mPendingImageUris.add(uri);
428 break;
429 case ATTACHMENT_CHOICE_CHOOSE_FILE:
430 chooser = true;
431 intent.setType("*/*");
432 intent.addCategory(Intent.CATEGORY_OPENABLE);
433 intent.setAction(Intent.ACTION_GET_CONTENT);
434 break;
435 case ATTACHMENT_CHOICE_RECORD_VOICE:
436 intent.setAction(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
437 fallbackPackageId = "eu.siacs.conversations.voicerecorder";
438 break;
439 case ATTACHMENT_CHOICE_LOCATION:
440 intent.setAction("eu.siacs.conversations.location.request");
441 fallbackPackageId = "eu.siacs.conversations.sharelocation";
442 break;
443 }
444 if (intent.resolveActivity(getPackageManager()) != null) {
445 if (chooser) {
446 startActivityForResult(
447 Intent.createChooser(intent, getString(R.string.perform_action_with)),
448 attachmentChoice);
449 } else {
450 startActivityForResult(intent, attachmentChoice);
451 }
452 } else if (fallbackPackageId != null) {
453 startActivity(getInstallApkIntent(fallbackPackageId));
454 }
455 }
456 };
457 if ((account.httpUploadAvailable() || attachmentChoice == ATTACHMENT_CHOICE_LOCATION) && encryption != Message.ENCRYPTION_OTR) {
458 conversation.setNextCounterpart(null);
459 callback.onPresenceSelected();
460 } else {
461 selectPresence(conversation,callback);
462 }
463 }
464
465 private Intent getInstallApkIntent(final String packageId) {
466 Intent intent = new Intent(Intent.ACTION_VIEW);
467 intent.setData(Uri.parse("market://details?id="+packageId));
468 if (intent.resolveActivity(getPackageManager()) != null) {
469 return intent;
470 } else {
471 intent.setData(Uri.parse("http://play.google.com/store/apps/details?id="+packageId));
472 return intent;
473 }
474 }
475
476 public void attachFile(final int attachmentChoice) {
477 switch (attachmentChoice) {
478 case ATTACHMENT_CHOICE_LOCATION:
479 getPreferences().edit().putString("recently_used_quick_action","location").apply();
480 break;
481 case ATTACHMENT_CHOICE_RECORD_VOICE:
482 getPreferences().edit().putString("recently_used_quick_action","voice").apply();
483 break;
484 case ATTACHMENT_CHOICE_TAKE_PHOTO:
485 getPreferences().edit().putString("recently_used_quick_action","photo").apply();
486 break;
487 case ATTACHMENT_CHOICE_CHOOSE_IMAGE:
488 getPreferences().edit().putString("recently_used_quick_action","picture").apply();
489 break;
490 }
491 final Conversation conversation = getSelectedConversation();
492 final int encryption = conversation.getNextEncryption(forceEncryption());
493 if (encryption == Message.ENCRYPTION_PGP) {
494 if (hasPgp()) {
495 if (conversation.getContact().getPgpKeyId() != 0) {
496 xmppConnectionService.getPgpEngine().hasKey(
497 conversation.getContact(),
498 new UiCallback<Contact>() {
499
500 @Override
501 public void userInputRequried(PendingIntent pi,
502 Contact contact) {
503 ConversationActivity.this.runIntent(pi,attachmentChoice);
504 }
505
506 @Override
507 public void success(Contact contact) {
508 selectPresenceToAttachFile(attachmentChoice,encryption);
509 }
510
511 @Override
512 public void error(int error, Contact contact) {
513 displayErrorDialog(error);
514 }
515 });
516 } else {
517 final ConversationFragment fragment = (ConversationFragment) getFragmentManager()
518 .findFragmentByTag("conversation");
519 if (fragment != null) {
520 fragment.showNoPGPKeyDialog(false,
521 new OnClickListener() {
522
523 @Override
524 public void onClick(DialogInterface dialog,
525 int which) {
526 conversation
527 .setNextEncryption(Message.ENCRYPTION_NONE);
528 xmppConnectionService.databaseBackend
529 .updateConversation(conversation);
530 selectPresenceToAttachFile(attachmentChoice,Message.ENCRYPTION_NONE);
531 }
532 });
533 }
534 }
535 } else {
536 showInstallPgpDialog();
537 }
538 } else {
539 selectPresenceToAttachFile(attachmentChoice,encryption);
540 }
541 }
542
543 @Override
544 public boolean onOptionsItemSelected(final MenuItem item) {
545 if (item.getItemId() == android.R.id.home) {
546 showConversationsOverview();
547 return true;
548 } else if (item.getItemId() == R.id.action_add) {
549 startActivity(new Intent(this, StartConversationActivity.class));
550 return true;
551 } else if (getSelectedConversation() != null) {
552 switch (item.getItemId()) {
553 case R.id.action_attach_file:
554 attachFileDialog();
555 break;
556 case R.id.action_archive:
557 this.endConversation(getSelectedConversation());
558 break;
559 case R.id.action_contact_details:
560 switchToContactDetails(getSelectedConversation().getContact());
561 break;
562 case R.id.action_muc_details:
563 Intent intent = new Intent(this,
564 ConferenceDetailsActivity.class);
565 intent.setAction(ConferenceDetailsActivity.ACTION_VIEW_MUC);
566 intent.putExtra("uuid", getSelectedConversation().getUuid());
567 startActivity(intent);
568 break;
569 case R.id.action_invite:
570 inviteToConversation(getSelectedConversation());
571 break;
572 case R.id.action_security:
573 selectEncryptionDialog(getSelectedConversation());
574 break;
575 case R.id.action_clear_history:
576 clearHistoryDialog(getSelectedConversation());
577 break;
578 case R.id.action_mute:
579 muteConversationDialog(getSelectedConversation());
580 break;
581 case R.id.action_unmute:
582 unmuteConversation(getSelectedConversation());
583 break;
584 case R.id.action_block:
585 BlockContactDialog.show(this, xmppConnectionService, getSelectedConversation());
586 break;
587 case R.id.action_unblock:
588 BlockContactDialog.show(this, xmppConnectionService, getSelectedConversation());
589 break;
590 default:
591 break;
592 }
593 return super.onOptionsItemSelected(item);
594 } else {
595 return super.onOptionsItemSelected(item);
596 }
597 }
598
599 public void endConversation(Conversation conversation) {
600 endConversation(conversation, true, true);
601 }
602
603 public void endConversation(Conversation conversation, boolean showOverview, boolean reinit) {
604 if (showOverview) {
605 showConversationsOverview();
606 }
607 xmppConnectionService.archiveConversation(conversation);
608 if (reinit) {
609 if (conversationList.size() > 0) {
610 setSelectedConversation(conversationList.get(0));
611 this.mConversationFragment.reInit(getSelectedConversation());
612 } else {
613 setSelectedConversation(null);
614 }
615 }
616 }
617
618 @SuppressLint("InflateParams")
619 protected void clearHistoryDialog(final Conversation conversation) {
620 AlertDialog.Builder builder = new AlertDialog.Builder(this);
621 builder.setTitle(getString(R.string.clear_conversation_history));
622 View dialogView = getLayoutInflater().inflate(
623 R.layout.dialog_clear_history, null);
624 final CheckBox endConversationCheckBox = (CheckBox) dialogView
625 .findViewById(R.id.end_conversation_checkbox);
626 builder.setView(dialogView);
627 builder.setNegativeButton(getString(R.string.cancel), null);
628 builder.setPositiveButton(getString(R.string.delete_messages),
629 new OnClickListener() {
630
631 @Override
632 public void onClick(DialogInterface dialog, int which) {
633 ConversationActivity.this.xmppConnectionService.clearConversationHistory(conversation);
634 if (endConversationCheckBox.isChecked()) {
635 endConversation(conversation);
636 } else {
637 updateConversationList();
638 ConversationActivity.this.mConversationFragment.updateMessages();
639 }
640 }
641 });
642 builder.create().show();
643 }
644
645 protected void attachFileDialog() {
646 View menuAttachFile = findViewById(R.id.action_attach_file);
647 if (menuAttachFile == null) {
648 return;
649 }
650 PopupMenu attachFilePopup = new PopupMenu(this, menuAttachFile);
651 attachFilePopup.inflate(R.menu.attachment_choices);
652 if (new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION).resolveActivity(getPackageManager()) == null) {
653 attachFilePopup.getMenu().findItem(R.id.attach_record_voice).setVisible(false);
654 }
655 if (new Intent("eu.siacs.conversations.location.request").resolveActivity(getPackageManager()) == null) {
656 attachFilePopup.getMenu().findItem(R.id.attach_location).setVisible(false);
657 }
658 attachFilePopup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
659
660 @Override
661 public boolean onMenuItemClick(MenuItem item) {
662 switch (item.getItemId()) {
663 case R.id.attach_choose_picture:
664 attachFile(ATTACHMENT_CHOICE_CHOOSE_IMAGE);
665 break;
666 case R.id.attach_take_picture:
667 attachFile(ATTACHMENT_CHOICE_TAKE_PHOTO);
668 break;
669 case R.id.attach_choose_file:
670 attachFile(ATTACHMENT_CHOICE_CHOOSE_FILE);
671 break;
672 case R.id.attach_record_voice:
673 attachFile(ATTACHMENT_CHOICE_RECORD_VOICE);
674 break;
675 case R.id.attach_location:
676 attachFile(ATTACHMENT_CHOICE_LOCATION);
677 break;
678 }
679 return false;
680 }
681 });
682 attachFilePopup.show();
683 }
684
685 public void verifyOtrSessionDialog(final Conversation conversation, View view) {
686 if (!conversation.hasValidOtrSession() || conversation.getOtrSession().getSessionStatus() != SessionStatus.ENCRYPTED) {
687 Toast.makeText(this, R.string.otr_session_not_started, Toast.LENGTH_LONG).show();
688 return;
689 }
690 if (view == null) {
691 return;
692 }
693 PopupMenu popup = new PopupMenu(this, view);
694 popup.inflate(R.menu.verification_choices);
695 popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
696 @Override
697 public boolean onMenuItemClick(MenuItem menuItem) {
698 Intent intent = new Intent(ConversationActivity.this, VerifyOTRActivity.class);
699 intent.setAction(VerifyOTRActivity.ACTION_VERIFY_CONTACT);
700 intent.putExtra("contact", conversation.getContact().getJid().toBareJid().toString());
701 intent.putExtra("account", conversation.getAccount().getJid().toBareJid().toString());
702 switch (menuItem.getItemId()) {
703 case R.id.scan_fingerprint:
704 intent.putExtra("mode",VerifyOTRActivity.MODE_SCAN_FINGERPRINT);
705 break;
706 case R.id.ask_question:
707 intent.putExtra("mode",VerifyOTRActivity.MODE_ASK_QUESTION);
708 break;
709 case R.id.manual_verification:
710 intent.putExtra("mode",VerifyOTRActivity.MODE_MANUAL_VERIFICATION);
711 break;
712 }
713 startActivity(intent);
714 return true;
715 }
716 });
717 popup.show();
718 }
719
720 protected void selectEncryptionDialog(final Conversation conversation) {
721 View menuItemView = findViewById(R.id.action_security);
722 if (menuItemView == null) {
723 return;
724 }
725 PopupMenu popup = new PopupMenu(this, menuItemView);
726 final ConversationFragment fragment = (ConversationFragment) getFragmentManager()
727 .findFragmentByTag("conversation");
728 if (fragment != null) {
729 popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
730
731 @Override
732 public boolean onMenuItemClick(MenuItem item) {
733 switch (item.getItemId()) {
734 case R.id.encryption_choice_none:
735 conversation.setNextEncryption(Message.ENCRYPTION_NONE);
736 item.setChecked(true);
737 break;
738 case R.id.encryption_choice_otr:
739 conversation.setNextEncryption(Message.ENCRYPTION_OTR);
740 item.setChecked(true);
741 break;
742 case R.id.encryption_choice_pgp:
743 if (hasPgp()) {
744 if (conversation.getAccount().getKeys().has("pgp_signature")) {
745 conversation.setNextEncryption(Message.ENCRYPTION_PGP);
746 item.setChecked(true);
747 } else {
748 announcePgp(conversation.getAccount(),conversation);
749 }
750 } else {
751 showInstallPgpDialog();
752 }
753 break;
754 case R.id.encryption_choice_axolotl:
755 Log.d(Config.LOGTAG, "Trying to enable axolotl...");
756 if(conversation.getAccount().getAxolotlService().isContactAxolotlCapable(conversation.getContact())) {
757 Log.d(Config.LOGTAG, "Enabled axolotl for Contact " + conversation.getContact().getJid() );
758 conversation.setNextEncryption(Message.ENCRYPTION_AXOLOTL);
759 item.setChecked(true);
760 } else {
761 Log.d(Config.LOGTAG, "Contact " + conversation.getContact().getJid() + " not axolotl capable!");
762 showAxolotlNoSessionsDialog();
763 }
764 break;
765 default:
766 conversation.setNextEncryption(Message.ENCRYPTION_NONE);
767 break;
768 }
769 xmppConnectionService.databaseBackend.updateConversation(conversation);
770 fragment.updateChatMsgHint();
771 invalidateOptionsMenu();
772 return true;
773 }
774 });
775 popup.inflate(R.menu.encryption_choices);
776 MenuItem otr = popup.getMenu().findItem(R.id.encryption_choice_otr);
777 MenuItem none = popup.getMenu().findItem(R.id.encryption_choice_none);
778 MenuItem pgp = popup.getMenu().findItem(R.id.encryption_choice_pgp);
779 if (conversation.getMode() == Conversation.MODE_MULTI) {
780 otr.setEnabled(false);
781 } else {
782 if (forceEncryption()) {
783 none.setVisible(false);
784 }
785 }
786 switch (conversation.getNextEncryption(forceEncryption())) {
787 case Message.ENCRYPTION_NONE:
788 none.setChecked(true);
789 break;
790 case Message.ENCRYPTION_OTR:
791 otr.setChecked(true);
792 break;
793 case Message.ENCRYPTION_PGP:
794 pgp.setChecked(true);
795 break;
796 case Message.ENCRYPTION_AXOLOTL:
797 Log.d(Config.LOGTAG, "Axolotl confirmed. Setting menu item checked!");
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}