package kh;

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/* loaded from: classes4.dex */
class e {

    /* renamed from: f, reason: collision with root package name */
    private static final a f25847f = new a();

    /* renamed from: a, reason: collision with root package name */
    private final a f25848a;

    /* renamed from: b, reason: collision with root package name */
    private final d f25849b;

    /* renamed from: c, reason: collision with root package name */
    private final mh.b f25850c;

    /* renamed from: d, reason: collision with root package name */
    private final ContentResolver f25851d;

    /* renamed from: e, reason: collision with root package name */
    private final List f25852e;

    /* JADX INFO: Access modifiers changed from: package-private */
    public e(List list, d dVar, mh.b bVar, ContentResolver contentResolver) {
        this(list, f25847f, dVar, bVar, contentResolver);
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:28:0x004b  */
    /* JADX WARN: Type inference failed for: r1v0 */
    /* JADX WARN: Type inference failed for: r1v1, types: [android.database.Cursor] */
    /* JADX WARN: Type inference failed for: r1v2 */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    private String b(Uri uri) {
        Cursor cursor;
        ?? r12 = 0;
        try {
            try {
                cursor = this.f25849b.a(uri);
                if (cursor != null) {
                    try {
                        if (cursor.moveToFirst()) {
                            String string = cursor.getString(0);
                            cursor.close();
                            return string;
                        }
                    } catch (SecurityException e10) {
                        e = e10;
                        if (Log.isLoggable("ThumbStreamOpener", 3)) {
                            Log.d("ThumbStreamOpener", "Failed to query for thumbnail for Uri: " + uri, e);
                        }
                        if (cursor != null) {
                            cursor.close();
                        }
                        return null;
                    }
                }
                if (cursor != null) {
                    cursor.close();
                }
                return null;
            } catch (Throwable th2) {
                th = th2;
                r12 = this;
                if (r12 != 0) {
                    r12.close();
                }
                throw th;
            }
        } catch (SecurityException e11) {
            e = e11;
            cursor = null;
        } catch (Throwable th3) {
            th = th3;
            if (r12 != 0) {
            }
            throw th;
        }
    }

    private boolean c(File file) {
        return this.f25848a.a(file) && 0 < this.f25848a.c(file);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public int a(Uri uri) {
        InputStream inputStream = null;
        try {
            try {
                inputStream = this.f25851d.openInputStream(uri);
                int b10 = com.bumptech.glide.load.a.b(this.f25852e, inputStream, this.f25850c);
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException unused) {
                    }
                }
                return b10;
            } catch (Throwable th2) {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException unused2) {
                    }
                }
                throw th2;
            }
        } catch (IOException | NullPointerException e10) {
            if (Log.isLoggable("ThumbStreamOpener", 3)) {
                Log.d("ThumbStreamOpener", "Failed to open uri: " + uri, e10);
            }
            if (inputStream == null) {
                return -1;
            }
            try {
                inputStream.close();
                return -1;
            } catch (IOException unused3) {
                return -1;
            }
        }
    }

    public InputStream d(Uri uri) {
        String b10 = b(uri);
        if (TextUtils.isEmpty(b10)) {
            return null;
        }
        File b11 = this.f25848a.b(b10);
        if (!c(b11)) {
            return null;
        }
        Uri fromFile = Uri.fromFile(b11);
        try {
            return this.f25851d.openInputStream(fromFile);
        } catch (NullPointerException e10) {
            throw ((FileNotFoundException) new FileNotFoundException("NPE opening uri: " + uri + " -> " + fromFile).initCause(e10));
        }
    }

    e(List list, a aVar, d dVar, mh.b bVar, ContentResolver contentResolver) {
        this.f25848a = aVar;
        this.f25849b = dVar;
        this.f25850c = bVar;
        this.f25851d = contentResolver;
        this.f25852e = list;
    }
}
