package a.c.d.k;

import android.content.Context;
import android.media.AudioManager;
import android.util.Log;
import com.ucar.vehiclesdk.UCarAdapter;
import com.ucar.vehiclesdk.UCarCommon;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class c extends Thread {
    private static final int h = 54321;

    /* renamed from: a  reason: collision with root package name */
    private final String f162a = c.class.getSimpleName();
    private final Context b;
    private ServerSocket c;
    private AudioManager d;
    private boolean e;
    private volatile boolean f;
    private boolean g;

    public c(Context context) {
        this.b = context;
    }

    private static /* synthetic */ boolean a(String str) {
        return !str.equals("");
    }

    private static /* synthetic */ Short b(String str) {
        return Short.valueOf(Short.parseShort(str.toString()));
    }

    private void b() {
        if (!this.g) {
            this.d.setMicrophoneMute(false);
        }
    }

    private void c() {
        AudioManager audioManager = (AudioManager) this.b.getSystemService("audio");
        this.d = audioManager;
        this.g = audioManager.isMicrophoneMute();
        this.d.setMicrophoneMute(true);
    }

    /* JADX INFO: Can't fix incorrect switch cases order, some code will duplicate */
    private void c(String str) {
        char c2;
        String str2;
        StringBuilder sb;
        List list = (List) Arrays.stream(str.split(" ")).filter($$Lambda$c$xc6Wi6EHaZIjOTD4xeZRvT8R9r4.INSTANCE).collect(Collectors.toList());
        int i = 0;
        String str3 = (String) list.get(0);
        str3.hashCode();
        switch (str3.hashCode()) {
            case -1875240789:
                if (str3.equals("sendGyroscopeInfo")) {
                    c2 = 0;
                    break;
                }
                c2 = 65535;
                break;
            case -1721692936:
                if (str3.equals("sendOilInfo")) {
                    c2 = 1;
                    break;
                }
                c2 = 65535;
                break;
            case -970604592:
                if (str3.equals("sendGPSInfo")) {
                    c2 = 2;
                    break;
                }
                c2 = 65535;
                break;
            case -742323277:
                if (str3.equals("sendBatteryInfo")) {
                    c2 = 3;
                    break;
                }
                c2 = 65535;
                break;
            case -247303947:
                if (str3.equals("deletePhoneById")) {
                    c2 = 4;
                    break;
                }
                c2 = 65535;
                break;
            case 17356374:
                if (str3.equals("sendVRCMD")) {
                    c2 = 5;
                    break;
                }
                c2 = 65535;
                break;
            case 317077362:
                if (str3.equals("notifyIsCallHungUp")) {
                    c2 = 6;
                    break;
                }
                c2 = 65535;
                break;
            case 523235574:
                if (str3.equals("sendAccelerationInfo")) {
                    c2 = 7;
                    break;
                }
                c2 = 65535;
                break;
            case 530405532:
                if (str3.equals("disconnect")) {
                    c2 = '\b';
                    break;
                }
                c2 = 65535;
                break;
            case 773108378:
                if (str3.equals("sendMicRecordData")) {
                    c2 = '\t';
                    break;
                }
                c2 = 65535;
                break;
            case 899494691:
                if (str3.equals("sendKeyEvent")) {
                    c2 = '\n';
                    break;
                }
                c2 = 65535;
                break;
            case 1620374646:
                if (str3.equals("notifySwitchDayOrNight")) {
                    c2 = 11;
                    break;
                }
                c2 = 65535;
                break;
            case 1625622965:
                if (str3.equals("awakenVoiceAssistant")) {
                    c2 = '\f';
                    break;
                }
                c2 = 65535;
                break;
            case 1767202969:
                if (str3.equals("sendGotoBackground")) {
                    c2 = '\r';
                    break;
                }
                c2 = 65535;
                break;
            case 1873273192:
                if (str3.equals("sendGearStateInfo")) {
                    c2 = 14;
                    break;
                }
                c2 = 65535;
                break;
            case 1936183827:
                if (str3.equals("sendLightsInfo")) {
                    c2 = 15;
                    break;
                }
                c2 = 65535;
                break;
            case 1937757334:
                if (str3.equals("sendLightSensorInfo")) {
                    c2 = 16;
                    break;
                }
                c2 = 65535;
                break;
            default:
                c2 = 65535;
                break;
        }
        switch (c2) {
            case 0:
                UCarAdapter.getInstance().sendGyroscopeInfo(new UCarCommon.GyroscopeInfo(Integer.parseInt((String) list.get(1)), Double.parseDouble((String) list.get(2)), Double.parseDouble((String) list.get(3)), Double.parseDouble((String) list.get(4)), System.currentTimeMillis()));
                i = 1;
                break;
            case 1:
                UCarAdapter.getInstance().sendOilInfo(new UCarCommon.OilInfo(Integer.parseInt((String) list.get(1)), 0, Integer.parseInt((String) list.get(2))));
                i = 1;
                break;
            case 2:
                UCarAdapter.getInstance().sendGPSInfo(new UCarCommon.GPSInfo(Double.parseDouble((String) list.get(1)), Double.parseDouble((String) list.get(2)), Double.parseDouble((String) list.get(3)), Integer.parseInt((String) list.get(4)), Integer.parseInt((String) list.get(5)), Integer.parseInt((String) list.get(6)), Integer.parseInt((String) list.get(7)), Integer.parseInt((String) list.get(8)), Integer.parseInt((String) list.get(9)), System.currentTimeMillis()));
                i = 1;
                break;
            case 3:
                UCarAdapter.getInstance().sendBatteryInfo(null);
                i = 1;
                break;
            case 4:
                UCarAdapter.deletePhoneById(this.b, (String) list.get(1));
                i = 1;
                break;
            case 5:
                UCarAdapter.getInstance().sendVRCMD(UCarCommon.VRCmdType.fromInt(Integer.parseInt((String) list.get(1))), (String) list.get(2));
                i = 1;
                break;
            case 6:
                UCarAdapter.getInstance().notifyHungUpCall();
                i = 1;
                break;
            case 7:
                UCarAdapter.getInstance().sendAccelerationInfo(new UCarCommon.AccelerationInfo(Double.parseDouble((String) list.get(1)), Double.parseDouble((String) list.get(2)), Double.parseDouble((String) list.get(3)), System.currentTimeMillis()));
                i = 1;
                break;
            case '\b':
                UCarAdapter.getInstance().disconnect();
                i = 1;
                break;
            case '\t':
                List list2 = (List) list.subList(2, list.size()).stream().map($$Lambda$c$Te2CdEtOpXROFrYBZ4wMvIyqrA.INSTANCE).collect(Collectors.toList());
                short[] sArr = new short[list2.size()];
                while (i < list2.size()) {
                    sArr[i] = ((Short) list2.get(i)).shortValue();
                    i++;
                }
                c();
                UCarAdapter.getInstance().sendMicRecordData(Integer.parseInt((String) list.get(1)), sArr, System.currentTimeMillis());
                b();
                i = 1;
                break;
            case '\n':
                UCarAdapter.getInstance().sendKeyEvent(UCarCommon.KeyEventActionType.fromInt(Integer.parseInt((String) list.get(1))), UCarCommon.KeyCodeType.fromInt(Integer.parseInt((String) list.get(2))), 0);
                i = 1;
                break;
            case 11:
                UCarAdapter.getInstance().notifySwitchDayOrNight(UCarCommon.DayNightMode.fromInt(Integer.parseInt((String) list.get(1))));
                i = 1;
                break;
            case '\f':
                UCarAdapter.getInstance().awakenVoiceAssistant(((String) list.get(5)).getBytes(StandardCharsets.UTF_8), new UCarCommon.AudioFormat(String.valueOf(list.get(1)), Integer.parseInt((String) list.get(2)), Integer.parseInt((String) list.get(3)), Integer.parseInt((String) list.get(4))), (String) list.get(6));
                i = 1;
                break;
            case '\r':
                UCarAdapter.getInstance().pauseCast();
                i = 1;
                break;
            case 14:
                UCarAdapter.getInstance().sendGearStateInfo(new UCarCommon.GearStateInfo(UCarCommon.GearState.fromInt(Integer.parseInt((String) list.get(1))), Integer.parseInt((String) list.get(2))));
                i = 1;
                break;
            case 15:
                UCarAdapter.getInstance().sendLightsInfo(new UCarCommon.LightsInfo(Boolean.parseBoolean((String) list.get(1)), Boolean.parseBoolean((String) list.get(2)), Boolean.parseBoolean((String) list.get(3)), Boolean.parseBoolean((String) list.get(4)), Boolean.parseBoolean((String) list.get(5))));
                i = 1;
                break;
            case 16:
                UCarAdapter.getInstance().sendLightSensorInfo(new UCarCommon.LightSensorInfo(Double.parseDouble((String) list.get(1)), Double.parseDouble((String) list.get(2)), Double.parseDouble((String) list.get(3))));
                i = 1;
                break;
            default:
                Log.d(this.f162a, "parse API failed, please check sender, api:" + str3);
                break;
        }
        String str4 = this.f162a;
        if (i != 0) {
            sb = new StringBuilder().append(str3);
            str2 = " executed";
        } else {
            sb = new StringBuilder().append(str3);
            str2 = " execute failed";
        }
        Log.d(str4, sb.append(str2).toString());
    }

    /* JADX WARNING: Removed duplicated region for block: B:49:0x00c1 A[SYNTHETIC, Splitter:B:49:0x00c1] */
    /* JADX WARNING: Removed duplicated region for block: B:54:0x00c9 A[Catch:{ IOException -> 0x00c5 }] */
    /* JADX WARNING: Removed duplicated region for block: B:56:0x00ce A[Catch:{ IOException -> 0x00c5 }] */
    /* JADX WARNING: Removed duplicated region for block: B:58:0x00d3 A[Catch:{ IOException -> 0x00c5 }] */
    /* JADX WARNING: Removed duplicated region for block: B:63:0x00e3 A[SYNTHETIC, Splitter:B:63:0x00e3] */
    /* JADX WARNING: Removed duplicated region for block: B:68:0x00eb A[Catch:{ IOException -> 0x00e7 }] */
    /* JADX WARNING: Removed duplicated region for block: B:70:0x00f0 A[Catch:{ IOException -> 0x00e7 }] */
    /* JADX WARNING: Removed duplicated region for block: B:72:0x00f5 A[Catch:{ IOException -> 0x00e7 }] */
    /* JADX WARNING: Removed duplicated region for block: B:81:0x000d A[SYNTHETIC] */
    private void d() {
        BufferedReader bufferedReader;
        InputStream inputStream;
        Socket socket;
        Throwable th;
        Exception e2;
        String str;
        InputStream inputStream2;
        try {
            this.c = new ServerSocket(h);
            this.f = true;
            while (this.f) {
                InputStreamReader inputStreamReader = null;
                try {
                    Log.d(this.f162a, "waiting for in socket");
                    Socket accept = this.c.accept();
                    try {
                        inputStream2 = accept.getInputStream();
                    } catch (Exception e3) {
                        inputStream = null;
                        socket = accept;
                        e2 = e3;
                        bufferedReader = null;
                        try {
                            Log.e(this.f162a, e2.getMessage(), e2);
                            if (inputStreamReader != null) {
                            }
                            if (inputStream != null) {
                            }
                            if (socket != null) {
                            }
                            if (bufferedReader != null) {
                            }
                        } catch (Throwable th2) {
                            th = th2;
                            if (inputStreamReader != null) {
                                try {
                                    inputStreamReader.close();
                                } catch (IOException e4) {
                                    Log.e(this.f162a, e4.getMessage(), e4);
                                    throw th;
                                }
                            }
                            if (inputStream != null) {
                                inputStream.close();
                            }
                            if (socket != null) {
                                socket.close();
                            }
                            if (bufferedReader != null) {
                                bufferedReader.close();
                            }
                            throw th;
                        }
                    } catch (Throwable th3) {
                        inputStream = null;
                        socket = accept;
                        th = th3;
                        bufferedReader = null;
                        if (inputStreamReader != null) {
                        }
                        if (inputStream != null) {
                        }
                        if (socket != null) {
                        }
                        if (bufferedReader != null) {
                        }
                        throw th;
                    }
                    try {
                        InputStreamReader inputStreamReader2 = new InputStreamReader(inputStream2);
                        try {
                            bufferedReader = new BufferedReader(inputStreamReader2);
                            String str2 = "";
                            while (true) {
                                try {
                                    String readLine = bufferedReader.readLine();
                                    if (readLine == null) {
                                        break;
                                    }
                                    Log.d(this.f162a, "received: " + readLine);
                                    str2 = readLine;
                                } catch (Exception e5) {
                                    e2 = e5;
                                    inputStreamReader = inputStreamReader2;
                                    inputStream = inputStream2;
                                    socket = accept;
                                    Log.e(this.f162a, e2.getMessage(), e2);
                                    if (inputStreamReader != null) {
                                    }
                                    if (inputStream != null) {
                                    }
                                    if (socket != null) {
                                    }
                                    if (bufferedReader != null) {
                                    }
                                } catch (Throwable th4) {
                                    th = th4;
                                    inputStreamReader = inputStreamReader2;
                                    inputStream = inputStream2;
                                    socket = accept;
                                    if (inputStreamReader != null) {
                                    }
                                    if (inputStream != null) {
                                    }
                                    if (socket != null) {
                                    }
                                    if (bufferedReader != null) {
                                    }
                                    throw th;
                                }
                            }
                            bufferedReader.close();
                            inputStreamReader2.close();
                            inputStream2.close();
                            accept.close();
                            c(str2);
                            try {
                                inputStreamReader2.close();
                                inputStream2.close();
                                accept.close();
                                bufferedReader.close();
                            } catch (IOException e6) {
                                e = e6;
                                str = this.f162a;
                            }
                        } catch (Exception e7) {
                            bufferedReader = null;
                            inputStreamReader = inputStreamReader2;
                            inputStream = inputStream2;
                            socket = accept;
                            e2 = e7;
                            Log.e(this.f162a, e2.getMessage(), e2);
                            if (inputStreamReader != null) {
                                try {
                                    inputStreamReader.close();
                                } catch (IOException e8) {
                                    e = e8;
                                    str = this.f162a;
                                    Log.e(str, e.getMessage(), e);
                                }
                            }
                            if (inputStream != null) {
                                inputStream.close();
                            }
                            if (socket != null) {
                                socket.close();
                            }
                            if (bufferedReader != null) {
                                bufferedReader.close();
                            }
                        } catch (Throwable th5) {
                            bufferedReader = null;
                            inputStreamReader = inputStreamReader2;
                            inputStream = inputStream2;
                            socket = accept;
                            th = th5;
                            if (inputStreamReader != null) {
                            }
                            if (inputStream != null) {
                            }
                            if (socket != null) {
                            }
                            if (bufferedReader != null) {
                            }
                            throw th;
                        }
                    } catch (Exception e9) {
                        socket = accept;
                        e2 = e9;
                        inputStream = inputStream2;
                        bufferedReader = null;
                        Log.e(this.f162a, e2.getMessage(), e2);
                        if (inputStreamReader != null) {
                        }
                        if (inputStream != null) {
                        }
                        if (socket != null) {
                        }
                        if (bufferedReader != null) {
                        }
                    } catch (Throwable th6) {
                        socket = accept;
                        th = th6;
                        inputStream = inputStream2;
                        bufferedReader = null;
                        if (inputStreamReader != null) {
                        }
                        if (inputStream != null) {
                        }
                        if (socket != null) {
                        }
                        if (bufferedReader != null) {
                        }
                        throw th;
                    }
                } catch (Exception e10) {
                    e2 = e10;
                    socket = null;
                    inputStream = null;
                    bufferedReader = null;
                    Log.e(this.f162a, e2.getMessage(), e2);
                    if (inputStreamReader != null) {
                    }
                    if (inputStream != null) {
                    }
                    if (socket != null) {
                    }
                    if (bufferedReader != null) {
                    }
                } catch (Throwable th7) {
                    th = th7;
                    socket = null;
                    inputStream = null;
                    bufferedReader = null;
                    if (inputStreamReader != null) {
                    }
                    if (inputStream != null) {
                    }
                    if (socket != null) {
                    }
                    if (bufferedReader != null) {
                    }
                    throw th;
                }
            }
            Log.d(this.f162a, "service stoped");
        } catch (Exception e11) {
            Log.e(this.f162a, e11.getMessage(), e11);
        }
    }

    /* renamed from: lambda$Te2CdEtOpXRO-FrYBZ4wMvIyqrA  reason: not valid java name */
    public static /* synthetic */ Short m11lambda$Te2CdEtOpXROFrYBZ4wMvIyqrA(String str) {
        return b(str);
    }

    public static /* synthetic */ boolean lambda$xc6Wi6EHaZIjOTD4xeZRvT8R9r4(String str) {
        return a(str);
    }

    public void a() {
        try {
            Log.d(this.f162a, "close");
            this.f = false;
            this.c.close();
        } catch (IOException e2) {
            Log.d(this.f162a, e2.getMessage(), e2);
        }
    }

    public void run() {
        if (!this.e) {
            this.e = true;
            d();
            return;
        }
        Log.d(this.f162a, "is Running, ignore");
    }
}
