package androidx.profileinstaller; import android.content.res.AssetManager; import android.os.Build; import androidx.profileinstaller.ProfileInstaller; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.concurrent.Executor; /* loaded from: classes2.dex */ public class DeviceProfileWriter { private final String mApkName; private final AssetManager mAssetManager; private final File mCurProfile; private final ProfileInstaller.DiagnosticsCallback mDiagnostics; private final Executor mExecutor; private DexProfileData[] mProfile; private final String mProfileMetaSourceLocation; private final String mProfileSourceLocation; private byte[] mTranscodedProfile; private boolean mDeviceSupportsAotProfile = false; private final byte[] mDesiredVersion = desiredVersion(); private static boolean requiresMetadata() { if (Build.VERSION.SDK_INT > 33) { return false; } int i = Build.VERSION.SDK_INT; return i == 32 || i == 33; } private void result(final int i, final Object obj) { this.mExecutor.execute(new Runnable() { // from class: androidx.profileinstaller.DeviceProfileWriter$$ExternalSyntheticLambda0 @Override // java.lang.Runnable public final void run() { DeviceProfileWriter.this.m5307lambda$result$0$androidxprofileinstallerDeviceProfileWriter(i, obj); } }); } /* JADX INFO: Access modifiers changed from: package-private */ /* renamed from: lambda$result$0$androidx-profileinstaller-DeviceProfileWriter, reason: not valid java name */ public /* synthetic */ void m5307lambda$result$0$androidxprofileinstallerDeviceProfileWriter(int i, Object obj) { this.mDiagnostics.onResultReceived(i, obj); } public DeviceProfileWriter(AssetManager assetManager, Executor executor, ProfileInstaller.DiagnosticsCallback diagnosticsCallback, String str, String str2, String str3, File file) { this.mAssetManager = assetManager; this.mExecutor = executor; this.mDiagnostics = diagnosticsCallback; this.mApkName = str; this.mProfileSourceLocation = str2; this.mProfileMetaSourceLocation = str3; this.mCurProfile = file; } public boolean deviceAllowsProfileInstallerAotWrites() { if (this.mDesiredVersion == null) { result(3, Integer.valueOf(Build.VERSION.SDK_INT)); return false; } if (this.mCurProfile.canWrite()) { this.mDeviceSupportsAotProfile = true; return true; } result(4, null); return false; } private void assertDeviceAllowsProfileInstallerAotWritesCalled() { if (!this.mDeviceSupportsAotProfile) { throw new IllegalStateException("This device doesn't support aot. Did you call deviceSupportsAotProfile()?"); } } public DeviceProfileWriter read() { DeviceProfileWriter addMetadata; assertDeviceAllowsProfileInstallerAotWritesCalled(); if (this.mDesiredVersion == null) { return this; } InputStream profileInputStream = getProfileInputStream(this.mAssetManager); if (profileInputStream != null) { this.mProfile = readProfileInternal(profileInputStream); } DexProfileData[] dexProfileDataArr = this.mProfile; return (dexProfileDataArr == null || !requiresMetadata() || (addMetadata = addMetadata(dexProfileDataArr, this.mDesiredVersion)) == null) ? this : addMetadata; } private InputStream openStreamFromAssets(AssetManager assetManager, String str) throws IOException { try { return assetManager.openFd(str).createInputStream(); } catch (FileNotFoundException e) { String message = e.getMessage(); if (message != null && message.contains("compressed")) { this.mDiagnostics.onDiagnosticReceived(5, null); } return null; } } private InputStream getProfileInputStream(AssetManager assetManager) { try { return openStreamFromAssets(assetManager, this.mProfileSourceLocation); } catch (FileNotFoundException e) { this.mDiagnostics.onResultReceived(6, e); return null; } catch (IOException e2) { this.mDiagnostics.onResultReceived(7, e2); return null; } } private DexProfileData[] readProfileInternal(InputStream inputStream) { try { } catch (IOException e) { this.mDiagnostics.onResultReceived(7, e); } try { try { DexProfileData[] readProfile = ProfileTranscoder.readProfile(inputStream, ProfileTranscoder.readHeader(inputStream, ProfileTranscoder.MAGIC_PROF), this.mApkName); try { inputStream.close(); return readProfile; } catch (IOException e2) { this.mDiagnostics.onResultReceived(7, e2); return readProfile; } } catch (Throwable th) { try { inputStream.close(); } catch (IOException e3) { this.mDiagnostics.onResultReceived(7, e3); } throw th; } } catch (IOException e4) { this.mDiagnostics.onResultReceived(7, e4); inputStream.close(); return null; } catch (IllegalStateException e5) { this.mDiagnostics.onResultReceived(8, e5); inputStream.close(); return null; } } private DeviceProfileWriter addMetadata(DexProfileData[] dexProfileDataArr, byte[] bArr) { InputStream openStreamFromAssets; try { openStreamFromAssets = openStreamFromAssets(this.mAssetManager, this.mProfileMetaSourceLocation); } catch (FileNotFoundException e) { this.mDiagnostics.onResultReceived(9, e); } catch (IOException e2) { this.mDiagnostics.onResultReceived(7, e2); } catch (IllegalStateException e3) { this.mProfile = null; this.mDiagnostics.onResultReceived(8, e3); } if (openStreamFromAssets == null) { if (openStreamFromAssets != null) { openStreamFromAssets.close(); } return null; } try { this.mProfile = ProfileTranscoder.readMeta(openStreamFromAssets, ProfileTranscoder.readHeader(openStreamFromAssets, ProfileTranscoder.MAGIC_PROFM), bArr, dexProfileDataArr); if (openStreamFromAssets != null) { openStreamFromAssets.close(); } return this; } catch (Throwable th) { if (openStreamFromAssets != null) { try { openStreamFromAssets.close(); } catch (Throwable th2) { th.addSuppressed(th2); } } throw th; } } public DeviceProfileWriter transcodeIfNeeded() { ByteArrayOutputStream byteArrayOutputStream; DexProfileData[] dexProfileDataArr = this.mProfile; byte[] bArr = this.mDesiredVersion; if (dexProfileDataArr != null && bArr != null) { assertDeviceAllowsProfileInstallerAotWritesCalled(); try { byteArrayOutputStream = new ByteArrayOutputStream(); try { ProfileTranscoder.writeHeader(byteArrayOutputStream, bArr); } catch (Throwable th) { try { byteArrayOutputStream.close(); } catch (Throwable th2) { th.addSuppressed(th2); } throw th; } } catch (IOException e) { this.mDiagnostics.onResultReceived(7, e); } catch (IllegalStateException e2) { this.mDiagnostics.onResultReceived(8, e2); } if (!ProfileTranscoder.transcodeAndWriteBody(byteArrayOutputStream, bArr, dexProfileDataArr)) { this.mDiagnostics.onResultReceived(5, null); this.mProfile = null; byteArrayOutputStream.close(); return this; } this.mTranscodedProfile = byteArrayOutputStream.toByteArray(); byteArrayOutputStream.close(); this.mProfile = null; } return this; } /* JADX WARN: Multi-variable type inference failed */ public boolean write() { byte[] bArr = this.mTranscodedProfile; if (bArr == null) { return false; } assertDeviceAllowsProfileInstallerAotWritesCalled(); try { try { ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bArr); try { FileOutputStream fileOutputStream = new FileOutputStream(this.mCurProfile); try { Encoding.writeAll(byteArrayInputStream, fileOutputStream); result(1, null); fileOutputStream.close(); byteArrayInputStream.close(); return true; } finally { } } catch (Throwable th) { try { byteArrayInputStream.close(); } catch (Throwable th2) { th.addSuppressed(th2); } throw th; } } finally { this.mTranscodedProfile = null; this.mProfile = null; } } catch (FileNotFoundException e) { result(6, e); return false; } catch (IOException e2) { result(7, e2); return false; } } private static byte[] desiredVersion() { if (Build.VERSION.SDK_INT > 33) { return null; } int i = Build.VERSION.SDK_INT; if (i == 32 || i == 33) { return ProfileVersion.V015_S; } return null; } }