package net.gotev.uploadservice.schemehandlers;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import net.gotev.uploadservice.ContentType;
import net.gotev.uploadservice.Logger;
/* loaded from: classes.dex */
class ContentSchemeHandler implements SchemeHandler {
    private Uri uri;

    ContentSchemeHandler() {
    }

    @Override // net.gotev.uploadservice.schemehandlers.SchemeHandler
    public void init(String str) {
        this.uri = Uri.parse(str);
    }

    @Override // net.gotev.uploadservice.schemehandlers.SchemeHandler
    public long getLength(Context context) {
        return getUriSize(context);
    }

    @Override // net.gotev.uploadservice.schemehandlers.SchemeHandler
    public InputStream getInputStream(Context context) throws FileNotFoundException {
        return context.getContentResolver().openInputStream(this.uri);
    }

    @Override // net.gotev.uploadservice.schemehandlers.SchemeHandler
    public String getContentType(Context context) {
        String type = context.getContentResolver().getType(this.uri);
        return (type == null || type.isEmpty()) ? ContentType.APPLICATION_OCTET_STREAM : type;
    }

    @Override // net.gotev.uploadservice.schemehandlers.SchemeHandler
    public String getName(Context context) {
        return getUriName(context);
    }

    private long getUriSize(Context context) {
        Cursor query = context.getContentResolver().query(this.uri, null, null, null, null);
        if (query == null) {
            String simpleName = getClass().getSimpleName();
            Logger.error(simpleName, "null cursor for " + this.uri + ", returning size 0");
            return 0L;
        }
        int columnIndex = query.getColumnIndex("_size");
        query.moveToFirst();
        long j = query.getLong(columnIndex);
        query.close();
        return j;
    }

    private String getUriName(Context context) {
        Cursor query = context.getContentResolver().query(this.uri, null, null, null, null);
        if (query == null) {
            return getUriNameFallback();
        }
        int columnIndex = query.getColumnIndex("_display_name");
        query.moveToFirst();
        String string = query.getString(columnIndex);
        query.close();
        return string;
    }

    private String getUriNameFallback() {
        String[] split = this.uri.toString().split(File.separator);
        return split[split.length - 1];
    }
}
