package a.c.d.g;

import a.c.b.s;
import a.c.b.z.f;
import a.c.d.c.a;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Range;
import android.util.Size;
import com.google.protobuf.ByteString;
import com.ucar.databus.proto.UCarProto;
import com.ucar.sdk.BuildConfig;
import com.ucar.vehiclesdk.MDevice;
import com.ucar.vehiclesdk.UCarCommon;
import com.ucar.vehiclesdk.UCarConfig;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class a {
    private static final String p = "ControlManager";
    private static final int q = 500;
    private static final int r = 3000;
    private static final int s = 268435456;
    private static final int t = 536870912;
    private static final int u = -1;
    private static final int v = 1000;
    private static final int w = 1;
    private static final int x = 5;

    /* renamed from: a  reason: collision with root package name */
    private a.b.a.g.a f113a;
    private HandlerThread b;
    private Handler c;
    private boolean d = false;
    private Thread e;
    private boolean f;
    private final Object g = new Object();
    private UCarCommon.PhoneStateInfo h;
    private UCarCommon.CallState i;
    private String j = null;
    private UCarCommon.MusicInfo k;
    private CountDownLatch l;
    private int m;
    private volatile boolean n = false;
    private t o;

    /* renamed from: a.c.d.g.a$a  reason: collision with other inner class name */
    public class RunnableC0013a implements Runnable {

        /* renamed from: a  reason: collision with root package name */
        public final /* synthetic */ u f114a;

        public RunnableC0013a(u uVar) {
            this.f114a = uVar;
        }

        public void run() {
            while (a.this.f) {
                try {
                    a.this.a((a) this.f114a);
                    Thread.sleep(2000);
                } catch (Exception e) {
                    a.a.a.b.b(a.p, "startSendHeartBeatThread Exception", e);
                }
            }
        }
    }

    public class b implements a.c.b.z.e {
        public b() {
        }

        @Override // a.c.b.z.e
        public void a(Boolean bool) {
            a.a.a.b.c(a.p, "add camera succeeded");
        }

        @Override // a.c.b.z.c
        public void a(Exception exc) {
            a.a.a.b.b(a.p, "failed to add camera");
        }
    }

    public class c implements a.c.b.z.e {
        public c() {
        }

        @Override // a.c.b.z.e
        public void a(Boolean bool) {
            a.a.a.b.c(a.p, "remove camera succeeded");
        }

        @Override // a.c.b.z.c
        public void a(Exception exc) {
            a.a.a.b.b(a.p, "failed to remove camera");
        }
    }

    public class d implements a.c.b.z.e {
        public d() {
        }

        @Override // a.c.b.z.e
        public void a(Boolean bool) {
            a.a.a.b.c(a.p, "notify camera state succeeded");
        }

        @Override // a.c.b.z.c
        public void a(Exception exc) {
            a.a.a.b.b(a.p, "failed to notify camera state");
        }
    }

    public class e implements a.c.b.z.e {
        public e() {
        }

        @Override // a.c.b.z.e
        public void a(Boolean bool) {
            a.a.a.b.c(a.p, "sendGetUCarConfigResponse succeed");
        }

        @Override // a.c.b.z.c
        public void a(Exception exc) {
            a.a.a.b.b(a.p, "failed to sendGetUCarConfigResponse");
        }
    }

    public class f implements a.c.b.z.e {
        public f() {
        }

        @Override // a.c.b.z.e
        public void a(Boolean bool) {
            a.a.a.b.c(a.p, "sendDisconnect succeed");
        }

        @Override // a.c.b.z.c
        public void a(Exception exc) {
            a.a.a.b.b(a.p, "failed to sendDisconnect", exc);
        }
    }

    public class g implements a.c.b.z.c<UCarProto.GetAppListResponse> {
        public g() {
        }

        public void a(UCarProto.GetAppListResponse getAppListResponse) {
            a.a.a.b.c(a.p, "GetAppList succeed");
        }

        @Override // a.c.b.z.c
        public void a(Exception exc) {
            a.a.a.b.c(a.p, "GetAppList failed: ", exc);
        }

        /* renamed from: b */
        public UCarProto.GetAppListResponse a(a.c.b.s sVar) {
            return a.c.b.r.n(sVar);
        }
    }

    public class h implements a.c.b.z.c<UCarProto.GetAppDetailInfoResponse> {
        public h() {
        }

        public void a(UCarProto.GetAppDetailInfoResponse getAppDetailInfoResponse) {
            a.a.a.b.c(a.p, "getAppDetailInfo succeed");
        }

        @Override // a.c.b.z.c
        public void a(Exception exc) {
            a.a.a.b.c(a.p, "getAppDetailInfo failed: ", exc);
        }

        /* renamed from: b */
        public UCarProto.GetAppDetailInfoResponse a(a.c.b.s sVar) {
            return a.c.b.r.l(sVar);
        }
    }

    public class i implements a.c.b.z.e {
        public i() {
        }

        @Override // a.c.b.z.e
        public void a(Boolean bool) {
            a.a.a.b.c(a.p, "send InvokeApp succeed");
        }

        @Override // a.c.b.z.c
        public void a(Exception exc) {
            a.a.a.b.b(a.p, "failed to send InvokeApp", exc);
        }
    }

    public class j implements a.c.b.z.e {
        public j() {
        }

        @Override // a.c.b.z.e
        public void a(Boolean bool) {
            a.a.a.b.c(a.p, "send CallPickUp succeed");
        }

        @Override // a.c.b.z.c
        public void a(Exception exc) {
            a.a.a.b.b(a.p, "failed to send CallPickUp", exc);
        }
    }

    public class k extends a.b.a.g.a {
        public k(a.c.b.z.b bVar, boolean z, boolean z2) {
            super(bVar, z, z2);
        }

        @Override // a.c.b.z.g.e, a.c.b.z.f
        public void h() {
            super.h();
            synchronized (a.this.g) {
                if (a.this.l != null) {
                    a.this.l.countDown();
                }
            }
        }
    }

    public class l implements a.c.b.z.e {
        public l() {
        }

        @Override // a.c.b.z.e
        public void a(Boolean bool) {
            a.a.a.b.c(a.p, "sendGotoForeground succeed");
        }

        @Override // a.c.b.z.c
        public void a(Exception exc) {
            a.a.a.b.b(a.p, "failed to sendGotoForeground", exc);
        }
    }

    public class m implements a.c.b.z.e {
        public m() {
        }

        @Override // a.c.b.z.e
        public void a(Boolean bool) {
            a.a.a.b.c(a.p, "sendGotoBackground succeed");
        }

        @Override // a.c.b.z.c
        public void a(Exception exc) {
            a.a.a.b.b(a.p, "failed to sendGotoBackground");
        }
    }

    public class n implements a.c.b.z.e {
        public n() {
        }

        @Override // a.c.b.z.e
        public void a(Boolean bool) {
            a.a.a.b.c(a.p, "sendKeyEventToPhone succeed");
        }

        @Override // a.c.b.z.c
        public void a(Exception exc) {
            a.a.a.b.b(a.p, "failed to sendKeyEventToPhone");
        }
    }

    public class o implements a.c.b.z.e {
        public o() {
        }

        @Override // a.c.b.z.e
        public void a(Boolean bool) {
            a.a.a.b.c(a.p, "sendVRCmdToPhone succeed");
        }

        @Override // a.c.b.z.c
        public void a(Exception exc) {
            a.a.a.b.b(a.p, "failed to sendVRCmdToPhone");
        }
    }

    public class p implements a.c.b.z.e {
        public p() {
        }

        @Override // a.c.b.z.e
        public void a(Boolean bool) {
            a.a.a.b.c(a.p, "sendCallHungUp succeed");
        }

        @Override // a.c.b.z.c
        public void a(Exception exc) {
            a.a.a.b.b(a.p, "failed to sendCallHungUp");
        }
    }

    public class q implements a.c.b.z.e {
        public q() {
        }

        @Override // a.c.b.z.e
        public void a(Boolean bool) {
            a.a.a.b.c(a.p, "sendSwitchDayOrNight succeed");
        }

        @Override // a.c.b.z.c
        public void a(Exception exc) {
            a.a.a.b.b(a.p, "failed to sendSwitchDayOrNight");
        }
    }

    public class r implements a.c.b.z.e {
        public r() {
        }

        @Override // a.c.b.z.e
        public void a(Boolean bool) {
            a.a.a.b.c(a.p, "sendAwakenVoiceAssistant succeed");
        }

        @Override // a.c.b.z.c
        public void a(Exception exc) {
            a.a.a.b.b(a.p, "failed to sendAwakenVoiceAssistant");
        }
    }

    public class s implements a.c.b.z.e {

        /* renamed from: a  reason: collision with root package name */
        public final /* synthetic */ u f131a;
        public final /* synthetic */ UCarProto.Heartbeat b;

        public s(u uVar, UCarProto.Heartbeat heartbeat) {
            this.f131a = uVar;
            this.b = heartbeat;
        }

        @Override // a.c.b.z.e
        public void a(Boolean bool) {
            a.a.a.b.a(a.p, "sendHeartBeat to phone succeeded," + this.b.getTimestamp());
        }

        @Override // a.c.b.z.c
        public void a(Exception exc) {
            a.a.a.b.b(a.p, "failed to sendHeartBeat");
            u uVar = this.f131a;
            if (uVar != null) {
                uVar.a();
            }
            a.this.j();
        }
    }

    public interface t {
        void a();

        void a(int i, boolean z, int i2, UCarCommon.AppState appState, long j);

        void a(long j);

        void a(a.c.b.s sVar);

        void a(a.b bVar);

        void a(UCarCommon.AudioType audioType, UCarCommon.AudioFormat audioFormat, a.d dVar);

        void a(UCarCommon.BluetoothMacInfo bluetoothMacInfo);

        void a(UCarCommon.CallInfo callInfo);

        void a(UCarCommon.CameraAction cameraAction, UCarCommon.CameraActionArgs cameraActionArgs);

        void a(UCarCommon.MusicInfo musicInfo);

        void a(UCarCommon.NavigationInfo navigationInfo);

        void a(UCarCommon.POIAddress pOIAddress);

        void a(UCarCommon.PhoneStateInfo phoneStateInfo);

        void a(List<Integer> list, UCarCommon.AppListState appListState);

        void a(boolean z, UCarCommon.AudioFormat audioFormat, boolean z2);

        void b();

        void c();

        void d();
    }

    public interface u {
        void a();
    }

    public a() {
        a();
        k kVar = new k(a.c.b.z.b.CONTROL, false, true);
        this.f113a = kVar;
        kVar.a(new f.a() {
            /* class a.c.d.g.$$Lambda$a$5xZt06k2ljtvIX4rye_0hTAe51k */

            @Override // a.c.b.z.f.a
            public final void a(s sVar) {
                a.lambda$5xZt06k2ljtvIX4rye_0hTAe51k(a.this, sVar);
            }
        });
        this.h = new UCarCommon.PhoneStateInfo();
    }

    private int a(UCarProto.ChannelMask channelMask) {
        if (channelMask.equals(UCarProto.ChannelMask.CHANNEL_MONO)) {
            return 16;
        }
        return channelMask.equals(UCarProto.ChannelMask.CHANNEL_STEREO) ? 12 : -1;
    }

    private int a(UCarProto.EncodingFormat encodingFormat) {
        if (encodingFormat.equals(UCarProto.EncodingFormat.ENCODING_PCM_8BIT)) {
            return 3;
        }
        if (encodingFormat.equals(UCarProto.EncodingFormat.ENCODING_PCM_16BIT)) {
            return 2;
        }
        return (!encodingFormat.equals(UCarProto.EncodingFormat.ENCODING_PCM_24BIT_PACKED) && !encodingFormat.equals(UCarProto.EncodingFormat.ENCODING_PCM_32BIT) && encodingFormat.equals(UCarProto.EncodingFormat.ENCODING_PCM_FLOAT)) ? 4 : -1;
    }

    private int a(UCarProto.SampleRate sampleRate) {
        return sampleRate.getNumber();
    }

    private UCarProto.ChannelMask a(int i2) {
        if (i2 != 4) {
            if (i2 == 12) {
                return UCarProto.ChannelMask.CHANNEL_STEREO;
            }
            if (i2 != 16) {
                return UCarProto.ChannelMask.UNKNOWN_CHANNEL;
            }
        }
        return UCarProto.ChannelMask.CHANNEL_MONO;
    }

    private UCarCommon.MusicInfo a(UCarProto.NotifyMusicInfo notifyMusicInfo) {
        UCarCommon.MusicInfo musicInfo;
        String b2 = b(notifyMusicInfo);
        boolean z = true;
        if (!TextUtils.equals(b2, this.j) || (musicInfo = this.k) == null) {
            UCarCommon.MusicInfo musicInfo2 = new UCarCommon.MusicInfo(notifyMusicInfo.getArtistName(), notifyMusicInfo.getAlbumName(), notifyMusicInfo.getCoverArt(), notifyMusicInfo.getLyrics(), notifyMusicInfo.getPlayingTimesMs(), notifyMusicInfo.getTitle(), notifyMusicInfo.getAuthorName(), notifyMusicInfo.getWriterName(), notifyMusicInfo.getComposerName(), notifyMusicInfo.getPlayingCurrentTimeMs(), notifyMusicInfo.getIsFavorite(), notifyMusicInfo.getIsPlaying());
            if (notifyMusicInfo.hasCoverArtBitmap() && notifyMusicInfo.getCoverArtBitmap() != null) {
                musicInfo2.setCover(notifyMusicInfo.getCoverArtBitmap().toByteArray());
            }
            if (notifyMusicInfo.hasId()) {
                int id = notifyMusicInfo.getId();
                if (id != -1) {
                    z = false;
                }
                musicInfo2.setDestroyed(z);
                if (id == -1) {
                    musicInfo2.setPlaying(false);
                } else {
                    musicInfo2.setAppId(id);
                }
            }
            this.j = b2;
            this.k = musicInfo2;
            return musicInfo2;
        }
        musicInfo.setPlaying(notifyMusicInfo.getIsPlaying());
        this.k.setPlayingCurrentTimeMs(notifyMusicInfo.getPlayingCurrentTimeMs());
        this.k.setLyrics(notifyMusicInfo.getLyrics());
        if (notifyMusicInfo.hasId()) {
            int id2 = notifyMusicInfo.getId();
            UCarCommon.MusicInfo musicInfo3 = this.k;
            if (id2 != -1) {
                z = false;
            }
            musicInfo3.setDestroyed(z);
            if (id2 == -1) {
                this.k.setPlaying(false);
            } else {
                this.k.setAppId(id2);
            }
        }
        return this.k;
    }

    private void a() {
        boolean z;
        if (!this.d) {
            a.a.a.b.a(p, "start worker thread");
            HandlerThread handlerThread = new HandlerThread("ControlManager_WorkerThread");
            this.b = handlerThread;
            handlerThread.start();
            Looper looper = this.b.getLooper();
            if (looper != null) {
                this.c = new Handler(looper);
                z = true;
            } else {
                a.a.a.b.b(p, "failed to get valid worker thread looper!");
                this.c = null;
                z = false;
            }
            this.d = z;
        }
    }

    private void a(a.c.b.s sVar) {
        UCarProto.AppListChanged c2 = a.c.b.r.c(sVar);
        if (c2 == null) {
            a.a.a.b.b(p, "app list state cannot be null");
        } else if (this.o != null) {
            int stateValue = c2.getStateValue();
            UCarCommon.AppListState.values();
            if (stateValue < 4) {
                this.o.a(c2.getIdList(), UCarCommon.AppListState.values()[c2.getStateValue()]);
            }
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void a(u uVar) {
        a.a.a.b.a(p, "sendHeartBeat");
        if (d()) {
            UCarProto.Heartbeat heartbeat = (UCarProto.Heartbeat) UCarProto.Heartbeat.newBuilder().setTimestamp(System.currentTimeMillis()).build();
            this.f113a.a(a.c.b.r.a(heartbeat), (a.c.b.z.e) new s(uVar, heartbeat));
        }
    }

    private void a(UCarCommon.CallState callState) {
        boolean z = true;
        if (a.c.b.m.a(this.m) <= 1 && a.c.b.m.b(this.m) < 5) {
            UCarCommon.CallState callState2 = this.i;
            if (callState2 == null || callState2 != callState) {
                this.i = callState;
                if (this.o != null) {
                    boolean isVoipCall = this.h.isVoipCall();
                    if (this.n || callState == UCarCommon.CallState.UNKNOWN_STATE || callState == UCarCommon.CallState.IDLE) {
                        z = false;
                    }
                    this.o.a(new UCarCommon.CallInfo(callState, "", "", isVoipCall, 0, z, 0));
                }
            }
        }
    }

    private int b(UCarProto.ChannelMask channelMask) {
        if (channelMask.equals(UCarProto.ChannelMask.CHANNEL_MONO)) {
            return 4;
        }
        return channelMask.equals(UCarProto.ChannelMask.CHANNEL_STEREO) ? 12 : -1;
    }

    private UCarProto.EncodingFormat b(int i2) {
        return i2 != 2 ? i2 != 3 ? i2 != 4 ? i2 != s ? i2 != t ? UCarProto.EncodingFormat.UNKNOWN_FORMAT : UCarProto.EncodingFormat.ENCODING_PCM_32BIT : UCarProto.EncodingFormat.ENCODING_PCM_24BIT_PACKED : UCarProto.EncodingFormat.ENCODING_PCM_FLOAT : UCarProto.EncodingFormat.ENCODING_PCM_8BIT : UCarProto.EncodingFormat.ENCODING_PCM_16BIT;
    }

    private static String b(UCarProto.NotifyMusicInfo notifyMusicInfo) {
        return notifyMusicInfo.getTitle() + notifyMusicInfo.getAlbumName() + notifyMusicInfo.getArtistName() + notifyMusicInfo.getPlayingTimesMs();
    }

    private void b(a.c.b.s sVar) {
        UCarProto.AppStateChanged d2 = a.c.b.r.d(sVar);
        if (d2 == null) {
            a.a.a.b.b(p, "app state cannot be null");
        } else if (this.o != null) {
            int stateValue = d2.getStateValue();
            UCarCommon.AppState.values();
            if (stateValue < 5) {
                this.o.a(d2.getId(), d2.getIsNeedShow(), d2.getDisplayMode(), UCarCommon.AppState.values()[d2.getStateValue()], d2.getTimestamp());
            }
        }
    }

    private long c() {
        return System.currentTimeMillis();
    }

    private UCarProto.SampleRate c(int i2) {
        return UCarProto.SampleRate.forNumber(i2);
    }

    private void c(a.c.b.s sVar) {
        a.a.a.b.a(p, "handleAudioPlayerControl");
        if (this.o == null) {
            a.a.a.b.c(p, "mPhoneCmdListener is null, ignore AudioPlayerControl");
            return;
        }
        UCarProto.AudioPlayerControl e2 = a.c.b.r.e(sVar);
        UCarCommon.AudioType fromInt = UCarCommon.AudioType.fromInt(e2.getAudioType().getNumber());
        a.b bVar = new a.b();
        bVar.a(fromInt);
        bVar.a(e2.getBufferingCount());
        bVar.b(e2.getSpeedAdjustStep());
        this.o.a(bVar);
    }

    private static UCarCommon.CallState d(int i2) {
        return i2 == 1 ? UCarCommon.CallState.IDLE : i2 == 2 ? UCarCommon.CallState.INCOMING : i2 == 3 ? UCarCommon.CallState.ACTIVE : i2 == 4 ? UCarCommon.CallState.INCOMING : i2 == 5 ? UCarCommon.CallState.DIALING : i2 == 6 ? UCarCommon.CallState.ALERTING : i2 == 7 ? UCarCommon.CallState.ACTIVE : i2 == 8 ? UCarCommon.CallState.HOLDING : UCarCommon.CallState.UNKNOWN_STATE;
    }

    private void d(a.c.b.s sVar) {
        a.a.a.b.a(p, "handleAudioPlayerState");
        UCarProto.NotifyAudioPlayerState v2 = a.c.b.r.v(sVar);
        if (v2 == null) {
            a.a.a.b.b(p, "audio player cannot be null");
            return;
        }
        int b2 = b(v2.getChannelMask());
        int a2 = a(v2.getEncodingFormat());
        int a3 = a(v2.getSampleRate());
        UCarCommon.AudioType fromInt = UCarCommon.AudioType.fromInt(v2.getTypeValue());
        UCarCommon.AudioFormat audioFormat = new UCarCommon.AudioFormat(v2.hasMimeType() ? v2.getMimeType() : UCarCommon.AudioFormat.MIME_TYPE_PCM, a2, a3, b2);
        a.d a4 = a.d.a(v2.getStateValue());
        t tVar = this.o;
        if (tVar != null) {
            tVar.a(fromInt, audioFormat, a4);
        }
    }

    private boolean d() {
        a.b.a.g.a aVar = this.f113a;
        return aVar != null && aVar.b();
    }

    private void e(a.c.b.s sVar) {
        a.a.a.b.a(p, "handleBluetoothMacInfo");
        UCarProto.BluetoothMacInfo g2 = a.c.b.r.g(sVar);
        if (g2 == null) {
            a.a.a.b.b(p, "camera state cannot be null");
            return;
        }
        UCarCommon.BluetoothMacInfo bluetoothMacInfo = new UCarCommon.BluetoothMacInfo(UCarCommon.OPType.fromType(g2.getOpTypeValue()), g2.getBluetoothMac());
        t tVar = this.o;
        if (tVar != null) {
            tVar.a(bluetoothMacInfo);
        }
    }

    private void f(a.c.b.s sVar) {
        UCarProto.CallInfo h2 = a.c.b.r.h(sVar);
        if (h2 == null) {
            a.a.a.b.b(p, "call info cannot be null");
        } else if (this.o != null) {
            this.o.a(new UCarCommon.CallInfo(d(h2.getCallStateValue()), h2.getNumber(), h2.getName(), h2.getIsIPCall(), h2.getConnectedTime(), h2.getIsNeedShowFloatingWindow(), h2.getInCallAppId()));
        }
    }

    private void g(a.c.b.s sVar) {
        a.a.a.b.a(p, "handleCameraState");
        UCarProto.SetCameraState J = a.c.b.r.J(sVar);
        if (J == null) {
            a.a.a.b.b(p, "camera state cannot be null");
            return;
        }
        UCarCommon.CameraActionArgs cameraActionArgs = new UCarCommon.CameraActionArgs(J.getCameraId(), new Range(Integer.valueOf(J.getFpsRange().getMin()), Integer.valueOf(J.getFpsRange().getMax())), new Size(J.getPictureSize().getWidth(), J.getPictureSize().getHeight()));
        t tVar = this.o;
        if (tVar != null) {
            tVar.a(UCarCommon.CameraAction.fromInt(J.getActionValue()), cameraActionArgs);
        }
    }

    private void h(a.c.b.s sVar) {
        t tVar = this.o;
        if (tVar != null) {
            tVar.a(sVar);
        }
    }

    private void i(a.c.b.s sVar) {
        a.a.a.b.a(p, "handleMicroPhoneState");
        UCarProto.NotifyMicrophoneState A = a.c.b.r.A(sVar);
        if (A == null) {
            a.a.a.b.b(p, "micro phone state cannot be null");
            return;
        }
        int a2 = a(A.getChannelMask());
        int a3 = a(A.getEncodingFormat());
        int a4 = a(A.getSampleRate());
        boolean state = A.getState();
        UCarCommon.AudioFormat audioFormat = new UCarCommon.AudioFormat(UCarCommon.AudioFormat.MIME_TYPE_PCM, a3, a4, a2);
        boolean z = !UCarCommon.CallState.IDLE.equals(this.h.getModemCallState()) || this.h.isVoipCall();
        t tVar = this.o;
        if (tVar != null) {
            tVar.a(state, audioFormat, z);
        }
        this.h.setUseMicrophone(state);
        l();
    }

    private void j(a.c.b.s sVar) {
        UCarProto.NotifyMirrorState B = a.c.b.r.B(sVar);
        if (B == null) {
            a.a.a.b.b(p, "mirror state cannot be null");
        } else if (this.o != null) {
            int mirrorStateValue = B.getMirrorStateValue();
            a.a.a.b.a(p, "handleMirrorState" + mirrorStateValue);
            if (mirrorStateValue == 0) {
                this.o.b();
            } else if (mirrorStateValue == 1) {
                this.o.a();
            } else if (mirrorStateValue == 2) {
                this.o.d();
            } else if (mirrorStateValue == 3) {
                this.o.c();
            }
        }
    }

    private void k() {
        HandlerThread handlerThread = this.b;
        if (handlerThread != null && this.d) {
            handlerThread.quitSafely();
            this.b = null;
            this.c = null;
            this.d = false;
        }
    }

    private void k(a.c.b.s sVar) {
        a.a.a.b.a(p, "handleMusicInfo");
        UCarProto.NotifyMusicInfo C = a.c.b.r.C(sVar);
        if (C == null) {
            a.a.a.b.b(p, "music info cannot be null");
            return;
        }
        UCarCommon.MusicInfo a2 = a(C);
        t tVar = this.o;
        if (tVar != null) {
            tVar.a(a2);
        }
    }

    private void l() {
        if (this.o != null) {
            a.a.a.b.a(p, "update phone state:" + this.h);
            this.o.a(this.h);
        }
    }

    private void l(a.c.b.s sVar) {
        a.a.a.b.a(p, "handleNavigationInfo");
        UCarProto.NotifyNavigationInfo D = a.c.b.r.D(sVar);
        if (D == null) {
            a.a.a.b.b(p, "navigation cannot be null");
            return;
        }
        UCarCommon.NavigationInfo navigationInfo = new UCarCommon.NavigationInfo(D.getIsNavigating(), D.getDirectionIcon().toByteArray(), D.getDistance(), D.getDistanceUnit(), D.getOperation(), D.getWhere(), D.getTitle1(), D.getTitle2());
        a.a.a.b.a(p, "navigation info, isNavigating:" + navigationInfo.isNavigating() + ", Dist" + navigationInfo.getDistance() + ", Unit" + navigationInfo.getDistanceUnit() + ", Operation" + navigationInfo.getOperation());
        t tVar = this.o;
        if (tVar != null) {
            tVar.a(navigationInfo);
        }
    }

    private void m(a.c.b.s sVar) {
        UCarProto.POIAddress H = a.c.b.r.H(sVar);
        if (H == null) {
            a.a.a.b.b(p, "poi address cannot be null");
        } else if (this.o != null) {
            UCarCommon.POIAddress pOIAddress = new UCarCommon.POIAddress(H.getName());
            if (H.hasAddress()) {
                pOIAddress.setAddress(H.getAddress());
            }
            if (H.hasPoiInfo()) {
                UCarProto.POIDetailInfo poiInfo = H.getPoiInfo();
                UCarCommon.POIDetailInfo pOIDetailInfo = new UCarCommon.POIDetailInfo(UCarCommon.CoordinateType.BD09, poiInfo.getLatitude(), poiInfo.getLongitude());
                if (poiInfo.hasAltitude()) {
                    pOIDetailInfo.setAltitude(poiInfo.getAltitude());
                }
                pOIAddress.setDetailInfo(pOIDetailInfo);
            }
            this.o.a(pOIAddress);
        }
    }

    private void n(a.c.b.s sVar) {
        a.a.a.b.a(p, "handlePhoneHeartbeat");
        UCarProto.Heartbeat s2 = a.c.b.r.s(sVar);
        if (s2 == null) {
            a.a.a.b.b(p, "heart beat cannot be null");
            return;
        }
        t tVar = this.o;
        if (tVar != null) {
            tVar.a(s2.getTimestamp());
        }
    }

    private void o(a.c.b.s sVar) {
        a.a.a.b.a(p, "handlePhoneState");
        UCarProto.NotifyPhoneState E = a.c.b.r.E(sVar);
        if (E == null) {
            a.a.a.b.b(p, "phone state cannot be null");
            return;
        }
        this.h.setModemCallState(d(E.getCsValue()));
        this.h.setScreenLocked(E.getIsScreenLocked());
        this.h.setVoipCall(E.getIsWechatQqCall());
        this.h.setVoiceAssistantActive(E.getIsVoiceAssistantActive());
        boolean z = true;
        this.h.setLowPower(E.hasIsLowPower() && E.getIsLowPower());
        UCarCommon.PhoneStateInfo phoneStateInfo = this.h;
        if (!E.hasIsOverheat() || !E.getIsOverheat()) {
            z = false;
        }
        phoneStateInfo.setOverheat(z);
        l();
        a(this.h.getModemCallState());
    }

    /* access modifiers changed from: private */
    /* renamed from: p */
    public void q(a.c.b.s sVar) {
        if (!d()) {
            a.a.a.b.b(p, "control socket channel not ready, please check!");
            return;
        }
        int j2 = sVar.j();
        if (j2 == 1) {
            n(sVar);
        } else if (j2 == 17) {
            c(sVar);
        } else if (j2 == 20) {
            g(sVar);
        } else if (j2 == 22) {
            e(sVar);
        } else if (j2 != 24) {
            switch (j2) {
                case 6:
                    i(sVar);
                    return;
                case 7:
                    j(sVar);
                    return;
                case 8:
                    d(sVar);
                    return;
                case 9:
                    o(sVar);
                    return;
                case 10:
                    k(sVar);
                    return;
                case 11:
                    l(sVar);
                    return;
                default:
                    switch (j2) {
                        case 31:
                            b(sVar);
                            return;
                        case 32:
                            a(sVar);
                            return;
                        case 33:
                            m(sVar);
                            return;
                        case 34:
                            f(sVar);
                            return;
                        default:
                            a.a.a.b.b(p, "unknown message method type: " + j2);
                            return;
                    }
            }
        } else {
            h(sVar);
        }
    }

    /* access modifiers changed from: private */
    public /* synthetic */ void r(a.c.b.s sVar) throws InterruptedException {
        if (sVar == null) {
            a.a.a.b.b(p, "received message cannot be null");
            return;
        }
        Handler handler = this.c;
        if (handler != null) {
            handler.post(new Runnable(sVar) {
                /* class a.c.d.g.$$Lambda$a$fZRT71bsf8eQmfvQphhQpHm6KQE */
                public final /* synthetic */ s f$1;

                {
                    this.f$1 = r2;
                }

                public final void run() {
                    a.lambda$fZRT71bsf8eQmfvQphhQpHm6KQE(a.this, this.f$1);
                }
            });
        }
    }

    public ArrayList<UCarCommon.AppDetailInfo> a(List<Integer> list) {
        UCarProto.GetAppDetailInfoResponse getAppDetailInfoResponse;
        String str;
        UCarProto.GetAppDetailInfoRequest getAppDetailInfoRequest = (UCarProto.GetAppDetailInfoRequest) UCarProto.GetAppDetailInfoRequest.newBuilder().addAllId(list).build();
        ArrayList<UCarCommon.AppDetailInfo> arrayList = null;
        if (d()) {
            try {
                getAppDetailInfoResponse = (UCarProto.GetAppDetailInfoResponse) this.f113a.a(a.c.b.r.a(getAppDetailInfoRequest), new h()).get(3000, TimeUnit.MILLISECONDS);
            } catch (TimeoutException unused) {
                str = "get AppDetailInfo ack time out";
            } catch (InterruptedException | ExecutionException e2) {
                a.a.a.b.b(p, "send GetAppDetailInfoRequest exception:", e2);
            }
        } else {
            str = "control channel not ready";
            a.a.a.b.b(p, str);
            getAppDetailInfoResponse = null;
        }
        if (getAppDetailInfoResponse != null && getAppDetailInfoResponse.getTotal() > 0 && getAppDetailInfoResponse.getTotal() == getAppDetailInfoResponse.getAppDetailInfoCount()) {
            arrayList = new ArrayList<>(getAppDetailInfoResponse.getTotal());
            for (int i2 = 0; i2 < getAppDetailInfoResponse.getTotal(); i2++) {
                int categoryValue = getAppDetailInfoResponse.getAppDetailInfo(i2).getCategoryValue();
                UCarCommon.AppCategory.values();
                if (categoryValue < 18) {
                    arrayList.add(new UCarCommon.AppDetailInfo(new UCarCommon.AppGeneralInfo(getAppDetailInfoResponse.getAppDetailInfo(i2).getId(), getAppDetailInfoResponse.getAppDetailInfo(i2).getLabel(), UCarCommon.AppCategory.values()[getAppDetailInfoResponse.getAppDetailInfo(i2).getCategoryValue()]), getAppDetailInfoResponse.getAppDetailInfo(i2).getPackageName(), getAppDetailInfoResponse.getAppDetailInfo(i2).getIcon().toByteArray()));
                }
            }
        }
        return arrayList;
    }

    public void a(t tVar) {
        this.o = tVar;
    }

    public void a(String str) {
        a();
        a.b.a.g.a aVar = this.f113a;
        if (aVar == null) {
            a.a.a.b.b(p, "control socket channel init failed");
        } else if (aVar.b()) {
            a.a.a.b.b(p, "control socket channel has opened");
        } else {
            a.a.a.b.c(p, "start control channel");
            try {
                HashMap hashMap = new HashMap();
                hashMap.put(a.c.b.z.g.e.t, 1);
                hashMap.put(a.c.b.z.g.e.u, 1);
                hashMap.put(a.c.b.z.g.e.v, 1);
                hashMap.put(a.c.b.z.g.e.w, 0);
                hashMap.put(a.c.b.z.g.e.x, Integer.valueOf((int) a.c.b.z.g.e.y));
                this.f113a.a(0, str, hashMap);
            } catch (IOException e2) {
                a.a.a.b.b(p, "Start control channel error.", e2);
            }
        }
    }

    public boolean a(int i2, int i3) {
        return a(i2, UCarProto.AppInvokeCategory.UPDATE, i3);
    }

    public boolean a(int i2, UCarProto.AppInvokeCategory appInvokeCategory) {
        return a(i2, appInvokeCategory, -1);
    }

    public boolean a(int i2, UCarProto.AppInvokeCategory appInvokeCategory, int i3) {
        String str;
        UCarProto.InvokeApp invokeApp = (UCarProto.InvokeApp) UCarProto.InvokeApp.newBuilder().setId(i2).setInvokeState(appInvokeCategory).setDisplayMode(i3).build();
        if (d()) {
            try {
                return this.f113a.a(a.c.b.r.a(invokeApp), new i(), true, 500).get(500, TimeUnit.MILLISECONDS).booleanValue();
            } catch (TimeoutException unused) {
                str = "send InvokeApp get ack time out";
            } catch (InterruptedException | ExecutionException e2) {
                a.a.a.b.b(p, "send InvokeApp exception:", e2);
            }
        } else {
            str = "control channel not ready";
            a.a.a.b.b(p, str);
            return false;
        }
    }

    public boolean a(UCarProto.NotifyAddCamera notifyAddCamera) {
        a.a.a.b.c(p, "add camera");
        if (!d()) {
            return false;
        }
        a.a.a.b.c(p, "add camera start");
        try {
            this.f113a.a(a.c.b.r.a(notifyAddCamera), (a.c.b.z.e) new b()).get(50, TimeUnit.MILLISECONDS);
            return true;
        } catch (InterruptedException | ExecutionException e2) {
            a.a.a.b.b(p, "notifyAddCamera exception:", e2);
            return true;
        } catch (TimeoutException unused) {
            a.a.a.b.b(p, "notifyAddCamera time out");
            return true;
        }
    }

    public boolean a(UCarProto.NotifyRemoveCamera notifyRemoveCamera) {
        a.a.a.b.c(p, "remove camera");
        if (d()) {
            this.f113a.a(a.c.b.r.a(notifyRemoveCamera), (a.c.b.z.e) new c());
            return true;
        }
        a.a.a.b.b(p, " notifyRemoveCamera() Channel not ready");
        return false;
    }

    public boolean a(UCarCommon.DayNightMode dayNightMode) {
        String str;
        if (dayNightMode == null || dayNightMode == UCarCommon.DayNightMode.UNKNOWN_MODE) {
            str = " sendSwitchDayOrNight() args error";
        } else if (d()) {
            this.f113a.a(a.c.b.r.a((UCarProto.NotifySwitchDayOrNight) UCarProto.NotifySwitchDayOrNight.newBuilder().setModeValue(dayNightMode.getValue()).build()), (a.c.b.z.e) new q());
            return true;
        } else {
            str = " sendSwitchDayOrNight Channel not ready";
        }
        a.a.a.b.b(p, str);
        return false;
    }

    public boolean a(UCarCommon.KeyEventActionType keyEventActionType, UCarCommon.KeyCodeType keyCodeType, int i2) {
        String str;
        if (keyEventActionType == null || keyCodeType == null || keyEventActionType == UCarCommon.KeyEventActionType.KEY_EVENT_ACTION_UNDEFINED || keyCodeType == UCarCommon.KeyCodeType.KEY_CODE_UNDEFINED) {
            str = " sendKeyEventToPhone() args error";
        } else if (d()) {
            this.f113a.a(a.c.b.r.a((UCarProto.CustomKeyEvent) UCarProto.CustomKeyEvent.newBuilder().setTimestamp(c()).setAction(keyEventActionType.getValue()).setKeycodeValue(keyCodeType.getValue()).setMetaState(i2).build()), (a.c.b.z.e) new n());
            return true;
        } else {
            str = " sendKeyEventToPhone() Channel not ready";
        }
        a.a.a.b.b(p, str);
        return false;
    }

    public boolean a(UCarCommon.VRCmdType vRCmdType, String str) {
        String str2;
        if (vRCmdType == null || str == null) {
            str2 = "sendVRCmdToPhone() args error";
        } else if (d()) {
            this.f113a.a(a.c.b.r.a((UCarProto.VRCmdToPhone) UCarProto.VRCmdToPhone.newBuilder().setCmdValue(vRCmdType.getValue()).setSource(str).build()), (a.c.b.z.e) new o());
            return true;
        } else {
            str2 = " sendVRCmdToPhone() Channel not ready";
        }
        a.a.a.b.b(p, str2);
        return false;
    }

    public boolean a(UCarConfig uCarConfig, MDevice mDevice, a.c.b.s sVar) {
        if (d()) {
            this.f113a.a(a.c.b.r.a((UCarProto.GetUCarConfigResponse) UCarProto.GetUCarConfigResponse.newBuilder().setCarBrMac(ByteString.copyFrom(uCarConfig.getCarBrMac())).setScreenWidth(uCarConfig.getScreenWidth()).setScreenHeight(uCarConfig.getScreenHeight()).setDpi(uCarConfig.getDpi()).setVideoDisplayWidth(uCarConfig.getVideoDisplayWidth()).setVideoDisplayHeight(uCarConfig.getVideoDisplayHeight()).setFps(uCarConfig.getFps()).setIsSupportP2P(uCarConfig.isSupportP2P()).setIsSupportSoftAP(uCarConfig.isSupportSoftAP()).setIsDataTransMode(mDevice.getSelectedWorkMode() == 2).setDefault5GChannel(uCarConfig.getAllowed5gChannels()[0]).setIsSupportCamera(uCarConfig.isSupportCamera()).setIsSupportMic(uCarConfig.isSupportMic()).setIsSupportLowLatencyDecodingMode(uCarConfig.isSupportLowLatencyDecodingMode()).setIsSupportVoiceWaken(uCarConfig.isSupportVoiceWaken()).setCarCustomField(ByteString.copyFrom(uCarConfig.getCarCustomField())).setSdkVersion(BuildConfig.SDK_VERSION).setWorkModes(uCarConfig.getSupportWorkModesInt()).setIsSupportPOINav(uCarConfig.isIsSupportPOINav()).setIsSupportRotation(uCarConfig.isSupportRotation()).setIsSupportHomeKey(uCarConfig.isSupportHomeKey()).setIsSupportBackKey(uCarConfig.isSupportBackKey()).setIsSupportHevc(uCarConfig.isSupportHevc()).setRealDpi(uCarConfig.getRealDpi()).build(), sVar.m()), (a.c.b.z.e) new e());
            return true;
        }
        a.a.a.b.b(p, " sendGetUCarConfigResponse() Channel not ready");
        return false;
    }

    public boolean a(String str, UCarCommon.CameraState cameraState) {
        a.a.a.b.c(p, "onCameraStateChanged, state = " + cameraState);
        if (d()) {
            this.f113a.a(a.c.b.r.a((UCarProto.NotifyCameraStateChanged) UCarProto.NotifyCameraStateChanged.newBuilder().setCameraId(str).setStateValue(cameraState.getValue()).build()), (a.c.b.z.e) new d());
            return true;
        }
        a.a.a.b.b(p, " notifyCameraStateChanged() Channel not ready");
        return false;
    }

    public boolean a(boolean z) {
        String str;
        if (z) {
            this.n = true;
        }
        if (d()) {
            try {
                return this.f113a.a(a.c.b.r.d(), (a.c.b.z.e) new l()).get(500, TimeUnit.MILLISECONDS).booleanValue();
            } catch (TimeoutException unused) {
                str = "sendGotoForeground get ack time out";
            } catch (InterruptedException | ExecutionException e2) {
                a.a.a.b.b(p, "sendGotoForeground exception:", e2);
                return false;
            }
        } else {
            str = " sendGotoForeground() Channel not ready";
            a.a.a.b.b(p, str);
            return false;
        }
    }

    public boolean a(byte[] bArr, UCarCommon.AudioFormat audioFormat, String str) {
        if (d()) {
            UCarProto.SampleRate c2 = c(audioFormat.getSampleRate());
            UCarProto.ChannelMask a2 = a(audioFormat.getChannelConfig());
            this.f113a.a(a.c.b.r.a((UCarProto.AwakenVoiceAssistant) UCarProto.AwakenVoiceAssistant.newBuilder().setPcmData(ByteString.copyFrom(bArr)).setSampleRate(c2).setChannelMask(a2).setEncodingFormat(b(audioFormat.getEncodingFormat())).setTimestamp(c()).setSource(str).build()), (a.c.b.z.e) new r());
            return true;
        }
        a.a.a.b.b(p, " sendAwakenVoiceAssistant() Channel not ready");
        return false;
    }

    /* JADX WARNING: Removed duplicated region for block: B:43:0x00a7  */
    public ArrayList<UCarCommon.AppGeneralInfo> b() {
        UCarProto.GetAppListResponse getAppListResponse;
        int i2;
        CountDownLatch countDownLatch;
        UCarProto.GetAppListRequest getAppListRequest = (UCarProto.GetAppListRequest) UCarProto.GetAppListRequest.newBuilder().build();
        ArrayList<UCarCommon.AppGeneralInfo> arrayList = null;
        if (!d()) {
            synchronized (this.g) {
                if (this.l == null) {
                    countDownLatch = new CountDownLatch(1);
                    this.l = countDownLatch;
                } else {
                    countDownLatch = null;
                }
            }
            if (countDownLatch != null) {
                a.a.a.b.b(p, "control channel not ready, waiting for connect");
                try {
                    if (!countDownLatch.await(1000, TimeUnit.MILLISECONDS) && !d()) {
                        a.a.a.b.b(p, "control channel not ready");
                        return null;
                    }
                } catch (InterruptedException e2) {
                    a.a.a.b.b(p, "catch InterruptedException", e2);
                    return null;
                }
            } else {
                a.a.a.b.b(p, "control channel not ready");
                return null;
            }
        }
        try {
            getAppListResponse = (UCarProto.GetAppListResponse) this.f113a.a(a.c.b.r.a(getAppListRequest), new g()).get(3000, TimeUnit.MILLISECONDS);
        } catch (TimeoutException unused) {
            a.a.a.b.b(p, "get AppListRequest ack time out");
        } catch (InterruptedException | ExecutionException e3) {
            a.a.a.b.b(p, "send GetAppListRequest exception:", e3);
        }
        if (getAppListResponse != null && getAppListResponse.getTotal() > 0 && getAppListResponse.getTotal() == getAppListResponse.getAppInfoCount()) {
            arrayList = new ArrayList<>(getAppListResponse.getTotal());
            for (i2 = 0; i2 < getAppListResponse.getTotal(); i2++) {
                int categoryValue = getAppListResponse.getAppInfo(i2).getCategoryValue();
                UCarCommon.AppCategory.values();
                if (categoryValue < 18) {
                    arrayList.add(new UCarCommon.AppGeneralInfo(getAppListResponse.getAppInfo(i2).getId(), getAppListResponse.getAppInfo(i2).getLabel(), UCarCommon.AppCategory.values()[getAppListResponse.getAppInfo(i2).getCategoryValue()]));
                }
            }
        }
        return arrayList;
        getAppListResponse = null;
        arrayList = new ArrayList<>(getAppListResponse.getTotal());
        while (i2 < getAppListResponse.getTotal()) {
        }
        return arrayList;
    }

    public void b(u uVar) {
        a.a.a.b.a(p, "startSendHeartBeatThread");
        this.f = true;
        Thread thread = new Thread(new RunnableC0013a(uVar));
        this.e = thread;
        thread.start();
    }

    public void e(int i2) {
        this.m = i2;
    }

    public boolean e() {
        if (d()) {
            this.f113a.a(a.c.b.r.a((UCarProto.NotifyCallHungUp) UCarProto.NotifyCallHungUp.newBuilder().setTimestamp(c()).build()), (a.c.b.z.e) new p());
            return true;
        }
        a.a.a.b.b(p, " sendCallHungUp() Channel not ready");
        return false;
    }

    public boolean f() {
        String str;
        UCarProto.PickUpCall pickUpCall = (UCarProto.PickUpCall) UCarProto.PickUpCall.newBuilder().setTimestamp(c()).build();
        if (d()) {
            try {
                return this.f113a.a(a.c.b.r.a(pickUpCall), new j(), true, 500).get(500, TimeUnit.MILLISECONDS).booleanValue();
            } catch (TimeoutException unused) {
                str = "send CallPickUp get ack time out";
            } catch (InterruptedException | ExecutionException e2) {
                a.a.a.b.b(p, "send CallPickUp exception:", e2);
            }
        } else {
            str = "control channel not ready";
            a.a.a.b.b(p, str);
            return false;
        }
    }

    public boolean g() {
        String str;
        UCarProto.Disconnect disconnect = (UCarProto.Disconnect) UCarProto.Disconnect.newBuilder().build();
        if (d()) {
            try {
                return this.f113a.a(a.c.b.r.a(disconnect), new f(), true, 500).get(500, TimeUnit.MILLISECONDS).booleanValue();
            } catch (TimeoutException unused) {
                str = "sendDisconnect get ack time out";
            } catch (InterruptedException | ExecutionException e2) {
                a.a.a.b.b(p, "sendDisconnect exception:", e2);
            }
        } else {
            str = "sendDisconnect Channel not ready";
            a.a.a.b.b(p, str);
            return false;
        }
    }

    public boolean h() {
        String str;
        this.n = false;
        if (d()) {
            try {
                return this.f113a.a(a.c.b.r.c(), (a.c.b.z.e) new m()).get(500, TimeUnit.MILLISECONDS).booleanValue();
            } catch (TimeoutException unused) {
                str = "sendGotoBackground get ack time out";
            } catch (InterruptedException | ExecutionException e2) {
                a.a.a.b.b(p, "sendGotoBackground exception:", e2);
                return false;
            }
        } else {
            str = " sendGotoBackground Channel not ready";
            a.a.a.b.b(p, str);
            return false;
        }
    }

    public void i() {
        synchronized (this.g) {
            j();
            k();
            if (this.f113a != null) {
                a.a.a.b.c(p, "close control channel");
                this.f113a.i();
            }
            CountDownLatch countDownLatch = this.l;
            if (countDownLatch != null) {
                countDownLatch.countDown();
            }
            this.l = null;
        }
    }

    public void j() {
        a.a.a.b.c(p, "stopSendHeartBeatThread");
        this.f = false;
        Thread thread = this.e;
        if (thread != null) {
            try {
                thread.interrupt();
                this.e.join();
                this.e = null;
            } catch (InterruptedException e2) {
                a.a.a.b.b(p, "stopSendHeartBeatThread Exception", e2);
            }
        }
    }
}
