1package eu.siacs.conversations.ui;
2
3import android.content.ComponentName;
4import android.content.Context;
5import android.content.Intent;
6import android.os.Bundle;
7import android.view.inputmethod.InputMethodManager;
8
9import androidx.appcompat.app.ActionBar;
10
11import java.util.Arrays;
12import java.util.Collections;
13import java.util.List;
14
15import eu.siacs.conversations.R;
16import eu.siacs.conversations.entities.Account;
17import eu.siacs.conversations.entities.Contact;
18import eu.siacs.conversations.entities.ListItem;
19
20public class ShortcutActivity extends AbstractSearchableListItemActivity {
21
22 private static final List<String> BLACKLISTED_ACTIVITIES = Arrays.asList("com.teslacoilsw.launcher.ChooseActionIntentActivity");
23
24 @Override
25 protected void refreshUiReal() {
26
27 }
28
29 @Override
30 public void onCreate(Bundle savedInstanceState) {
31 super.onCreate(savedInstanceState);
32 getListView().setOnItemClickListener((parent, view, position, id) -> {
33
34 final ComponentName callingActivity = getCallingActivity();
35
36 final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
37 imm.hideSoftInputFromWindow(getSearchEditText().getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
38
39 ListItem listItem = getListItems().get(position);
40 final boolean legacy = BLACKLISTED_ACTIVITIES.contains(callingActivity == null ? null : callingActivity.getClassName());
41 Intent shortcut = xmppConnectionService.getShortcutService().createShortcut(((Contact) listItem), legacy);
42 setResult(RESULT_OK,shortcut);
43 finish();
44 });
45 }
46
47 @Override
48 public void onStart() {
49 super.onStart();
50 ActionBar bar = getSupportActionBar();
51 if(bar != null){
52 bar.setTitle(R.string.create_shortcut);
53 }
54 }
55
56 @Override
57 protected void filterContacts(String needle) {
58 getListItems().clear();
59 if (xmppConnectionService == null) {
60 getListItemAdapter().notifyDataSetChanged();
61 return;
62 }
63 for (final Account account : xmppConnectionService.getAccounts()) {
64 if (account.isEnabled()) {
65 for (final Contact contact : account.getRoster().getContacts()) {
66 if (contact.showInContactList()
67 && contact.match(this, needle)) {
68 getListItems().add(contact);
69 }
70 }
71 }
72 }
73 Collections.sort(getListItems());
74 getListItemAdapter().notifyDataSetChanged();
75 }
76}