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