code clean up in ContactChooserTargetService
This commit is contained in:
parent
f851761252
commit
2093aa76ad
|
@ -1,5 +1,6 @@
|
||||||
package eu.siacs.conversations.services;
|
package eu.siacs.conversations.services;
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint;
|
||||||
import android.annotation.TargetApi;
|
import android.annotation.TargetApi;
|
||||||
import android.content.ComponentName;
|
import android.content.ComponentName;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
@ -12,19 +13,23 @@ import android.os.Bundle;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import android.service.chooser.ChooserTarget;
|
import android.service.chooser.ChooserTarget;
|
||||||
import android.service.chooser.ChooserTargetService;
|
import android.service.chooser.ChooserTargetService;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import eu.siacs.conversations.Config;
|
||||||
import eu.siacs.conversations.entities.Conversation;
|
import eu.siacs.conversations.entities.Conversation;
|
||||||
import eu.siacs.conversations.ui.ConversationsActivity;
|
import eu.siacs.conversations.ui.ConversationsActivity;
|
||||||
import eu.siacs.conversations.utils.Compatibility;
|
import eu.siacs.conversations.utils.Compatibility;
|
||||||
|
|
||||||
|
@SuppressLint("Deprecated")
|
||||||
@TargetApi(Build.VERSION_CODES.M)
|
@TargetApi(Build.VERSION_CODES.M)
|
||||||
public class ContactChooserTargetService extends ChooserTargetService implements ServiceConnection {
|
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 static final int MAX_TARGETS = 5;
|
||||||
private XmppConnectionService mXmppConnectionService;
|
private XmppConnectionService mXmppConnectionService;
|
||||||
|
|
||||||
private static boolean textOnly(IntentFilter filter) {
|
private static boolean textOnly(IntentFilter filter) {
|
||||||
|
@ -37,10 +42,10 @@ public class ContactChooserTargetService extends ChooserTargetService implements
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<ChooserTarget> onGetChooserTargets(ComponentName targetActivityName, IntentFilter matchedFilter) {
|
public List<ChooserTarget> onGetChooserTargets(
|
||||||
final ArrayList<ChooserTarget> chooserTargets = new ArrayList<>();
|
final ComponentName targetActivityName, final IntentFilter matchedFilter) {
|
||||||
if (!EventReceiver.hasEnabledAccounts(this)) {
|
if (!EventReceiver.hasEnabledAccounts(this)) {
|
||||||
return chooserTargets;
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
final Intent intent = new Intent(this, XmppConnectionService.class);
|
final Intent intent = new Intent(this, XmppConnectionService.class);
|
||||||
intent.setAction("contact_chooser");
|
intent.setAction("contact_chooser");
|
||||||
|
@ -48,37 +53,48 @@ public class ContactChooserTargetService extends ChooserTargetService implements
|
||||||
bindService(intent, this, Context.BIND_AUTO_CREATE);
|
bindService(intent, this, Context.BIND_AUTO_CREATE);
|
||||||
try {
|
try {
|
||||||
waitForService();
|
waitForService();
|
||||||
final ArrayList<Conversation> conversations = new ArrayList<>();
|
|
||||||
if (!mXmppConnectionService.areMessagesInitialized()) {
|
if (!mXmppConnectionService.areMessagesInitialized()) {
|
||||||
return chooserTargets;
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
final ArrayList<Conversation> conversations = new ArrayList<>();
|
||||||
mXmppConnectionService.populateWithOrderedConversations(conversations, textOnly(matchedFilter));
|
mXmppConnectionService.populateWithOrderedConversations(
|
||||||
final ComponentName componentName = new ComponentName(this, ConversationsActivity.class);
|
conversations, textOnly(matchedFilter));
|
||||||
|
final ComponentName componentName =
|
||||||
|
new ComponentName(this, ConversationsActivity.class);
|
||||||
final int pixel = AvatarService.getSystemUiAvatarSize(this);
|
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) {
|
if (conversation.sentMessagesCount() == 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
final String name = conversation.getName().toString();
|
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 float score = 1 - (1.0f / MAX_TARGETS) * chooserTargets.size();
|
||||||
final Bundle extras = new Bundle();
|
final Bundle extras = new Bundle();
|
||||||
extras.putString(ConversationsActivity.EXTRA_CONVERSATION, conversation.getUuid());
|
extras.putString(ConversationsActivity.EXTRA_CONVERSATION, conversation.getUuid());
|
||||||
chooserTargets.add(new ChooserTarget(name, icon, score, componentName, extras));
|
chooserTargets.add(new ChooserTarget(name, icon, score, componentName, extras));
|
||||||
if (chooserTargets.size() >= MAX_TARGETS) {
|
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 Collections.emptyList();
|
||||||
return chooserTargets;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onServiceConnected(ComponentName name, IBinder service) {
|
public void onServiceConnected(final ComponentName name, final IBinder service) {
|
||||||
XmppConnectionService.XmppConnectionBinder binder = (XmppConnectionService.XmppConnectionBinder) service;
|
XmppConnectionService.XmppConnectionBinder binder =
|
||||||
|
(XmppConnectionService.XmppConnectionBinder) service;
|
||||||
mXmppConnectionService = binder.getService();
|
mXmppConnectionService = binder.getService();
|
||||||
synchronized (this.lock) {
|
synchronized (this.lock) {
|
||||||
lock.notifyAll();
|
lock.notifyAll();
|
||||||
|
|
Loading…
Reference in a new issue