package ඓ.ඓ.K.䁗;

import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import ඓ.K.K.K.C1293;
import ῲ.ᨨ.ῲ.C1806;

/* renamed from: ඓ.ඓ.K.䁗.ݜ  reason: contains not printable characters */
/* compiled from: India_FileApp_InstallUtil */
public class C0713 {

    /* renamed from: K  reason: contains not printable characters */
    public String f3728 = "";

    /* JADX WARNING: Removed duplicated region for block: B:101:0x0163 A[SYNTHETIC, Splitter:B:101:0x0163] */
    /* JADX WARNING: Removed duplicated region for block: B:36:0x0081  */
    /* JADX WARNING: Removed duplicated region for block: B:37:0x0089 A[SYNTHETIC, Splitter:B:37:0x0089] */
    /* JADX WARNING: Removed duplicated region for block: B:54:0x00ab A[SYNTHETIC, Splitter:B:54:0x00ab] */
    /* JADX WARNING: Removed duplicated region for block: B:58:0x00b2 A[ADDED_TO_REGION, RETURN] */
    /* JADX WARNING: Removed duplicated region for block: B:59:0x00b3  */
    /* JADX WARNING: Removed duplicated region for block: B:63:0x00d1 A[SYNTHETIC, Splitter:B:63:0x00d1] */
    /* renamed from: K  reason: contains not printable characters */
    public Uri m2137(Context context, String str, String str2) {
        Uri uri;
        Throwable th;
        Exception e;
        OutputStream outputStream;
        boolean z;
        Throwable th2;
        FileOutputStream fileOutputStream;
        Exception e2;
        boolean z2;
        this.f3728 = str2;
        OutputStream outputStream2 = null;
        FileOutputStream fileOutputStream2 = null;
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        int i = Build.VERSION.SDK_INT;
        if (i < 29) {
            boolean z3 = false;
            if (!(i < 23 || (C1806.m4687(context, "android.permission.WRITE_EXTERNAL_STORAGE") == 0 && C1806.m4687(context, "android.permission.READ_EXTERNAL_STORAGE") == 0))) {
                Log.e("data", "save to file need storage permission");
                return null;
            }
            File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), C1293.m3414(new StringBuilder(), this.f3728, ".txt"));
            if (file.exists()) {
                return null;
            }
            if (!file.exists() || file.delete()) {
                File parentFile = file.getParentFile();
                if (parentFile == null) {
                    z2 = false;
                } else if (parentFile.exists()) {
                    z2 = parentFile.isDirectory();
                } else {
                    z2 = parentFile.mkdirs();
                }
                if (z2) {
                    try {
                        z = file.createNewFile();
                    } catch (IOException unused) {
                    }
                    if (z) {
                        Log.e("FileSaveUtil", "create or delete file <$file> failed.");
                    } else {
                        try {
                            fileOutputStream = new FileOutputStream(file);
                            try {
                                fileOutputStream.write(str.getBytes());
                                fileOutputStream.flush();
                                try {
                                    fileOutputStream.close();
                                } catch (IOException unused2) {
                                }
                                z3 = true;
                            } catch (Exception e3) {
                                e2 = e3;
                                try {
                                    e2.printStackTrace();
                                    if (fileOutputStream != null) {
                                        try {
                                            fileOutputStream.close();
                                        } catch (IOException unused3) {
                                        }
                                    }
                                    if (z3) {
                                    }
                                } catch (Throwable th3) {
                                    th2 = th3;
                                    fileOutputStream2 = fileOutputStream;
                                    if (fileOutputStream2 != null) {
                                    }
                                    throw th2;
                                }
                            }
                        } catch (Exception e4) {
                            e2 = e4;
                            fileOutputStream = null;
                            e2.printStackTrace();
                            if (fileOutputStream != null) {
                            }
                            if (z3) {
                            }
                        } catch (Throwable th4) {
                            th2 = th4;
                            if (fileOutputStream2 != null) {
                                try {
                                    fileOutputStream2.close();
                                } catch (IOException unused4) {
                                }
                            }
                            throw th2;
                        }
                    }
                    if (z3 || !file.exists()) {
                        return null;
                    }
                    StringBuilder r7 = C1293.m3416("file://");
                    r7.append(file.getAbsolutePath());
                    return Uri.parse(r7.toString());
                }
            }
            z = false;
            if (z) {
            }
            if (z3) {
                return null;
            }
            StringBuilder r72 = C1293.m3416("file://");
            r72.append(file.getAbsolutePath());
            return Uri.parse(r72.toString());
        } else if (new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), C1293.m3414(new StringBuilder(), this.f3728, ".txt")).exists()) {
            return null;
        } else {
            if (Environment.getExternalStorageState().equals("mounted")) {
                uri = MediaStore.Downloads.EXTERNAL_CONTENT_URI;
            } else {
                uri = MediaStore.Downloads.INTERNAL_CONTENT_URI;
            }
            ContentValues contentValues = new ContentValues();
            contentValues.put("mime_type", "text/plain");
            contentValues.put("datetaken", Long.valueOf(System.currentTimeMillis()));
            contentValues.put("_display_name", this.f3728);
            Uri insert = context.getContentResolver().insert(uri, contentValues);
            if (insert == null) {
                return null;
            }
            try {
                outputStream = context.getContentResolver().openOutputStream(insert);
                if (outputStream != null) {
                    try {
                        outputStream.write(str.getBytes());
                        outputStream.flush();
                    } catch (Exception e5) {
                        e = e5;
                    }
                }
                if (outputStream != null) {
                    try {
                        outputStream.close();
                    } catch (IOException unused5) {
                    }
                }
                return insert;
            } catch (Exception e6) {
                e = e6;
                outputStream = null;
                try {
                    e.printStackTrace();
                    context.getContentResolver().delete(insert, null, null);
                    if (outputStream != null) {
                        try {
                            outputStream.close();
                        } catch (IOException unused6) {
                        }
                    }
                    return null;
                } catch (Throwable th5) {
                    th = th5;
                    outputStream2 = outputStream;
                    if (outputStream2 != null) {
                    }
                    throw th;
                }
            } catch (Throwable th6) {
                th = th6;
                if (outputStream2 != null) {
                    try {
                        outputStream2.close();
                    } catch (IOException unused7) {
                    }
                }
                throw th;
            }
        }
    }
}
