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
|
package com.android.dialer.discovery;
import android.content.ComponentName;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import com.android.dialer.DialtactsActivity;
import com.android.phone.common.ambient.AmbientConnection;
import com.android.phone.common.incall.CallMethodUtils;
import com.android.phone.common.util.ImageUtils;
import com.cyanogen.ambient.common.api.AmbientApiClient;
import com.cyanogen.ambient.discovery.DiscoveryManagerServices;
import com.cyanogen.ambient.discovery.NudgeServices;
import com.cyanogen.ambient.discovery.nudge.NotificationNudge;
import com.cyanogen.ambient.discovery.nudge.Nudge;
import com.cyanogen.ambient.discovery.util.NudgeKey;
import com.cyanogen.ambient.incall.InCallApi;
import com.cyanogen.ambient.incall.InCallServices;
import com.cyanogen.ambient.incall.results.InCallProviderInfoResult;
import com.cyanogen.ambient.incall.results.PluginStatusResult;
import com.cyanogen.ambient.plugin.PluginStatus;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DiscoveryEventHandler {
private static final String TAG = "DiscoveryEventHandler";
private static final boolean DEBUG_STATUS = false;
public static void getNudgeProvidersWithKey(final Context context, final String key) {
// Spin up a new thread to make the needed calls to ambient.
new Thread(new Runnable() {
@Override
public void run() {
AmbientApiClient client = AmbientConnection.CLIENT.get(context);
ArrayList<NotificationNudge> nudges = getNudges(client, context, key);
sendNudgeRequestToDiscovery(client, nudges);
}
}).start();
}
public static void sendNudgeRequestToDiscovery(AmbientApiClient client,
ArrayList<NotificationNudge> nudges) {
for (NotificationNudge nn : nudges) {
DiscoveryManagerServices.DiscoveryManagerApi.publishNudge(client, nn);
}
}
public static ArrayList<NotificationNudge> getNudges(AmbientApiClient client, Context context,
String key) {
Map nudgePlugins =
NudgeServices.NudgeApi.getAvailableNudgesForKey(client, key).await().components;
ArrayList<NotificationNudge> notificationNudges = new ArrayList<>();
if (nudgePlugins == null) {
return notificationNudges;
}
InCallApi api = InCallServices.getInstance();
List<ComponentName> plugins = api.getInstalledPlugins(client).await().components;
HashMap<String, Bundle> availableNudges = new HashMap<>();
for (Object entry : nudgePlugins.entrySet()) {
Map.Entry<ComponentName, Bundle> theEntry = (Map.Entry<ComponentName, Bundle>) entry;
availableNudges.put(theEntry.getKey().getPackageName(), theEntry.getValue());
}
if (plugins != null && plugins.size() > 0) {
for (ComponentName component : plugins) {
if (availableNudges.containsKey(component.getPackageName())) {
PluginStatusResult statusResult =
api.getPluginStatus(client, component).await();
Bundle b = availableNudges.get(component.getPackageName());
if (validateShouldShowNudge(key, context, b)) {
// Nudge not yet ready for this item.
continue;
}
if (DEBUG_STATUS || (statusResult.status != PluginStatus.DISABLED &&
statusResult.status != PluginStatus.UNAVAILABLE)) {
Bitmap notificationIcon = null;
InCallProviderInfoResult providerInfo =
api.getProviderInfo(client, component).await();
if (providerInfo != null && providerInfo.inCallProviderInfo != null) {
try {
Resources pluginResources = context.getPackageManager()
.getResourcesForApplication(component.getPackageName());
Drawable d = pluginResources.getDrawable(
providerInfo.inCallProviderInfo.getBrandIcon(), null);
notificationIcon = ImageUtils.drawableToBitmap(d);
} catch (Resources.NotFoundException e) {
Log.e(TAG, "Unable to retrieve icon for plugin: " + component);
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, "Plugin isn't installed: " + component);
}
}
NotificationNudge nn = new NotificationNudge(component.getPackageName(),
Nudge.Type.IMMEDIATE,
b.getString(NudgeKey.NUDGE_PARAM_TITLE),
b.getString(NudgeKey.NOTIFICATION_PARAM_BODY));
if (notificationIcon != null) {
nn.setLargeIcon(notificationIcon);
}
Parcelable[] actions =
b.getParcelableArray(NudgeKey.NOTIFICATION_PARAM_NUDGE_ACTIONS);
for (Parcelable action : actions) {
NotificationNudge.Button button = (NotificationNudge.Button) action;
nn.addButton(button);
}
notificationNudges.add(nn);
}
}
}
}
return notificationNudges;
}
private static boolean validateShouldShowNudge(String key, Context c, Bundle b) {
boolean checkCount = false;
SharedPreferences preferences = c.getSharedPreferences(DialtactsActivity.SHARED_PREFS_NAME,
Context.MODE_PRIVATE);
int count = 0;
if (key.equals(NudgeKey.NOTIFICATION_INTERNATIONAL_CALL)) {
count = preferences.getInt(CallMethodUtils.PREF_INTERNATIONAL_CALLS, 0);
} else if (key.equals(NudgeKey.NOTIFICATION_WIFI_CALL)) {
count = preferences.getInt(CallMethodUtils.PREF_WIFI_CALL, 0);
}
checkCount =
count == b.getInt(NudgeKey.NOTIFICATION_PARAM_EVENTS_FIRST_NUDGE, 0) ||
count == b.getInt(NudgeKey.NOTIFICATION_PARAM_EVENTS_SECOND_NUDGE, 0);
// return true if nudge should be shown
return checkCount;
}
}
|