Merge branch 'dev' of github.com:loki-project/loki-messenger-android into custom-server

This commit is contained in:
Niels Andriesse 2019-10-15 13:42:44 +11:00
commit 08a1b0e947
12 changed files with 71 additions and 77 deletions

Binary file not shown.

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<org.thoughtcrime.securesms.loki.UserSelectionViewCell
<org.thoughtcrime.securesms.loki.MentionCandidateSelectionViewCell
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="52dp"
@ -48,4 +48,4 @@
style="@style/Signal.Text.Body"
android:ellipsize="end" />
</org.thoughtcrime.securesms.loki.UserSelectionViewCell>
</org.thoughtcrime.securesms.loki.MentionCandidateSelectionViewCell>

View File

@ -76,7 +76,7 @@
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="@layout/view_user_selection" />
<include layout="@layout/view_mention_candidate_selection" />
<FrameLayout
android:layout_width="match_parent"

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<org.thoughtcrime.securesms.loki.UserSelectionView
<org.thoughtcrime.securesms.loki.MentionCandidateSelectionView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/userSelectionView"
android:layout_width="match_parent"

View File

@ -272,7 +272,7 @@ public class ConversationListItem extends RelativeLayout
}
private @NonNull CharSequence getTrimmedSnippet(@NonNull CharSequence snippet) {
LokiAPIUtilities.INSTANCE.populateUserIDCacheIfNeeded(threadId, getContext()); // TODO: Terrible place to do this, but okay for now
LokiAPIUtilities.INSTANCE.populateUserHexEncodedPublicKeyCacheIfNeeded(threadId, getContext()); // TODO: Terrible place to do this, but okay for now
snippet = MentionUtilities.highlightMentions(snippet, this.recipient.isGroupRecipient(), getContext());
return snippet.length() <= MAX_SNIPPET_LENGTH ? snippet : snippet.subSequence(0, MAX_SNIPPET_LENGTH);
}

View File

@ -159,8 +159,7 @@ import org.thoughtcrime.securesms.loki.FriendRequestViewDelegate;
import org.thoughtcrime.securesms.loki.LokiAPIUtilities;
import org.thoughtcrime.securesms.loki.LokiThreadDatabaseDelegate;
import org.thoughtcrime.securesms.loki.LokiUserDatabase;
import org.thoughtcrime.securesms.loki.Mention;
import org.thoughtcrime.securesms.loki.UserSelectionView;
import org.thoughtcrime.securesms.loki.MentionCandidateSelectionView;
import org.thoughtcrime.securesms.mediasend.Media;
import org.thoughtcrime.securesms.mediasend.MediaSendActivity;
import org.thoughtcrime.securesms.mms.AttachmentManager;
@ -231,6 +230,7 @@ import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import org.whispersystems.signalservice.loki.api.LokiAPI;
import org.whispersystems.signalservice.loki.messaging.LokiMessageFriendRequestStatus;
import org.whispersystems.signalservice.loki.messaging.LokiThreadFriendRequestStatus;
import org.whispersystems.signalservice.loki.messaging.Mention;
import org.whispersystems.signalservice.loki.utilities.Analytics;
import java.io.IOException;
@ -247,7 +247,6 @@ import java.util.concurrent.atomic.AtomicInteger;
import kotlin.Unit;
import network.loki.messenger.R;
import nl.komponents.kovenant.combine.Tuple2;
import static org.thoughtcrime.securesms.TransportOption.Type;
import static org.thoughtcrime.securesms.database.GroupDatabase.GroupRecord;
@ -348,7 +347,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
private final DynamicLanguage dynamicLanguage = new DynamicLanguage();
// Mentions
private UserSelectionView userSelectionView;
private MentionCandidateSelectionView mentionCandidateSelectionView;
private int currentMentionStartIndex = -1;
private ArrayList<Mention> mentions = new ArrayList<>();
private String oldText = "";
@ -405,15 +404,15 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
}
composeText.setSelection(composeText.length(), composeText.length());
composeText.addTextChangedListener(mentionTextWatcher);
userSelectionView.setOnUserSelected(tuple -> {
Mention mention = new Mention(currentMentionStartIndex, tuple.getFirst(), tuple.getSecond());
mentions.add(mention);
mentionCandidateSelectionView.setOnMentionCandidateSelected( mentionCandidate -> {
mentions.add(mentionCandidate);
String oldText = composeText.getText().toString();
String newText = oldText.substring(0, currentMentionStartIndex) + "@" + tuple.getSecond();
String newText = oldText.substring(0, currentMentionStartIndex) + "@" + mentionCandidate.getDisplayName();
composeText.setText(newText);
composeText.setSelection(newText.length());
userSelectionView.hide();
currentMentionStartIndex = -1;
mentionCandidateSelectionView.hide();
ConversationActivity.this.oldText = newText;
return Unit.INSTANCE;
});
}
@ -421,7 +420,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
}
});
LokiAPIUtilities.INSTANCE.populateUserIDCacheIfNeeded(threadId, this);
LokiAPIUtilities.INSTANCE.populateUserHexEncodedPublicKeyCacheIfNeeded(threadId, this);
if (this.recipient.isGroupRecipient()) {
if (this.recipient.getName().equals("Loki Public Chat")) {
@ -1581,7 +1580,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
inlineAttachmentToggle = ViewUtil.findById(this, R.id.inline_attachment_container);
inputPanel = ViewUtil.findById(this, R.id.bottom_panel);
searchNav = ViewUtil.findById(this, R.id.conversation_search_nav);
userSelectionView = ViewUtil.findById(this, R.id.userSelectionView);
mentionCandidateSelectionView = ViewUtil.findById(this, R.id.userSelectionView);
ImageButton quickCameraToggle = ViewUtil.findById(this, R.id.quick_camera_toggle);
ImageButton inlineAttachmentButton = ViewUtil.findById(this, R.id.inline_attachment_button);
@ -2093,15 +2092,13 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
private String getMessage() throws InvalidMessageException {
String result = composeText.getTextTrimmed();
if (result.length() < 1 && !attachmentManager.isAttachmentPresent()) throw new InvalidMessageException();
int shift = 0;
for (Mention mention : mentions) {
try {
int startIndex = mention.getLocationInString() + shift;
int startIndex = result.indexOf("@" + mention.getDisplayName());
int endIndex = startIndex + mention.getDisplayName().length() + 1; // + 1 to include the @
shift = shift + mention.getHexEncodedPublicKey().length() - mention.getDisplayName().length();
result = result.substring(0, startIndex) + "@" + mention.getHexEncodedPublicKey() + result.substring(endIndex);
} catch (Exception exception) {
// Do nothing
Log.d("Loki", "Couldn't process mention due to error: " + exception.toString() + ".");
}
}
return result;
@ -2631,7 +2628,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
private void silentlySetComposeText(String text) {
typingTextWatcher.setEnabled(false);
composeText.setText(text);
if (text.isEmpty()) clearMentions();
if (text.isEmpty()) resetMentions();
typingTextWatcher.setEnabled(true);
}
@ -2767,43 +2764,43 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
boolean isBackspace = text.length() < oldText.length();
if (isBackspace) {
currentMentionStartIndex = -1;
for (Mention mention : mentions) {
boolean isValid;
if (mention.getLocationInString() > (text.length() - 1)) {
isValid = false;
} else {
isValid = text.substring(mention.getLocationInString()).startsWith("@" + mention.getDisplayName());
}
if (!isValid) {
mentions.remove(mention);
mentionCandidateSelectionView.hide();
try {
for (Mention mention : mentions) {
if (!text.contains(mention.getDisplayName())) {
mentions.remove(mention);
}
}
} catch (Exception exception) {
mentions.clear(); // TODO: Dirty workaround for ConcurrentModificationException
}
} else if (text.length() > 0) {
if (currentMentionStartIndex > text.length()) {
clearMentions(); // Should never occur
resetMentions(); // Should never occur
}
int currentEndIndex = text.length() - 1;
char lastCharacter = text.charAt(currentEndIndex);
int lastCharacterIndex = text.length() - 1;
char lastCharacter = text.charAt(lastCharacterIndex);
LokiUserDatabase userDatabase = DatabaseFactory.getLokiUserDatabase(ConversationActivity.this);
if (lastCharacter == '@') {
List<Tuple2<String, String>> users = LokiAPI.Companion.getUsers("", threadId, userDatabase);
currentMentionStartIndex = currentEndIndex;
userSelectionView.show(users, threadId);
List<Mention> mentionCandidates = LokiAPI.Companion.getMentionCandidates("", threadId, userDatabase);
currentMentionStartIndex = lastCharacterIndex;
mentionCandidateSelectionView.show(mentionCandidates, threadId);
} else if (Character.isWhitespace(lastCharacter)) {
currentMentionStartIndex = -1;
userSelectionView.hide();
mentionCandidateSelectionView.hide();
} else {
if (currentMentionStartIndex != -1) {
String query = text.substring(currentMentionStartIndex + 1); // + 1 to get rid of the @
List<Tuple2<String, String>> users = LokiAPI.Companion.getUsers(query, threadId, userDatabase);
userSelectionView.show(users, threadId);
List<Mention> mentionCandidates = LokiAPI.Companion.getMentionCandidates(query, threadId, userDatabase);
mentionCandidateSelectionView.show(mentionCandidates, threadId);
}
}
}
ConversationActivity.this.oldText = text;
}
}
private void clearMentions() {
private void resetMentions() {
oldText = "";
currentMentionStartIndex = -1;
mentions.clear();

View File

@ -1025,7 +1025,7 @@ public class PushDecryptJob extends BaseJob implements InjectableType {
// Loki - Cache the user hex encoded public key (for mentions)
if (threadId != null) {
LokiAPIUtilities.INSTANCE.populateUserIDCacheIfNeeded(threadId, context);
LokiAPIUtilities.INSTANCE.populateUserHexEncodedPublicKeyCacheIfNeeded(threadId, context);
LokiAPI.Companion.cache(textMessage.getSender().serialize(), threadId);
}

View File

@ -8,7 +8,7 @@ import org.whispersystems.signalservice.loki.api.LokiAPI
object LokiAPIUtilities {
fun populateUserIDCacheIfNeeded(threadID: Long, context: Context) {
fun populateUserHexEncodedPublicKeyCacheIfNeeded(threadID: Long, context: Context) {
if (LokiAPI.userHexEncodedPublicKeyCache[threadID] != null) { return }
val result = mutableSetOf<String>()
val messageDatabase = DatabaseFactory.getMmsSmsDatabase(context)

View File

@ -1,3 +0,0 @@
package org.thoughtcrime.securesms.loki
data class Mention(val locationInString: Int, val hexEncodedPublicKey: String, val displayName: String)

View File

@ -7,42 +7,42 @@ import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ListView
import nl.komponents.kovenant.combine.Tuple2
import org.thoughtcrime.securesms.database.DatabaseFactory
import org.whispersystems.signalservice.loki.messaging.Mention
class UserSelectionView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : ListView(context, attrs, defStyleAttr) {
private var users = listOf<Tuple2<String, String>>()
set(newValue) { field = newValue; userSelectionViewAdapter.users = newValue }
private var mentionCandidates = listOf<Mention>()
set(newValue) { field = newValue; mentionCandidateSelectionViewAdapter.mentionCandidates = newValue }
var publicChatServer: String? = null
set(newValue) { field = newValue; userSelectionViewAdapter.publicChatServer = publicChatServer }
set(newValue) { field = newValue; mentionCandidateSelectionViewAdapter.publicChatServer = publicChatServer }
var publicChatChannel: Long? = null
set(newValue) { field = newValue; userSelectionViewAdapter.publicChatChannel = publicChatChannel }
var onUserSelected: ((Tuple2<String, String>) -> Unit)? = null
set(newValue) { field = newValue; mentionCandidateSelectionViewAdapter.publicChatChannel = publicChatChannel }
var onMentionCandidateSelected: ((Mention) -> Unit)? = null
private val userSelectionViewAdapter by lazy { Adapter(context) }
private val mentionCandidateSelectionViewAdapter by lazy { Adapter(context) }
private class Adapter(private val context: Context) : BaseAdapter() {
var users = listOf<Tuple2<String, String>>()
var mentionCandidates = listOf<Mention>()
set(newValue) { field = newValue; notifyDataSetChanged() }
var publicChatServer: String? = null
var publicChatChannel: Long? = null
override fun getCount(): Int {
return users.count()
return mentionCandidates.count()
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getItem(position: Int): Tuple2<String, String> {
return users[position]
override fun getItem(position: Int): Mention {
return mentionCandidates[position]
}
override fun getView(position: Int, cellToBeReused: View?, parent: ViewGroup): View {
val cell = cellToBeReused as UserSelectionViewCell? ?: UserSelectionViewCell.inflate(LayoutInflater.from(context), parent)
val user = getItem(position)
cell.user = user
val cell = cellToBeReused as MentionCandidateSelectionViewCell? ?: MentionCandidateSelectionViewCell.inflate(LayoutInflater.from(context), parent)
val mentionCandidate = getItem(position)
cell.mentionCandidate = mentionCandidate
cell.publicChatServer = publicChatServer
cell.publicChatChannel = publicChatChannel
return cell
@ -53,22 +53,22 @@ class UserSelectionView(context: Context, attrs: AttributeSet?, defStyleAttr: In
constructor(context: Context) : this(context, null)
init {
adapter = userSelectionViewAdapter
userSelectionViewAdapter.users = users
adapter = mentionCandidateSelectionViewAdapter
mentionCandidateSelectionViewAdapter.mentionCandidates = mentionCandidates
setOnItemClickListener { _, _, position, _ ->
onUserSelected?.invoke(users[position])
onMentionCandidateSelected?.invoke(mentionCandidates[position])
}
}
fun show(users: List<Tuple2<String, String>>, threadID: Long) {
fun show(mentionCandidates: List<Mention>, threadID: Long) {
val publicChat = DatabaseFactory.getLokiThreadDatabase(context).getPublicChat(threadID)
if (publicChat != null) {
publicChatServer = publicChat.server
publicChatChannel = publicChat.channel
}
this.users = users
this.mentionCandidates = mentionCandidates
val layoutParams = this.layoutParams as ViewGroup.LayoutParams
layoutParams.height = toPx(6 + Math.min(users.count(), 4) * 52, resources)
layoutParams.height = toPx(6 + Math.min(mentionCandidates.count(), 4) * 52, resources)
this.layoutParams = layoutParams
}

View File

@ -8,13 +8,13 @@ import android.view.View
import android.view.ViewGroup
import android.view.ViewOutlineProvider
import android.widget.LinearLayout
import kotlinx.android.synthetic.main.cell_user_selection_view.view.*
import kotlinx.android.synthetic.main.cell_mention_candidate_selection_view.view.*
import network.loki.messenger.R
import nl.komponents.kovenant.combine.Tuple2
import org.whispersystems.signalservice.loki.api.LokiPublicChatAPI
import org.whispersystems.signalservice.loki.messaging.Mention
class UserSelectionViewCell(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : LinearLayout(context, attrs, defStyleAttr) {
var user = Tuple2("", "")
class MentionCandidateSelectionViewCell(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : LinearLayout(context, attrs, defStyleAttr) {
var mentionCandidate = Mention("", "")
set(newValue) { field = newValue; update() }
var publicChatServer: String? = null
var publicChatChannel: Long? = null
@ -24,8 +24,8 @@ class UserSelectionViewCell(context: Context, attrs: AttributeSet?, defStyleAttr
companion object {
fun inflate(layoutInflater: LayoutInflater, parent: ViewGroup): UserSelectionViewCell {
return layoutInflater.inflate(R.layout.cell_user_selection_view, parent, false) as UserSelectionViewCell
fun inflate(layoutInflater: LayoutInflater, parent: ViewGroup): MentionCandidateSelectionViewCell {
return layoutInflater.inflate(R.layout.cell_mention_candidate_selection_view, parent, false) as MentionCandidateSelectionViewCell
}
}
@ -41,8 +41,8 @@ class UserSelectionViewCell(context: Context, attrs: AttributeSet?, defStyleAttr
}
private fun update() {
displayNameTextView.text = user.second
profilePictureImageView.update(user.first)
displayNameTextView.text = mentionCandidate.displayName
profilePictureImageView.update(mentionCandidate.hexEncodedPublicKey)
if (publicChatServer != null && publicChatChannel != null) {
val isUserModerator = LokiPublicChatAPI.isUserModerator(user.first, publicChatChannel!!, publicChatServer!!)
moderatorIconImageView.visibility = if (isUserModerator) View.VISIBLE else View.GONE

View File

@ -27,13 +27,13 @@ object MentionUtilities {
val publicChat = DatabaseFactory.getLokiThreadDatabase(context).getPublicChat(threadID)
if (matcher.find(startIndex)) {
while (true) {
val userID = text.subSequence(matcher.start() + 1, matcher.end()).toString() // +1 to get rid of the @
val userDisplayName: String? = if (userID.toLowerCase() == TextSecurePreferences.getLocalNumber(context).toLowerCase()) {
val hexEncodedPublicKey = text.subSequence(matcher.start() + 1, matcher.end()).toString() // +1 to get rid of the @
val userDisplayName: String? = if (hexEncodedPublicKey.toLowerCase() == TextSecurePreferences.getLocalNumber(context).toLowerCase()) {
TextSecurePreferences.getProfileName(context)
} else if (publicChat != null) {
DatabaseFactory.getLokiUserDatabase(context).getServerDisplayName(publicChat.id, userID)
DatabaseFactory.getLokiUserDatabase(context).getServerDisplayName(publicChat.id, hexEncodedPublicKey)
} else {
"" // TODO: Implement
DatabaseFactory.getLokiUserDatabase(context).getDisplayName(hexEncodedPublicKey)
}
if (userDisplayName != null) {
text = text.subSequence(0, matcher.start()).toString() + "@" + userDisplayName + text.subSequence(matcher.end(), text.length)