1/*
2 * Copyright (c) 2018, Daniel Gultsch All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation and/or
12 * other materials provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors
15 * may be used to endorse or promote products derived from this software without
16 * specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30package eu.siacs.conversations.services;
31
32import android.database.Cursor;
33import android.os.SystemClock;
34import android.util.Log;
35
36import java.util.ArrayList;
37import java.util.Arrays;
38import java.util.HashMap;
39import java.util.List;
40
41import eu.siacs.conversations.Config;
42import eu.siacs.conversations.entities.Account;
43import eu.siacs.conversations.entities.Conversation;
44import eu.siacs.conversations.entities.Conversational;
45import eu.siacs.conversations.entities.IndividualMessage;
46import eu.siacs.conversations.entities.Message;
47import eu.siacs.conversations.entities.StubConversation;
48import eu.siacs.conversations.ui.interfaces.OnSearchResultsAvailable;
49import eu.siacs.conversations.utils.Cancellable;
50import eu.siacs.conversations.utils.ReplacingSerialSingleThreadExecutor;
51import rocks.xmpp.addr.Jid;
52
53public class MessageSearchTask implements Runnable, Cancellable {
54
55 private static final ReplacingSerialSingleThreadExecutor EXECUTOR = new ReplacingSerialSingleThreadExecutor(MessageSearchTask.class.getName());
56
57 private final XmppConnectionService xmppConnectionService;
58 private final String term;
59 private final OnSearchResultsAvailable onSearchResultsAvailable;
60
61 private boolean isCancelled = false;
62
63 private MessageSearchTask(XmppConnectionService xmppConnectionService, String term, OnSearchResultsAvailable onSearchResultsAvailable) {
64 this.xmppConnectionService = xmppConnectionService;
65 this.term = term;
66 this.onSearchResultsAvailable = onSearchResultsAvailable;
67 }
68
69 public static void search(XmppConnectionService xmppConnectionService, String term, OnSearchResultsAvailable onSearchResultsAvailable) {
70 new MessageSearchTask(xmppConnectionService, term, onSearchResultsAvailable).executeInBackground();
71 }
72
73 public static void cancelRunningTasks() {
74 EXECUTOR.cancelRunningTasks();
75 }
76
77 @Override
78 public void cancel() {
79 this.isCancelled = true;
80 }
81
82 @Override
83 public void run() {
84 long startTimestamp = SystemClock.elapsedRealtime();
85 Cursor cursor = null;
86 try {
87 final HashMap<String, Conversational> conversationCache = new HashMap<>();
88 final List<Message> result = new ArrayList<>();
89 cursor = xmppConnectionService.databaseBackend.getMessageSearchCursor(term);
90 if (isCancelled) {
91 Log.d(Config.LOGTAG, "canceled search task");
92 return;
93 }
94 if (cursor != null && cursor.getCount() > 0) {
95 cursor.moveToLast();
96 do {
97 if (isCancelled) {
98 Log.d(Config.LOGTAG, "canceled search task");
99 return;
100 }
101 final String conversationUuid = cursor.getString(cursor.getColumnIndex(Message.CONVERSATION));
102 Conversational conversation;
103 if (conversationCache.containsKey(conversationUuid)) {
104 conversation = conversationCache.get(conversationUuid);
105 } else {
106 String accountUuid = cursor.getString(cursor.getColumnIndex(Conversation.ACCOUNT));
107 String contactJid = cursor.getString(cursor.getColumnIndex(Conversation.CONTACTJID));
108 int mode = cursor.getInt(cursor.getColumnIndex(Conversation.MODE));
109 conversation = findOrGenerateStub(conversationUuid, accountUuid, contactJid, mode);
110 conversationCache.put(conversationUuid, conversation);
111 }
112 Message message = IndividualMessage.fromCursor(cursor, conversation);
113 result.add(message);
114 } while (cursor.moveToPrevious());
115 }
116 long stopTimestamp = SystemClock.elapsedRealtime();
117 Log.d(Config.LOGTAG, "found " + result.size() + " messages in " + (stopTimestamp - startTimestamp) + "ms");
118 onSearchResultsAvailable.onSearchResultsAvailable(term, result);
119 } catch (Exception e) {
120 Log.d(Config.LOGTAG, "exception while searching ", e);
121 } finally {
122 if (cursor != null) {
123 cursor.close();
124 }
125 }
126 }
127
128 private Conversational findOrGenerateStub(String conversationUuid, String accountUuid, String contactJid, int mode) throws Exception {
129 Conversation conversation = xmppConnectionService.findConversationByUuid(conversationUuid);
130 if (conversation != null) {
131 return conversation;
132 }
133 Account account = xmppConnectionService.findAccountByUuid(accountUuid);
134 Jid jid = Jid.of(contactJid);
135 if (account != null && jid != null) {
136 return new StubConversation(account, conversationUuid, jid.asBareJid(), mode);
137 }
138 throw new Exception("Unable to generate stub for " + contactJid);
139 }
140
141 private void executeInBackground() {
142 EXECUTOR.execute(this);
143 }
144}