aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/ui/SearchActivity.java
blob: 54c5ef5e4a643bc63cfaf536c1c4942eea9e9a63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
 * Copyright (c) 2018, Daniel Gultsch All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation and/or
 * other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors
 * may be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package de.pixart.messenger.ui;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import de.pixart.messenger.R;
import de.pixart.messenger.databinding.ActivitySearchBinding;
import de.pixart.messenger.entities.Contact;
import de.pixart.messenger.entities.Conversation;
import de.pixart.messenger.entities.Conversational;
import de.pixart.messenger.entities.Message;
import de.pixart.messenger.services.MessageSearchTask;
import de.pixart.messenger.ui.adapter.MessageAdapter;
import de.pixart.messenger.ui.interfaces.OnSearchResultsAvailable;
import de.pixart.messenger.ui.util.ChangeWatcher;
import de.pixart.messenger.ui.util.Color;
import de.pixart.messenger.ui.util.DateSeparator;
import de.pixart.messenger.ui.util.Drawable;
import de.pixart.messenger.ui.util.ListViewUtils;
import de.pixart.messenger.ui.util.ShareUtil;
import de.pixart.messenger.utils.MessageUtils;

import static de.pixart.messenger.ui.util.SoftKeyboardUtils.hideSoftKeyboard;
import static de.pixart.messenger.ui.util.SoftKeyboardUtils.showKeyboard;

public class SearchActivity extends XmppActivity implements TextWatcher, OnSearchResultsAvailable, MessageAdapter.OnContactPictureClicked {

    private ActivitySearchBinding binding;
    private MessageAdapter messageListAdapter;
    private final List<Message> messages = new ArrayList<>();
    private WeakReference<Message> selectedMessageReference = new WeakReference<>(null);
	private final ChangeWatcher<String> currentSearch = new ChangeWatcher<>();

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.binding = DataBindingUtil.setContentView(this, R.layout.activity_search);
        setSupportActionBar((Toolbar) this.binding.toolbar);
        configureActionBar(getSupportActionBar());
        this.messageListAdapter = new MessageAdapter(this, this.messages);
        this.messageListAdapter.setOnContactPictureClicked(this);
        this.binding.searchResults.setAdapter(messageListAdapter);
        registerForContextMenu(this.binding.searchResults);
    }

    @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        getMenuInflater().inflate(R.menu.activity_search, menu);
        MenuItem searchActionMenuItem = menu.findItem(R.id.action_search);
        EditText searchField = searchActionMenuItem.getActionView().findViewById(R.id.search_field);
        searchField.addTextChangedListener(this);
        searchField.setHint(R.string.search_messages);
        searchField.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE);
        showKeyboard(searchField);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        AdapterView.AdapterContextMenuInfo acmi = (AdapterView.AdapterContextMenuInfo) menuInfo;
        final Message message = this.messages.get(acmi.position);
        this.selectedMessageReference = new WeakReference<>(message);
        getMenuInflater().inflate(R.menu.search_result_context, menu);
        MenuItem copy = menu.findItem(R.id.copy_message);
        MenuItem quote = menu.findItem(R.id.quote_message);
        MenuItem copyUrl = menu.findItem(R.id.copy_url);
        if (message.isGeoUri()) {
            copy.setVisible(false);
            quote.setVisible(false);
        } else {
            copyUrl.setVisible(false);
        }
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            hideSoftKeyboard(this);
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        final Message message = selectedMessageReference.get();
        if (message != null) {
            switch (item.getItemId()) {
                case R.id.share_with:
                    ShareUtil.share(this, message);
                    break;
                case R.id.copy_message:
                    ShareUtil.copyToClipboard(this, message);
                    break;
                case R.id.copy_url:
                    ShareUtil.copyUrlToClipboard(this, message);
                    break;
                case R.id.quote_message:
                    quote(message);
            }
        }
        return super.onContextItemSelected(item);
    }

    private void quote(Message message) {
        String text = MessageUtils.prepareQuote(message);
        final Conversational conversational = message.getConversation();
        final Conversation conversation;
        if (conversational instanceof Conversation) {
            conversation = (Conversation) conversational;
        } else {
            conversation = xmppConnectionService.findOrCreateConversation(conversational.getAccount(),
                    conversational.getJid(),
                    conversational.getMode() == Conversational.MODE_MULTI,
                    true,
                    true);
        }
        switchToConversationAndQuote(conversation, text);
    }

    @Override
    protected void refreshUiReal() {

    }

    @Override
    void onBackendConnected() {

    }

    private void changeBackground(boolean hasSearch, boolean hasResults) {
        if (hasSearch) {
            if (hasResults) {
                binding.searchResults.setBackgroundColor(Color.get(this, R.attr.color_background_secondary));
            } else {
                binding.searchResults.setBackground(Drawable.get(this, R.attr.activity_background_no_results));
            }
        } else {
            binding.searchResults.setBackground(Drawable.get(this, R.attr.activity_background_search));
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        final String term = s.toString().trim();
        if (!currentSearch.watch(term)) {
            return;
        }
        if (term.length() > 0) {
            xmppConnectionService.search(s.toString().trim(), this);
        } else {
            MessageSearchTask.cancelRunningTasks();
            this.messages.clear();
            messageListAdapter.notifyDataSetChanged();
            changeBackground(false, false);
        }
    }

    @Override
    public void onSearchResultsAvailable(String term, List<Message> messages) {
        runOnUiThread(() -> {
            this.messages.clear();
            DateSeparator.addAll(messages);
            this.messages.addAll(messages);
            messageListAdapter.notifyDataSetChanged();
            changeBackground(true, messages.size() > 0);
            ListViewUtils.scrollToBottom(this.binding.searchResults);
        });
    }

    @Override
    public void onContactPictureClicked(Message message) {
        String fingerprint;
        if (message.getEncryption() == Message.ENCRYPTION_PGP || message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
            fingerprint = "pgp";
        } else {
            fingerprint = message.getFingerprint();
        }
        if (message.getStatus() == Message.STATUS_RECEIVED) {
            final Contact contact = message.getContact();
            if (contact != null) {
                if (contact.isSelf()) {
                    switchToAccount(message.getConversation().getAccount(), fingerprint);
                } else {
                    switchToContactDetails(contact, fingerprint);
                }
            }
        } else {
            switchToAccount(message.getConversation().getAccount(), fingerprint);
        }
    }
}