1package eu.siacs.conversations.ui;
2
3import android.Manifest;
4import android.annotation.SuppressLint;
5import android.app.ActionBar;
6import android.app.AlertDialog;
7import android.app.FragmentTransaction;
8import android.app.PendingIntent;
9import android.content.ClipData;
10import android.content.DialogInterface;
11import android.content.DialogInterface.OnClickListener;
12import android.content.Intent;
13import android.content.IntentSender.SendIntentException;
14import android.content.pm.PackageManager;
15import android.net.Uri;
16import android.os.Build;
17import android.os.Bundle;
18import android.provider.MediaStore;
19import android.support.v4.widget.SlidingPaneLayout;
20import android.support.v4.widget.SlidingPaneLayout.PanelSlideListener;
21import android.util.Log;
22import android.util.Pair;
23import android.view.Gravity;
24import android.view.KeyEvent;
25import android.view.Menu;
26import android.view.MenuItem;
27import android.view.Surface;
28import android.view.View;
29import android.widget.AdapterView;
30import android.widget.AdapterView.OnItemClickListener;
31import android.widget.ArrayAdapter;
32import android.widget.CheckBox;
33import android.widget.PopupMenu;
34import android.widget.PopupMenu.OnMenuItemClickListener;
35import android.widget.Toast;
36
37import net.java.otr4j.session.SessionStatus;
38
39import java.util.ArrayList;
40import java.util.Iterator;
41import java.util.List;
42import java.util.concurrent.atomic.AtomicBoolean;
43
44import de.timroes.android.listview.EnhancedListView;
45import eu.siacs.conversations.Config;
46import eu.siacs.conversations.R;
47import eu.siacs.conversations.crypto.axolotl.AxolotlService;
48import eu.siacs.conversations.crypto.axolotl.XmppAxolotlSession;
49import eu.siacs.conversations.entities.Account;
50import eu.siacs.conversations.entities.Blockable;
51import eu.siacs.conversations.entities.Contact;
52import eu.siacs.conversations.entities.Conversation;
53import eu.siacs.conversations.entities.Message;
54import eu.siacs.conversations.entities.Transferable;
55import eu.siacs.conversations.services.XmppConnectionService;
56import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
57import eu.siacs.conversations.services.XmppConnectionService.OnConversationUpdate;
58import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate;
59import eu.siacs.conversations.ui.adapter.ConversationAdapter;
60import eu.siacs.conversations.utils.ExceptionHelper;
61import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
62import eu.siacs.conversations.xmpp.jid.InvalidJidException;
63import eu.siacs.conversations.xmpp.jid.Jid;
64import org.openintents.openpgp.util.OpenPgpApi;
65
66public class ConversationActivity extends XmppActivity
67 implements OnAccountUpdate, OnConversationUpdate, OnRosterUpdate, OnUpdateBlocklist, XmppConnectionService.OnShowErrorToast {
68
69 public static final String ACTION_DOWNLOAD = "eu.siacs.conversations.action.DOWNLOAD";
70
71 public static final String VIEW_CONVERSATION = "viewConversation";
72 public static final String CONVERSATION = "conversationUuid";
73 public static final String MESSAGE = "messageUuid";
74 public static final String TEXT = "text";
75 public static final String NICK = "nick";
76 public static final String PRIVATE_MESSAGE = "pm";
77
78 public static final int REQUEST_SEND_MESSAGE = 0x0201;
79 public static final int REQUEST_DECRYPT_PGP = 0x0202;
80 public static final int REQUEST_ENCRYPT_MESSAGE = 0x0207;
81 public static final int REQUEST_TRUST_KEYS_TEXT = 0x0208;
82 public static final int REQUEST_TRUST_KEYS_MENU = 0x0209;
83 public static final int REQUEST_START_DOWNLOAD = 0x0210;
84 public static final int ATTACHMENT_CHOICE_CHOOSE_IMAGE = 0x0301;
85 public static final int ATTACHMENT_CHOICE_TAKE_PHOTO = 0x0302;
86 public static final int ATTACHMENT_CHOICE_CHOOSE_FILE = 0x0303;
87 public static final int ATTACHMENT_CHOICE_RECORD_VOICE = 0x0304;
88 public static final int ATTACHMENT_CHOICE_LOCATION = 0x0305;
89 public static final int ATTACHMENT_CHOICE_INVALID = 0x0306;
90 private static final String STATE_OPEN_CONVERSATION = "state_open_conversation";
91 private static final String STATE_PANEL_OPEN = "state_panel_open";
92 private static final String STATE_PENDING_URI = "state_pending_uri";
93
94 private String mOpenConverstaion = null;
95 private boolean mPanelOpen = true;
96 final private List<Uri> mPendingImageUris = new ArrayList<>();
97 final private List<Uri> mPendingFileUris = new ArrayList<>();
98 private Uri mPendingGeoUri = null;
99 private boolean forbidProcessingPendings = false;
100 private Message mPendingDownloadableMessage = null;
101
102 private boolean conversationWasSelectedByKeyboard = false;
103
104 private View mContentView;
105
106 private List<Conversation> conversationList = new ArrayList<>();
107 private Conversation swipedConversation = null;
108 private Conversation mSelectedConversation = null;
109 private EnhancedListView listView;
110 private ConversationFragment mConversationFragment;
111
112 private ArrayAdapter<Conversation> listAdapter;
113
114 private Toast prepareFileToast;
115
116 private boolean mActivityPaused = false;
117 private AtomicBoolean mRedirected = new AtomicBoolean(false);
118 private Pair<Integer, Intent> mPostponedActivityResult;
119
120 public Conversation getSelectedConversation() {
121 return this.mSelectedConversation;
122 }
123
124 public void setSelectedConversation(Conversation conversation) {
125 this.mSelectedConversation = conversation;
126 }
127
128 public void showConversationsOverview() {
129 if (mContentView instanceof SlidingPaneLayout) {
130 SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
131 mSlidingPaneLayout.openPane();
132 }
133 }
134
135 @Override
136 protected String getShareableUri() {
137 Conversation conversation = getSelectedConversation();
138 if (conversation != null) {
139 return conversation.getAccount().getShareableUri();
140 } else {
141 return "";
142 }
143 }
144
145 public void hideConversationsOverview() {
146 if (mContentView instanceof SlidingPaneLayout) {
147 SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
148 mSlidingPaneLayout.closePane();
149 }
150 }
151
152 public boolean isConversationsOverviewHideable() {
153 if (mContentView instanceof SlidingPaneLayout) {
154 SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
155 return mSlidingPaneLayout.isSlideable();
156 } else {
157 return false;
158 }
159 }
160
161 public boolean isConversationsOverviewVisable() {
162 if (mContentView instanceof SlidingPaneLayout) {
163 SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
164 return mSlidingPaneLayout.isOpen();
165 } else {
166 return true;
167 }
168 }
169
170 @Override
171 protected void onCreate(final Bundle savedInstanceState) {
172 super.onCreate(savedInstanceState);
173 if (savedInstanceState != null) {
174 mOpenConverstaion = savedInstanceState.getString(STATE_OPEN_CONVERSATION, null);
175 mPanelOpen = savedInstanceState.getBoolean(STATE_PANEL_OPEN, true);
176 String pending = savedInstanceState.getString(STATE_PENDING_URI, null);
177 if (pending != null) {
178 mPendingImageUris.clear();
179 mPendingImageUris.add(Uri.parse(pending));
180 }
181 }
182
183 setContentView(R.layout.fragment_conversations_overview);
184
185 this.mConversationFragment = new ConversationFragment();
186 FragmentTransaction transaction = getFragmentManager().beginTransaction();
187 transaction.replace(R.id.selected_conversation, this.mConversationFragment, "conversation");
188 transaction.commit();
189
190 listView = (EnhancedListView) findViewById(R.id.list);
191 this.listAdapter = new ConversationAdapter(this, conversationList);
192 listView.setAdapter(this.listAdapter);
193
194 if (getActionBar() != null) {
195 getActionBar().setDisplayHomeAsUpEnabled(false);
196 getActionBar().setHomeButtonEnabled(false);
197 }
198
199 listView.setOnItemClickListener(new OnItemClickListener() {
200
201 @Override
202 public void onItemClick(AdapterView<?> arg0, View clickedView,
203 int position, long arg3) {
204 if (getSelectedConversation() != conversationList.get(position)) {
205 setSelectedConversation(conversationList.get(position));
206 ConversationActivity.this.mConversationFragment.reInit(getSelectedConversation());
207 conversationWasSelectedByKeyboard = false;
208 }
209 hideConversationsOverview();
210 openConversation();
211 }
212 });
213
214 listView.setDismissCallback(new EnhancedListView.OnDismissCallback() {
215
216 @Override
217 public EnhancedListView.Undoable onDismiss(final EnhancedListView enhancedListView, final int position) {
218
219 final int index = listView.getFirstVisiblePosition();
220 View v = listView.getChildAt(0);
221 final int top = (v == null) ? 0 : (v.getTop() - listView.getPaddingTop());
222
223 try {
224 swipedConversation = listAdapter.getItem(position);
225 } catch (IndexOutOfBoundsException e) {
226 return null;
227 }
228 listAdapter.remove(swipedConversation);
229 xmppConnectionService.markRead(swipedConversation);
230
231 final boolean formerlySelected = (getSelectedConversation() == swipedConversation);
232 if (position == 0 && listAdapter.getCount() == 0) {
233 endConversation(swipedConversation, false, true);
234 return null;
235 } else if (formerlySelected) {
236 setSelectedConversation(listAdapter.getItem(0));
237 ConversationActivity.this.mConversationFragment
238 .reInit(getSelectedConversation());
239 }
240
241 return new EnhancedListView.Undoable() {
242
243 @Override
244 public void undo() {
245 listAdapter.insert(swipedConversation, position);
246 if (formerlySelected) {
247 setSelectedConversation(swipedConversation);
248 ConversationActivity.this.mConversationFragment
249 .reInit(getSelectedConversation());
250 }
251 swipedConversation = null;
252 listView.setSelectionFromTop(index + (listView.getChildCount() < position ? 1 : 0), top);
253 }
254
255 @Override
256 public void discard() {
257 if (!swipedConversation.isRead()
258 && swipedConversation.getMode() == Conversation.MODE_SINGLE) {
259 swipedConversation = null;
260 return;
261 }
262 endConversation(swipedConversation, false, false);
263 swipedConversation = null;
264 }
265
266 @Override
267 public String getTitle() {
268 if (swipedConversation.getMode() == Conversation.MODE_MULTI) {
269 return getResources().getString(R.string.title_undo_swipe_out_muc);
270 } else {
271 return getResources().getString(R.string.title_undo_swipe_out_conversation);
272 }
273 }
274 };
275 }
276 });
277 listView.enableSwipeToDismiss();
278 listView.setSwipingLayout(R.id.swipeable_item);
279 listView.setUndoStyle(EnhancedListView.UndoStyle.SINGLE_POPUP);
280 listView.setUndoHideDelay(5000);
281 listView.setRequireTouchBeforeDismiss(false);
282
283 mContentView = findViewById(R.id.content_view_spl);
284 if (mContentView == null) {
285 mContentView = findViewById(R.id.content_view_ll);
286 }
287 if (mContentView instanceof SlidingPaneLayout) {
288 SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
289 mSlidingPaneLayout.setParallaxDistance(150);
290 mSlidingPaneLayout
291 .setShadowResource(R.drawable.es_slidingpane_shadow);
292 mSlidingPaneLayout.setSliderFadeColor(0);
293 mSlidingPaneLayout.setPanelSlideListener(new PanelSlideListener() {
294
295 @Override
296 public void onPanelOpened(View arg0) {
297 updateActionBarTitle();
298 invalidateOptionsMenu();
299 hideKeyboard();
300 if (xmppConnectionServiceBound) {
301 xmppConnectionService.getNotificationService()
302 .setOpenConversation(null);
303 }
304 closeContextMenu();
305 }
306
307 @Override
308 public void onPanelClosed(View arg0) {
309 listView.discardUndo();
310 openConversation();
311 }
312
313 @Override
314 public void onPanelSlide(View arg0, float arg1) {
315 // TODO Auto-generated method stub
316
317 }
318 });
319 }
320 }
321
322 @Override
323 public void switchToConversation(Conversation conversation) {
324 setSelectedConversation(conversation);
325 runOnUiThread(new Runnable() {
326 @Override
327 public void run() {
328 ConversationActivity.this.mConversationFragment.reInit(getSelectedConversation());
329 openConversation();
330 }
331 });
332 }
333
334 private void updateActionBarTitle() {
335 updateActionBarTitle(isConversationsOverviewHideable() && !isConversationsOverviewVisable());
336 }
337
338 private void updateActionBarTitle(boolean titleShouldBeName) {
339 final ActionBar ab = getActionBar();
340 final Conversation conversation = getSelectedConversation();
341 if (ab != null) {
342 if (titleShouldBeName && conversation != null) {
343 ab.setDisplayHomeAsUpEnabled(true);
344 ab.setHomeButtonEnabled(true);
345 if (conversation.getMode() == Conversation.MODE_SINGLE || useSubjectToIdentifyConference()) {
346 ab.setTitle(conversation.getName());
347 } else {
348 ab.setTitle(conversation.getJid().toBareJid().toString());
349 }
350 } else {
351 ab.setDisplayHomeAsUpEnabled(false);
352 ab.setHomeButtonEnabled(false);
353 ab.setTitle(R.string.app_name);
354 }
355 }
356 }
357
358 private void openConversation() {
359 this.updateActionBarTitle();
360 this.invalidateOptionsMenu();
361 if (xmppConnectionServiceBound) {
362 final Conversation conversation = getSelectedConversation();
363 xmppConnectionService.getNotificationService().setOpenConversation(conversation);
364 sendReadMarkerIfNecessary(conversation);
365 }
366 listAdapter.notifyDataSetChanged();
367 }
368
369 public void sendReadMarkerIfNecessary(final Conversation conversation) {
370 if (!mActivityPaused && conversation != null) {
371 if (!conversation.isRead()) {
372 xmppConnectionService.sendReadMarker(conversation);
373 } else {
374 xmppConnectionService.markRead(conversation);
375 }
376 }
377 }
378
379 @Override
380 public boolean onCreateOptionsMenu(Menu menu) {
381 getMenuInflater().inflate(R.menu.conversations, menu);
382 final MenuItem menuSecure = menu.findItem(R.id.action_security);
383 final MenuItem menuArchive = menu.findItem(R.id.action_archive);
384 final MenuItem menuMucDetails = menu.findItem(R.id.action_muc_details);
385 final MenuItem menuContactDetails = menu.findItem(R.id.action_contact_details);
386 final MenuItem menuAttach = menu.findItem(R.id.action_attach_file);
387 final MenuItem menuClearHistory = menu.findItem(R.id.action_clear_history);
388 final MenuItem menuAdd = menu.findItem(R.id.action_add);
389 final MenuItem menuInviteContact = menu.findItem(R.id.action_invite);
390 final MenuItem menuMute = menu.findItem(R.id.action_mute);
391 final MenuItem menuUnmute = menu.findItem(R.id.action_unmute);
392
393 if (isConversationsOverviewVisable() && isConversationsOverviewHideable()) {
394 menuArchive.setVisible(false);
395 menuMucDetails.setVisible(false);
396 menuContactDetails.setVisible(false);
397 menuSecure.setVisible(false);
398 menuInviteContact.setVisible(false);
399 menuAttach.setVisible(false);
400 menuClearHistory.setVisible(false);
401 menuMute.setVisible(false);
402 menuUnmute.setVisible(false);
403 } else {
404 menuAdd.setVisible(!isConversationsOverviewHideable());
405 if (this.getSelectedConversation() != null) {
406 if (this.getSelectedConversation().getNextEncryption() != Message.ENCRYPTION_NONE) {
407 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
408 menuSecure.setIcon(R.drawable.ic_lock_white_24dp);
409 } else {
410 menuSecure.setIcon(R.drawable.ic_action_secure);
411 }
412 }
413 if (this.getSelectedConversation().getMode() == Conversation.MODE_MULTI) {
414 menuContactDetails.setVisible(false);
415 menuAttach.setVisible(getSelectedConversation().getAccount().httpUploadAvailable() && getSelectedConversation().getMucOptions().participating());
416 menuInviteContact.setVisible(getSelectedConversation().getMucOptions().canInvite());
417 menuSecure.setVisible(!Config.HIDE_PGP_IN_UI); //if pgp is hidden conferences have no choice of encryption
418 } else {
419 menuMucDetails.setVisible(false);
420 }
421 if (this.getSelectedConversation().isMuted()) {
422 menuMute.setVisible(false);
423 } else {
424 menuUnmute.setVisible(false);
425 }
426 }
427 }
428 return true;
429 }
430
431 protected void selectPresenceToAttachFile(final int attachmentChoice, final int encryption) {
432 final Conversation conversation = getSelectedConversation();
433 final Account account = conversation.getAccount();
434 final OnPresenceSelected callback = new OnPresenceSelected() {
435
436 @Override
437 public void onPresenceSelected() {
438 Intent intent = new Intent();
439 boolean chooser = false;
440 String fallbackPackageId = null;
441 switch (attachmentChoice) {
442 case ATTACHMENT_CHOICE_CHOOSE_IMAGE:
443 intent.setAction(Intent.ACTION_GET_CONTENT);
444 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
445 intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,true);
446 }
447 intent.setType("image/*");
448 chooser = true;
449 break;
450 case ATTACHMENT_CHOICE_TAKE_PHOTO:
451 Uri uri = xmppConnectionService.getFileBackend().getTakePhotoUri();
452 intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
453 intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
454 mPendingImageUris.clear();
455 mPendingImageUris.add(uri);
456 break;
457 case ATTACHMENT_CHOICE_CHOOSE_FILE:
458 chooser = true;
459 intent.setType("*/*");
460 intent.addCategory(Intent.CATEGORY_OPENABLE);
461 intent.setAction(Intent.ACTION_GET_CONTENT);
462 break;
463 case ATTACHMENT_CHOICE_RECORD_VOICE:
464 intent.setAction(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
465 fallbackPackageId = "eu.siacs.conversations.voicerecorder";
466 break;
467 case ATTACHMENT_CHOICE_LOCATION:
468 intent.setAction("eu.siacs.conversations.location.request");
469 fallbackPackageId = "eu.siacs.conversations.sharelocation";
470 break;
471 }
472 if (intent.resolveActivity(getPackageManager()) != null) {
473 if (chooser) {
474 startActivityForResult(
475 Intent.createChooser(intent, getString(R.string.perform_action_with)),
476 attachmentChoice);
477 } else {
478 startActivityForResult(intent, attachmentChoice);
479 }
480 } else if (fallbackPackageId != null) {
481 startActivity(getInstallApkIntent(fallbackPackageId));
482 }
483 }
484 };
485 if ((account.httpUploadAvailable() || attachmentChoice == ATTACHMENT_CHOICE_LOCATION) && encryption != Message.ENCRYPTION_OTR) {
486 conversation.setNextCounterpart(null);
487 callback.onPresenceSelected();
488 } else {
489 selectPresence(conversation, callback);
490 }
491 }
492
493 private Intent getInstallApkIntent(final String packageId) {
494 Intent intent = new Intent(Intent.ACTION_VIEW);
495 intent.setData(Uri.parse("market://details?id=" + packageId));
496 if (intent.resolveActivity(getPackageManager()) != null) {
497 return intent;
498 } else {
499 intent.setData(Uri.parse("http://play.google.com/store/apps/details?id=" + packageId));
500 return intent;
501 }
502 }
503
504 public void attachFile(final int attachmentChoice) {
505 if (attachmentChoice != ATTACHMENT_CHOICE_LOCATION) {
506 if (!hasStoragePermission(attachmentChoice)) {
507 return;
508 }
509 }
510 switch (attachmentChoice) {
511 case ATTACHMENT_CHOICE_LOCATION:
512 getPreferences().edit().putString("recently_used_quick_action","location").apply();
513 break;
514 case ATTACHMENT_CHOICE_RECORD_VOICE:
515 getPreferences().edit().putString("recently_used_quick_action","voice").apply();
516 break;
517 case ATTACHMENT_CHOICE_TAKE_PHOTO:
518 getPreferences().edit().putString("recently_used_quick_action","photo").apply();
519 break;
520 case ATTACHMENT_CHOICE_CHOOSE_IMAGE:
521 getPreferences().edit().putString("recently_used_quick_action","picture").apply();
522 break;
523 }
524 final Conversation conversation = getSelectedConversation();
525 final int encryption = conversation.getNextEncryption();
526 final int mode = conversation.getMode();
527 if (encryption == Message.ENCRYPTION_PGP) {
528 if (hasPgp()) {
529 if (mode == Conversation.MODE_SINGLE && conversation.getContact().getPgpKeyId() != 0) {
530 xmppConnectionService.getPgpEngine().hasKey(
531 conversation.getContact(),
532 new UiCallback<Contact>() {
533
534 @Override
535 public void userInputRequried(PendingIntent pi, Contact contact) {
536 ConversationActivity.this.runIntent(pi, attachmentChoice);
537 }
538
539 @Override
540 public void success(Contact contact) {
541 selectPresenceToAttachFile(attachmentChoice, encryption);
542 }
543
544 @Override
545 public void error(int error, Contact contact) {
546 displayErrorDialog(error);
547 }
548 });
549 } else if (mode == Conversation.MODE_MULTI && conversation.getMucOptions().pgpKeysInUse()) {
550 if (!conversation.getMucOptions().everybodyHasKeys()) {
551 Toast warning = Toast
552 .makeText(this,
553 R.string.missing_public_keys,
554 Toast.LENGTH_LONG);
555 warning.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
556 warning.show();
557 }
558 selectPresenceToAttachFile(attachmentChoice, encryption);
559 } else {
560 final ConversationFragment fragment = (ConversationFragment) getFragmentManager()
561 .findFragmentByTag("conversation");
562 if (fragment != null) {
563 fragment.showNoPGPKeyDialog(false,
564 new OnClickListener() {
565
566 @Override
567 public void onClick(DialogInterface dialog,
568 int which) {
569 conversation
570 .setNextEncryption(Message.ENCRYPTION_NONE);
571 xmppConnectionService.databaseBackend
572 .updateConversation(conversation);
573 selectPresenceToAttachFile(attachmentChoice,Message.ENCRYPTION_NONE);
574 }
575 });
576 }
577 }
578 } else {
579 showInstallPgpDialog();
580 }
581 } else {
582 if (encryption != Message.ENCRYPTION_AXOLOTL || !trustKeysIfNeeded(REQUEST_TRUST_KEYS_MENU, attachmentChoice)) {
583 selectPresenceToAttachFile(attachmentChoice, encryption);
584 }
585 }
586 }
587
588 public boolean hasStoragePermission(int attachmentChoice) {
589 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
590 if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
591 requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, attachmentChoice);
592 return false;
593 } else {
594 return true;
595 }
596 } else {
597 return true;
598 }
599 }
600
601 @Override
602 public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
603 if (grantResults.length > 0)
604 if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
605 if (requestCode == REQUEST_START_DOWNLOAD) {
606 if (this.mPendingDownloadableMessage != null) {
607 startDownloadable(this.mPendingDownloadableMessage);
608 }
609 } else {
610 attachFile(requestCode);
611 }
612 } else {
613 Toast.makeText(this,R.string.no_storage_permission,Toast.LENGTH_SHORT).show();
614 }
615 }
616
617 public void startDownloadable(Message message) {
618 if (!hasStoragePermission(ConversationActivity.REQUEST_START_DOWNLOAD)) {
619 this.mPendingDownloadableMessage = message;
620 return;
621 }
622 Transferable transferable = message.getTransferable();
623 if (transferable != null) {
624 if (!transferable.start()) {
625 Toast.makeText(this, R.string.not_connected_try_again,Toast.LENGTH_SHORT).show();
626 }
627 } else if (message.treatAsDownloadable() != Message.Decision.NEVER) {
628 xmppConnectionService.getHttpConnectionManager().createNewDownloadConnection(message, true);
629 }
630 }
631
632 @Override
633 public boolean onOptionsItemSelected(final MenuItem item) {
634 if (item.getItemId() == android.R.id.home) {
635 showConversationsOverview();
636 return true;
637 } else if (item.getItemId() == R.id.action_add) {
638 startActivity(new Intent(this, StartConversationActivity.class));
639 return true;
640 } else if (getSelectedConversation() != null) {
641 switch (item.getItemId()) {
642 case R.id.action_attach_file:
643 attachFileDialog();
644 break;
645 case R.id.action_archive:
646 this.endConversation(getSelectedConversation());
647 break;
648 case R.id.action_contact_details:
649 switchToContactDetails(getSelectedConversation().getContact());
650 break;
651 case R.id.action_muc_details:
652 Intent intent = new Intent(this,
653 ConferenceDetailsActivity.class);
654 intent.setAction(ConferenceDetailsActivity.ACTION_VIEW_MUC);
655 intent.putExtra("uuid", getSelectedConversation().getUuid());
656 startActivity(intent);
657 break;
658 case R.id.action_invite:
659 inviteToConversation(getSelectedConversation());
660 break;
661 case R.id.action_security:
662 selectEncryptionDialog(getSelectedConversation());
663 break;
664 case R.id.action_clear_history:
665 clearHistoryDialog(getSelectedConversation());
666 break;
667 case R.id.action_mute:
668 muteConversationDialog(getSelectedConversation());
669 break;
670 case R.id.action_unmute:
671 unmuteConversation(getSelectedConversation());
672 break;
673 case R.id.action_block:
674 BlockContactDialog.show(this, xmppConnectionService, getSelectedConversation());
675 break;
676 case R.id.action_unblock:
677 BlockContactDialog.show(this, xmppConnectionService, getSelectedConversation());
678 break;
679 default:
680 break;
681 }
682 return super.onOptionsItemSelected(item);
683 } else {
684 return super.onOptionsItemSelected(item);
685 }
686 }
687
688 public void endConversation(Conversation conversation) {
689 endConversation(conversation, true, true);
690 }
691
692 public void endConversation(Conversation conversation, boolean showOverview, boolean reinit) {
693 if (showOverview) {
694 showConversationsOverview();
695 }
696 xmppConnectionService.archiveConversation(conversation);
697 if (reinit) {
698 if (conversationList.size() > 0) {
699 setSelectedConversation(conversationList.get(0));
700 this.mConversationFragment.reInit(getSelectedConversation());
701 } else {
702 setSelectedConversation(null);
703 if (mRedirected.compareAndSet(false,true)) {
704 Intent intent = new Intent(this, StartConversationActivity.class);
705 intent.putExtra("init",true);
706 startActivity(intent);
707 finish();
708 }
709 }
710 }
711 }
712
713 @SuppressLint("InflateParams")
714 protected void clearHistoryDialog(final Conversation conversation) {
715 AlertDialog.Builder builder = new AlertDialog.Builder(this);
716 builder.setTitle(getString(R.string.clear_conversation_history));
717 View dialogView = getLayoutInflater().inflate(
718 R.layout.dialog_clear_history, null);
719 final CheckBox endConversationCheckBox = (CheckBox) dialogView
720 .findViewById(R.id.end_conversation_checkbox);
721 builder.setView(dialogView);
722 builder.setNegativeButton(getString(R.string.cancel), null);
723 builder.setPositiveButton(getString(R.string.delete_messages),
724 new OnClickListener() {
725
726 @Override
727 public void onClick(DialogInterface dialog, int which) {
728 ConversationActivity.this.xmppConnectionService.clearConversationHistory(conversation);
729 if (endConversationCheckBox.isChecked()) {
730 endConversation(conversation);
731 } else {
732 updateConversationList();
733 ConversationActivity.this.mConversationFragment.updateMessages();
734 }
735 }
736 });
737 builder.create().show();
738 }
739
740 protected void attachFileDialog() {
741 View menuAttachFile = findViewById(R.id.action_attach_file);
742 if (menuAttachFile == null) {
743 return;
744 }
745 PopupMenu attachFilePopup = new PopupMenu(this, menuAttachFile);
746 attachFilePopup.inflate(R.menu.attachment_choices);
747 if (new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION).resolveActivity(getPackageManager()) == null) {
748 attachFilePopup.getMenu().findItem(R.id.attach_record_voice).setVisible(false);
749 }
750 if (new Intent("eu.siacs.conversations.location.request").resolveActivity(getPackageManager()) == null) {
751 attachFilePopup.getMenu().findItem(R.id.attach_location).setVisible(false);
752 }
753 attachFilePopup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
754
755 @Override
756 public boolean onMenuItemClick(MenuItem item) {
757 switch (item.getItemId()) {
758 case R.id.attach_choose_picture:
759 attachFile(ATTACHMENT_CHOICE_CHOOSE_IMAGE);
760 break;
761 case R.id.attach_take_picture:
762 attachFile(ATTACHMENT_CHOICE_TAKE_PHOTO);
763 break;
764 case R.id.attach_choose_file:
765 attachFile(ATTACHMENT_CHOICE_CHOOSE_FILE);
766 break;
767 case R.id.attach_record_voice:
768 attachFile(ATTACHMENT_CHOICE_RECORD_VOICE);
769 break;
770 case R.id.attach_location:
771 attachFile(ATTACHMENT_CHOICE_LOCATION);
772 break;
773 }
774 return false;
775 }
776 });
777 attachFilePopup.show();
778 }
779
780 public void verifyOtrSessionDialog(final Conversation conversation, View view) {
781 if (!conversation.hasValidOtrSession() || conversation.getOtrSession().getSessionStatus() != SessionStatus.ENCRYPTED) {
782 Toast.makeText(this, R.string.otr_session_not_started, Toast.LENGTH_LONG).show();
783 return;
784 }
785 if (view == null) {
786 return;
787 }
788 PopupMenu popup = new PopupMenu(this, view);
789 popup.inflate(R.menu.verification_choices);
790 popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
791 @Override
792 public boolean onMenuItemClick(MenuItem menuItem) {
793 Intent intent = new Intent(ConversationActivity.this, VerifyOTRActivity.class);
794 intent.setAction(VerifyOTRActivity.ACTION_VERIFY_CONTACT);
795 intent.putExtra("contact", conversation.getContact().getJid().toBareJid().toString());
796 intent.putExtra("account", conversation.getAccount().getJid().toBareJid().toString());
797 switch (menuItem.getItemId()) {
798 case R.id.scan_fingerprint:
799 intent.putExtra("mode", VerifyOTRActivity.MODE_SCAN_FINGERPRINT);
800 break;
801 case R.id.ask_question:
802 intent.putExtra("mode", VerifyOTRActivity.MODE_ASK_QUESTION);
803 break;
804 case R.id.manual_verification:
805 intent.putExtra("mode", VerifyOTRActivity.MODE_MANUAL_VERIFICATION);
806 break;
807 }
808 startActivity(intent);
809 return true;
810 }
811 });
812 popup.show();
813 }
814
815 protected void selectEncryptionDialog(final Conversation conversation) {
816 View menuItemView = findViewById(R.id.action_security);
817 if (menuItemView == null) {
818 return;
819 }
820 PopupMenu popup = new PopupMenu(this, menuItemView);
821 final ConversationFragment fragment = (ConversationFragment) getFragmentManager()
822 .findFragmentByTag("conversation");
823 if (fragment != null) {
824 popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
825
826 @Override
827 public boolean onMenuItemClick(MenuItem item) {
828 switch (item.getItemId()) {
829 case R.id.encryption_choice_none:
830 conversation.setNextEncryption(Message.ENCRYPTION_NONE);
831 item.setChecked(true);
832 break;
833 case R.id.encryption_choice_otr:
834 conversation.setNextEncryption(Message.ENCRYPTION_OTR);
835 item.setChecked(true);
836 break;
837 case R.id.encryption_choice_pgp:
838 if (hasPgp()) {
839 if (conversation.getAccount().getPgpSignature() != null) {
840 conversation.setNextEncryption(Message.ENCRYPTION_PGP);
841 item.setChecked(true);
842 } else {
843 announcePgp(conversation.getAccount(),conversation);
844 }
845 } else {
846 showInstallPgpDialog();
847 }
848 break;
849 case R.id.encryption_choice_axolotl:
850 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(conversation.getAccount())
851 + "Enabled axolotl for Contact " + conversation.getContact().getJid());
852 conversation.setNextEncryption(Message.ENCRYPTION_AXOLOTL);
853 item.setChecked(true);
854 break;
855 default:
856 conversation.setNextEncryption(Message.ENCRYPTION_NONE);
857 break;
858 }
859 xmppConnectionService.databaseBackend.updateConversation(conversation);
860 fragment.updateChatMsgHint();
861 invalidateOptionsMenu();
862 refreshUi();
863 return true;
864 }
865 });
866 popup.inflate(R.menu.encryption_choices);
867 MenuItem otr = popup.getMenu().findItem(R.id.encryption_choice_otr);
868 MenuItem none = popup.getMenu().findItem(R.id.encryption_choice_none);
869 MenuItem pgp = popup.getMenu().findItem(R.id.encryption_choice_pgp);
870 MenuItem axolotl = popup.getMenu().findItem(R.id.encryption_choice_axolotl);
871 pgp.setVisible(!Config.HIDE_PGP_IN_UI);
872 none.setVisible(!Config.PARANOID_MODE);
873 if (conversation.getMode() == Conversation.MODE_MULTI) {
874 otr.setVisible(false);
875 axolotl.setVisible(false);
876 } else if (!conversation.getAccount().getAxolotlService().isContactAxolotlCapable(conversation.getContact())) {
877 axolotl.setEnabled(false);
878 }
879 switch (conversation.getNextEncryption()) {
880 case Message.ENCRYPTION_NONE:
881 none.setChecked(true);
882 break;
883 case Message.ENCRYPTION_OTR:
884 otr.setChecked(true);
885 break;
886 case Message.ENCRYPTION_PGP:
887 pgp.setChecked(true);
888 break;
889 case Message.ENCRYPTION_AXOLOTL:
890 axolotl.setChecked(true);
891 break;
892 default:
893 none.setChecked(true);
894 break;
895 }
896 popup.show();
897 }
898 }
899
900 protected void muteConversationDialog(final Conversation conversation) {
901 AlertDialog.Builder builder = new AlertDialog.Builder(this);
902 builder.setTitle(R.string.disable_notifications);
903 final int[] durations = getResources().getIntArray(R.array.mute_options_durations);
904 builder.setItems(R.array.mute_options_descriptions,
905 new OnClickListener() {
906
907 @Override
908 public void onClick(final DialogInterface dialog, final int which) {
909 final long till;
910 if (durations[which] == -1) {
911 till = Long.MAX_VALUE;
912 } else {
913 till = System.currentTimeMillis() + (durations[which] * 1000);
914 }
915 conversation.setMutedTill(till);
916 ConversationActivity.this.xmppConnectionService.databaseBackend
917 .updateConversation(conversation);
918 updateConversationList();
919 ConversationActivity.this.mConversationFragment.updateMessages();
920 invalidateOptionsMenu();
921 }
922 });
923 builder.create().show();
924 }
925
926 public void unmuteConversation(final Conversation conversation) {
927 conversation.setMutedTill(0);
928 this.xmppConnectionService.databaseBackend.updateConversation(conversation);
929 updateConversationList();
930 ConversationActivity.this.mConversationFragment.updateMessages();
931 invalidateOptionsMenu();
932 }
933
934 @Override
935 public void onBackPressed() {
936 if (!isConversationsOverviewVisable()) {
937 showConversationsOverview();
938 } else {
939 moveTaskToBack(true);
940 }
941 }
942
943 @Override
944 public boolean onKeyUp(int key, KeyEvent event) {
945 int rotation = getWindowManager().getDefaultDisplay().getRotation();
946 final int upKey;
947 final int downKey;
948 switch(rotation) {
949 case Surface.ROTATION_90:
950 upKey = KeyEvent.KEYCODE_DPAD_LEFT;
951 downKey = KeyEvent.KEYCODE_DPAD_RIGHT;
952 break;
953 case Surface.ROTATION_180:
954 upKey = KeyEvent.KEYCODE_DPAD_DOWN;
955 downKey = KeyEvent.KEYCODE_DPAD_UP;
956 break;
957 case Surface.ROTATION_270:
958 upKey = KeyEvent.KEYCODE_DPAD_RIGHT;
959 downKey = KeyEvent.KEYCODE_DPAD_LEFT;
960 break;
961 default:
962 upKey = KeyEvent.KEYCODE_DPAD_UP;
963 downKey = KeyEvent.KEYCODE_DPAD_DOWN;
964 }
965 final boolean modifier = event.isCtrlPressed() || event.isAltPressed();
966 if (modifier && key == KeyEvent.KEYCODE_TAB && isConversationsOverviewHideable()) {
967 toggleConversationsOverview();
968 return true;
969 } else if (modifier && key == downKey) {
970 if (isConversationsOverviewHideable() && !isConversationsOverviewVisable()) {
971 showConversationsOverview();;
972 }
973 return selectDownConversation();
974 } else if (modifier && key == upKey) {
975 if (isConversationsOverviewHideable() && !isConversationsOverviewVisable()) {
976 showConversationsOverview();
977 }
978 return selectUpConversation();
979 } else if (modifier && key == KeyEvent.KEYCODE_1) {
980 return openConversationByIndex(0);
981 } else if (modifier && key == KeyEvent.KEYCODE_2) {
982 return openConversationByIndex(1);
983 } else if (modifier && key == KeyEvent.KEYCODE_3) {
984 return openConversationByIndex(2);
985 } else if (modifier && key == KeyEvent.KEYCODE_4) {
986 return openConversationByIndex(3);
987 } else if (modifier && key == KeyEvent.KEYCODE_5) {
988 return openConversationByIndex(4);
989 } else if (modifier && key == KeyEvent.KEYCODE_6) {
990 return openConversationByIndex(5);
991 } else if (modifier && key == KeyEvent.KEYCODE_7) {
992 return openConversationByIndex(6);
993 } else if (modifier && key == KeyEvent.KEYCODE_8) {
994 return openConversationByIndex(7);
995 } else if (modifier && key == KeyEvent.KEYCODE_9) {
996 return openConversationByIndex(8);
997 } else if (modifier && key == KeyEvent.KEYCODE_0) {
998 return openConversationByIndex(9);
999 } else {
1000 return super.onKeyUp(key, event);
1001 }
1002 }
1003
1004 private void toggleConversationsOverview() {
1005 if (isConversationsOverviewVisable()) {
1006 hideConversationsOverview();
1007 if (mConversationFragment != null) {
1008 mConversationFragment.setFocusOnInputField();
1009 }
1010 } else {
1011 showConversationsOverview();
1012 }
1013 }
1014
1015 private boolean selectUpConversation() {
1016 if (this.mSelectedConversation != null) {
1017 int index = this.conversationList.indexOf(this.mSelectedConversation);
1018 if (index > 0) {
1019 return openConversationByIndex(index - 1);
1020 }
1021 }
1022 return false;
1023 }
1024
1025 private boolean selectDownConversation() {
1026 if (this.mSelectedConversation != null) {
1027 int index = this.conversationList.indexOf(this.mSelectedConversation);
1028 if (index != -1 && index < this.conversationList.size() - 1) {
1029 return openConversationByIndex(index + 1);
1030 }
1031 }
1032 return false;
1033 }
1034
1035 private boolean openConversationByIndex(int index) {
1036 try {
1037 this.conversationWasSelectedByKeyboard = true;
1038 setSelectedConversation(this.conversationList.get(index));
1039 this.mConversationFragment.reInit(getSelectedConversation());
1040 if (index > listView.getLastVisiblePosition() - 1 || index < listView.getFirstVisiblePosition() + 1) {
1041 this.listView.setSelection(index);
1042 }
1043 openConversation();
1044 return true;
1045 } catch (IndexOutOfBoundsException e) {
1046 return false;
1047 }
1048 }
1049
1050 @Override
1051 protected void onNewIntent(final Intent intent) {
1052 if (xmppConnectionServiceBound) {
1053 if (intent != null && VIEW_CONVERSATION.equals(intent.getType())) {
1054 handleViewConversationIntent(intent);
1055 setIntent(new Intent());
1056 }
1057 } else {
1058 setIntent(intent);
1059 }
1060 }
1061
1062 @Override
1063 public void onStart() {
1064 super.onStart();
1065 this.mRedirected.set(false);
1066 if (this.xmppConnectionServiceBound) {
1067 this.onBackendConnected();
1068 }
1069 if (conversationList.size() >= 1) {
1070 this.onConversationUpdate();
1071 }
1072 }
1073
1074 @Override
1075 public void onPause() {
1076 listView.discardUndo();
1077 super.onPause();
1078 this.mActivityPaused = true;
1079 if (this.xmppConnectionServiceBound) {
1080 this.xmppConnectionService.getNotificationService().setIsInForeground(false);
1081 }
1082 }
1083
1084 @Override
1085 public void onResume() {
1086 super.onResume();
1087 final int theme = findTheme();
1088 final boolean usingEnterKey = usingEnterKey();
1089 if (this.mTheme != theme || usingEnterKey != mUsingEnterKey) {
1090 recreate();
1091 }
1092 this.mActivityPaused = false;
1093 if (this.xmppConnectionServiceBound) {
1094 this.xmppConnectionService.getNotificationService().setIsInForeground(true);
1095 }
1096
1097 if (!isConversationsOverviewVisable() || !isConversationsOverviewHideable()) {
1098 sendReadMarkerIfNecessary(getSelectedConversation());
1099 }
1100
1101 }
1102
1103 @Override
1104 public void onSaveInstanceState(final Bundle savedInstanceState) {
1105 Conversation conversation = getSelectedConversation();
1106 if (conversation != null) {
1107 savedInstanceState.putString(STATE_OPEN_CONVERSATION,conversation.getUuid());
1108 } else {
1109 savedInstanceState.remove(STATE_OPEN_CONVERSATION);
1110 }
1111 savedInstanceState.putBoolean(STATE_PANEL_OPEN,isConversationsOverviewVisable());
1112 if (this.mPendingImageUris.size() >= 1) {
1113 savedInstanceState.putString(STATE_PENDING_URI, this.mPendingImageUris.get(0).toString());
1114 } else {
1115 savedInstanceState.remove(STATE_PENDING_URI);
1116 }
1117 super.onSaveInstanceState(savedInstanceState);
1118 }
1119
1120 private void clearPending() {
1121 mPendingImageUris.clear();
1122 mPendingFileUris.clear();
1123 mPendingGeoUri = null;
1124 mPostponedActivityResult = null;
1125 }
1126
1127 @Override
1128 void onBackendConnected() {
1129 this.xmppConnectionService.getNotificationService().setIsInForeground(true);
1130 updateConversationList();
1131
1132 if (mPendingConferenceInvite != null) {
1133 mPendingConferenceInvite.execute(this);
1134 mPendingConferenceInvite = null;
1135 }
1136
1137 if (xmppConnectionService.getAccounts().size() == 0) {
1138 if (mRedirected.compareAndSet(false,true)) {
1139 if (Config.X509_VERIFICATION) {
1140 startActivity(new Intent(this, ManageAccountActivity.class));
1141 } else {
1142 startActivity(new Intent(this, EditAccountActivity.class));
1143 }
1144 finish();
1145 }
1146 } else if (conversationList.size() <= 0) {
1147 if (mRedirected.compareAndSet(false,true)) {
1148 Intent intent = new Intent(this, StartConversationActivity.class);
1149 intent.putExtra("init",true);
1150 startActivity(intent);
1151 finish();
1152 }
1153 } else if (getIntent() != null && VIEW_CONVERSATION.equals(getIntent().getType())) {
1154 clearPending();
1155 handleViewConversationIntent(getIntent());
1156 } else if (selectConversationByUuid(mOpenConverstaion)) {
1157 if (mPanelOpen) {
1158 showConversationsOverview();
1159 } else {
1160 if (isConversationsOverviewHideable()) {
1161 openConversation();
1162 }
1163 }
1164 this.mConversationFragment.reInit(getSelectedConversation());
1165 mOpenConverstaion = null;
1166 } else if (getSelectedConversation() == null) {
1167 showConversationsOverview();
1168 clearPending();
1169 setSelectedConversation(conversationList.get(0));
1170 this.mConversationFragment.reInit(getSelectedConversation());
1171 } else {
1172 this.mConversationFragment.messageListAdapter.updatePreferences();
1173 this.mConversationFragment.messagesView.invalidateViews();
1174 this.mConversationFragment.setupIme();
1175 }
1176
1177 if (this.mPostponedActivityResult != null) {
1178 this.onActivityResult(mPostponedActivityResult.first, RESULT_OK, mPostponedActivityResult.second);
1179 }
1180
1181 if(!forbidProcessingPendings) {
1182 for (Iterator<Uri> i = mPendingImageUris.iterator(); i.hasNext(); i.remove()) {
1183 Uri foo = i.next();
1184 attachImageToConversation(getSelectedConversation(), foo);
1185 }
1186
1187 for (Iterator<Uri> i = mPendingFileUris.iterator(); i.hasNext(); i.remove()) {
1188 attachFileToConversation(getSelectedConversation(), i.next());
1189 }
1190
1191 if (mPendingGeoUri != null) {
1192 attachLocationToConversation(getSelectedConversation(), mPendingGeoUri);
1193 mPendingGeoUri = null;
1194 }
1195 }
1196 forbidProcessingPendings = false;
1197
1198 ExceptionHelper.checkForCrash(this, this.xmppConnectionService);
1199 setIntent(new Intent());
1200 }
1201
1202 private void handleViewConversationIntent(final Intent intent) {
1203 final String uuid = intent.getStringExtra(CONVERSATION);
1204 final String downloadUuid = intent.getStringExtra(MESSAGE);
1205 final String text = intent.getStringExtra(TEXT);
1206 final String nick = intent.getStringExtra(NICK);
1207 final boolean pm = intent.getBooleanExtra(PRIVATE_MESSAGE,false);
1208 if (selectConversationByUuid(uuid)) {
1209 this.mConversationFragment.reInit(getSelectedConversation());
1210 if (nick != null) {
1211 if (pm) {
1212 Jid jid = getSelectedConversation().getJid();
1213 try {
1214 Jid next = Jid.fromParts(jid.getLocalpart(),jid.getDomainpart(),nick);
1215 this.mConversationFragment.privateMessageWith(next);
1216 } catch (final InvalidJidException ignored) {
1217 //do nothing
1218 }
1219 } else {
1220 this.mConversationFragment.highlightInConference(nick);
1221 }
1222 } else {
1223 this.mConversationFragment.appendText(text);
1224 }
1225 hideConversationsOverview();
1226 openConversation();
1227 if (mContentView instanceof SlidingPaneLayout) {
1228 updateActionBarTitle(true); //fixes bug where slp isn't properly closed yet
1229 }
1230 if (downloadUuid != null) {
1231 final Message message = mSelectedConversation.findMessageWithFileAndUuid(downloadUuid);
1232 if (message != null) {
1233 startDownloadable(message);
1234 }
1235 }
1236 }
1237 }
1238
1239 private boolean selectConversationByUuid(String uuid) {
1240 if (uuid == null) {
1241 return false;
1242 }
1243 for (Conversation aConversationList : conversationList) {
1244 if (aConversationList.getUuid().equals(uuid)) {
1245 setSelectedConversation(aConversationList);
1246 return true;
1247 }
1248 }
1249 return false;
1250 }
1251
1252 @Override
1253 protected void unregisterListeners() {
1254 super.unregisterListeners();
1255 xmppConnectionService.getNotificationService().setOpenConversation(null);
1256 }
1257
1258 @SuppressLint("NewApi")
1259 private static List<Uri> extractUriFromIntent(final Intent intent) {
1260 List<Uri> uris = new ArrayList<>();
1261 Uri uri = intent.getData();
1262 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 && uri == null) {
1263 ClipData clipData = intent.getClipData();
1264 for(int i = 0; i < clipData.getItemCount(); ++i) {
1265 uris.add(clipData.getItemAt(i).getUri());
1266 }
1267 } else {
1268 uris.add(uri);
1269 }
1270 return uris;
1271 }
1272
1273 @Override
1274 protected void onActivityResult(int requestCode, int resultCode,
1275 final Intent data) {
1276 super.onActivityResult(requestCode, resultCode, data);
1277 if (resultCode == RESULT_OK) {
1278 if (requestCode == REQUEST_DECRYPT_PGP) {
1279 mConversationFragment.onActivityResult(requestCode, resultCode, data);
1280 } else if (requestCode == REQUEST_CHOOSE_PGP_ID) {
1281 if (xmppConnectionServiceBound) {
1282 if (data.getExtras().containsKey(OpenPgpApi.EXTRA_SIGN_KEY_ID)) {
1283 mSelectedConversation.getAccount().setPgpSignId(data.getExtras().getLong(OpenPgpApi.EXTRA_SIGN_KEY_ID));
1284 announcePgp(mSelectedConversation.getAccount(), null);
1285 } else {
1286 choosePgpSignId(mSelectedConversation.getAccount());
1287 }
1288 this.mPostponedActivityResult = null;
1289 } else {
1290 this.mPostponedActivityResult = new Pair<>(requestCode, data);
1291 }
1292 } else if (requestCode == REQUEST_ANNOUNCE_PGP) {
1293 if (xmppConnectionServiceBound) {
1294 announcePgp(mSelectedConversation.getAccount(), null);
1295 this.mPostponedActivityResult = null;
1296 } else {
1297 this.mPostponedActivityResult = new Pair<>(requestCode, data);
1298 }
1299 } else if (requestCode == ATTACHMENT_CHOICE_CHOOSE_IMAGE) {
1300 mPendingImageUris.clear();
1301 mPendingImageUris.addAll(extractUriFromIntent(data));
1302 if (xmppConnectionServiceBound) {
1303 for(Iterator<Uri> i = mPendingImageUris.iterator(); i.hasNext(); i.remove()) {
1304 attachImageToConversation(getSelectedConversation(),i.next());
1305 }
1306 }
1307 } else if (requestCode == ATTACHMENT_CHOICE_CHOOSE_FILE || requestCode == ATTACHMENT_CHOICE_RECORD_VOICE) {
1308 mPendingFileUris.clear();
1309 mPendingFileUris.addAll(extractUriFromIntent(data));
1310 if (xmppConnectionServiceBound) {
1311 for(Iterator<Uri> i = mPendingFileUris.iterator(); i.hasNext(); i.remove()) {
1312 attachFileToConversation(getSelectedConversation(), i.next());
1313 }
1314 }
1315 } else if (requestCode == ATTACHMENT_CHOICE_TAKE_PHOTO) {
1316 if (mPendingImageUris.size() == 1) {
1317 Uri uri = mPendingImageUris.get(0);
1318 if (xmppConnectionServiceBound) {
1319 attachImageToConversation(getSelectedConversation(), uri);
1320 mPendingImageUris.clear();
1321 }
1322 Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
1323 intent.setData(uri);
1324 sendBroadcast(intent);
1325 } else {
1326 mPendingImageUris.clear();
1327 }
1328 } else if (requestCode == ATTACHMENT_CHOICE_LOCATION) {
1329 double latitude = data.getDoubleExtra("latitude",0);
1330 double longitude = data.getDoubleExtra("longitude",0);
1331 this.mPendingGeoUri = Uri.parse("geo:"+String.valueOf(latitude)+","+String.valueOf(longitude));
1332 if (xmppConnectionServiceBound) {
1333 attachLocationToConversation(getSelectedConversation(), mPendingGeoUri);
1334 this.mPendingGeoUri = null;
1335 }
1336 } else if (requestCode == REQUEST_TRUST_KEYS_TEXT || requestCode == REQUEST_TRUST_KEYS_MENU) {
1337 this.forbidProcessingPendings = !xmppConnectionServiceBound;
1338 mConversationFragment.onActivityResult(requestCode, resultCode, data);
1339 }
1340 } else {
1341 mPendingImageUris.clear();
1342 mPendingFileUris.clear();
1343 if (requestCode == ConversationActivity.REQUEST_DECRYPT_PGP) {
1344 mConversationFragment.onActivityResult(requestCode, resultCode, data);
1345 }
1346 }
1347 }
1348
1349 private void attachLocationToConversation(Conversation conversation, Uri uri) {
1350 if (conversation == null) {
1351 return;
1352 }
1353 xmppConnectionService.attachLocationToConversation(conversation,uri, new UiCallback<Message>() {
1354
1355 @Override
1356 public void success(Message message) {
1357 xmppConnectionService.sendMessage(message);
1358 }
1359
1360 @Override
1361 public void error(int errorCode, Message object) {
1362
1363 }
1364
1365 @Override
1366 public void userInputRequried(PendingIntent pi, Message object) {
1367
1368 }
1369 });
1370 }
1371
1372 private void attachFileToConversation(Conversation conversation, Uri uri) {
1373 if (conversation == null) {
1374 return;
1375 }
1376 prepareFileToast = Toast.makeText(getApplicationContext(),getText(R.string.preparing_file), Toast.LENGTH_LONG);
1377 prepareFileToast.show();
1378 xmppConnectionService.attachFileToConversation(conversation, uri, new UiCallback<Message>() {
1379 @Override
1380 public void success(Message message) {
1381 hidePrepareFileToast();
1382 xmppConnectionService.sendMessage(message);
1383 }
1384
1385 @Override
1386 public void error(int errorCode, Message message) {
1387 displayErrorDialog(errorCode);
1388 }
1389
1390 @Override
1391 public void userInputRequried(PendingIntent pi, Message message) {
1392
1393 }
1394 });
1395 }
1396
1397 private void attachImageToConversation(Conversation conversation, Uri uri) {
1398 if (conversation == null) {
1399 return;
1400 }
1401 prepareFileToast = Toast.makeText(getApplicationContext(),getText(R.string.preparing_image), Toast.LENGTH_LONG);
1402 prepareFileToast.show();
1403 xmppConnectionService.attachImageToConversation(conversation, uri,
1404 new UiCallback<Message>() {
1405
1406 @Override
1407 public void userInputRequried(PendingIntent pi,
1408 Message object) {
1409 hidePrepareFileToast();
1410 }
1411
1412 @Override
1413 public void success(Message message) {
1414 hidePrepareFileToast();
1415 xmppConnectionService.sendMessage(message);
1416 }
1417
1418 @Override
1419 public void error(int error, Message message) {
1420 hidePrepareFileToast();
1421 displayErrorDialog(error);
1422 }
1423 });
1424 }
1425
1426 private void hidePrepareFileToast() {
1427 if (prepareFileToast != null) {
1428 runOnUiThread(new Runnable() {
1429
1430 @Override
1431 public void run() {
1432 prepareFileToast.cancel();
1433 }
1434 });
1435 }
1436 }
1437
1438 public void updateConversationList() {
1439 xmppConnectionService
1440 .populateWithOrderedConversations(conversationList);
1441 if (swipedConversation != null) {
1442 if (swipedConversation.isRead()) {
1443 conversationList.remove(swipedConversation);
1444 } else {
1445 listView.discardUndo();
1446 }
1447 }
1448 listAdapter.notifyDataSetChanged();
1449 }
1450
1451 public void runIntent(PendingIntent pi, int requestCode) {
1452 try {
1453 this.startIntentSenderForResult(pi.getIntentSender(), requestCode,
1454 null, 0, 0, 0);
1455 } catch (final SendIntentException ignored) {
1456 }
1457 }
1458
1459 public void encryptTextMessage(Message message) {
1460 xmppConnectionService.getPgpEngine().encrypt(message,
1461 new UiCallback<Message>() {
1462
1463 @Override
1464 public void userInputRequried(PendingIntent pi,
1465 Message message) {
1466 ConversationActivity.this.runIntent(pi,
1467 ConversationActivity.REQUEST_SEND_MESSAGE);
1468 }
1469
1470 @Override
1471 public void success(Message message) {
1472 message.setEncryption(Message.ENCRYPTION_DECRYPTED);
1473 xmppConnectionService.sendMessage(message);
1474 }
1475
1476 @Override
1477 public void error(int error, Message message) {
1478
1479 }
1480 });
1481 }
1482
1483 public boolean useSendButtonToIndicateStatus() {
1484 return getPreferences().getBoolean("send_button_status", false);
1485 }
1486
1487 public boolean indicateReceived() {
1488 return getPreferences().getBoolean("indicate_received", false);
1489 }
1490
1491 public boolean useWhiteBackground() {
1492 return getPreferences().getBoolean("use_white_background",false);
1493 }
1494
1495 protected boolean trustKeysIfNeeded(int requestCode) {
1496 return trustKeysIfNeeded(requestCode, ATTACHMENT_CHOICE_INVALID);
1497 }
1498
1499 protected boolean trustKeysIfNeeded(int requestCode, int attachmentChoice) {
1500 AxolotlService axolotlService = mSelectedConversation.getAccount().getAxolotlService();
1501 boolean hasPendingKeys = !axolotlService.getKeysWithTrust(XmppAxolotlSession.Trust.UNDECIDED,
1502 mSelectedConversation.getContact()).isEmpty()
1503 || !axolotlService.findDevicesWithoutSession(mSelectedConversation).isEmpty();
1504 boolean hasNoTrustedKeys = axolotlService.getNumTrustedKeys(mSelectedConversation.getContact()) == 0;
1505 if( hasPendingKeys || hasNoTrustedKeys) {
1506 axolotlService.createSessionsIfNeeded(mSelectedConversation);
1507 Intent intent = new Intent(getApplicationContext(), TrustKeysActivity.class);
1508 intent.putExtra("contact", mSelectedConversation.getContact().getJid().toBareJid().toString());
1509 intent.putExtra("account", mSelectedConversation.getAccount().getJid().toBareJid().toString());
1510 intent.putExtra("choice", attachmentChoice);
1511 intent.putExtra("has_no_trusted", hasNoTrustedKeys);
1512 startActivityForResult(intent, requestCode);
1513 return true;
1514 } else {
1515 return false;
1516 }
1517 }
1518
1519 @Override
1520 protected void refreshUiReal() {
1521 updateConversationList();
1522 if (conversationList.size() > 0) {
1523 ConversationActivity.this.mConversationFragment.updateMessages();
1524 updateActionBarTitle();
1525 invalidateOptionsMenu();
1526 }
1527 }
1528
1529 @Override
1530 public void onAccountUpdate() {
1531 this.refreshUi();
1532 }
1533
1534 @Override
1535 public void onConversationUpdate() {
1536 this.refreshUi();
1537 }
1538
1539 @Override
1540 public void onRosterUpdate() {
1541 this.refreshUi();
1542 }
1543
1544 @Override
1545 public void OnUpdateBlocklist(Status status) {
1546 this.refreshUi();
1547 }
1548
1549 public void unblockConversation(final Blockable conversation) {
1550 xmppConnectionService.sendUnblockRequest(conversation);
1551 }
1552
1553 public boolean enterIsSend() {
1554 return getPreferences().getBoolean("enter_is_send",false);
1555 }
1556
1557 @Override
1558 public void onShowErrorToast(final int resId) {
1559 runOnUiThread(new Runnable() {
1560 @Override
1561 public void run() {
1562 Toast.makeText(ConversationActivity.this,resId,Toast.LENGTH_SHORT).show();
1563 }
1564 });
1565 }
1566
1567 public boolean highlightSelectedConversations() {
1568 return !isConversationsOverviewHideable() || this.conversationWasSelectedByKeyboard;
1569 }
1570}