aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/services/ContactChooserTargetService.java
blob: 4c0aec3cacc5ade0d3db432edd8a072fc04441d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package de.pixart.messenger.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 de.pixart.messenger.entities.Conversation;
import de.pixart.messenger.ui.ConversationsActivity;

@TargetApi(Build.VERSION_CODES.M)
public class ContactChooserTargetService extends ChooserTargetService implements ServiceConnection {

    private final Object lock = new Object();

    private XmppConnectionService mXmppConnectionService;

    private final int MAX_TARGETS = 5;

    @Override
    public List<ChooserTarget> onGetChooserTargets(ComponentName targetActivityName, IntentFilter matchedFilter) {
        Intent intent = new Intent(this, XmppConnectionService.class);
        intent.setAction("contact_chooser");
        bindService(intent, this, Context.BIND_AUTO_CREATE);
        ArrayList<ChooserTarget> chooserTargets = new ArrayList<>();
        try {
            waitForService();
            final ArrayList<Conversation> conversations = new ArrayList<>();
            if (!mXmppConnectionService.areMessagesInitialized()) {
                return chooserTargets;
            }
            mXmppConnectionService.populateWithOrderedConversations(conversations, false);
            final ComponentName componentName = new ComponentName(this, ConversationsActivity.class);
            final int pixel = (int) (48 * getResources().getDisplayMetrics().density);
            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;
    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        XmppConnectionService.XmppConnectionBinder binder = (XmppConnectionService.XmppConnectionBinder) service;
        mXmppConnectionService = binder.getService();
        synchronized (this.lock) {
            lock.notifyAll();
        }
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        mXmppConnectionService = null;
    }

    private void waitForService() throws InterruptedException {
        if (mXmppConnectionService == null) {
            synchronized (this.lock) {
                lock.wait();
            }
        }
    }
}