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