1package eu.siacs.conversations.ui;
2
3import android.Manifest;
4import android.annotation.SuppressLint;
5import android.content.res.TypedArray;
6import android.databinding.DataBindingUtil;
7import android.databinding.ViewDataBinding;
8import android.graphics.drawable.Drawable;
9import android.support.annotation.AttrRes;
10import android.support.annotation.DrawableRes;
11import android.support.annotation.NonNull;
12import android.support.v4.content.ContextCompat;
13import android.support.v7.app.AlertDialog;
14import android.app.Dialog;
15import android.app.PendingIntent;
16import android.content.ActivityNotFoundException;
17import android.content.Context;
18import android.content.DialogInterface;
19import android.content.DialogInterface.OnClickListener;
20import android.content.Intent;
21import android.content.pm.PackageManager;
22import android.net.Uri;
23import android.os.Build;
24import android.os.Bundle;
25import android.support.v4.app.Fragment;
26import android.support.v4.app.FragmentManager;
27import android.support.v4.app.FragmentTransaction;
28import android.support.v4.app.ListFragment;
29import android.support.v4.view.MenuItemCompat;
30import android.support.v4.view.PagerAdapter;
31import android.support.v4.view.ViewPager;
32import android.support.v7.app.ActionBar;
33import android.text.Editable;
34import android.text.SpannableString;
35import android.text.Spanned;
36import android.text.TextWatcher;
37import android.text.style.TypefaceSpan;
38import android.util.Pair;
39import android.view.ContextMenu;
40import android.view.ContextMenu.ContextMenuInfo;
41import android.view.KeyEvent;
42import android.view.Menu;
43import android.view.MenuItem;
44import android.view.View;
45import android.view.ViewGroup;
46import android.view.inputmethod.InputMethodManager;
47import android.widget.AdapterView;
48import android.widget.AdapterView.AdapterContextMenuInfo;
49import android.widget.AdapterView.OnItemClickListener;
50import android.widget.ArrayAdapter;
51import android.widget.AutoCompleteTextView;
52import android.widget.CheckBox;
53import android.widget.Checkable;
54import android.widget.EditText;
55import android.widget.ListView;
56import android.widget.Spinner;
57import android.widget.TextView;
58import android.widget.Toast;
59
60import java.util.ArrayList;
61import java.util.Arrays;
62import java.util.Collections;
63import java.util.List;
64import java.util.concurrent.atomic.AtomicBoolean;
65
66import eu.siacs.conversations.Config;
67import eu.siacs.conversations.R;
68import eu.siacs.conversations.databinding.ActivityStartConversationBinding;
69import eu.siacs.conversations.entities.Account;
70import eu.siacs.conversations.entities.Bookmark;
71import eu.siacs.conversations.entities.Contact;
72import eu.siacs.conversations.entities.Conversation;
73import eu.siacs.conversations.entities.ListItem;
74import eu.siacs.conversations.entities.Presence;
75import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate;
76import eu.siacs.conversations.ui.adapter.KnownHostsAdapter;
77import eu.siacs.conversations.ui.adapter.ListItemAdapter;
78import eu.siacs.conversations.ui.service.EmojiService;
79import eu.siacs.conversations.ui.util.DelayedHintHelper;
80import eu.siacs.conversations.utils.XmppUri;
81import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
82import eu.siacs.conversations.xmpp.XmppConnection;
83import eu.siacs.conversations.xmpp.jid.InvalidJidException;
84import eu.siacs.conversations.xmpp.jid.Jid;
85
86public class StartConversationActivity extends XmppActivity implements OnRosterUpdate, OnUpdateBlocklist {
87
88 private final int REQUEST_SYNC_CONTACTS = 0x28cf;
89 private final int REQUEST_CREATE_CONFERENCE = 0x39da;
90 public int conference_context_id;
91 public int contact_context_id;
92 private ListPagerAdapter mListPagerAdapter;
93 private List<ListItem> contacts = new ArrayList<>();
94 private ListItemAdapter mContactsAdapter;
95 private List<ListItem> conferences = new ArrayList<>();
96 private ListItemAdapter mConferenceAdapter;
97 private List<String> mActivatedAccounts = new ArrayList<>();
98 private List<String> mKnownHosts;
99 private List<String> mKnownConferenceHosts;
100 private Invite mPendingInvite = null;
101 private EditText mSearchEditText;
102 private AtomicBoolean mRequestedContactsPermission = new AtomicBoolean(false);
103 private Dialog mCurrentDialog = null;
104 private boolean mHideOfflineContacts = false;
105 private MenuItem.OnActionExpandListener mOnActionExpandListener = new MenuItem.OnActionExpandListener() {
106
107 @Override
108 public boolean onMenuItemActionExpand(MenuItem item) {
109 mSearchEditText.post(() -> {
110 mSearchEditText.requestFocus();
111 InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
112 imm.showSoftInput(mSearchEditText, InputMethodManager.SHOW_IMPLICIT);
113 });
114
115 return true;
116 }
117
118 @Override
119 public boolean onMenuItemActionCollapse(MenuItem item) {
120 hideKeyboard();
121 mSearchEditText.setText("");
122 filter(null);
123 return true;
124 }
125 };
126 private TextWatcher mSearchTextWatcher = new TextWatcher() {
127
128 @Override
129 public void afterTextChanged(Editable editable) {
130 filter(editable.toString());
131 }
132
133 @Override
134 public void beforeTextChanged(CharSequence s, int start, int count,
135 int after) {
136 }
137
138 @Override
139 public void onTextChanged(CharSequence s, int start, int before, int count) {
140 }
141 };
142 private TextView.OnEditorActionListener mSearchDone = new TextView.OnEditorActionListener() {
143 @Override
144 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
145 int pos = getSupportActionBar().getSelectedNavigationIndex();
146 if (pos == 0) {
147 if (contacts.size() == 1) {
148 openConversationForContact((Contact) contacts.get(0));
149 return true;
150 }
151 } else {
152 if (conferences.size() == 1) {
153 openConversationsForBookmark((Bookmark) conferences.get(0));
154 return true;
155 }
156 }
157 hideKeyboard();
158 mListPagerAdapter.requestFocus(pos);
159 return true;
160 }
161 };
162 private MenuItem mMenuSearchView;
163 private ListItemAdapter.OnTagClickedListener mOnTagClickedListener = new ListItemAdapter.OnTagClickedListener() {
164 @Override
165 public void onTagClicked(String tag) {
166 if (mMenuSearchView != null) {
167 mMenuSearchView.expandActionView();
168 mSearchEditText.setText("");
169 mSearchEditText.append(tag);
170 filter(tag);
171 }
172 }
173 };
174 private String mInitialJid;
175 private Pair<Integer, Intent> mPostponedActivityResult;
176 private Toast mToast;
177 private UiCallback<Conversation> mAdhocConferenceCallback = new UiCallback<Conversation>() {
178 @Override
179 public void success(final Conversation conversation) {
180 runOnUiThread(() -> {
181 hideToast();
182 switchToConversation(conversation);
183 });
184 }
185
186 @Override
187 public void error(final int errorCode, Conversation object) {
188 runOnUiThread(() -> replaceToast(getString(errorCode)));
189 }
190
191 @Override
192 public void userInputRequried(PendingIntent pi, Conversation object) {
193
194 }
195 };
196 private ActivityStartConversationBinding binding;
197 private ActionBar.TabListener mTabListener = new ActionBar.TabListener() {
198
199 @Override
200 public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
201 return;
202 }
203
204 @Override
205 public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
206 binding.startConversationViewPager.setCurrentItem(tab.getPosition());
207 onTabChanged();
208 }
209
210 @Override
211 public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
212 return;
213 }
214 };
215 private ViewPager.SimpleOnPageChangeListener mOnPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
216 @Override
217 public void onPageSelected(int position) {
218 ActionBar actionBar = getSupportActionBar();
219 if (actionBar != null) {
220 actionBar.setSelectedNavigationItem(position);
221 }
222 onTabChanged();
223 }
224 };
225
226 public static void populateAccountSpinner(Context context, List<String> accounts, Spinner spinner) {
227 if (accounts.size() > 0) {
228 ArrayAdapter<String> adapter = new ArrayAdapter<>(context, R.layout.simple_list_item, accounts);
229 adapter.setDropDownViewResource(R.layout.simple_list_item);
230 spinner.setAdapter(adapter);
231 spinner.setEnabled(true);
232 } else {
233 ArrayAdapter<String> adapter = new ArrayAdapter<>(context,
234 R.layout.simple_list_item,
235 Arrays.asList(context.getString(R.string.no_accounts)));
236 adapter.setDropDownViewResource(R.layout.simple_list_item);
237 spinner.setAdapter(adapter);
238 spinner.setEnabled(false);
239 }
240 }
241
242 public static void launch(Context context) {
243 final Intent intent = new Intent(context, StartConversationActivity.class);
244 context.startActivity(intent);
245 }
246
247 protected void hideToast() {
248 if (mToast != null) {
249 mToast.cancel();
250 }
251 }
252
253 protected void replaceToast(String msg) {
254 hideToast();
255 mToast = Toast.makeText(this, msg, Toast.LENGTH_LONG);
256 mToast.show();
257 }
258
259 @Override
260 public void onRosterUpdate() {
261 this.refreshUi();
262 }
263
264 @Override
265 public void onCreate(Bundle savedInstanceState) {
266 super.onCreate(savedInstanceState);
267 new EmojiService(this).init();
268 this.binding = DataBindingUtil.setContentView(this, R.layout.activity_start_conversation);
269 this.binding.fab.setOnClickListener((v) -> {
270 if (getSupportActionBar().getSelectedNavigationIndex() == 0) {
271 String searchString = mSearchEditText != null ? mSearchEditText.getText().toString() : null;
272 if (searchString != null && !searchString.trim().isEmpty()) {
273 try {
274 Jid jid = Jid.fromString(searchString);
275 if (!jid.isDomainJid() && jid.isBareJid() && jid.getDomainpart().contains(".")) {
276 showCreateContactDialog(jid.toString(),null);
277 return;
278 }
279 } catch (InvalidJidException ignored) {
280 //ignore and fall through
281 }
282 }
283 showCreateContactDialog(null, null);
284 } else {
285 showCreateConferenceDialog();
286 }
287 });
288 ActionBar actionBar = getSupportActionBar();
289 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
290
291 ActionBar.Tab mContactsTab = actionBar.newTab().setText(R.string.contacts).setTabListener(mTabListener);
292 ActionBar.Tab mConferencesTab = actionBar.newTab().setText(R.string.conferences).setTabListener(mTabListener);
293 actionBar.addTab(mContactsTab);
294 actionBar.addTab(mConferencesTab);
295
296 binding.startConversationViewPager.setOnPageChangeListener(mOnPageChangeListener);
297 mListPagerAdapter = new ListPagerAdapter(getSupportFragmentManager());
298 binding.startConversationViewPager.setAdapter(mListPagerAdapter);
299
300 mConferenceAdapter = new ListItemAdapter(this, conferences);
301 mContactsAdapter = new ListItemAdapter(this, contacts);
302 mContactsAdapter.setOnTagClickedListener(this.mOnTagClickedListener);
303 this.mHideOfflineContacts = getPreferences().getBoolean("hide_offline", false);
304
305 }
306
307 @Override
308 public void onStart() {
309 super.onStart();
310 final int theme = findTheme();
311 if (this.mTheme != theme) {
312 recreate();
313 } else {
314 Intent i = getIntent();
315 if (i == null || !i.hasExtra(WelcomeActivity.EXTRA_INVITE_URI)) {
316 askForContactsPermissions();
317 }
318 }
319 mConferenceAdapter.refreshSettings();
320 mContactsAdapter.refreshSettings();
321 }
322
323 @Override
324 public void onStop() {
325 if (mCurrentDialog != null) {
326 mCurrentDialog.dismiss();
327 }
328 super.onStop();
329 }
330
331 @Override
332 public void onNewIntent(Intent intent) {
333 if (xmppConnectionServiceBound) {
334 handleIntent(intent);
335 } else {
336 setIntent(intent);
337 }
338 }
339
340 protected void openConversationForContact(int position) {
341 Contact contact = (Contact) contacts.get(position);
342 openConversationForContact(contact);
343 }
344
345 protected void openConversationForContact(Contact contact) {
346 Conversation conversation = xmppConnectionService.findOrCreateConversation(contact.getAccount(), contact.getJid(), false, true);
347 switchToConversation(conversation);
348 }
349
350 protected void openConversationForContact() {
351 int position = contact_context_id;
352 openConversationForContact(position);
353 }
354
355 protected void openConversationForBookmark() {
356 openConversationForBookmark(conference_context_id);
357 }
358
359 protected void openConversationForBookmark(int position) {
360 Bookmark bookmark = (Bookmark) conferences.get(position);
361 openConversationsForBookmark(bookmark);
362 }
363
364 protected void shareBookmarkUri() {
365 shareBookmarkUri(conference_context_id);
366 }
367
368 protected void shareBookmarkUri(int position) {
369 Bookmark bookmark = (Bookmark) conferences.get(position);
370 Intent shareIntent = new Intent();
371 shareIntent.setAction(Intent.ACTION_SEND);
372 shareIntent.putExtra(Intent.EXTRA_TEXT, "xmpp:" + bookmark.getJid().toBareJid().toString() + "?join");
373 shareIntent.setType("text/plain");
374 try {
375 startActivity(Intent.createChooser(shareIntent, getText(R.string.share_uri_with)));
376 } catch (ActivityNotFoundException e) {
377 Toast.makeText(this, R.string.no_application_to_share_uri, Toast.LENGTH_SHORT).show();
378 }
379 }
380
381 protected void openConversationsForBookmark(Bookmark bookmark) {
382 Jid jid = bookmark.getJid();
383 if (jid == null) {
384 Toast.makeText(this, R.string.invalid_jid, Toast.LENGTH_SHORT).show();
385 return;
386 }
387 Conversation conversation = xmppConnectionService.findOrCreateConversation(bookmark.getAccount(), jid, true, true, true);
388 bookmark.setConversation(conversation);
389 if (!bookmark.autojoin() && getPreferences().getBoolean("autojoin", getResources().getBoolean(R.bool.autojoin))) {
390 bookmark.setAutojoin(true);
391 xmppConnectionService.pushBookmarks(bookmark.getAccount());
392 }
393 switchToConversation(conversation);
394 }
395
396 protected void openDetailsForContact() {
397 int position = contact_context_id;
398 Contact contact = (Contact) contacts.get(position);
399 switchToContactDetails(contact);
400 }
401
402 protected void toggleContactBlock() {
403 final int position = contact_context_id;
404 BlockContactDialog.show(this, (Contact) contacts.get(position));
405 }
406
407 protected void deleteContact() {
408 final int position = contact_context_id;
409 final Contact contact = (Contact) contacts.get(position);
410 final AlertDialog.Builder builder = new AlertDialog.Builder(this);
411 builder.setNegativeButton(R.string.cancel, null);
412 builder.setTitle(R.string.action_delete_contact);
413 builder.setMessage(getString(R.string.remove_contact_text,
414 contact.getJid()));
415 builder.setPositiveButton(R.string.delete, new OnClickListener() {
416
417 @Override
418 public void onClick(DialogInterface dialog, int which) {
419 xmppConnectionService.deleteContactOnServer(contact);
420 filter(mSearchEditText.getText().toString());
421 }
422 });
423 builder.create().show();
424 }
425
426 protected void deleteConference() {
427 int position = conference_context_id;
428 final Bookmark bookmark = (Bookmark) conferences.get(position);
429
430 AlertDialog.Builder builder = new AlertDialog.Builder(this);
431 builder.setNegativeButton(R.string.cancel, null);
432 builder.setTitle(R.string.delete_bookmark);
433 builder.setMessage(getString(R.string.remove_bookmark_text,
434 bookmark.getJid()));
435 builder.setPositiveButton(R.string.delete, new OnClickListener() {
436
437 @Override
438 public void onClick(DialogInterface dialog, int which) {
439 bookmark.setConversation(null);
440 Account account = bookmark.getAccount();
441 account.getBookmarks().remove(bookmark);
442 xmppConnectionService.pushBookmarks(account);
443 filter(mSearchEditText.getText().toString());
444 }
445 });
446 builder.create().show();
447
448 }
449
450 @SuppressLint("InflateParams")
451 protected void showCreateContactDialog(final String prefilledJid, final Invite invite) {
452 EnterJidDialog dialog = new EnterJidDialog(
453 this, mKnownHosts, mActivatedAccounts,
454 getString(R.string.dialog_title_create_contact), getString(R.string.create),
455 prefilledJid, null, invite == null || !invite.hasFingerprints()
456 );
457
458 dialog.setOnEnterJidDialogPositiveListener((accountJid, contactJid) -> {
459 if (!xmppConnectionServiceBound) {
460 return false;
461 }
462
463 final Account account = xmppConnectionService.findAccountByJid(accountJid);
464 if (account == null) {
465 return true;
466 }
467
468 final Contact contact = account.getRoster().getContact(contactJid);
469 if (invite != null && invite.getName() != null) {
470 contact.setServerName(invite.getName());
471 }
472 if (contact.isSelf()) {
473 switchToConversation(contact, null);
474 return true;
475 } else if (contact.showInRoster()) {
476 throw new EnterJidDialog.JidError(getString(R.string.contact_already_exists));
477 } else {
478 xmppConnectionService.createContact(contact,true);
479 if (invite != null && invite.hasFingerprints()) {
480 xmppConnectionService.verifyFingerprints(contact, invite.getFingerprints());
481 }
482 switchToConversation(contact, invite == null ? null : invite.getBody());
483 return true;
484 }
485 });
486
487 mCurrentDialog = dialog.show();
488 }
489
490 @SuppressLint("InflateParams")
491 protected void showJoinConferenceDialog(final String prefilledJid) {
492 final AlertDialog.Builder builder = new AlertDialog.Builder(this);
493 builder.setTitle(R.string.dialog_title_join_conference);
494 final View dialogView = getLayoutInflater().inflate(R.layout.join_conference_dialog, null);
495 final Spinner spinner = dialogView.findViewById(R.id.account);
496 final AutoCompleteTextView jid = dialogView.findViewById(R.id.jid);
497 DelayedHintHelper.setHint(R.string.conference_address_example,jid);
498 jid.setAdapter(new KnownHostsAdapter(this, R.layout.simple_list_item, mKnownConferenceHosts));
499 if (prefilledJid != null) {
500 jid.append(prefilledJid);
501 }
502 populateAccountSpinner(this, mActivatedAccounts, spinner);
503 final Checkable bookmarkCheckBox = (CheckBox) dialogView
504 .findViewById(R.id.bookmark);
505 builder.setView(dialogView);
506 builder.setNegativeButton(R.string.cancel, null);
507 builder.setPositiveButton(R.string.join, null);
508 final AlertDialog dialog = builder.create();
509 dialog.show();
510 mCurrentDialog = dialog;
511 dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(v -> {
512 if (!xmppConnectionServiceBound) {
513 return;
514 }
515 final Account account = getSelectedAccount(spinner);
516 if (account == null) {
517 return;
518 }
519 final Jid conferenceJid;
520 try {
521 conferenceJid = Jid.fromString(jid.getText().toString());
522 } catch (final InvalidJidException e) {
523 jid.setError(getString(R.string.invalid_jid));
524 return;
525 }
526
527 if (bookmarkCheckBox.isChecked()) {
528 if (account.hasBookmarkFor(conferenceJid)) {
529 jid.setError(getString(R.string.bookmark_already_exists));
530 } else {
531 final Bookmark bookmark = new Bookmark(account, conferenceJid.toBareJid());
532 bookmark.setAutojoin(getPreferences().getBoolean("autojoin", getResources().getBoolean(R.bool.autojoin)));
533 String nick = conferenceJid.getResourcepart();
534 if (nick != null && !nick.isEmpty()) {
535 bookmark.setNick(nick);
536 }
537 account.getBookmarks().add(bookmark);
538 xmppConnectionService.pushBookmarks(account);
539 final Conversation conversation = xmppConnectionService
540 .findOrCreateConversation(account, conferenceJid, true, true, true);
541 bookmark.setConversation(conversation);
542 dialog.dismiss();
543 mCurrentDialog = null;
544 switchToConversation(conversation);
545 }
546 } else {
547 final Conversation conversation = xmppConnectionService
548 .findOrCreateConversation(account, conferenceJid, true, true, true);
549 dialog.dismiss();
550 mCurrentDialog = null;
551 switchToConversation(conversation);
552 }
553 });
554 }
555
556 private void showCreateConferenceDialog() {
557 final AlertDialog.Builder builder = new AlertDialog.Builder(this);
558 builder.setTitle(R.string.dialog_title_create_conference);
559 final View dialogView = getLayoutInflater().inflate(R.layout.create_conference_dialog, null);
560 final Spinner spinner = dialogView.findViewById(R.id.account);
561 final EditText subject = dialogView.findViewById(R.id.subject);
562 populateAccountSpinner(this, mActivatedAccounts, spinner);
563 builder.setView(dialogView);
564 builder.setPositiveButton(R.string.choose_participants, new OnClickListener() {
565 @Override
566 public void onClick(DialogInterface dialog, int which) {
567 if (!xmppConnectionServiceBound) {
568 return;
569 }
570 final Account account = getSelectedAccount(spinner);
571 if (account == null) {
572 return;
573 }
574 Intent intent = new Intent(getApplicationContext(), ChooseContactActivity.class);
575 intent.putExtra("multiple", true);
576 intent.putExtra("show_enter_jid", true);
577 intent.putExtra("subject", subject.getText().toString());
578 intent.putExtra(EXTRA_ACCOUNT, account.getJid().toBareJid().toString());
579 intent.putExtra(ChooseContactActivity.EXTRA_TITLE_RES_ID, R.string.choose_participants);
580 startActivityForResult(intent, REQUEST_CREATE_CONFERENCE);
581 }
582 });
583 builder.setNegativeButton(R.string.cancel, null);
584 mCurrentDialog = builder.create();
585 mCurrentDialog.show();
586 }
587
588 private Account getSelectedAccount(Spinner spinner) {
589 if (!spinner.isEnabled()) {
590 return null;
591 }
592 Jid jid;
593 try {
594 if (Config.DOMAIN_LOCK != null) {
595 jid = Jid.fromParts((String) spinner.getSelectedItem(), Config.DOMAIN_LOCK, null);
596 } else {
597 jid = Jid.fromString((String) spinner.getSelectedItem());
598 }
599 } catch (final InvalidJidException e) {
600 return null;
601 }
602 return xmppConnectionService.findAccountByJid(jid);
603 }
604
605 protected void switchToConversation(Contact contact, String body) {
606 Conversation conversation = xmppConnectionService
607 .findOrCreateConversation(contact.getAccount(),
608 contact.getJid(), false, true);
609 switchToConversation(conversation, body, false);
610 }
611
612 @Override
613 public boolean onCreateOptionsMenu(Menu menu) {
614 getMenuInflater().inflate(R.menu.start_conversation, menu);
615 MenuItem menuHideOffline = menu.findItem(R.id.action_hide_offline);
616 MenuItem joinGroupChat = menu.findItem(R.id.action_join_conference);
617 ActionBar bar = getSupportActionBar();
618 joinGroupChat.setVisible(bar != null && bar.getSelectedNavigationIndex() == 1);
619 menuHideOffline.setChecked(this.mHideOfflineContacts);
620 mMenuSearchView = menu.findItem(R.id.action_search);
621 mMenuSearchView.setOnActionExpandListener(mOnActionExpandListener);
622 View mSearchView = mMenuSearchView.getActionView();
623 mSearchEditText = mSearchView.findViewById(R.id.search_field);
624 mSearchEditText.addTextChangedListener(mSearchTextWatcher);
625 mSearchEditText.setOnEditorActionListener(mSearchDone);
626 if (mInitialJid != null) {
627 MenuItemCompat.expandActionView(mMenuSearchView);
628 mSearchEditText.append(mInitialJid);
629 filter(mInitialJid);
630 }
631 return super.onCreateOptionsMenu(menu);
632 }
633
634 @Override
635 public boolean onOptionsItemSelected(MenuItem item) {
636 switch (item.getItemId()) {
637 case R.id.action_join_conference:
638 showJoinConferenceDialog(null);
639 return true;
640 case R.id.action_scan_qr_code:
641 UriHandlerActivity.scan(this);
642 return true;
643 case R.id.action_hide_offline:
644 mHideOfflineContacts = !item.isChecked();
645 getPreferences().edit().putBoolean("hide_offline", mHideOfflineContacts).commit();
646 if (mSearchEditText != null) {
647 filter(mSearchEditText.getText().toString());
648 }
649 invalidateOptionsMenu();
650 }
651 return super.onOptionsItemSelected(item);
652 }
653
654 @Override
655 public boolean onKeyUp(int keyCode, KeyEvent event) {
656 if (keyCode == KeyEvent.KEYCODE_SEARCH && !event.isLongPress()) {
657 openSearch();
658 return true;
659 }
660 int c = event.getUnicodeChar();
661 if (c > 32) {
662 if (mSearchEditText != null && !mSearchEditText.isFocused()) {
663 openSearch();
664 mSearchEditText.append(Character.toString((char) c));
665 return true;
666 }
667 }
668 return super.onKeyUp(keyCode, event);
669 }
670
671 private void openSearch() {
672 if (mMenuSearchView != null) {
673 mMenuSearchView.expandActionView();
674 }
675 }
676
677 @Override
678 public void onActivityResult(int requestCode, int resultCode, Intent intent) {
679 if (resultCode == RESULT_OK) {
680 if (xmppConnectionServiceBound) {
681 this.mPostponedActivityResult = null;
682 if (requestCode == REQUEST_CREATE_CONFERENCE) {
683 Account account = extractAccount(intent);
684 final String subject = intent.getStringExtra("subject");
685 List<Jid> jids = new ArrayList<>();
686 if (intent.getBooleanExtra("multiple", false)) {
687 String[] toAdd = intent.getStringArrayExtra("contacts");
688 for (String item : toAdd) {
689 try {
690 jids.add(Jid.fromString(item));
691 } catch (InvalidJidException e) {
692 //ignored
693 }
694 }
695 } else {
696 try {
697 jids.add(Jid.fromString(intent.getStringExtra("contact")));
698 } catch (Exception e) {
699 //ignored
700 }
701 }
702 if (account != null && jids.size() > 0) {
703 if (xmppConnectionService.createAdhocConference(account, subject, jids, mAdhocConferenceCallback)) {
704 mToast = Toast.makeText(this, R.string.creating_conference, Toast.LENGTH_LONG);
705 mToast.show();
706 }
707 }
708 }
709 } else {
710 this.mPostponedActivityResult = new Pair<>(requestCode, intent);
711 }
712 }
713 super.onActivityResult(requestCode, requestCode, intent);
714 }
715
716 private void askForContactsPermissions() {
717 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
718 if (checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
719 if (mRequestedContactsPermission.compareAndSet(false, true)) {
720 if (shouldShowRequestPermissionRationale(Manifest.permission.READ_CONTACTS)) {
721 AlertDialog.Builder builder = new AlertDialog.Builder(this);
722 builder.setTitle(R.string.sync_with_contacts);
723 builder.setMessage(R.string.sync_with_contacts_long);
724 builder.setPositiveButton(R.string.next, new OnClickListener() {
725 @Override
726 public void onClick(DialogInterface dialog, int which) {
727 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
728 requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, REQUEST_SYNC_CONTACTS);
729 }
730 }
731 });
732 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
733 builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
734 @Override
735 public void onDismiss(DialogInterface dialog) {
736 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
737 requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, REQUEST_SYNC_CONTACTS);
738 }
739 }
740 });
741 }
742 builder.create().show();
743 } else {
744 requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, 0);
745 }
746 }
747 }
748 }
749 }
750
751 @Override
752 public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
753 if (grantResults.length > 0)
754 if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
755 if (requestCode == REQUEST_SYNC_CONTACTS && xmppConnectionServiceBound) {
756 xmppConnectionService.loadPhoneContacts();
757 }
758 }
759 }
760
761 @Override
762 protected void onBackendConnected() {
763 if (mPostponedActivityResult != null) {
764 onActivityResult(mPostponedActivityResult.first, RESULT_OK, mPostponedActivityResult.second);
765 this.mPostponedActivityResult = null;
766 }
767 this.mActivatedAccounts.clear();
768 for (Account account : xmppConnectionService.getAccounts()) {
769 if (account.getStatus() != Account.State.DISABLED) {
770 if (Config.DOMAIN_LOCK != null) {
771 this.mActivatedAccounts.add(account.getJid().getLocalpart());
772 } else {
773 this.mActivatedAccounts.add(account.getJid().toBareJid().toString());
774 }
775 }
776 }
777 final Intent intent = getIntent();
778 final ActionBar ab = getSupportActionBar();
779 boolean init = intent != null && intent.getBooleanExtra("init", false);
780 boolean noConversations = xmppConnectionService.getConversations().size() == 0;
781 if ((init || noConversations) && ab != null) {
782 ab.setDisplayShowHomeEnabled(false);
783 ab.setDisplayHomeAsUpEnabled(false);
784 ab.setHomeButtonEnabled(false);
785 }
786 this.mKnownHosts = xmppConnectionService.getKnownHosts();
787 this.mKnownConferenceHosts = xmppConnectionService.getKnownConferenceHosts();
788 if (this.mPendingInvite != null) {
789 mPendingInvite.invite();
790 this.mPendingInvite = null;
791 filter(null);
792 } else if (!handleIntent(getIntent())) {
793 if (mSearchEditText != null) {
794 filter(mSearchEditText.getText().toString());
795 } else {
796 filter(null);
797 }
798 } else {
799 filter(null);
800 }
801 setIntent(null);
802 }
803
804 protected boolean handleIntent(Intent intent) {
805 if (intent == null) {
806 return false;
807 }
808 final String inviteUri = intent.getStringExtra(WelcomeActivity.EXTRA_INVITE_URI);
809 if (inviteUri != null) {
810 Invite invite = new Invite(inviteUri);
811 if (invite.isJidValid()) {
812 return invite.invite();
813 }
814 }
815 if (intent.getAction() == null) {
816 return false;
817 }
818 switch (intent.getAction()) {
819 case Intent.ACTION_SENDTO:
820 case Intent.ACTION_VIEW:
821 Uri uri = intent.getData();
822 if (uri != null) {
823 Invite invite = new Invite(intent.getData(), false);
824 invite.account = intent.getStringExtra("account");
825 return invite.invite();
826 } else {
827 return false;
828 }
829 }
830 return false;
831 }
832
833 private boolean handleJid(Invite invite) {
834 List<Contact> contacts = xmppConnectionService.findContacts(invite.getJid(), invite.account);
835 if (invite.isAction(XmppUri.ACTION_JOIN)) {
836 Conversation muc = xmppConnectionService.findFirstMuc(invite.getJid());
837 if (muc != null) {
838 switchToConversation(muc, invite.getBody(), false);
839 return true;
840 } else {
841 showJoinConferenceDialog(invite.getJid().toBareJid().toString());
842 return false;
843 }
844 } else if (contacts.size() == 0) {
845 showCreateContactDialog(invite.getJid().toString(), invite);
846 return false;
847 } else if (contacts.size() == 1) {
848 Contact contact = contacts.get(0);
849 if (!invite.isSafeSource() && invite.hasFingerprints()) {
850 displayVerificationWarningDialog(contact, invite);
851 } else {
852 if (invite.hasFingerprints()) {
853 if (xmppConnectionService.verifyFingerprints(contact, invite.getFingerprints())) {
854 Toast.makeText(this, R.string.verified_fingerprints, Toast.LENGTH_SHORT).show();
855 }
856 }
857 if (invite.account != null) {
858 xmppConnectionService.getShortcutService().report(contact);
859 }
860 switchToConversation(contact, invite.getBody());
861 }
862 return true;
863 } else {
864 if (mMenuSearchView != null) {
865 mMenuSearchView.expandActionView();
866 mSearchEditText.setText("");
867 mSearchEditText.append(invite.getJid().toString());
868 filter(invite.getJid().toString());
869 } else {
870 mInitialJid = invite.getJid().toString();
871 }
872 return true;
873 }
874 }
875
876 private void displayVerificationWarningDialog(final Contact contact, final Invite invite) {
877 AlertDialog.Builder builder = new AlertDialog.Builder(this);
878 builder.setTitle(R.string.verify_omemo_keys);
879 View view = getLayoutInflater().inflate(R.layout.dialog_verify_fingerprints, null);
880 final CheckBox isTrustedSource = (CheckBox) view.findViewById(R.id.trusted_source);
881 TextView warning = (TextView) view.findViewById(R.id.warning);
882 String jid = contact.getJid().toBareJid().toString();
883 SpannableString spannable = new SpannableString(getString(R.string.verifying_omemo_keys_trusted_source, jid, contact.getDisplayName()));
884 int start = spannable.toString().indexOf(jid);
885 if (start >= 0) {
886 spannable.setSpan(new TypefaceSpan("monospace"), start, start + jid.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
887 }
888 warning.setText(spannable);
889 builder.setView(view);
890 builder.setPositiveButton(R.string.confirm, (dialog, which) -> {
891 if (isTrustedSource.isChecked() && invite.hasFingerprints()) {
892 xmppConnectionService.verifyFingerprints(contact, invite.getFingerprints());
893 }
894 switchToConversation(contact, invite.getBody());
895 });
896 builder.setNegativeButton(R.string.cancel, (dialog, which) -> StartConversationActivity.this.finish());
897 AlertDialog dialog = builder.create();
898 dialog.setCanceledOnTouchOutside(false);
899 dialog.setOnCancelListener(dialog1 -> StartConversationActivity.this.finish());
900 dialog.show();
901 }
902
903 protected void filter(String needle) {
904 if (xmppConnectionServiceBound) {
905 this.filterContacts(needle);
906 this.filterConferences(needle);
907 }
908 }
909
910 protected void filterContacts(String needle) {
911 this.contacts.clear();
912 for (Account account : xmppConnectionService.getAccounts()) {
913 if (account.getStatus() != Account.State.DISABLED) {
914 for (Contact contact : account.getRoster().getContacts()) {
915 Presence.Status s = contact.getShownStatus();
916 if (contact.showInRoster() && contact.match(this, needle)
917 && (!this.mHideOfflineContacts
918 || (needle != null && !needle.trim().isEmpty())
919 || s.compareTo(Presence.Status.OFFLINE) < 0)) {
920 this.contacts.add(contact);
921 }
922 }
923 }
924 }
925 Collections.sort(this.contacts);
926 mContactsAdapter.notifyDataSetChanged();
927 }
928
929 protected void filterConferences(String needle) {
930 this.conferences.clear();
931 for (Account account : xmppConnectionService.getAccounts()) {
932 if (account.getStatus() != Account.State.DISABLED) {
933 for (Bookmark bookmark : account.getBookmarks()) {
934 if (bookmark.match(this, needle)) {
935 this.conferences.add(bookmark);
936 }
937 }
938 }
939 }
940 Collections.sort(this.conferences);
941 mConferenceAdapter.notifyDataSetChanged();
942 }
943
944 private void onTabChanged() {
945 @DrawableRes final int fabDrawable;
946 if (getSupportActionBar().getSelectedNavigationIndex() == 0) {
947 fabDrawable = R.drawable.ic_person_add_white_24dp;
948 } else {
949 fabDrawable = R.drawable.ic_group_add_white_24dp;
950 }
951 binding.fab.setImageResource(fabDrawable);
952 invalidateOptionsMenu();
953 }
954
955 @Override
956 public void OnUpdateBlocklist(final Status status) {
957 refreshUi();
958 }
959
960 @Override
961 protected void refreshUiReal() {
962 if (mSearchEditText != null) {
963 filter(mSearchEditText.getText().toString());
964 }
965 }
966
967 public static class MyListFragment extends ListFragment {
968 private AdapterView.OnItemClickListener mOnItemClickListener;
969 private int mResContextMenu;
970
971 public void setContextMenu(final int res) {
972 this.mResContextMenu = res;
973 }
974
975 @Override
976 public void onListItemClick(final ListView l, final View v, final int position, final long id) {
977 if (mOnItemClickListener != null) {
978 mOnItemClickListener.onItemClick(l, v, position, id);
979 }
980 }
981
982 public void setOnListItemClickListener(AdapterView.OnItemClickListener l) {
983 this.mOnItemClickListener = l;
984 }
985
986 @Override
987 public void onViewCreated(@NonNull final View view, final Bundle savedInstanceState) {
988 super.onViewCreated(view, savedInstanceState);
989 registerForContextMenu(getListView());
990 getListView().setFastScrollEnabled(true);
991 getListView().setDivider(null);
992 getListView().setDividerHeight(0);
993 }
994
995 @Override
996 public void onCreateContextMenu(final ContextMenu menu, final View v,
997 final ContextMenuInfo menuInfo) {
998 super.onCreateContextMenu(menu, v, menuInfo);
999 final StartConversationActivity activity = (StartConversationActivity) getActivity();
1000 if (activity == null) {
1001 return;
1002 }
1003 activity.getMenuInflater().inflate(mResContextMenu, menu);
1004 final AdapterView.AdapterContextMenuInfo acmi = (AdapterContextMenuInfo) menuInfo;
1005 if (mResContextMenu == R.menu.conference_context) {
1006 activity.conference_context_id = acmi.position;
1007 } else if (mResContextMenu == R.menu.contact_context) {
1008 activity.contact_context_id = acmi.position;
1009 final Contact contact = (Contact) activity.contacts.get(acmi.position);
1010 final MenuItem blockUnblockItem = menu.findItem(R.id.context_contact_block_unblock);
1011 final MenuItem showContactDetailsItem = menu.findItem(R.id.context_contact_details);
1012 if (contact.isSelf()) {
1013 showContactDetailsItem.setVisible(false);
1014 }
1015 XmppConnection xmpp = contact.getAccount().getXmppConnection();
1016 if (xmpp != null && xmpp.getFeatures().blocking() && !contact.isSelf()) {
1017 if (contact.isBlocked()) {
1018 blockUnblockItem.setTitle(R.string.unblock_contact);
1019 } else {
1020 blockUnblockItem.setTitle(R.string.block_contact);
1021 }
1022 } else {
1023 blockUnblockItem.setVisible(false);
1024 }
1025 }
1026 }
1027
1028 @Override
1029 public boolean onContextItemSelected(final MenuItem item) {
1030 StartConversationActivity activity = (StartConversationActivity) getActivity();
1031 if (activity == null) {
1032 return true;
1033 }
1034 switch (item.getItemId()) {
1035 case R.id.context_start_conversation:
1036 activity.openConversationForContact();
1037 break;
1038 case R.id.context_contact_details:
1039 activity.openDetailsForContact();
1040 break;
1041 case R.id.context_contact_block_unblock:
1042 activity.toggleContactBlock();
1043 break;
1044 case R.id.context_delete_contact:
1045 activity.deleteContact();
1046 break;
1047 case R.id.context_join_conference:
1048 activity.openConversationForBookmark();
1049 break;
1050 case R.id.context_share_uri:
1051 activity.shareBookmarkUri();
1052 break;
1053 case R.id.context_delete_conference:
1054 activity.deleteConference();
1055 }
1056 return true;
1057 }
1058 }
1059
1060 public class ListPagerAdapter extends PagerAdapter {
1061 FragmentManager fragmentManager;
1062 MyListFragment[] fragments;
1063
1064 public ListPagerAdapter(FragmentManager fm) {
1065 fragmentManager = fm;
1066 fragments = new MyListFragment[2];
1067 }
1068
1069 public void requestFocus(int pos) {
1070 if (fragments.length > pos) {
1071 fragments[pos].getListView().requestFocus();
1072 }
1073 }
1074
1075 @Override
1076 public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
1077 assert (0 <= position && position < fragments.length);
1078 FragmentTransaction trans = fragmentManager.beginTransaction();
1079 trans.remove(fragments[position]);
1080 trans.commit();
1081 fragments[position] = null;
1082 }
1083
1084 @Override
1085 public Fragment instantiateItem(@NonNull ViewGroup container, int position) {
1086 Fragment fragment = getItem(position);
1087 FragmentTransaction trans = fragmentManager.beginTransaction();
1088 trans.add(container.getId(), fragment, "fragment:" + position);
1089 trans.commit();
1090 return fragment;
1091 }
1092
1093 @Override
1094 public int getCount() {
1095 return fragments.length;
1096 }
1097
1098 @Override
1099 public boolean isViewFromObject(@NonNull View view, @NonNull Object fragment) {
1100 return ((Fragment) fragment).getView() == view;
1101 }
1102
1103 public Fragment getItem(int position) {
1104 assert (0 <= position && position < fragments.length);
1105 if (fragments[position] == null) {
1106 final MyListFragment listFragment = new MyListFragment();
1107 if (position == 1) {
1108 listFragment.setListAdapter(mConferenceAdapter);
1109 listFragment.setContextMenu(R.menu.conference_context);
1110 listFragment.setOnListItemClickListener((arg0, arg1, p, arg3) -> openConversationForBookmark(p));
1111 } else {
1112
1113 listFragment.setListAdapter(mContactsAdapter);
1114 listFragment.setContextMenu(R.menu.contact_context);
1115 listFragment.setOnListItemClickListener((arg0, arg1, p, arg3) -> openConversationForContact(p));
1116 }
1117 fragments[position] = listFragment;
1118 }
1119 return fragments[position];
1120 }
1121 }
1122
1123 private class Invite extends XmppUri {
1124
1125 public String account;
1126
1127 public Invite(final Uri uri) {
1128 super(uri);
1129 }
1130
1131 public Invite(final String uri) {
1132 super(uri);
1133 }
1134
1135 public Invite(Uri uri, boolean safeSource) {
1136 super(uri, safeSource);
1137 }
1138
1139 boolean invite() {
1140 if (!isJidValid()) {
1141 Toast.makeText(StartConversationActivity.this, R.string.invalid_jid, Toast.LENGTH_SHORT).show();
1142 return false;
1143 }
1144 if (getJid() != null) {
1145 return handleJid(this);
1146 }
1147 return false;
1148 }
1149 }
1150}