package ඓ.ᨨ.K.ඓ.ῲ;

import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.database.MergeCursor;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import com.facebook.share.internal.ShareConstants;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.internal.entity.Album;
import java.util.HashMap;
import java.util.HashSet;
import ඓ.ᨨ.K.ඓ.K.C0778;
import ῲ.ᓣ.ῲ.C1687;

/* renamed from: ඓ.ᨨ.K.ඓ.ῲ.K  reason: contains not printable characters */
/* compiled from: AlbumLoader */
public class C0777 extends C1687 {

    /* renamed from: ᑓ  reason: contains not printable characters */
    public static final String[] f3939 = {"_id", "bucket_id", "bucket_display_name", "mime_type", "COUNT(*) AS count"};

    /* renamed from: ᓆ  reason: contains not printable characters */
    public static final Uri f3940 = MediaStore.Files.getContentUri("external");

    /* renamed from: ゎ  reason: contains not printable characters */
    public static final String[] f3941 = {"_id", "bucket_id", "bucket_display_name", "mime_type"};

    /* renamed from: ト  reason: contains not printable characters */
    public static final String[] f3942 = {String.valueOf(1), String.valueOf(3)};

    /* renamed from: 㴪  reason: contains not printable characters */
    public static final String[] f3943 = {"_id", "bucket_id", "bucket_display_name", "mime_type", ShareConstants.MEDIA_URI, "count"};

    /* JADX INFO: super call moved to the top of the method (can break code semantics) */
    public C0777(Context context, String str, String[] strArr) {
        super(context, f3940, m2354() ? f3939 : f3941, str, strArr, "datetaken DESC");
    }

    /* renamed from: ʬ  reason: contains not printable characters */
    public static Uri m2352(Cursor cursor) {
        Uri uri;
        long j = cursor.getLong(cursor.getColumnIndex("_id"));
        String string = cursor.getString(cursor.getColumnIndex("mime_type"));
        if (MimeType.isImage(string)) {
            uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        } else if (MimeType.isVideo(string)) {
            uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
        } else {
            uri = MediaStore.Files.getContentUri("external");
        }
        return ContentUris.withAppendedId(uri, j);
    }

    /* renamed from: K  reason: contains not printable characters */
    public static C1687 m2353(Context context) {
        String[] strArr;
        String str;
        if (C0778.C0779.f3967.m2360()) {
            str = m2354() ? "media_type=? AND _size>0 AND mime_type=?) GROUP BY (bucket_id" : "media_type=? AND _size>0 AND mime_type=?";
            strArr = new String[]{String.valueOf(1), "image/gif"};
        } else if (C0778.C0779.f3967.m2359()) {
            str = m2354() ? "media_type=? AND _size>0) GROUP BY (bucket_id" : "media_type=? AND _size>0";
            strArr = new String[]{String.valueOf(1)};
        } else if (C0778.C0779.f3967.m2358()) {
            str = m2354() ? "media_type=? AND _size>0) GROUP BY (bucket_id" : "media_type=? AND _size>0";
            strArr = new String[]{String.valueOf(3)};
        } else {
            str = m2354() ? "(media_type=? OR media_type=?) AND _size>0) GROUP BY (bucket_id" : "(media_type=? OR media_type=?) AND _size>0";
            strArr = f3942;
        }
        return new C0777(context, str, strArr);
    }

    /* renamed from: ト  reason: contains not printable characters */
    public static boolean m2354() {
        return Build.VERSION.SDK_INT < 29;
    }

    @Override // ῲ.ᓣ.ῲ.C1683
    /* renamed from: 㑃  reason: contains not printable characters */
    public void m2357() {
    }

    @Override // ῲ.ᓣ.ῲ.AbstractC1688, ῲ.ᓣ.ῲ.C1687, ῲ.ᓣ.ῲ.C1687
    /* renamed from: ᑓ  reason: contains not printable characters */
    public Cursor m2356() {
        Uri uri;
        int i;
        long j;
        int i2;
        Uri uri2;
        String str;
        Cursor r0 = super.m4299();
        MatrixCursor matrixCursor = new MatrixCursor(f3943);
        if (m2354()) {
            MatrixCursor matrixCursor2 = new MatrixCursor(f3943);
            if (r0 != null) {
                i2 = 0;
                while (r0.moveToNext()) {
                    long j2 = r0.getLong(r0.getColumnIndex("_id"));
                    long j3 = r0.getLong(r0.getColumnIndex("bucket_id"));
                    String string = r0.getString(r0.getColumnIndex("bucket_display_name"));
                    String string2 = r0.getString(r0.getColumnIndex("mime_type"));
                    Uri r21 = m2352(r0);
                    int i3 = r0.getInt(r0.getColumnIndex("count"));
                    matrixCursor2.addRow(new String[]{Long.toString(j2), Long.toString(j3), string, string2, r21.toString(), String.valueOf(i3)});
                    i2 += i3;
                }
                uri2 = r0.moveToFirst() ? m2352(r0) : null;
            } else {
                uri2 = null;
                i2 = 0;
            }
            String[] strArr = new String[6];
            String str2 = Album.f3330;
            strArr[0] = str2;
            strArr[1] = str2;
            strArr[2] = "All";
            strArr[3] = null;
            if (uri2 == null) {
                str = null;
            } else {
                str = uri2.toString();
            }
            strArr[4] = str;
            strArr[5] = String.valueOf(i2);
            matrixCursor.addRow(strArr);
            return new MergeCursor(new Cursor[]{matrixCursor, matrixCursor2});
        }
        HashMap hashMap = new HashMap();
        if (r0 != null) {
            while (r0.moveToNext()) {
                long j4 = r0.getLong(r0.getColumnIndex("bucket_id"));
                Long l = (Long) hashMap.get(Long.valueOf(j4));
                if (l == null) {
                    j = 1L;
                } else {
                    j = Long.valueOf(l.longValue() + 1);
                }
                hashMap.put(Long.valueOf(j4), j);
            }
        }
        MatrixCursor matrixCursor3 = new MatrixCursor(f3943);
        if (r0 == null || !r0.moveToFirst()) {
            i = 0;
            uri = null;
        } else {
            uri = m2352(r0);
            HashSet hashSet = new HashSet();
            i = 0;
            while (true) {
                long j5 = r0.getLong(r0.getColumnIndex("bucket_id"));
                if (!hashSet.contains(Long.valueOf(j5))) {
                    long j6 = r0.getLong(r0.getColumnIndex("_id"));
                    String string3 = r0.getString(r0.getColumnIndex("bucket_display_name"));
                    String string4 = r0.getString(r0.getColumnIndex("mime_type"));
                    Uri r22 = m2352(r0);
                    long longValue = ((Long) hashMap.get(Long.valueOf(j5))).longValue();
                    matrixCursor3.addRow(new String[]{Long.toString(j6), Long.toString(j5), string3, string4, r22.toString(), String.valueOf(longValue)});
                    hashSet.add(Long.valueOf(j5));
                    i = (int) (((long) i) + longValue);
                }
                if (!r0.moveToNext()) {
                    break;
                }
            }
        }
        String[] strArr2 = new String[6];
        String str3 = Album.f3330;
        strArr2[0] = str3;
        strArr2[1] = str3;
        strArr2[2] = "All";
        String str4 = null;
        strArr2[3] = null;
        if (uri != null) {
            str4 = uri.toString();
        }
        strArr2[4] = str4;
        strArr2[5] = String.valueOf(i);
        matrixCursor.addRow(strArr2);
        return new MergeCursor(new Cursor[]{matrixCursor, matrixCursor3});
    }
}
