package ඓ.ᨨ.K.ඓ.ඓ;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.net.Uri;
import android.util.DisplayMetrics;
import android.util.Log;
import com.facebook.appevents.codeless.CodelessMatcher;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.R$string;
import com.zhihu.matisse.internal.entity.Item;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import ඓ.ᨨ.K.ඓ.K.C0778;
import ඓ.ᨨ.K.ඓ.K.C0781;
import ඓ.ᨨ.K.㸾.AbstractC0809;

/* renamed from: ඓ.ᨨ.K.ඓ.ඓ.ʬ  reason: contains not printable characters */
/* compiled from: PhotoMetadataUtils */
public final class C0771 {
    /* JADX WARNING: Code restructure failed: missing block: B:18:0x0039, code lost:
        r1.close();
     */
    /* JADX WARNING: Failed to process nested try/catch */
    /* JADX WARNING: Missing exception handler attribute for start block: B:15:0x0032 */
    /* JADX WARNING: Removed duplicated region for block: B:18:0x0039  */
    /* JADX WARNING: Removed duplicated region for block: B:30:0x0070  */
    /* JADX WARNING: Removed duplicated region for block: B:32:0x0076  */
    /* JADX WARNING: Removed duplicated region for block: B:34:0x007e  */
    /* JADX WARNING: Removed duplicated region for block: B:41:0x00b6 A[SYNTHETIC, Splitter:B:41:0x00b6] */
    /* renamed from: K  reason: contains not printable characters */
    public static Point m2342(Uri uri, Activity activity) {
        Throwable th;
        Point point;
        int i;
        ContentResolver contentResolver = activity.getContentResolver();
        InputStream inputStream = null;
        boolean z = false;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        InputStream inputStream2 = contentResolver.openInputStream(uri);
        try {
            BitmapFactory.decodeStream(inputStream2, null, options);
            point = new Point(options.outWidth, options.outHeight);
            if (inputStream2 != null) {
                try {
                    inputStream2.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (FileNotFoundException unused) {
            inputStream = inputStream2;
            try {
                point = new Point(0, 0);
                if (inputStream != null) {
                }
                int i2 = point.x;
                i = point.y;
                int attributeInt = C0773.m2347(m2343(contentResolver, uri)).getAttributeInt("Orientation", -1);
                z = true;
                if (z) {
                }
                if (i != 0) {
                }
            } catch (Throwable th2) {
                th = th2;
                inputStream2 = inputStream;
                if (inputStream2 != null) {
                }
                throw th;
            }
        } catch (Throwable th3) {
            th = th3;
            if (inputStream2 != null) {
                try {
                    inputStream2.close();
                } catch (IOException e2) {
                    e2.printStackTrace();
                }
            }
            throw th;
        }
        int i22 = point.x;
        i = point.y;
        try {
            int attributeInt2 = C0773.m2347(m2343(contentResolver, uri)).getAttributeInt("Orientation", -1);
            if (attributeInt2 == 6 || attributeInt2 == 8) {
                z = true;
            }
        } catch (IOException unused2) {
            Log.e("ඓ.ᨨ.K.ඓ.ඓ.ʬ", "could not read exif info of the image: " + uri);
        }
        if (z) {
            i22 = point.y;
            i = point.x;
        }
        if (i != 0) {
            return new Point(1600, 1600);
        }
        DisplayMetrics displayMetrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        float f = (float) i22;
        float f2 = ((float) displayMetrics.widthPixels) / f;
        float f3 = (float) i;
        float f4 = ((float) displayMetrics.heightPixels) / f3;
        if (f2 > f4) {
            return new Point((int) (f * f2), (int) (f3 * f4));
        }
        return new Point((int) (f * f2), (int) (f3 * f4));
    }

    /* JADX WARNING: Removed duplicated region for block: B:22:0x0040  */
    /* renamed from: K  reason: contains not printable characters */
    public static String m2343(ContentResolver contentResolver, Uri uri) {
        Throwable th;
        Cursor cursor;
        if (uri == null) {
            return null;
        }
        if (!"content".equals(uri.getScheme())) {
            return uri.getPath();
        }
        try {
            cursor = contentResolver.query(uri, new String[]{"_data"}, null, null, null);
            if (cursor != null) {
                try {
                    if (cursor.moveToFirst()) {
                        String string = cursor.getString(cursor.getColumnIndex("_data"));
                        cursor.close();
                        return string;
                    }
                } catch (Throwable th2) {
                    th = th2;
                    if (cursor != null) {
                        cursor.close();
                    }
                    throw th;
                }
            }
            if (cursor != null) {
                cursor.close();
            }
            return null;
        } catch (Throwable th3) {
            th = th3;
            cursor = null;
            if (cursor != null) {
            }
            throw th;
        }
    }

    /* renamed from: K  reason: contains not printable characters */
    public static C0781 m2344(Context context, Item item) {
        boolean z = false;
        if (context != null) {
            ContentResolver contentResolver = context.getContentResolver();
            Iterator<MimeType> it2 = C0778.C0779.f3967.f3956.iterator();
            while (true) {
                if (it2.hasNext()) {
                    if (it2.next().checkType(contentResolver, item.m1915())) {
                        z = true;
                        break;
                    }
                } else {
                    break;
                }
            }
        }
        if (!z) {
            return new C0781(context.getString(R$string.error_file_type));
        }
        List<AbstractC0809> list = C0778.C0779.f3967.f3960;
        if (list == null) {
            return null;
        }
        for (AbstractC0809 k : list) {
            C0781 r1 = k.m2398(context, item);
            if (r1 != null) {
                return r1;
            }
        }
        return null;
    }

    /* renamed from: K  reason: contains not printable characters */
    public static float m2341(long j) {
        DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getNumberInstance(Locale.US);
        decimalFormat.applyPattern("0.0");
        String format = decimalFormat.format((double) ((((float) j) / 1024.0f) / 1024.0f));
        Log.e("ʬ", "getSizeInMB: " + format);
        return Float.valueOf(format.replaceAll(",", CodelessMatcher.CURRENT_CLASS_NAME)).floatValue();
    }
}
