summaryrefslogtreecommitdiffstats
path: root/src-ambient/com/android/phone/common/incall/ContactsDataSubscription.java
blob: e93a72ce8eb1803487ef8465160390904e9f671c (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * Copyright (C)  2016 The CyanogenMod Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.phone.common.incall;

import android.content.ComponentName;
import android.content.Context;
import android.util.Log;

import com.android.phone.common.ambient.SingletonHolder;
import com.android.phone.common.ambient.TypedPendingResult;
import com.android.phone.common.incall.api.InCallListeners;
import com.android.phone.common.incall.api.InCallQueries;
import com.android.phone.common.incall.api.InCallResults;
import com.android.phone.common.nudge.api.NudgeQueries;
import com.cyanogen.ambient.common.api.PendingResult;
import com.cyanogen.ambient.common.api.Result;
import com.cyanogen.ambient.common.api.ResultCallback;
import com.cyanogen.ambient.discovery.util.NudgeKey;
import com.cyanogen.ambient.incall.extension.InCallContactInfo;
import com.cyanogen.ambient.incall.results.PendingIntentResult;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

public class ContactsDataSubscription extends DialerDataSubscription {

    public ContactsDataSubscription(Context context) {
        super(context);
    }

    private static final SingletonHolder<ContactsDataSubscription, Context> sInstance =
            new SingletonHolder<ContactsDataSubscription, Context>() {

                @Override
                protected ContactsDataSubscription create(Context context) {
                    // Let's get started here.
                    return new ContactsDataSubscription(context);
                }
            };


    public static ContactsDataSubscription get(Context context) {
        return sInstance.get(context);
    }

    public static boolean isCreated() {
        return sInstance.isCreated();
    }

    public static void init(Context context) {
        ContactsDataSubscription.get(context).refresh();
    }

    @Override
    public void onDynamicRefreshRequested(ArrayList<TypedPendingResult> queries,
            ComponentName componentName) {
        queries.add(InCallQueries.getCallMethodAuthenticated(mClient, componentName));
        queries.add(InCallQueries.getCallMethodAccountHandle(mClient, componentName));
    }

    @Override
    protected void enableListeners(CallMethodInfo mod) {
        InCallListeners.enableAuthListener(this, mod);
    }

    @Override
    protected void disableListeners(CallMethodInfo mod) {
        InCallListeners.disableAuthListener(this, mod);
    }

    @Override
    protected void requestedModInfo(ArrayList<TypedPendingResult> queries,
            ComponentName componentName) {

        queries.add(InCallQueries.getCallMethodInfo(mClient, componentName));
        queries.add(InCallQueries.getCallMethodStatus(mClient, componentName));
        queries.add(InCallQueries.getCallMethodMimeType(mClient, componentName));
        queries.add(InCallQueries.getCallMethodVideoCallableMimeType(mClient, componentName));
        queries.add(InCallQueries.getCallMethodAuthenticated(mClient, componentName));
        queries.add(InCallQueries.getLoginIntent(mClient, componentName));
        queries.add(InCallQueries.getDefaultDirectorySearchIntent(mClient, componentName));
        queries.add(InCallQueries.getCallMethodImMimeType(mClient, componentName));

        TypedPendingResult fragLogin = NudgeQueries.getNudgeConfig(mClient, mContext, componentName,
                NudgeKey.INCALL_CONTACT_FRAGMENT_LOGIN);
        if (fragLogin != null) {
            queries.add(fragLogin);
        }
        TypedPendingResult cardLogin = NudgeQueries.getNudgeConfig(mClient, mContext, componentName,
                NudgeKey.INCALL_CONTACT_CARD_LOGIN);
        if (cardLogin != null) {
            queries.add(cardLogin);
        }
        TypedPendingResult cardDownload = NudgeQueries.getNudgeConfig(mClient, mContext,
                componentName, NudgeKey.INCALL_CONTACT_CARD_DOWNLOAD);
        if (cardDownload != null) {
            queries.add(cardDownload);
        }
    }

    public Set<String> getAllPluginComponentNames() {
        Set<String> names = new HashSet<String>();
        HashMap<ComponentName, CallMethodInfo> plugins = this.getPluginInfo();
        for (ComponentName cn : plugins.keySet()) {
            names.add(cn.flattenToString());
        }
        return names;
    }

    public void updatePendingIntents(final HashMap<ComponentName, ContactsPendingIntents> map,
                                     InCallContactInfo contactInfo) {
        HashMap<ComponentName, CallMethodInfo> plugins = this.getPluginInfo();
        for (ComponentName cn : plugins.keySet()) {
            ArrayList<TypedPendingResult> queries = new ArrayList<>();
            queries.add(InCallQueries.getDirectorySearchIntent(mClient, cn, contactInfo));
            queries.add(InCallQueries.getInviteIntent(mClient, cn, contactInfo));
            executeAllPendingIntentQueries(queries, cn, map);
        }
    }

    private void executeAllPendingIntentQueries(final ArrayList<TypedPendingResult> apiCallbacks,
            final ComponentName componentName,
            final HashMap<ComponentName, ContactsPendingIntents> map) {
        final ArrayList<PendingResult> pendingResults = new ArrayList<>();

        for (final TypedPendingResult pendingResult : apiCallbacks) {
            pendingResult.setResultCallback(new ResultCallback() {
                @Override
                public void onResult(Result result) {
                    pendingResults.remove(pendingResult.getPendingResult());
                    if (result == null) {
                        // Our plugin failed to make this call, log out what and why
                        Log.e(TAG, "Result from: " + componentName.getPackageName() + " failed");
                    } else {
                        if (result.getStatus().isSuccess()) {
                            // check to see if our onPostResult removed the plugin.
                            if (!getPluginInfo().containsKey(componentName)) {
                                // Our plugin no longer exists, clear other callbacks
                                for (PendingResult pr : pendingResults) {
                                    pr.cancel();
                                }
                                pendingResults.clear();
                            } else {
                                ContactsPendingIntents intentInfo = map.get(componentName);
                                if (intentInfo == null) {
                                    intentInfo = new ContactsPendingIntents();
                                    map.put(componentName, intentInfo);
                                }
                                InCallResults.gotContactsIntent(intentInfo,
                                        (PendingIntentResult) result,
                                        pendingResult
                                        .getType());
                            }
                        } else if (result.getStatus().isCanceled()) {
                            // Our plugin was found invalid or no longer exists.
                            Log.e(TAG, "Queries to: " + componentName.getPackageName()
                                    + " are cancelled");
                        } else {
                            Log.e(TAG, "Query to: " + componentName.getPackageName() + " "
                                    + result.getClass().getSimpleName() + " failed. Code: " +
                                    result.getStatus().getStatusMessage());
                        }
                    }
                }
            });

            // Add items that we've already added to our queue for so we can cancel them if
            // need be. Set resultcallback will return instantly but onResult will not.
            // So we can use this as our "counter" to determine if we should broadcast
            pendingResults.add(pendingResult.getPendingResult());
        }
    }
}