From 6ecf6f1149f466ad70b3a6db55e6d4e406f3ed4d Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Fri, 11 Jan 2019 17:45:18 +0100 Subject: [PATCH] make contact chooser (direct sharing) smart about sharing text in groups when http is not available --- src/main/AndroidManifest.xml | 10 ++ .../services/ContactChooserTargetService.java | 124 +++++++++--------- 2 files changed, 75 insertions(+), 59 deletions(-) diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index 4f854b5a9..d797715ef 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -190,6 +190,16 @@ android:name=".ui.ShareWithActivity" android:label="@string/app_name" android:launchMode="singleTop"> + + + + + + + + + + diff --git a/src/main/java/eu/siacs/conversations/services/ContactChooserTargetService.java b/src/main/java/eu/siacs/conversations/services/ContactChooserTargetService.java index d207a8566..68ab48271 100644 --- a/src/main/java/eu/siacs/conversations/services/ContactChooserTargetService.java +++ b/src/main/java/eu/siacs/conversations/services/ContactChooserTargetService.java @@ -12,78 +12,84 @@ import android.os.Bundle; import android.os.IBinder; import android.service.chooser.ChooserTarget; import android.service.chooser.ChooserTargetService; -import android.support.v4.content.ContextCompat; import java.util.ArrayList; import java.util.List; import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.ui.ConversationsActivity; -import eu.siacs.conversations.ui.ShareWithActivity; @TargetApi(Build.VERSION_CODES.M) public class ContactChooserTargetService extends ChooserTargetService implements ServiceConnection { - private final Object lock = new Object(); + private final Object lock = new Object(); + private final int MAX_TARGETS = 5; + private XmppConnectionService mXmppConnectionService; - private XmppConnectionService mXmppConnectionService; + private static boolean textOnly(IntentFilter filter) { + for (int i = 0; i < filter.countDataTypes(); ++i) { + if (!"text/plain".equals(filter.getDataType(i))) { + return false; + } + } + return true; + } - private final int MAX_TARGETS = 5; + @Override + public List onGetChooserTargets(ComponentName targetActivityName, IntentFilter matchedFilter) { + Intent intent = new Intent(this, XmppConnectionService.class); + intent.setAction("contact_chooser"); + bindService(intent, this, Context.BIND_AUTO_CREATE); + ArrayList chooserTargets = new ArrayList<>(); + try { + waitForService(); + final ArrayList conversations = new ArrayList<>(); + if (!mXmppConnectionService.areMessagesInitialized()) { + return chooserTargets; + } + + mXmppConnectionService.populateWithOrderedConversations(conversations, textOnly(matchedFilter)); + final ComponentName componentName = new ComponentName(this, ConversationsActivity.class); + final int pixel = AvatarService.getSystemUiAvatarSize(this); + for (Conversation conversation : conversations) { + if (conversation.sentMessagesCount() == 0) { + continue; + } + final String name = conversation.getName().toString(); + final Icon icon = Icon.createWithBitmap(mXmppConnectionService.getAvatarService().get(conversation, pixel)); + final float score = 1 - (1.0f / MAX_TARGETS) * chooserTargets.size(); + final Bundle extras = new Bundle(); + extras.putString(ConversationsActivity.EXTRA_CONVERSATION, conversation.getUuid()); + chooserTargets.add(new ChooserTarget(name, icon, score, componentName, extras)); + if (chooserTargets.size() >= MAX_TARGETS) { + break; + } + } + } catch (InterruptedException e) { + } + unbindService(this); + return chooserTargets; + } - @Override - public List onGetChooserTargets(ComponentName targetActivityName, IntentFilter matchedFilter) { - Intent intent = new Intent(this, XmppConnectionService.class); - intent.setAction("contact_chooser"); - bindService(intent, this, Context.BIND_AUTO_CREATE); - ArrayList chooserTargets = new ArrayList<>(); - try { - waitForService(); - final ArrayList conversations = new ArrayList<>(); - if (!mXmppConnectionService.areMessagesInitialized()) { - return chooserTargets; - } - mXmppConnectionService.populateWithOrderedConversations(conversations, false); - final ComponentName componentName = new ComponentName(this, ConversationsActivity.class); - final int pixel = AvatarService.getSystemUiAvatarSize(this); - for(Conversation conversation : conversations) { - if (conversation.sentMessagesCount() == 0) { - continue; - } - final String name = conversation.getName().toString(); - final Icon icon = Icon.createWithBitmap(mXmppConnectionService.getAvatarService().get(conversation, pixel)); - final float score = 1 - (1.0f / MAX_TARGETS) * chooserTargets.size(); - final Bundle extras = new Bundle(); - extras.putString(ConversationsActivity.EXTRA_CONVERSATION, conversation.getUuid()); - chooserTargets.add(new ChooserTarget(name, icon, score, componentName, extras)); - if (chooserTargets.size() >= MAX_TARGETS) { - break; - } - } - } catch (InterruptedException e) { - } - unbindService(this); - return chooserTargets; - } + @Override + public void onServiceConnected(ComponentName name, IBinder service) { + XmppConnectionService.XmppConnectionBinder binder = (XmppConnectionService.XmppConnectionBinder) service; + mXmppConnectionService = binder.getService(); + synchronized (this.lock) { + lock.notifyAll(); + } + } - @Override - public void onServiceConnected(ComponentName name, IBinder service) { - XmppConnectionService.XmppConnectionBinder binder = (XmppConnectionService.XmppConnectionBinder) service; - mXmppConnectionService = binder.getService(); - synchronized (this.lock) { - lock.notifyAll(); - } - } + @Override + public void onServiceDisconnected(ComponentName name) { + mXmppConnectionService = null; + } - @Override - public void onServiceDisconnected(ComponentName name) { - mXmppConnectionService = null; - } - - private void waitForService() throws InterruptedException { - if (mXmppConnectionService == null) { - synchronized (this.lock) { - lock.wait(); - } - } - } + private void waitForService() throws InterruptedException { + if (mXmppConnectionService == null) { + synchronized (this.lock) { + lock.wait(); + } + } + } }