package Y0;

import android.net.LocalSocket;
import android.net.LocalSocketAddress;
import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import okhttp3.HttpUrl;

/* JADX INFO: loaded from: classes.dex */
public abstract class d {

    public static class a {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        public int f1900a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public String f1901b;

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        public String f1902c;

        public a(int i2, String str, String str2) {
            this.f1900a = i2;
            this.f1901b = str;
            this.f1902c = str2;
        }
    }

    public interface b {
        void a(String str);
    }

    public static a a(String str) {
        return c(new String[]{str}, true, true);
    }

    public static a b(String str, boolean z2) {
        return c(new String[]{str}, z2, true);
    }

    public static a c(String[] strArr, boolean z2, boolean z3) {
        return (strArr == null || strArr.length == 0) ? new a(-1, null, null) : z2 ? d(strArr, z3) : e(strArr, z3, null);
    }

    private static a d(String[] strArr, boolean z2) throws Throwable {
        LocalSocket localSocket;
        OutputStream outputStream;
        OutputStream outputStream2;
        BufferedReader bufferedReader;
        StringBuilder sb = new StringBuilder();
        for (int i2 = 0; i2 < strArr.length; i2++) {
            String str = strArr[i2];
            if (str != null) {
                sb.append(str);
                if (i2 != strArr.length - 1) {
                    sb.append(" && ");
                }
            }
        }
        String string = sb.toString();
        Log.v("ShellCommandUtils", "root#" + string);
        StringBuilder sb2 = new StringBuilder(HttpUrl.FRAGMENT_ENCODE_SET);
        InputStream inputStream = null;
        try {
            try {
                try {
                    localSocket = new LocalSocket();
                } catch (Throwable th) {
                    th = th;
                }
            } catch (IOException e2) {
                e = e2;
                localSocket = null;
                outputStream2 = null;
            } catch (Throwable th2) {
                th = th2;
                localSocket = null;
                outputStream = null;
            }
            try {
                localSocket.connect(new LocalSocketAddress("rootdaemon", LocalSocketAddress.Namespace.RESERVED));
                outputStream2 = localSocket.getOutputStream();
                try {
                    outputStream2.write(string.getBytes());
                    inputStream = localSocket.getInputStream();
                    bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                    Log.i("ShellCommandUtils", "executeCommandWithRootPermission: InputStream");
                } catch (IOException e3) {
                    e = e3;
                    Log.e("ShellCommandUtils", "execute root shell command error", e);
                    if (inputStream != null) {
                        inputStream.close();
                    }
                    if (outputStream2 != null) {
                        outputStream2.close();
                    }
                    if (localSocket != null) {
                        localSocket.close();
                    }
                }
            } catch (IOException e4) {
                e = e4;
                outputStream2 = null;
            } catch (Throwable th3) {
                th = th3;
                outputStream = null;
                if (0 != 0) {
                    try {
                        inputStream.close();
                    } catch (IOException e5) {
                        Log.e("ShellCommandUtils", "close stream error", e5);
                        throw th;
                    }
                }
                if (outputStream != null) {
                    outputStream.close();
                }
                if (localSocket != null) {
                    localSocket.close();
                }
                throw th;
            }
        } catch (IOException e6) {
            Log.e("ShellCommandUtils", "close stream error", e6);
        }
        while (true) {
            String line = bufferedReader.readLine();
            if (line == null) {
                break;
            }
            sb2.append(line);
            sb2.append('\n');
            return f(sb2, z2);
        }
        Log.i("ShellCommandUtils", "executeCommandWithRootPermission:finish ");
        if (inputStream != null) {
            inputStream.close();
        }
        outputStream2.close();
        localSocket.close();
        return f(sb2, z2);
    }

    /* JADX WARN: Can't wrap try/catch for region: R(16:0|2|(2:135|3)|(8:155|4|136|5|(3:7|(2:9|159)(2:10|158)|11)|157|21|(1:23))|(18:25|147|26|154|27|152|28|(8:149|29|(3:31|(3:160|33|164)(1:163)|162)(1:161)|113|(1:115)(1:116)|(1:119)|120|121)|(2:40|(3:42|(3:165|44|168)(1:167)|166)(0))|61|(1:63)|(1:67)|70|113|(0)(0)|(0)|120|121)(1:60)|140|61|(0)|(0)|70|113|(0)(0)|(0)|120|121|(1:(0))) */
    /* JADX WARN: Code restructure failed: missing block: B:64:0x00ff, code lost:
    
        r14 = move-exception;
     */
    /* JADX WARN: Code restructure failed: missing block: B:69:0x0107, code lost:
    
        android.util.Log.e("ShellCommandUtils", "close stream error", r14);
     */
    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:106:0x0164 A[Catch: IOException -> 0x0160, TryCatch #18 {IOException -> 0x0160, blocks: (B:102:0x015c, B:106:0x0164, B:108:0x0169), top: B:142:0x015c }] */
    /* JADX WARN: Removed duplicated region for block: B:108:0x0169 A[Catch: IOException -> 0x0160, TRY_LEAVE, TryCatch #18 {IOException -> 0x0160, blocks: (B:102:0x015c, B:106:0x0164, B:108:0x0169), top: B:142:0x015c }] */
    /* JADX WARN: Removed duplicated region for block: B:115:0x0177  */
    /* JADX WARN: Removed duplicated region for block: B:116:0x0179  */
    /* JADX WARN: Removed duplicated region for block: B:119:0x0180  */
    /* JADX WARN: Removed duplicated region for block: B:127:0x0192 A[Catch: IOException -> 0x018e, TryCatch #9 {IOException -> 0x018e, blocks: (B:123:0x018a, B:127:0x0192, B:129:0x0197), top: B:138:0x018a }] */
    /* JADX WARN: Removed duplicated region for block: B:129:0x0197 A[Catch: IOException -> 0x018e, TRY_LEAVE, TryCatch #9 {IOException -> 0x018e, blocks: (B:123:0x018a, B:127:0x0192, B:129:0x0197), top: B:138:0x018a }] */
    /* JADX WARN: Removed duplicated region for block: B:133:0x01a0  */
    /* JADX WARN: Removed duplicated region for block: B:138:0x018a A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:142:0x015c A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:144:0x0139 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:169:? A[SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:63:0x00fb A[Catch: IOException -> 0x00ff, TryCatch #17 {IOException -> 0x00ff, blocks: (B:61:0x00f6, B:63:0x00fb, B:67:0x0103), top: B:140:0x00f6 }] */
    /* JADX WARN: Removed duplicated region for block: B:67:0x0103 A[Catch: IOException -> 0x00ff, TRY_LEAVE, TryCatch #17 {IOException -> 0x00ff, blocks: (B:61:0x00f6, B:63:0x00fb, B:67:0x0103), top: B:140:0x00f6 }] */
    /* JADX WARN: Removed duplicated region for block: B:92:0x0141 A[Catch: IOException -> 0x013d, TryCatch #19 {IOException -> 0x013d, blocks: (B:88:0x0139, B:92:0x0141, B:94:0x0146), top: B:144:0x0139 }] */
    /* JADX WARN: Removed duplicated region for block: B:94:0x0146 A[Catch: IOException -> 0x013d, TRY_LEAVE, TryCatch #19 {IOException -> 0x013d, blocks: (B:88:0x0139, B:92:0x0141, B:94:0x0146), top: B:144:0x0139 }] */
    /* JADX WARN: Removed duplicated region for block: B:98:0x014f A[PHI: r2 r4 r13 r14
      0x014f: PHI (r2v4 int) = (r2v3 int), (r2v6 int) binds: [B:97:0x014d, B:111:0x0170] A[DONT_GENERATE, DONT_INLINE]
      0x014f: PHI (r4v11 java.lang.Process) = (r4v10 java.lang.Process), (r4v12 java.lang.Process) binds: [B:97:0x014d, B:111:0x0170] A[DONT_GENERATE, DONT_INLINE]
      0x014f: PHI (r13v9 java.lang.StringBuilder) = (r13v8 java.lang.StringBuilder), (r13v12 java.lang.StringBuilder) binds: [B:97:0x014d, B:111:0x0170] A[DONT_GENERATE, DONT_INLINE]
      0x014f: PHI (r14v8 java.lang.StringBuilder) = (r14v7 java.lang.StringBuilder), (r14v10 java.lang.StringBuilder) binds: [B:97:0x014d, B:111:0x0170] A[DONT_GENERATE, DONT_INLINE]] */
    /* JADX WARN: Type inference failed for: r6v18 */
    /* JADX WARN: Type inference failed for: r6v19 */
    /* JADX WARN: Type inference failed for: r6v26 */
    /* JADX WARN: Type inference failed for: r6v27 */
    /* JADX WARN: Type inference failed for: r6v28 */
    /* JADX WARN: Type inference failed for: r6v29 */
    /* JADX WARN: Type inference failed for: r6v8, types: [java.io.BufferedReader] */
    /* JADX WARN: Type inference failed for: r6v9, types: [java.io.BufferedReader] */
    /* JADX WARN: Type inference failed for: r7v1 */
    /* JADX WARN: Type inference failed for: r7v12 */
    /* JADX WARN: Type inference failed for: r7v13 */
    /* JADX WARN: Type inference failed for: r7v2 */
    /* JADX WARN: Type inference failed for: r7v25 */
    /* JADX WARN: Type inference failed for: r7v26 */
    /* JADX WARN: Type inference failed for: r7v5, types: [java.io.BufferedReader] */
    /* JADX WARN: Type inference failed for: r7v6, types: [java.io.BufferedReader] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    private static Y0.d.a e(java.lang.String[] r12, boolean r13, Y0.d.b r14) throws java.lang.Throwable {
        /*
            Method dump skipped, instruction units count: 420
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: Y0.d.e(java.lang.String[], boolean, Y0.d$b):Y0.d$a");
    }

    private static a f(StringBuilder sb, boolean z2) {
        String strG;
        int iH = h(sb);
        String strG2 = null;
        if (iH == 0) {
            strG2 = z2 ? g(sb) : null;
            strG = null;
        } else {
            strG = z2 ? g(sb) : null;
        }
        return new a(iH, strG2, strG);
    }

    private static String g(StringBuilder sb) {
        String[] strArrSplit = sb.toString().split("\n");
        StringBuilder sb2 = new StringBuilder(HttpUrl.FRAGMENT_ENCODE_SET);
        for (int i2 = 0; i2 < strArrSplit.length - 1; i2++) {
            sb2.append(strArrSplit[i2]);
            sb2.append('\n');
        }
        c.h("ShellCommandUtils", "response#" + ((Object) sb2));
        return sb2.toString();
    }

    private static int h(StringBuilder sb) {
        String[] strArrSplit = sb.toString().split("\n");
        if (strArrSplit.length <= 0) {
            return -1;
        }
        String[] strArrSplit2 = strArrSplit[strArrSplit.length - 1].split(" ");
        if (strArrSplit2.length == 3) {
            return Integer.parseInt(strArrSplit2[1]);
        }
        return -1;
    }
}
