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 && !Config.X509_VERIFICATION); //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 && !Config.X509_VERIFICATION);
872 none.setVisible(!Config.FORCE_ENCRYPTION);
873 otr.setVisible(!Config.X509_VERIFICATION);
874 if (conversation.getMode() == Conversation.MODE_MULTI) {
875 otr.setVisible(false);
876 axolotl.setVisible(false);
877 } else if (!conversation.getAccount().getAxolotlService().isContactAxolotlCapable(conversation.getContact())) {
878 axolotl.setEnabled(false);
879 }
880 switch (conversation.getNextEncryption()) {
881 case Message.ENCRYPTION_NONE:
882 none.setChecked(true);
883 break;
884 case Message.ENCRYPTION_OTR:
885 otr.setChecked(true);
886 break;
887 case Message.ENCRYPTION_PGP:
888 pgp.setChecked(true);
889 break;
890 case Message.ENCRYPTION_AXOLOTL:
891 axolotl.setChecked(true);
892 break;
893 default:
894 none.setChecked(true);
895 break;
896 }
897 popup.show();
898 }
899 }
900
901 protected void muteConversationDialog(final Conversation conversation) {
902 AlertDialog.Builder builder = new AlertDialog.Builder(this);
903 builder.setTitle(R.string.disable_notifications);
904 final int[] durations = getResources().getIntArray(R.array.mute_options_durations);
905 builder.setItems(R.array.mute_options_descriptions,
906 new OnClickListener() {
907
908 @Override
909 public void onClick(final DialogInterface dialog, final int which) {
910 final long till;
911 if (durations[which] == -1) {
912 till = Long.MAX_VALUE;
913 } else {
914 till = System.currentTimeMillis() + (durations[which] * 1000);
915 }
916 conversation.setMutedTill(till);
917 ConversationActivity.this.xmppConnectionService.databaseBackend
918 .updateConversation(conversation);
919 updateConversationList();
920 ConversationActivity.this.mConversationFragment.updateMessages();
921 invalidateOptionsMenu();
922 }
923 });
924 builder.create().show();
925 }
926
927 public void unmuteConversation(final Conversation conversation) {
928 conversation.setMutedTill(0);
929 this.xmppConnectionService.databaseBackend.updateConversation(conversation);
930 updateConversationList();
931 ConversationActivity.this.mConversationFragment.updateMessages();
932 invalidateOptionsMenu();
933 }
934
935 @Override
936 public void onBackPressed() {
937 if (!isConversationsOverviewVisable()) {
938 showConversationsOverview();
939 } else {
940 moveTaskToBack(true);
941 }
942 }
943
944 @Override
945 public boolean onKeyUp(int key, KeyEvent event) {
946 int rotation = getWindowManager().getDefaultDisplay().getRotation();
947 final int upKey;
948 final int downKey;
949 switch(rotation) {
950 case Surface.ROTATION_90:
951 upKey = KeyEvent.KEYCODE_DPAD_LEFT;
952 downKey = KeyEvent.KEYCODE_DPAD_RIGHT;
953 break;
954 case Surface.ROTATION_180:
955 upKey = KeyEvent.KEYCODE_DPAD_DOWN;
956 downKey = KeyEvent.KEYCODE_DPAD_UP;
957 break;
958 case Surface.ROTATION_270:
959 upKey = KeyEvent.KEYCODE_DPAD_RIGHT;
960 downKey = KeyEvent.KEYCODE_DPAD_LEFT;
961 break;
962 default:
963 upKey = KeyEvent.KEYCODE_DPAD_UP;
964 downKey = KeyEvent.KEYCODE_DPAD_DOWN;
965 }
966 final boolean modifier = event.isCtrlPressed() || event.isAltPressed();
967 if (modifier && key == KeyEvent.KEYCODE_TAB && isConversationsOverviewHideable()) {
968 toggleConversationsOverview();
969 return true;
970 } else if (modifier && key == downKey) {
971 if (isConversationsOverviewHideable() && !isConversationsOverviewVisable()) {
972 showConversationsOverview();;
973 }
974 return selectDownConversation();
975 } else if (modifier && key == upKey) {
976 if (isConversationsOverviewHideable() && !isConversationsOverviewVisable()) {
977 showConversationsOverview();
978 }
979 return selectUpConversation();
980 } else if (modifier && key == KeyEvent.KEYCODE_1) {
981 return openConversationByIndex(0);
982 } else if (modifier && key == KeyEvent.KEYCODE_2) {
983 return openConversationByIndex(1);
984 } else if (modifier && key == KeyEvent.KEYCODE_3) {
985 return openConversationByIndex(2);
986 } else if (modifier && key == KeyEvent.KEYCODE_4) {
987 return openConversationByIndex(3);
988 } else if (modifier && key == KeyEvent.KEYCODE_5) {
989 return openConversationByIndex(4);
990 } else if (modifier && key == KeyEvent.KEYCODE_6) {
991 return openConversationByIndex(5);
992 } else if (modifier && key == KeyEvent.KEYCODE_7) {
993 return openConversationByIndex(6);
994 } else if (modifier && key == KeyEvent.KEYCODE_8) {
995 return openConversationByIndex(7);
996 } else if (modifier && key == KeyEvent.KEYCODE_9) {
997 return openConversationByIndex(8);
998 } else if (modifier && key == KeyEvent.KEYCODE_0) {
999 return openConversationByIndex(9);
1000 } else {
1001 return super.onKeyUp(key, event);
1002 }
1003 }
1004
1005 private void toggleConversationsOverview() {
1006 if (isConversationsOverviewVisable()) {
1007 hideConversationsOverview();
1008 if (mConversationFragment != null) {
1009 mConversationFragment.setFocusOnInputField();
1010 }
1011 } else {
1012 showConversationsOverview();
1013 }
1014 }
1015
1016 private boolean selectUpConversation() {
1017 if (this.mSelectedConversation != null) {
1018 int index = this.conversationList.indexOf(this.mSelectedConversation);
1019 if (index > 0) {
1020 return openConversationByIndex(index - 1);
1021 }
1022 }
1023 return false;
1024 }
1025
1026 private boolean selectDownConversation() {
1027 if (this.mSelectedConversation != null) {
1028 int index = this.conversationList.indexOf(this.mSelectedConversation);
1029 if (index != -1 && index < this.conversationList.size() - 1) {
1030 return openConversationByIndex(index + 1);
1031 }
1032 }
1033 return false;
1034 }
1035
1036 private boolean openConversationByIndex(int index) {
1037 try {
1038 this.conversationWasSelectedByKeyboard = true;
1039 setSelectedConversation(this.conversationList.get(index));
1040 this.mConversationFragment.reInit(getSelectedConversation());
1041 if (index > listView.getLastVisiblePosition() - 1 || index < listView.getFirstVisiblePosition() + 1) {
1042 this.listView.setSelection(index);
1043 }
1044 openConversation();
1045 return true;
1046 } catch (IndexOutOfBoundsException e) {
1047 return false;
1048 }
1049 }
1050
1051 @Override
1052 protected void onNewIntent(final Intent intent) {
1053 if (xmppConnectionServiceBound) {
1054 if (intent != null && VIEW_CONVERSATION.equals(intent.getType())) {
1055 handleViewConversationIntent(intent);
1056 setIntent(new Intent());
1057 }
1058 } else {
1059 setIntent(intent);
1060 }
1061 }
1062
1063 @Override
1064 public void onStart() {
1065 super.onStart();
1066 this.mRedirected.set(false);
1067 if (this.xmppConnectionServiceBound) {
1068 this.onBackendConnected();
1069 }
1070 if (conversationList.size() >= 1) {
1071 this.onConversationUpdate();
1072 }
1073 }
1074
1075 @Override
1076 public void onPause() {
1077 listView.discardUndo();
1078 super.onPause();
1079 this.mActivityPaused = true;
1080 if (this.xmppConnectionServiceBound) {
1081 this.xmppConnectionService.getNotificationService().setIsInForeground(false);
1082 }
1083 }
1084
1085 @Override
1086 public void onResume() {
1087 super.onResume();
1088 final int theme = findTheme();
1089 final boolean usingEnterKey = usingEnterKey();
1090 if (this.mTheme != theme || usingEnterKey != mUsingEnterKey) {
1091 recreate();
1092 }
1093 this.mActivityPaused = false;
1094 if (this.xmppConnectionServiceBound) {
1095 this.xmppConnectionService.getNotificationService().setIsInForeground(true);
1096 }
1097
1098 if (!isConversationsOverviewVisable() || !isConversationsOverviewHideable()) {
1099 sendReadMarkerIfNecessary(getSelectedConversation());
1100 }
1101
1102 }
1103
1104 @Override
1105 public void onSaveInstanceState(final Bundle savedInstanceState) {
1106 Conversation conversation = getSelectedConversation();
1107 if (conversation != null) {
1108 savedInstanceState.putString(STATE_OPEN_CONVERSATION,conversation.getUuid());
1109 } else {
1110 savedInstanceState.remove(STATE_OPEN_CONVERSATION);
1111 }
1112 savedInstanceState.putBoolean(STATE_PANEL_OPEN,isConversationsOverviewVisable());
1113 if (this.mPendingImageUris.size() >= 1) {
1114 savedInstanceState.putString(STATE_PENDING_URI, this.mPendingImageUris.get(0).toString());
1115 } else {
1116 savedInstanceState.remove(STATE_PENDING_URI);
1117 }
1118 super.onSaveInstanceState(savedInstanceState);
1119 }
1120
1121 private void clearPending() {
1122 mPendingImageUris.clear();
1123 mPendingFileUris.clear();
1124 mPendingGeoUri = null;
1125 mPostponedActivityResult = null;
1126 }
1127
1128 @Override
1129 void onBackendConnected() {
1130 this.xmppConnectionService.getNotificationService().setIsInForeground(true);
1131 updateConversationList();
1132
1133 if (mPendingConferenceInvite != null) {
1134 mPendingConferenceInvite.execute(this);
1135 mPendingConferenceInvite = null;
1136 }
1137
1138 if (xmppConnectionService.getAccounts().size() == 0) {
1139 if (mRedirected.compareAndSet(false,true)) {
1140 if (Config.X509_VERIFICATION) {
1141 startActivity(new Intent(this, ManageAccountActivity.class));
1142 } else {
1143 startActivity(new Intent(this, EditAccountActivity.class));
1144 }
1145 finish();
1146 }
1147 } else if (conversationList.size() <= 0) {
1148 if (mRedirected.compareAndSet(false,true)) {
1149 Intent intent = new Intent(this, StartConversationActivity.class);
1150 intent.putExtra("init",true);
1151 startActivity(intent);
1152 finish();
1153 }
1154 } else if (getIntent() != null && VIEW_CONVERSATION.equals(getIntent().getType())) {
1155 clearPending();
1156 handleViewConversationIntent(getIntent());
1157 } else if (selectConversationByUuid(mOpenConverstaion)) {
1158 if (mPanelOpen) {
1159 showConversationsOverview();
1160 } else {
1161 if (isConversationsOverviewHideable()) {
1162 openConversation();
1163 }
1164 }
1165 this.mConversationFragment.reInit(getSelectedConversation());
1166 mOpenConverstaion = null;
1167 } else if (getSelectedConversation() == null) {
1168 showConversationsOverview();
1169 clearPending();
1170 setSelectedConversation(conversationList.get(0));
1171 this.mConversationFragment.reInit(getSelectedConversation());
1172 } else {
1173 this.mConversationFragment.messageListAdapter.updatePreferences();
1174 this.mConversationFragment.messagesView.invalidateViews();
1175 this.mConversationFragment.setupIme();
1176 }
1177
1178 if (this.mPostponedActivityResult != null) {
1179 this.onActivityResult(mPostponedActivityResult.first, RESULT_OK, mPostponedActivityResult.second);
1180 }
1181
1182 if(!forbidProcessingPendings) {
1183 for (Iterator<Uri> i = mPendingImageUris.iterator(); i.hasNext(); i.remove()) {
1184 Uri foo = i.next();
1185 attachImageToConversation(getSelectedConversation(), foo);
1186 }
1187
1188 for (Iterator<Uri> i = mPendingFileUris.iterator(); i.hasNext(); i.remove()) {
1189 attachFileToConversation(getSelectedConversation(), i.next());
1190 }
1191
1192 if (mPendingGeoUri != null) {
1193 attachLocationToConversation(getSelectedConversation(), mPendingGeoUri);
1194 mPendingGeoUri = null;
1195 }
1196 }
1197 forbidProcessingPendings = false;
1198
1199 ExceptionHelper.checkForCrash(this, this.xmppConnectionService);
1200 setIntent(new Intent());
1201 }
1202
1203 private void handleViewConversationIntent(final Intent intent) {
1204 final String uuid = intent.getStringExtra(CONVERSATION);
1205 final String downloadUuid = intent.getStringExtra(MESSAGE);
1206 final String text = intent.getStringExtra(TEXT);
1207 final String nick = intent.getStringExtra(NICK);
1208 final boolean pm = intent.getBooleanExtra(PRIVATE_MESSAGE,false);
1209 if (selectConversationByUuid(uuid)) {
1210 this.mConversationFragment.reInit(getSelectedConversation());
1211 if (nick != null) {
1212 if (pm) {
1213 Jid jid = getSelectedConversation().getJid();
1214 try {
1215 Jid next = Jid.fromParts(jid.getLocalpart(),jid.getDomainpart(),nick);
1216 this.mConversationFragment.privateMessageWith(next);
1217 } catch (final InvalidJidException ignored) {
1218 //do nothing
1219 }
1220 } else {
1221 this.mConversationFragment.highlightInConference(nick);
1222 }
1223 } else {
1224 this.mConversationFragment.appendText(text);
1225 }
1226 hideConversationsOverview();
1227 openConversation();
1228 if (mContentView instanceof SlidingPaneLayout) {
1229 updateActionBarTitle(true); //fixes bug where slp isn't properly closed yet
1230 }
1231 if (downloadUuid != null) {
1232 final Message message = mSelectedConversation.findMessageWithFileAndUuid(downloadUuid);
1233 if (message != null) {
1234 startDownloadable(message);
1235 }
1236 }
1237 }
1238 }
1239
1240 private boolean selectConversationByUuid(String uuid) {
1241 if (uuid == null) {
1242 return false;
1243 }
1244 for (Conversation aConversationList : conversationList) {
1245 if (aConversationList.getUuid().equals(uuid)) {
1246 setSelectedConversation(aConversationList);
1247 return true;
1248 }
1249 }
1250 return false;
1251 }
1252
1253 @Override
1254 protected void unregisterListeners() {
1255 super.unregisterListeners();
1256 xmppConnectionService.getNotificationService().setOpenConversation(null);
1257 }
1258
1259 @SuppressLint("NewApi")
1260 private static List<Uri> extractUriFromIntent(final Intent intent) {
1261 List<Uri> uris = new ArrayList<>();
1262 Uri uri = intent.getData();
1263 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 && uri == null) {
1264 ClipData clipData = intent.getClipData();
1265 for(int i = 0; i < clipData.getItemCount(); ++i) {
1266 uris.add(clipData.getItemAt(i).getUri());
1267 }
1268 } else {
1269 uris.add(uri);
1270 }
1271 return uris;
1272 }
1273
1274 @Override
1275 protected void onActivityResult(int requestCode, int resultCode,
1276 final Intent data) {
1277 super.onActivityResult(requestCode, resultCode, data);
1278 if (resultCode == RESULT_OK) {
1279 if (requestCode == REQUEST_DECRYPT_PGP) {
1280 mConversationFragment.onActivityResult(requestCode, resultCode, data);
1281 } else if (requestCode == REQUEST_CHOOSE_PGP_ID) {
1282 if (xmppConnectionServiceBound) {
1283 if (data.getExtras().containsKey(OpenPgpApi.EXTRA_SIGN_KEY_ID)) {
1284 mSelectedConversation.getAccount().setPgpSignId(data.getExtras().getLong(OpenPgpApi.EXTRA_SIGN_KEY_ID));
1285 announcePgp(mSelectedConversation.getAccount(), null);
1286 } else {
1287 choosePgpSignId(mSelectedConversation.getAccount());
1288 }
1289 this.mPostponedActivityResult = null;
1290 } else {
1291 this.mPostponedActivityResult = new Pair<>(requestCode, data);
1292 }
1293 } else if (requestCode == REQUEST_ANNOUNCE_PGP) {
1294 if (xmppConnectionServiceBound) {
1295 announcePgp(mSelectedConversation.getAccount(), null);
1296 this.mPostponedActivityResult = null;
1297 } else {
1298 this.mPostponedActivityResult = new Pair<>(requestCode, data);
1299 }
1300 } else if (requestCode == ATTACHMENT_CHOICE_CHOOSE_IMAGE) {
1301 mPendingImageUris.clear();
1302 mPendingImageUris.addAll(extractUriFromIntent(data));
1303 if (xmppConnectionServiceBound) {
1304 for(Iterator<Uri> i = mPendingImageUris.iterator(); i.hasNext(); i.remove()) {
1305 attachImageToConversation(getSelectedConversation(),i.next());
1306 }
1307 }
1308 } else if (requestCode == ATTACHMENT_CHOICE_CHOOSE_FILE || requestCode == ATTACHMENT_CHOICE_RECORD_VOICE) {
1309 mPendingFileUris.clear();
1310 mPendingFileUris.addAll(extractUriFromIntent(data));
1311 if (xmppConnectionServiceBound) {
1312 for(Iterator<Uri> i = mPendingFileUris.iterator(); i.hasNext(); i.remove()) {
1313 attachFileToConversation(getSelectedConversation(), i.next());
1314 }
1315 }
1316 } else if (requestCode == ATTACHMENT_CHOICE_TAKE_PHOTO) {
1317 if (mPendingImageUris.size() == 1) {
1318 Uri uri = mPendingImageUris.get(0);
1319 if (xmppConnectionServiceBound) {
1320 attachImageToConversation(getSelectedConversation(), uri);
1321 mPendingImageUris.clear();
1322 }
1323 Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
1324 intent.setData(uri);
1325 sendBroadcast(intent);
1326 } else {
1327 mPendingImageUris.clear();
1328 }
1329 } else if (requestCode == ATTACHMENT_CHOICE_LOCATION) {
1330 double latitude = data.getDoubleExtra("latitude",0);
1331 double longitude = data.getDoubleExtra("longitude",0);
1332 this.mPendingGeoUri = Uri.parse("geo:"+String.valueOf(latitude)+","+String.valueOf(longitude));
1333 if (xmppConnectionServiceBound) {
1334 attachLocationToConversation(getSelectedConversation(), mPendingGeoUri);
1335 this.mPendingGeoUri = null;
1336 }
1337 } else if (requestCode == REQUEST_TRUST_KEYS_TEXT || requestCode == REQUEST_TRUST_KEYS_MENU) {
1338 this.forbidProcessingPendings = !xmppConnectionServiceBound;
1339 mConversationFragment.onActivityResult(requestCode, resultCode, data);
1340 }
1341 } else {
1342 mPendingImageUris.clear();
1343 mPendingFileUris.clear();
1344 if (requestCode == ConversationActivity.REQUEST_DECRYPT_PGP) {
1345 mConversationFragment.onActivityResult(requestCode, resultCode, data);
1346 }
1347 }
1348 }
1349
1350 private void attachLocationToConversation(Conversation conversation, Uri uri) {
1351 if (conversation == null) {
1352 return;
1353 }
1354 xmppConnectionService.attachLocationToConversation(conversation,uri, new UiCallback<Message>() {
1355
1356 @Override
1357 public void success(Message message) {
1358 xmppConnectionService.sendMessage(message);
1359 }
1360
1361 @Override
1362 public void error(int errorCode, Message object) {
1363
1364 }
1365
1366 @Override
1367 public void userInputRequried(PendingIntent pi, Message object) {
1368
1369 }
1370 });
1371 }
1372
1373 private void attachFileToConversation(Conversation conversation, Uri uri) {
1374 if (conversation == null) {
1375 return;
1376 }
1377 prepareFileToast = Toast.makeText(getApplicationContext(),getText(R.string.preparing_file), Toast.LENGTH_LONG);
1378 prepareFileToast.show();
1379 xmppConnectionService.attachFileToConversation(conversation, uri, new UiCallback<Message>() {
1380 @Override
1381 public void success(Message message) {
1382 hidePrepareFileToast();
1383 xmppConnectionService.sendMessage(message);
1384 }
1385
1386 @Override
1387 public void error(int errorCode, Message message) {
1388 displayErrorDialog(errorCode);
1389 }
1390
1391 @Override
1392 public void userInputRequried(PendingIntent pi, Message message) {
1393
1394 }
1395 });
1396 }
1397
1398 private void attachImageToConversation(Conversation conversation, Uri uri) {
1399 if (conversation == null) {
1400 return;
1401 }
1402 prepareFileToast = Toast.makeText(getApplicationContext(),getText(R.string.preparing_image), Toast.LENGTH_LONG);
1403 prepareFileToast.show();
1404 xmppConnectionService.attachImageToConversation(conversation, uri,
1405 new UiCallback<Message>() {
1406
1407 @Override
1408 public void userInputRequried(PendingIntent pi,
1409 Message object) {
1410 hidePrepareFileToast();
1411 }
1412
1413 @Override
1414 public void success(Message message) {
1415 hidePrepareFileToast();
1416 xmppConnectionService.sendMessage(message);
1417 }
1418
1419 @Override
1420 public void error(int error, Message message) {
1421 hidePrepareFileToast();
1422 displayErrorDialog(error);
1423 }
1424 });
1425 }
1426
1427 private void hidePrepareFileToast() {
1428 if (prepareFileToast != null) {
1429 runOnUiThread(new Runnable() {
1430
1431 @Override
1432 public void run() {
1433 prepareFileToast.cancel();
1434 }
1435 });
1436 }
1437 }
1438
1439 public void updateConversationList() {
1440 xmppConnectionService
1441 .populateWithOrderedConversations(conversationList);
1442 if (swipedConversation != null) {
1443 if (swipedConversation.isRead()) {
1444 conversationList.remove(swipedConversation);
1445 } else {
1446 listView.discardUndo();
1447 }
1448 }
1449 listAdapter.notifyDataSetChanged();
1450 }
1451
1452 public void runIntent(PendingIntent pi, int requestCode) {
1453 try {
1454 this.startIntentSenderForResult(pi.getIntentSender(), requestCode,
1455 null, 0, 0, 0);
1456 } catch (final SendIntentException ignored) {
1457 }
1458 }
1459
1460 public void encryptTextMessage(Message message) {
1461 xmppConnectionService.getPgpEngine().encrypt(message,
1462 new UiCallback<Message>() {
1463
1464 @Override
1465 public void userInputRequried(PendingIntent pi,
1466 Message message) {
1467 ConversationActivity.this.runIntent(pi,
1468 ConversationActivity.REQUEST_SEND_MESSAGE);
1469 }
1470
1471 @Override
1472 public void success(Message message) {
1473 message.setEncryption(Message.ENCRYPTION_DECRYPTED);
1474 xmppConnectionService.sendMessage(message);
1475 }
1476
1477 @Override
1478 public void error(int error, Message message) {
1479
1480 }
1481 });
1482 }
1483
1484 public boolean useSendButtonToIndicateStatus() {
1485 return getPreferences().getBoolean("send_button_status", false);
1486 }
1487
1488 public boolean indicateReceived() {
1489 return getPreferences().getBoolean("indicate_received", false);
1490 }
1491
1492 public boolean useWhiteBackground() {
1493 return getPreferences().getBoolean("use_white_background",false);
1494 }
1495
1496 protected boolean trustKeysIfNeeded(int requestCode) {
1497 return trustKeysIfNeeded(requestCode, ATTACHMENT_CHOICE_INVALID);
1498 }
1499
1500 protected boolean trustKeysIfNeeded(int requestCode, int attachmentChoice) {
1501 AxolotlService axolotlService = mSelectedConversation.getAccount().getAxolotlService();
1502 Contact contact = mSelectedConversation.getContact();
1503 boolean hasUndecidedOwn = !axolotlService.getKeysWithTrust(XmppAxolotlSession.Trust.UNDECIDED).isEmpty();
1504 boolean hasUndecidedContact = !axolotlService.getKeysWithTrust(XmppAxolotlSession.Trust.UNDECIDED,contact).isEmpty();
1505 boolean hasPendingKeys = !axolotlService.findDevicesWithoutSession(mSelectedConversation).isEmpty();
1506 boolean hasNoTrustedKeys = axolotlService.getNumTrustedKeys(mSelectedConversation.getContact()) == 0;
1507 if(hasUndecidedOwn || hasUndecidedContact || hasPendingKeys || hasNoTrustedKeys) {
1508 axolotlService.createSessionsIfNeeded(mSelectedConversation);
1509 Intent intent = new Intent(getApplicationContext(), TrustKeysActivity.class);
1510 intent.putExtra("contact", mSelectedConversation.getContact().getJid().toBareJid().toString());
1511 intent.putExtra("account", mSelectedConversation.getAccount().getJid().toBareJid().toString());
1512 intent.putExtra("choice", attachmentChoice);
1513 intent.putExtra("has_no_trusted", hasNoTrustedKeys);
1514 startActivityForResult(intent, requestCode);
1515 return true;
1516 } else {
1517 return false;
1518 }
1519 }
1520
1521 @Override
1522 protected void refreshUiReal() {
1523 updateConversationList();
1524 if (conversationList.size() > 0) {
1525 ConversationActivity.this.mConversationFragment.updateMessages();
1526 updateActionBarTitle();
1527 invalidateOptionsMenu();
1528 }
1529 }
1530
1531 @Override
1532 public void onAccountUpdate() {
1533 this.refreshUi();
1534 }
1535
1536 @Override
1537 public void onConversationUpdate() {
1538 this.refreshUi();
1539 }
1540
1541 @Override
1542 public void onRosterUpdate() {
1543 this.refreshUi();
1544 }
1545
1546 @Override
1547 public void OnUpdateBlocklist(Status status) {
1548 this.refreshUi();
1549 }
1550
1551 public void unblockConversation(final Blockable conversation) {
1552 xmppConnectionService.sendUnblockRequest(conversation);
1553 }
1554
1555 public boolean enterIsSend() {
1556 return getPreferences().getBoolean("enter_is_send",false);
1557 }
1558
1559 @Override
1560 public void onShowErrorToast(final int resId) {
1561 runOnUiThread(new Runnable() {
1562 @Override
1563 public void run() {
1564 Toast.makeText(ConversationActivity.this,resId,Toast.LENGTH_SHORT).show();
1565 }
1566 });
1567 }
1568
1569 public boolean highlightSelectedConversations() {
1570 return !isConversationsOverviewHideable() || this.conversationWasSelectedByKeyboard;
1571 }
1572}