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