code clean up in ContactChooserTargetService

This commit is contained in:
Daniel Gultsch 2022-12-11 20:13:09 +01:00
parent f851761252
commit 2093aa76ad

View file

@ -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<ChooserTarget> onGetChooserTargets(ComponentName targetActivityName, IntentFilter matchedFilter) {
final ArrayList<ChooserTarget> chooserTargets = new ArrayList<>();
public List<ChooserTarget> 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<Conversation> 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<Conversation> 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<ChooserTarget> 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;
}
}
} catch (InterruptedException e) {
}
unbindService(this);
return chooserTargets;
}
}
return chooserTargets;
} catch (final InterruptedException e) {
Log.d(
Config.LOGTAG,
"Thread got interrupted before binding to XmppConnectionService",
e);
} finally {
unbindService(this);
}
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();