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