jimu-decompiled/sources/androidx/core/provider/FontsContractCompat.java
2025-05-13 19:24:51 +02:00

336 lines
13 KiB
Java

package androidx.core.provider;
import android.content.ContentUris;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.ProviderInfo;
import android.content.pm.Signature;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Build;
import android.os.CancellationSignal;
import android.os.Handler;
import androidx.collection.LruCache;
import androidx.collection.SimpleArrayMap;
import androidx.core.content.res.FontResourcesParserCompat;
import androidx.core.content.res.ResourcesCompat;
import androidx.core.graphics.TypefaceCompat;
import androidx.core.graphics.TypefaceCompatUtil;
import androidx.core.provider.SelfDestructiveThread;
import androidx.core.util.Preconditions;
import com.liulishuo.filedownloader.model.FileDownloadModel;
import com.thoughtworks.xstream.XStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
/* loaded from: classes.dex */
public class FontsContractCompat {
static final LruCache<String, Typeface> a = new LruCache<>(16);
private static final SelfDestructiveThread b = new SelfDestructiveThread("fonts", 10, XStream.PRIORITY_VERY_HIGH);
static final Object c = new Object();
static final SimpleArrayMap<String, ArrayList<SelfDestructiveThread.ReplyCallback<TypefaceResult>>> d = new SimpleArrayMap<>();
private static final Comparator<byte[]> e = new Comparator<byte[]>() { // from class: androidx.core.provider.FontsContractCompat.5
@Override // java.util.Comparator
/* renamed from: a, reason: merged with bridge method [inline-methods] */
public int compare(byte[] bArr, byte[] bArr2) {
int i;
int i2;
if (bArr.length == bArr2.length) {
for (int i3 = 0; i3 < bArr.length; i3++) {
if (bArr[i3] != bArr2[i3]) {
i = bArr[i3];
i2 = bArr2[i3];
}
}
return 0;
}
i = bArr.length;
i2 = bArr2.length;
return i - i2;
}
};
public static class FontFamilyResult {
private final int a;
private final FontInfo[] b;
public FontFamilyResult(int i, FontInfo[] fontInfoArr) {
this.a = i;
this.b = fontInfoArr;
}
public FontInfo[] a() {
return this.b;
}
public int b() {
return this.a;
}
}
public static class FontInfo {
private final Uri a;
private final int b;
private final int c;
private final boolean d;
private final int e;
public FontInfo(Uri uri, int i, int i2, boolean z, int i3) {
Preconditions.a(uri);
this.a = uri;
this.b = i;
this.c = i2;
this.d = z;
this.e = i3;
}
public int a() {
return this.e;
}
public int b() {
return this.b;
}
public Uri c() {
return this.a;
}
public int d() {
return this.c;
}
public boolean e() {
return this.d;
}
}
private static final class TypefaceResult {
final Typeface a;
final int b;
TypefaceResult(Typeface typeface, int i) {
this.a = typeface;
this.b = i;
}
}
static TypefaceResult a(Context context, FontRequest fontRequest, int i) {
try {
FontFamilyResult a2 = a(context, (CancellationSignal) null, fontRequest);
if (a2.b() != 0) {
return new TypefaceResult(null, a2.b() == 1 ? -2 : -3);
}
Typeface a3 = TypefaceCompat.a(context, null, a2.a(), i);
return new TypefaceResult(a3, a3 != null ? 0 : -3);
} catch (PackageManager.NameNotFoundException unused) {
return new TypefaceResult(null, -1);
}
}
public static Typeface a(final Context context, final FontRequest fontRequest, final ResourcesCompat.FontCallback fontCallback, final Handler handler, boolean z, int i, final int i2) {
final String str = fontRequest.c() + "-" + i2;
Typeface b2 = a.b(str);
if (b2 != null) {
if (fontCallback != null) {
fontCallback.a(b2);
}
return b2;
}
if (z && i == -1) {
TypefaceResult a2 = a(context, fontRequest, i2);
if (fontCallback != null) {
int i3 = a2.b;
if (i3 == 0) {
fontCallback.a(a2.a, handler);
} else {
fontCallback.a(i3, handler);
}
}
return a2.a;
}
Callable<TypefaceResult> callable = new Callable<TypefaceResult>() { // from class: androidx.core.provider.FontsContractCompat.1
/* JADX WARN: Can't rename method to resolve collision */
@Override // java.util.concurrent.Callable
public TypefaceResult call() throws Exception {
TypefaceResult a3 = FontsContractCompat.a(context, fontRequest, i2);
Typeface typeface = a3.a;
if (typeface != null) {
FontsContractCompat.a.a(str, typeface);
}
return a3;
}
};
if (z) {
try {
return ((TypefaceResult) b.a(callable, i)).a;
} catch (InterruptedException unused) {
return null;
}
}
SelfDestructiveThread.ReplyCallback<TypefaceResult> replyCallback = fontCallback == null ? null : new SelfDestructiveThread.ReplyCallback<TypefaceResult>() { // from class: androidx.core.provider.FontsContractCompat.2
@Override // androidx.core.provider.SelfDestructiveThread.ReplyCallback
public void a(TypefaceResult typefaceResult) {
if (typefaceResult == null) {
ResourcesCompat.FontCallback.this.a(1, handler);
return;
}
int i4 = typefaceResult.b;
if (i4 == 0) {
ResourcesCompat.FontCallback.this.a(typefaceResult.a, handler);
} else {
ResourcesCompat.FontCallback.this.a(i4, handler);
}
}
};
synchronized (c) {
if (d.containsKey(str)) {
if (replyCallback != null) {
d.get(str).add(replyCallback);
}
return null;
}
if (replyCallback != null) {
ArrayList<SelfDestructiveThread.ReplyCallback<TypefaceResult>> arrayList = new ArrayList<>();
arrayList.add(replyCallback);
d.put(str, arrayList);
}
b.a(callable, new SelfDestructiveThread.ReplyCallback<TypefaceResult>() { // from class: androidx.core.provider.FontsContractCompat.3
@Override // androidx.core.provider.SelfDestructiveThread.ReplyCallback
public void a(TypefaceResult typefaceResult) {
synchronized (FontsContractCompat.c) {
ArrayList<SelfDestructiveThread.ReplyCallback<TypefaceResult>> arrayList2 = FontsContractCompat.d.get(str);
if (arrayList2 == null) {
return;
}
FontsContractCompat.d.remove(str);
for (int i4 = 0; i4 < arrayList2.size(); i4++) {
arrayList2.get(i4).a(typefaceResult);
}
}
}
});
return null;
}
}
public static Map<Uri, ByteBuffer> a(Context context, FontInfo[] fontInfoArr, CancellationSignal cancellationSignal) {
HashMap hashMap = new HashMap();
for (FontInfo fontInfo : fontInfoArr) {
if (fontInfo.a() == 0) {
Uri c2 = fontInfo.c();
if (!hashMap.containsKey(c2)) {
hashMap.put(c2, TypefaceCompatUtil.a(context, cancellationSignal, c2));
}
}
}
return Collections.unmodifiableMap(hashMap);
}
public static FontFamilyResult a(Context context, CancellationSignal cancellationSignal, FontRequest fontRequest) throws PackageManager.NameNotFoundException {
ProviderInfo a2 = a(context.getPackageManager(), fontRequest, context.getResources());
if (a2 == null) {
return new FontFamilyResult(1, null);
}
return new FontFamilyResult(0, a(context, fontRequest, a2.authority, cancellationSignal));
}
public static ProviderInfo a(PackageManager packageManager, FontRequest fontRequest, Resources resources) throws PackageManager.NameNotFoundException {
String d2 = fontRequest.d();
ProviderInfo resolveContentProvider = packageManager.resolveContentProvider(d2, 0);
if (resolveContentProvider != null) {
if (resolveContentProvider.packageName.equals(fontRequest.e())) {
List<byte[]> a2 = a(packageManager.getPackageInfo(resolveContentProvider.packageName, 64).signatures);
Collections.sort(a2, e);
List<List<byte[]>> a3 = a(fontRequest, resources);
for (int i = 0; i < a3.size(); i++) {
ArrayList arrayList = new ArrayList(a3.get(i));
Collections.sort(arrayList, e);
if (a(a2, arrayList)) {
return resolveContentProvider;
}
}
return null;
}
throw new PackageManager.NameNotFoundException("Found content provider " + d2 + ", but package was not " + fontRequest.e());
}
throw new PackageManager.NameNotFoundException("No package found for authority: " + d2);
}
private static List<List<byte[]>> a(FontRequest fontRequest, Resources resources) {
if (fontRequest.a() != null) {
return fontRequest.a();
}
return FontResourcesParserCompat.a(resources, fontRequest.b());
}
private static boolean a(List<byte[]> list, List<byte[]> list2) {
if (list.size() != list2.size()) {
return false;
}
for (int i = 0; i < list.size(); i++) {
if (!Arrays.equals(list.get(i), list2.get(i))) {
return false;
}
}
return true;
}
private static List<byte[]> a(Signature[] signatureArr) {
ArrayList arrayList = new ArrayList();
for (Signature signature : signatureArr) {
arrayList.add(signature.toByteArray());
}
return arrayList;
}
static FontInfo[] a(Context context, FontRequest fontRequest, String str, CancellationSignal cancellationSignal) {
Uri withAppendedId;
ArrayList arrayList = new ArrayList();
Uri build = new Uri.Builder().scheme("content").authority(str).build();
Uri build2 = new Uri.Builder().scheme("content").authority(str).appendPath("file").build();
Cursor cursor = null;
try {
if (Build.VERSION.SDK_INT > 16) {
cursor = context.getContentResolver().query(build, new String[]{FileDownloadModel.ID, "file_id", "font_ttc_index", "font_variation_settings", "font_weight", "font_italic", "result_code"}, "query = ?", new String[]{fontRequest.f()}, null, cancellationSignal);
} else {
cursor = context.getContentResolver().query(build, new String[]{FileDownloadModel.ID, "file_id", "font_ttc_index", "font_variation_settings", "font_weight", "font_italic", "result_code"}, "query = ?", new String[]{fontRequest.f()}, null);
}
if (cursor != null && cursor.getCount() > 0) {
int columnIndex = cursor.getColumnIndex("result_code");
ArrayList arrayList2 = new ArrayList();
int columnIndex2 = cursor.getColumnIndex(FileDownloadModel.ID);
int columnIndex3 = cursor.getColumnIndex("file_id");
int columnIndex4 = cursor.getColumnIndex("font_ttc_index");
int columnIndex5 = cursor.getColumnIndex("font_weight");
int columnIndex6 = cursor.getColumnIndex("font_italic");
while (cursor.moveToNext()) {
int i = columnIndex != -1 ? cursor.getInt(columnIndex) : 0;
int i2 = columnIndex4 != -1 ? cursor.getInt(columnIndex4) : 0;
if (columnIndex3 == -1) {
withAppendedId = ContentUris.withAppendedId(build, cursor.getLong(columnIndex2));
} else {
withAppendedId = ContentUris.withAppendedId(build2, cursor.getLong(columnIndex3));
}
arrayList2.add(new FontInfo(withAppendedId, i2, columnIndex5 != -1 ? cursor.getInt(columnIndex5) : 400, columnIndex6 != -1 && cursor.getInt(columnIndex6) == 1, i));
}
arrayList = arrayList2;
}
return (FontInfo[]) arrayList.toArray(new FontInfo[0]);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
}