From 3ecb7136c1e714e738ae076bc2f3d6d13c874f04 Mon Sep 17 00:00:00 2001 From: Christoph Scholz Date: Sun, 26 Aug 2018 17:32:30 +0200 Subject: [PATCH] start new conversations by long press on avatar --- .../conversations/entities/MucOptions.java | 10 ++ .../ui/ConferenceDetailsActivity.java | 119 ++------------ .../ui/ConversationFragment.java | 35 ++-- .../ui/ConversationsActivity.java | 27 +++- .../ui/adapter/MessageAdapter.java | 4 +- .../ui/util/MucDetailsContextMenuHelper.java | 150 ++++++++++++++++++ 6 files changed, 219 insertions(+), 126 deletions(-) create mode 100644 src/main/java/eu/siacs/conversations/ui/util/MucDetailsContextMenuHelper.java diff --git a/src/main/java/eu/siacs/conversations/entities/MucOptions.java b/src/main/java/eu/siacs/conversations/entities/MucOptions.java index 727b06226..3d938bfe5 100644 --- a/src/main/java/eu/siacs/conversations/entities/MucOptions.java +++ b/src/main/java/eu/siacs/conversations/entities/MucOptions.java @@ -559,6 +559,16 @@ public class MucOptions { return null; } + public User findOrCreateUserByRealJid(Jid jid) { + User user = findUserByRealJid(jid); + if (user == null) { + user = new User(this, null); + user.setRealJid(jid); + user.setRole("visitor"); + } + return user; + } + public User findUser(ReadByMarker readByMarker) { if (readByMarker.getRealJid() != null) { User user = findUserByRealJid(readByMarker.getRealJid().asBareJid()); diff --git a/src/main/java/eu/siacs/conversations/ui/ConferenceDetailsActivity.java b/src/main/java/eu/siacs/conversations/ui/ConferenceDetailsActivity.java index 8ab6d4c6d..7e5dc81e5 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConferenceDetailsActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ConferenceDetailsActivity.java @@ -11,6 +11,7 @@ import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.AsyncTask; import android.os.Bundle; +import android.preference.PreferenceManager; import android.support.v7.app.AlertDialog; import android.support.v7.widget.Toolbar; import android.text.Editable; @@ -49,6 +50,7 @@ import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.services.XmppConnectionService.OnConversationUpdate; import eu.siacs.conversations.services.XmppConnectionService.OnMucRosterUpdate; import eu.siacs.conversations.ui.util.MenuDoubleTabUtil; +import eu.siacs.conversations.ui.util.MucDetailsContextMenuHelper; import eu.siacs.conversations.ui.util.MyLinkify; import eu.siacs.conversations.ui.util.SoftKeyboardUtils; import eu.siacs.conversations.utils.EmojiWrapper; @@ -216,6 +218,7 @@ public class ConferenceDetailsActivity extends XmppActivity implements OnConvers return null; } + @Override public void onConversationUpdate() { refreshUi(); @@ -427,129 +430,25 @@ public class ConferenceDetailsActivity extends XmppActivity implements OnConvers name = user.getName(); } menu.setHeaderTitle(name); - if (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); - MenuItem removeAdminPrivileges = menu.findItem(R.id.remove_admin_privileges); - MenuItem removeFromRoom = menu.findItem(R.id.remove_from_room); - MenuItem banFromConference = menu.findItem(R.id.ban_from_conference); - MenuItem invite = menu.findItem(R.id.invite); - startConversation.setVisible(true); - if (contact != null && contact.showInRoster()) { - showContactDetails.setVisible(!contact.isSelf()); - } - if (user.getRole() == MucOptions.Role.NONE) { - invite.setVisible(true); - } - if (self.getAffiliation().ranks(MucOptions.Affiliation.ADMIN) && - self.getAffiliation().outranks(user.getAffiliation())) { - if (mAdvancedMode) { - if (user.getAffiliation() == MucOptions.Affiliation.NONE) { - giveMembership.setVisible(true); - } else { - removeMembership.setVisible(true); - } - if (!Config.DISABLE_BAN) { - banFromConference.setVisible(true); - } - } else { - if (!Config.DISABLE_BAN || mConversation.getMucOptions().membersOnly()) { - removeFromRoom.setVisible(true); - } - } - if (user.getAffiliation() != MucOptions.Affiliation.ADMIN) { - giveAdminPrivileges.setVisible(true); - } else { - removeAdminPrivileges.setVisible(true); - } - } - } else { - MenuItem sendPrivateMessage = menu.findItem(R.id.send_private_message); - sendPrivateMessage.setVisible(user.getRole().ranks(MucOptions.Role.VISITOR)); - } - + MucDetailsContextMenuHelper.configureMucDetailsContextMenu(menu, mConversation, user, mAdvancedMode); } super.onCreateContextMenu(menu, v, menuInfo); } @Override public boolean onContextItemSelected(MenuItem item) { - Jid jid = mSelectedUser.getRealJid(); - switch (item.getItemId()) { - case R.id.action_contact_details: - Contact contact = mSelectedUser.getContact(); - if (contact != null) { - switchToContactDetails(contact); - } - return true; - case R.id.start_conversation: - startConversation(mSelectedUser); - return true; - case R.id.give_admin_privileges: - xmppConnectionService.changeAffiliationInConference(mConversation, jid, MucOptions.Affiliation.ADMIN, this); - return true; - case R.id.give_membership: - xmppConnectionService.changeAffiliationInConference(mConversation, jid, MucOptions.Affiliation.MEMBER, this); - return true; - case R.id.remove_membership: - xmppConnectionService.changeAffiliationInConference(mConversation, jid, MucOptions.Affiliation.NONE, this); - return true; - case R.id.remove_admin_privileges: - xmppConnectionService.changeAffiliationInConference(mConversation, jid, MucOptions.Affiliation.MEMBER, this); - return true; - case R.id.remove_from_room: - removeFromRoom(mSelectedUser); - return true; - case R.id.ban_from_conference: - xmppConnectionService.changeAffiliationInConference(mConversation, jid, MucOptions.Affiliation.OUTCAST, this); - if (mSelectedUser.getRole() != MucOptions.Role.NONE) { - xmppConnectionService.changeRoleInConference(mConversation, mSelectedUser.getName(), MucOptions.Role.NONE, this); - } - return true; - case R.id.send_private_message: - if (mConversation.getMucOptions().allowPm()) { - privateMsgInMuc(mConversation, mSelectedUser.getName()); - } else { - Toast.makeText(this, R.string.private_messages_are_disabled, Toast.LENGTH_SHORT).show(); - } - return true; - case R.id.invite: - xmppConnectionService.directInvite(mConversation, jid); - return true; - default: - return super.onContextItemSelected(item); + if (!MucDetailsContextMenuHelper.onContextItemSelected(item, mSelectedUser, mConversation, this, this, this)) { + return super.onContextItemSelected(item); } + return true; } private void removeFromRoom(final User user) { - if (mConversation.getMucOptions().membersOnly()) { - xmppConnectionService.changeAffiliationInConference(mConversation, user.getRealJid(), MucOptions.Affiliation.NONE, this); - if (user.getRole() != MucOptions.Role.NONE) { - xmppConnectionService.changeRoleInConference(mConversation, mSelectedUser.getName(), MucOptions.Role.NONE, ConferenceDetailsActivity.this); - } - } else { - AlertDialog.Builder builder = new AlertDialog.Builder(this); - builder.setTitle(R.string.ban_from_conference); - builder.setMessage(getString(R.string.removing_from_public_conference, user.getName())); - builder.setNegativeButton(R.string.cancel, null); - builder.setPositiveButton(R.string.ban_now, (dialog, which) -> { - xmppConnectionService.changeAffiliationInConference(mConversation, user.getRealJid(), MucOptions.Affiliation.OUTCAST, ConferenceDetailsActivity.this); - if (user.getRole() != MucOptions.Role.NONE) { - xmppConnectionService.changeRoleInConference(mConversation, mSelectedUser.getName(), MucOptions.Role.NONE, ConferenceDetailsActivity.this); - } - }); - builder.create().show(); - } + MucDetailsContextMenuHelper.removeFromRoom(user, mConversation, this, this, this); } protected void startConversation(User user) { - if (user.getRealJid() != null) { - Conversation conversation = xmppConnectionService.findOrCreateConversation(this.mConversation.getAccount(), user.getRealJid().asBareJid(), false, true); - switchToConversation(conversation); - } + MucDetailsContextMenuHelper.startConversation(user, this.mConversation, this); } protected void saveAsBookmark() { diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java index 4df20878c..58feaff1b 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationFragment.java @@ -73,6 +73,7 @@ import eu.siacs.conversations.entities.Conversational; import eu.siacs.conversations.entities.DownloadableFile; import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.entities.MucOptions; +import eu.siacs.conversations.entities.MucOptions.User; import eu.siacs.conversations.entities.Presence; import eu.siacs.conversations.entities.ReadByMarker; import eu.siacs.conversations.entities.Transferable; @@ -89,6 +90,7 @@ import eu.siacs.conversations.ui.util.DateSeparator; import eu.siacs.conversations.ui.util.EditMessageActionModeCallback; import eu.siacs.conversations.ui.util.ListViewUtils; import eu.siacs.conversations.ui.util.MenuDoubleTabUtil; +import eu.siacs.conversations.ui.util.MucDetailsContextMenuHelper; import eu.siacs.conversations.ui.util.PendingItem; import eu.siacs.conversations.ui.util.PresenceSelector; import eu.siacs.conversations.ui.util.ScrollState; @@ -978,21 +980,28 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke } activity.switchToAccount(message.getConversation().getAccount(), fingerprint); }); - messageListAdapter.setOnContactPictureLongClicked(message -> { + messageListAdapter.setOnContactPictureLongClicked((v, message) -> { if (message.getStatus() <= Message.STATUS_RECEIVED) { if (message.getConversation().getMode() == Conversation.MODE_MULTI) { - final MucOptions mucOptions = conversation.getMucOptions(); - if (!mucOptions.allowPm()) { - Toast.makeText(getActivity(), R.string.private_messages_are_disabled, Toast.LENGTH_SHORT).show(); - return; - } - Jid user = message.getCounterpart(); - if (user != null && !user.isBareJid()) { - if (mucOptions.isUserInRoom(user)) { - privateMessageWith(user); - } else { - Toast.makeText(getActivity(), activity.getString(R.string.user_has_left_conference, user.getResource()), Toast.LENGTH_SHORT).show(); - } + Jid tcp = message.getTrueCounterpart(); + Jid cp = message.getCounterpart(); + if (cp != null && !cp.isBareJid()) { + User userByRealJid = conversation.getMucOptions().findOrCreateUserByRealJid(tcp); + final User user = userByRealJid != null ? userByRealJid : conversation.getMucOptions().findUserByFullJid(cp); + final PopupMenu popupMenu = new PopupMenu(getActivity(), v); + popupMenu.inflate(R.menu.muc_details_context); + final Menu menu = popupMenu.getMenu(); + final boolean advancedMode = activity.getPreferences().getBoolean("advanced_muc_mode", false); + MucDetailsContextMenuHelper.configureMucDetailsContextMenu(menu, conversation, user, advancedMode); + 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(); } } } else { diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationsActivity.java b/src/main/java/eu/siacs/conversations/ui/ConversationsActivity.java index 70d682564..0f8ec1123 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationsActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationsActivity.java @@ -78,10 +78,11 @@ import eu.siacs.conversations.utils.EmojiWrapper; import eu.siacs.conversations.utils.ExceptionHelper; import eu.siacs.conversations.utils.XmppUri; import eu.siacs.conversations.xmpp.OnUpdateBlocklist; +import rocks.xmpp.addr.Jid; import static eu.siacs.conversations.ui.ConversationFragment.REQUEST_DECRYPT_PGP; -public class ConversationsActivity extends XmppActivity implements OnConversationSelected, OnConversationArchived, OnConversationsListItemUpdated, OnConversationRead, XmppConnectionService.OnAccountUpdate, XmppConnectionService.OnConversationUpdate, XmppConnectionService.OnRosterUpdate, OnUpdateBlocklist, XmppConnectionService.OnShowErrorToast { +public class ConversationsActivity extends XmppActivity implements OnConversationSelected, OnConversationArchived, OnConversationsListItemUpdated, OnConversationRead, XmppConnectionService.OnAccountUpdate, XmppConnectionService.OnConversationUpdate, XmppConnectionService.OnRosterUpdate, OnUpdateBlocklist, XmppConnectionService.OnShowErrorToast, XmppConnectionService.OnAffiliationChanged, XmppConnectionService.OnRoleChanged { public static final String ACTION_VIEW_CONVERSATION = "eu.siacs.conversations.action.VIEW"; public static final String EXTRA_CONVERSATION = "conversationUuid"; @@ -415,6 +416,30 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio openConversation(conversation, null); } + private void displayToast(final String msg) { + runOnUiThread(() -> Toast.makeText(ConversationsActivity.this, msg, Toast.LENGTH_SHORT).show()); + } + + @Override + public void onAffiliationChangedSuccessful(Jid jid) { + + } + + @Override + public void onAffiliationChangeFailed(Jid jid, int resId) { + displayToast(getString(resId, jid.asBareJid().toString())); + } + + @Override + public void onRoleChangedSuccessful(String nick) { + + } + + @Override + public void onRoleChangeFailed(String nick, int resId) { + displayToast(getString(resId, nick)); + } + private void openConversation(Conversation conversation, Bundle extras) { ConversationFragment conversationFragment = (ConversationFragment) getFragmentManager().findFragmentById(R.id.secondary_fragment); final boolean mainNeedsRefresh; diff --git a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java index fcd54c4cb..c86717a32 100644 --- a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java +++ b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java @@ -730,7 +730,7 @@ public class MessageAdapter extends ArrayAdapter implements CopyTextVie viewHolder.contact_picture.setOnLongClickListener(v -> { if (MessageAdapter.this.mOnContactPictureLongClickedListener != null) { MessageAdapter.this.mOnContactPictureLongClickedListener - .onContactPictureLongClicked(message); + .onContactPictureLongClicked(v, message); return true; } else { return false; @@ -975,7 +975,7 @@ public class MessageAdapter extends ArrayAdapter implements CopyTextVie } public interface OnContactPictureLongClicked { - void onContactPictureLongClicked(Message message); + void onContactPictureLongClicked(View v, Message message); } private static class ViewHolder { diff --git a/src/main/java/eu/siacs/conversations/ui/util/MucDetailsContextMenuHelper.java b/src/main/java/eu/siacs/conversations/ui/util/MucDetailsContextMenuHelper.java new file mode 100644 index 000000000..1d8828714 --- /dev/null +++ b/src/main/java/eu/siacs/conversations/ui/util/MucDetailsContextMenuHelper.java @@ -0,0 +1,150 @@ +package eu.siacs.conversations.ui.util; + +import android.support.v7.app.AlertDialog; +import android.view.Menu; +import android.view.MenuItem; +import android.widget.Toast; + +import eu.siacs.conversations.Config; +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.R; +import eu.siacs.conversations.services.XmppConnectionService; +import eu.siacs.conversations.ui.XmppActivity; +import rocks.xmpp.addr.Jid; + + +public final class MucDetailsContextMenuHelper { + public static void configureMucDetailsContextMenu(Menu menu, Conversation conversation, User user, boolean advancedMode) { + if (user != null) { + if (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); + MenuItem removeAdminPrivileges = menu.findItem(R.id.remove_admin_privileges); + MenuItem removeFromRoom = menu.findItem(R.id.remove_from_room); + MenuItem banFromConference = menu.findItem(R.id.ban_from_conference); + MenuItem invite = menu.findItem(R.id.invite); + startConversation.setVisible(true); + final Contact contact = user.getContact(); + final User self = conversation.getMucOptions().getSelf(); + if (contact != null && contact.showInRoster()) { + showContactDetails.setVisible(!contact.isSelf()); + } + if (user.getRole() == MucOptions.Role.NONE) { + invite.setVisible(true); + } + if (self.getAffiliation().ranks(MucOptions.Affiliation.ADMIN) && + self.getAffiliation().outranks(user.getAffiliation())) { + if (advancedMode) { + if (user.getAffiliation() == MucOptions.Affiliation.NONE) { + giveMembership.setVisible(true); + } else { + removeMembership.setVisible(true); + } + if (!Config.DISABLE_BAN) { + banFromConference.setVisible(true); + } + } else { + if (!Config.DISABLE_BAN || conversation.getMucOptions().membersOnly()) { + removeFromRoom.setVisible(true); + } + } + if (user.getAffiliation() != MucOptions.Affiliation.ADMIN) { + giveAdminPrivileges.setVisible(true); + } else { + removeAdminPrivileges.setVisible(true); + } + } + } else { + MenuItem sendPrivateMessage = menu.findItem(R.id.send_private_message); + sendPrivateMessage.setVisible(true); + sendPrivateMessage.setEnabled(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) { + Jid jid = user.getRealJid(); + switch (item.getItemId()) { + case R.id.action_contact_details: + Contact contact = user.getContact(); + if (contact != null) { + activity.switchToContactDetails(contact); + } + return true; + case R.id.start_conversation: + startConversation(user, conversation, activity); + return true; + case R.id.give_admin_privileges: + activity.xmppConnectionService.changeAffiliationInConference(conversation, jid, MucOptions.Affiliation.ADMIN, onAffiliationChanged); + return true; + case R.id.give_membership: + activity.xmppConnectionService.changeAffiliationInConference(conversation, jid, MucOptions.Affiliation.MEMBER, onAffiliationChanged); + return true; + case R.id.remove_membership: + activity.xmppConnectionService.changeAffiliationInConference(conversation, jid, MucOptions.Affiliation.NONE, onAffiliationChanged); + return true; + case R.id.remove_admin_privileges: + activity.xmppConnectionService.changeAffiliationInConference(conversation, jid, MucOptions.Affiliation.MEMBER, onAffiliationChanged); + return true; + case R.id.remove_from_room: + removeFromRoom(user, conversation, activity, onAffiliationChanged, onRoleChanged); + return true; + case R.id.ban_from_conference: + activity.xmppConnectionService.changeAffiliationInConference(conversation, jid, MucOptions.Affiliation.OUTCAST, onAffiliationChanged); + if (user.getRole() != MucOptions.Role.NONE) { + activity.xmppConnectionService.changeRoleInConference(conversation, user.getName(), MucOptions.Role.NONE, onRoleChanged); + } + return true; + case R.id.send_private_message: + if (conversation.getMucOptions().allowPm()) { + activity.privateMsgInMuc(conversation, user.getName()); + } else { + Toast.makeText(activity, R.string.private_messages_are_disabled, Toast.LENGTH_SHORT).show(); + } + return true; + case R.id.invite: + activity.xmppConnectionService.directInvite(conversation, jid); + return true; + default: + return false; + } + } + + public static void removeFromRoom(final User user, Conversation conversation, XmppActivity activity, XmppConnectionService.OnAffiliationChanged onAffiliationChanged, XmppConnectionService.OnRoleChanged onRoleChanged) { + if (conversation.getMucOptions().membersOnly()) { + activity.xmppConnectionService.changeAffiliationInConference(conversation, user.getRealJid(), MucOptions.Affiliation.NONE, onAffiliationChanged); + if (user.getRole() != MucOptions.Role.NONE) { + activity.xmppConnectionService.changeRoleInConference(conversation, user.getName(), MucOptions.Role.NONE, onRoleChanged); + } + } else { + AlertDialog.Builder builder = new AlertDialog.Builder(activity); + builder.setTitle(R.string.ban_from_conference); + builder.setMessage(activity.getString(R.string.removing_from_public_conference, user.getName())); + 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) { + activity.xmppConnectionService.changeRoleInConference(conversation, user.getName(), MucOptions.Role.NONE, onRoleChanged); + } + }); + builder.create().show(); + } + } + + public static void startConversation(User user, Conversation conversation, XmppActivity activity) { + if (user.getRealJid() != null) { + Conversation newConversation = activity.xmppConnectionService.findOrCreateConversation(conversation.getAccount(), user.getRealJid().asBareJid(), false, true); + activity.switchToConversation(newConversation); + } + } +} \ No newline at end of file