From 3820950408fc34f786ff125f8ed11d07da5c2a7d Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sat, 21 Sep 2019 10:51:05 +0200 Subject: [PATCH] show context menu in channel search to share uri --- .../ui/ChannelDiscoveryActivity.java | 18 +++++++--- .../ui/StartConversationActivity.java | 10 ++++-- .../adapter/ChannelSearchResultAdapter.java | 36 ++++++++++++++----- src/main/res/menu/channel_item_context.xml | 6 ++++ 4 files changed, 55 insertions(+), 15 deletions(-) create mode 100644 src/main/res/menu/channel_item_context.xml diff --git a/src/main/java/eu/siacs/conversations/ui/ChannelDiscoveryActivity.java b/src/main/java/eu/siacs/conversations/ui/ChannelDiscoveryActivity.java index ab5b66b10..af96f700a 100644 --- a/src/main/java/eu/siacs/conversations/ui/ChannelDiscoveryActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ChannelDiscoveryActivity.java @@ -37,11 +37,8 @@ public class ChannelDiscoveryActivity extends XmppActivity implements MenuItem.O private static final String CHANNEL_DISCOVERY_OPT_IN = "channel_discovery_opt_in"; private final ChannelSearchResultAdapter adapter = new ChannelSearchResultAdapter(); - - private ActivityChannelDiscoveryBinding binding; - private final PendingItem mInitialSearchValue = new PendingItem<>(); - + private ActivityChannelDiscoveryBinding binding; private MenuItem mMenuSearchView; private EditText mSearchEditText; @@ -198,6 +195,19 @@ public class ChannelDiscoveryActivity extends XmppActivity implements MenuItem.O } + @Override + public boolean onContextItemSelected(MenuItem item) { + final MuclumbusService.Room room = adapter.getCurrent(); + if (room != null) { + switch (item.getItemId()) { + case R.id.share_with: + StartConversationActivity.shareAsChannel(this, room.address); + return true; + } + } + return false; + } + public void joinChannelSearchResult(String accountJid, MuclumbusService.Room result) { final boolean syncAutojoin = getBooleanPreference("autojoin", R.bool.autojoin); Account account = xmppConnectionService.findAccountByJid(Jid.of(accountJid)); diff --git a/src/main/java/eu/siacs/conversations/ui/StartConversationActivity.java b/src/main/java/eu/siacs/conversations/ui/StartConversationActivity.java index 4de40206c..d1834e62a 100644 --- a/src/main/java/eu/siacs/conversations/ui/StartConversationActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/StartConversationActivity.java @@ -405,14 +405,18 @@ public class StartConversationActivity extends XmppActivity implements XmppConne protected void shareBookmarkUri(int position) { Bookmark bookmark = (Bookmark) conferences.get(position); + shareAsChannel(this, bookmark.getJid().asBareJid().toEscapedString()); + } + + public static void shareAsChannel(final Context context, final String address) { Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); - shareIntent.putExtra(Intent.EXTRA_TEXT, "xmpp:" + bookmark.getJid().asBareJid().toEscapedString() + "?join"); + shareIntent.putExtra(Intent.EXTRA_TEXT, "xmpp:" + address + "?join"); shareIntent.setType("text/plain"); try { - startActivity(Intent.createChooser(shareIntent, getText(R.string.share_uri_with))); + context.startActivity(Intent.createChooser(shareIntent, context.getText(R.string.share_uri_with))); } catch (ActivityNotFoundException e) { - Toast.makeText(this, R.string.no_application_to_share_uri, Toast.LENGTH_SHORT).show(); + Toast.makeText(context, R.string.no_application_to_share_uri, Toast.LENGTH_SHORT).show(); } } diff --git a/src/main/java/eu/siacs/conversations/ui/adapter/ChannelSearchResultAdapter.java b/src/main/java/eu/siacs/conversations/ui/adapter/ChannelSearchResultAdapter.java index d39910cfd..5ba28c446 100644 --- a/src/main/java/eu/siacs/conversations/ui/adapter/ChannelSearchResultAdapter.java +++ b/src/main/java/eu/siacs/conversations/ui/adapter/ChannelSearchResultAdapter.java @@ -1,11 +1,13 @@ package eu.siacs.conversations.ui.adapter; +import android.app.Activity; import android.databinding.DataBindingUtil; import android.support.annotation.NonNull; import android.support.v7.recyclerview.extensions.ListAdapter; import android.support.v7.util.DiffUtil; import android.support.v7.widget.RecyclerView; import android.text.TextUtils; +import android.view.ContextMenu; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -15,12 +17,11 @@ import java.util.Locale; import eu.siacs.conversations.R; import eu.siacs.conversations.databinding.SearchResultItemBinding; import eu.siacs.conversations.http.services.MuclumbusService; +import eu.siacs.conversations.ui.XmppActivity; import eu.siacs.conversations.ui.util.AvatarWorkerTask; import rocks.xmpp.addr.Jid; -public class ChannelSearchResultAdapter extends ListAdapter { - - private OnChannelSearchResultSelected listener; +public class ChannelSearchResultAdapter extends ListAdapter implements View.OnCreateContextMenuListener { private static final DiffUtil.ItemCallback DIFF = new DiffUtil.ItemCallback() { @Override @@ -33,6 +34,8 @@ public class ChannelSearchResultAdapter extends ListAdapter listener.onChannelSearchResult(searchResult)); + final View root = viewHolder.binding.getRoot(); + root.setTag(searchResult); + root.setOnClickListener(v -> listener.onChannelSearchResult(searchResult)); + root.setOnCreateContextMenuListener(this); } public void setOnChannelSearchResultSelectedListener(OnChannelSearchResultSelected listener) { this.listener = listener; } + public MuclumbusService.Room getCurrent() { + return this.current; + } + + @Override + public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { + final Activity activity = XmppActivity.find(v); + final Object tag = v.getTag(); + if (activity != null && tag instanceof MuclumbusService.Room) { + activity.getMenuInflater().inflate(R.menu.channel_item_context, menu); + this.current = (MuclumbusService.Room) tag; + } + } + + + public interface OnChannelSearchResultSelected { + void onChannelSearchResult(MuclumbusService.Room result); + } public static class ViewHolder extends RecyclerView.ViewHolder { @@ -82,8 +106,4 @@ public class ChannelSearchResultAdapter extends ListAdapter + + + \ No newline at end of file