2018-08-26 15:32:30 +00:00
|
|
|
package eu.siacs.conversations.ui.util;
|
|
|
|
|
2018-09-02 18:54:50 +00:00
|
|
|
import android.app.Activity;
|
|
|
|
import android.preference.PreferenceManager;
|
2018-08-26 15:32:30 +00:00
|
|
|
import android.support.v7.app.AlertDialog;
|
2018-09-02 18:54:50 +00:00
|
|
|
import android.text.SpannableString;
|
|
|
|
import android.text.Spanned;
|
|
|
|
import android.text.style.TypefaceSpan;
|
2019-01-26 19:31:52 +00:00
|
|
|
import android.view.ContextMenu;
|
2018-08-26 15:32:30 +00:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
2019-01-26 19:31:52 +00:00
|
|
|
import android.view.View;
|
2018-08-26 15:32:30 +00:00
|
|
|
|
|
|
|
import eu.siacs.conversations.Config;
|
2019-01-28 09:52:35 +00:00
|
|
|
import eu.siacs.conversations.R;
|
2019-10-09 18:40:42 +00:00
|
|
|
import eu.siacs.conversations.entities.Account;
|
2018-08-26 15:32:30 +00:00
|
|
|
import eu.siacs.conversations.entities.Contact;
|
|
|
|
import eu.siacs.conversations.entities.Conversation;
|
|
|
|
import eu.siacs.conversations.entities.MucOptions;
|
|
|
|
import eu.siacs.conversations.entities.MucOptions.User;
|
|
|
|
import eu.siacs.conversations.services.XmppConnectionService;
|
2018-09-02 18:54:50 +00:00
|
|
|
import eu.siacs.conversations.ui.ConferenceDetailsActivity;
|
|
|
|
import eu.siacs.conversations.ui.ConversationFragment;
|
|
|
|
import eu.siacs.conversations.ui.ConversationsActivity;
|
2019-01-26 19:31:52 +00:00
|
|
|
import eu.siacs.conversations.ui.MucUsersActivity;
|
2018-08-26 15:32:30 +00:00
|
|
|
import eu.siacs.conversations.ui.XmppActivity;
|
2020-05-15 15:06:16 +00:00
|
|
|
import eu.siacs.conversations.xmpp.Jid;
|
2018-08-26 15:32:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
public final class MucDetailsContextMenuHelper {
|
2019-01-26 19:31:52 +00:00
|
|
|
|
|
|
|
public static void onCreateContextMenu(ContextMenu menu, View v) {
|
|
|
|
final XmppActivity activity = XmppActivity.find(v);
|
|
|
|
final Object tag = v.getTag();
|
|
|
|
if (tag instanceof MucOptions.User && activity != null) {
|
|
|
|
activity.getMenuInflater().inflate(R.menu.muc_details_context, menu);
|
|
|
|
final MucOptions.User user = (MucOptions.User) tag;
|
|
|
|
String name;
|
|
|
|
final Contact contact = user.getContact();
|
|
|
|
if (contact != null && contact.showInContactList()) {
|
|
|
|
name = contact.getDisplayName();
|
|
|
|
} else if (user.getRealJid() != null) {
|
|
|
|
name = user.getRealJid().asBareJid().toString();
|
|
|
|
} else {
|
|
|
|
name = user.getName();
|
|
|
|
}
|
|
|
|
menu.setHeaderTitle(name);
|
|
|
|
MucDetailsContextMenuHelper.configureMucDetailsContextMenu(activity, menu, user.getConversation(), user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-02 18:54:50 +00:00
|
|
|
public static void configureMucDetailsContextMenu(Activity activity, Menu menu, Conversation conversation, User user) {
|
2018-11-03 16:07:02 +00:00
|
|
|
final MucOptions mucOptions = conversation.getMucOptions();
|
2018-09-02 18:54:50 +00:00
|
|
|
final boolean advancedMode = PreferenceManager.getDefaultSharedPreferences(activity).getBoolean("advanced_muc_mode", false);
|
2019-02-06 22:47:29 +00:00
|
|
|
final boolean isGroupChat = mucOptions.isPrivateAndNonAnonymous();
|
2018-11-03 16:07:02 +00:00
|
|
|
MenuItem sendPrivateMessage = menu.findItem(R.id.send_private_message);
|
2018-09-02 18:54:50 +00:00
|
|
|
if (user != null && user.getRealJid() != null) {
|
|
|
|
MenuItem showContactDetails = menu.findItem(R.id.action_contact_details);
|
|
|
|
MenuItem startConversation = menu.findItem(R.id.start_conversation);
|
|
|
|
MenuItem giveMembership = menu.findItem(R.id.give_membership);
|
|
|
|
MenuItem removeMembership = menu.findItem(R.id.remove_membership);
|
|
|
|
MenuItem giveAdminPrivileges = menu.findItem(R.id.give_admin_privileges);
|
2019-02-06 22:47:29 +00:00
|
|
|
MenuItem giveOwnerPrivileges = menu.findItem(R.id.give_owner_privileges);
|
|
|
|
MenuItem removeOwnerPrivileges = menu.findItem(R.id.revoke_owner_privileges);
|
2018-09-02 18:54:50 +00:00
|
|
|
MenuItem removeAdminPrivileges = menu.findItem(R.id.remove_admin_privileges);
|
|
|
|
MenuItem removeFromRoom = menu.findItem(R.id.remove_from_room);
|
2019-02-07 17:11:21 +00:00
|
|
|
MenuItem managePermissions = menu.findItem(R.id.manage_permissions);
|
2019-02-07 12:18:42 +00:00
|
|
|
removeFromRoom.setTitle(isGroupChat ? R.string.remove_from_room : R.string.remove_from_channel);
|
2018-09-02 18:54:50 +00:00
|
|
|
MenuItem banFromConference = menu.findItem(R.id.ban_from_conference);
|
2019-02-07 12:18:42 +00:00
|
|
|
banFromConference.setTitle(isGroupChat ? R.string.ban_from_conference : R.string.ban_from_channel);
|
2018-09-02 18:54:50 +00:00
|
|
|
MenuItem invite = menu.findItem(R.id.invite);
|
|
|
|
startConversation.setVisible(true);
|
|
|
|
final Contact contact = user.getContact();
|
|
|
|
final User self = conversation.getMucOptions().getSelf();
|
2019-10-09 18:40:42 +00:00
|
|
|
if ((contact != null && contact.showInRoster()) || mucOptions.isPrivateAndNonAnonymous()) {
|
|
|
|
showContactDetails.setVisible(contact == null || !contact.isSelf());
|
2018-09-02 18:54:50 +00:00
|
|
|
}
|
2019-01-26 19:31:52 +00:00
|
|
|
if ((activity instanceof ConferenceDetailsActivity || activity instanceof MucUsersActivity) && user.getRole() == MucOptions.Role.NONE) {
|
2018-09-02 18:54:50 +00:00
|
|
|
invite.setVisible(true);
|
|
|
|
}
|
2019-02-07 12:18:42 +00:00
|
|
|
boolean managePermissionsVisible = false;
|
|
|
|
if ((self.getAffiliation().ranks(MucOptions.Affiliation.ADMIN) && self.getAffiliation().outranks(user.getAffiliation())) || self.getAffiliation() == MucOptions.Affiliation.OWNER) {
|
2018-09-02 18:54:50 +00:00
|
|
|
if (advancedMode) {
|
2019-02-06 22:47:29 +00:00
|
|
|
if (!user.getAffiliation().ranks(MucOptions.Affiliation.MEMBER)) {
|
2019-02-07 17:11:21 +00:00
|
|
|
managePermissionsVisible = true;
|
2018-09-02 18:54:50 +00:00
|
|
|
giveMembership.setVisible(true);
|
2019-02-06 22:47:29 +00:00
|
|
|
} else if (user.getAffiliation() == MucOptions.Affiliation.MEMBER) {
|
2019-02-07 17:11:21 +00:00
|
|
|
managePermissionsVisible = true;
|
2018-09-02 18:54:50 +00:00
|
|
|
removeMembership.setVisible(true);
|
2018-08-26 15:32:30 +00:00
|
|
|
}
|
2018-09-02 18:54:50 +00:00
|
|
|
if (!Config.DISABLE_BAN) {
|
2019-02-07 17:11:21 +00:00
|
|
|
managePermissionsVisible = true;
|
2018-09-02 18:54:50 +00:00
|
|
|
banFromConference.setVisible(true);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!Config.DISABLE_BAN || conversation.getMucOptions().membersOnly()) {
|
|
|
|
removeFromRoom.setVisible(true);
|
2018-08-26 15:32:30 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-06 22:47:29 +00:00
|
|
|
}
|
|
|
|
if (self.getAffiliation().ranks(MucOptions.Affiliation.OWNER)) {
|
|
|
|
if (isGroupChat || advancedMode || user.getAffiliation() == MucOptions.Affiliation.OWNER) {
|
|
|
|
if (!user.getAffiliation().ranks(MucOptions.Affiliation.OWNER)) {
|
2019-02-07 17:11:21 +00:00
|
|
|
managePermissionsVisible = true;
|
2019-02-06 22:47:29 +00:00
|
|
|
giveOwnerPrivileges.setVisible(true);
|
|
|
|
} else if (user.getAffiliation() == MucOptions.Affiliation.OWNER){
|
2019-02-07 17:11:21 +00:00
|
|
|
managePermissionsVisible = true;
|
2019-02-06 22:47:29 +00:00
|
|
|
removeOwnerPrivileges.setVisible(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!isGroupChat || advancedMode || user.getAffiliation() == MucOptions.Affiliation.ADMIN) {
|
|
|
|
if (!user.getAffiliation().ranks(MucOptions.Affiliation.ADMIN)) {
|
2019-02-07 17:11:21 +00:00
|
|
|
managePermissionsVisible = true;
|
2019-02-06 22:47:29 +00:00
|
|
|
giveAdminPrivileges.setVisible(true);
|
|
|
|
} else if (user.getAffiliation() == MucOptions.Affiliation.ADMIN) {
|
2019-02-07 17:11:21 +00:00
|
|
|
managePermissionsVisible = true;
|
2019-02-06 22:47:29 +00:00
|
|
|
removeAdminPrivileges.setVisible(true);
|
|
|
|
}
|
2018-09-02 18:54:50 +00:00
|
|
|
}
|
2018-08-26 15:32:30 +00:00
|
|
|
}
|
2019-02-07 17:11:21 +00:00
|
|
|
managePermissions.setVisible(managePermissionsVisible);
|
2019-02-06 22:47:29 +00:00
|
|
|
sendPrivateMessage.setVisible(!isGroupChat && mucOptions.allowPm() && user.getRole().ranks(MucOptions.Role.VISITOR));
|
2018-08-26 15:32:30 +00:00
|
|
|
} else {
|
|
|
|
sendPrivateMessage.setVisible(true);
|
2018-09-02 18:54:50 +00:00
|
|
|
sendPrivateMessage.setEnabled(user != null && mucOptions.allowPm() && user.getRole().ranks(MucOptions.Role.VISITOR));
|
2018-08-26 15:32:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-26 19:31:52 +00:00
|
|
|
public static boolean onContextItemSelected(MenuItem item, User user, XmppActivity activity) {
|
2019-01-27 10:38:20 +00:00
|
|
|
return onContextItemSelected(item, user, activity, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean onContextItemSelected(MenuItem item, User user, XmppActivity activity, final String fingerprint) {
|
2019-01-26 19:31:52 +00:00
|
|
|
final Conversation conversation = user.getConversation();
|
2018-09-02 18:54:50 +00:00
|
|
|
final XmppConnectionService.OnAffiliationChanged onAffiliationChanged = activity instanceof XmppConnectionService.OnAffiliationChanged ? (XmppConnectionService.OnAffiliationChanged) activity : null;
|
2018-08-26 15:32:30 +00:00
|
|
|
Jid jid = user.getRealJid();
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case R.id.action_contact_details:
|
2019-10-09 18:40:42 +00:00
|
|
|
final Jid realJid = user.getRealJid();
|
|
|
|
final Account account = conversation.getAccount();
|
|
|
|
final Contact contact = realJid == null ? null : account.getRoster().getContact(realJid);
|
2018-08-26 15:32:30 +00:00
|
|
|
if (contact != null) {
|
2019-01-27 10:38:20 +00:00
|
|
|
activity.switchToContactDetails(contact, fingerprint);
|
2018-08-26 15:32:30 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
case R.id.start_conversation:
|
2019-01-26 19:31:52 +00:00
|
|
|
startConversation(user, activity);
|
2018-08-26 15:32:30 +00:00
|
|
|
return true;
|
|
|
|
case R.id.give_admin_privileges:
|
|
|
|
activity.xmppConnectionService.changeAffiliationInConference(conversation, jid, MucOptions.Affiliation.ADMIN, onAffiliationChanged);
|
|
|
|
return true;
|
|
|
|
case R.id.give_membership:
|
2019-09-16 12:20:15 +00:00
|
|
|
case R.id.remove_admin_privileges:
|
|
|
|
case R.id.revoke_owner_privileges:
|
2018-08-26 15:32:30 +00:00
|
|
|
activity.xmppConnectionService.changeAffiliationInConference(conversation, jid, MucOptions.Affiliation.MEMBER, onAffiliationChanged);
|
|
|
|
return true;
|
2019-02-06 22:47:29 +00:00
|
|
|
case R.id.give_owner_privileges:
|
|
|
|
activity.xmppConnectionService.changeAffiliationInConference(conversation, jid, MucOptions.Affiliation.OWNER, onAffiliationChanged);
|
|
|
|
return true;
|
2018-08-26 15:32:30 +00:00
|
|
|
case R.id.remove_membership:
|
|
|
|
activity.xmppConnectionService.changeAffiliationInConference(conversation, jid, MucOptions.Affiliation.NONE, onAffiliationChanged);
|
|
|
|
return true;
|
|
|
|
case R.id.remove_from_room:
|
2019-01-28 09:52:35 +00:00
|
|
|
removeFromRoom(user, activity, onAffiliationChanged);
|
2018-08-26 15:32:30 +00:00
|
|
|
return true;
|
|
|
|
case R.id.ban_from_conference:
|
|
|
|
activity.xmppConnectionService.changeAffiliationInConference(conversation, jid, MucOptions.Affiliation.OUTCAST, onAffiliationChanged);
|
|
|
|
if (user.getRole() != MucOptions.Role.NONE) {
|
2019-01-28 09:52:35 +00:00
|
|
|
activity.xmppConnectionService.changeRoleInConference(conversation, user.getName(), MucOptions.Role.NONE);
|
2018-08-26 15:32:30 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
case R.id.send_private_message:
|
2018-09-02 18:54:50 +00:00
|
|
|
if (activity instanceof ConversationsActivity) {
|
|
|
|
ConversationFragment conversationFragment = ConversationFragment.get(activity);
|
|
|
|
if (conversationFragment != null) {
|
|
|
|
conversationFragment.privateMessageWith(user.getFullJid());
|
|
|
|
return true;
|
|
|
|
}
|
2018-08-26 15:32:30 +00:00
|
|
|
}
|
2018-09-02 18:54:50 +00:00
|
|
|
activity.privateMsgInMuc(conversation, user.getName());
|
2018-08-26 15:32:30 +00:00
|
|
|
return true;
|
|
|
|
case R.id.invite:
|
2019-04-11 06:57:20 +00:00
|
|
|
if (user.getAffiliation().ranks(MucOptions.Affiliation.MEMBER)) {
|
2019-09-18 07:55:18 +00:00
|
|
|
activity.xmppConnectionService.directInvite(conversation, jid.asBareJid());
|
2019-04-11 06:57:20 +00:00
|
|
|
} else {
|
|
|
|
activity.xmppConnectionService.invite(conversation, jid);
|
|
|
|
}
|
2018-08-26 15:32:30 +00:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-28 09:52:35 +00:00
|
|
|
private static void removeFromRoom(final User user, XmppActivity activity, XmppConnectionService.OnAffiliationChanged onAffiliationChanged) {
|
2019-01-26 19:31:52 +00:00
|
|
|
final Conversation conversation = user.getConversation();
|
2018-08-26 15:32:30 +00:00
|
|
|
if (conversation.getMucOptions().membersOnly()) {
|
|
|
|
activity.xmppConnectionService.changeAffiliationInConference(conversation, user.getRealJid(), MucOptions.Affiliation.NONE, onAffiliationChanged);
|
|
|
|
if (user.getRole() != MucOptions.Role.NONE) {
|
2019-01-28 09:52:35 +00:00
|
|
|
activity.xmppConnectionService.changeRoleInConference(conversation, user.getName(), MucOptions.Role.NONE);
|
2018-08-26 15:32:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
|
|
|
|
builder.setTitle(R.string.ban_from_conference);
|
2018-09-02 18:54:50 +00:00
|
|
|
String jid = user.getRealJid().asBareJid().toString();
|
|
|
|
SpannableString message = new SpannableString(activity.getString(R.string.removing_from_public_conference, jid));
|
|
|
|
int start = message.toString().indexOf(jid);
|
|
|
|
if (start >= 0) {
|
|
|
|
message.setSpan(new TypefaceSpan("monospace"), start, start + jid.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
|
|
|
}
|
|
|
|
builder.setMessage(message);
|
2018-08-26 15:32:30 +00:00
|
|
|
builder.setNegativeButton(R.string.cancel, null);
|
|
|
|
builder.setPositiveButton(R.string.ban_now, (dialog, which) -> {
|
|
|
|
activity.xmppConnectionService.changeAffiliationInConference(conversation, user.getRealJid(), MucOptions.Affiliation.OUTCAST, onAffiliationChanged);
|
|
|
|
if (user.getRole() != MucOptions.Role.NONE) {
|
2019-01-28 09:52:35 +00:00
|
|
|
activity.xmppConnectionService.changeRoleInConference(conversation, user.getName(), MucOptions.Role.NONE);
|
2018-08-26 15:32:30 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
builder.create().show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-26 19:31:52 +00:00
|
|
|
private static void startConversation(User user, XmppActivity activity) {
|
2018-08-26 15:32:30 +00:00
|
|
|
if (user.getRealJid() != null) {
|
2019-01-26 19:31:52 +00:00
|
|
|
Conversation newConversation = activity.xmppConnectionService.findOrCreateConversation(user.getAccount(), user.getRealJid().asBareJid(), false, true);
|
2018-08-26 15:32:30 +00:00
|
|
|
activity.switchToConversation(newConversation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|