2015-12-06 10:55:37 +00:00
|
|
|
package eu.siacs.conversations.services;
|
|
|
|
|
2022-12-11 19:13:09 +00:00
|
|
|
import android.annotation.SuppressLint;
|
2015-12-06 10:55:37 +00:00
|
|
|
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;
|
2022-12-11 19:13:09 +00:00
|
|
|
import android.util.Log;
|
2015-12-06 10:55:37 +00:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2022-12-11 19:13:09 +00:00
|
|
|
import java.util.Collections;
|
2015-12-06 10:55:37 +00:00
|
|
|
import java.util.List;
|
|
|
|
|
2022-12-11 19:13:09 +00:00
|
|
|
import eu.siacs.conversations.Config;
|
2015-12-06 10:55:37 +00:00
|
|
|
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
|
|
|
|
2022-12-11 19:13:09 +00:00
|
|
|
@SuppressLint("Deprecated")
|
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();
|
2022-12-11 19:13:09 +00:00
|
|
|
private static final int MAX_TARGETS = 5;
|
2019-01-11 16:45:18 +00:00
|
|
|
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
|
2022-12-11 19:13:09 +00:00
|
|
|
public List<ChooserTarget> onGetChooserTargets(
|
|
|
|
final ComponentName targetActivityName, final IntentFilter matchedFilter) {
|
2019-01-13 08:42:44 +00:00
|
|
|
if (!EventReceiver.hasEnabledAccounts(this)) {
|
2022-12-11 19:13:09 +00:00
|
|
|
return Collections.emptyList();
|
2019-01-13 08:42:44 +00:00
|
|
|
}
|
|
|
|
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();
|
|
|
|
if (!mXmppConnectionService.areMessagesInitialized()) {
|
2022-12-11 19:13:09 +00:00
|
|
|
return Collections.emptyList();
|
2019-01-11 16:45:18 +00:00
|
|
|
}
|
2022-12-11 19:13:09 +00:00
|
|
|
final ArrayList<Conversation> conversations = new ArrayList<>();
|
|
|
|
mXmppConnectionService.populateWithOrderedConversations(
|
|
|
|
conversations, textOnly(matchedFilter));
|
|
|
|
final ComponentName componentName =
|
|
|
|
new ComponentName(this, ConversationsActivity.class);
|
2019-01-11 16:45:18 +00:00
|
|
|
final int pixel = AvatarService.getSystemUiAvatarSize(this);
|
2022-12-11 19:13:09 +00:00
|
|
|
final ArrayList<ChooserTarget> chooserTargets = new ArrayList<>();
|
|
|
|
for (final Conversation conversation : conversations) {
|
2019-01-11 16:45:18 +00:00
|
|
|
if (conversation.sentMessagesCount() == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
final String name = conversation.getName().toString();
|
2022-12-11 19:13:09 +00:00
|
|
|
final Icon icon =
|
|
|
|
Icon.createWithBitmap(
|
|
|
|
mXmppConnectionService.getAvatarService().get(conversation, pixel));
|
2019-01-11 16:45:18 +00:00
|
|
|
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) {
|
2022-12-11 19:13:09 +00:00
|
|
|
return chooserTargets;
|
2019-01-11 16:45:18 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-11 19:13:09 +00:00
|
|
|
return chooserTargets;
|
|
|
|
} catch (final InterruptedException e) {
|
|
|
|
Log.d(
|
|
|
|
Config.LOGTAG,
|
|
|
|
"Thread got interrupted before binding to XmppConnectionService",
|
|
|
|
e);
|
|
|
|
} finally {
|
|
|
|
unbindService(this);
|
2019-01-11 16:45:18 +00:00
|
|
|
}
|
2022-12-11 19:13:09 +00:00
|
|
|
return Collections.emptyList();
|
2019-01-11 16:45:18 +00:00
|
|
|
}
|
2015-12-06 10:55:37 +00:00
|
|
|
|
2019-01-11 16:45:18 +00:00
|
|
|
@Override
|
2022-12-11 19:13:09 +00:00
|
|
|
public void onServiceConnected(final ComponentName name, final IBinder service) {
|
|
|
|
XmppConnectionService.XmppConnectionBinder binder =
|
|
|
|
(XmppConnectionService.XmppConnectionBinder) service;
|
2019-01-11 16:45:18 +00:00
|
|
|
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
|
|
|
}
|