WIP: enable contact with UI

This commit is contained in:
Ryan ZHAO 2021-05-11 17:05:04 +10:00
parent 2cac49b965
commit b43000bdd0
8 changed files with 316 additions and 272 deletions

View File

@ -17,8 +17,6 @@
package org.thoughtcrime.securesms.conversation;
import android.Manifest;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.ActivityNotFoundException;
@ -58,9 +56,7 @@ import android.view.View.OnFocusChangeListener;
import android.view.View.OnKeyListener;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
@ -242,11 +238,11 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
public static final String LAST_SEEN_EXTRA = "last_seen";
public static final String STARTING_POSITION_EXTRA = "starting_position";
// private static final int PICK_GALLERY = 1;
// private static final int PICK_GALLERY = 1;
private static final int PICK_DOCUMENT = 2;
private static final int PICK_AUDIO = 3;
private static final int PICK_CONTACT = 4;
// private static final int GET_CONTACT_DETAILS = 5;
// private static final int GET_CONTACT_DETAILS = 5;
// private static final int GROUP_EDIT = 6;
private static final int TAKE_PHOTO = 7;
private static final int ADD_CONTACT = 8;
@ -261,7 +257,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
private ImageButton sendButton;
private ImageButton attachButton;
private ProfilePictureView profilePictureView;
private EditText titleTextView;
private TextView titleTextView;
private ConversationFragment fragment;
private Button unblockButton;
private Button makeDefaultSmsButton;
@ -274,7 +270,6 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
private ImageView muteIndicatorImageView;
private TextView subtitleTextView;
private View homeButtonContainer;
private View cancelButtonContainer;
private AttachmentTypeSelector attachmentTypeSelector;
private AttachmentManager attachmentManager;
@ -378,52 +373,6 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
}
});
if (isGroupConversation()) {
titleTextView.setEnabled(false);
} else {
titleTextView.setImeOptions(EditorInfo.IME_ACTION_DONE);
titleTextView.setOnEditorActionListener((v, actionId, event) -> {
if (actionId == EditorInfo.IME_ACTION_DONE) {
String nickname = v.getText().toString().trim();
SSKEnvironment.shared.getProfileManager().setDisplayName(this, getRecipient(), nickname);
v.clearFocus();
return true;
}
return false;
});
titleTextView.setOnFocusChangeListener((v, hasFocus) -> {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
if (!hasFocus) {
EditText textView = (EditText) v;
if (textView.getText().toString().isEmpty()) {
textView.setText(getRecipient().getName());
}
imm.hideSoftInputFromWindow(v.getWindowToken(),0);
inputPanel.animate().alpha(1f).setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
inputPanel.setVisibility(View.VISIBLE);
}
});
cancelButtonContainer.setVisibility(View.GONE);
} else {
inputPanel.animate().alpha(0f).setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
inputPanel.setVisibility(View.INVISIBLE);
}
});
String nickname = DatabaseFactory.getStorage(this).getDisplayName(getRecipient().getAddress().serialize());
titleTextView.setText(nickname);
if (nickname != null) {
titleTextView.setSelection(nickname.length());
}
imm.showSoftInput(v, 0);
cancelButtonContainer.setVisibility(View.VISIBLE);
}
});
}
MentionManagerUtilities.INSTANCE.populateUserPublicKeyCacheIfNeeded(threadId, this);
OpenGroup publicChat = DatabaseFactory.getLokiThreadDatabase(this).getPublicChat(threadId);
@ -722,14 +671,10 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
if (isSingleConversation() && getRecipient().getContactUri() == null) {
inflater.inflate(R.menu.conversation_add_to_contacts, menu);
}
if (recipient != null && recipient.isLocalNumber()) {
if (isSecureText) menu.findItem(R.id.menu_call_secure).setVisible(false);
else menu.findItem(R.id.menu_call_insecure).setVisible(false);
MenuItem muteItem = menu.findItem(R.id.menu_mute_notifications);
if (muteItem != null) {
muteItem.setVisible(false);
}
@ -1142,7 +1087,6 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
/* Loki - We don't support SMS
if (!isSecureText && !isPushGroupConversation()) sendButton.disableTransport(Type.TEXTSECURE);
if (recipient.isPushGroupRecipient()) sendButton.disableTransport(Type.SMS);
if (!recipient.isPushGroupRecipient() && recipient.isForceSmsSelection()) {
sendButton.setDefaultTransport(Type.SMS);
} else {
@ -1314,7 +1258,6 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
muteIndicatorImageView = ViewUtil.findById(this, R.id.muteIndicatorImageView);
subtitleTextView = ViewUtil.findById(this, R.id.subtitleTextView);
homeButtonContainer = ViewUtil.findById(this, R.id.homeButtonContainer);
cancelButtonContainer = ViewUtil.findById(this, R.id.cancelButtonContainer);
ImageButton quickCameraToggle = ViewUtil.findById(this, R.id.quick_camera_toggle);
ImageButton inlineAttachmentButton = ViewUtil.findById(this, R.id.inline_attachment_button);
@ -1360,12 +1303,6 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
inlineAttachmentButton.setOnClickListener(v -> handleAddAttachment());
homeButtonContainer.setOnClickListener(v -> onSupportNavigateUp());
cancelButtonContainer.setOnClickListener(v -> {
titleTextView.setText("");
SSKEnvironment.shared.getProfileManager().setDisplayName(this, getRecipient(), "");
titleTextView.clearFocus();
});
}
protected void initializeActionBar() {
@ -2382,8 +2319,9 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
} else if (recipient.getAddress().toString().toLowerCase().equals(userPublicKey)) {
titleTextView.setText(getResources().getString(R.string.note_to_self));
} else {
boolean hasName = (recipient.getName() != null && !recipient.getName().isEmpty());
titleTextView.setText(hasName ? recipient.getName() : recipient.getAddress().toString());
String displayName = SSKEnvironment.shared.getProfileManager().getDisplayName(getApplicationContext(), recipient);
boolean hasName = displayName != null;
titleTextView.setText(hasName ? displayName : recipient.getAddress().toString());
}
}

View File

@ -2,6 +2,7 @@ package org.thoughtcrime.securesms.loki.database
import android.content.ContentValues
import android.content.Context
import androidx.core.database.getStringOrNull
import net.sqlcipher.Cursor
import org.session.libsession.messaging.contacts.Contact
import org.session.libsession.messaging.jobs.Job
@ -35,7 +36,7 @@ class SessionContactDatabase(context: Context, helper: SQLCipherOpenHelper) : Da
fun getContactWithSessionID(sessionID: String): Contact? {
val database = databaseHelper.readableDatabase
return database.get(sessionContactTable, "$sessionID = ?", arrayOf(sessionID)) { cursor ->
return database.get(sessionContactTable, "${SessionContactDatabase.sessionID} = ?", arrayOf(sessionID)) { cursor ->
contactFromCursor(cursor)
}
}
@ -55,7 +56,9 @@ class SessionContactDatabase(context: Context, helper: SQLCipherOpenHelper) : Da
contentValues.put(nickname, contact.nickname)
contentValues.put(profilePictureURL, contact.profilePictureURL)
contentValues.put(profilePictureFileName, contact.profilePictureFileName)
contentValues.put(profilePictureEncryptionKey, Base64.encodeBytes(contact.profilePictureEncryptionKey))
contact.profilePictureEncryptionKey?.let {
contentValues.put(profilePictureEncryptionKey, Base64.encodeBytes(it))
}
contentValues.put(threadID, threadID)
contentValues.put(isTrusted, if (contact.isTrusted) 1 else 0)
database.insertOrUpdate(sessionContactTable, contentValues, "$sessionID = ?", arrayOf(contact.sessionID))
@ -64,11 +67,13 @@ class SessionContactDatabase(context: Context, helper: SQLCipherOpenHelper) : Da
private fun contactFromCursor(cursor: Cursor): Contact {
val sessionID = cursor.getString(sessionID)
val contact = Contact(sessionID)
contact.name = cursor.getString(name)
contact.nickname = cursor.getString(nickname)
contact.profilePictureURL = cursor.getString(profilePictureURL)
contact.profilePictureFileName = cursor.getString(profilePictureFileName)
contact.profilePictureEncryptionKey = Base64.decode(cursor.getString(profilePictureEncryptionKey))
contact.name = cursor.getStringOrNull(name)
contact.nickname = cursor.getStringOrNull(nickname)
contact.profilePictureURL = cursor.getStringOrNull(profilePictureURL)
contact.profilePictureFileName = cursor.getStringOrNull(profilePictureFileName)
cursor.getStringOrNull(profilePictureEncryptionKey)?.let {
contact.profilePictureEncryptionKey = Base64.decode(it)
}
contact.threadID = cursor.getInt(threadID)
contact.isTrusted = cursor.getInt(isTrusted) != 0
return contact

View File

@ -3,6 +3,7 @@ package org.thoughtcrime.securesms.loki.dialogs
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.opengl.Visibility
import android.os.Bundle
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import android.view.LayoutInflater
@ -12,6 +13,9 @@ import android.widget.Toast
import kotlinx.android.synthetic.main.fragment_user_details_bottom_sheet.*
import kotlinx.android.synthetic.main.view_conversation.view.*
import network.loki.messenger.R
import org.session.libsession.messaging.threads.Address
import org.session.libsession.messaging.threads.recipients.Recipient
import org.session.libsession.utilities.SSKEnvironment
import org.thoughtcrime.securesms.database.DatabaseFactory
import org.thoughtcrime.securesms.mms.GlideApp
@ -24,11 +28,32 @@ public class UserDetailsBottomSheet : BottomSheetDialogFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val publicKey = arguments?.getString("publicKey") ?: return dismiss()
val recipient = Recipient.from(requireContext(), Address.fromSerialized(publicKey), false)
profilePictureView.publicKey = publicKey
profilePictureView.glide = GlideApp.with(this)
profilePictureView.isLarge = true
profilePictureView.update()
nameTextView.text = DatabaseFactory.getLokiUserDatabase(requireContext()).getDisplayName(publicKey) ?: "Anonymous"
nameTextViewContainer.visibility = View.VISIBLE
nameTextViewContainer.setOnClickListener {
nameTextViewContainer.visibility = View.INVISIBLE
nameEditContainer.visibility = View.VISIBLE
nameEditText.requestFocus()
}
btnCancelNickNameEdit.setOnClickListener {
nameEditText.clearFocus()
nameTextViewContainer.visibility = View.VISIBLE
nameEditContainer.visibility = View.INVISIBLE
nameEditText.text = null
}
btnSaveNickNameEdit.setOnClickListener {
nameEditText.clearFocus()
nameTextViewContainer.visibility = View.VISIBLE
nameEditContainer.visibility = View.INVISIBLE
var newNickName = null
SSKEnvironment.shared.profileManager.setDisplayName(requireContext(), recipient, newNickName)
nameTextView.text = SSKEnvironment.shared.profileManager.getDisplayName(requireContext(), recipient) ?: "Anonymous"
}
nameTextView.text = SSKEnvironment.shared.profileManager.getDisplayName(requireContext(), recipient) ?: "Anonymous"
publicKeyTextView.text = publicKey
copyButton.setOnClickListener {
val clipboard = requireContext().getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager

View File

@ -1,6 +1,7 @@
package org.thoughtcrime.securesms.loki.utilities
import android.content.ContentValues
import androidx.core.database.getStringOrNull
import net.sqlcipher.Cursor
import net.sqlcipher.database.SQLiteDatabase
import org.session.libsignal.utilities.Base64
@ -57,3 +58,7 @@ fun Cursor.getLong(columnName: String): Long {
fun Cursor.getBase64EncodedData(columnName: String): ByteArray {
return Base64.decode(getString(columnName))
}
fun Cursor.getStringOrNull(columnName: String): String? {
return getStringOrNull(getColumnIndexOrThrow(columnName))
}

View File

@ -73,4 +73,16 @@ class ProfileManager: SSKEnvironment.ProfileManagerProtocol {
override fun updateOpenGroupProfilePicturesIfNeeded(context: Context) {
ApplicationContext.getInstance(context).updateOpenGroupProfilePicturesIfNeeded()
}
override fun getDisplayName(context: Context, recipient: Recipient): String? {
val sessionID = recipient.address.serialize()
val contactDatabase = DatabaseFactory.getSessionContactDatabase(context)
var contact = contactDatabase.getContactWithSessionID(sessionID)
if (contact == null) {
contact = Contact(sessionID)
contact.name = DatabaseFactory.getLokiUserDatabase(context).getDisplayName(sessionID) ?: recipient.profileName ?: recipient.name
contactDatabase.setContact(contact)
}
return contact.displayName(Contact.contextForRecipient(recipient))
}
}

View File

@ -51,7 +51,7 @@
android:layout_marginStart="@dimen/medium_spacing"
android:orientation="vertical">
<EditText
<TextView
android:id="@+id/titleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
@ -59,10 +59,8 @@
android:maxLines="1"
android:ellipsize="end"
android:text="Conversation"
android:hint="Enter a name"
android:textColor="@color/text"
android:textSize="@dimen/large_font_size"
android:inputType="text"
android:fontFamily="sans-serif-medium" />
<LinearLayout

View File

@ -16,17 +16,77 @@
android:id="@+id/profilePictureView"
android:layout_width="@dimen/large_profile_picture_size"
android:layout_height="@dimen/large_profile_picture_size"
android:layout_marginTop="@dimen/large_spacing" />
android:layout_marginTop="@dimen/large_spacing"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/medium_spacing"
android:gravity="center">
<LinearLayout
android:id="@+id/nameTextViewContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:visibility="invisible">
<TextView
android:id="@+id/nameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/medium_spacing"
android:textSize="@dimen/massive_font_size"
android:textStyle="bold"
android:textColor="@color/text"
android:text="Spiderman" />
android:text="Spiderman"
android:paddingStart="24dp"
android:paddingEnd="0dp"
android:drawablePadding="@dimen/small_spacing"
app:drawableEndCompat="@drawable/ic_baseline_edit_24" />
</LinearLayout>
<LinearLayout
android:id="@+id/nameEditContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:visibility="invisible">
<ImageView
android:id="@+id/btnCancelNickNameEdit"
android:layout_width="24dp"
android:layout_height="24dp"
android:src="@drawable/ic_baseline_clear_24"/>
<EditText
android:id="@+id/nameEditText"
style="@style/SessionEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="@dimen/small_spacing"
android:layout_marginEnd="@dimen/small_spacing"
android:textAlignment="center"
android:textSize="@dimen/very_large_font_size"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:inputType="text"
android:singleLine="true"
android:imeOptions="actionDone"
android:hint="Enter a name"/>
<ImageView
android:id="@+id/btnSaveNickNameEdit"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="center"
android:src="@drawable/ic_baseline_done_24"/>
</LinearLayout>
</RelativeLayout>
<TextView
style="@style/SessionIDTextView"

View File

@ -36,6 +36,7 @@ class SSKEnvironment(
fun setProfileKey(context: Context, recipient: Recipient, profileKey: ByteArray)
fun setUnidentifiedAccessMode(context: Context, recipient: Recipient, unidentifiedAccessMode: Recipient.UnidentifiedAccessMode)
fun updateOpenGroupProfilePicturesIfNeeded(context: Context)
fun getDisplayName(context: Context, recipient: Recipient): String?
}
interface MessageExpirationManagerProtocol {