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