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 final OnPresenceSelected callback = new OnPresenceSelected() {
402
403 @Override
404 public void onPresenceSelected() {
405 Intent intent = new Intent();
406 boolean chooser = false;
407 String fallbackPackageId = null;
408 switch (attachmentChoice) {
409 case ATTACHMENT_CHOICE_CHOOSE_IMAGE:
410 intent.setAction(Intent.ACTION_GET_CONTENT);
411 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
412 intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,true);
413 }
414 intent.setType("image/*");
415 chooser = true;
416 break;
417 case ATTACHMENT_CHOICE_TAKE_PHOTO:
418 Uri uri = xmppConnectionService.getFileBackend().getTakePhotoUri();
419 intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
420 intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
421 mPendingImageUris.clear();
422 mPendingImageUris.add(uri);
423 break;
424 case ATTACHMENT_CHOICE_CHOOSE_FILE:
425 chooser = true;
426 intent.setType("*/*");
427 intent.addCategory(Intent.CATEGORY_OPENABLE);
428 intent.setAction(Intent.ACTION_GET_CONTENT);
429 break;
430 case ATTACHMENT_CHOICE_RECORD_VOICE:
431 intent.setAction(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
432 fallbackPackageId = "eu.siacs.conversations.voicerecorder";
433 break;
434 case ATTACHMENT_CHOICE_LOCATION:
435 intent.setAction("eu.siacs.conversations.location.request");
436 fallbackPackageId = "eu.siacs.conversations.sharelocation";
437 break;
438 }
439 if (intent.resolveActivity(getPackageManager()) != null) {
440 if (chooser) {
441 startActivityForResult(
442 Intent.createChooser(intent, getString(R.string.perform_action_with)),
443 attachmentChoice);
444 } else {
445 startActivityForResult(intent, attachmentChoice);
446 }
447 } else if (fallbackPackageId != null) {
448 startActivity(getInstallApkIntent(fallbackPackageId));
449 }
450 }
451 };
452 if (attachmentChoice == ATTACHMENT_CHOICE_LOCATION && encryption != Message.ENCRYPTION_OTR) {
453 getSelectedConversation().setNextCounterpart(null);
454 callback.onPresenceSelected();
455 } else {
456 selectPresence(getSelectedConversation(),callback);
457 }
458 }
459
460 private Intent getInstallApkIntent(final String packageId) {
461 Intent intent = new Intent(Intent.ACTION_VIEW);
462 intent.setData(Uri.parse("market://details?id="+packageId));
463 if (intent.resolveActivity(getPackageManager()) != null) {
464 return intent;
465 } else {
466 intent.setData(Uri.parse("http://play.google.com/store/apps/details?id="+packageId));
467 return intent;
468 }
469 }
470
471 public void attachFile(final int attachmentChoice) {
472 switch (attachmentChoice) {
473 case ATTACHMENT_CHOICE_LOCATION:
474 getPreferences().edit().putString("recently_used_quick_action","location").apply();
475 break;
476 case ATTACHMENT_CHOICE_RECORD_VOICE:
477 getPreferences().edit().putString("recently_used_quick_action","voice").apply();
478 break;
479 case ATTACHMENT_CHOICE_TAKE_PHOTO:
480 getPreferences().edit().putString("recently_used_quick_action","photo").apply();
481 break;
482 }
483 final Conversation conversation = getSelectedConversation();
484 final int encryption = conversation.getNextEncryption(forceEncryption());
485 if (encryption == Message.ENCRYPTION_PGP) {
486 if (hasPgp()) {
487 if (conversation.getContact().getPgpKeyId() != 0) {
488 xmppConnectionService.getPgpEngine().hasKey(
489 conversation.getContact(),
490 new UiCallback<Contact>() {
491
492 @Override
493 public void userInputRequried(PendingIntent pi,
494 Contact contact) {
495 ConversationActivity.this.runIntent(pi,attachmentChoice);
496 }
497
498 @Override
499 public void success(Contact contact) {
500 selectPresenceToAttachFile(attachmentChoice,encryption);
501 }
502
503 @Override
504 public void error(int error, Contact contact) {
505 displayErrorDialog(error);
506 }
507 });
508 } else {
509 final ConversationFragment fragment = (ConversationFragment) getFragmentManager()
510 .findFragmentByTag("conversation");
511 if (fragment != null) {
512 fragment.showNoPGPKeyDialog(false,
513 new OnClickListener() {
514
515 @Override
516 public void onClick(DialogInterface dialog,
517 int which) {
518 conversation
519 .setNextEncryption(Message.ENCRYPTION_NONE);
520 xmppConnectionService.databaseBackend
521 .updateConversation(conversation);
522 selectPresenceToAttachFile(attachmentChoice,Message.ENCRYPTION_NONE);
523 }
524 });
525 }
526 }
527 } else {
528 showInstallPgpDialog();
529 }
530 } else {
531 selectPresenceToAttachFile(attachmentChoice,encryption);
532 }
533 }
534
535 @Override
536 public boolean onOptionsItemSelected(final MenuItem item) {
537 if (item.getItemId() == android.R.id.home) {
538 showConversationsOverview();
539 return true;
540 } else if (item.getItemId() == R.id.action_add) {
541 startActivity(new Intent(this, StartConversationActivity.class));
542 return true;
543 } else if (getSelectedConversation() != null) {
544 switch (item.getItemId()) {
545 case R.id.action_attach_file:
546 attachFileDialog();
547 break;
548 case R.id.action_archive:
549 this.endConversation(getSelectedConversation());
550 break;
551 case R.id.action_contact_details:
552 switchToContactDetails(getSelectedConversation().getContact());
553 break;
554 case R.id.action_muc_details:
555 Intent intent = new Intent(this,
556 ConferenceDetailsActivity.class);
557 intent.setAction(ConferenceDetailsActivity.ACTION_VIEW_MUC);
558 intent.putExtra("uuid", getSelectedConversation().getUuid());
559 startActivity(intent);
560 break;
561 case R.id.action_invite:
562 inviteToConversation(getSelectedConversation());
563 break;
564 case R.id.action_security:
565 selectEncryptionDialog(getSelectedConversation());
566 break;
567 case R.id.action_clear_history:
568 clearHistoryDialog(getSelectedConversation());
569 break;
570 case R.id.action_mute:
571 muteConversationDialog(getSelectedConversation());
572 break;
573 case R.id.action_unmute:
574 unmuteConversation(getSelectedConversation());
575 break;
576 case R.id.action_block:
577 BlockContactDialog.show(this, xmppConnectionService, getSelectedConversation());
578 break;
579 case R.id.action_unblock:
580 BlockContactDialog.show(this, xmppConnectionService, getSelectedConversation());
581 break;
582 default:
583 break;
584 }
585 return super.onOptionsItemSelected(item);
586 } else {
587 return super.onOptionsItemSelected(item);
588 }
589 }
590
591 public void endConversation(Conversation conversation) {
592 endConversation(conversation, true, true);
593 }
594
595 public void endConversation(Conversation conversation, boolean showOverview, boolean reinit) {
596 if (showOverview) {
597 showConversationsOverview();
598 }
599 xmppConnectionService.archiveConversation(conversation);
600 if (reinit) {
601 if (conversationList.size() > 0) {
602 setSelectedConversation(conversationList.get(0));
603 this.mConversationFragment.reInit(getSelectedConversation());
604 } else {
605 setSelectedConversation(null);
606 }
607 }
608 }
609
610 @SuppressLint("InflateParams")
611 protected void clearHistoryDialog(final Conversation conversation) {
612 AlertDialog.Builder builder = new AlertDialog.Builder(this);
613 builder.setTitle(getString(R.string.clear_conversation_history));
614 View dialogView = getLayoutInflater().inflate(
615 R.layout.dialog_clear_history, null);
616 final CheckBox endConversationCheckBox = (CheckBox) dialogView
617 .findViewById(R.id.end_conversation_checkbox);
618 builder.setView(dialogView);
619 builder.setNegativeButton(getString(R.string.cancel), null);
620 builder.setPositiveButton(getString(R.string.delete_messages),
621 new OnClickListener() {
622
623 @Override
624 public void onClick(DialogInterface dialog, int which) {
625 ConversationActivity.this.xmppConnectionService.clearConversationHistory(conversation);
626 if (endConversationCheckBox.isChecked()) {
627 endConversation(conversation);
628 } else {
629 updateConversationList();
630 ConversationActivity.this.mConversationFragment.updateMessages();
631 }
632 }
633 });
634 builder.create().show();
635 }
636
637 protected void attachFileDialog() {
638 View menuAttachFile = findViewById(R.id.action_attach_file);
639 if (menuAttachFile == null) {
640 return;
641 }
642 PopupMenu attachFilePopup = new PopupMenu(this, menuAttachFile);
643 attachFilePopup.inflate(R.menu.attachment_choices);
644 if (new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION).resolveActivity(getPackageManager()) == null) {
645 attachFilePopup.getMenu().findItem(R.id.attach_record_voice).setVisible(false);
646 }
647 if (new Intent("eu.siacs.conversations.location.request").resolveActivity(getPackageManager()) == null) {
648 attachFilePopup.getMenu().findItem(R.id.attach_location).setVisible(false);
649 }
650 attachFilePopup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
651
652 @Override
653 public boolean onMenuItemClick(MenuItem item) {
654 switch (item.getItemId()) {
655 case R.id.attach_choose_picture:
656 attachFile(ATTACHMENT_CHOICE_CHOOSE_IMAGE);
657 break;
658 case R.id.attach_take_picture:
659 attachFile(ATTACHMENT_CHOICE_TAKE_PHOTO);
660 break;
661 case R.id.attach_choose_file:
662 attachFile(ATTACHMENT_CHOICE_CHOOSE_FILE);
663 break;
664 case R.id.attach_record_voice:
665 attachFile(ATTACHMENT_CHOICE_RECORD_VOICE);
666 break;
667 case R.id.attach_location:
668 attachFile(ATTACHMENT_CHOICE_LOCATION);
669 break;
670 }
671 return false;
672 }
673 });
674 attachFilePopup.show();
675 }
676
677 public void verifyOtrSessionDialog(final Conversation conversation, View view) {
678 if (!conversation.hasValidOtrSession() || conversation.getOtrSession().getSessionStatus() != SessionStatus.ENCRYPTED) {
679 Toast.makeText(this, R.string.otr_session_not_started, Toast.LENGTH_LONG).show();
680 return;
681 }
682 if (view == null) {
683 return;
684 }
685 PopupMenu popup = new PopupMenu(this, view);
686 popup.inflate(R.menu.verification_choices);
687 popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
688 @Override
689 public boolean onMenuItemClick(MenuItem menuItem) {
690 Intent intent = new Intent(ConversationActivity.this, VerifyOTRActivity.class);
691 intent.setAction(VerifyOTRActivity.ACTION_VERIFY_CONTACT);
692 intent.putExtra("contact", conversation.getContact().getJid().toBareJid().toString());
693 intent.putExtra("account", conversation.getAccount().getJid().toBareJid().toString());
694 switch (menuItem.getItemId()) {
695 case R.id.scan_fingerprint:
696 intent.putExtra("mode",VerifyOTRActivity.MODE_SCAN_FINGERPRINT);
697 break;
698 case R.id.ask_question:
699 intent.putExtra("mode",VerifyOTRActivity.MODE_ASK_QUESTION);
700 break;
701 case R.id.manual_verification:
702 intent.putExtra("mode",VerifyOTRActivity.MODE_MANUAL_VERIFICATION);
703 break;
704 }
705 startActivity(intent);
706 return true;
707 }
708 });
709 popup.show();
710 }
711
712 protected void selectEncryptionDialog(final Conversation conversation) {
713 View menuItemView = findViewById(R.id.action_security);
714 if (menuItemView == null) {
715 return;
716 }
717 PopupMenu popup = new PopupMenu(this, menuItemView);
718 final ConversationFragment fragment = (ConversationFragment) getFragmentManager()
719 .findFragmentByTag("conversation");
720 if (fragment != null) {
721 popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
722
723 @Override
724 public boolean onMenuItemClick(MenuItem item) {
725 switch (item.getItemId()) {
726 case R.id.encryption_choice_none:
727 conversation.setNextEncryption(Message.ENCRYPTION_NONE);
728 item.setChecked(true);
729 break;
730 case R.id.encryption_choice_otr:
731 conversation.setNextEncryption(Message.ENCRYPTION_OTR);
732 item.setChecked(true);
733 break;
734 case R.id.encryption_choice_pgp:
735 if (hasPgp()) {
736 if (conversation.getAccount().getKeys()
737 .has("pgp_signature")) {
738 conversation
739 .setNextEncryption(Message.ENCRYPTION_PGP);
740 item.setChecked(true);
741 } else {
742 announcePgp(conversation.getAccount(),
743 conversation);
744 }
745 } else {
746 showInstallPgpDialog();
747 }
748 break;
749 default:
750 conversation.setNextEncryption(Message.ENCRYPTION_NONE);
751 break;
752 }
753 xmppConnectionService.databaseBackend
754 .updateConversation(conversation);
755 fragment.updateChatMsgHint();
756 return true;
757 }
758 });
759 popup.inflate(R.menu.encryption_choices);
760 MenuItem otr = popup.getMenu().findItem(R.id.encryption_choice_otr);
761 MenuItem none = popup.getMenu().findItem(
762 R.id.encryption_choice_none);
763 if (conversation.getMode() == Conversation.MODE_MULTI) {
764 otr.setEnabled(false);
765 } else {
766 if (forceEncryption()) {
767 none.setVisible(false);
768 }
769 }
770 switch (conversation.getNextEncryption(forceEncryption())) {
771 case Message.ENCRYPTION_NONE:
772 none.setChecked(true);
773 break;
774 case Message.ENCRYPTION_OTR:
775 otr.setChecked(true);
776 break;
777 case Message.ENCRYPTION_PGP:
778 popup.getMenu().findItem(R.id.encryption_choice_pgp)
779 .setChecked(true);
780 break;
781 default:
782 popup.getMenu().findItem(R.id.encryption_choice_none)
783 .setChecked(true);
784 break;
785 }
786 popup.show();
787 }
788 }
789
790 protected void muteConversationDialog(final Conversation conversation) {
791 AlertDialog.Builder builder = new AlertDialog.Builder(this);
792 builder.setTitle(R.string.disable_notifications);
793 final int[] durations = getResources().getIntArray(
794 R.array.mute_options_durations);
795 builder.setItems(R.array.mute_options_descriptions,
796 new OnClickListener() {
797
798 @Override
799 public void onClick(final DialogInterface dialog, final int which) {
800 final long till;
801 if (durations[which] == -1) {
802 till = Long.MAX_VALUE;
803 } else {
804 till = System.currentTimeMillis() + (durations[which] * 1000);
805 }
806 conversation.setMutedTill(till);
807 ConversationActivity.this.xmppConnectionService.databaseBackend
808 .updateConversation(conversation);
809 updateConversationList();
810 ConversationActivity.this.mConversationFragment.updateMessages();
811 invalidateOptionsMenu();
812 }
813 });
814 builder.create().show();
815 }
816
817 public void unmuteConversation(final Conversation conversation) {
818 conversation.setMutedTill(0);
819 this.xmppConnectionService.databaseBackend.updateConversation(conversation);
820 updateConversationList();
821 ConversationActivity.this.mConversationFragment.updateMessages();
822 invalidateOptionsMenu();
823 }
824
825 @Override
826 public void onBackPressed() {
827 if (!isConversationsOverviewVisable()) {
828 showConversationsOverview();
829 } else {
830 moveTaskToBack(true);
831 }
832 }
833
834 @Override
835 protected void onNewIntent(final Intent intent) {
836 if (xmppConnectionServiceBound) {
837 if (intent != null && VIEW_CONVERSATION.equals(intent.getType())) {
838 handleViewConversationIntent(intent);
839 }
840 } else {
841 setIntent(intent);
842 }
843 }
844
845 @Override
846 public void onStart() {
847 super.onStart();
848 this.mRedirected = false;
849 if (this.xmppConnectionServiceBound) {
850 this.onBackendConnected();
851 }
852 if (conversationList.size() >= 1) {
853 this.onConversationUpdate();
854 }
855 }
856
857 @Override
858 public void onPause() {
859 listView.discardUndo();
860 super.onPause();
861 this.mActivityPaused = true;
862 if (this.xmppConnectionServiceBound) {
863 this.xmppConnectionService.getNotificationService().setIsInForeground(false);
864 }
865 }
866
867 @Override
868 public void onResume() {
869 super.onResume();
870 final int theme = findTheme();
871 final boolean usingEnterKey = usingEnterKey();
872 if (this.mTheme != theme || usingEnterKey != mUsingEnterKey) {
873 recreate();
874 }
875 this.mActivityPaused = false;
876 if (this.xmppConnectionServiceBound) {
877 this.xmppConnectionService.getNotificationService().setIsInForeground(true);
878 }
879
880 if (!isConversationsOverviewVisable() || !isConversationsOverviewHideable()) {
881 sendReadMarkerIfNecessary(getSelectedConversation());
882 }
883
884 }
885
886 @Override
887 public void onSaveInstanceState(final Bundle savedInstanceState) {
888 Conversation conversation = getSelectedConversation();
889 if (conversation != null) {
890 savedInstanceState.putString(STATE_OPEN_CONVERSATION,
891 conversation.getUuid());
892 }
893 savedInstanceState.putBoolean(STATE_PANEL_OPEN,
894 isConversationsOverviewVisable());
895 if (this.mPendingImageUris.size() >= 1) {
896 savedInstanceState.putString(STATE_PENDING_URI, this.mPendingImageUris.get(0).toString());
897 }
898 super.onSaveInstanceState(savedInstanceState);
899 }
900
901 @Override
902 void onBackendConnected() {
903 this.xmppConnectionService.getNotificationService().setIsInForeground(true);
904 updateConversationList();
905
906 if (mPendingConferenceInvite != null) {
907 mPendingConferenceInvite.execute(this);
908 mPendingConferenceInvite = null;
909 }
910
911 if (xmppConnectionService.getAccounts().size() == 0) {
912 if (!mRedirected) {
913 this.mRedirected = true;
914 startActivity(new Intent(this, EditAccountActivity.class));
915 finish();
916 }
917 } else if (conversationList.size() <= 0) {
918 if (!mRedirected) {
919 this.mRedirected = true;
920 Intent intent = new Intent(this, StartConversationActivity.class);
921 intent.putExtra("init",true);
922 startActivity(intent);
923 finish();
924 }
925 } else if (getIntent() != null && VIEW_CONVERSATION.equals(getIntent().getType())) {
926 handleViewConversationIntent(getIntent());
927 } else if (selectConversationByUuid(mOpenConverstaion)) {
928 if (mPanelOpen) {
929 showConversationsOverview();
930 } else {
931 if (isConversationsOverviewHideable()) {
932 openConversation();
933 }
934 }
935 this.mConversationFragment.reInit(getSelectedConversation());
936 mOpenConverstaion = null;
937 } else if (getSelectedConversation() == null) {
938 showConversationsOverview();
939 mPendingImageUris.clear();
940 mPendingFileUris.clear();
941 mPendingGeoUri = null;
942 setSelectedConversation(conversationList.get(0));
943 this.mConversationFragment.reInit(getSelectedConversation());
944 }
945
946 for(Iterator<Uri> i = mPendingImageUris.iterator(); i.hasNext(); i.remove()) {
947 attachImageToConversation(getSelectedConversation(),i.next());
948 }
949
950 for(Iterator<Uri> i = mPendingFileUris.iterator(); i.hasNext(); i.remove()) {
951 attachFileToConversation(getSelectedConversation(),i.next());
952 }
953
954 if (mPendingGeoUri != null) {
955 attachLocationToConversation(getSelectedConversation(), mPendingGeoUri);
956 mPendingGeoUri = null;
957 }
958 ExceptionHelper.checkForCrash(this, this.xmppConnectionService);
959 setIntent(new Intent());
960 }
961
962 private void handleViewConversationIntent(final Intent intent) {
963 final String uuid = intent.getStringExtra(CONVERSATION);
964 final String downloadUuid = intent.getStringExtra(MESSAGE);
965 final String text = intent.getStringExtra(TEXT);
966 final String nick = intent.getStringExtra(NICK);
967 if (selectConversationByUuid(uuid)) {
968 this.mConversationFragment.reInit(getSelectedConversation());
969 if (nick != null) {
970 this.mConversationFragment.highlightInConference(nick);
971 } else {
972 this.mConversationFragment.appendText(text);
973 }
974 hideConversationsOverview();
975 openConversation();
976 if (mContentView instanceof SlidingPaneLayout) {
977 updateActionBarTitle(true); //fixes bug where slp isn't properly closed yet
978 }
979 if (downloadUuid != null) {
980 final Message message = mSelectedConversation.findMessageWithFileAndUuid(downloadUuid);
981 if (message != null) {
982 mConversationFragment.messageListAdapter.startDownloadable(message);
983 }
984 }
985 }
986 }
987
988 private boolean selectConversationByUuid(String uuid) {
989 if (uuid == null) {
990 return false;
991 }
992 for (Conversation aConversationList : conversationList) {
993 if (aConversationList.getUuid().equals(uuid)) {
994 setSelectedConversation(aConversationList);
995 return true;
996 }
997 }
998 return false;
999 }
1000
1001 @Override
1002 protected void unregisterListeners() {
1003 super.unregisterListeners();
1004 xmppConnectionService.getNotificationService().setOpenConversation(null);
1005 }
1006
1007 @SuppressLint("NewApi")
1008 private static List<Uri> extractUriFromIntent(final Intent intent) {
1009 List<Uri> uris = new ArrayList<>();
1010 Uri uri = intent.getData();
1011 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 && uri == null) {
1012 ClipData clipData = intent.getClipData();
1013 for(int i = 0; i < clipData.getItemCount(); ++i) {
1014 uris.add(clipData.getItemAt(i).getUri());
1015 }
1016 } else {
1017 uris.add(uri);
1018 }
1019 return uris;
1020 }
1021
1022 @Override
1023 protected void onActivityResult(int requestCode, int resultCode,
1024 final Intent data) {
1025 super.onActivityResult(requestCode, resultCode, data);
1026 if (resultCode == RESULT_OK) {
1027 if (requestCode == REQUEST_DECRYPT_PGP) {
1028 mConversationFragment.hideSnackbar();
1029 mConversationFragment.updateMessages();
1030 } else if (requestCode == ATTACHMENT_CHOICE_CHOOSE_IMAGE) {
1031 mPendingImageUris.clear();
1032 mPendingImageUris.addAll(extractUriFromIntent(data));
1033 if (xmppConnectionServiceBound) {
1034 for(Iterator<Uri> i = mPendingImageUris.iterator(); i.hasNext(); i.remove()) {
1035 attachImageToConversation(getSelectedConversation(),i.next());
1036 }
1037 }
1038 } else if (requestCode == ATTACHMENT_CHOICE_CHOOSE_FILE || requestCode == ATTACHMENT_CHOICE_RECORD_VOICE) {
1039 mPendingFileUris.clear();
1040 mPendingFileUris.addAll(extractUriFromIntent(data));
1041 if (xmppConnectionServiceBound) {
1042 for(Iterator<Uri> i = mPendingImageUris.iterator(); i.hasNext(); i.remove()) {
1043 attachFileToConversation(getSelectedConversation(), i.next());
1044 }
1045 }
1046 } else if (requestCode == ATTACHMENT_CHOICE_TAKE_PHOTO) {
1047 if (mPendingImageUris.size() == 1) {
1048 Uri uri = mPendingImageUris.get(0);
1049 if (xmppConnectionServiceBound) {
1050 attachImageToConversation(getSelectedConversation(), uri);
1051 mPendingImageUris.clear();
1052 }
1053 Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
1054 intent.setData(uri);
1055 sendBroadcast(intent);
1056 } else {
1057 mPendingImageUris.clear();
1058 }
1059 } else if (requestCode == ATTACHMENT_CHOICE_LOCATION) {
1060 double latitude = data.getDoubleExtra("latitude",0);
1061 double longitude = data.getDoubleExtra("longitude",0);
1062 this.mPendingGeoUri = Uri.parse("geo:"+String.valueOf(latitude)+","+String.valueOf(longitude));
1063 if (xmppConnectionServiceBound) {
1064 attachLocationToConversation(getSelectedConversation(), mPendingGeoUri);
1065 this.mPendingGeoUri = null;
1066 }
1067 }
1068 } else {
1069 mPendingImageUris.clear();
1070 mPendingFileUris.clear();
1071 }
1072 }
1073
1074 private void attachLocationToConversation(Conversation conversation, Uri uri) {
1075 xmppConnectionService.attachLocationToConversation(conversation,uri, new UiCallback<Message>() {
1076
1077 @Override
1078 public void success(Message message) {
1079 xmppConnectionService.sendMessage(message);
1080 }
1081
1082 @Override
1083 public void error(int errorCode, Message object) {
1084
1085 }
1086
1087 @Override
1088 public void userInputRequried(PendingIntent pi, Message object) {
1089
1090 }
1091 });
1092 }
1093
1094 private void attachFileToConversation(Conversation conversation, Uri uri) {
1095 prepareFileToast = Toast.makeText(getApplicationContext(),
1096 getText(R.string.preparing_file), Toast.LENGTH_LONG);
1097 prepareFileToast.show();
1098 xmppConnectionService.attachFileToConversation(conversation,uri, new UiCallback<Message>() {
1099 @Override
1100 public void success(Message message) {
1101 hidePrepareFileToast();
1102 xmppConnectionService.sendMessage(message);
1103 }
1104
1105 @Override
1106 public void error(int errorCode, Message message) {
1107 displayErrorDialog(errorCode);
1108 }
1109
1110 @Override
1111 public void userInputRequried(PendingIntent pi, Message message) {
1112
1113 }
1114 });
1115 }
1116
1117 private void attachImageToConversation(Conversation conversation, Uri uri) {
1118 prepareFileToast = Toast.makeText(getApplicationContext(),
1119 getText(R.string.preparing_image), Toast.LENGTH_LONG);
1120 prepareFileToast.show();
1121 xmppConnectionService.attachImageToConversation(conversation, uri,
1122 new UiCallback<Message>() {
1123
1124 @Override
1125 public void userInputRequried(PendingIntent pi,
1126 Message object) {
1127 hidePrepareFileToast();
1128 }
1129
1130 @Override
1131 public void success(Message message) {
1132 xmppConnectionService.sendMessage(message);
1133 }
1134
1135 @Override
1136 public void error(int error, Message message) {
1137 hidePrepareFileToast();
1138 displayErrorDialog(error);
1139 }
1140 });
1141 }
1142
1143 private void hidePrepareFileToast() {
1144 if (prepareFileToast != null) {
1145 runOnUiThread(new Runnable() {
1146
1147 @Override
1148 public void run() {
1149 prepareFileToast.cancel();
1150 }
1151 });
1152 }
1153 }
1154
1155 public void updateConversationList() {
1156 xmppConnectionService
1157 .populateWithOrderedConversations(conversationList);
1158 if (swipedConversation != null) {
1159 if (swipedConversation.isRead()) {
1160 conversationList.remove(swipedConversation);
1161 } else {
1162 listView.discardUndo();
1163 }
1164 }
1165 listAdapter.notifyDataSetChanged();
1166 }
1167
1168 public void runIntent(PendingIntent pi, int requestCode) {
1169 try {
1170 this.startIntentSenderForResult(pi.getIntentSender(), requestCode,
1171 null, 0, 0, 0);
1172 } catch (final SendIntentException ignored) {
1173 }
1174 }
1175
1176 public void encryptTextMessage(Message message) {
1177 xmppConnectionService.getPgpEngine().encrypt(message,
1178 new UiCallback<Message>() {
1179
1180 @Override
1181 public void userInputRequried(PendingIntent pi,
1182 Message message) {
1183 ConversationActivity.this.runIntent(pi,
1184 ConversationActivity.REQUEST_SEND_MESSAGE);
1185 }
1186
1187 @Override
1188 public void success(Message message) {
1189 message.setEncryption(Message.ENCRYPTION_DECRYPTED);
1190 xmppConnectionService.sendMessage(message);
1191 }
1192
1193 @Override
1194 public void error(int error, Message message) {
1195
1196 }
1197 });
1198 }
1199
1200 public boolean forceEncryption() {
1201 return getPreferences().getBoolean("force_encryption", false);
1202 }
1203
1204 public boolean useSendButtonToIndicateStatus() {
1205 return getPreferences().getBoolean("send_button_status", false);
1206 }
1207
1208 public boolean indicateReceived() {
1209 return getPreferences().getBoolean("indicate_received", false);
1210 }
1211
1212 @Override
1213 protected void refreshUiReal() {
1214 updateConversationList();
1215 if (xmppConnectionService != null && xmppConnectionService.getAccounts().size() == 0) {
1216 if (!mRedirected) {
1217 this.mRedirected = true;
1218 startActivity(new Intent(this, EditAccountActivity.class));
1219 finish();
1220 }
1221 } else if (conversationList.size() == 0) {
1222 if (!mRedirected) {
1223 this.mRedirected = true;
1224 Intent intent = new Intent(this, StartConversationActivity.class);
1225 intent.putExtra("init",true);
1226 startActivity(intent);
1227 finish();
1228 }
1229 } else {
1230 ConversationActivity.this.mConversationFragment.updateMessages();
1231 updateActionBarTitle();
1232 }
1233 }
1234
1235 @Override
1236 public void onAccountUpdate() {
1237 this.refreshUi();
1238 }
1239
1240 @Override
1241 public void onConversationUpdate() {
1242 this.refreshUi();
1243 }
1244
1245 @Override
1246 public void onRosterUpdate() {
1247 this.refreshUi();
1248 }
1249
1250 @Override
1251 public void OnUpdateBlocklist(Status status) {
1252 this.refreshUi();
1253 runOnUiThread(new Runnable() {
1254 @Override
1255 public void run() {
1256 invalidateOptionsMenu();
1257 }
1258 });
1259 }
1260
1261 public void unblockConversation(final Blockable conversation) {
1262 xmppConnectionService.sendUnblockRequest(conversation);
1263 }
1264
1265 public boolean enterIsSend() {
1266 return getPreferences().getBoolean("enter_is_send",false);
1267 }
1268}