/
ContentDiscoveryManifest.java
232 lines (200 loc) · 8.34 KB
/
ContentDiscoveryManifest.java
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
package io.branch.indexing;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Created by sojanpr on 6/14/16.
* <p>
* The content discovery manifest is the instruction set for how the Branch SDK will *optionally* and automatically discover content
* within your app. It parses the configuration from the server, which will tell the client whether it's eligible for content discovery.
* This manifest is then used to inform the ContentDiscover class's behavior.
* * Note that this behavior can be controlled from the dashboard.
* </p>
*/
public class ContentDiscoveryManifest {
private static ContentDiscoveryManifest thisInstance_;
/* JsonObject representation for the CD manifest */
private JSONObject cdManifestObject_;
/* Manifest version number */
private String manifestVersion_;
/* Max length for an individual text item */
private int maxTextLen_ = 0;
/* Max num of views to do content discovery in a session */
private int maxViewHistoryLength_ = 1;
/* Maximum size of CD data payload per requests updating CD data to server */
private int maxPacketSize_ = 0;
/* Specifies if CD is enabled for this session */
private boolean isCDEnabled_ = false;
/* Json Array for the content path object and the filtered views for this application */
private JSONArray contentPaths_;
public static final String MANIFEST_VERSION_KEY = "mv";
public static final String PACKAGE_NAME_KEY = "pn";
static final String HASH_MODE_KEY = "h";
private static final String MANIFEST_KEY = "m";
private static final String PATH_KEY = "p";
private static final String FILTERED_KEYS = "ck";
private static final String MAX_TEXT_LEN_KEY = "mtl";
private static final String MAX_VIEW_HISTORY_LENGTH = "mhl";
private static final String MAX_PACKET_SIZE_KEY = "mps";
public static final String CONTENT_DISCOVER_KEY = "cd";
private static final String DISCOVERY_REPEAT_INTERVAL = "dri";
private static final String MAX_DISCOVERY_REPEAT = "mdr";
static final int DEF_MAX_DISCOVERY_REPEAT = 15; // Default Maximum number for discovery repeat
static final int DRI_MINIMUM_THRESHOLD = 500; // Minimum value for Discovery repeat interval
private SharedPreferences sharedPref;
private final String PREF_KEY = "BNC_CD_MANIFEST";
private ContentDiscoveryManifest(Context context) {
sharedPref = context.getSharedPreferences("bnc_content_discovery_manifest_storage", Context.MODE_PRIVATE);
retrieve(context);
}
public static ContentDiscoveryManifest getInstance(Context context) {
if (thisInstance_ == null) {
thisInstance_ = new ContentDiscoveryManifest(context);
}
return thisInstance_;
}
private void persist() {
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString(PREF_KEY, cdManifestObject_.toString()).apply();
}
private void retrieve(Context context) {
String jsonStr = sharedPref.getString(PREF_KEY, null);
if (jsonStr != null) {
try {
cdManifestObject_ = new JSONObject(jsonStr);
if (cdManifestObject_.has(MANIFEST_VERSION_KEY)) {
manifestVersion_ = cdManifestObject_.getString(MANIFEST_VERSION_KEY);
}
if (cdManifestObject_.has(MANIFEST_KEY)) {
contentPaths_ = cdManifestObject_.getJSONArray(MANIFEST_KEY);
}
} catch (JSONException ignored) {
cdManifestObject_ = new JSONObject();
}
} else {
cdManifestObject_ = new JSONObject();
}
}
public void onBranchInitialised(JSONObject branchInitResp) {
if (branchInitResp.has(CONTENT_DISCOVER_KEY)) {
isCDEnabled_ = true;
try {
JSONObject cdObj = branchInitResp.getJSONObject(CONTENT_DISCOVER_KEY);
if (cdObj.has(MANIFEST_VERSION_KEY)) {
manifestVersion_ = cdObj.getString(MANIFEST_VERSION_KEY);
}
if (cdObj.has(MAX_VIEW_HISTORY_LENGTH)) {
maxViewHistoryLength_ = cdObj.getInt(MAX_VIEW_HISTORY_LENGTH);
}
if (cdObj.has(MANIFEST_KEY)) {
contentPaths_ = cdObj.getJSONArray(MANIFEST_KEY);
}
if (cdObj.has(MAX_TEXT_LEN_KEY)) {
int maxTextLength = cdObj.getInt(MAX_TEXT_LEN_KEY);
if (maxTextLength > 0) {
maxTextLen_ = maxTextLength;
}
}
if (cdObj.has(MAX_PACKET_SIZE_KEY)) {
maxPacketSize_ = cdObj.getInt(MAX_PACKET_SIZE_KEY);
}
cdManifestObject_.put(MANIFEST_VERSION_KEY, manifestVersion_);
cdManifestObject_.put(MANIFEST_KEY, contentPaths_);
persist();
} catch (JSONException ignore) {
}
} else {
isCDEnabled_ = false;
}
}
CDPathProperties getCDPathProperties(Activity activity) {
CDPathProperties pathProperties = null;
if (contentPaths_ != null) {
String viewPath = "/" + activity.getClass().getSimpleName();
try {
for (int i = 0; i < contentPaths_.length(); i++) {
JSONObject pathObj = contentPaths_.getJSONObject(i);
if (pathObj.has(PATH_KEY) && pathObj.getString(PATH_KEY).equals(viewPath)) {
pathProperties = new CDPathProperties(pathObj);
break;
}
}
} catch (JSONException ignore) {
}
}
return pathProperties;
}
boolean isCDEnabled() {
return isCDEnabled_;
}
int getMaxTextLen() {
return maxTextLen_;
}
int getMaxPacketSize() {
return maxPacketSize_;
}
int getMaxViewHistorySize() {
return maxViewHistoryLength_;
}
public String getManifestVersion() {
if (TextUtils.isEmpty(manifestVersion_)) {
return "-1";
}
return manifestVersion_;
}
class CDPathProperties {
final JSONObject pathInfo_;
private boolean isClearText_;
private int discoveryRepeatInterval_;
private int maxDiscoveryRepeat_;
int getDiscoveryRepeatInterval() {
return discoveryRepeatInterval_;
}
int getMaxDiscoveryRepeatNumber() {
return maxDiscoveryRepeat_;
}
CDPathProperties(JSONObject pathInfo) {
pathInfo_ = pathInfo;
maxDiscoveryRepeat_ = DEF_MAX_DISCOVERY_REPEAT;
if (pathInfo.has(HASH_MODE_KEY)) {
try {
isClearText_ = !pathInfo.getBoolean(HASH_MODE_KEY);
} catch (JSONException e) {
e.printStackTrace();
}
}
try {
if (pathInfo.has(DISCOVERY_REPEAT_INTERVAL)) {
discoveryRepeatInterval_ = pathInfo.getInt(DISCOVERY_REPEAT_INTERVAL);
}
if (pathInfo.has(MAX_DISCOVERY_REPEAT)) {
maxDiscoveryRepeat_ = pathInfo.getInt(MAX_DISCOVERY_REPEAT);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
JSONArray getFilteredElements() {
JSONArray elementArray = null;
if (pathInfo_.has(FILTERED_KEYS)) {
try {
elementArray = pathInfo_.getJSONArray(FILTERED_KEYS);
} catch (JSONException e) {
e.printStackTrace();
}
}
return elementArray;
}
boolean isClearTextRequested() {
return isClearText_;
}
boolean isSkipContentDiscovery() {
JSONArray filteredElements = getFilteredElements();
return filteredElements != null && filteredElements.length() == 0;
}
}
}