package com.facebook.internal; import android.content.Context; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.util.Log; import androidx.annotation.RestrictTo; import androidx.annotation.VisibleForTesting; import com.facebook.FacebookSdk; import com.facebook.GraphRequest; import com.facebook.internal.FetchedAppGateKeepersManager; import com.facebook.internal.gatekeeper.GateKeeper; import com.facebook.internal.gatekeeper.GateKeeperRuntimeCache; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.Executor; import java.util.concurrent.atomic.AtomicBoolean; import kotlin.jvm.internal.Intrinsics; import kotlin.jvm.internal.Reflection; import kotlin.jvm.internal.StringCompanionObject; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @RestrictTo({RestrictTo.Scope.LIBRARY_GROUP}) /* loaded from: classes2.dex */ public final class FetchedAppGateKeepersManager { private static final String APPLICATION_FIELDS = "fields"; private static final long APPLICATION_GATEKEEPER_CACHE_TIMEOUT = 3600000; private static final String APPLICATION_GATEKEEPER_EDGE = "mobile_sdk_gk"; private static final String APPLICATION_GATEKEEPER_FIELD = "gatekeepers"; private static final String APPLICATION_GRAPH_DATA = "data"; private static final String APPLICATION_PLATFORM = "platform"; private static final String APPLICATION_SDK_VERSION = "sdk_version"; private static final String APP_GATEKEEPERS_PREFS_KEY_FORMAT = "com.facebook.internal.APP_GATEKEEPERS.%s"; private static final String APP_GATEKEEPERS_PREFS_STORE = "com.facebook.internal.preferences.APP_GATEKEEPERS"; private static final String APP_PLATFORM = "android"; private static GateKeeperRuntimeCache gateKeeperRuntimeCache; private static Long timestamp; public static final FetchedAppGateKeepersManager INSTANCE = new FetchedAppGateKeepersManager(); private static final String TAG = Reflection.getOrCreateKotlinClass(FetchedAppGateKeepersManager.class).getSimpleName(); private static final AtomicBoolean isLoading = new AtomicBoolean(false); private static final ConcurrentLinkedQueue callbacks = new ConcurrentLinkedQueue<>(); private static final Map fetchedAppGateKeepers = new ConcurrentHashMap(); public interface Callback { void onCompleted(); } private FetchedAppGateKeepersManager() { } public final void loadAppGateKeepersAsync() { loadAppGateKeepersAsync(null); } public static final synchronized void loadAppGateKeepersAsync(Callback callback) { synchronized (FetchedAppGateKeepersManager.class) { if (callback != null) { try { callbacks.add(callback); } catch (Throwable th) { throw th; } } final String applicationId = FacebookSdk.getApplicationId(); FetchedAppGateKeepersManager fetchedAppGateKeepersManager = INSTANCE; if (fetchedAppGateKeepersManager.isTimestampValid(timestamp) && fetchedAppGateKeepers.containsKey(applicationId)) { fetchedAppGateKeepersManager.pollCallbacks(); return; } final Context applicationContext = FacebookSdk.getApplicationContext(); StringCompanionObject stringCompanionObject = StringCompanionObject.INSTANCE; final String format = String.format(APP_GATEKEEPERS_PREFS_KEY_FORMAT, Arrays.copyOf(new Object[]{applicationId}, 1)); Intrinsics.checkNotNullExpressionValue(format, "java.lang.String.format(format, *args)"); if (applicationContext == null) { return; } JSONObject jSONObject = null; String string = applicationContext.getSharedPreferences(APP_GATEKEEPERS_PREFS_STORE, 0).getString(format, null); if (!Utility.isNullOrEmpty(string)) { try { jSONObject = new JSONObject(string); } catch (JSONException e) { Utility.logd(Utility.LOG_TAG, e); } if (jSONObject != null) { parseAppGateKeepersFromJSON$facebook_core_release(applicationId, jSONObject); } } Executor executor = FacebookSdk.getExecutor(); if (executor == null) { return; } if (isLoading.compareAndSet(false, true)) { executor.execute(new Runnable() { // from class: com.facebook.internal.FetchedAppGateKeepersManager$$ExternalSyntheticLambda0 @Override // java.lang.Runnable public final void run() { FetchedAppGateKeepersManager.m562loadAppGateKeepersAsync$lambda0(applicationId, applicationContext, format); } }); } } } /* JADX INFO: Access modifiers changed from: private */ /* renamed from: loadAppGateKeepersAsync$lambda-0, reason: not valid java name */ public static final void m562loadAppGateKeepersAsync$lambda0(String applicationId, Context context, String gateKeepersKey) { Intrinsics.checkNotNullParameter(applicationId, "$applicationId"); Intrinsics.checkNotNullParameter(context, "$context"); Intrinsics.checkNotNullParameter(gateKeepersKey, "$gateKeepersKey"); FetchedAppGateKeepersManager fetchedAppGateKeepersManager = INSTANCE; JSONObject appGateKeepersQueryResponse = fetchedAppGateKeepersManager.getAppGateKeepersQueryResponse(applicationId); if (appGateKeepersQueryResponse.length() != 0) { parseAppGateKeepersFromJSON$facebook_core_release(applicationId, appGateKeepersQueryResponse); context.getSharedPreferences(APP_GATEKEEPERS_PREFS_STORE, 0).edit().putString(gateKeepersKey, appGateKeepersQueryResponse.toString()).apply(); timestamp = Long.valueOf(System.currentTimeMillis()); } fetchedAppGateKeepersManager.pollCallbacks(); isLoading.set(false); } private final void pollCallbacks() { Handler handler = new Handler(Looper.getMainLooper()); while (true) { ConcurrentLinkedQueue concurrentLinkedQueue = callbacks; if (concurrentLinkedQueue.isEmpty()) { return; } final Callback poll = concurrentLinkedQueue.poll(); if (poll != null) { handler.post(new Runnable() { // from class: com.facebook.internal.FetchedAppGateKeepersManager$$ExternalSyntheticLambda1 @Override // java.lang.Runnable public final void run() { FetchedAppGateKeepersManager.Callback.this.onCompleted(); } }); } } } public static final JSONObject queryAppGateKeepers(String applicationId, boolean z) { Intrinsics.checkNotNullParameter(applicationId, "applicationId"); if (!z) { Map map = fetchedAppGateKeepers; if (map.containsKey(applicationId)) { JSONObject jSONObject = map.get(applicationId); return jSONObject == null ? new JSONObject() : jSONObject; } } JSONObject appGateKeepersQueryResponse = INSTANCE.getAppGateKeepersQueryResponse(applicationId); Context applicationContext = FacebookSdk.getApplicationContext(); StringCompanionObject stringCompanionObject = StringCompanionObject.INSTANCE; String format = String.format(APP_GATEKEEPERS_PREFS_KEY_FORMAT, Arrays.copyOf(new Object[]{applicationId}, 1)); Intrinsics.checkNotNullExpressionValue(format, "java.lang.String.format(format, *args)"); applicationContext.getSharedPreferences(APP_GATEKEEPERS_PREFS_STORE, 0).edit().putString(format, appGateKeepersQueryResponse.toString()).apply(); return parseAppGateKeepersFromJSON$facebook_core_release(applicationId, appGateKeepersQueryResponse); } public final Map getGateKeepersForApplication(String str) { loadAppGateKeepersAsync(); if (str != null) { Map map = fetchedAppGateKeepers; if (map.containsKey(str)) { GateKeeperRuntimeCache gateKeeperRuntimeCache2 = gateKeeperRuntimeCache; List dumpGateKeepers = gateKeeperRuntimeCache2 == null ? null : gateKeeperRuntimeCache2.dumpGateKeepers(str); if (dumpGateKeepers != null) { HashMap hashMap = new HashMap(); for (GateKeeper gateKeeper : dumpGateKeepers) { hashMap.put(gateKeeper.getName(), Boolean.valueOf(gateKeeper.getValue())); } return hashMap; } HashMap hashMap2 = new HashMap(); JSONObject jSONObject = map.get(str); if (jSONObject == null) { jSONObject = new JSONObject(); } Iterator keys = jSONObject.keys(); while (keys.hasNext()) { String key = keys.next(); Intrinsics.checkNotNullExpressionValue(key, "key"); hashMap2.put(key, Boolean.valueOf(jSONObject.optBoolean(key))); } GateKeeperRuntimeCache gateKeeperRuntimeCache3 = gateKeeperRuntimeCache; if (gateKeeperRuntimeCache3 == null) { gateKeeperRuntimeCache3 = new GateKeeperRuntimeCache(); } ArrayList arrayList = new ArrayList(hashMap2.size()); for (Map.Entry entry : hashMap2.entrySet()) { arrayList.add(new GateKeeper((String) entry.getKey(), ((Boolean) entry.getValue()).booleanValue())); } gateKeeperRuntimeCache3.setGateKeepers(str, arrayList); gateKeeperRuntimeCache = gateKeeperRuntimeCache3; return hashMap2; } } return new HashMap(); } public static final boolean getGateKeeperForKey(String name, String str, boolean z) { Boolean bool; Intrinsics.checkNotNullParameter(name, "name"); Map gateKeepersForApplication = INSTANCE.getGateKeepersForApplication(str); return (gateKeepersForApplication.containsKey(name) && (bool = gateKeepersForApplication.get(name)) != null) ? bool.booleanValue() : z; } public static /* synthetic */ void setRuntimeGateKeeper$default(String str, GateKeeper gateKeeper, int i, Object obj) { if ((i & 1) != 0) { str = FacebookSdk.getApplicationId(); } setRuntimeGateKeeper(str, gateKeeper); } public static final void setRuntimeGateKeeper(String applicationId, GateKeeper gateKeeper) { Intrinsics.checkNotNullParameter(applicationId, "applicationId"); Intrinsics.checkNotNullParameter(gateKeeper, "gateKeeper"); GateKeeperRuntimeCache gateKeeperRuntimeCache2 = gateKeeperRuntimeCache; if ((gateKeeperRuntimeCache2 == null ? null : gateKeeperRuntimeCache2.getGateKeeper(applicationId, gateKeeper.getName())) == null) { Log.w(TAG, "Missing gatekeeper runtime cache"); return; } GateKeeperRuntimeCache gateKeeperRuntimeCache3 = gateKeeperRuntimeCache; if (gateKeeperRuntimeCache3 == null) { return; } gateKeeperRuntimeCache3.setGateKeeper(applicationId, gateKeeper); } public static final void resetRuntimeGateKeeperCache() { GateKeeperRuntimeCache gateKeeperRuntimeCache2 = gateKeeperRuntimeCache; if (gateKeeperRuntimeCache2 == null) { return; } GateKeeperRuntimeCache.resetCache$default(gateKeeperRuntimeCache2, null, 1, null); } private final JSONObject getAppGateKeepersQueryResponse(String str) { Bundle bundle = new Bundle(); bundle.putString("platform", "android"); bundle.putString(APPLICATION_SDK_VERSION, FacebookSdk.getSdkVersion()); bundle.putString("fields", APPLICATION_GATEKEEPER_FIELD); GraphRequest.Companion companion = GraphRequest.Companion; StringCompanionObject stringCompanionObject = StringCompanionObject.INSTANCE; String format = String.format("app/%s", Arrays.copyOf(new Object[]{APPLICATION_GATEKEEPER_EDGE}, 1)); Intrinsics.checkNotNullExpressionValue(format, "java.lang.String.format(format, *args)"); GraphRequest newGraphPathRequest = companion.newGraphPathRequest(null, format, null); newGraphPathRequest.setParameters(bundle); JSONObject jsonObject = newGraphPathRequest.executeAndWait().getJsonObject(); return jsonObject == null ? new JSONObject() : jsonObject; } @VisibleForTesting(otherwise = 2) public static final synchronized JSONObject parseAppGateKeepersFromJSON$facebook_core_release(String applicationId, JSONObject jSONObject) { JSONObject jSONObject2; JSONArray optJSONArray; synchronized (FetchedAppGateKeepersManager.class) { try { Intrinsics.checkNotNullParameter(applicationId, "applicationId"); jSONObject2 = fetchedAppGateKeepers.get(applicationId); if (jSONObject2 == null) { jSONObject2 = new JSONObject(); } int i = 0; JSONObject jSONObject3 = null; if (jSONObject != null && (optJSONArray = jSONObject.optJSONArray("data")) != null) { jSONObject3 = optJSONArray.optJSONObject(0); } if (jSONObject3 == null) { jSONObject3 = new JSONObject(); } JSONArray optJSONArray2 = jSONObject3.optJSONArray(APPLICATION_GATEKEEPER_FIELD); if (optJSONArray2 == null) { optJSONArray2 = new JSONArray(); } int length = optJSONArray2.length(); if (length > 0) { while (true) { int i2 = i + 1; try { JSONObject jSONObject4 = optJSONArray2.getJSONObject(i); jSONObject2.put(jSONObject4.getString("key"), jSONObject4.getBoolean("value")); } catch (JSONException e) { Utility.logd(Utility.LOG_TAG, e); } if (i2 >= length) { break; } i = i2; } } fetchedAppGateKeepers.put(applicationId, jSONObject2); } catch (Throwable th) { throw th; } } return jSONObject2; } private final boolean isTimestampValid(Long l) { return l != null && System.currentTimeMillis() - l.longValue() < APPLICATION_GATEKEEPER_CACHE_TIMEOUT; } }