diff options
Diffstat (limited to 'src')
26 files changed, 634 insertions, 126 deletions
diff --git a/src/main/java/de/pixart/messenger/crypto/PgpEngine.java b/src/main/java/de/pixart/messenger/crypto/PgpEngine.java index f4490ed07..3be5ceb23 100644 --- a/src/main/java/de/pixart/messenger/crypto/PgpEngine.java +++ b/src/main/java/de/pixart/messenger/crypto/PgpEngine.java @@ -48,7 +48,7 @@ public class PgpEngine { public void encrypt(final Message message, final UiCallback<Message> callback) { Intent params = new Intent(); params.setAction(OpenPgpApi.ACTION_ENCRYPT); - final Conversation conversation = message.getConversation(); + final Conversation conversation = (Conversation) message.getConversation(); if (conversation.getMode() == Conversation.MODE_SINGLE) { long[] keys = { conversation.getContact().getPgpKeyId(), diff --git a/src/main/java/de/pixart/messenger/crypto/axolotl/AxolotlService.java b/src/main/java/de/pixart/messenger/crypto/axolotl/AxolotlService.java index 0cc823ba3..989fe90a4 100644 --- a/src/main/java/de/pixart/messenger/crypto/axolotl/AxolotlService.java +++ b/src/main/java/de/pixart/messenger/crypto/axolotl/AxolotlService.java @@ -1307,7 +1307,7 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded { if (message.getType() == Message.TYPE_PRIVATE) { success = buildHeader(axolotlMessage, message.getTrueCounterpart()); } else { - success = buildHeader(axolotlMessage, message.getConversation()); + success = buildHeader(axolotlMessage, (Conversation) message.getConversation()); } return success ? axolotlMessage : null; } diff --git a/src/main/java/de/pixart/messenger/entities/Conversation.java b/src/main/java/de/pixart/messenger/entities/Conversation.java index d2d081a90..3aaa89529 100644 --- a/src/main/java/de/pixart/messenger/entities/Conversation.java +++ b/src/main/java/de/pixart/messenger/entities/Conversation.java @@ -36,15 +36,12 @@ import rocks.xmpp.addr.Jid; import static de.pixart.messenger.entities.Bookmark.printableValue; -public class Conversation extends AbstractEntity implements Blockable, Comparable<Conversation> { +public class Conversation extends AbstractEntity implements Blockable, Comparable<Conversation>, Conversational { public static final String TABLENAME = "conversations"; public static final int STATUS_AVAILABLE = 0; public static final int STATUS_ARCHIVED = 1; - public static final int MODE_MULTI = 1; - public static final int MODE_SINGLE = 0; - public static final String NAME = "name"; public static final String ACCOUNT = "accountUuid"; public static final String CONTACT = "contactUuid"; diff --git a/src/main/java/de/pixart/messenger/entities/Conversational.java b/src/main/java/de/pixart/messenger/entities/Conversational.java new file mode 100644 index 000000000..27482e195 --- /dev/null +++ b/src/main/java/de/pixart/messenger/entities/Conversational.java @@ -0,0 +1,47 @@ +/* + * 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.entities; + +import rocks.xmpp.addr.Jid; + +public interface Conversational { + + int MODE_MULTI = 1; + int MODE_SINGLE = 0; + + Account getAccount(); + + Contact getContact(); + + Jid getJid(); + + int getMode(); + + String getUuid(); +}
\ No newline at end of file diff --git a/src/main/java/de/pixart/messenger/entities/IndividualMessage.java b/src/main/java/de/pixart/messenger/entities/IndividualMessage.java new file mode 100644 index 000000000..7a1030fef --- /dev/null +++ b/src/main/java/de/pixart/messenger/entities/IndividualMessage.java @@ -0,0 +1,106 @@ +/* + * 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.entities; + +import android.database.Cursor; + +import java.util.Set; + +import rocks.xmpp.addr.Jid; + +public class IndividualMessage extends Message { + + + private IndividualMessage(Conversational conversation, String uuid, String conversationUUid, Jid counterpart, Jid trueCounterpart, String body, long timeSent, int encryption, int status, int type, boolean carbon, String remoteMsgId, String relativeFilePath, String serverMsgId, String fingerprint, boolean read, String edited, boolean oob, String errorMessage, Set<ReadByMarker> readByMarkers, boolean markable) { + super(conversation, uuid, conversationUUid, counterpart, trueCounterpart, body, timeSent, encryption, status, type, carbon, remoteMsgId, relativeFilePath, serverMsgId, fingerprint, read, edited, oob, errorMessage, readByMarkers, markable); + } + + public static Message fromCursor(Cursor cursor, Conversational conversation) { + Jid jid; + try { + String value = cursor.getString(cursor.getColumnIndex(COUNTERPART)); + if (value != null) { + jid = Jid.of(value); + } else { + jid = null; + } + } catch (IllegalArgumentException e) { + jid = null; + } catch (IllegalStateException e) { + return null; // message too long? + } + Jid trueCounterpart; + try { + String value = cursor.getString(cursor.getColumnIndex(TRUE_COUNTERPART)); + if (value != null) { + trueCounterpart = Jid.of(value); + } else { + trueCounterpart = null; + } + } catch (IllegalArgumentException e) { + trueCounterpart = null; + } + return new IndividualMessage(conversation, + cursor.getString(cursor.getColumnIndex(UUID)), + cursor.getString(cursor.getColumnIndex(CONVERSATION)), + jid, + trueCounterpart, + cursor.getString(cursor.getColumnIndex(BODY)), + cursor.getLong(cursor.getColumnIndex(TIME_SENT)), + cursor.getInt(cursor.getColumnIndex(ENCRYPTION)), + cursor.getInt(cursor.getColumnIndex(STATUS)), + cursor.getInt(cursor.getColumnIndex(TYPE)), + cursor.getInt(cursor.getColumnIndex(CARBON)) > 0, + cursor.getString(cursor.getColumnIndex(REMOTE_MSG_ID)), + cursor.getString(cursor.getColumnIndex(RELATIVE_FILE_PATH)), + cursor.getString(cursor.getColumnIndex(SERVER_MSG_ID)), + cursor.getString(cursor.getColumnIndex(FINGERPRINT)), + cursor.getInt(cursor.getColumnIndex(READ)) > 0, + cursor.getString(cursor.getColumnIndex(EDITED)), + cursor.getInt(cursor.getColumnIndex(OOB)) > 0, + cursor.getString(cursor.getColumnIndex(ERROR_MESSAGE)), + ReadByMarker.fromJsonString(cursor.getString(cursor.getColumnIndex(READ_BY_MARKERS))), + cursor.getInt(cursor.getColumnIndex(MARKABLE)) > 0); + } + + @Override + public Message next() { + return null; + } + + @Override + public Message prev() { + return null; + } + + @Override + public boolean isValidInSession() { + return true; + } +}
\ No newline at end of file diff --git a/src/main/java/de/pixart/messenger/entities/Message.java b/src/main/java/de/pixart/messenger/entities/Message.java index 5df54861f..605fa0001 100644 --- a/src/main/java/de/pixart/messenger/entities/Message.java +++ b/src/main/java/de/pixart/messenger/entities/Message.java @@ -92,7 +92,7 @@ public class Message extends AbstractEntity { protected boolean read = true; protected String remoteMsgId = null; protected String serverMsgId = null; - private final Conversation conversation; + private final Conversational conversation; protected Transferable transferable = null; private Message mNextMessage = null; private Message mPreviousMessage = null; @@ -108,15 +108,15 @@ public class Message extends AbstractEntity { private List<MucOptions.User> counterparts; private WeakReference<MucOptions.User> user; - private Message(Conversation conversation) { + private Message(Conversational conversation) { this.conversation = conversation; } - public Message(Conversation conversation, String body, int encryption) { + public Message(Conversational conversation, String body, int encryption) { this(conversation, body, encryption, STATUS_UNSEND); } - public Message(Conversation conversation, String body, int encryption, int status) { + public Message(Conversational conversation, String body, int encryption, int status) { this(conversation, java.util.UUID.randomUUID().toString(), conversation.getUuid(), conversation.getJid() == null ? null : conversation.getJid().asBareJid(), @@ -139,7 +139,7 @@ public class Message extends AbstractEntity { false); } - private Message(final Conversation conversation, final String uuid, final String conversationUUid, final Jid counterpart, + protected Message(final Conversational conversation, final String uuid, final String conversationUUid, final Jid counterpart, final Jid trueCounterpart, final String body, final long timeSent, final int encryption, final int status, final int type, final boolean carbon, final String remoteMsgId, final String relativeFilePath, @@ -278,7 +278,7 @@ public class Message extends AbstractEntity { return conversationUuid; } - public Conversation getConversation() { + public Conversational getConversation() { return this.conversation; } @@ -514,30 +514,40 @@ public class Message extends AbstractEntity { } public Message next() { - synchronized (this.conversation.messages) { - if (this.mNextMessage == null) { - int index = this.conversation.messages.indexOf(this); - if (index < 0 || index >= this.conversation.messages.size() - 1) { - this.mNextMessage = null; - } else { - this.mNextMessage = this.conversation.messages.get(index + 1); + if (this.conversation instanceof Conversation) { + final Conversation conversation = (Conversation) this.conversation; + synchronized (conversation.messages) { + if (this.mNextMessage == null) { + int index = conversation.messages.indexOf(this); + if (index < 0 || index >= conversation.messages.size() - 1) { + this.mNextMessage = null; + } else { + this.mNextMessage = conversation.messages.get(index + 1); + } } + return this.mNextMessage; } - return this.mNextMessage; + } else { + throw new AssertionError("Calling next should be disabled for stubs"); } } public Message prev() { - synchronized (this.conversation.messages) { - if (this.mPreviousMessage == null) { - int index = this.conversation.messages.indexOf(this); - if (index <= 0 || index > this.conversation.messages.size()) { - this.mPreviousMessage = null; - } else { - this.mPreviousMessage = this.conversation.messages.get(index - 1); + if (this.conversation instanceof Conversation) { + final Conversation conversation = (Conversation) this.conversation; + synchronized (conversation.messages) { + if (this.mPreviousMessage == null) { + int index = conversation.messages.indexOf(this); + if (index <= 0 || index > conversation.messages.size()) { + this.mPreviousMessage = null; + } else { + this.mPreviousMessage = conversation.messages.get(index - 1); + } } } return this.mPreviousMessage; + } else { + throw new AssertionError("Calling prev should be disabled for stubs"); } } @@ -893,13 +903,18 @@ public class Message extends AbstractEntity { } private int getNextEncryption() { - for (Message iterator = this.next(); iterator != null; iterator = iterator.next()) { - if (iterator.isCarbon() || iterator.getStatus() == STATUS_RECEIVED) { - continue; + if (this.conversation instanceof Conversation) { + Conversation conversation = (Conversation) this.conversation; + for (Message iterator = this.next(); iterator != null; iterator = iterator.next()) { + if (iterator.isCarbon() || iterator.getStatus() == STATUS_RECEIVED) { + continue; + } + return iterator.getEncryption(); } - return iterator.getEncryption(); + return conversation.getNextEncryption(); + } else { + throw new AssertionError("This should never be called since isInValidSession should be disabled for stubs"); } - return conversation.getNextEncryption(); } public boolean isValidInSession() { diff --git a/src/main/java/de/pixart/messenger/entities/StubConversation.java b/src/main/java/de/pixart/messenger/entities/StubConversation.java new file mode 100644 index 000000000..75f17812e --- /dev/null +++ b/src/main/java/de/pixart/messenger/entities/StubConversation.java @@ -0,0 +1,73 @@ +/* + * 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.entities; + +import rocks.xmpp.addr.Jid; + + +public class StubConversation implements Conversational { + + private final Account account; + private final String uuid; + private final Jid jid; + private final int mode; + + public StubConversation(Account account, String uuid, Jid jid, int mode) { + this.account = account; + this.uuid = uuid; + this.jid = jid; + this.mode = mode; + } + + @Override + public Account getAccount() { + return account; + } + + @Override + public Contact getContact() { + return account.getRoster().getContact(jid); + } + + @Override + public Jid getJid() { + return jid; + } + + @Override + public int getMode() { + return mode; + } + + @Override + public String getUuid() { + return uuid; + } +}
\ No newline at end of file diff --git a/src/main/java/de/pixart/messenger/generator/MessageGenerator.java b/src/main/java/de/pixart/messenger/generator/MessageGenerator.java index ca028d9b7..f8f419c1f 100644 --- a/src/main/java/de/pixart/messenger/generator/MessageGenerator.java +++ b/src/main/java/de/pixart/messenger/generator/MessageGenerator.java @@ -33,7 +33,7 @@ public class MessageGenerator extends AbstractGenerator { } private MessagePacket preparePacket(Message message) { - Conversation conversation = message.getConversation(); + Conversation conversation = (Conversation) message.getConversation(); Account account = conversation.getAccount(); MessagePacket packet = new MessagePacket(); final boolean isWithSelf = conversation.getContact().isSelf(); @@ -109,7 +109,8 @@ public class MessageGenerator extends AbstractGenerator { } public MessagePacket generateOtrChat(Message message) { - Session otrSession = message.getConversation().getOtrSession(); + Conversation conversation = (Conversation) message.getConversation(); + Session otrSession = conversation.getOtrSession(); if (otrSession == null) { return null; } diff --git a/src/main/java/de/pixart/messenger/parser/MessageParser.java b/src/main/java/de/pixart/messenger/parser/MessageParser.java index cacf51c0f..1f3166015 100644 --- a/src/main/java/de/pixart/messenger/parser/MessageParser.java +++ b/src/main/java/de/pixart/messenger/parser/MessageParser.java @@ -306,7 +306,8 @@ public class MessageParser extends AbstractParser implements OnMessagePacketRece extractErrorMessage(packet)); if (message != null) { if (message.getEncryption() == Message.ENCRYPTION_OTR) { - message.getConversation().endOtrIfNeeded(); + Conversation conversation = (Conversation) message.getConversation(); + conversation.endOtrIfNeeded(); } } } diff --git a/src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java b/src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java index 480f2d941..d55f36bdf 100644 --- a/src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java +++ b/src/main/java/de/pixart/messenger/persistance/DatabaseBackend.java @@ -717,40 +717,43 @@ public class DatabaseBackend extends SQLiteOpenHelper { return list; } + public Cursor getMessageSearchCursor(String term) { + SQLiteDatabase db = this.getReadableDatabase(); + String SQL = "SELECT " + Message.TABLENAME + ".*," + Conversation.TABLENAME + '.' + Conversation.CONTACTJID + ',' + Conversation.TABLENAME + '.' + Conversation.ACCOUNT + ',' + Conversation.TABLENAME + '.' + Conversation.MODE + " FROM " + Message.TABLENAME + " join " + Conversation.TABLENAME + " on " + Message.TABLENAME + '.' + Message.CONVERSATION + '=' + Conversation.TABLENAME + '.' + Conversation.UUID + " where " + Message.BODY + " LIKE ? limit 200"; + return db.rawQuery(SQL, new String[]{'%' + term + '%'}); + } + public Iterable<Message> getMessagesIterable(final Conversation conversation) { - return new Iterable<Message>() { - @Override - public Iterator<Message> iterator() { - class MessageIterator implements Iterator<Message> { - SQLiteDatabase db = getReadableDatabase(); - String[] selectionArgs = {conversation.getUuid()}; - Cursor cursor = db.query(Message.TABLENAME, null, Message.CONVERSATION - + "=?", selectionArgs, null, null, Message.TIME_SENT - + " ASC", null); - - public MessageIterator() { - cursor.moveToFirst(); - } - - @Override - public boolean hasNext() { - return !cursor.isAfterLast(); - } - - @Override - public Message next() { - Message message = Message.fromCursor(cursor, conversation); - cursor.moveToNext(); - return message; - } - - @Override - public void remove() { - throw new UnsupportedOperationException(); - } + return () -> { + class MessageIterator implements Iterator<Message> { + SQLiteDatabase db = getReadableDatabase(); + String[] selectionArgs = {conversation.getUuid()}; + Cursor cursor = db.query(Message.TABLENAME, null, Message.CONVERSATION + + "=?", selectionArgs, null, null, Message.TIME_SENT + + " ASC", null); + + public MessageIterator() { + cursor.moveToFirst(); + } + + @Override + public boolean hasNext() { + return !cursor.isAfterLast(); + } + + @Override + public Message next() { + Message message = Message.fromCursor(cursor, conversation); + cursor.moveToNext(); + return message; + } + + @Override + public void remove() { + throw new UnsupportedOperationException(); } - return new MessageIterator(); } + return new MessageIterator(); }; } diff --git a/src/main/java/de/pixart/messenger/services/AvatarService.java b/src/main/java/de/pixart/messenger/services/AvatarService.java index 81afa9342..d734d6f39 100644 --- a/src/main/java/de/pixart/messenger/services/AvatarService.java +++ b/src/main/java/de/pixart/messenger/services/AvatarService.java @@ -28,6 +28,7 @@ import de.pixart.messenger.entities.Account; import de.pixart.messenger.entities.Bookmark; import de.pixart.messenger.entities.Contact; import de.pixart.messenger.entities.Conversation; +import de.pixart.messenger.entities.Conversational; import de.pixart.messenger.entities.ListItem; import de.pixart.messenger.entities.Message; import de.pixart.messenger.entities.MucOptions; @@ -400,20 +401,21 @@ public class AvatarService implements OnAdvancedStreamFeaturesLoaded { } public Bitmap get(Message message, int size, boolean cachedOnly) { - final Conversation conversation = message.getConversation(); + final Conversational conversation = message.getConversation(); if (message.getType() == Message.TYPE_STATUS && message.getCounterparts() != null && message.getCounterparts().size() > 1) { return get(message.getCounterparts(), size, cachedOnly); } else if (message.getStatus() == Message.STATUS_RECEIVED) { Contact c = message.getContact(); if (c != null && (c.getProfilePhoto() != null || c.getAvatar() != null)) { return get(c, size, cachedOnly); - } else if (message.getConversation().getMode() == Conversation.MODE_MULTI) { + } else if (conversation instanceof Conversation && message.getConversation().getMode() == Conversation.MODE_MULTI) { final Jid trueCounterpart = message.getTrueCounterpart(); + final MucOptions mucOptions = ((Conversation) conversation).getMucOptions(); MucOptions.User user; if (trueCounterpart != null) { - user = conversation.getMucOptions().findUserByRealJid(trueCounterpart); + user = mucOptions.findUserByRealJid(trueCounterpart); } else { - user = conversation.getMucOptions().findUserByFullJid(message.getCounterpart()); + user = mucOptions.findUserByFullJid(message.getCounterpart()); } if (user != null) { return getImpl(user, size, cachedOnly); diff --git a/src/main/java/de/pixart/messenger/services/MessageSearchTask.java b/src/main/java/de/pixart/messenger/services/MessageSearchTask.java new file mode 100644 index 000000000..4911eff75 --- /dev/null +++ b/src/main/java/de/pixart/messenger/services/MessageSearchTask.java @@ -0,0 +1,128 @@ +/* + * 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.services; + +import android.database.Cursor; +import android.os.SystemClock; +import android.util.Log; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import de.pixart.messenger.Config; +import de.pixart.messenger.entities.Account; +import de.pixart.messenger.entities.Conversation; +import de.pixart.messenger.entities.Conversational; +import de.pixart.messenger.entities.IndividualMessage; +import de.pixart.messenger.entities.Message; +import de.pixart.messenger.entities.StubConversation; +import de.pixart.messenger.ui.interfaces.OnSearchResultsAvailable; +import de.pixart.messenger.utils.Cancellable; +import de.pixart.messenger.utils.ReplacingSerialSingleThreadExecutor; +import rocks.xmpp.addr.Jid; + +public class MessageSearchTask implements Runnable, Cancellable { + + private static final ReplacingSerialSingleThreadExecutor EXECUTOR = new ReplacingSerialSingleThreadExecutor(MessageSearchTask.class.getName()); + + private final XmppConnectionService xmppConnectionService; + private final String term; + private final OnSearchResultsAvailable onSearchResultsAvailable; + + private boolean isCancelled = false; + + private MessageSearchTask(XmppConnectionService xmppConnectionService, String term, OnSearchResultsAvailable onSearchResultsAvailable) { + this.xmppConnectionService = xmppConnectionService; + this.term = term; + this.onSearchResultsAvailable = onSearchResultsAvailable; + } + + public static void search(XmppConnectionService xmppConnectionService, String term, OnSearchResultsAvailable onSearchResultsAvailable) { + new MessageSearchTask(xmppConnectionService, term, onSearchResultsAvailable).executeInBackground(); + } + + @Override + public void cancel() { + this.isCancelled = true; + } + + @Override + public void run() { + long startTimestamp = SystemClock.elapsedRealtime(); + Cursor cursor = null; + try { + final HashMap<String, Conversational> conversationCache = new HashMap<>(); + final List<Message> result = new ArrayList<>(); + cursor = xmppConnectionService.databaseBackend.getMessageSearchCursor(term); + while (cursor.moveToNext()) { + final String conversationUuid = cursor.getString(cursor.getColumnIndex(Message.CONVERSATION)); + Conversational conversation; + if (conversationCache.containsKey(conversationUuid)) { + conversation = conversationCache.get(conversationUuid); + } else { + String accountUuid = cursor.getString(cursor.getColumnIndex(Conversation.ACCOUNT)); + String contactJid = cursor.getString(cursor.getColumnIndex(Conversation.CONTACTJID)); + int mode = cursor.getInt(cursor.getColumnIndex(Conversation.MODE)); + conversation = findOrGenerateStub(conversationUuid, accountUuid, contactJid, mode); + conversationCache.put(conversationUuid, conversation); + } + Message message = IndividualMessage.fromCursor(cursor, conversation); + result.add(message); + } + long stopTimestamp = SystemClock.elapsedRealtime(); + Log.d(Config.LOGTAG, "found " + result.size() + " messages in " + (stopTimestamp - startTimestamp) + "ms"); + onSearchResultsAvailable.onSearchResultsAvailable(term, result); + } catch (Exception e) { + Log.d(Config.LOGTAG, "exception while searching ", e); + } finally { + if (cursor != null) { + cursor.close(); + } + } + } + + private Conversational findOrGenerateStub(String conversationUuid, String accountUuid, String contactJid, int mode) throws Exception { + Conversation conversation = xmppConnectionService.findConversationByUuid(conversationUuid); + if (conversation != null) { + return conversation; + } + Account account = xmppConnectionService.findAccountByUuid(accountUuid); + Jid jid = Jid.of(contactJid); + if (account != null && jid != null) { + return new StubConversation(account, conversationUuid, jid.asBareJid(), mode); + } + throw new Exception("Unable to generate stub for " + contactJid); + } + + private void executeInBackground() { + EXECUTOR.execute(this); + } +}
\ No newline at end of file diff --git a/src/main/java/de/pixart/messenger/services/NotificationService.java b/src/main/java/de/pixart/messenger/services/NotificationService.java index a823c8307..fe167b904 100644 --- a/src/main/java/de/pixart/messenger/services/NotificationService.java +++ b/src/main/java/de/pixart/messenger/services/NotificationService.java @@ -42,6 +42,7 @@ import de.pixart.messenger.R; import de.pixart.messenger.entities.Account; 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.persistance.FileBackend; import de.pixart.messenger.ui.ConversationsActivity; @@ -78,11 +79,12 @@ public class NotificationService { } public boolean notify(final Message message) { + final Conversation conversation = (Conversation) message.getConversation(); return message.getStatus() == Message.STATUS_RECEIVED && notificationsEnabled() - && !message.getConversation().isMuted() - && (message.getConversation().alwaysNotify() || wasHighlightedOrPrivate(message)) - && (!message.getConversation().isWithStranger() || notificationsFromStrangers() + && !conversation.isMuted() + && (conversation.alwaysNotify() || wasHighlightedOrPrivate(message)) + && (!conversation.isWithStranger() || notificationsFromStrangers() ); } @@ -113,7 +115,7 @@ public class NotificationService { public void pushFromBacklog(final Message message) { if (notify(message)) { synchronized (notifications) { - getBacklogMessageCounter(message.getConversation()).incrementAndGet(); + getBacklogMessageCounter((Conversation) message.getConversation()).incrementAndGet(); pushToStack(message); } } @@ -244,7 +246,7 @@ public class NotificationService { if (messages != null && messages.size() > 0) { Message last = messages.get(messages.size() - 1); if (last.getStatus() != Message.STATUS_RECEIVED) { - if (mXmppConnectionService.markRead(last.getConversation(), false)) { + if (mXmppConnectionService.markRead((Conversation) last.getConversation(), false)) { mXmppConnectionService.updateConversationUi(); } } @@ -341,7 +343,7 @@ public class NotificationService { Conversation conversation = null; for (final ArrayList<Message> messages : notifications.values()) { if (messages.size() > 0) { - conversation = messages.get(0).getConversation(); + conversation = (Conversation) messages.get(0).getConversation(); final String name = conversation.getName().toString(); SpannableString styledString; if (Config.HIDE_MESSAGE_TEXT_IN_NOTIFICATION) { @@ -380,7 +382,7 @@ public class NotificationService { private Builder buildSingleConversations(final ArrayList<Message> messages) { final Builder mBuilder = new NotificationCompat.Builder(mXmppConnectionService); if (messages.size() >= 1) { - final Conversation conversation = messages.get(0).getConversation(); + final Conversation conversation = (Conversation) messages.get(0).getConversation(); final UnreadConversation.Builder mUnreadBuilder = new UnreadConversation.Builder(conversation.getName().toString()); mBuilder.setLargeIcon(mXmppConnectionService.getAvatarService() .get(conversation, getPixel(64))); @@ -514,7 +516,7 @@ public class NotificationService { private void modifyForTextOnly(final Builder builder, final UnreadConversation.Builder uBuilder, final ArrayList<Message> messages) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { NotificationCompat.MessagingStyle messagingStyle = new NotificationCompat.MessagingStyle(mXmppConnectionService.getString(R.string.me)); - Conversation conversation = messages.get(0).getConversation(); + final Conversation conversation = (Conversation) messages.get(0).getConversation(); if (conversation.getMode() == Conversation.MODE_MULTI) { messagingStyle.setConversationTitle(conversation.getName()); } @@ -636,7 +638,7 @@ public class NotificationService { return (actionId * NOTIFICATION_ID_MULTIPLIER) + (uuid.hashCode() % NOTIFICATION_ID_MULTIPLIER); } - private int generateRequestCode(Conversation conversation, int actionId) { + private int generateRequestCode(Conversational conversation, int actionId) { return generateRequestCode(conversation.getUuid(), actionId); } @@ -644,7 +646,7 @@ public class NotificationService { return createContentIntent(message.getConversationUuid(), message.getUuid()); } - private PendingIntent createContentIntent(final Conversation conversation) { + private PendingIntent createContentIntent(final Conversational conversation) { return createContentIntent(conversation.getUuid(), null); } @@ -696,13 +698,18 @@ public class NotificationService { } private boolean wasHighlightedOrPrivate(final Message message) { - final String nick = message.getConversation().getMucOptions().getActualNick(); - final Pattern highlight = generateNickHighlightPattern(nick); - if (message.getBody() == null || nick == null) { + if (message.getConversation() instanceof Conversation) { + Conversation conversation = (Conversation) message.getConversation(); + final String nick = conversation.getMucOptions().getActualNick(); + final Pattern highlight = generateNickHighlightPattern(nick); + if (message.getBody() == null || nick == null) { + return false; + } + final Matcher m = highlight.matcher(message.getBody()); + return (m.find() || message.getType() == Message.TYPE_PRIVATE); + } else { return false; } - final Matcher m = highlight.matcher(message.getBody()); - return (m.find() || message.getType() == Message.TYPE_PRIVATE); } public void setOpenConversation(final Conversation conversation) { diff --git a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java index 00baa733a..c2a0378f7 100644 --- a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java +++ b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java @@ -109,6 +109,7 @@ import de.pixart.messenger.persistance.DatabaseBackend; import de.pixart.messenger.persistance.FileBackend; import de.pixart.messenger.ui.SettingsActivity; import de.pixart.messenger.ui.UiCallback; +import de.pixart.messenger.ui.interfaces.OnSearchResultsAvailable; import de.pixart.messenger.utils.ConversationsFileObserver; import de.pixart.messenger.utils.CryptoHelper; import de.pixart.messenger.utils.ExceptionHelper; @@ -559,6 +560,10 @@ public class XmppConnectionService extends Service { return find(getConversations(), account, jid); } + public void search(String term, OnSearchResultsAvailable onSearchResultsAvailable) { + MessageSearchTask.search(this, term, onSearchResultsAvailable); + } + @Override public int onStartCommand(Intent intent, int flags, int startId) { final String action = intent == null ? null : intent.getAction(); @@ -813,7 +818,7 @@ public class XmppConnectionService extends Service { message.setEncryption(Message.ENCRYPTION_DECRYPTED); sendMessage(message); if (dismissAfterReply) { - markRead(message.getConversation(), true); + markRead((Conversation) message.getConversation(), true); } else { mNotificationService.pushFromDirectReply(message); } @@ -1279,7 +1284,7 @@ public class XmppConnectionService extends Service { databaseBackend.updateAccount(account); mNotificationService.updateErrorNotification(); } - final Conversation conversation = message.getConversation(); + final Conversation conversation = (Conversation) message.getConversation(); account.deactivateGracePeriod(); MessagePacket packet = null; final boolean addToConversation = (conversation.getMode() != Conversation.MODE_MULTI @@ -1289,8 +1294,8 @@ public class XmppConnectionService extends Service { message.setStatus(Message.STATUS_WAITING); if (!resend && message.getEncryption() != Message.ENCRYPTION_OTR) { - message.getConversation().endOtrIfNeeded(); - message.getConversation().findUnsentMessagesWithEncryption(Message.ENCRYPTION_OTR, + conversation.endOtrIfNeeded(); + conversation.findUnsentMessagesWithEncryption(Message.ENCRYPTION_OTR, new Conversation.OnMessageFound() { @Override public void onMessageFound(Message message) { @@ -3592,6 +3597,15 @@ public class XmppConnectionService extends Service { return null; } + public Account findAccountByUuid(final String uuid) { + for (Account account : this.accounts) { + if (account.getUuid().equals(uuid)) { + return account; + } + } + return null; + } + public Conversation findConversationByUuid(String uuid) { for (Conversation conversation : getConversations()) { if (conversation.getUuid().equals(uuid)) { @@ -3885,7 +3899,9 @@ public class XmppConnectionService extends Service { markMessage(msg, Message.STATUS_WAITING); this.resendMessage(msg, false); } - message.getConversation().sort(); + if (message.getConversation() instanceof Conversation) { + ((Conversation) message.getConversation()).sort(); + } updateConversationUi(); } diff --git a/src/main/java/de/pixart/messenger/ui/ConversationFragment.java b/src/main/java/de/pixart/messenger/ui/ConversationFragment.java index 371bdb5e5..4207a1b5e 100644 --- a/src/main/java/de/pixart/messenger/ui/ConversationFragment.java +++ b/src/main/java/de/pixart/messenger/ui/ConversationFragment.java @@ -960,7 +960,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke message.setEdited(message.getUuid()); message.setUuid(UUID.randomUUID().toString()); } - switch (message.getConversation().getNextEncryption()) { + switch (conversation.getNextEncryption()) { case Message.ENCRYPTION_OTR: sendOtrMessage(message); break; @@ -1204,11 +1204,11 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke messageListAdapter.setOnContactPictureClicked(message -> { final boolean received = message.getStatus() <= Message.STATUS_RECEIVED; if (received) { - if (message.getConversation().getMode() == Conversation.MODE_MULTI) { + if (message.getConversation() instanceof Conversation && message.getConversation().getMode() == Conversation.MODE_MULTI) { Jid user = message.getCounterpart(); if (user != null && !user.isBareJid()) { - final MucOptions mucOptions = message.getConversation().getMucOptions(); - if (mucOptions.participating() || message.getConversation().getNextCounterpart() != null) { + final MucOptions mucOptions = ((Conversation) message.getConversation()).getMucOptions(); + if (mucOptions.participating() || ((Conversation) message.getConversation()).getNextCounterpart() != null) { if (!mucOptions.isUserInRoom(user)) { Toast.makeText(getActivity(), activity.getString(R.string.user_has_left_conference, user.getResource()), Toast.LENGTH_SHORT).show(); } @@ -1350,7 +1350,8 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke } if (relevantForCorrection.getType() == Message.TYPE_TEXT && relevantForCorrection.isLastCorrectableMessage() - && (m.getConversation().getMucOptions().nonanonymous() || m.getConversation().getMode() == Conversation.MODE_SINGLE)) { + && m.getConversation() instanceof Conversation + && (((Conversation) m.getConversation()).getMucOptions().nonanonymous() || m.getConversation().getMode() == Conversation.MODE_SINGLE)) { correctMessage.setVisible(true); } if ((m.isFileOrImage() && !deleted && !receiving) || (m.getType() == Message.TYPE_TEXT && !m.treatAsDownloadable())) { @@ -1898,9 +1899,12 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke public void resendMessage(final Message message) { if (message != null && message.isFileOrImage()) { + if (!(message.getConversation() instanceof Conversation)) { + return; + } + final Conversation conversation = (Conversation) message.getConversation(); DownloadableFile file = activity.xmppConnectionService.getFileBackend().getFile(message); if (file.exists()) { - final Conversation conversation = message.getConversation(); final XmppConnection xmppConnection = conversation.getAccount().getXmppConnection(); if (!message.hasFileOnRemoteHost() && xmppConnection != null @@ -2797,7 +2801,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke protected void sendOtrMessage(final Message message) { final ConversationsActivity activity = (ConversationsActivity) getActivity(); final XmppConnectionService xmppService = activity.xmppConnectionService; - activity.selectPresence(message.getConversation(), + activity.selectPresence(conversation, () -> { message.setCounterpart(conversation.getNextCounterpart()); xmppService.sendMessage(message); diff --git a/src/main/java/de/pixart/messenger/ui/SearchActivity.java b/src/main/java/de/pixart/messenger/ui/SearchActivity.java index e65b2dd6c..2120cac3e 100644 --- a/src/main/java/de/pixart/messenger/ui/SearchActivity.java +++ b/src/main/java/de/pixart/messenger/ui/SearchActivity.java @@ -34,7 +34,6 @@ import android.os.Bundle; import android.support.v7.widget.Toolbar; import android.text.Editable; import android.text.TextWatcher; -import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.widget.EditText; @@ -42,18 +41,18 @@ import android.widget.EditText; import java.util.ArrayList; import java.util.List; -import de.pixart.messenger.Config; import de.pixart.messenger.R; import de.pixart.messenger.databinding.ActivitySearchBinding; import de.pixart.messenger.entities.Message; import de.pixart.messenger.ui.adapter.MessageAdapter; +import de.pixart.messenger.ui.interfaces.OnSearchResultsAvailable; import de.pixart.messenger.ui.util.Color; import de.pixart.messenger.ui.util.Drawable; 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 { +public class SearchActivity extends XmppActivity implements TextWatcher, OnSearchResultsAvailable { private final List<Message> messages = new ArrayList<>(); private ActivitySearchBinding binding; @@ -122,7 +121,24 @@ public class SearchActivity extends XmppActivity implements TextWatcher { @Override public void afterTextChanged(Editable s) { - Log.d(Config.LOGTAG, "searching for " + s); + String term = s.toString().trim(); + if (term.length() > 0) { + xmppConnectionService.search(s.toString().trim(), this); + } else { + this.messages.clear(); + messageListAdapter.notifyDataSetChanged(); + changeBackground(false, false); + } + } + + @Override + public void onSearchResultsAvailable(String term, List<Message> messages) { + this.messages.clear(); + this.messages.addAll(messages); + runOnUiThread(() -> { + messageListAdapter.notifyDataSetChanged(); + changeBackground(true, messages.size() > 0); + }); } }
\ No newline at end of file diff --git a/src/main/java/de/pixart/messenger/ui/ShareWithActivity.java b/src/main/java/de/pixart/messenger/ui/ShareWithActivity.java index f27b8b7aa..22cd2a677 100644 --- a/src/main/java/de/pixart/messenger/ui/ShareWithActivity.java +++ b/src/main/java/de/pixart/messenger/ui/ShareWithActivity.java @@ -90,11 +90,12 @@ public class ShareWithActivity extends XmppActivity implements XmppConnectionSer } else { resId = R.string.shared_file_with_x; } - replaceToast(getString(resId, message.getConversation().getName())); + Conversation conversation = (Conversation) message.getConversation(); + replaceToast(getString(resId, conversation.getName())); if (mReturnToPrevious) { finish(); } else { - switchToConversation(message.getConversation()); + switchToConversation(conversation); } } }); diff --git a/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java b/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java index 7a1809a98..1b0553bed 100644 --- a/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java +++ b/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java @@ -63,6 +63,7 @@ import de.pixart.messenger.R; import de.pixart.messenger.crypto.axolotl.FingerprintStatus; import de.pixart.messenger.entities.Account; import de.pixart.messenger.entities.Conversation; +import de.pixart.messenger.entities.Conversational; import de.pixart.messenger.entities.DownloadableFile; import de.pixart.messenger.entities.Message; import de.pixart.messenger.entities.Message.FileParams; @@ -594,10 +595,13 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie } } if (message.getConversation().getMode() == Conversation.MODE_MULTI && message.getStatus() == Message.STATUS_RECEIVED) { - Pattern pattern = NotificationService.generateNickHighlightPattern(message.getConversation().getMucOptions().getActualNick()); - Matcher matcher = pattern.matcher(body); - while (matcher.find()) { - body.setSpan(new StyleSpan(Typeface.BOLD), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + if (message.getConversation() instanceof Conversation) { + final Conversation conversation = (Conversation) message.getConversation(); + Pattern pattern = NotificationService.generateNickHighlightPattern(conversation.getMucOptions().getActualNick()); + Matcher matcher = pattern.matcher(body); + while (matcher.find()) { + body.setSpan(new StyleSpan(Typeface.BOLD), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + } } } Matcher matcher = Emoticons.generatePattern(body).matcher(body); @@ -798,7 +802,7 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie final Message message = getItem(position); final boolean omemoEncryption = message.getEncryption() == Message.ENCRYPTION_AXOLOTL; final boolean isInValidSession = message.isValidInSession() && (!omemoEncryption || message.isTrusted()); - final Conversation conversation = message.getConversation(); + final Conversational conversation = message.getConversation(); final Account account = conversation.getAccount(); final int type = getItemViewType(position); ViewHolder viewHolder; @@ -875,7 +879,7 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie viewHolder.status_message.setVisibility(View.GONE); viewHolder.contact_picture.setVisibility(View.GONE); viewHolder.load_more_messages.setVisibility(View.VISIBLE); - viewHolder.load_more_messages.setOnClickListener(v -> loadMoreMessages(message.getConversation())); + viewHolder.load_more_messages.setOnClickListener(v -> loadMoreMessages((Conversation) message.getConversation())); } else { viewHolder.status_message.setVisibility(View.VISIBLE); viewHolder.load_more_messages.setVisibility(View.GONE); @@ -943,7 +947,7 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie } } else if (message.getEncryption() == Message.ENCRYPTION_PGP) { if (account.isPgpDecryptionServiceConnected()) { - if (!account.hasPendingPgpIntent(conversation)) { + if (conversation instanceof Conversation && !account.hasPendingPgpIntent((Conversation) conversation)) { displayInfoMessage(viewHolder, activity.getString(R.string.message_decrypting), darkBackground); } else { displayInfoMessage(viewHolder, activity.getString(R.string.pgp_message), darkBackground); diff --git a/src/main/java/de/pixart/messenger/ui/interfaces/OnSearchResultsAvailable.java b/src/main/java/de/pixart/messenger/ui/interfaces/OnSearchResultsAvailable.java new file mode 100644 index 000000000..57f2a40cc --- /dev/null +++ b/src/main/java/de/pixart/messenger/ui/interfaces/OnSearchResultsAvailable.java @@ -0,0 +1,39 @@ +/* + * 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.interfaces; + +import java.util.List; + +import de.pixart.messenger.entities.Message; + +public interface OnSearchResultsAvailable { + + void onSearchResultsAvailable(String term, List<Message> messages); + +}
\ No newline at end of file diff --git a/src/main/java/de/pixart/messenger/utils/Cancellable.java b/src/main/java/de/pixart/messenger/utils/Cancellable.java new file mode 100644 index 000000000..73da4422d --- /dev/null +++ b/src/main/java/de/pixart/messenger/utils/Cancellable.java @@ -0,0 +1,34 @@ +/* +* 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.utils; + + +public interface Cancellable { + void cancel(); +}
\ No newline at end of file diff --git a/src/main/java/de/pixart/messenger/utils/GeoHelper.java b/src/main/java/de/pixart/messenger/utils/GeoHelper.java index e8ca33fb3..d2963f206 100644 --- a/src/main/java/de/pixart/messenger/utils/GeoHelper.java +++ b/src/main/java/de/pixart/messenger/utils/GeoHelper.java @@ -13,6 +13,7 @@ import java.util.regex.Pattern; import de.pixart.messenger.Config; 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.ui.ShowLocationActivity; @@ -62,11 +63,11 @@ public class GeoHelper { } catch (NumberFormatException nfe) { return intents; } - final Conversation conversation = message.getConversation(); + final Conversational conversation = message.getConversation(); String label; - if (conversation.getMode() == Conversation.MODE_SINGLE && message.getStatus() == Message.STATUS_RECEIVED) { + if (conversation instanceof Conversation && conversation.getMode() == Conversation.MODE_SINGLE && message.getStatus() == Message.STATUS_RECEIVED) { try { - label = "(" + URLEncoder.encode(message.getConversation().getName().toString(), "UTF-8") + ")"; + label = "(" + URLEncoder.encode(((Conversation) conversation).getName().toString(), "UTF-8") + ")"; } catch (UnsupportedEncodingException e) { label = ""; } diff --git a/src/main/java/de/pixart/messenger/utils/ReplacingSerialSingleThreadExecutor.java b/src/main/java/de/pixart/messenger/utils/ReplacingSerialSingleThreadExecutor.java index 22ad7f7a4..7bd33eff4 100644 --- a/src/main/java/de/pixart/messenger/utils/ReplacingSerialSingleThreadExecutor.java +++ b/src/main/java/de/pixart/messenger/utils/ReplacingSerialSingleThreadExecutor.java @@ -2,6 +2,10 @@ package de.pixart.messenger.utils; public class ReplacingSerialSingleThreadExecutor extends SerialSingleThreadExecutor { + public ReplacingSerialSingleThreadExecutor(String name) { + super(name, false); + } + public ReplacingSerialSingleThreadExecutor(boolean prepareLooper) { super(ReplacingSerialSingleThreadExecutor.class.getName(), prepareLooper); } @@ -9,6 +13,9 @@ public class ReplacingSerialSingleThreadExecutor extends SerialSingleThreadExecu @Override public synchronized void execute(final Runnable r) { tasks.clear(); + if (active != null && active instanceof Cancellable) { + ((Cancellable) active).cancel(); + } super.execute(r); } } diff --git a/src/main/java/de/pixart/messenger/utils/SerialSingleThreadExecutor.java b/src/main/java/de/pixart/messenger/utils/SerialSingleThreadExecutor.java index 1562160fd..1a1280d5a 100644 --- a/src/main/java/de/pixart/messenger/utils/SerialSingleThreadExecutor.java +++ b/src/main/java/de/pixart/messenger/utils/SerialSingleThreadExecutor.java @@ -13,7 +13,7 @@ public class SerialSingleThreadExecutor implements Executor { private final Executor executor = Executors.newSingleThreadExecutor(); final ArrayDeque<Runnable> tasks = new ArrayDeque<>(); - private Runnable active; + protected Runnable active; private final String name; public SerialSingleThreadExecutor(String name) { diff --git a/src/main/java/de/pixart/messenger/utils/UIHelper.java b/src/main/java/de/pixart/messenger/utils/UIHelper.java index ea9f97f98..290a5829d 100644 --- a/src/main/java/de/pixart/messenger/utils/UIHelper.java +++ b/src/main/java/de/pixart/messenger/utils/UIHelper.java @@ -18,6 +18,7 @@ import de.pixart.messenger.R; import de.pixart.messenger.crypto.axolotl.AxolotlService; import de.pixart.messenger.entities.Contact; import de.pixart.messenger.entities.Conversation; +import de.pixart.messenger.entities.Conversational; import de.pixart.messenger.entities.ListItem; import de.pixart.messenger.entities.Message; import de.pixart.messenger.entities.MucOptions; @@ -444,7 +445,7 @@ public class UIHelper { } public static String getMessageDisplayName(final Message message) { - final Conversation conversation = message.getConversation(); + final Conversational conversation = message.getConversation(); if (message.getStatus() == Message.STATUS_RECEIVED) { final Contact contact = message.getContact(); if (conversation.getMode() == Conversation.MODE_MULTI) { @@ -457,8 +458,8 @@ public class UIHelper { return contact != null ? contact.getDisplayName() : ""; } } else { - if (conversation.getMode() == Conversation.MODE_MULTI) { - return conversation.getMucOptions().getSelf().getName(); + if (conversation instanceof Conversation && conversation.getMode() == Conversation.MODE_MULTI) { + return ((Conversation) conversation).getMucOptions().getSelf().getName(); } else { final Jid jid = conversation.getAccount().getJid(); return jid.getLocal() != null ? jid.getLocal() : Jid.ofDomain(jid.getDomain()).toString(); diff --git a/src/main/java/de/pixart/messenger/xmpp/jingle/JingleConnection.java b/src/main/java/de/pixart/messenger/xmpp/jingle/JingleConnection.java index 3b83857e7..b2eb6a766 100644 --- a/src/main/java/de/pixart/messenger/xmpp/jingle/JingleConnection.java +++ b/src/main/java/de/pixart/messenger/xmpp/jingle/JingleConnection.java @@ -24,6 +24,7 @@ import de.pixart.messenger.crypto.axolotl.OnMessageCreatedCallback; import de.pixart.messenger.crypto.axolotl.XmppAxolotlMessage; import de.pixart.messenger.entities.Account; import de.pixart.messenger.entities.Conversation; +import de.pixart.messenger.entities.Conversational; import de.pixart.messenger.entities.DownloadableFile; import de.pixart.messenger.entities.Message; import de.pixart.messenger.entities.Presence; @@ -264,7 +265,7 @@ public class JingleConnection implements Transferable { public void init(final Message message) { if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL) { - Conversation conversation = message.getConversation(); + Conversation conversation = (Conversation) message.getConversation(); conversation.getAccount().getAxolotlService().prepareKeyTransportMessage(conversation, new OnMessageCreatedCallback() { @Override public void run(XmppAxolotlMessage xmppAxolotlMessage) { @@ -355,9 +356,7 @@ public class JingleConnection implements Transferable { public void init(Account account, JinglePacket packet) { this.mJingleStatus = JINGLE_STATUS_INITIATED; - Conversation conversation = this.mXmppConnectionService - .findOrCreateConversation(account, - packet.getFrom().asBareJid(), false, true); + Conversation conversation = this.mXmppConnectionService.findOrCreateConversation(account, packet.getFrom().asBareJid(), false, false); this.message = new Message(conversation, "", Message.ENCRYPTION_NONE); this.message.setStatus(Message.STATUS_RECEIVED); this.mStatus = Transferable.STATUS_OFFER; @@ -493,12 +492,13 @@ public class JingleConnection implements Transferable { Pair<InputStream, Integer> pair; try { if (message.getEncryption() == Message.ENCRYPTION_OTR) { - Conversation conversation = this.message.getConversation(); - if (!this.mXmppConnectionService.renewSymmetricKey(conversation)) { + Conversational conversation = this.message.getConversation(); + if (!this.mXmppConnectionService.renewSymmetricKey((Conversation) conversation)) { Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": could not set symmetric key"); cancel(); } - this.file.setKeyAndIv(conversation.getSymmetricKey()); + Conversation c = (Conversation) this.message.getConversation(); + this.file.setKeyAndIv(c.getSymmetricKey()); pair = AbstractConnectionManager.createInputStream(this.file, false); this.file.setExpectedSize(pair.second); content.setFileOffer(this.file, true, this.ftVersion); @@ -845,6 +845,8 @@ public class JingleConnection implements Transferable { content.setTransportId(this.transportId); content.ibbTransport().setAttribute("block-size", this.ibbBlockSize); answer.setContent(content); + + if (initiating()) { this.sendJinglePacket(answer, new OnIqPacketReceived() { @Override @@ -873,6 +875,7 @@ public class JingleConnection implements Transferable { } } this.transport = new JingleInbandTransport(this, this.transportId, this.ibbBlockSize); + //might be receive instead if we are not initiating if (initiating()) { this.transport.connect(onIbbTransportConnected); @@ -1140,4 +1143,4 @@ public class JingleConnection implements Transferable { public AbstractConnectionManager getConnectionManager() { return this.mJingleConnectionManager; } -} +}
\ No newline at end of file diff --git a/src/main/res/layout/activity_search.xml b/src/main/res/layout/activity_search.xml index 9fc9d15d5..ba2963b81 100644 --- a/src/main/res/layout/activity_search.xml +++ b/src/main/res/layout/activity_search.xml @@ -45,6 +45,8 @@ android:layout_height="match_parent" android:background="?attr/activity_background_search" android:divider="@android:color/transparent" - android:dividerHeight="0dp" /> + android:dividerHeight="0dp" + android:listSelector="@android:color/transparent" + android:stackFromBottom="true" /> </LinearLayout> </layout>
\ No newline at end of file |