package com.tencent.mm.pluginsdk.i;

import android.content.ContentResolver;
import android.net.Uri;
import android.webkit.MimeTypeMap;
import com.tencent.matrix.trace.core.AppMethodBeat;
import com.tencent.mm.model.aw;
import com.tencent.mm.model.c;
import com.tencent.mm.pluginsdk.model.app.l;
import com.tencent.mm.sdk.platformtools.ab;
import com.tencent.mm.sdk.platformtools.ah;
import com.tencent.mm.sdk.platformtools.bo;
import com.tencent.mm.vfs.b;
import com.tencent.mm.vfs.j;
import com.tencent.ytcommon.BuildConfig;
import java.io.Closeable;

public final class e {
    public static final String a(ContentResolver contentResolver, Uri uri) {
        Throwable e;
        AppMethodBeat.i(27476);
        if (uri == null) {
            AppMethodBeat.o(27476);
            return null;
        }
        b bVar;
        String type = contentResolver.getType(uri);
        String ajn = ajn(type);
        String b = b(contentResolver, uri);
        if (bo.isNullOrNil(b)) {
            b = "share_file" + ajn(type);
        }
        b bVar2 = new b(com.tencent.mm.compatible.util.e.euQ, b);
        if (bVar2.exists()) {
            aw.ZK();
            bVar = new b(l.X(c.Ye(), b, ajn));
        } else {
            bVar = bVar2;
        }
        Closeable q;
        Closeable openInputStream;
        try {
            q = com.tencent.mm.vfs.e.q(bVar);
            try {
                openInputStream = contentResolver.openInputStream(uri);
                if (openInputStream == null) {
                    bo.b(openInputStream);
                    bo.b(q);
                    AppMethodBeat.o(27476);
                    return null;
                }
                try {
                    byte[] bArr = new byte[1024];
                    while (true) {
                        int read = openInputStream.read(bArr);
                        if (read > 0) {
                            q.write(bArr, 0, read);
                        } else {
                            b = j.w(bVar.dMD());
                            bo.b(openInputStream);
                            bo.b(q);
                            AppMethodBeat.o(27476);
                            return b;
                        }
                    }
                } catch (Exception e2) {
                    e = e2;
                    try {
                        ab.printErrStackTrace("MicroMsg.ShareUtil", e, "copy google photo img fail", new Object[0]);
                        bo.b(openInputStream);
                        bo.b(q);
                        AppMethodBeat.o(27476);
                        return null;
                    } catch (Throwable th) {
                        e = th;
                        bo.b(openInputStream);
                        bo.b(q);
                        AppMethodBeat.o(27476);
                        throw e;
                    }
                }
            } catch (Exception e3) {
                e = e3;
                openInputStream = null;
            } catch (Throwable th2) {
                e = th2;
                openInputStream = null;
                bo.b(openInputStream);
                bo.b(q);
                AppMethodBeat.o(27476);
                throw e;
            }
        } catch (Exception e4) {
            e = e4;
            openInputStream = null;
            q = null;
        } catch (Throwable th3) {
            e = th3;
            openInputStream = null;
            q = null;
            bo.b(openInputStream);
            bo.b(q);
            AppMethodBeat.o(27476);
            throw e;
        }
    }

    /* JADX WARNING: Removed duplicated region for block: B:10:0x0031  */
    /* JADX WARNING: Removed duplicated region for block: B:28:0x0076  */
    private static java.lang.String b(android.content.ContentResolver r8, android.net.Uri r9) {
        /*
        r7 = 27477; // 0x6b55 float:3.8503E-41 double:1.35754E-319;
        r1 = 0;
        r6 = 0;
        com.tencent.matrix.trace.core.AppMethodBeat.i(r7);
        r0 = 1;
        r2 = new java.lang.String[r0];
        r0 = "_data";
        r2[r1] = r0;
        r3 = 0;
        r4 = 0;
        r5 = 0;
        r0 = r8;
        r1 = r9;
        r1 = r0.query(r1, r2, r3, r4, r5);	 Catch:{ Exception -> 0x0038, all -> 0x0072 }
        if (r1 == 0) goto L_0x0081;
    L_0x001a:
        r0 = r1.moveToFirst();	 Catch:{ Exception -> 0x007f }
        if (r0 == 0) goto L_0x0081;
    L_0x0020:
        r0 = 0;
        r0 = r1.getString(r0);	 Catch:{ Exception -> 0x007f }
        r2 = new com.tencent.mm.vfs.b;	 Catch:{ Exception -> 0x007f }
        r2.<init>(r0);	 Catch:{ Exception -> 0x007f }
        r6 = r2.getName();	 Catch:{ Exception -> 0x007f }
        r0 = r6;
    L_0x002f:
        if (r1 == 0) goto L_0x0034;
    L_0x0031:
        r1.close();
    L_0x0034:
        com.tencent.matrix.trace.core.AppMethodBeat.o(r7);
        return r0;
    L_0x0038:
        r0 = move-exception;
        r1 = r6;
    L_0x003a:
        r2 = "MicroMsg.ShareUtil";
        r3 = "getFileName";
        r4 = 0;
        r4 = new java.lang.Object[r4];	 Catch:{ all -> 0x007d }
        com.tencent.mm.sdk.platformtools.ab.printErrStackTrace(r2, r0, r3, r4);	 Catch:{ all -> 0x007d }
        r0 = r9.toString();	 Catch:{ Exception -> 0x0063 }
        r2 = "utf8";
        r0 = java.net.URLDecoder.decode(r0, r2);	 Catch:{ Exception -> 0x0063 }
        r2 = "/";
        r0 = r0.split(r2);	 Catch:{ Exception -> 0x0063 }
        r2 = r0.length;	 Catch:{ Exception -> 0x0063 }
        r2 = r2 + -1;
        r0 = r0[r2];	 Catch:{ Exception -> 0x0063 }
    L_0x005d:
        if (r1 == 0) goto L_0x0034;
    L_0x005f:
        r1.close();
        goto L_0x0034;
    L_0x0063:
        r0 = move-exception;
        r2 = "MicroMsg.ShareUtil";
        r3 = "getFileName From Uri";
        r4 = 0;
        r4 = new java.lang.Object[r4];	 Catch:{ all -> 0x007d }
        com.tencent.mm.sdk.platformtools.ab.printErrStackTrace(r2, r0, r3, r4);	 Catch:{ all -> 0x007d }
        r0 = r6;
        goto L_0x005d;
    L_0x0072:
        r0 = move-exception;
        r1 = r6;
    L_0x0074:
        if (r1 == 0) goto L_0x0079;
    L_0x0076:
        r1.close();
    L_0x0079:
        com.tencent.matrix.trace.core.AppMethodBeat.o(r7);
        throw r0;
    L_0x007d:
        r0 = move-exception;
        goto L_0x0074;
    L_0x007f:
        r0 = move-exception;
        goto L_0x003a;
    L_0x0081:
        r0 = r6;
        goto L_0x002f;
        */
        throw new UnsupportedOperationException("Method not decompiled: com.tencent.mm.pluginsdk.i.e.b(android.content.ContentResolver, android.net.Uri):java.lang.String");
    }

    public static final String a(ContentResolver contentResolver, Uri uri, int i) {
        Throwable e;
        Object obj;
        String str = null;
        AppMethodBeat.i(27478);
        if (uri == null) {
            AppMethodBeat.o(27478);
        } else {
            b bVar = new b(ah.getContext().getCacheDir(), "share_img_tmp_" + i + ajn(contentResolver.getType(uri)));
            bVar.delete();
            Closeable q;
            Closeable obj2;
            try {
                q = com.tencent.mm.vfs.e.q(bVar);
                try {
                    obj2 = contentResolver.openInputStream(uri);
                    if (obj2 == null) {
                        bo.b(obj2);
                        bo.b(q);
                        AppMethodBeat.o(27478);
                    } else {
                        try {
                            byte[] bArr = new byte[1024];
                            while (true) {
                                int read = obj2.read(bArr);
                                if (read <= 0) {
                                    break;
                                }
                                q.write(bArr, 0, read);
                            }
                            str = j.w(bVar.dMD());
                            bo.b(obj2);
                            bo.b(q);
                            AppMethodBeat.o(27478);
                        } catch (Exception e2) {
                            e = e2;
                            try {
                                ab.printErrStackTrace("MicroMsg.ShareUtil", e, "copy google photo img fail", new Object[0]);
                                bo.b(obj2);
                                bo.b(q);
                                AppMethodBeat.o(27478);
                                return str;
                            } catch (Throwable th) {
                                e = th;
                                bo.b(obj2);
                                bo.b(q);
                                AppMethodBeat.o(27478);
                                throw e;
                            }
                        }
                    }
                } catch (Exception e3) {
                    e = e3;
                    obj2 = str;
                } catch (Throwable th2) {
                    e = th2;
                    obj2 = str;
                    bo.b(obj2);
                    bo.b(q);
                    AppMethodBeat.o(27478);
                    throw e;
                }
            } catch (Exception e4) {
                e = e4;
                obj2 = str;
                Object obj3 = str;
            } catch (Throwable th3) {
                e = th3;
                obj2 = str;
                q = str;
                bo.b(obj2);
                bo.b(q);
                AppMethodBeat.o(27478);
                throw e;
            }
        }
        return str;
    }

    private static String ajn(String str) {
        AppMethodBeat.i(27479);
        String str2;
        if (bo.isNullOrNil(str)) {
            str2 = BuildConfig.FLAVOR;
            AppMethodBeat.o(27479);
            return str2;
        }
        Object extensionFromMimeType = MimeTypeMap.getSingleton().getExtensionFromMimeType(str);
        if (extensionFromMimeType == null || extensionFromMimeType.length() == 0) {
            String[] split = str.split("/");
            if (split != null && split.length > 1) {
                extensionFromMimeType = split[1];
            }
        }
        str2 = ".".concat(String.valueOf(extensionFromMimeType));
        AppMethodBeat.o(27479);
        return str2;
    }
}
