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