Merge pull request #106 from loki-project/group-leaving

Group leaving
pull/111/head
gmbnt 4 years ago committed by GitHub
commit c1404d7cca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1666,6 +1666,8 @@
<!-- Session --> <!-- Session -->
<string name="activity_register_public_key_copied_message">Copied to clipboard</string> <string name="activity_register_public_key_copied_message">Copied to clipboard</string>
<!-- Session --> <string name="activity_home_leave_group_dialog_message">Are you sure you want to leave this group?</string>
<string name="activity_home_delete_conversation_dialog_message">Are you sure you want to delete this conversation?</string>
<string name="activity_home_conversation_deleted_message">Conversation deleted</string>
</resources> </resources>

@ -1156,21 +1156,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
builder.setMessage(getString(R.string.ConversationActivity_are_you_sure_you_want_to_leave_this_group)); builder.setMessage(getString(R.string.ConversationActivity_are_you_sure_you_want_to_leave_this_group));
builder.setPositiveButton(R.string.yes, (dialog, which) -> { builder.setPositiveButton(R.string.yes, (dialog, which) -> {
Recipient groupRecipient = getRecipient(); Recipient groupRecipient = getRecipient();
long threadId = DatabaseFactory.getThreadDatabase(this).getThreadIdFor(groupRecipient); if (GroupUtil.leaveGroup(this, groupRecipient)) {
Optional<OutgoingGroupMediaMessage> leaveMessage = GroupUtil.createGroupLeaveMessage(this, groupRecipient);
if (threadId != -1 && leaveMessage.isPresent()) {
MessageSender.send(this, leaveMessage.get(), threadId, false, null);
// We need to remove the master device from the group
String masterHexEncodedPublicKey = TextSecurePreferences.getMasterHexEncodedPublicKey(this);
String localNumber = masterHexEncodedPublicKey != null ? masterHexEncodedPublicKey : TextSecurePreferences.getLocalNumber(this);
GroupDatabase groupDatabase = DatabaseFactory.getGroupDatabase(this);
String groupId = groupRecipient.getAddress().toGroupString();
groupDatabase.setActive(groupId, false);
groupDatabase.remove(groupId, Address.fromSerialized(localNumber));
initializeEnabledCheck(); initializeEnabledCheck();
} else { } else {
Toast.makeText(this, R.string.ConversationActivity_error_leaving_group, Toast.LENGTH_LONG).show(); Toast.makeText(this, R.string.ConversationActivity_error_leaving_group, Toast.LENGTH_LONG).show();

@ -1,6 +1,7 @@
package org.thoughtcrime.securesms.loki.redesign.activities package org.thoughtcrime.securesms.loki.redesign.activities
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.AlertDialog
import android.arch.lifecycle.Observer import android.arch.lifecycle.Observer
import android.content.Intent import android.content.Intent
import android.database.Cursor import android.database.Cursor
@ -10,7 +11,6 @@ import android.graphics.Paint
import android.os.AsyncTask import android.os.AsyncTask
import android.os.Bundle import android.os.Bundle
import android.os.Handler import android.os.Handler
import android.support.design.widget.Snackbar
import android.support.v4.app.LoaderManager import android.support.v4.app.LoaderManager
import android.support.v4.content.Loader import android.support.v4.content.Loader
import android.support.v7.widget.LinearLayoutManager import android.support.v7.widget.LinearLayoutManager
@ -20,6 +20,7 @@ import android.text.Spannable
import android.text.SpannableString import android.text.SpannableString
import android.text.style.ForegroundColorSpan import android.text.style.ForegroundColorSpan
import android.view.View import android.view.View
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_home.* import kotlinx.android.synthetic.main.activity_home.*
import network.loki.messenger.R import network.loki.messenger.R
import org.thoughtcrime.securesms.ApplicationContext import org.thoughtcrime.securesms.ApplicationContext
@ -36,6 +37,7 @@ import org.thoughtcrime.securesms.loki.redesign.views.SeedReminderViewDelegate
import org.thoughtcrime.securesms.mms.GlideApp import org.thoughtcrime.securesms.mms.GlideApp
import org.thoughtcrime.securesms.mms.GlideRequests import org.thoughtcrime.securesms.mms.GlideRequests
import org.thoughtcrime.securesms.notifications.MessageNotifier import org.thoughtcrime.securesms.notifications.MessageNotifier
import org.thoughtcrime.securesms.util.GroupUtil
import org.thoughtcrime.securesms.util.TextSecurePreferences import org.thoughtcrime.securesms.util.TextSecurePreferences
import kotlin.math.abs import kotlin.math.abs
@ -229,9 +231,10 @@ class HomeActivity : PassphraseRequiredActionBarActivity, ConversationClickListe
@SuppressLint("StaticFieldLeak") @SuppressLint("StaticFieldLeak")
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) { override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
val threadID = (viewHolder as HomeAdapter.ViewHolder).view.thread!!.threadId viewHolder as HomeAdapter.ViewHolder
val threadID = viewHolder.view.thread!!.threadId
val recipient = viewHolder.view.thread!!.recipient
val threadDatabase = DatabaseFactory.getThreadDatabase(activity) val threadDatabase = DatabaseFactory.getThreadDatabase(activity)
threadDatabase.archiveConversation(threadID)
val deleteThread = object : Runnable { val deleteThread = object : Runnable {
override fun run() { override fun run() {
@ -248,16 +251,32 @@ class HomeActivity : PassphraseRequiredActionBarActivity, ConversationClickListe
} }
} }
} }
val dialogMessage = if (recipient.isGroupRecipient) R.string.activity_home_leave_group_dialog_message else R.string.activity_home_delete_conversation_dialog_message
val dialog = AlertDialog.Builder(activity)
dialog.setMessage(dialogMessage)
dialog.setPositiveButton(R.string.yes) { _, _ ->
val isGroup = recipient.isGroupRecipient
// Send a leave group message if this is an active closed group
if (isGroup && DatabaseFactory.getGroupDatabase(activity).isActive(recipient.address.toGroupString())) {
if (!GroupUtil.leaveGroup(activity, recipient)) {
Toast.makeText(activity, "Couldn't leave group", Toast.LENGTH_LONG).show()
clearView(activity.recyclerView, viewHolder)
return@setPositiveButton
}
}
// Archive the conversation and then delete it after 10 seconds (the case where the
// app was closed before the conversation could be deleted is handled in onCreate)
threadDatabase.archiveConversation(threadID)
val handler = Handler() val handler = Handler()
handler.postDelayed(deleteThread, 5000) handler.postDelayed(deleteThread, 10000)
val snackbar = Snackbar.make(activity.contentView, "Conversation Deleted", Snackbar.LENGTH_LONG) // Notify the user
snackbar.setAction("Undo") { val toastMessage = if (isGroup) R.string.MessageRecord_left_group else R.string.activity_home_conversation_deleted_message
threadDatabase.unarchiveConversation(threadID) Toast.makeText(activity, toastMessage, Toast.LENGTH_LONG).show()
handler.removeCallbacks(deleteThread) }
animate(viewHolder, 0.0f) dialog.setNegativeButton(R.string.no) { _, _ ->
} clearView(activity.recyclerView, viewHolder)
snackbar.setActionTextColor(activity.resources.getColorWithID(R.color.accent, activity.theme)) }
snackbar.show() dialog.create().show()
} }
override fun onChildDraw(c: Canvas, recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, dx: Float, dy: Float, actionState: Int, isCurrentlyActive: Boolean) { override fun onChildDraw(c: Canvas, recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, dx: Float, dy: Float, actionState: Int, isCurrentlyActive: Boolean) {

@ -4,6 +4,7 @@ import android.content.Context;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.annotation.WorkerThread; import android.support.annotation.WorkerThread;
import android.widget.Toast;
import com.google.protobuf.ByteString; import com.google.protobuf.ByteString;
@ -16,6 +17,7 @@ import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.mms.OutgoingGroupMediaMessage; import org.thoughtcrime.securesms.mms.OutgoingGroupMediaMessage;
import org.thoughtcrime.securesms.recipients.Recipient; import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.RecipientModifiedListener; import org.thoughtcrime.securesms.recipients.RecipientModifiedListener;
import org.thoughtcrime.securesms.sms.MessageSender;
import org.whispersystems.libsignal.util.guava.Optional; import org.whispersystems.libsignal.util.guava.Optional;
import org.whispersystems.signalservice.api.messages.SignalServiceGroup; import org.whispersystems.signalservice.api.messages.SignalServiceGroup;
@ -111,6 +113,27 @@ public class GroupUtil {
return Optional.of(new OutgoingGroupMediaMessage(groupRecipient, groupContext, null, System.currentTimeMillis(), 0, null, Collections.emptyList(), Collections.emptyList())); return Optional.of(new OutgoingGroupMediaMessage(groupRecipient, groupContext, null, System.currentTimeMillis(), 0, null, Collections.emptyList(), Collections.emptyList()));
} }
public static boolean leaveGroup(@NonNull Context context, Recipient groupRecipient) {
long threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(groupRecipient);
Optional<OutgoingGroupMediaMessage> leaveMessage = GroupUtil.createGroupLeaveMessage(context, groupRecipient);
if (threadId < 0 || !leaveMessage.isPresent()) {
return false;
}
MessageSender.send(context, leaveMessage.get(), threadId, false, null);
// We need to remove the master device from the group
String masterHexEncodedPublicKey = TextSecurePreferences.getMasterHexEncodedPublicKey(context);
String localNumber = masterHexEncodedPublicKey != null ? masterHexEncodedPublicKey : TextSecurePreferences.getLocalNumber(context);
GroupDatabase groupDatabase = DatabaseFactory.getGroupDatabase(context);
String groupId = groupRecipient.getAddress().toGroupString();
groupDatabase.setActive(groupId, false);
groupDatabase.remove(groupId, Address.fromSerialized(localNumber));
return true;
}
public static @NonNull GroupDescription getDescription(@NonNull Context context, @Nullable String encodedGroup) { public static @NonNull GroupDescription getDescription(@NonNull Context context, @Nullable String encodedGroup) {
if (encodedGroup == null) { if (encodedGroup == null) {

Loading…
Cancel
Save