/
FetchedAppSettingsManager.java
298 lines (263 loc) · 13.8 KB
/
FetchedAppSettingsManager.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
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
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
* copy, modify, and distribute this software in source code or binary form for use
* in connection with the web services and APIs provided by Facebook.
*
* As with any software that integrates with the Facebook platform, your use of
* this software is subject to the Facebook Developer Principles and Policies
* [http://developers.facebook.com/policy/]. This copyright notice shall be
* included in all copies or substantial portions of the software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.facebook.internal;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import com.facebook.FacebookSdk;
import com.facebook.GraphRequest;
import com.facebook.appevents.internal.AutomaticAnalyticsLogger;
import com.facebook.appevents.internal.Constants;
import com.facebook.core.BuildConfig;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* com.facebook.internal is solely for the use of other packages within the Facebook SDK for
* Android. Use of any of the classes in this package is unsupported, and they may be modified or
* removed without warning at any time.
*/
public final class FetchedAppSettingsManager {
private static final String TAG = FetchedAppSettingsManager.class.getCanonicalName();
private static final String APP_SETTINGS_PREFS_STORE =
"com.facebook.internal.preferences.APP_SETTINGS";
private static final String APP_SETTINGS_PREFS_KEY_FORMAT =
"com.facebook.internal.APP_SETTINGS.%s";
private static final String APP_SETTING_SUPPORTS_IMPLICIT_SDK_LOGGING =
"supports_implicit_sdk_logging";
private static final String APP_SETTING_NUX_CONTENT = "gdpv4_nux_content";
private static final String APP_SETTING_NUX_ENABLED = "gdpv4_nux_enabled";
private static final String APP_SETTING_CUSTOM_TABS_ENABLED =
"gdpv4_chrome_custom_tabs_enabled";
private static final String APP_SETTING_DIALOG_CONFIGS = "android_dialog_configs";
private static final String APP_SETTING_ANDROID_SDK_ERROR_CATEGORIES =
"android_sdk_error_categories";
private static final String APP_SETTING_APP_EVENTS_SESSION_TIMEOUT =
"app_events_session_timeout";
private static final String APP_SETTING_APP_EVENTS_FEATURE_BITMASK =
"app_events_feature_bitmask";
private static final int AUTOMATIC_LOGGING_ENABLED_BITMASK_FIELD = 1 << 3;
// The second bit of app_events_feature_bitmask is used for iOS in-app purchase automatic
// logging, while the fourth bit is used for Android in-app purchase automatic logging.
private static final int IAP_AUTOMATIC_LOGGING_ENABLED_BITMASK_FIELD = 1 << 4;
private static final String APP_SETTING_SMART_LOGIN_OPTIONS =
"seamless_login";
private static final String SMART_LOGIN_BOOKMARK_ICON_URL = "smart_login_bookmark_icon_url";
private static final String SMART_LOGIN_MENU_ICON_URL = "smart_login_menu_icon_url";
private static final String SDK_UPDATE_MESSAGE = "sdk_update_message";
private static final String[] APP_SETTING_FIELDS = new String[]{
APP_SETTING_SUPPORTS_IMPLICIT_SDK_LOGGING,
APP_SETTING_NUX_CONTENT,
APP_SETTING_NUX_ENABLED,
APP_SETTING_CUSTOM_TABS_ENABLED,
APP_SETTING_DIALOG_CONFIGS,
APP_SETTING_ANDROID_SDK_ERROR_CATEGORIES,
APP_SETTING_APP_EVENTS_SESSION_TIMEOUT,
APP_SETTING_APP_EVENTS_FEATURE_BITMASK,
APP_SETTING_SMART_LOGIN_OPTIONS,
SMART_LOGIN_BOOKMARK_ICON_URL,
SMART_LOGIN_MENU_ICON_URL
};
private static final String APPLICATION_FIELDS = "fields";
private static Map<String, FetchedAppSettings> fetchedAppSettings =
new ConcurrentHashMap<String, FetchedAppSettings>();
private static AtomicBoolean loadingSettings = new AtomicBoolean(false);
private static boolean printedSDKUpdatedMessage = false;
public static void loadAppSettingsAsync() {
final Context context = FacebookSdk.getApplicationContext();
final String applicationId = FacebookSdk.getApplicationId();
boolean canStartLoading = loadingSettings.compareAndSet(false, true);
if (Utility.isNullOrEmpty(applicationId) ||
fetchedAppSettings.containsKey(applicationId) ||
!canStartLoading) {
return;
}
final String settingsKey = String.format(APP_SETTINGS_PREFS_KEY_FORMAT, applicationId);
FacebookSdk.getExecutor().execute(new Runnable() {
@Override
public void run() {
// See if we had a cached copy and use that immediately.
SharedPreferences sharedPrefs = context.getSharedPreferences(
APP_SETTINGS_PREFS_STORE,
Context.MODE_PRIVATE);
String settingsJSONString = sharedPrefs.getString(settingsKey, null);
FetchedAppSettings appSettings = null;
if (!Utility.isNullOrEmpty(settingsJSONString)) {
JSONObject settingsJSON = null;
try {
settingsJSON = new JSONObject(settingsJSONString);
} catch (JSONException je) {
Utility.logd(Utility.LOG_TAG, je);
}
if (settingsJSON != null) {
appSettings = parseAppSettingsFromJSON(applicationId, settingsJSON);
}
}
JSONObject resultJSON = getAppSettingsQueryResponse(applicationId);
if (resultJSON != null) {
parseAppSettingsFromJSON(applicationId, resultJSON);
sharedPrefs.edit()
.putString(settingsKey, resultJSON.toString())
.apply();
}
// Print log to notify developers to upgrade SDK when version is too old
if (appSettings != null) {
String updateMessage = appSettings.getSdkUpdateMessage();
if (!printedSDKUpdatedMessage
&& updateMessage != null
&& updateMessage.length() > 0) {
printedSDKUpdatedMessage = true;
Log.w(TAG, updateMessage);
}
}
// Start log activate & deactivate app events, in case autoLogAppEvents flag is set
AutomaticAnalyticsLogger.logActivateAppEvent();
// Automatically log In App Purchase events
startInAppPurchaseAutoLogging(context);
loadingSettings.set(false);
}
});
}
// This call only gets the app settings if they're already fetched
public static FetchedAppSettings getAppSettingsWithoutQuery(final String applicationId) {
return applicationId != null ? fetchedAppSettings.get(applicationId) : null;
}
// Note that this method makes a synchronous Graph API call, so should not be called from the
// main thread.
public static FetchedAppSettings queryAppSettings(
final String applicationId,
final boolean forceRequery) {
// Cache the last app checked results.
if (!forceRequery && fetchedAppSettings.containsKey(applicationId)) {
return fetchedAppSettings.get(applicationId);
}
JSONObject response = getAppSettingsQueryResponse(applicationId);
if (response == null) {
return null;
}
return parseAppSettingsFromJSON(applicationId, response);
}
private static FetchedAppSettings parseAppSettingsFromJSON(
String applicationId,
JSONObject settingsJSON) {
JSONArray errorClassificationJSON =
settingsJSON.optJSONArray(APP_SETTING_ANDROID_SDK_ERROR_CATEGORIES);
FacebookRequestErrorClassification errorClassification =
errorClassificationJSON == null
? FacebookRequestErrorClassification.getDefaultErrorClassification()
: FacebookRequestErrorClassification.createFromJSON(
errorClassificationJSON
);
int featureBitmask = settingsJSON.optInt(APP_SETTING_APP_EVENTS_FEATURE_BITMASK,0);
boolean automaticLoggingEnabled =
(featureBitmask & AUTOMATIC_LOGGING_ENABLED_BITMASK_FIELD) != 0;
boolean inAppPurchaseAutomaticLoggingEnabled =
(featureBitmask & IAP_AUTOMATIC_LOGGING_ENABLED_BITMASK_FIELD) != 0;
FetchedAppSettings result = new FetchedAppSettings(
settingsJSON.optBoolean(APP_SETTING_SUPPORTS_IMPLICIT_SDK_LOGGING, false),
settingsJSON.optString(APP_SETTING_NUX_CONTENT, ""),
settingsJSON.optBoolean(APP_SETTING_NUX_ENABLED, false),
settingsJSON.optBoolean(APP_SETTING_CUSTOM_TABS_ENABLED, false),
settingsJSON.optInt(
APP_SETTING_APP_EVENTS_SESSION_TIMEOUT,
Constants.getDefaultAppEventsSessionTimeoutInSeconds()),
SmartLoginOption.parseOptions(settingsJSON.optLong(APP_SETTING_SMART_LOGIN_OPTIONS)),
parseDialogConfigurations(settingsJSON.optJSONObject(APP_SETTING_DIALOG_CONFIGS)),
automaticLoggingEnabled,
errorClassification,
settingsJSON.optString(SMART_LOGIN_BOOKMARK_ICON_URL),
settingsJSON.optString(SMART_LOGIN_MENU_ICON_URL),
inAppPurchaseAutomaticLoggingEnabled,
settingsJSON.optString(SDK_UPDATE_MESSAGE)
);
fetchedAppSettings.put(applicationId, result);
return result;
}
// Note that this method makes a synchronous Graph API call, so should not be called from the
// main thread.
private static JSONObject getAppSettingsQueryResponse(String applicationId) {
Bundle appSettingsParams = new Bundle();
ArrayList<String> appSettingFields = new ArrayList<>(Arrays.asList(APP_SETTING_FIELDS));
if (BuildConfig.DEBUG) {
appSettingFields.add(SDK_UPDATE_MESSAGE);
}
appSettingsParams.putString(APPLICATION_FIELDS, TextUtils.join(",", appSettingFields));
GraphRequest request = GraphRequest.newGraphPathRequest(null, applicationId, null);
request.setSkipClientToken(true);
request.setParameters(appSettingsParams);
return request.executeAndWait().getJSONObject();
}
private static Map<String, Map<String, FetchedAppSettings.DialogFeatureConfig>> parseDialogConfigurations(
JSONObject dialogConfigResponse) {
HashMap<String, Map<String, FetchedAppSettings.DialogFeatureConfig>> dialogConfigMap
= new HashMap<String, Map<String, FetchedAppSettings.DialogFeatureConfig>>();
if (dialogConfigResponse != null) {
JSONArray dialogConfigData = dialogConfigResponse.optJSONArray("data");
if (dialogConfigData != null) {
for (int i = 0; i < dialogConfigData.length(); i++) {
FetchedAppSettings.DialogFeatureConfig dialogConfig =
FetchedAppSettings.DialogFeatureConfig.parseDialogConfig(
dialogConfigData.optJSONObject(i));
if (dialogConfig == null) {
continue;
}
String dialogName = dialogConfig.getDialogName();
Map<String, FetchedAppSettings.DialogFeatureConfig> featureMap =
dialogConfigMap.get(dialogName);
if (featureMap == null) {
featureMap = new HashMap<String, FetchedAppSettings.DialogFeatureConfig>();
dialogConfigMap.put(dialogName, featureMap);
}
featureMap.put(dialogConfig.getFeatureName(), dialogConfig);
}
}
}
return dialogConfigMap;
}
private static void startInAppPurchaseAutoLogging(final Context context) {
// Register callback to do auto logging.
CallbackManagerImpl.registerStaticCallback(
CallbackManagerImpl.RequestCodeOffset.InAppPurchase.toRequestCode(),
new CallbackManagerImpl.Callback() {
@Override
public boolean onActivityResult(int resultCode, Intent data) {
final int finalResultCode = resultCode;
final Intent finalData = data;
FacebookSdk.getExecutor().execute(new Runnable() {
@Override
public void run() {
AutomaticAnalyticsLogger.logInAppPurchaseEvent(
context, finalResultCode, finalData);
}
});
return true;
}
});
}
}