2015-12-06 10:55:37 +00:00
|
|
|
package eu.siacs.conversations.services;
|
|
|
|
|
|
|
|
import android.annotation.TargetApi;
|
|
|
|
import android.content.ComponentName;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.IntentFilter;
|
|
|
|
import android.content.ServiceConnection;
|
|
|
|
import android.graphics.drawable.Icon;
|
|
|
|
import android.os.Build;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.IBinder;
|
|
|
|
import android.service.chooser.ChooserTarget;
|
|
|
|
import android.service.chooser.ChooserTargetService;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import eu.siacs.conversations.entities.Conversation;
|
2018-09-12 15:21:32 +00:00
|
|
|
import eu.siacs.conversations.ui.ConversationsActivity;
|
2019-01-13 08:42:44 +00:00
|
|
|
import eu.siacs.conversations.utils.Compatibility;
|
2015-12-06 10:55:37 +00:00
|
|
|
|
|
|
|
@TargetApi(Build.VERSION_CODES.M)
|
|
|
|
public class ContactChooserTargetService extends ChooserTargetService implements ServiceConnection {
|
|
|
|
|
2019-01-11 16:45:18 +00:00
|
|
|
private final Object lock = new Object();
|
|
|
|
private final int MAX_TARGETS = 5;
|
|
|
|
private XmppConnectionService mXmppConnectionService;
|
2015-12-06 10:55:37 +00:00
|
|
|
|
2019-01-11 16:45:18 +00:00
|
|
|
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;
|
|
|
|
}
|
2015-12-06 10:55:37 +00:00
|
|
|
|
2019-01-11 16:45:18 +00:00
|
|
|
@Override
|
|
|
|
public List<ChooserTarget> onGetChooserTargets(ComponentName targetActivityName, IntentFilter matchedFilter) {
|
2019-01-13 08:42:44 +00:00
|
|
|
final ArrayList<ChooserTarget> chooserTargets = new ArrayList<>();
|
|
|
|
if (!EventReceiver.hasEnabledAccounts(this)) {
|
|
|
|
return chooserTargets;
|
|
|
|
}
|
|
|
|
final Intent intent = new Intent(this, XmppConnectionService.class);
|
2019-01-11 16:45:18 +00:00
|
|
|
intent.setAction("contact_chooser");
|
2019-01-13 08:42:44 +00:00
|
|
|
Compatibility.startService(this, intent);
|
2019-01-11 16:45:18 +00:00
|
|
|
bindService(intent, this, Context.BIND_AUTO_CREATE);
|
|
|
|
try {
|
|
|
|
waitForService();
|
|
|
|
final ArrayList<Conversation> 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;
|
|
|
|
}
|
2015-12-06 10:55:37 +00:00
|
|
|
|
2019-01-11 16:45:18 +00:00
|
|
|
@Override
|
|
|
|
public void onServiceConnected(ComponentName name, IBinder service) {
|
|
|
|
XmppConnectionService.XmppConnectionBinder binder = (XmppConnectionService.XmppConnectionBinder) service;
|
|
|
|
mXmppConnectionService = binder.getService();
|
|
|
|
synchronized (this.lock) {
|
|
|
|
lock.notifyAll();
|
|
|
|
}
|
|
|
|
}
|
2015-12-06 10:55:37 +00:00
|
|
|
|
2019-01-11 16:45:18 +00:00
|
|
|
@Override
|
|
|
|
public void onServiceDisconnected(ComponentName name) {
|
|
|
|
mXmppConnectionService = null;
|
|
|
|
}
|
2015-12-06 10:55:37 +00:00
|
|
|
|
2019-01-11 16:45:18 +00:00
|
|
|
private void waitForService() throws InterruptedException {
|
|
|
|
if (mXmppConnectionService == null) {
|
|
|
|
synchronized (this.lock) {
|
|
|
|
lock.wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-06 10:55:37 +00:00
|
|
|
}
|