package h5;

import a5.l;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.TextUtils;
import androidx.annotation.Nullable;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.DataSource;
import g5.a0;
import g5.b0;
import java.io.File;
import java.io.FileNotFoundException;

/* JADX INFO: loaded from: classes.dex */
public final class e implements com.bumptech.glide.load.data.e {

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public static final String[] f26433j = {"_data"};

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final Context f26434a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final b0 f26435b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final b0 f26436c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final Uri f26437d;

    @Nullable
    private volatile com.bumptech.glide.load.data.e delegate;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final int f26438e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final int f26439f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final l f26440g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final Class f26441h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public volatile boolean f26442i;

    public e(Context context, b0 b0Var, b0 b0Var2, Uri uri, int i10, int i11, l lVar, Class cls) {
        this.f26434a = context.getApplicationContext();
        this.f26435b = b0Var;
        this.f26436c = b0Var2;
        this.f26437d = uri;
        this.f26438e = i10;
        this.f26439f = i11;
        this.f26440g = lVar;
        this.f26441h = cls;
    }

    @Nullable
    private a0 buildDelegateData() throws FileNotFoundException {
        boolean zIsExternalStorageLegacy = Environment.isExternalStorageLegacy();
        l lVar = this.f26440g;
        int i10 = this.f26439f;
        int i11 = this.f26438e;
        Context context = this.f26434a;
        if (!zIsExternalStorageLegacy) {
            int iCheckSelfPermission = context.checkSelfPermission("android.permission.ACCESS_MEDIA_LOCATION");
            Uri requireOriginal = this.f26437d;
            if (iCheckSelfPermission == 0) {
                requireOriginal = MediaStore.setRequireOriginal(requireOriginal);
            }
            return this.f26436c.buildLoadData(requireOriginal, i11, i10, lVar);
        }
        Uri uri = this.f26437d;
        Cursor cursor = null;
        try {
            Cursor cursorQuery = context.getContentResolver().query(uri, f26433j, null, null, null);
            if (cursorQuery == null || !cursorQuery.moveToFirst()) {
                throw new FileNotFoundException("Failed to media store entry for: " + uri);
            }
            String string = cursorQuery.getString(cursorQuery.getColumnIndexOrThrow("_data"));
            if (TextUtils.isEmpty(string)) {
                throw new FileNotFoundException("File path was empty in media store for: " + uri);
            }
            File file = new File(string);
            cursorQuery.close();
            return this.f26435b.buildLoadData(file, i11, i10, lVar);
        } catch (Throwable th) {
            if (0 != 0) {
                cursor.close();
            }
            throw th;
        }
    }

    @Nullable
    private com.bumptech.glide.load.data.e buildDelegateFetcher() throws FileNotFoundException {
        a0 a0VarBuildDelegateData = buildDelegateData();
        if (a0VarBuildDelegateData != null) {
            return a0VarBuildDelegateData.f26167c;
        }
        return null;
    }

    @Override // com.bumptech.glide.load.data.e
    public final Class a() {
        return this.f26441h;
    }

    @Override // com.bumptech.glide.load.data.e
    public final void b() {
        com.bumptech.glide.load.data.e eVar = this.delegate;
        if (eVar != null) {
            eVar.b();
        }
    }

    @Override // com.bumptech.glide.load.data.e
    public final void cancel() {
        this.f26442i = true;
        com.bumptech.glide.load.data.e eVar = this.delegate;
        if (eVar != null) {
            eVar.cancel();
        }
    }

    @Override // com.bumptech.glide.load.data.e
    public final DataSource d() {
        return DataSource.f5589a;
    }

    @Override // com.bumptech.glide.load.data.e
    public final void e(Priority priority, com.bumptech.glide.load.data.d dVar) {
        try {
            com.bumptech.glide.load.data.e eVarBuildDelegateFetcher = buildDelegateFetcher();
            if (eVarBuildDelegateFetcher == null) {
                dVar.c(new IllegalArgumentException("Failed to build fetcher for: " + this.f26437d));
            } else {
                this.delegate = eVarBuildDelegateFetcher;
                if (this.f26442i) {
                    cancel();
                } else {
                    eVarBuildDelegateFetcher.e(priority, dVar);
                }
            }
        } catch (FileNotFoundException e10) {
            dVar.c(e10);
        }
    }
}
