discord-jadx/app/src/main/java/androidx/core/graphics/TypefaceCompatUtil.java

209 lines
6.8 KiB
Java
Raw Normal View History

2021-06-27 20:44:35 +00:00
package androidx.core.graphics;
import android.content.Context;
import android.content.res.Resources;
import android.net.Uri;
import android.os.CancellationSignal;
import android.os.ParcelFileDescriptor;
import android.os.Process;
import android.os.StrictMode;
import android.util.Log;
2021-09-29 00:45:08 +00:00
import androidx.annotation.NonNull;
2021-06-27 20:44:35 +00:00
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.annotation.RestrictTo;
2021-09-29 00:45:08 +00:00
import androidx.core.provider.FontsContractCompat;
2021-12-21 23:37:30 +00:00
import b.d.b.a.a;
2021-06-27 20:44:35 +00:00
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
2021-09-29 00:45:08 +00:00
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
2021-06-27 20:44:35 +00:00
@RestrictTo({RestrictTo.Scope.LIBRARY_GROUP_PREFIX})
2022-03-02 20:59:20 +00:00
/* loaded from: classes.dex */
2021-06-27 20:44:35 +00:00
public class TypefaceCompatUtil {
private static final String CACHE_FILE_PREFIX = ".font";
private static final String TAG = "TypefaceCompatUtil";
private TypefaceCompatUtil() {
}
public static void closeQuietly(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException unused) {
}
}
}
@Nullable
@RequiresApi(19)
public static ByteBuffer copyToDirectBuffer(Context context, Resources resources, int i) {
File tempFile = getTempFile(context);
if (tempFile == null) {
return null;
}
try {
if (!copyToFile(tempFile, resources, i)) {
return null;
}
2022-03-02 20:59:20 +00:00
return mmap(tempFile);
2021-06-27 20:44:35 +00:00
} finally {
tempFile.delete();
}
}
public static boolean copyToFile(File file, Resources resources, int i) {
InputStream inputStream;
Throwable th;
try {
inputStream = resources.openRawResource(i);
try {
boolean copyToFile = copyToFile(file, inputStream);
closeQuietly(inputStream);
return copyToFile;
} catch (Throwable th2) {
th = th2;
closeQuietly(inputStream);
throw th;
}
} catch (Throwable th3) {
th = th3;
inputStream = null;
}
}
public static boolean copyToFile(File file, InputStream inputStream) {
Throwable th;
IOException e;
2022-03-02 20:59:20 +00:00
FileOutputStream fileOutputStream;
2021-06-27 20:44:35 +00:00
StrictMode.ThreadPolicy allowThreadDiskWrites = StrictMode.allowThreadDiskWrites();
2022-03-02 20:59:20 +00:00
FileOutputStream fileOutputStream2 = null;
2021-06-27 20:44:35 +00:00
try {
try {
2022-03-02 20:59:20 +00:00
fileOutputStream = new FileOutputStream(file, false);
2021-06-27 20:44:35 +00:00
} catch (IOException e2) {
e = e2;
2022-03-02 20:59:20 +00:00
}
} catch (Throwable th2) {
th = th2;
}
try {
byte[] bArr = new byte[1024];
while (true) {
int read = inputStream.read(bArr);
if (read != -1) {
fileOutputStream.write(bArr, 0, read);
} else {
2021-06-27 20:44:35 +00:00
closeQuietly(fileOutputStream);
StrictMode.setThreadPolicy(allowThreadDiskWrites);
2022-03-02 20:59:20 +00:00
return true;
2021-06-27 20:44:35 +00:00
}
}
} catch (IOException e3) {
e = e3;
2022-03-02 20:59:20 +00:00
fileOutputStream2 = fileOutputStream;
Log.e(TAG, "Error copying resource contents to temp file: " + e.getMessage());
2022-03-02 20:59:20 +00:00
closeQuietly(fileOutputStream2);
2021-06-27 20:44:35 +00:00
StrictMode.setThreadPolicy(allowThreadDiskWrites);
return false;
2022-03-02 20:59:20 +00:00
} catch (Throwable th3) {
th = th3;
fileOutputStream2 = fileOutputStream;
closeQuietly(fileOutputStream2);
StrictMode.setThreadPolicy(allowThreadDiskWrites);
throw th;
2021-06-27 20:44:35 +00:00
}
}
@Nullable
public static File getTempFile(Context context) {
File cacheDir = context.getCacheDir();
if (cacheDir == null) {
return null;
}
2022-03-30 16:43:10 +00:00
StringBuilder R = a.R(CACHE_FILE_PREFIX);
R.append(Process.myPid());
R.append("-");
R.append(Process.myTid());
R.append("-");
String sb = R.toString();
2021-06-27 20:44:35 +00:00
for (int i = 0; i < 100; i++) {
2021-11-08 18:25:28 +00:00
File file = new File(cacheDir, a.p(sb, i));
2022-03-02 20:59:20 +00:00
if (file.createNewFile()) {
return file;
2021-06-27 20:44:35 +00:00
}
}
return null;
}
@Nullable
@RequiresApi(19)
public static ByteBuffer mmap(Context context, CancellationSignal cancellationSignal, Uri uri) {
try {
ParcelFileDescriptor openFileDescriptor = context.getContentResolver().openFileDescriptor(uri, "r", cancellationSignal);
if (openFileDescriptor == null) {
if (openFileDescriptor != null) {
openFileDescriptor.close();
}
return null;
}
2022-03-02 20:59:20 +00:00
FileInputStream fileInputStream = new FileInputStream(openFileDescriptor.getFileDescriptor());
2021-06-27 20:44:35 +00:00
try {
2022-03-02 20:59:20 +00:00
FileChannel channel = fileInputStream.getChannel();
MappedByteBuffer map = channel.map(FileChannel.MapMode.READ_ONLY, 0L, channel.size());
fileInputStream.close();
openFileDescriptor.close();
return map;
} catch (Throwable th) {
2021-06-27 20:44:35 +00:00
try {
fileInputStream.close();
2022-03-02 20:59:20 +00:00
} catch (Throwable th2) {
th.addSuppressed(th2);
2021-06-27 20:44:35 +00:00
}
2022-03-02 20:59:20 +00:00
throw th;
2021-06-27 20:44:35 +00:00
}
} catch (IOException unused) {
return null;
}
}
@Nullable
@RequiresApi(19)
private static ByteBuffer mmap(File file) {
try {
FileInputStream fileInputStream = new FileInputStream(file);
2022-03-02 20:59:20 +00:00
FileChannel channel = fileInputStream.getChannel();
MappedByteBuffer map = channel.map(FileChannel.MapMode.READ_ONLY, 0L, channel.size());
fileInputStream.close();
return map;
2021-06-27 20:44:35 +00:00
} catch (IOException unused) {
return null;
}
}
2021-09-29 00:45:08 +00:00
@NonNull
@RequiresApi(19)
@RestrictTo({RestrictTo.Scope.LIBRARY})
public static Map<Uri, ByteBuffer> readFontInfoIntoByteBuffer(@NonNull Context context, @NonNull FontsContractCompat.FontInfo[] fontInfoArr, @Nullable CancellationSignal cancellationSignal) {
HashMap hashMap = new HashMap();
for (FontsContractCompat.FontInfo fontInfo : fontInfoArr) {
if (fontInfo.getResultCode() == 0) {
Uri uri = fontInfo.getUri();
if (!hashMap.containsKey(uri)) {
hashMap.put(uri, mmap(context, cancellationSignal, uri));
}
}
}
return Collections.unmodifiableMap(hashMap);
}
2021-06-27 20:44:35 +00:00
}