summaryrefslogtreecommitdiffstats
path: root/src-ambient/incall/CallMethodHelper.java
blob: 5352c285c12fd880271075f8d2cb2db25c8c0314 (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
/*
 * Copyright (C) 2015 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.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.android.phone.common.R;
import com.android.phone.common.ambient.AmbientConnection;
import com.android.phone.common.util.StartInCallCallReceiver;
import com.cyanogen.ambient.analytics.Event;
import com.cyanogen.ambient.common.api.AmbientApiClient;
import com.cyanogen.ambient.common.api.CommonStatusCodes;
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.NudgeServices;
import com.cyanogen.ambient.discovery.results.BundleResult;
import com.cyanogen.ambient.discovery.util.NudgeKey;
import com.cyanogen.ambient.incall.InCallApi;
import com.cyanogen.ambient.incall.InCallServices;
import com.cyanogen.ambient.incall.extension.CreditBalance;
import com.cyanogen.ambient.incall.extension.CreditInfo;
import com.cyanogen.ambient.incall.extension.GetCreditInfoResult;
import com.cyanogen.ambient.incall.extension.HintTextResult;
import com.cyanogen.ambient.incall.extension.IAuthenticationListener;
import com.cyanogen.ambient.incall.extension.ICallCreditListener;
import com.cyanogen.ambient.incall.extension.InCallContactInfo;
import com.cyanogen.ambient.incall.extension.StatusCodes;
import com.cyanogen.ambient.incall.results.AccountHandleResult;
import com.cyanogen.ambient.incall.results.AuthenticationStateResult;
import com.cyanogen.ambient.incall.results.GetCreditInfoResultResult;
import com.cyanogen.ambient.incall.results.HintTextResultResult;
import com.cyanogen.ambient.incall.results.InCallProviderInfoResult;
import com.cyanogen.ambient.incall.results.InstalledPluginsResult;
import com.cyanogen.ambient.incall.results.MimeTypeResult;
import com.cyanogen.ambient.incall.results.PendingIntentResult;
import com.cyanogen.ambient.incall.results.PluginStatusResult;
import com.cyanogen.ambient.incall.util.InCallProviderInfo;
import com.cyanogen.ambient.plugin.PluginStatus;
import com.google.common.base.Joiner;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.cyanogen.ambient.incall.util.InCallHelper.NO_COLOR;

/**
 *  Call Method Helper - In charge of keeping a running and updated hashmap of all InCallProviders
 *  currently installed.
 *
 *  Fragments and Activities can subscribe to changes with subscribe.
 *
 */
public class CallMethodHelper {

    protected static final String TAG = CallMethodHelper.class.getSimpleName();
    protected static final boolean DEBUG = false;

    protected static CallMethodHelper sInstance;
    protected AmbientApiClient mClient;
    protected Context mContext;
    protected InCallApi mInCallApi;
    protected Handler mMainHandler;
    protected static List<ComponentName> mInstalledPlugins;
    protected static HashMap<ComponentName, CallMethodInfo> mCallMethodInfos = new HashMap<>();
    protected static HashMap<ComponentName, ICallCreditListener> mCallCreditListeners = new
            HashMap<>();
    protected static HashMap<ComponentName, IAuthenticationListener> mAuthenticationListeners = new
            HashMap<>();
    protected static HashMap<String, CallMethodReceiver> mRegisteredClients = new HashMap<>();
    protected static boolean dataHasBeenBroadcastPreviously = false;
    // determine which info types to load


    public interface CallMethodReceiver {
        void onChanged(HashMap<ComponentName, CallMethodInfo> callMethodInfos);
    }

    /**
     * Broadcasts mCallMethodInfos to all registered clients on the Main thread.
     */
    protected static void broadcast() {
        getInstance().mMainHandler.post(new Runnable() {
            @Override
            public void run() {
                if (DEBUG) Log.d(TAG, "broadcast");
                for (CallMethodReceiver client : mRegisteredClients.values()) {
                    client.onChanged(mCallMethodInfos);
                }
                enableListeners();
                if (DEBUG) {
                    for (CallMethodInfo cmi : mCallMethodInfos.values()) {
                        Log.v(TAG, "Broadcast: " + cmi.mName);
                    }
                }
                dataHasBeenBroadcastPreviously = true;
            }
        });
    }

    private static void enableListeners() {
        if (DEBUG) Log.d(TAG, "Enabling Listeners");
        for (ComponentName callProviders : mCallMethodInfos.keySet()) {
            if (!mCallCreditListeners.containsKey(callProviders)) {
                CallCreditListenerImpl listener =
                        CallCreditListenerImpl.getInstance(callProviders);
                getInstance().mInCallApi.addCreditListener(getInstance().mClient, callProviders,
                        listener);
                mCallCreditListeners.put(callProviders, listener);
            }
            if (!mAuthenticationListeners.containsKey(callProviders)) {
                AuthenticationListenerImpl listener =
                        AuthenticationListenerImpl.getInstance(callProviders);
                getInstance().mInCallApi.addAuthenticationListener(getInstance().mClient,
                        callProviders, listener);
                mAuthenticationListeners.put(callProviders, listener);
            }
        }
    }

    private static void disableListeners() {
        if (DEBUG) Log.d(TAG, "Disabling Listeners");
        for(ComponentName callCreditProvider : mCallCreditListeners.keySet()) {
            if (mCallCreditListeners.get(callCreditProvider) != null) {
                getInstance().mInCallApi.removeCreditListener(getInstance().mClient,
                        callCreditProvider, mCallCreditListeners.get(callCreditProvider));
            }
        }
        for (ComponentName plugin : mAuthenticationListeners.keySet()) {
            if (mAuthenticationListeners.get(plugin) != null) {
                getInstance().mInCallApi.removeAuthenticationListener(getInstance().mClient,
                        plugin, mAuthenticationListeners.get(plugin));
            }
        }
        mCallCreditListeners.clear();
        mAuthenticationListeners.clear();
    }

    /**
     * Helper method for subscribed clients to remove any item that is not enabled from the hashmap
     * @param input HashMap returned from a broadcast
     * @param output HashMap with only enabled items
     */
    public static void removeDisabled(HashMap<ComponentName, CallMethodInfo> input,
                                      HashMap<ComponentName, CallMethodInfo> output) {
        for (Map.Entry<ComponentName, CallMethodInfo> entry : input.entrySet()) {
            ComponentName key = entry.getKey();
            CallMethodInfo value = entry.getValue();

            if (value.mStatus == PluginStatus.ENABLED) {
                output.put(key, value);
            }
        }
    }

    public static HashMap<ComponentName, CallMethodInfo> getAllEnabledCallMethods() {
        HashMap<ComponentName, CallMethodInfo> cmi = new HashMap<ComponentName, CallMethodInfo>();
        for (Map.Entry<ComponentName, CallMethodInfo> entry : mCallMethodInfos.entrySet()) {
            ComponentName key = entry.getKey();
            CallMethodInfo value = entry.getValue();

            if (value.mStatus == PluginStatus.ENABLED) {
                cmi.put(key, value);
            }
        }
        return cmi;
    }

    public static HashMap<ComponentName, CallMethodInfo> getAllEnabledAndHiddenCallMethods() {
        HashMap<ComponentName, CallMethodInfo> cmi = new HashMap<ComponentName, CallMethodInfo>();
        synchronized (mCallMethodInfos) {
            for (Map.Entry<ComponentName, CallMethodInfo> entry : mCallMethodInfos.entrySet()) {
                ComponentName key = entry.getKey();
                CallMethodInfo value = entry.getValue();

                if (value.mStatus == PluginStatus.ENABLED || value.mStatus == PluginStatus.HIDDEN) {
                    cmi.put(key, value);
                }
            }
        }
        return cmi;
    }

    public static CallMethodInfo getMethodForMimeType(String mimeType, boolean enableOnly) {
        CallMethodInfo targetEntry = null;
        synchronized (mCallMethodInfos) {
            for (CallMethodInfo entry : mCallMethodInfos.values()) {
                // TODO: find out why mimetype may be null
                if (!TextUtils.isEmpty(entry.mMimeType)) {
                    if (enableOnly && entry.mStatus != PluginStatus.ENABLED) {
                        continue;
                    }
                    if (entry.mMimeType.equals(mimeType)) {
                        targetEntry = entry;
                        break;
                    }
                }
            }
        }
        return targetEntry;
    }

    /***
     * Registers the client, on register returns boolean if
     * callMethodInfo data is already collected and the initial broadcast has been sent.
     * @param id unique string for the client
     * @param cmr client receiver
     * @return boolean isempty
     */
    public static synchronized boolean subscribe(String id, CallMethodReceiver cmr) {
        mRegisteredClients.put(id, cmr);

        return dataHasBeenBroadcastPreviously;
    }

    /**
     * Unsubscribes the client. All clients should unsubscribe when they are removed.
     * @param id of the client to remove
     */
    public static synchronized void unsubscribe(String id) {
        mRegisteredClients.remove(id);
        disableListeners();
    }

    /**
     * Get a single instance of our call method helper. There should only be ever one instance.
     * @return
     */
    protected static synchronized CallMethodHelper getInstance() {
        if (sInstance == null) {
            sInstance = new CallMethodHelper();
        }
        return sInstance;
    }

    public interface InCallCallListener {
        void onResult(int resultCode);
    }

    /**
     * Generic CallResultReceiver with basic error handling
     * @param cmi
     * @return
     */
    public static StartInCallCallReceiver getVoIPResultReceiver(final CallMethodInfo cmi,
            final String originCode) {
        return getVoIPResultReceiver(cmi, originCode, null);
    }

    public static StartInCallCallReceiver getVoIPResultReceiver(final CallMethodInfo cmi,
            final String originCode, final InCallCallListener listener) {
        StartInCallCallReceiver svcrr =
                new StartInCallCallReceiver(new Handler(Looper.getMainLooper()));

        svcrr.setReceiver(new StartInCallCallReceiver.Receiver() {

            @Override
            public void onReceiveResult(int resultCode, Bundle resultData) {
                if (DEBUG) Log.i(TAG, "Got Start VoIP Call result callback code = " + resultCode);

                switch (resultCode) {
                    case StatusCodes.StartCall.CALL_FAILURE_INSUFFICIENT_CREDITS:
                    case StatusCodes.StartCall.CALL_FAILURE_INVALID_NUMBER:
                    case StatusCodes.StartCall.CALL_FAILURE_TIMEOUT:
                    case StatusCodes.StartCall.CALL_FAILURE_UNAUTHENTICATED:
                    case StatusCodes.StartCall.CALL_FAILURE:
                        String text = getInstance().mContext.getResources()
                                .getString(R.string.invalid_number_text);
                        text = String.format(text, cmi.mName);
                        Toast.makeText(getInstance().mContext, text, Toast.LENGTH_LONG).show();
                        break;
                    default:
                        Log.i(TAG, "Nothing to do for this Start VoIP Call resultcode = "
                                + resultCode);
                        break;
                }
                if (listener != null) {
                    listener.onResult(resultCode);
                }
            }

        });

        return svcrr;
    }

    /**
     * Start our Helper and kick off our first ModCore queries.
     * @param context
     */
    public static void init(Context context) {
        CallMethodHelper helper = getInstance();
        helper.mContext = context;
        helper.mClient = AmbientConnection.CLIENT.get(context);
        helper.mInCallApi = InCallServices.getInstance();
        helper.mMainHandler = new Handler(context.getMainLooper());
        refresh();
    }

    /**
     * *sip* ahhhh so refreshing
     */
    public static void refresh() {
        updateCallPlugins();
    }

    /**
     * This is helpful for items that don't want to subscribe to updates or for things that
     * need a quick CMI and have a component name.
     * @param cn Component name wanted.
     * @return specific call method when given a component name.
     */
    public static CallMethodInfo getCallMethod(ComponentName cn) {
        CallMethodInfo cmi = null;
        synchronized (mCallMethodInfos) {
            if (mCallMethodInfos.containsKey(cn)) {
                cmi = mCallMethodInfos.get(cn);
            }
        }
        return cmi;
    }

    /**
     * This is useful for items that subscribe after the initial broadcast has been sent out and
     * need to go get some data right away
     * @return the current HashMap of CMIs.
     */
    public static HashMap<ComponentName, CallMethodInfo> getAllCallMethods() {
        // after the initial broadcast on resume we need to go and get some new data
        // this data will broadcast as soon as it becomes available
        return mCallMethodInfos;
    }

    public static void refreshDynamicItems() {
        enableListeners();
        for (ComponentName cn : mCallMethodInfos.keySet()) {
            HashMap<ResultCallback, PendingResult> apiCallbacks = new HashMap<ResultCallback,
                    PendingResult>();
            getCallMethodAuthenticated(cn, apiCallbacks);
            getCreditInfo(cn, apiCallbacks);
            executeAll(apiCallbacks);
        }
    }

    /**
     * A few items need a list of mime types in a comma delimited list. Since we are already
     * querying all the plugins. We can easily build this list ahead of time.
     *
     * Items that require this should subscribe and grab this updated list when needed.
     * @return string of all (not limited to enabled) mime types
     */
    public static String getAllMimeTypes() {
        String mimeTypes = "";

        List<String> mimeTypesList = new ArrayList<>();
        synchronized (mCallMethodInfos) {
            for (CallMethodInfo cmi : mCallMethodInfos.values()) {
                mimeTypesList.add(cmi.mMimeType);
            }
        }

        if (!mimeTypesList.isEmpty()) {
            mimeTypes = Joiner.on(",").skipNulls().join(mimeTypesList);
        }
        return mimeTypes;
    }

    /**
     * A few items need a list of mime types in a comma delimited list. Since we are already
     * querying all the plugins. We can easily build this list ahead of time.
     *
     * Items that require this should subscribe and grab this updated list when needed.
     * @return string of enabled mime types
     */
    public static String getAllEnabledMimeTypes() {
        String mimeTypes = "";

        List<String> enabledMimeTypes = new ArrayList<>();
        synchronized (mCallMethodInfos) {
            for (CallMethodInfo cmi : mCallMethodInfos.values()) {
                if (cmi.mStatus == PluginStatus.ENABLED) {
                    enabledMimeTypes.add(cmi.mMimeType);
                }
            }
        }

        if (!enabledMimeTypes.isEmpty()) {
            mimeTypes = Joiner.on(",").skipNulls().join(enabledMimeTypes);
        }
        return mimeTypes;
    }

    /**
     * A few items need a list of video callable mime types in a comma delimited list.
     * Since we are already querying all the plugins. We can easily build this list ahead of time.
     *
     * Items that require this should subscribe and grab this updated list when needed.
     * @return string of enabled video callable mime types
     */
    public static String getAllEnabledVideoCallableMimeTypes() {
        String mimeTypes = "";

        List<String> enabledMimeTypes = new ArrayList<>();
        synchronized (mCallMethodInfos) {
            for (CallMethodInfo cmi : mCallMethodInfos.values()) {
                if (cmi.mStatus == PluginStatus.ENABLED) {
                    enabledMimeTypes.add(cmi.mVideoCallableMimeType);
                }
            }
        }

        if (!enabledMimeTypes.isEmpty()) {
            mimeTypes = Joiner.on(",").skipNulls().join(enabledMimeTypes);
        }
        return mimeTypes;
    }

    public static String getAllEnabledImMimeTypes() {
        String mimeTypes = "";

        List<String> enabledMimeTypes = new ArrayList<>();
        synchronized (mCallMethodInfos) {
            for (CallMethodInfo cmi : mCallMethodInfos.values()) {
                if (cmi.mStatus == PluginStatus.ENABLED) {
                    enabledMimeTypes.add(cmi.mImMimeType);
                }
            }
        }
        if (!enabledMimeTypes.isEmpty()) {
            mimeTypes = Joiner.on(",").skipNulls().join(enabledMimeTypes);
        }
        return mimeTypes;
    }

    public static void updateCreditInfo(ComponentName name, GetCreditInfoResult gcir) {
        CallMethodInfo cmi = getCallMethodIfExists(name);
        if (cmi != null) {
            if (gcir == null || gcir.creditInfo == null) {
                // Build zero credit dummy if no result found.
                cmi.mProviderCreditInfo =
                        new CreditInfo(new CreditBalance(0, null), null);
            } else {
                cmi.mProviderCreditInfo = gcir.creditInfo;
            }

            // Since a CallMethodInfo object was updated here, we should let the subscribers know
            broadcast();
        }
    }

    public static void updateAuthenticationState(ComponentName name, int state) {
        CallMethodInfo cmi = getCallMethodIfExists(name);
        if (cmi != null) {
            cmi.mIsAuthenticated = state == StatusCodes.AuthenticationState
                    .LOGGED_IN;
            mCallMethodInfos.put(name, cmi);

            // Since a CallMethodInfo object was updated here, we should let the subscribers know
            broadcast();
        }
    }

    /**
     * Broadcast to subscribers once we know we've gathered all our data. Do not do this until we
     * have everything we need for sure.
     *
     * This method is called after every callback from AmbientCore. We will keep track of all of
     * the callbacks, once we have accounted for all callbacks from all plugins, we can go ahead
     * and update subscribers.
     */
    protected static void maybeBroadcastToSubscribers(ResultCallback callback,
            HashMap<ResultCallback, PendingResult> apiCallbacks) {
        if (callback != null) {
            apiCallbacks.remove(callback);
        }
        if (DEBUG) Log.d(TAG, "maybeBroadcastToSubscribers: mInstalledPugins:" + mInstalledPlugins
                .size());
        if (apiCallbacks.isEmpty())  {
            // we are on the last item. broadcast updated hashmap
            broadcast();
        }
    }

    /**
     * In order to speed up the process we make calls for providers that may be invalid
     * To prevent this, make sure every resultcallback uses this before filling in the hashmap.
     * @param cn componentname
     * @return callmethodinfo if valid, otherwise null
     */
    public static CallMethodInfo getCallMethodIfExists(ComponentName cn) {
        if (mCallMethodInfos.containsKey(cn)) {
            return mCallMethodInfos.get(cn);
        } else {
            return null;
        }
    }

    /**
     * Prepare to query and fire off ModCore calls in all directions
     */
    protected static void updateCallPlugins() {
        getInstance().mInCallApi.getInstalledPlugins(getInstance().mClient)
                .setResultCallback(new ResultCallback<InstalledPluginsResult>() {
            @Override
            public void onResult(InstalledPluginsResult installedPluginsResult) {
                if (DEBUG) Log.d(TAG, "+++updateCallPlugins");
                // got installed components
                mInstalledPlugins = installedPluginsResult.components;

                mCallMethodInfos.clear();

                if (mInstalledPlugins == null || mInstalledPlugins.size() == 0) {
                    broadcast();
                    return;
                }

                for (ComponentName cn : mInstalledPlugins) {
                    HashMap<ResultCallback, PendingResult> apiCallbacks =
                            new HashMap<ResultCallback, PendingResult>();
                    mCallMethodInfos.put(cn, new CallMethodInfo());
                    getCallMethodInfo(cn, apiCallbacks);
                    getCallMethodStatus(cn, apiCallbacks);
                    getCallMethodMimeType(cn, apiCallbacks);
                    getCallMethodVideoCallableMimeType(cn, apiCallbacks);
                    getCallMethodAuthenticated(cn, apiCallbacks);
                    getLoginIntent(cn, apiCallbacks);
                    getSettingsIntent(cn, apiCallbacks);
                    getHintText(cn, apiCallbacks);
                    getCreditInfo(cn, apiCallbacks);
                    getManageCreditsIntent(cn, apiCallbacks);
                    checkLowCreditConfig(cn, apiCallbacks);
                    executeAll(apiCallbacks);
                }
            }
        });
    }

    protected static void executeAll(HashMap<ResultCallback, PendingResult> apiCallbacks) {
        for (ResultCallback resultCallback : apiCallbacks.keySet()) {
            PendingResult pendingResult = apiCallbacks.get(resultCallback);
            pendingResult.setResultCallback(resultCallback);
        }
    }

    /**
     * Get the plugin info
     * @param cn
     * @param apiCallbacks keeps track of the target callbacks before broadcast
     */
    protected static void getCallMethodInfo(final ComponentName cn, final
            HashMap<ResultCallback, PendingResult> apiCallbacks) {
        PendingResult result = getInstance().mInCallApi.getProviderInfo(getInstance()
                .mClient, cn);
        ResultCallback callback = new ResultCallback<InCallProviderInfoResult>() {
                    @Override
                    public void onResult(InCallProviderInfoResult inCallProviderInfoResult) {
                        InCallProviderInfo icpi = inCallProviderInfoResult.inCallProviderInfo;
                        if (icpi == null) {
                            mCallMethodInfos.remove(cn);
                            return;
                        }

                        PackageManager packageManager = getInstance().mContext.getPackageManager();
                        Resources pluginResources = null;
                        try {
                            pluginResources = packageManager.getResourcesForApplication(
                                    cn.getPackageName());
                        } catch (PackageManager.NameNotFoundException e) {
                            Log.e(TAG, "Plugin isn't installed: " + cn);
                            mCallMethodInfos.remove(cn);
                            return;
                        }

                        synchronized (mCallMethodInfos) {
                            CallMethodInfo cmi = getCallMethodIfExists(cn);

                            if (cmi == null) {
                                return;
                            }

                            try {
                                cmi.mSingleColorBrandIcon =
                                        pluginResources.getDrawable(icpi.getSingleColorBrandIcon(),
                                                null);
                                cmi.mActionOneIcon =
                                        pluginResources.getDrawable(icpi.getActionOneIcon(), null);
                                cmi.mActionTwoIcon =
                                        pluginResources.getDrawable(icpi.getActionTwoIcon(), null);
                                cmi.mBrandIcon =
                                        pluginResources.getDrawable(icpi.getBrandIcon(), null);
                                cmi.mLoginIcon =
                                        pluginResources.getDrawable(icpi.getLoginIcon(), null);
                                cmi.mVoiceIcon = pluginResources.getDrawable(icpi
                                        .getVoiceMimeIcon(), null);
                                cmi.mVideoIcon = pluginResources.getDrawable(icpi
                                        .getVideoMimeIcon(), null);
                                cmi.mImIcon = pluginResources.getDrawable(icpi.getImMimeIcon(),
                                        null);
                                cmi.mBadgeIcon = pluginResources.getDrawable(icpi.getBadgeIcon(),
                                        null);

                            } catch (Resources.NotFoundException e) {
                                Log.e(TAG, "Resource Not found: " + cn);
                                mCallMethodInfos.remove(cn);
                                return;
                            }

                            cmi.mSlotId = -1;
                            cmi.mSubId = -1;
                            cmi.mColor = NO_COLOR;
                            cmi.mSubscriptionButtonText = icpi.getSubscriptionButtonText();
                            cmi.mCreditButtonText = icpi.getCreditsButtonText();
                            // If present, use the deprecated attribute defined hint text.
                            // These values may be overwritten by getHintText.
                            cmi.mT9HintDescriptionNoCreditOrSub = icpi.getT9HintDescription();
                            cmi.mT9HintDescriptionHasCreditOrSub = icpi.getT9HintDescription();
                            cmi.mActionOneText = icpi.getActionOneTitle();
                            cmi.mActionTwoText = icpi.getActionTwoTitle();
                            cmi.mIsInCallProvider = true;

                            cmi.mComponent = cn;
                            cmi.mNudgeComponent = icpi.getNudgeComponent() == null ? null :
                                    ComponentName.unflattenFromString(icpi.getNudgeComponent());
                            cmi.mDependentPackage = icpi.getDependentPackage();
                            cmi.mName = icpi.getTitle();
                            cmi.mSummary = icpi.getSummary();
                            cmi.mAccountType = icpi.getAccountType();
                            cmi.mAccountHandle = icpi.getAccountHandle();
                            if (TextUtils.isEmpty(cmi.mAccountHandle)) {
                                cmi.mAccountHandle = CallMethodUtils.lookupAccountHandle(
                                        getInstance().mContext, cmi.mAccountType);
                            }
                            cmi.mBrandIconId = icpi.getBrandIcon();
                            cmi.mLoginIconId = icpi.getLoginIcon();

                            cmi.mAccountType = icpi.getAccountType();
                            mCallMethodInfos.put(cn, cmi);
                            maybeBroadcastToSubscribers(this, apiCallbacks);
                        }
                    }
                };
        apiCallbacks.put(callback, result);
    }

    /**
     * Get our plugin enabled status
     * @param cn
     * @param apiCallbacks keeps track of the target callbacks before broadcast
     */
    protected static void getCallMethodStatus(final ComponentName cn, final
            HashMap<ResultCallback, PendingResult> apiCallbacks) {
        PendingResult result = getInstance().mInCallApi.getPluginStatus(getInstance().mClient, cn);
        ResultCallback callback = new ResultCallback<PluginStatusResult>() {
                @Override
                public void onResult(PluginStatusResult pluginStatusResult) {
                    synchronized (mCallMethodInfos) {
                        CallMethodInfo cmi = getCallMethodIfExists(cn);
                        if (cmi != null) {
                            cmi.mStatus = pluginStatusResult.status;
                            mCallMethodInfos.put(cn, cmi);
                            maybeBroadcastToSubscribers(this, apiCallbacks);
                        }
                    }
                }
            };
        apiCallbacks.put(callback, result);
    }

    /**
     * Send an event to the component
     * @param cn componentName to send the data to.
     */
    public static void shipAnalyticsToPlugin(final ComponentName cn, Event e) {
        if (cn == null) {
            return;
        }
        if (DEBUG) {
            Log.d(TAG, "componentName: " + cn.toShortString());
            Log.d(TAG, "Event: " + e.toString());
        }
        if (getInstance() == null ||
                getInstance().mInCallApi == null ||
                getInstance().mClient == null) {
            // For testing purposes, this might be null
            return;
        }
        getInstance().mInCallApi.sendAnalyticsEventToPlugin(getInstance().mClient, cn, e)
                .setResultCallback(new ResultCallback<Result>() {
                    @Override
                    public void onResult(Result result) {
                        if (DEBUG) {
                            Log.v(TAG, "Event sent with result: " + result.getStatus()
                                    .getStatusMessage());
                        }
                    }
                });
    }

    /**
     * Get the call method mime type
     * @param cn
     * @param apiCallbacks keeps track of the target callback before broadcast
     */
    protected static void getCallMethodMimeType(final ComponentName cn, final
            HashMap<ResultCallback, PendingResult> apiCallbacks) {
        PendingResult result = getInstance().mInCallApi.getCallableMimeType(getInstance().mClient,
                cn);
        ResultCallback callback = new ResultCallback<MimeTypeResult>() {
                @Override
                public void onResult(MimeTypeResult mimeTypeResult) {
                    synchronized (mCallMethodInfos) {
                        CallMethodInfo cmi = getCallMethodIfExists(cn);
                        if (cmi != null) {
                            cmi.mMimeType = mimeTypeResult.mimeType;
                            mCallMethodInfos.put(cn, cmi);
                            maybeBroadcastToSubscribers(this, apiCallbacks);
                        }
                    }
                }
            };
        apiCallbacks.put(callback, result);
    }

    /**
     * Get the call method mime type
     * @param cn
     * @param apiCallbacks keeps track of the target callback before broadcast
     */
    protected static void getCallMethodVideoCallableMimeType(final ComponentName cn, final
            HashMap<ResultCallback, PendingResult> apiCallbacks) {
        PendingResult result = getInstance().mInCallApi.getVideoCallableMimeType(getInstance()
                .mClient, cn);
        ResultCallback callback = new ResultCallback<MimeTypeResult>() {
                @Override
                public void onResult(MimeTypeResult mimeTypeResult) {
                    synchronized (mCallMethodInfos) {
                        CallMethodInfo cmi = getCallMethodIfExists(cn);
                        if (cmi != null) {
                            cmi.mVideoCallableMimeType = mimeTypeResult.mimeType;
                            mCallMethodInfos.put(cn, cmi);
                            maybeBroadcastToSubscribers(this, apiCallbacks);
                        }
                    }
                }
            };
        apiCallbacks.put(callback, result);
    }

    /**
     * Get the IM mime type
     * @param cn
     * @param apiCallbacks keeps track of the target callbacks before broadcast
     */
    protected static void getCallMethodImMimeType(final ComponentName cn, final
            HashMap<ResultCallback, PendingResult> apiCallbacks) {
        PendingResult result = getInstance().mInCallApi.getImMimeType(getInstance().mClient, cn);
        ResultCallback callback = new ResultCallback<MimeTypeResult>() {
                @Override
                public void onResult(MimeTypeResult mimeTypeResult) {
                    synchronized (mCallMethodInfos) {
                        CallMethodInfo cmi = getCallMethodIfExists(cn);
                        if (cmi != null) {
                            cmi.mImMimeType = mimeTypeResult.mimeType;
                            mCallMethodInfos.put(cn, cmi);
                            maybeBroadcastToSubscribers(this, apiCallbacks);
                        }
                    }
                }
            };
        apiCallbacks.put(callback, result);
    }

    /**
     * Get the Authentication state of the callmethod
     * @param cn
     * @param apiCallbacks keeps track of the target callbacks before broadcast
     */
    protected static void getCallMethodAuthenticated(final ComponentName cn, final
            HashMap<ResultCallback, PendingResult> apiCallbacks) {
        PendingResult result = getInstance().mInCallApi.getAuthenticationState(getInstance()
                .mClient, cn);
        ResultCallback callback = new ResultCallback<AuthenticationStateResult>() {
            @Override
            public void onResult(AuthenticationStateResult result) {
                synchronized (mCallMethodInfos) {
                    CallMethodInfo cmi = getCallMethodIfExists(cn);
                    if (cmi != null) {
                        cmi.mIsAuthenticated = result.result == StatusCodes.AuthenticationState
                                .LOGGED_IN;
                        mCallMethodInfos.put(cn, cmi);
                        maybeBroadcastToSubscribers(this, apiCallbacks);
                    }
                }
            }
        };
        apiCallbacks.put(callback, result);
    }

    /**
     * Get the logged in account handle of the callmethod
     * @param cn
     * @param apiCallbacks keeps track of the target callbacks before broadcast
     */
    protected static void getCallMethodAccountHandle(final ComponentName cn, final
            HashMap<ResultCallback, PendingResult> apiCallbacks) {
        PendingResult result = getInstance().mInCallApi.getAccountHandle(getInstance().mClient, cn);
        ResultCallback callback = new ResultCallback<AccountHandleResult>() {
                    @Override
                    public void onResult(AccountHandleResult result) {
                        synchronized (mCallMethodInfos) {
                            CallMethodInfo cmi = getCallMethodIfExists(cn);
                            if (cmi != null) {
                                cmi.mAccountHandle = result.accountHandle;
                                if (TextUtils.isEmpty(cmi.mAccountHandle)) {
                                    cmi.mAccountHandle =
                                            CallMethodUtils.lookupAccountHandle(
                                            getInstance().mContext, cmi.mAccountType);
                                }
                                mCallMethodInfos.put(cn, cmi);
                                maybeBroadcastToSubscribers(this, apiCallbacks);
                            }
                        }
                    }
                };
        apiCallbacks.put(callback, result);
    }

    /**
     * Get the settings intent for the callmethod
     * @param cn
     * @param apiCallbacks keeps track of the target callback counts before broadcast
     */
    private static void getSettingsIntent(final ComponentName cn, final
            HashMap<ResultCallback, PendingResult> apiCallbacks) {
        PendingResult result = getInstance().mInCallApi.getSettingsIntent(getInstance().mClient,
                cn);
        ResultCallback callback = new ResultCallback<PendingIntentResult>() {
                    @Override
                    public void onResult(PendingIntentResult pendingIntentResult) {
                        synchronized (mCallMethodInfos) {
                            CallMethodInfo cmi = getCallMethodIfExists(cn);
                            if (cmi != null) {
                                cmi.mSettingsIntent = pendingIntentResult.intent;
                                mCallMethodInfos.put(cn, cmi);
                                maybeBroadcastToSubscribers(this, apiCallbacks);
                            }
                        }
                    }
                };
        apiCallbacks.put(callback, result);
    }

    /**
     * Get the hint texts for the callmethod
     * @param cn
     * @param apiCallbacks keeps track of the target callback counts before broadcast
     */
    private static void getHintText(final ComponentName cn,
            final HashMap<ResultCallback, PendingResult> apiCallbacks) {
        PendingResult result = getInstance().mInCallApi.getHintText(getInstance().mClient, cn);
        ResultCallback callback = new ResultCallback<HintTextResultResult>() {
            @Override
            public void onResult(HintTextResultResult hintTextResultResult) {
                if (hintTextResultResult != null) {
                    HintTextResult result = hintTextResultResult.result;
                    if (result != null) {
                        synchronized (mCallMethodInfos) {
                            CallMethodInfo cmi = getCallMethodIfExists(cn);
                            if (cmi != null) {
                                String hintText = result.getNoCreditOrSubscriptionHint();
                                if (!TextUtils.isEmpty(hintText)) {
                                    cmi.mT9HintDescriptionNoCreditOrSub = hintText;
                                }

                                hintText = result.getHasCreditOrSubscriptionHint();
                                if (!TextUtils.isEmpty(hintText)) {
                                    cmi.mT9HintDescriptionHasCreditOrSub = hintText;
                                }
                                mCallMethodInfos.put(cn, cmi);
                                maybeBroadcastToSubscribers(this, apiCallbacks);
                            }
                        }
                    }
                }
            }
        };
        apiCallbacks.put(callback, result);
    }

    private static void getCreditInfo(final ComponentName cn, final
            HashMap<ResultCallback, PendingResult> apiCallbacks) {
        PendingResult result = getInstance().mInCallApi.getCreditInfo(getInstance().mClient, cn);
        ResultCallback callback = new ResultCallback<GetCreditInfoResultResult>() {
                    @Override
                    public void onResult(GetCreditInfoResultResult getCreditInfoResultResult) {
                        CallMethodInfo cmi = getCallMethodIfExists(cn);
                        if (cmi != null) {
                            GetCreditInfoResult gcir = getCreditInfoResultResult.result;
                            if (gcir == null || gcir.creditInfo == null) {
                                // Build zero credit dummy if no result found.
                                cmi.mProviderCreditInfo =
                                        new CreditInfo(new CreditBalance(0, null), null);
                            } else {
                                cmi.mProviderCreditInfo = gcir.creditInfo;
                            }
                            mCallMethodInfos.put(cn, cmi);
                            maybeBroadcastToSubscribers(this, apiCallbacks);
                        }
                    }
                };
        apiCallbacks.put(callback, result);
    }

    private static void getManageCreditsIntent(final ComponentName cn, final
            HashMap<ResultCallback, PendingResult> apiCallbacks) {
        PendingResult result = getInstance().mInCallApi.getManageCreditsIntent(getInstance()
                .mClient, cn);
        ResultCallback callback = new ResultCallback<PendingIntentResult>() {
                    @Override
                    public void onResult(PendingIntentResult pendingIntentResult) {
                        CallMethodInfo cmi = getCallMethodIfExists(cn);
                        if (cmi != null) {
                            cmi.mManageCreditIntent = pendingIntentResult.intent;
                            mCallMethodInfos.put(cn, cmi);
                            maybeBroadcastToSubscribers(this, apiCallbacks);
                        }
                    }
                };
        apiCallbacks.put(callback, result);
    }

    private static void checkLowCreditConfig(final ComponentName cn, final
            HashMap<ResultCallback, PendingResult> apiCallbacks) {
        // find a nudge component if it exists for this package
        Intent nudgeIntent = new Intent("cyanogen.service.NUDGE_PROVIDER");
        nudgeIntent.setPackage(cn.getPackageName());
        List<ResolveInfo> resolved = getInstance().mContext.getPackageManager()
                .queryIntentServices(nudgeIntent, 0);
        if (resolved != null && !resolved.isEmpty()) {
            ResolveInfo result = resolved.get(0);
            ComponentName nudgeComponent = new ComponentName(result.serviceInfo.applicationInfo
                    .packageName, result.serviceInfo.name);
            collectLowCreditConfig(cn, nudgeComponent, apiCallbacks);
            return;
        }

        // if a nudge component doesn't exist, just finish here
        maybeBroadcastToSubscribers(null, apiCallbacks);
    }

    private static void collectLowCreditConfig(final ComponentName pluginComponent, final
            ComponentName nudgeComponent, final HashMap<ResultCallback, PendingResult>
            apiCallbacks) {
        PendingResult result = NudgeServices.NudgeApi.getConfigurationForKey(getInstance().mClient,
                nudgeComponent, NudgeKey.INCALL_CREDIT_NUDGE);
        ResultCallback callback = new ResultCallback<BundleResult>() {
            @Override
            public void onResult(BundleResult bundleResult) {
                CallMethodInfo cmi = getCallMethodIfExists(pluginComponent);
                if (cmi != null) {
                    if (bundleResult != null && bundleResult.bundle != null &&
                            bundleResult.bundle.containsKey(NudgeKey
                                    .INCALL_PARAM_CREDIT_WARN)) {
                        Object creditWarn = bundleResult.bundle.get(NudgeKey
                                .INCALL_PARAM_CREDIT_WARN);
                        if (creditWarn.getClass().equals(Integer.class)) {
                            cmi.mCreditWarn = (Integer) creditWarn;
                        } else if (creditWarn.getClass().equals(Float.class)) {
                            cmi.mCreditWarn = (Float) creditWarn;
                        } else {
                            Log.e(TAG, "Invalid value for Credit Warn limit: " + creditWarn);
                        }
                        mCallMethodInfos.put(pluginComponent, cmi);
                    }
                    maybeBroadcastToSubscribers(this, apiCallbacks);
                }
            }
        };
        apiCallbacks.put(callback, result);
    }

    protected static void getLoginIntent(final ComponentName cn, final
            HashMap<ResultCallback, PendingResult> apiCallbacks) {
        PendingResult result = getInstance().mInCallApi.getLoginIntent(getInstance().mClient, cn);
        ResultCallback callback = new ResultCallback<PendingIntentResult>() {
                    @Override
                    public void onResult(PendingIntentResult pendingIntentResult) {
                        synchronized (mCallMethodInfos) {
                            CallMethodInfo cmi = getCallMethodIfExists(cn);
                            if (cmi != null) {
                                cmi.mLoginIntent = pendingIntentResult.intent;
                                mCallMethodInfos.put(cn, cmi);
                                maybeBroadcastToSubscribers(this, apiCallbacks);
                            }
                        }
                    }
                };
        apiCallbacks.put(callback, result);
    }

    /**
     * Get the the contact directory search intent with a callback
     * @param cn
     * @param apiCallbacks keeps track of the target callbacks before broadcast
     */
    protected static void getDefaultDirectorySearchIntent(final ComponentName cn, final
            HashMap<ResultCallback, PendingResult> apiCallbacks) {
        PendingResult result = getInstance().mInCallApi.getDirectorySearchIntent(getInstance()
                .mClient, cn, Uri.parse(""));
        ResultCallback callback = new ResultCallback<PendingIntentResult>() {
                    @Override
                    public void onResult(PendingIntentResult pendingIntentResult) {
                        synchronized (mCallMethodInfos) {
                            CallMethodInfo cmi = getCallMethodIfExists(cn);
                            if (cmi != null) {
                                cmi.mDefaultDirectorySearchIntent = pendingIntentResult.intent;
                                maybeBroadcastToSubscribers(this, apiCallbacks);
                            }
                        }
                    }
                };
        apiCallbacks.put(callback, result);
    }

    /**
     * Get the the invite intent with a callback
     * @param cn
     * @param contactInfo contact info contains display name, phone, and contact Uri for lookup
     */
    protected static void getInviteIntent(final ComponentName cn, InCallContactInfo contactInfo) {
        getInstance().mInCallApi.getInviteIntent(getInstance().mClient, cn,
                contactInfo).setResultCallback(new ResultCallback<PendingIntentResult>() {
            @Override
            public void onResult(PendingIntentResult pendingIntentResult) {
                synchronized (mCallMethodInfos) {
                    CallMethodInfo cmi = getCallMethodIfExists(cn);
                    if (cmi != null) {
                        cmi.mInviteIntent = pendingIntentResult.intent;
                    }
                }
            }
        });
    }

    /**
     * Get the the contact directory search intent with a blocking call
     * @param cn
     * @param contactInfo contact info contains display name, phone and contact Uri for lookup
     */

    public static PendingIntent getInviteIntentSync(final ComponentName cn, InCallContactInfo
            contactInfo) {
        PendingIntentResult pendingIntentResult =
                getInstance().mInCallApi.getInviteIntent(getInstance().mClient, cn, contactInfo)
                        .await();
        CallMethodInfo cmi = getCallMethodIfExists(cn);
        if (cmi != null) {
            cmi.mInviteIntent = pendingIntentResult.intent;
        }
        return cmi.mInviteIntent;
    }

    /**
     * Get the the contact directory search intent with a callback
     * @param cn
     * @param contactUri contact lookup Uri
     */
    protected static void getDirectorySearchIntent(final ComponentName cn, Uri contactUri) {
        getInstance().mInCallApi.getDirectorySearchIntent(getInstance().mClient, cn, contactUri)
                .setResultCallback(new ResultCallback<PendingIntentResult>() {
                    @Override
                    public void onResult(PendingIntentResult pendingIntentResult) {
                        synchronized (mCallMethodInfos) {
                            CallMethodInfo cmi = getCallMethodIfExists(cn);
                            if (cmi != null) {
                                cmi.mDirectorySearchIntent = pendingIntentResult.intent;
                            }
                        }
                    }
                });
    }

    /**
     * Get the the contact directory search intent with a blocking call
     * @param cn
     * @param contactUri contact lookup Uri
     */

    public static PendingIntent getDirectorySearchIntentSync(final ComponentName cn, Uri
            contactUri) {
        PendingIntentResult pendingIntentResult =
                getInstance().mInCallApi.getDirectorySearchIntent(getInstance().mClient, cn,
                        contactUri).await();
        CallMethodInfo cmi = getCallMethodIfExists(cn);
        if (cmi != null) {
            cmi.mDirectorySearchIntent = pendingIntentResult.intent;
        }
        return cmi.mDirectorySearchIntent;
    }

    protected static ComponentName getNudgeComponent(final ComponentName cn) {
        // find a nudge component if it exists for this package
        Intent nudgeIntent = new Intent("cyanogen.service.NUDGE_PROVIDER");
        nudgeIntent.setPackage(cn.getPackageName());
        List<ResolveInfo> resolved = getInstance().mContext.getPackageManager()
                .queryIntentServices(nudgeIntent, 0);
        if (resolved != null && !resolved.isEmpty()) {
            ResolveInfo result = resolved.get(0);
            return new ComponentName(result.serviceInfo.applicationInfo
                    .packageName, result.serviceInfo.name);
        }
        // if a nudge component doesn't exist, just finish here
        return null;
    }

    protected static void getNudgeConfiguration(final ComponentName cn, final String key, final
            HashMap<ResultCallback, PendingResult> apiCallbacks) {
        final ComponentName nudgeComponent;

        CallMethodInfo cm = null;
        synchronized (mCallMethodInfos) {
            cm = getCallMethodIfExists(cn);
        }
        if (cm == null) {
            return;
        }

        if (cm.mNudgeComponent == null) {
            nudgeComponent = getNudgeComponent(cn);
            cm.mNudgeComponent = nudgeComponent;
        } else {
            nudgeComponent = cm.mNudgeComponent;
        }
        PendingResult result = NudgeServices.NudgeApi.getConfigurationForKey(getInstance()
                .mClient, nudgeComponent, key);
        ResultCallback callback = new ResultCallback<BundleResult>() {
            @Override
            public void onResult(BundleResult bundleResult) {
                synchronized (mCallMethodInfos) {
                    CallMethodInfo cmi = getCallMethodIfExists(cn);
                    if (bundleResult != null && bundleResult.bundle != null) {
                        Bundle nudgeConfig = bundleResult.bundle;
                        switch (key) {
                            case NudgeKey.INCALL_CONTACT_FRAGMENT_LOGIN:
                                cmi.mLoginSubtitle =
                                        nudgeConfig.getString(NudgeKey.NUDGE_PARAM_SUBTITLE, "");
                                break;
                            case NudgeKey.INCALL_CONTACT_CARD_LOGIN:
                                cmi.mLoginNudgeEnable =
                                        nudgeConfig.getBoolean(NudgeKey.NUDGE_PARAM_ENABLED, true)
                                                &&
                                                PreferenceManager.getDefaultSharedPreferences
                                                        (getInstance().mContext)
                                                        .getBoolean(cn.getClassName() + "." + key,
                                                                true);
                                cmi.mLoginNudgeTitle =
                                        nudgeConfig.getString(NudgeKey.NUDGE_PARAM_TITLE);
                                cmi.mLoginNudgeSubtitle =
                                        nudgeConfig.getString(NudgeKey.NUDGE_PARAM_SUBTITLE);
                                cmi.mLoginNudgeActionText = nudgeConfig.getString(NudgeKey.
                                        NUDGE_PARAM_ACTION_TEXT);
                                break;
                            case NudgeKey.INCALL_CONTACT_CARD_DOWNLOAD:
                                cmi.mInstallNudgeEnable =
                                        nudgeConfig.getBoolean(NudgeKey.NUDGE_PARAM_ENABLED, true)
                                                &&
                                                PreferenceManager.getDefaultSharedPreferences
                                                        (getInstance().mContext)
                                                        .getBoolean(cn.getClassName() + "." + key,
                                                                true);
                                cmi.mInstallNudgeTitle =
                                        nudgeConfig.getString(NudgeKey.NUDGE_PARAM_TITLE);
                                cmi.mInstallNudgeSubtitle =
                                        nudgeConfig.getString(NudgeKey.NUDGE_PARAM_SUBTITLE);
                                cmi.mInstallNudgeActionText = nudgeConfig.getString(NudgeKey.
                                        NUDGE_PARAM_ACTION_TEXT);
                                break;
                            default:
                                break;

                        }
                    }

                    maybeBroadcastToSubscribers(this, apiCallbacks);
                }
            }
        };
        apiCallbacks.put(callback, result);
    }

    public static Set<String> getAllEnabledVoiceMimeSet() {
        String[] mimes = getAllEnabledMimeTypes().split(",");
        HashSet<String> mimeSet = new HashSet<String>();
        if (mimes != null) {
            mimeSet.addAll(Arrays.asList(mimes));
        }
        return mimeSet;
    }

    public static Set<String> getAllEnabledVideoImMimeSet() {
        String[] videoMimes = getAllEnabledVideoCallableMimeTypes().split(",");
        String[] imMimes = getAllEnabledImMimeTypes().split(",");
        HashSet<String> mimeSet = new HashSet<String>();

        if (videoMimes != null) {
            mimeSet.addAll(Arrays.asList(videoMimes));
        }
        if (imMimes != null) {
            mimeSet.addAll(Arrays.asList(imMimes));
        }
        return mimeSet;
    }

    public static boolean infoReady() {
        return dataHasBeenBroadcastPreviously;
    }
}