what-the-bank/sources/com/google/firebase/remoteconfig/internal/ConfigFetchHttpClient.java

242 lines
11 KiB
Java
Raw Normal View History

2024-07-27 18:17:47 +07:00
package com.google.firebase.remoteconfig.internal;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import com.google.android.gms.common.util.AndroidUtilsLight;
import com.google.android.gms.common.util.Hex;
import com.google.common.net.HttpHeaders;
import com.google.firebase.crashlytics.internal.common.AbstractSpiCall;
import com.google.firebase.remoteconfig.FirebaseRemoteConfigClientException;
import com.google.firebase.remoteconfig.FirebaseRemoteConfigException;
import com.google.firebase.remoteconfig.FirebaseRemoteConfigServerException;
import com.google.firebase.remoteconfig.internal.ConfigContainer;
import com.google.firebase.remoteconfig.internal.ConfigFetchHandler;
import com.huawei.hms.support.api.entity.common.CommonConstant;
import com.huawei.hms.support.feature.result.CommonConstant;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import o.lKK;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/* loaded from: classes.dex */
public class ConfigFetchHttpClient {
private static final Pattern GMP_APP_ID_PATTERN = Pattern.compile("^[^:]+:([0-9]+):(android|ios|web):([0-9a-f]+)");
private final String apiKey;
private final String appId;
private final long connectTimeoutInSeconds;
private final Context context;
private final String namespace;
private final String projectNumber;
private final long readTimeoutInSeconds;
public ConfigFetchHttpClient(Context context, String str, String str2, String str3, long j, long j2) {
this.context = context;
this.appId = str;
this.apiKey = str2;
this.projectNumber = extractProjectNumberFromAppId(str);
this.namespace = str3;
this.connectTimeoutInSeconds = j;
this.readTimeoutInSeconds = j2;
}
private static String extractProjectNumberFromAppId(String str) {
Matcher matcher = GMP_APP_ID_PATTERN.matcher(str);
if (matcher.matches()) {
return matcher.group(1);
}
return null;
}
/* JADX INFO: Access modifiers changed from: package-private */
public HttpURLConnection createHttpURLConnection() throws FirebaseRemoteConfigException {
try {
return (HttpURLConnection) new URL(getFetchUrl(this.projectNumber, this.namespace)).openConnection();
} catch (IOException e) {
throw new FirebaseRemoteConfigException(e.getMessage());
}
}
/* JADX INFO: Access modifiers changed from: package-private */
public ConfigFetchHandler.FetchResponse fetch(HttpURLConnection httpURLConnection, String str, String str2, Map<String, String> map, String str3, Map<String, String> map2, Date date) throws FirebaseRemoteConfigException {
setUpUrlConnection(httpURLConnection, str3, str2, map2);
try {
try {
setFetchRequestBody(httpURLConnection, createFetchRequestBody(str, str2, map).toString().getBytes("utf-8"));
httpURLConnection.connect();
int responseCode = httpURLConnection.getResponseCode();
if (responseCode != 200) {
throw new FirebaseRemoteConfigServerException(responseCode, httpURLConnection.getResponseMessage());
}
String headerField = httpURLConnection.getHeaderField(HttpHeaders.ETAG);
JSONObject fetchResponseBody = getFetchResponseBody(httpURLConnection);
try {
httpURLConnection.getInputStream().close();
} catch (IOException unused) {
}
if (!backendHasUpdates(fetchResponseBody)) {
return ConfigFetchHandler.FetchResponse.forBackendHasNoUpdates(date);
}
return ConfigFetchHandler.FetchResponse.forBackendUpdatesFetched(extractConfigs(fetchResponseBody, date), headerField);
} finally {
httpURLConnection.disconnect();
try {
httpURLConnection.getInputStream().close();
} catch (IOException unused2) {
}
}
} catch (IOException | JSONException e) {
throw new FirebaseRemoteConfigClientException("The client had an error while calling the backend!", e);
}
}
private void setUpUrlConnection(HttpURLConnection httpURLConnection, String str, String str2, Map<String, String> map) {
httpURLConnection.setDoOutput(true);
httpURLConnection.setConnectTimeout((int) TimeUnit.SECONDS.toMillis(this.connectTimeoutInSeconds));
httpURLConnection.setReadTimeout((int) TimeUnit.SECONDS.toMillis(this.readTimeoutInSeconds));
httpURLConnection.setRequestProperty(HttpHeaders.IF_NONE_MATCH, str);
setCommonRequestHeaders(httpURLConnection, str2);
setCustomRequestHeaders(httpURLConnection, map);
}
private String getFetchUrl(String str, String str2) {
return String.format("https://firebaseremoteconfig.googleapis.com/v1/projects/%s/namespaces/%s:fetch", str, str2);
}
private void setCommonRequestHeaders(HttpURLConnection httpURLConnection, String str) {
httpURLConnection.setRequestProperty("X-Goog-Api-Key", this.apiKey);
httpURLConnection.setRequestProperty("X-Android-Package", this.context.getPackageName());
httpURLConnection.setRequestProperty("X-Android-Cert", getFingerprintHashForPackage());
httpURLConnection.setRequestProperty("X-Google-GFE-Can-Retry", "yes");
httpURLConnection.setRequestProperty("X-Goog-Firebase-Installations-Auth", str);
httpURLConnection.setRequestProperty(HttpHeaders.CONTENT_TYPE, AbstractSpiCall.ACCEPT_JSON_VALUE);
httpURLConnection.setRequestProperty("Accept", AbstractSpiCall.ACCEPT_JSON_VALUE);
}
private void setCustomRequestHeaders(HttpURLConnection httpURLConnection, Map<String, String> map) {
for (Map.Entry<String, String> entry : map.entrySet()) {
httpURLConnection.setRequestProperty(entry.getKey(), entry.getValue());
}
}
private String getFingerprintHashForPackage() {
try {
Context context = this.context;
byte[] packageCertificateHashBytes = AndroidUtilsLight.getPackageCertificateHashBytes(context, context.getPackageName());
if (packageCertificateHashBytes == null) {
return null;
}
return Hex.bytesToStringUppercase(packageCertificateHashBytes, false);
} catch (PackageManager.NameNotFoundException unused) {
return null;
}
}
private JSONObject createFetchRequestBody(String str, String str2, Map<String, String> map) throws FirebaseRemoteConfigClientException {
HashMap hashMap = new HashMap();
if (str == null) {
throw new FirebaseRemoteConfigClientException("Fetch failed: Firebase installation id is null.");
}
hashMap.put("appInstanceId", str);
hashMap.put("appInstanceIdToken", str2);
hashMap.put("appId", this.appId);
Locale locale = this.context.getResources().getConfiguration().locale;
hashMap.put(CommonConstant.KEY_COUNTRY_CODE, locale.getCountry());
hashMap.put("languageCode", locale.toLanguageTag());
hashMap.put("platformVersion", Integer.toString(Build.VERSION.SDK_INT));
hashMap.put("timeZone", TimeZone.getDefault().getID());
try {
PackageInfo packageInfo = this.context.getPackageManager().getPackageInfo(this.context.getPackageName(), 0);
if (packageInfo != null) {
hashMap.put("appVersion", packageInfo.versionName);
hashMap.put("appBuild", Long.toString(lKK.nw_(packageInfo)));
}
} catch (PackageManager.NameNotFoundException unused) {
}
hashMap.put(CommonConstant.ReqAccessTokenParam.PACKAGE_NAME, this.context.getPackageName());
hashMap.put("sdkVersion", "20.0.4");
hashMap.put("analyticsUserProperties", new JSONObject(map));
return new JSONObject(hashMap);
}
private void setFetchRequestBody(HttpURLConnection httpURLConnection, byte[] bArr) throws IOException {
httpURLConnection.setFixedLengthStreamingMode(bArr.length);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(httpURLConnection.getOutputStream());
bufferedOutputStream.write(bArr);
bufferedOutputStream.flush();
bufferedOutputStream.close();
}
private JSONObject getFetchResponseBody(URLConnection uRLConnection) throws IOException, JSONException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(uRLConnection.getInputStream(), "utf-8"));
StringBuilder sb = new StringBuilder();
while (true) {
int read = bufferedReader.read();
if (read != -1) {
sb.append((char) read);
} else {
return new JSONObject(sb.toString());
}
}
}
private boolean backendHasUpdates(JSONObject jSONObject) {
try {
return !jSONObject.get(CommonConstant.ReqAccessTokenParam.STATE_LABEL).equals("NO_CHANGE");
} catch (JSONException unused) {
return true;
}
}
private static ConfigContainer extractConfigs(JSONObject jSONObject, Date date) throws FirebaseRemoteConfigClientException {
JSONObject jSONObject2;
JSONArray jSONArray;
try {
ConfigContainer.Builder withFetchTime = ConfigContainer.newBuilder().withFetchTime(date);
JSONObject jSONObject3 = null;
try {
jSONObject2 = jSONObject.getJSONObject("entries");
} catch (JSONException unused) {
jSONObject2 = null;
}
if (jSONObject2 != null) {
withFetchTime.replaceConfigsWith(jSONObject2);
}
try {
jSONArray = jSONObject.getJSONArray("experimentDescriptions");
} catch (JSONException unused2) {
jSONArray = null;
}
if (jSONArray != null) {
withFetchTime.withAbtExperiments(jSONArray);
}
try {
jSONObject3 = jSONObject.getJSONObject("personalizationMetadata");
} catch (JSONException unused3) {
}
if (jSONObject3 != null) {
withFetchTime.withPersonalizationMetadata(jSONObject3);
}
return withFetchTime.build();
} catch (JSONException e) {
throw new FirebaseRemoteConfigClientException("Fetch failed: fetch response could not be parsed.", e);
}
}
}