1package eu.siacs.conversations.ui;
2
3import android.annotation.SuppressLint;
4import android.app.ActionBar;
5import android.app.AlertDialog;
6import android.app.FragmentTransaction;
7import android.app.PendingIntent;
8import android.content.DialogInterface;
9import android.content.DialogInterface.OnClickListener;
10import android.content.Intent;
11import android.content.IntentSender.SendIntentException;
12import android.net.Uri;
13import android.os.Build;
14import android.os.Bundle;
15import android.provider.MediaStore;
16import android.support.v4.widget.SlidingPaneLayout;
17import android.support.v4.widget.SlidingPaneLayout.PanelSlideListener;
18import android.view.Menu;
19import android.view.MenuItem;
20import android.view.View;
21import android.widget.AdapterView;
22import android.widget.AdapterView.OnItemClickListener;
23import android.widget.ArrayAdapter;
24import android.widget.CheckBox;
25import android.widget.ListView;
26import android.widget.PopupMenu;
27import android.widget.PopupMenu.OnMenuItemClickListener;
28import android.widget.Toast;
29
30import net.java.otr4j.session.SessionStatus;
31
32import java.util.ArrayList;
33import java.util.List;
34
35import eu.siacs.conversations.R;
36import eu.siacs.conversations.entities.Account;
37import eu.siacs.conversations.entities.Blockable;
38import eu.siacs.conversations.entities.Contact;
39import eu.siacs.conversations.entities.Conversation;
40import eu.siacs.conversations.entities.Message;
41import eu.siacs.conversations.services.XmppConnectionService.OnAccountUpdate;
42import eu.siacs.conversations.services.XmppConnectionService.OnConversationUpdate;
43import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate;
44import eu.siacs.conversations.ui.adapter.ConversationAdapter;
45import eu.siacs.conversations.utils.ExceptionHelper;
46import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
47
48public class ConversationActivity extends XmppActivity
49 implements OnAccountUpdate, OnConversationUpdate, OnRosterUpdate, OnUpdateBlocklist {
50
51 public static final String ACTION_DOWNLOAD = "eu.siacs.conversations.action.DOWNLOAD";
52
53 public static final String VIEW_CONVERSATION = "viewConversation";
54 public static final String CONVERSATION = "conversationUuid";
55 public static final String MESSAGE = "messageUuid";
56 public static final String TEXT = "text";
57 public static final String NICK = "nick";
58
59 public static final int REQUEST_SEND_MESSAGE = 0x0201;
60 public static final int REQUEST_DECRYPT_PGP = 0x0202;
61 public static final int REQUEST_ENCRYPT_MESSAGE = 0x0207;
62 private static final int ATTACHMENT_CHOICE_CHOOSE_IMAGE = 0x0301;
63 private static final int ATTACHMENT_CHOICE_TAKE_PHOTO = 0x0302;
64 private static final int ATTACHMENT_CHOICE_CHOOSE_FILE = 0x0303;
65 private static final int ATTACHMENT_CHOICE_RECORD_VOICE = 0x0304;
66 private static final String STATE_OPEN_CONVERSATION = "state_open_conversation";
67 private static final String STATE_PANEL_OPEN = "state_panel_open";
68 private static final String STATE_PENDING_URI = "state_pending_uri";
69
70 private String mOpenConverstaion = null;
71 private boolean mPanelOpen = true;
72 private Uri mPendingImageUri = null;
73 private Uri mPendingFileUri = null;
74
75 private View mContentView;
76
77 private List<Conversation> conversationList = new ArrayList<>();
78 private Conversation mSelectedConversation = null;
79 private ListView listView;
80 private ConversationFragment mConversationFragment;
81
82 private ArrayAdapter<Conversation> listAdapter;
83
84 private Toast prepareFileToast;
85
86 private boolean mActivityPaused = false;
87
88 public Conversation getSelectedConversation() {
89 return this.mSelectedConversation;
90 }
91
92 public void setSelectedConversation(Conversation conversation) {
93 this.mSelectedConversation = conversation;
94 }
95
96 public void showConversationsOverview() {
97 if (mContentView instanceof SlidingPaneLayout) {
98 SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
99 mSlidingPaneLayout.openPane();
100 }
101 }
102
103 @Override
104 protected String getShareableUri() {
105 Conversation conversation = getSelectedConversation();
106 if (conversation != null) {
107 return conversation.getAccount().getShareableUri();
108 } else {
109 return "";
110 }
111 }
112
113 public void hideConversationsOverview() {
114 if (mContentView instanceof SlidingPaneLayout) {
115 SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
116 mSlidingPaneLayout.closePane();
117 }
118 }
119
120 public boolean isConversationsOverviewHideable() {
121 if (mContentView instanceof SlidingPaneLayout) {
122 SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
123 return mSlidingPaneLayout.isSlideable();
124 } else {
125 return false;
126 }
127 }
128
129 public boolean isConversationsOverviewVisable() {
130 if (mContentView instanceof SlidingPaneLayout) {
131 SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
132 return mSlidingPaneLayout.isOpen();
133 } else {
134 return true;
135 }
136 }
137
138 @Override
139 protected void onCreate(final Bundle savedInstanceState) {
140 super.onCreate(savedInstanceState);
141 if (savedInstanceState != null) {mOpenConverstaion = savedInstanceState.getString(
142 STATE_OPEN_CONVERSATION, null);
143 mPanelOpen = savedInstanceState.getBoolean(STATE_PANEL_OPEN, true);
144 String pending = savedInstanceState.getString(STATE_PENDING_URI, null);
145 if (pending != null) {
146 mPendingImageUri = Uri.parse(pending);
147 }
148 }
149
150 setContentView(R.layout.fragment_conversations_overview);
151
152 this.mConversationFragment = new ConversationFragment();
153 FragmentTransaction transaction = getFragmentManager().beginTransaction();
154 transaction.replace(R.id.selected_conversation, this.mConversationFragment, "conversation");
155 transaction.commit();
156
157 listView = (ListView) findViewById(R.id.list);
158 this.listAdapter = new ConversationAdapter(this, conversationList);
159 listView.setAdapter(this.listAdapter);
160
161 if (getActionBar() != null) {
162 getActionBar().setDisplayHomeAsUpEnabled(false);
163 getActionBar().setHomeButtonEnabled(false);
164 }
165
166 listView.setOnItemClickListener(new OnItemClickListener() {
167
168 @Override
169 public void onItemClick(AdapterView<?> arg0, View clickedView,
170 int position, long arg3) {
171 if (getSelectedConversation() != conversationList.get(position)) {
172 setSelectedConversation(conversationList.get(position));
173 ConversationActivity.this.mConversationFragment.reInit(getSelectedConversation());
174 }
175 hideConversationsOverview();
176 openConversation();
177 }
178 });
179 mContentView = findViewById(R.id.content_view_spl);
180 if (mContentView == null) {
181 mContentView = findViewById(R.id.content_view_ll);
182 }
183 if (mContentView instanceof SlidingPaneLayout) {
184 SlidingPaneLayout mSlidingPaneLayout = (SlidingPaneLayout) mContentView;
185 mSlidingPaneLayout.setParallaxDistance(150);
186 mSlidingPaneLayout
187 .setShadowResource(R.drawable.es_slidingpane_shadow);
188 mSlidingPaneLayout.setSliderFadeColor(0);
189 mSlidingPaneLayout.setPanelSlideListener(new PanelSlideListener() {
190
191 @Override
192 public void onPanelOpened(View arg0) {
193 updateActionBarTitle();
194 invalidateOptionsMenu();
195 hideKeyboard();
196 if (xmppConnectionServiceBound) {
197 xmppConnectionService.getNotificationService()
198 .setOpenConversation(null);
199 }
200 closeContextMenu();
201 }
202
203 @Override
204 public void onPanelClosed(View arg0) {
205 openConversation();
206 }
207
208 @Override
209 public void onPanelSlide(View arg0, float arg1) {
210 // TODO Auto-generated method stub
211
212 }
213 });
214 }
215 }
216
217 @Override
218 public void switchToConversation(Conversation conversation) {
219 setSelectedConversation(conversation);
220 runOnUiThread(new Runnable() {
221 @Override
222 public void run() {
223 ConversationActivity.this.mConversationFragment.reInit(getSelectedConversation());
224 openConversation();
225 }
226 });
227 }
228
229 private void updateActionBarTitle() {
230 updateActionBarTitle(isConversationsOverviewHideable() && !isConversationsOverviewVisable());
231 }
232
233 private void updateActionBarTitle(boolean titleShouldBeName) {
234 final ActionBar ab = getActionBar();
235 final Conversation conversation = getSelectedConversation();
236 if (ab != null) {
237 if (titleShouldBeName && conversation != null) {
238 ab.setDisplayHomeAsUpEnabled(true);
239 ab.setHomeButtonEnabled(true);
240 if (conversation.getMode() == Conversation.MODE_SINGLE || useSubjectToIdentifyConference()) {
241 ab.setTitle(conversation.getName());
242 } else {
243 ab.setTitle(conversation.getJid().toBareJid().toString());
244 }
245 } else {
246 ab.setDisplayHomeAsUpEnabled(false);
247 ab.setHomeButtonEnabled(false);
248 ab.setTitle(R.string.app_name);
249 }
250 }
251 }
252
253 private void openConversation() {
254 this.updateActionBarTitle();
255 this.invalidateOptionsMenu();
256 if (xmppConnectionServiceBound) {
257 final Conversation conversation = getSelectedConversation();
258 xmppConnectionService.getNotificationService().setOpenConversation(conversation);
259 sendReadMarkerIfNecessary(conversation);
260 }
261 listAdapter.notifyDataSetChanged();
262 }
263
264 public void sendReadMarkerIfNecessary(final Conversation conversation) {
265 if (!mActivityPaused && conversation != null) {
266 if (!conversation.isRead()) {
267 xmppConnectionService.sendReadMarker(conversation);
268 } else {
269 xmppConnectionService.markRead(conversation);
270 }
271 }
272 }
273
274 @Override
275 public boolean onCreateOptionsMenu(Menu menu) {
276 getMenuInflater().inflate(R.menu.conversations, menu);
277 final MenuItem menuSecure = menu.findItem(R.id.action_security);
278 final MenuItem menuArchive = menu.findItem(R.id.action_archive);
279 final MenuItem menuMucDetails = menu.findItem(R.id.action_muc_details);
280 final MenuItem menuContactDetails = menu.findItem(R.id.action_contact_details);
281 final MenuItem menuAttach = menu.findItem(R.id.action_attach_file);
282 final MenuItem menuClearHistory = menu.findItem(R.id.action_clear_history);
283 final MenuItem menuAdd = menu.findItem(R.id.action_add);
284 final MenuItem menuInviteContact = menu.findItem(R.id.action_invite);
285 final MenuItem menuMute = menu.findItem(R.id.action_mute);
286 final MenuItem menuUnmute = menu.findItem(R.id.action_unmute);
287
288 if (isConversationsOverviewVisable() && isConversationsOverviewHideable()) {
289 menuArchive.setVisible(false);
290 menuMucDetails.setVisible(false);
291 menuContactDetails.setVisible(false);
292 menuSecure.setVisible(false);
293 menuInviteContact.setVisible(false);
294 menuAttach.setVisible(false);
295 menuClearHistory.setVisible(false);
296 menuMute.setVisible(false);
297 menuUnmute.setVisible(false);
298 } else {
299 menuAdd.setVisible(!isConversationsOverviewHideable());
300 if (this.getSelectedConversation() != null) {
301 if (this.getSelectedConversation().getLatestMessage()
302 .getEncryption() != Message.ENCRYPTION_NONE) {
303 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
304 menuSecure.setIcon(R.drawable.ic_lock_outline_white_48dp);
305 } else {
306 menuSecure.setIcon(R.drawable.ic_action_secure);
307 }
308 }
309 if (this.getSelectedConversation().getMode() == Conversation.MODE_MULTI) {
310 menuContactDetails.setVisible(false);
311 menuAttach.setVisible(false);
312 menuInviteContact.setVisible(getSelectedConversation().getMucOptions().canInvite());
313 } else {
314 menuMucDetails.setVisible(false);
315 final Account account = this.getSelectedConversation().getAccount();
316 }
317 if (this.getSelectedConversation().isMuted()) {
318 menuMute.setVisible(false);
319 } else {
320 menuUnmute.setVisible(false);
321 }
322 }
323 }
324 return true;
325 }
326
327 private void selectPresenceToAttachFile(final int attachmentChoice) {
328 selectPresence(getSelectedConversation(), new OnPresenceSelected() {
329
330 @Override
331 public void onPresenceSelected() {
332 Intent intent = new Intent();
333 boolean chooser = false;
334 switch (attachmentChoice) {
335 case ATTACHMENT_CHOICE_CHOOSE_IMAGE:
336 intent.setAction(Intent.ACTION_GET_CONTENT);
337 intent.setType("image/*");
338 chooser = true;
339 break;
340 case ATTACHMENT_CHOICE_TAKE_PHOTO:
341 mPendingImageUri = xmppConnectionService.getFileBackend().getTakePhotoUri();
342 intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
343 intent.putExtra(MediaStore.EXTRA_OUTPUT,mPendingImageUri);
344 break;
345 case ATTACHMENT_CHOICE_CHOOSE_FILE:
346 chooser = true;
347 intent.setType("*/*");
348 intent.addCategory(Intent.CATEGORY_OPENABLE);
349 intent.setAction(Intent.ACTION_GET_CONTENT);
350 break;
351 case ATTACHMENT_CHOICE_RECORD_VOICE:
352 intent.setAction(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
353 break;
354 }
355 if (intent.resolveActivity(getPackageManager()) != null) {
356 if (chooser) {
357 startActivityForResult(
358 Intent.createChooser(intent,getString(R.string.perform_action_with)),
359 attachmentChoice);
360 } else {
361 startActivityForResult(intent, attachmentChoice);
362 }
363 }
364 }
365 });
366 }
367
368 private void attachFile(final int attachmentChoice) {
369 final Conversation conversation = getSelectedConversation();
370 if (conversation.getNextEncryption(forceEncryption()) == Message.ENCRYPTION_PGP) {
371 if (hasPgp()) {
372 if (conversation.getContact().getPgpKeyId() != 0) {
373 xmppConnectionService.getPgpEngine().hasKey(
374 conversation.getContact(),
375 new UiCallback<Contact>() {
376
377 @Override
378 public void userInputRequried(PendingIntent pi,
379 Contact contact) {
380 ConversationActivity.this.runIntent(pi,
381 attachmentChoice);
382 }
383
384 @Override
385 public void success(Contact contact) {
386 selectPresenceToAttachFile(attachmentChoice);
387 }
388
389 @Override
390 public void error(int error, Contact contact) {
391 displayErrorDialog(error);
392 }
393 });
394 } else {
395 final ConversationFragment fragment = (ConversationFragment) getFragmentManager()
396 .findFragmentByTag("conversation");
397 if (fragment != null) {
398 fragment.showNoPGPKeyDialog(false,
399 new OnClickListener() {
400
401 @Override
402 public void onClick(DialogInterface dialog,
403 int which) {
404 conversation
405 .setNextEncryption(Message.ENCRYPTION_NONE);
406 xmppConnectionService.databaseBackend
407 .updateConversation(conversation);
408 selectPresenceToAttachFile(attachmentChoice);
409 }
410 });
411 }
412 }
413 } else {
414 showInstallPgpDialog();
415 }
416 } else if (getSelectedConversation().getNextEncryption(
417 forceEncryption()) == Message.ENCRYPTION_NONE) {
418 selectPresenceToAttachFile(attachmentChoice);
419 } else {
420 selectPresenceToAttachFile(attachmentChoice);
421 }
422 }
423
424 @Override
425 public boolean onOptionsItemSelected(final MenuItem item) {
426 if (item.getItemId() == android.R.id.home) {
427 showConversationsOverview();
428 return true;
429 } else if (item.getItemId() == R.id.action_add) {
430 startActivity(new Intent(this, StartConversationActivity.class));
431 return true;
432 } else if (getSelectedConversation() != null) {
433 switch (item.getItemId()) {
434 case R.id.action_attach_file:
435 attachFileDialog();
436 break;
437 case R.id.action_archive:
438 this.endConversation(getSelectedConversation());
439 break;
440 case R.id.action_contact_details:
441 switchToContactDetails(getSelectedConversation().getContact());
442 break;
443 case R.id.action_muc_details:
444 Intent intent = new Intent(this,
445 ConferenceDetailsActivity.class);
446 intent.setAction(ConferenceDetailsActivity.ACTION_VIEW_MUC);
447 intent.putExtra("uuid", getSelectedConversation().getUuid());
448 startActivity(intent);
449 break;
450 case R.id.action_invite:
451 inviteToConversation(getSelectedConversation());
452 break;
453 case R.id.action_security:
454 selectEncryptionDialog(getSelectedConversation());
455 break;
456 case R.id.action_clear_history:
457 clearHistoryDialog(getSelectedConversation());
458 break;
459 case R.id.action_mute:
460 muteConversationDialog(getSelectedConversation());
461 break;
462 case R.id.action_unmute:
463 unmuteConversation(getSelectedConversation());
464 break;
465 case R.id.action_block:
466 BlockContactDialog.show(this, xmppConnectionService, getSelectedConversation());
467 break;
468 case R.id.action_unblock:
469 BlockContactDialog.show(this, xmppConnectionService, getSelectedConversation());
470 break;
471 default:
472 break;
473 }
474 return super.onOptionsItemSelected(item);
475 } else {
476 return super.onOptionsItemSelected(item);
477 }
478 }
479
480 public void endConversation(Conversation conversation) {
481 showConversationsOverview();
482 xmppConnectionService.archiveConversation(conversation);
483 if (conversationList.size() > 0) {
484 setSelectedConversation(conversationList.get(0));
485 this.mConversationFragment.reInit(getSelectedConversation());
486 } else {
487 setSelectedConversation(null);
488 }
489 }
490
491 @SuppressLint("InflateParams")
492 protected void clearHistoryDialog(final Conversation conversation) {
493 AlertDialog.Builder builder = new AlertDialog.Builder(this);
494 builder.setTitle(getString(R.string.clear_conversation_history));
495 View dialogView = getLayoutInflater().inflate(
496 R.layout.dialog_clear_history, null);
497 final CheckBox endConversationCheckBox = (CheckBox) dialogView
498 .findViewById(R.id.end_conversation_checkbox);
499 builder.setView(dialogView);
500 builder.setNegativeButton(getString(R.string.cancel), null);
501 builder.setPositiveButton(getString(R.string.delete_messages),
502 new OnClickListener() {
503
504 @Override
505 public void onClick(DialogInterface dialog, int which) {
506 ConversationActivity.this.xmppConnectionService.clearConversationHistory(conversation);
507 if (endConversationCheckBox.isChecked()) {
508 endConversation(conversation);
509 } else {
510 updateConversationList();
511 ConversationActivity.this.mConversationFragment.updateMessages();
512 }
513 }
514 });
515 builder.create().show();
516 }
517
518 protected void attachFileDialog() {
519 View menuAttachFile = findViewById(R.id.action_attach_file);
520 if (menuAttachFile == null) {
521 return;
522 }
523 PopupMenu attachFilePopup = new PopupMenu(this, menuAttachFile);
524 attachFilePopup.inflate(R.menu.attachment_choices);
525 if (new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION).resolveActivity(getPackageManager()) == null) {
526 attachFilePopup.getMenu().findItem(R.id.attach_record_voice).setVisible(false);
527 }
528 attachFilePopup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
529
530 @Override
531 public boolean onMenuItemClick(MenuItem item) {
532 switch (item.getItemId()) {
533 case R.id.attach_choose_picture:
534 attachFile(ATTACHMENT_CHOICE_CHOOSE_IMAGE);
535 break;
536 case R.id.attach_take_picture:
537 attachFile(ATTACHMENT_CHOICE_TAKE_PHOTO);
538 break;
539 case R.id.attach_choose_file:
540 attachFile(ATTACHMENT_CHOICE_CHOOSE_FILE);
541 break;
542 case R.id.attach_record_voice:
543 attachFile(ATTACHMENT_CHOICE_RECORD_VOICE);
544 break;
545 }
546 return false;
547 }
548 });
549 attachFilePopup.show();
550 }
551
552 public void verifyOtrSessionDialog(final Conversation conversation, View view) {
553 if (!conversation.hasValidOtrSession() || conversation.getOtrSession().getSessionStatus() != SessionStatus.ENCRYPTED) {
554 Toast.makeText(this, R.string.otr_session_not_started, Toast.LENGTH_LONG).show();
555 return;
556 }
557 if (view == null) {
558 return;
559 }
560 PopupMenu popup = new PopupMenu(this, view);
561 popup.inflate(R.menu.verification_choices);
562 popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
563 @Override
564 public boolean onMenuItemClick(MenuItem menuItem) {
565 Intent intent = new Intent(ConversationActivity.this, VerifyOTRActivity.class);
566 intent.setAction(VerifyOTRActivity.ACTION_VERIFY_CONTACT);
567 intent.putExtra("contact", conversation.getContact().getJid().toBareJid().toString());
568 intent.putExtra("account", conversation.getAccount().getJid().toBareJid().toString());
569 switch (menuItem.getItemId()) {
570 case R.id.scan_fingerprint:
571 intent.putExtra("mode",VerifyOTRActivity.MODE_SCAN_FINGERPRINT);
572 break;
573 case R.id.ask_question:
574 intent.putExtra("mode",VerifyOTRActivity.MODE_ASK_QUESTION);
575 break;
576 case R.id.manual_verification:
577 intent.putExtra("mode",VerifyOTRActivity.MODE_MANUAL_VERIFICATION);
578 break;
579 }
580 startActivity(intent);
581 return true;
582 }
583 });
584 popup.show();
585 }
586
587 protected void selectEncryptionDialog(final Conversation conversation) {
588 View menuItemView = findViewById(R.id.action_security);
589 if (menuItemView == null) {
590 return;
591 }
592 PopupMenu popup = new PopupMenu(this, menuItemView);
593 final ConversationFragment fragment = (ConversationFragment) getFragmentManager()
594 .findFragmentByTag("conversation");
595 if (fragment != null) {
596 popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
597
598 @Override
599 public boolean onMenuItemClick(MenuItem item) {
600 switch (item.getItemId()) {
601 case R.id.encryption_choice_none:
602 conversation.setNextEncryption(Message.ENCRYPTION_NONE);
603 item.setChecked(true);
604 break;
605 case R.id.encryption_choice_otr:
606 conversation.setNextEncryption(Message.ENCRYPTION_OTR);
607 item.setChecked(true);
608 break;
609 case R.id.encryption_choice_pgp:
610 if (hasPgp()) {
611 if (conversation.getAccount().getKeys()
612 .has("pgp_signature")) {
613 conversation
614 .setNextEncryption(Message.ENCRYPTION_PGP);
615 item.setChecked(true);
616 } else {
617 announcePgp(conversation.getAccount(),
618 conversation);
619 }
620 } else {
621 showInstallPgpDialog();
622 }
623 break;
624 default:
625 conversation.setNextEncryption(Message.ENCRYPTION_NONE);
626 break;
627 }
628 xmppConnectionService.databaseBackend
629 .updateConversation(conversation);
630 fragment.updateChatMsgHint();
631 return true;
632 }
633 });
634 popup.inflate(R.menu.encryption_choices);
635 MenuItem otr = popup.getMenu().findItem(R.id.encryption_choice_otr);
636 MenuItem none = popup.getMenu().findItem(
637 R.id.encryption_choice_none);
638 if (conversation.getMode() == Conversation.MODE_MULTI) {
639 otr.setEnabled(false);
640 } else {
641 if (forceEncryption()) {
642 none.setVisible(false);
643 }
644 }
645 switch (conversation.getNextEncryption(forceEncryption())) {
646 case Message.ENCRYPTION_NONE:
647 none.setChecked(true);
648 break;
649 case Message.ENCRYPTION_OTR:
650 otr.setChecked(true);
651 break;
652 case Message.ENCRYPTION_PGP:
653 popup.getMenu().findItem(R.id.encryption_choice_pgp)
654 .setChecked(true);
655 break;
656 default:
657 popup.getMenu().findItem(R.id.encryption_choice_none)
658 .setChecked(true);
659 break;
660 }
661 popup.show();
662 }
663 }
664
665 protected void muteConversationDialog(final Conversation conversation) {
666 AlertDialog.Builder builder = new AlertDialog.Builder(this);
667 builder.setTitle(R.string.disable_notifications);
668 final int[] durations = getResources().getIntArray(
669 R.array.mute_options_durations);
670 builder.setItems(R.array.mute_options_descriptions,
671 new OnClickListener() {
672
673 @Override
674 public void onClick(final DialogInterface dialog, final int which) {
675 final long till;
676 if (durations[which] == -1) {
677 till = Long.MAX_VALUE;
678 } else {
679 till = System.currentTimeMillis() + (durations[which] * 1000);
680 }
681 conversation.setMutedTill(till);
682 ConversationActivity.this.xmppConnectionService.databaseBackend
683 .updateConversation(conversation);
684 updateConversationList();
685 ConversationActivity.this.mConversationFragment.updateMessages();
686 invalidateOptionsMenu();
687 }
688 });
689 builder.create().show();
690 }
691
692 public void unmuteConversation(final Conversation conversation) {
693 conversation.setMutedTill(0);
694 this.xmppConnectionService.databaseBackend.updateConversation(conversation);
695 updateConversationList();
696 ConversationActivity.this.mConversationFragment.updateMessages();
697 invalidateOptionsMenu();
698 }
699
700 @Override
701 public void onBackPressed() {
702 if (!isConversationsOverviewVisable()) {
703 showConversationsOverview();
704 } else {
705 moveTaskToBack(true);
706 }
707 }
708
709 @Override
710 protected void onNewIntent(final Intent intent) {
711 if (xmppConnectionServiceBound) {
712 if (intent != null && VIEW_CONVERSATION.equals(intent.getType())) {
713 handleViewConversationIntent(intent);
714 }
715 } else {
716 setIntent(intent);
717 }
718 }
719
720 @Override
721 public void onStart() {
722 super.onStart();
723 if (this.xmppConnectionServiceBound) {
724 this.onBackendConnected();
725 }
726 if (conversationList.size() >= 1) {
727 this.onConversationUpdate();
728 }
729 }
730
731 @Override
732 public void onPause() {
733 super.onPause();
734 this.mActivityPaused = true;
735 if (this.xmppConnectionServiceBound) {
736 this.xmppConnectionService.getNotificationService().setIsInForeground(false);
737 }
738 }
739
740 @Override
741 public void onResume() {
742 super.onResume();
743 final int theme = findTheme();
744 final boolean usingEnterKey = usingEnterKey();
745 if (this.mTheme != theme || usingEnterKey != mUsingEnterKey) {
746 recreate();
747 }
748 this.mActivityPaused = false;
749 if (this.xmppConnectionServiceBound) {
750 this.xmppConnectionService.getNotificationService().setIsInForeground(true);
751 }
752
753 if (!isConversationsOverviewVisable() || !isConversationsOverviewHideable()) {
754 sendReadMarkerIfNecessary(getSelectedConversation());
755 }
756
757 }
758
759 @Override
760 public void onSaveInstanceState(final Bundle savedInstanceState) {
761 Conversation conversation = getSelectedConversation();
762 if (conversation != null) {
763 savedInstanceState.putString(STATE_OPEN_CONVERSATION,
764 conversation.getUuid());
765 }
766 savedInstanceState.putBoolean(STATE_PANEL_OPEN,
767 isConversationsOverviewVisable());
768 if (this.mPendingImageUri != null) {
769 savedInstanceState.putString(STATE_PENDING_URI, this.mPendingImageUri.toString());
770 }
771 super.onSaveInstanceState(savedInstanceState);
772 }
773
774 @Override
775 void onBackendConnected() {
776 this.xmppConnectionService.getNotificationService().setIsInForeground(true);
777 updateConversationList();
778 if (xmppConnectionService.getAccounts().size() == 0) {
779 startActivity(new Intent(this, EditAccountActivity.class));
780 } else if (conversationList.size() <= 0) {
781 startActivity(new Intent(this, StartConversationActivity.class));
782 finish();
783 } else if (getIntent() != null && VIEW_CONVERSATION.equals(getIntent().getType())) {
784 handleViewConversationIntent(getIntent());
785 } else if (selectConversationByUuid(mOpenConverstaion)) {
786 if (mPanelOpen) {
787 showConversationsOverview();
788 } else {
789 if (isConversationsOverviewHideable()) {
790 openConversation();
791 }
792 }
793 this.mConversationFragment.reInit(getSelectedConversation());
794 mOpenConverstaion = null;
795 } else if (getSelectedConversation() != null) {
796 this.mConversationFragment.reInit(getSelectedConversation());
797 } else {
798 showConversationsOverview();
799 mPendingImageUri = null;
800 mPendingFileUri = null;
801 setSelectedConversation(conversationList.get(0));
802 this.mConversationFragment.reInit(getSelectedConversation());
803 }
804
805 if (mPendingImageUri != null) {
806 attachImageToConversation(getSelectedConversation(),mPendingImageUri);
807 mPendingImageUri = null;
808 } else if (mPendingFileUri != null) {
809 attachFileToConversation(getSelectedConversation(),mPendingFileUri);
810 mPendingFileUri = null;
811 }
812 ExceptionHelper.checkForCrash(this, this.xmppConnectionService);
813 setIntent(new Intent());
814 }
815
816 private void handleViewConversationIntent(final Intent intent) {
817 final String uuid = (String) intent.getExtras().get(CONVERSATION);
818 final String downloadUuid = (String) intent.getExtras().get(MESSAGE);
819 final String text = intent.getExtras().getString(TEXT, "");
820 final String nick = intent.getExtras().getString(NICK, null);
821 if (selectConversationByUuid(uuid)) {
822 this.mConversationFragment.reInit(getSelectedConversation());
823 if (nick != null) {
824 this.mConversationFragment.highlightInConference(nick);
825 } else {
826 this.mConversationFragment.appendText(text);
827 }
828 hideConversationsOverview();
829 openConversation();
830 if (mContentView instanceof SlidingPaneLayout) {
831 updateActionBarTitle(true); //fixes bug where slp isn't properly closed yet
832 }
833 if (downloadUuid != null) {
834 final Message message = mSelectedConversation.findMessageWithFileAndUuid(downloadUuid);
835 if (message != null) {
836 mConversationFragment.messageListAdapter.startDownloadable(message);
837 }
838 }
839 }
840 }
841
842 private boolean selectConversationByUuid(String uuid) {
843 if (uuid == null) {
844 return false;
845 }
846 for (Conversation aConversationList : conversationList) {
847 if (aConversationList.getUuid().equals(uuid)) {
848 setSelectedConversation(aConversationList);
849 return true;
850 }
851 }
852 return false;
853 }
854
855 @Override
856 protected void unregisterListeners() {
857 super.unregisterListeners();
858 xmppConnectionService.getNotificationService().setOpenConversation(null);
859 }
860
861 @Override
862 protected void onActivityResult(int requestCode, int resultCode,
863 final Intent data) {
864 super.onActivityResult(requestCode, resultCode, data);
865 if (resultCode == RESULT_OK) {
866 if (requestCode == REQUEST_DECRYPT_PGP) {
867 mConversationFragment.hideSnackbar();
868 mConversationFragment.updateMessages();
869 } else if (requestCode == ATTACHMENT_CHOICE_CHOOSE_IMAGE) {
870 mPendingImageUri = data.getData();
871 if (xmppConnectionServiceBound) {
872 attachImageToConversation(getSelectedConversation(),mPendingImageUri);
873 mPendingImageUri = null;
874 }
875 } else if (requestCode == ATTACHMENT_CHOICE_CHOOSE_FILE || requestCode == ATTACHMENT_CHOICE_RECORD_VOICE) {
876 mPendingFileUri = data.getData();
877 if (xmppConnectionServiceBound) {
878 attachFileToConversation(getSelectedConversation(),mPendingFileUri);
879 mPendingFileUri = null;
880 }
881 } else if (requestCode == ATTACHMENT_CHOICE_TAKE_PHOTO && mPendingImageUri != null) {
882 if (xmppConnectionServiceBound) {
883 attachImageToConversation(getSelectedConversation(),mPendingImageUri);
884 mPendingImageUri = null;
885 }
886 Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
887 intent.setData(mPendingImageUri);
888 sendBroadcast(intent);
889 }
890 } else {
891 if (requestCode == ATTACHMENT_CHOICE_TAKE_PHOTO) {
892 mPendingImageUri = null;
893 }
894 }
895 }
896
897 private void attachFileToConversation(Conversation conversation, Uri uri) {
898 prepareFileToast = Toast.makeText(getApplicationContext(),
899 getText(R.string.preparing_file), Toast.LENGTH_LONG);
900 prepareFileToast.show();
901 xmppConnectionService.attachFileToConversation(conversation,uri, new UiCallback<Message>() {
902 @Override
903 public void success(Message message) {
904 hidePrepareFileToast();
905 xmppConnectionService.sendMessage(message);
906 }
907
908 @Override
909 public void error(int errorCode, Message message) {
910 displayErrorDialog(errorCode);
911 }
912
913 @Override
914 public void userInputRequried(PendingIntent pi, Message message) {
915
916 }
917 });
918 }
919
920 private void attachImageToConversation(Conversation conversation, Uri uri) {
921 prepareFileToast = Toast.makeText(getApplicationContext(),
922 getText(R.string.preparing_image), Toast.LENGTH_LONG);
923 prepareFileToast.show();
924 xmppConnectionService.attachImageToConversation(conversation, uri,
925 new UiCallback<Message>() {
926
927 @Override
928 public void userInputRequried(PendingIntent pi,
929 Message object) {
930 hidePrepareFileToast();
931 }
932
933 @Override
934 public void success(Message message) {
935 xmppConnectionService.sendMessage(message);
936 }
937
938 @Override
939 public void error(int error, Message message) {
940 hidePrepareFileToast();
941 displayErrorDialog(error);
942 }
943 });
944 }
945
946 private void hidePrepareFileToast() {
947 if (prepareFileToast != null) {
948 runOnUiThread(new Runnable() {
949
950 @Override
951 public void run() {
952 prepareFileToast.cancel();
953 }
954 });
955 }
956 }
957
958 public void updateConversationList() {
959 xmppConnectionService
960 .populateWithOrderedConversations(conversationList);
961 listAdapter.notifyDataSetChanged();
962 }
963
964 public void runIntent(PendingIntent pi, int requestCode) {
965 try {
966 this.startIntentSenderForResult(pi.getIntentSender(), requestCode,
967 null, 0, 0, 0);
968 } catch (final SendIntentException ignored) {
969 }
970 }
971
972 public void encryptTextMessage(Message message) {
973 xmppConnectionService.getPgpEngine().encrypt(message,
974 new UiCallback<Message>() {
975
976 @Override
977 public void userInputRequried(PendingIntent pi,
978 Message message) {
979 ConversationActivity.this.runIntent(pi,
980 ConversationActivity.REQUEST_SEND_MESSAGE);
981 }
982
983 @Override
984 public void success(Message message) {
985 message.setEncryption(Message.ENCRYPTION_DECRYPTED);
986 xmppConnectionService.sendMessage(message);
987 }
988
989 @Override
990 public void error(int error, Message message) {
991
992 }
993 });
994 }
995
996 public boolean forceEncryption() {
997 return getPreferences().getBoolean("force_encryption", false);
998 }
999
1000 public boolean useSendButtonToIndicateStatus() {
1001 return getPreferences().getBoolean("send_button_status", false);
1002 }
1003
1004 public boolean indicateReceived() {
1005 return getPreferences().getBoolean("indicate_received", false);
1006 }
1007
1008 @Override
1009 protected void refreshUiReal() {
1010 updateConversationList();
1011 if (conversationList.size() == 0) {
1012 startActivity(new Intent(getApplicationContext(),
1013 StartConversationActivity.class));
1014 finish();
1015 }
1016 ConversationActivity.this.mConversationFragment.updateMessages();
1017 updateActionBarTitle();
1018 }
1019
1020 @Override
1021 public void onAccountUpdate() {
1022 this.refreshUi();
1023 }
1024
1025 @Override
1026 public void onConversationUpdate() {
1027 this.refreshUi();
1028 }
1029
1030 @Override
1031 public void onRosterUpdate() {
1032 this.refreshUi();
1033 }
1034
1035 @Override
1036 public void OnUpdateBlocklist(Status status) {
1037 this.refreshUi();
1038 runOnUiThread(new Runnable() {
1039 @Override
1040 public void run() {
1041 invalidateOptionsMenu();
1042 }
1043 });
1044 }
1045
1046 public void unblockConversation(final Blockable conversation) {
1047 xmppConnectionService.sendUnblockRequest(conversation);
1048 }
1049
1050 public boolean enterIsSend() {
1051 return getPreferences().getBoolean("enter_is_send",false);
1052 }
1053}