jimu-decompiled/sources/com/google/firebase/iid/zzy.java
2025-05-13 19:24:51 +02:00

340 lines
12 KiB
Java

package com.google.firebase.iid;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import androidx.core.content.ContextCompat;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Properties;
/* loaded from: classes.dex */
final class zzy {
zzy() {
}
private final zzz c(Context context, String str) throws zzaa {
zzz d;
try {
d = d(context, str);
} catch (zzaa e) {
e = e;
}
if (d != null) {
a(context, str, d);
return d;
}
e = null;
try {
zzz a = a(context.getSharedPreferences("com.google.android.gms.appid", 0), str);
if (a != null) {
a(context, str, a, false);
return a;
}
} catch (zzaa e2) {
e = e2;
}
if (e == null) {
return null;
}
throw e;
}
private final zzz d(Context context, String str) throws zzaa {
File e = e(context, str);
if (!e.exists()) {
return null;
}
try {
return a(e);
} catch (zzaa | IOException e2) {
if (Log.isLoggable("FirebaseInstanceId", 3)) {
String valueOf = String.valueOf(e2);
StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 40);
sb.append("Failed to read key from file, retrying: ");
sb.append(valueOf);
Log.d("FirebaseInstanceId", sb.toString());
}
try {
return a(e);
} catch (IOException e3) {
String valueOf2 = String.valueOf(e3);
StringBuilder sb2 = new StringBuilder(String.valueOf(valueOf2).length() + 45);
sb2.append("IID file exists, but failed to read from it: ");
sb2.append(valueOf2);
Log.w("FirebaseInstanceId", sb2.toString());
throw new zzaa(e3);
}
}
}
private static File e(Context context, String str) {
String sb;
if (TextUtils.isEmpty(str)) {
sb = "com.google.InstanceId.properties";
} else {
try {
String encodeToString = Base64.encodeToString(str.getBytes("UTF-8"), 11);
StringBuilder sb2 = new StringBuilder(String.valueOf(encodeToString).length() + 33);
sb2.append("com.google.InstanceId_");
sb2.append(encodeToString);
sb2.append(".properties");
sb = sb2.toString();
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
}
return new File(b(context), sb);
}
final zzz a(Context context, String str) throws zzaa {
zzz c = c(context, str);
return c != null ? c : b(context, str);
}
final zzz b(Context context, String str) {
zzz zzzVar = new zzz(zza.a(), System.currentTimeMillis());
zzz a = a(context, str, zzzVar, true);
if (a != null && !a.equals(zzzVar)) {
if (Log.isLoggable("FirebaseInstanceId", 3)) {
Log.d("FirebaseInstanceId", "Loaded key after generating new one, using loaded one");
}
return a;
}
if (Log.isLoggable("FirebaseInstanceId", 3)) {
Log.d("FirebaseInstanceId", "Generated new key");
}
a(context, str, zzzVar);
return zzzVar;
}
static void a(Context context) {
for (File file : b(context).listFiles()) {
if (file.getName().startsWith("com.google.InstanceId")) {
file.delete();
}
}
}
private static KeyPair a(String str, String str2) throws zzaa {
try {
byte[] decode = Base64.decode(str, 8);
byte[] decode2 = Base64.decode(str2, 8);
try {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return new KeyPair(keyFactory.generatePublic(new X509EncodedKeySpec(decode)), keyFactory.generatePrivate(new PKCS8EncodedKeySpec(decode2)));
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
String valueOf = String.valueOf(e);
StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 19);
sb.append("Invalid key stored ");
sb.append(valueOf);
Log.w("FirebaseInstanceId", sb.toString());
throw new zzaa(e);
}
} catch (IllegalArgumentException e2) {
throw new zzaa(e2);
}
}
private static File b(Context context) {
File b = ContextCompat.b(context);
if (b != null && b.isDirectory()) {
return b;
}
Log.w("FirebaseInstanceId", "noBackupFilesDir doesn't exist, using regular files directory instead");
return context.getFilesDir();
}
private static long b(SharedPreferences sharedPreferences, String str) {
String string = sharedPreferences.getString(zzaw.a(str, "cre"), null);
if (string == null) {
return 0L;
}
try {
return Long.parseLong(string);
} catch (NumberFormatException unused) {
return 0L;
}
}
private final zzz a(Context context, String str, zzz zzzVar, boolean z) {
String b;
String c;
long j;
if (Log.isLoggable("FirebaseInstanceId", 3)) {
Log.d("FirebaseInstanceId", "Writing key to properties file");
}
Properties properties = new Properties();
b = zzzVar.b();
properties.setProperty("pub", b);
c = zzzVar.c();
properties.setProperty("pri", c);
j = zzzVar.b;
properties.setProperty("cre", String.valueOf(j));
File e = e(context, str);
try {
e.createNewFile();
RandomAccessFile randomAccessFile = new RandomAccessFile(e, "rw");
try {
FileChannel channel = randomAccessFile.getChannel();
try {
channel.lock();
if (z && channel.size() > 0) {
try {
channel.position(0L);
zzz a = a(channel);
if (channel != null) {
a((Throwable) null, channel);
}
a((Throwable) null, randomAccessFile);
return a;
} catch (zzaa | IOException e2) {
if (Log.isLoggable("FirebaseInstanceId", 3)) {
String valueOf = String.valueOf(e2);
StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 64);
sb.append("Tried reading key pair before writing new one, but failed with: ");
sb.append(valueOf);
Log.d("FirebaseInstanceId", sb.toString());
}
}
}
channel.position(0L);
properties.store(Channels.newOutputStream(channel), (String) null);
if (channel != null) {
a((Throwable) null, channel);
}
a((Throwable) null, randomAccessFile);
return zzzVar;
} finally {
}
} finally {
}
} catch (IOException e3) {
String valueOf2 = String.valueOf(e3);
StringBuilder sb2 = new StringBuilder(String.valueOf(valueOf2).length() + 21);
sb2.append("Failed to write key: ");
sb2.append(valueOf2);
Log.w("FirebaseInstanceId", sb2.toString());
return null;
}
}
private final zzz a(File file) throws zzaa, IOException {
FileInputStream fileInputStream = new FileInputStream(file);
try {
FileChannel channel = fileInputStream.getChannel();
try {
channel.lock(0L, Long.MAX_VALUE, true);
zzz a = a(channel);
if (channel != null) {
a((Throwable) null, channel);
}
a((Throwable) null, fileInputStream);
return a;
} finally {
}
} finally {
}
}
private static zzz a(FileChannel fileChannel) throws zzaa, IOException {
Properties properties = new Properties();
properties.load(Channels.newInputStream(fileChannel));
String property = properties.getProperty("pub");
String property2 = properties.getProperty("pri");
if (property != null && property2 != null) {
try {
return new zzz(a(property, property2), Long.parseLong(properties.getProperty("cre")));
} catch (NumberFormatException e) {
throw new zzaa(e);
}
}
throw new zzaa("Invalid properties file");
}
private static zzz a(SharedPreferences sharedPreferences, String str) throws zzaa {
String string = sharedPreferences.getString(zzaw.a(str, "|P|"), null);
String string2 = sharedPreferences.getString(zzaw.a(str, "|K|"), null);
if (string == null || string2 == null) {
return null;
}
return new zzz(a(string, string2), b(sharedPreferences, str));
}
private final void a(Context context, String str, zzz zzzVar) {
String b;
String c;
long j;
SharedPreferences sharedPreferences = context.getSharedPreferences("com.google.android.gms.appid", 0);
try {
if (zzzVar.equals(a(sharedPreferences, str))) {
return;
}
} catch (zzaa unused) {
}
if (Log.isLoggable("FirebaseInstanceId", 3)) {
Log.d("FirebaseInstanceId", "Writing key to shared preferences");
}
SharedPreferences.Editor edit = sharedPreferences.edit();
String a = zzaw.a(str, "|P|");
b = zzzVar.b();
edit.putString(a, b);
String a2 = zzaw.a(str, "|K|");
c = zzzVar.c();
edit.putString(a2, c);
String a3 = zzaw.a(str, "cre");
j = zzzVar.b;
edit.putString(a3, String.valueOf(j));
edit.commit();
}
private static /* synthetic */ void a(Throwable th, FileChannel fileChannel) {
if (th == null) {
fileChannel.close();
return;
}
try {
fileChannel.close();
} catch (Throwable th2) {
com.google.android.gms.internal.firebase_messaging.zzc.a(th, th2);
}
}
private static /* synthetic */ void a(Throwable th, RandomAccessFile randomAccessFile) {
if (th == null) {
randomAccessFile.close();
return;
}
try {
randomAccessFile.close();
} catch (Throwable th2) {
com.google.android.gms.internal.firebase_messaging.zzc.a(th, th2);
}
}
private static /* synthetic */ void a(Throwable th, FileInputStream fileInputStream) {
if (th == null) {
fileInputStream.close();
return;
}
try {
fileInputStream.close();
} catch (Throwable th2) {
com.google.android.gms.internal.firebase_messaging.zzc.a(th, th2);
}
}
}