code cleanup for group chat user context menu

This commit is contained in:
Daniel Gultsch 2018-09-02 20:54:50 +02:00
parent ffc5d47091
commit 516b6dcc1f
4 changed files with 84 additions and 74 deletions

View File

@ -564,7 +564,6 @@ public class MucOptions {
if (user == null) { if (user == null) {
user = new User(this, null); user = new User(this, null);
user.setRealJid(jid); user.setRealJid(jid);
user.setRole("visitor");
} }
return user; return user;
} }

View File

@ -418,7 +418,6 @@ public class ConferenceDetailsActivity extends XmppActivity implements OnConvers
if (tag instanceof User) { if (tag instanceof User) {
getMenuInflater().inflate(R.menu.muc_details_context, menu); getMenuInflater().inflate(R.menu.muc_details_context, menu);
final User user = (User) tag; final User user = (User) tag;
final User self = mConversation.getMucOptions().getSelf();
this.mSelectedUser = user; this.mSelectedUser = user;
String name; String name;
final Contact contact = user.getContact(); final Contact contact = user.getContact();
@ -430,26 +429,19 @@ public class ConferenceDetailsActivity extends XmppActivity implements OnConvers
name = user.getName(); name = user.getName();
} }
menu.setHeaderTitle(name); menu.setHeaderTitle(name);
MucDetailsContextMenuHelper.configureMucDetailsContextMenu(menu, mConversation, user, mAdvancedMode); MucDetailsContextMenuHelper.configureMucDetailsContextMenu(this, menu, mConversation, user);
} }
super.onCreateContextMenu(menu, v, menuInfo); super.onCreateContextMenu(menu, v, menuInfo);
} }
@Override @Override
public boolean onContextItemSelected(MenuItem item) { public boolean onContextItemSelected(MenuItem item) {
if (!MucDetailsContextMenuHelper.onContextItemSelected(item, mSelectedUser, mConversation, this, this, this)) { if (!MucDetailsContextMenuHelper.onContextItemSelected(item, mSelectedUser, mConversation, this)) {
return super.onContextItemSelected(item); return super.onContextItemSelected(item);
} }
return true; return true;
} }
private void removeFromRoom(final User user) {
MucDetailsContextMenuHelper.removeFromRoom(user, mConversation, this, this, this);
}
protected void startConversation(User user) {
MucDetailsContextMenuHelper.startConversation(user, this.mConversation, this);
}
protected void saveAsBookmark() { protected void saveAsBookmark() {
xmppConnectionService.saveConversationAsBookmark(mConversation, mConversation.getMucOptions().getName()); xmppConnectionService.saveConversationAsBookmark(mConversation, mConversation.getMucOptions().getName());

View File

@ -3,6 +3,7 @@ package eu.siacs.conversations.ui;
import android.Manifest; import android.Manifest;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.Activity; import android.app.Activity;
import android.app.FragmentManager;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.databinding.DataBindingUtil; import android.databinding.DataBindingUtil;
@ -485,6 +486,17 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
} }
} }
public static ConversationFragment get(Activity activity) {
FragmentManager fragmentManager = activity.getFragmentManager();
Fragment fragment = fragmentManager.findFragmentById(R.id.main_fragment);
if (fragment != null && fragment instanceof ConversationFragment) {
return (ConversationFragment) fragment;
} else {
fragment = fragmentManager.findFragmentById(R.id.secondary_fragment);
return fragment != null && fragment instanceof ConversationFragment ? (ConversationFragment) fragment : null;
}
}
public static Conversation getConversationReliable(Activity activity) { public static Conversation getConversationReliable(Activity activity) {
final Conversation conversation = getConversation(activity, R.id.secondary_fragment); final Conversation conversation = getConversation(activity, R.id.secondary_fragment);
if (conversation != null) { if (conversation != null) {
@ -986,21 +998,13 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
Jid tcp = message.getTrueCounterpart(); Jid tcp = message.getTrueCounterpart();
Jid cp = message.getCounterpart(); Jid cp = message.getCounterpart();
if (cp != null && !cp.isBareJid()) { if (cp != null && !cp.isBareJid()) {
User userByRealJid = conversation.getMucOptions().findOrCreateUserByRealJid(tcp); User userByRealJid = tcp != null ? conversation.getMucOptions().findOrCreateUserByRealJid(tcp) : null;
final User user = userByRealJid != null ? userByRealJid : conversation.getMucOptions().findUserByFullJid(cp); final User user = userByRealJid != null ? userByRealJid : conversation.getMucOptions().findUserByFullJid(cp);
final PopupMenu popupMenu = new PopupMenu(getActivity(), v); final PopupMenu popupMenu = new PopupMenu(getActivity(), v);
popupMenu.inflate(R.menu.muc_details_context); popupMenu.inflate(R.menu.muc_details_context);
final Menu menu = popupMenu.getMenu(); final Menu menu = popupMenu.getMenu();
final boolean advancedMode = activity.getPreferences().getBoolean("advanced_muc_mode", false); MucDetailsContextMenuHelper.configureMucDetailsContextMenu(activity, menu, conversation, user);
MucDetailsContextMenuHelper.configureMucDetailsContextMenu(menu, conversation, user, advancedMode); popupMenu.setOnMenuItemClickListener(menuItem -> MucDetailsContextMenuHelper.onContextItemSelected(menuItem, user, conversation, activity));
final MucOptions mucOptions = ((Conversation) message.getConversation()).getMucOptions();
popupMenu.setOnMenuItemClickListener(menuItem -> {
if (menuItem.getItemId() == R.id.send_private_message) {
privateMessageWith(cp);
return true;
}
return MucDetailsContextMenuHelper.onContextItemSelected(menuItem, user, conversation, activity, activity, activity);
});
popupMenu.show(); popupMenu.show();
} }
} }
@ -1673,7 +1677,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
conversation.getAccount().getPgpDecryptionService().decrypt(message, false); conversation.getAccount().getPgpDecryptionService().decrypt(message, false);
} }
private void privateMessageWith(final Jid counterpart) { public void privateMessageWith(final Jid counterpart) {
if (conversation.setOutgoingChatState(Config.DEFAULT_CHATSTATE)) { if (conversation.setOutgoingChatState(Config.DEFAULT_CHATSTATE)) {
activity.xmppConnectionService.sendChatState(conversation); activity.xmppConnectionService.sendChatState(conversation);
} }

View File

@ -1,9 +1,14 @@
package eu.siacs.conversations.ui.util; package eu.siacs.conversations.ui.util;
import android.app.Activity;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.TypefaceSpan;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.widget.Toast;
import eu.siacs.conversations.Config; import eu.siacs.conversations.Config;
import eu.siacs.conversations.entities.Contact; import eu.siacs.conversations.entities.Contact;
@ -12,14 +17,17 @@ import eu.siacs.conversations.entities.MucOptions;
import eu.siacs.conversations.entities.MucOptions.User; import eu.siacs.conversations.entities.MucOptions.User;
import eu.siacs.conversations.R; import eu.siacs.conversations.R;
import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.ui.ConferenceDetailsActivity;
import eu.siacs.conversations.ui.ConversationFragment;
import eu.siacs.conversations.ui.ConversationsActivity;
import eu.siacs.conversations.ui.XmppActivity; import eu.siacs.conversations.ui.XmppActivity;
import rocks.xmpp.addr.Jid; import rocks.xmpp.addr.Jid;
public final class MucDetailsContextMenuHelper { public final class MucDetailsContextMenuHelper {
public static void configureMucDetailsContextMenu(Menu menu, Conversation conversation, User user, boolean advancedMode) { public static void configureMucDetailsContextMenu(Activity activity, Menu menu, Conversation conversation, User user) {
if (user != null) { final boolean advancedMode = PreferenceManager.getDefaultSharedPreferences(activity).getBoolean("advanced_muc_mode", false);
if (user.getRealJid() != null) { if (user != null && user.getRealJid() != null) {
MenuItem showContactDetails = menu.findItem(R.id.action_contact_details); MenuItem showContactDetails = menu.findItem(R.id.action_contact_details);
MenuItem startConversation = menu.findItem(R.id.start_conversation); MenuItem startConversation = menu.findItem(R.id.start_conversation);
MenuItem giveMembership = menu.findItem(R.id.give_membership); MenuItem giveMembership = menu.findItem(R.id.give_membership);
@ -35,7 +43,7 @@ public final class MucDetailsContextMenuHelper {
if (contact != null && contact.showInRoster()) { if (contact != null && contact.showInRoster()) {
showContactDetails.setVisible(!contact.isSelf()); showContactDetails.setVisible(!contact.isSelf());
} }
if (user.getRole() == MucOptions.Role.NONE) { if (activity instanceof ConferenceDetailsActivity && user.getRole() == MucOptions.Role.NONE) {
invite.setVisible(true); invite.setVisible(true);
} }
if (self.getAffiliation().ranks(MucOptions.Affiliation.ADMIN) && if (self.getAffiliation().ranks(MucOptions.Affiliation.ADMIN) &&
@ -61,18 +69,16 @@ public final class MucDetailsContextMenuHelper {
} }
} }
} else { } else {
final MucOptions mucOptions = conversation.getMucOptions();
MenuItem sendPrivateMessage = menu.findItem(R.id.send_private_message); MenuItem sendPrivateMessage = menu.findItem(R.id.send_private_message);
sendPrivateMessage.setVisible(true); sendPrivateMessage.setVisible(true);
sendPrivateMessage.setEnabled(user.getRole().ranks(MucOptions.Role.VISITOR)); sendPrivateMessage.setEnabled(user != null && mucOptions.allowPm() && user.getRole().ranks(MucOptions.Role.VISITOR));
}
} else {
MenuItem sendPrivateMessage = menu.findItem(R.id.send_private_message);
sendPrivateMessage.setVisible(true);
sendPrivateMessage.setEnabled(false);
} }
} }
public static boolean onContextItemSelected(MenuItem item, User user, Conversation conversation, XmppActivity activity, XmppConnectionService.OnAffiliationChanged onAffiliationChanged, XmppConnectionService.OnRoleChanged onRoleChanged) { public static boolean onContextItemSelected(MenuItem item, User user, Conversation conversation, XmppActivity activity) {
final XmppConnectionService.OnAffiliationChanged onAffiliationChanged = activity instanceof XmppConnectionService.OnAffiliationChanged ? (XmppConnectionService.OnAffiliationChanged) activity : null;
final XmppConnectionService.OnRoleChanged onRoleChanged = activity instanceof XmppConnectionService.OnRoleChanged ? (XmppConnectionService.OnRoleChanged) activity : null;
Jid jid = user.getRealJid(); Jid jid = user.getRealJid();
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.action_contact_details: case R.id.action_contact_details:
@ -106,11 +112,14 @@ public final class MucDetailsContextMenuHelper {
} }
return true; return true;
case R.id.send_private_message: case R.id.send_private_message:
if (conversation.getMucOptions().allowPm()) { if (activity instanceof ConversationsActivity) {
activity.privateMsgInMuc(conversation, user.getName()); ConversationFragment conversationFragment = ConversationFragment.get(activity);
} else { if (conversationFragment != null) {
Toast.makeText(activity, R.string.private_messages_are_disabled, Toast.LENGTH_SHORT).show(); conversationFragment.privateMessageWith(user.getFullJid());
return true;
} }
}
activity.privateMsgInMuc(conversation, user.getName());
return true; return true;
case R.id.invite: case R.id.invite:
activity.xmppConnectionService.directInvite(conversation, jid); activity.xmppConnectionService.directInvite(conversation, jid);
@ -129,7 +138,13 @@ public final class MucDetailsContextMenuHelper {
} else { } else {
AlertDialog.Builder builder = new AlertDialog.Builder(activity); AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle(R.string.ban_from_conference); builder.setTitle(R.string.ban_from_conference);
builder.setMessage(activity.getString(R.string.removing_from_public_conference, user.getName())); 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);
builder.setNegativeButton(R.string.cancel, null); builder.setNegativeButton(R.string.cancel, null);
builder.setPositiveButton(R.string.ban_now, (dialog, which) -> { builder.setPositiveButton(R.string.ban_now, (dialog, which) -> {
activity.xmppConnectionService.changeAffiliationInConference(conversation, user.getRealJid(), MucOptions.Affiliation.OUTCAST, onAffiliationChanged); activity.xmppConnectionService.changeAffiliationInConference(conversation, user.getRealJid(), MucOptions.Affiliation.OUTCAST, onAffiliationChanged);