96 lines
4.8 KiB
Java
96 lines
4.8 KiB
Java
|
package com.google.android.gms.internal.firebase_ml;
|
||
|
|
||
|
import android.net.Uri;
|
||
|
import android.text.TextUtils;
|
||
|
import com.google.android.gms.common.internal.GmsLogger;
|
||
|
import com.google.android.gms.common.util.IOUtils;
|
||
|
import com.google.common.net.HttpHeaders;
|
||
|
import com.google.firebase.FirebaseApp;
|
||
|
import com.google.firebase.iid.FirebaseInstanceId;
|
||
|
import com.google.firebase.ml.common.FirebaseMLException;
|
||
|
import com.google.firebase.ml.common.modeldownload.FirebaseRemoteModel;
|
||
|
import java.io.IOException;
|
||
|
import java.util.ArrayList;
|
||
|
import javax.net.ssl.HttpsURLConnection;
|
||
|
import org.json.JSONArray;
|
||
|
import org.json.JSONException;
|
||
|
import org.json.JSONObject;
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
/* loaded from: classes2.dex */
|
||
|
public final class zzom {
|
||
|
private static final GmsLogger zzaoz = new GmsLogger("ModelInfoRetriever", "");
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
public static zzop zza(FirebaseApp firebaseApp, FirebaseRemoteModel firebaseRemoteModel, zzon zzonVar) throws FirebaseMLException {
|
||
|
JSONObject jSONObject;
|
||
|
HttpsURLConnection zza = zzou.zza(zza(firebaseApp, firebaseRemoteModel.getModelName(), zzonVar), zzonVar);
|
||
|
if (zza == null) {
|
||
|
return null;
|
||
|
}
|
||
|
String headerField = zza.getHeaderField(HttpHeaders.CONTENT_LOCATION);
|
||
|
String headerField2 = zza.getHeaderField(HttpHeaders.ETAG);
|
||
|
GmsLogger gmsLogger = zzaoz;
|
||
|
String valueOf = String.valueOf(headerField);
|
||
|
gmsLogger.d("ModelInfoRetriever", valueOf.length() != 0 ? "Received download URL: ".concat(valueOf) : new String("Received download URL: "));
|
||
|
if (headerField == null) {
|
||
|
return null;
|
||
|
}
|
||
|
if (headerField2 == null) {
|
||
|
zzonVar.zza(zzmk.MODEL_INFO_DOWNLOAD_NO_HASH, false);
|
||
|
throw new FirebaseMLException("No hash value for the custom model", 13);
|
||
|
}
|
||
|
firebaseRemoteModel.zzce(headerField2);
|
||
|
try {
|
||
|
String str = new String(IOUtils.readInputStreamFully(zza.getInputStream()));
|
||
|
if (TextUtils.isEmpty(str)) {
|
||
|
str = "{}";
|
||
|
}
|
||
|
JSONObject jSONObject2 = new JSONObject(str);
|
||
|
zzok zzokVar = jSONObject2.has("inferenceInfo") ? zzok.AUTOML : zzok.CUSTOM;
|
||
|
if (zzokVar.equals(zzok.AUTOML) && (jSONObject = jSONObject2.getJSONObject("inferenceInfo")) != null) {
|
||
|
JSONArray jSONArray = jSONObject.getJSONArray("labels");
|
||
|
if (jSONArray == null || jSONArray.length() == 0) {
|
||
|
throw new FirebaseMLException("Cannot parse AutoML model's labels from model downloading backend.", 13);
|
||
|
}
|
||
|
ArrayList arrayList = new ArrayList();
|
||
|
for (int i = 0; i < jSONArray.length(); i++) {
|
||
|
arrayList.add(jSONArray.getString(i));
|
||
|
}
|
||
|
zzob.zza(firebaseApp, firebaseRemoteModel.zzmj(), arrayList);
|
||
|
}
|
||
|
return new zzop(firebaseRemoteModel.zzmj(), Uri.parse(headerField), headerField2, zzokVar);
|
||
|
} catch (IOException | JSONException e) {
|
||
|
throw new FirebaseMLException("Failed to parse the model backend response message", 13, e);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private static String zza(FirebaseApp firebaseApp, String str, zzon zzonVar) throws FirebaseMLException {
|
||
|
String gcmSenderId = firebaseApp.getOptions().getGcmSenderId();
|
||
|
if (gcmSenderId == null) {
|
||
|
throw new FirebaseMLException("GCM sender id cannot be null in FirebaseOptions. Please configure FirebaseApp properly.", 9);
|
||
|
}
|
||
|
FirebaseInstanceId firebaseInstanceId = FirebaseInstanceId.getInstance(firebaseApp);
|
||
|
if (firebaseInstanceId == null) {
|
||
|
zzaoz.w("ModelInfoRetriever", "Cannot get a valid instance of FirebaseInstanceId. Cannot retrieve model info.");
|
||
|
return null;
|
||
|
}
|
||
|
String id = firebaseInstanceId.getId();
|
||
|
if (id == null) {
|
||
|
zzaoz.w("ModelInfoRetriever", "Firebase instance id is null. Cannot retrieve model info.");
|
||
|
return null;
|
||
|
}
|
||
|
try {
|
||
|
String token = firebaseInstanceId.getToken(gcmSenderId, "*");
|
||
|
if (token == null) {
|
||
|
zzaoz.w("ModelInfoRetriever", "Firebase instance token is null. Cannot retrieve model info.");
|
||
|
return null;
|
||
|
}
|
||
|
return String.format("https://mlkit.googleapis.com/v1beta1/projects/%s/models/%s/versions/active?key=%s&app_instance_id=%s&app_instance_token=%s", firebaseApp.getOptions().getProjectId(), str, firebaseApp.getOptions().getApiKey(), id, token);
|
||
|
} catch (IOException e) {
|
||
|
zzonVar.zza(zzmk.MODEL_INFO_DOWNLOAD_CONNECTION_FAILED, false);
|
||
|
throw new FirebaseMLException("Failed to get model URL", 13, e);
|
||
|
}
|
||
|
}
|
||
|
}
|