package defpackage;

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.util.Log;
import androidx.credentials.exceptions.publickeycredential.DomExceptionUtils;
import androidx.media3.container.NalUnitUtil;
import androidx.media3.session.legacy.PlaybackStateCompat;
import fi.iki.elonen.NanoHTTPD;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
import kotlin.Metadata;

/* JADX INFO: loaded from: classes3.dex */
@Metadata(d1 = {"\u0000\n\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0000\bÆ\u0002\u0018\u00002\u00020\u0001¨\u0006\u0002"}, d2 = {"Lkq;", "", "app_sideloadRelease"}, k = 1, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
@vx8
public final class kq {
    public static File c;
    public static File d;
    public static boolean e;
    public static Process f;
    public static final ExecutorService a = Executors.newSingleThreadExecutor();
    public static final SimpleDateFormat b = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
    public static final AtomicBoolean g = new AtomicBoolean(false);

    public static void a(String str, String str2, Throwable th) {
        cr4.g(str2, "message");
        if (th != null) {
            str2 = vw2.l(str2, "\n", cr2.b(th));
        }
        f("E", str, str2);
    }

    public static boolean b(OutputStream outputStream) {
        BufferedReader bufferedReader;
        try {
            Charset charset = bz0.a;
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, charset), 8192);
            try {
                File file = c;
                if (file != null) {
                    File file2 = new File(file, "strmr_log_old.txt");
                    if (file2.exists()) {
                        bufferedWriter.write("=== Previous Session Logs ===\n");
                        bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file2), charset), 8192);
                        try {
                            el9.a(bufferedReader, bufferedWriter);
                            bufferedReader.close();
                            bufferedWriter.write("\n\n");
                        } finally {
                        }
                    }
                }
                File file3 = d;
                if (file3 != null && file3.exists()) {
                    bufferedWriter.write("=== Current Session Logs ===\n");
                    bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file3), charset), 8192);
                    try {
                        el9.a(bufferedReader, bufferedWriter);
                        bufferedReader.close();
                    } finally {
                        try {
                            throw th;
                        } finally {
                        }
                    }
                }
                bufferedWriter.close();
                return true;
            } finally {
            }
        } catch (Exception e2) {
            Log.e("AppLogger", "Failed to export logs", e2);
            return false;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:50:0x0104  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static q07 c(Context context) {
        String message;
        q07 q07Var;
        q07 q07Var2;
        File file = d;
        if (file == null || !file.exists() || file.length() <= 0) {
            return new q07(Boolean.FALSE, "No logs to export");
        }
        String strAb = vw2.ab("strmr_logs_", new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date()), ".txt");
        try {
            if (Build.VERSION.SDK_INT >= 29) {
                ContentValues contentValues = new ContentValues();
                contentValues.put("_display_name", strAb);
                contentValues.put("mime_type", NanoHTTPD.MIME_PLAINTEXT);
                contentValues.put("relative_path", Environment.DIRECTORY_DOWNLOADS);
                Uri uriInsert = context.getContentResolver().insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues);
                if (uriInsert == null) {
                    return new q07(Boolean.FALSE, "Could not create file in Downloads");
                }
                OutputStream outputStreamOpenOutputStream = context.getContentResolver().openOutputStream(uriInsert);
                if (outputStreamOpenOutputStream == null) {
                    context.getContentResolver().delete(uriInsert, null, null);
                    return new q07(Boolean.FALSE, "Could not open output stream");
                }
                try {
                    if (b(outputStreamOpenOutputStream)) {
                        q07Var2 = new q07(Boolean.TRUE, "Downloads/" + strAb);
                    } else {
                        context.getContentResolver().delete(uriInsert, null, null);
                        q07Var2 = new q07(Boolean.FALSE, "Failed to write logs");
                    }
                    outputStreamOpenOutputStream.close();
                    return q07Var2;
                } finally {
                }
            } else {
                File file2 = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), strAb);
                FileOutputStream fileOutputStream = new FileOutputStream(file2);
                try {
                    if (b(fileOutputStream)) {
                        q07Var = new q07(Boolean.TRUE, file2.getAbsolutePath());
                    } else {
                        file2.delete();
                        q07Var = new q07(Boolean.FALSE, "Failed to write logs");
                    }
                    fileOutputStream.close();
                    return q07Var;
                } finally {
                }
            }
        } catch (Exception e2) {
            Log.e("AppLogger", "Failed to export logs to Downloads", e2);
            Boolean bool = Boolean.FALSE;
            message = e2.getMessage();
            if (message == null) {
            }
            return new q07(bool, message);
        }
        Log.e("AppLogger", "Failed to export logs to Downloads", e2);
        Boolean bool2 = Boolean.FALSE;
        message = e2.getMessage();
        if (message == null) {
            message = "Unknown error";
        }
        return new q07(bool2, message);
    }

    public static String d() {
        String absolutePath;
        File file = c;
        return (file == null || (absolutePath = file.getAbsolutePath()) == null) ? "Logs not available" : n89.an(n89.an(absolutePath, "/storage/emulated/0", ""), "/storage/emulated/legacy", "");
    }

    public static String e() {
        File file = d;
        if (file == null || !file.exists()) {
            return "0 KB";
        }
        long length = file.length();
        if (length < 1024) {
            return length + " B";
        }
        if (length >= PlaybackStateCompat.ACTION_SET_CAPTIONING_ENABLED) {
            return String.format("%.1f MB", Arrays.copyOf(new Object[]{Double.valueOf(length / 1048576.0d)}, 1));
        }
        return (length / ((long) 1024)) + " KB";
    }

    public static void f(String str, String str2, String str3) {
        String str4;
        if (e) {
            String strU = n41.u(fi7.m("[", b.format(new Date()), "] ", str, DomExceptionUtils.SEPARATOR), str2, ": ", str3, "\n");
            int iHashCode = str.hashCode();
            if (iHashCode != 69) {
                if (iHashCode == 73) {
                    str4 = "I";
                } else if (iHashCode == 80) {
                    str4 = "P";
                } else if (iHashCode == 87 && str.equals("W")) {
                    Log.w(str2, str3);
                }
                str.equals(str4);
            } else if (str.equals("E")) {
                Log.e(str2, str3);
            }
            a.execute(new r21(strU, 7));
        }
    }

    public static void g(String str, Map map) {
        Set setEntrySet = map.entrySet();
        ArrayList arrayList = new ArrayList();
        for (Object obj : setEntrySet) {
            if (((Map.Entry) obj).getValue() != null) {
                arrayList.add(obj);
            }
        }
        f("P", "Playback", str + " | " + y41.ai(arrayList, ", ", null, null, new od0(25), 30));
    }

    public static void h(String str, String str2) {
        cr4.g(str2, "message");
        f("W", str, str2);
    }
}
