package com.seewo.vcommons.utils;

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 java.util.List;

/* JADX INFO: loaded from: classes.dex */
public class ShellCommandUtils {
    public static final String COMMAND_EXIT = "exit\n";
    public static final String COMMAND_LINE_END = "\n";
    public static final String COMMAND_SH = "sh";
    public static final String COMMAND_SU = "su";
    private static final String TAG = "ShellCommandUtils";

    public interface OnCommandListener {
        void onResult(String str);
    }

    public static boolean checkRootPermission() {
        return true;
    }

    private ShellCommandUtils() {
    }

    public static CommandResult executeCommand(String str, boolean z) {
        return executeCommand(new String[]{str}, z, true);
    }

    public static CommandResult executeCommand(String str) {
        return executeCommand(new String[]{str}, true, true);
    }

    public static CommandResult executeCommand(String str, OnCommandListener onCommandListener) {
        return executeCommandWithUserPermission(new String[]{str}, true, onCommandListener);
    }

    public static CommandResult executeCommand(List<String> list, boolean z) {
        return executeCommand(list == null ? null : (String[]) list.toArray(new String[0]), z, true);
    }

    public static CommandResult executeCommand(String[] strArr, boolean z) {
        return executeCommand(strArr, z, true);
    }

    public static CommandResult executeCommand(String str, boolean z, boolean z2) {
        return executeCommand(new String[]{str}, z, z2);
    }

    public static CommandResult executeCommand(List<String> list, boolean z, boolean z2) {
        return executeCommand(list == null ? null : (String[]) list.toArray(new String[0]), z, z2);
    }

    public static CommandResult executeCommand(String[] strArr, boolean z, boolean z2) {
        if (strArr == null || strArr.length == 0) {
            return new CommandResult(-1, null, null);
        }
        if (z) {
            return executeCommandWithRootPermission(strArr, z2);
        }
        return executeCommandWithUserPermission(strArr, z2, null);
    }

    /* JADX WARN: Can't wrap try/catch for region: R(15:0|2|(10:146|3|149|4|144|5|(3:7|(2:9|160)(2:10|159)|11)|158|12|(1:14))|(18:16|147|17|156|18|153|19|(8:151|20|(3:22|(3:162|24|165)(1:164)|163)(1:161)|112|(1:114)(1:115)|(1:118)|119|120)|(2:25|(3:27|(3:166|29|169)(1:168)|167)(0))|52|(1:54)|(1:56)|(1:61)|112|(0)(0)|(0)|119|120)(1:51)|140|52|(0)|(0)|(0)|112|(0)(0)|(0)|119|120|(1:(0))) */
    /* JADX WARN: Code restructure failed: missing block: B:58:0x00ee, code lost:
    
        r14 = move-exception;
     */
    /* JADX WARN: Code restructure failed: missing block: B:59:0x00ef, code lost:
    
        android.util.Log.e(com.seewo.vcommons.utils.ShellCommandUtils.TAG, "close stream error", r14);
     */
    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:104:0x0156 A[Catch: IOException -> 0x0152, TryCatch #3 {IOException -> 0x0152, blocks: (B:100:0x014e, B:104:0x0156, B:106:0x015b), top: B:136:0x014e }] */
    /* JADX WARN: Removed duplicated region for block: B:106:0x015b A[Catch: IOException -> 0x0152, TRY_LEAVE, TryCatch #3 {IOException -> 0x0152, blocks: (B:100:0x014e, B:104:0x0156, B:106:0x015b), top: B:136:0x014e }] */
    /* JADX WARN: Removed duplicated region for block: B:110:0x0164 A[PHI: r2 r4 r13 r14
      0x0164: PHI (r2v5 int) = (r2v3 int), (r2v6 int) binds: [B:92:0x013d, B:109:0x0162] A[DONT_GENERATE, DONT_INLINE]
      0x0164: PHI (r4v11 java.lang.Process) = (r4v10 java.lang.Process), (r4v12 java.lang.Process) binds: [B:92:0x013d, B:109:0x0162] A[DONT_GENERATE, DONT_INLINE]
      0x0164: PHI (r13v11 java.lang.StringBuilder) = (r13v8 java.lang.StringBuilder), (r13v12 java.lang.StringBuilder) binds: [B:92:0x013d, B:109:0x0162] A[DONT_GENERATE, DONT_INLINE]
      0x0164: PHI (r14v9 java.lang.StringBuilder) = (r14v7 java.lang.StringBuilder), (r14v10 java.lang.StringBuilder) binds: [B:92:0x013d, B:109:0x0162] A[DONT_GENERATE, DONT_INLINE]] */
    /* JADX WARN: Removed duplicated region for block: B:114:0x016d  */
    /* JADX WARN: Removed duplicated region for block: B:115:0x016f  */
    /* JADX WARN: Removed duplicated region for block: B:118:0x0176  */
    /* JADX WARN: Removed duplicated region for block: B:128:0x018a A[Catch: IOException -> 0x0186, TryCatch #13 {IOException -> 0x0186, blocks: (B:124:0x0182, B:128:0x018a, B:130:0x018f), top: B:142:0x0182 }] */
    /* JADX WARN: Removed duplicated region for block: B:130:0x018f A[Catch: IOException -> 0x0186, TRY_LEAVE, TryCatch #13 {IOException -> 0x0186, blocks: (B:124:0x0182, B:128:0x018a, B:130:0x018f), top: B:142:0x0182 }] */
    /* JADX WARN: Removed duplicated region for block: B:134:0x0198  */
    /* JADX WARN: Removed duplicated region for block: B:136:0x014e A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:138:0x0129 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:142:0x0182 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:170:? A[SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:54:0x00e5 A[Catch: IOException -> 0x00ee, TryCatch #9 {IOException -> 0x00ee, blocks: (B:52:0x00e0, B:54:0x00e5, B:56:0x00ea), top: B:140:0x00e0 }] */
    /* JADX WARN: Removed duplicated region for block: B:56:0x00ea A[Catch: IOException -> 0x00ee, TRY_LEAVE, TryCatch #9 {IOException -> 0x00ee, blocks: (B:52:0x00e0, B:54:0x00e5, B:56:0x00ea), top: B:140:0x00e0 }] */
    /* JADX WARN: Removed duplicated region for block: B:61:0x00f4  */
    /* JADX WARN: Removed duplicated region for block: B:87:0x0131 A[Catch: IOException -> 0x012d, TryCatch #6 {IOException -> 0x012d, blocks: (B:83:0x0129, B:87:0x0131, B:89:0x0136), top: B:138:0x0129 }] */
    /* JADX WARN: Removed duplicated region for block: B:89:0x0136 A[Catch: IOException -> 0x012d, TRY_LEAVE, TryCatch #6 {IOException -> 0x012d, blocks: (B:83:0x0129, B:87:0x0131, B:89:0x0136), top: B:138:0x0129 }] */
    /* 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 com.seewo.vcommons.utils.ShellCommandUtils.CommandResult executeCommandWithUserPermission(java.lang.String[] r12, boolean r13, com.seewo.vcommons.utils.ShellCommandUtils.OnCommandListener r14) throws java.lang.Throwable {
        /*
            Method dump skipped, instruction units count: 412
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: com.seewo.vcommons.utils.ShellCommandUtils.executeCommandWithUserPermission(java.lang.String[], boolean, com.seewo.vcommons.utils.ShellCommandUtils$OnCommandListener):com.seewo.vcommons.utils.ShellCommandUtils$CommandResult");
    }

    public static class CommandResult {
        public String errorMsg;
        public int result;
        public String successMsg;

        public CommandResult(int i) {
            this.result = i;
        }

        public CommandResult(int i, String str, String str2) {
            this.result = i;
            this.successMsg = str;
            this.errorMsg = str2;
        }
    }

    private static CommandResult executeCommandWithRootPermission(String[] strArr, boolean z) throws Throwable {
        LocalSocket localSocket;
        OutputStream outputStream;
        OutputStream outputStream2;
        BufferedReader bufferedReader;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < strArr.length; i++) {
            String str = strArr[i];
            if (str != null) {
                sb.append(str);
                if (i != strArr.length - 1) {
                    sb.append(" && ");
                }
            }
        }
        String string = sb.toString();
        Log.v(TAG, "root#" + string);
        StringBuilder sb2 = new StringBuilder("");
        InputStream inputStream = null;
        try {
            try {
                try {
                    localSocket = new LocalSocket();
                } catch (Throwable th) {
                    th = th;
                }
                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(TAG, "executeCommandWithRootPermission: InputStream");
                    } catch (IOException e) {
                        e = e;
                        Log.e(TAG, "execute root shell command error", e);
                        if (inputStream != null) {
                            inputStream.close();
                        }
                        if (outputStream2 != null) {
                            outputStream2.close();
                        }
                        if (localSocket != null) {
                            localSocket.close();
                        }
                    }
                } catch (IOException e2) {
                    e = e2;
                    outputStream2 = null;
                } catch (Throwable th2) {
                    th = th2;
                    outputStream = null;
                    if (0 != 0) {
                        try {
                            inputStream.close();
                        } catch (IOException e3) {
                            Log.e(TAG, "close stream error", e3);
                            throw th;
                        }
                    }
                    if (outputStream != null) {
                        outputStream.close();
                    }
                    if (localSocket != null) {
                        localSocket.close();
                    }
                    throw th;
                }
            } catch (IOException e4) {
                e = e4;
                localSocket = null;
                outputStream2 = null;
            } catch (Throwable th3) {
                th = th3;
                localSocket = null;
                outputStream = null;
            }
        } catch (IOException e5) {
            Log.e(TAG, "close stream error", e5);
        }
        while (true) {
            String line = bufferedReader.readLine();
            if (line == null) {
                break;
            }
            sb2.append(line);
            sb2.append('\n');
            return getCommandResultFromBuilder(sb2, z);
        }
        Log.i(TAG, "executeCommandWithRootPermission:finish ");
        if (inputStream != null) {
            inputStream.close();
        }
        if (outputStream2 != null) {
            outputStream2.close();
        }
        localSocket.close();
        return getCommandResultFromBuilder(sb2, z);
    }

    private static CommandResult getCommandResultFromBuilder(StringBuilder sb, boolean z) {
        String messageFromBuilder;
        int resultSignFromBuilder = getResultSignFromBuilder(sb);
        String messageFromBuilder2 = null;
        if (resultSignFromBuilder == 0) {
            messageFromBuilder2 = z ? getMessageFromBuilder(sb) : null;
            messageFromBuilder = null;
        } else {
            messageFromBuilder = z ? getMessageFromBuilder(sb) : null;
        }
        return new CommandResult(resultSignFromBuilder, messageFromBuilder2, messageFromBuilder);
    }

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

    private static String getMessageFromBuilder(StringBuilder sb) {
        String[] strArrSplit = sb.toString().split(COMMAND_LINE_END);
        StringBuilder sb2 = new StringBuilder("");
        for (int i = 0; i < strArrSplit.length - 1; i++) {
            sb2.append(strArrSplit[i]);
            sb2.append('\n');
        }
        RLog.v(TAG, "response#" + ((Object) sb2));
        return sb2.toString();
    }
}
