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