From 2093aa76ada6f7dc3724973c63f91710b22ee7d3 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sun, 11 Dec 2022 20:13:09 +0100 Subject: [PATCH] code clean up in ContactChooserTargetService --- .../services/ContactChooserTargetService.java | 50 ++++++++++++------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/services/ContactChooserTargetService.java b/src/main/java/eu/siacs/conversations/services/ContactChooserTargetService.java index 96097d0b3..c68b502af 100644 --- a/src/main/java/eu/siacs/conversations/services/ContactChooserTargetService.java +++ b/src/main/java/eu/siacs/conversations/services/ContactChooserTargetService.java @@ -1,5 +1,6 @@ package eu.siacs.conversations.services; +import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.content.ComponentName; import android.content.Context; @@ -12,19 +13,23 @@ import android.os.Bundle; import android.os.IBinder; import android.service.chooser.ChooserTarget; import android.service.chooser.ChooserTargetService; +import android.util.Log; import java.util.ArrayList; +import java.util.Collections; import java.util.List; +import eu.siacs.conversations.Config; import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.ui.ConversationsActivity; import eu.siacs.conversations.utils.Compatibility; +@SuppressLint("Deprecated") @TargetApi(Build.VERSION_CODES.M) public class ContactChooserTargetService extends ChooserTargetService implements ServiceConnection { private final Object lock = new Object(); - private final int MAX_TARGETS = 5; + private static final int MAX_TARGETS = 5; private XmppConnectionService mXmppConnectionService; private static boolean textOnly(IntentFilter filter) { @@ -37,10 +42,10 @@ public class ContactChooserTargetService extends ChooserTargetService implements } @Override - public List onGetChooserTargets(ComponentName targetActivityName, IntentFilter matchedFilter) { - final ArrayList chooserTargets = new ArrayList<>(); + public List onGetChooserTargets( + final ComponentName targetActivityName, final IntentFilter matchedFilter) { if (!EventReceiver.hasEnabledAccounts(this)) { - return chooserTargets; + return Collections.emptyList(); } final Intent intent = new Intent(this, XmppConnectionService.class); intent.setAction("contact_chooser"); @@ -48,37 +53,48 @@ public class ContactChooserTargetService extends ChooserTargetService implements bindService(intent, this, Context.BIND_AUTO_CREATE); try { waitForService(); - final ArrayList conversations = new ArrayList<>(); if (!mXmppConnectionService.areMessagesInitialized()) { - return chooserTargets; + return Collections.emptyList(); } - - mXmppConnectionService.populateWithOrderedConversations(conversations, textOnly(matchedFilter)); - final ComponentName componentName = new ComponentName(this, ConversationsActivity.class); + final ArrayList conversations = new ArrayList<>(); + mXmppConnectionService.populateWithOrderedConversations( + conversations, textOnly(matchedFilter)); + final ComponentName componentName = + new ComponentName(this, ConversationsActivity.class); final int pixel = AvatarService.getSystemUiAvatarSize(this); - for (Conversation conversation : conversations) { + final ArrayList chooserTargets = new ArrayList<>(); + for (final 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 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; + return chooserTargets; } } - } catch (InterruptedException e) { + return chooserTargets; + } catch (final InterruptedException e) { + Log.d( + Config.LOGTAG, + "Thread got interrupted before binding to XmppConnectionService", + e); + } finally { + unbindService(this); } - unbindService(this); - return chooserTargets; + return Collections.emptyList(); } @Override - public void onServiceConnected(ComponentName name, IBinder service) { - XmppConnectionService.XmppConnectionBinder binder = (XmppConnectionService.XmppConnectionBinder) service; + public void onServiceConnected(final ComponentName name, final IBinder service) { + XmppConnectionService.XmppConnectionBinder binder = + (XmppConnectionService.XmppConnectionBinder) service; mXmppConnectionService = binder.getService(); synchronized (this.lock) { lock.notifyAll();