in facebook-core/src/main/java/com/facebook/internal/FetchedAppGateKeepersManager.kt [75:125]
fun loadAppGateKeepersAsync(callback: Callback?) {
if (callback != null) {
callbacks.add(callback)
}
val applicationId = FacebookSdk.getApplicationId()
if (isTimestampValid(timestamp) && fetchedAppGateKeepers.containsKey(applicationId)) {
pollCallbacks()
return
}
val context = FacebookSdk.getApplicationContext()
val gateKeepersKey = String.format(APP_GATEKEEPERS_PREFS_KEY_FORMAT, applicationId)
if (context == null) {
return
}
// See if we had a cached copy of gatekeepers and use that immediately
val gateKeepersSharedPrefs =
context.getSharedPreferences(APP_GATEKEEPERS_PREFS_STORE, Context.MODE_PRIVATE)
val gateKeepersJSONString = gateKeepersSharedPrefs.getString(gateKeepersKey, null)
if (!Utility.isNullOrEmpty(gateKeepersJSONString)) {
var gateKeepersJSON: JSONObject? = null
try {
gateKeepersJSON = JSONObject(gateKeepersJSONString)
} catch (je: JSONException) {
Utility.logd(Utility.LOG_TAG, je)
}
if (gateKeepersJSON != null) {
parseAppGateKeepersFromJSON(applicationId, gateKeepersJSON)
}
}
val executor = FacebookSdk.getExecutor() ?: return
if (!isLoading.compareAndSet(false, true)) {
return
}
executor.execute {
val gateKeepersResultJSON = getAppGateKeepersQueryResponse(applicationId)
if (gateKeepersResultJSON.length() != 0) {
parseAppGateKeepersFromJSON(applicationId, gateKeepersResultJSON)
val gateKeepersSharedPrefs =
context.getSharedPreferences(APP_GATEKEEPERS_PREFS_STORE, Context.MODE_PRIVATE)
gateKeepersSharedPrefs
.edit()
.putString(gateKeepersKey, gateKeepersResultJSON.toString())
.apply()
// Update timestamp only when the GKs are successfully fetched and stored
timestamp = System.currentTimeMillis()
}
pollCallbacks()
isLoading.set(false)
}
}