package com.facebook.internal; import android.graphics.Bitmap; import android.net.Uri; import android.util.Log; import com.facebook.FacebookContentProvider; import com.facebook.FacebookException; import com.facebook.FacebookSdk; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.ArrayList; import java.util.Collection; import java.util.UUID; import kotlin.io.FilesKt__UtilsKt; import kotlin.jvm.internal.Intrinsics; import kotlin.text.StringsKt__StringsJVMKt; /* loaded from: classes2.dex */ public final class NativeAppCallAttachmentStore { public static final String ATTACHMENTS_DIR_NAME = "com.facebook.NativeAppCallAttachmentStore.files"; public static final NativeAppCallAttachmentStore INSTANCE = new NativeAppCallAttachmentStore(); private static final String TAG = NativeAppCallAttachmentStore.class.getName(); private static File attachmentsDirectory; private NativeAppCallAttachmentStore() { } public static final Attachment createAttachment(UUID callId, Bitmap attachmentBitmap) { Intrinsics.checkNotNullParameter(callId, "callId"); Intrinsics.checkNotNullParameter(attachmentBitmap, "attachmentBitmap"); return new Attachment(callId, attachmentBitmap, null); } public static final Attachment createAttachment(UUID callId, Uri attachmentUri) { Intrinsics.checkNotNullParameter(callId, "callId"); Intrinsics.checkNotNullParameter(attachmentUri, "attachmentUri"); return new Attachment(callId, null, attachmentUri); } private final void processAttachmentBitmap(Bitmap bitmap, File file) throws IOException { FileOutputStream fileOutputStream = new FileOutputStream(file); try { bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream); } finally { Utility.closeQuietly(fileOutputStream); } } private final void processAttachmentFile(Uri uri, boolean z, File file) throws IOException { InputStream openInputStream; FileOutputStream fileOutputStream = new FileOutputStream(file); try { if (!z) { openInputStream = new FileInputStream(uri.getPath()); } else { openInputStream = FacebookSdk.getApplicationContext().getContentResolver().openInputStream(uri); } Utility.copyAndCloseInputStream(openInputStream, fileOutputStream); Utility.closeQuietly(fileOutputStream); } catch (Throwable th) { Utility.closeQuietly(fileOutputStream); throw th; } } public static final void addAttachments(Collection collection) throws FacebookException { File attachmentFile; if (collection == null || collection.isEmpty()) { return; } if (attachmentsDirectory == null) { cleanupAllAttachments(); } ensureAttachmentsDirectoryExists(); ArrayList arrayList = new ArrayList(); try { for (Attachment attachment : collection) { if (attachment.getShouldCreateFile() && (attachmentFile = getAttachmentFile(attachment.getCallId(), attachment.getAttachmentName(), true)) != null) { arrayList.add(attachmentFile); if (attachment.getBitmap() != null) { INSTANCE.processAttachmentBitmap(attachment.getBitmap(), attachmentFile); } else if (attachment.getOriginalUri() != null) { INSTANCE.processAttachmentFile(attachment.getOriginalUri(), attachment.isContentUri(), attachmentFile); } } } } catch (IOException e) { Log.e(TAG, Intrinsics.stringPlus("Got unexpected exception:", e)); for (File file : arrayList) { if (file != null) { try { file.delete(); } catch (Exception unused) { } } } throw new FacebookException(e); } } public static final void cleanupAttachmentsForCall(UUID callId) { Intrinsics.checkNotNullParameter(callId, "callId"); File attachmentsDirectoryForCall = getAttachmentsDirectoryForCall(callId, false); if (attachmentsDirectoryForCall == null) { return; } FilesKt__UtilsKt.deleteRecursively(attachmentsDirectoryForCall); } public static final File openAttachment(UUID uuid, String str) throws FileNotFoundException { if (Utility.isNullOrEmpty(str) || uuid == null) { throw new FileNotFoundException(); } try { return getAttachmentFile(uuid, str, false); } catch (IOException unused) { throw new FileNotFoundException(); } } public static final synchronized File getAttachmentsDirectory() { File file; synchronized (NativeAppCallAttachmentStore.class) { try { if (attachmentsDirectory == null) { attachmentsDirectory = new File(FacebookSdk.getApplicationContext().getCacheDir(), ATTACHMENTS_DIR_NAME); } file = attachmentsDirectory; } catch (Throwable th) { throw th; } } return file; } public static final File ensureAttachmentsDirectoryExists() { File attachmentsDirectory2 = getAttachmentsDirectory(); if (attachmentsDirectory2 != null) { attachmentsDirectory2.mkdirs(); } return attachmentsDirectory2; } public static final File getAttachmentsDirectoryForCall(UUID callId, boolean z) { Intrinsics.checkNotNullParameter(callId, "callId"); if (attachmentsDirectory == null) { return null; } File file = new File(attachmentsDirectory, callId.toString()); if (z && !file.exists()) { file.mkdirs(); } return file; } public static final File getAttachmentFile(UUID callId, String str, boolean z) throws IOException { Intrinsics.checkNotNullParameter(callId, "callId"); File attachmentsDirectoryForCall = getAttachmentsDirectoryForCall(callId, z); if (attachmentsDirectoryForCall == null) { return null; } try { return new File(attachmentsDirectoryForCall, URLEncoder.encode(str, "UTF-8")); } catch (UnsupportedEncodingException unused) { return null; } } public static final void cleanupAllAttachments() { File attachmentsDirectory2 = getAttachmentsDirectory(); if (attachmentsDirectory2 == null) { return; } FilesKt__UtilsKt.deleteRecursively(attachmentsDirectory2); } public static final class Attachment { private final String attachmentName; private final String attachmentUrl; private final Bitmap bitmap; private final UUID callId; private boolean isContentUri; private final Uri originalUri; private boolean shouldCreateFile; public final String getAttachmentName() { return this.attachmentName; } public final String getAttachmentUrl() { return this.attachmentUrl; } public final Bitmap getBitmap() { return this.bitmap; } public final UUID getCallId() { return this.callId; } public final Uri getOriginalUri() { return this.originalUri; } public final boolean getShouldCreateFile() { return this.shouldCreateFile; } public final boolean isContentUri() { return this.isContentUri; } public final void setContentUri(boolean z) { this.isContentUri = z; } public final void setShouldCreateFile(boolean z) { this.shouldCreateFile = z; } public Attachment(UUID callId, Bitmap bitmap, Uri uri) { String attachmentUrl; boolean equals; boolean equals2; Intrinsics.checkNotNullParameter(callId, "callId"); this.callId = callId; this.bitmap = bitmap; this.originalUri = uri; if (uri != null) { String scheme = uri.getScheme(); equals = StringsKt__StringsJVMKt.equals("content", scheme, true); if (equals) { this.isContentUri = true; String authority = uri.getAuthority(); this.shouldCreateFile = (authority == null || StringsKt__StringsJVMKt.startsWith$default(authority, "media", false, 2, null)) ? false : true; } else { equals2 = StringsKt__StringsJVMKt.equals("file", uri.getScheme(), true); if (equals2) { this.shouldCreateFile = true; } else if (!Utility.isWebUri(uri)) { throw new FacebookException(Intrinsics.stringPlus("Unsupported scheme for media Uri : ", scheme)); } } } else { if (bitmap == null) { throw new FacebookException("Cannot share media without a bitmap or Uri set"); } this.shouldCreateFile = true; } String uuid = this.shouldCreateFile ? UUID.randomUUID().toString() : null; this.attachmentName = uuid; if (!this.shouldCreateFile) { attachmentUrl = String.valueOf(uri); } else { attachmentUrl = FacebookContentProvider.Companion.getAttachmentUrl(FacebookSdk.getApplicationId(), callId, uuid); } this.attachmentUrl = attachmentUrl; } } }