package a.c.d.i;

import a.c.d.c.a;
import a.c.d.i.a;
import a.c.d.i.b;
import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioFocusRequest;
import android.media.AudioManager;
import android.os.Build;
import android.os.Handler;
import android.os.HandlerThread;
import com.ucar.vehiclesdk.UCarAdapter;
import com.ucar.vehiclesdk.UCarCommon;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

public class a {
    private static final String m = "AudioPlayerMgr";

    /* renamed from: a  reason: collision with root package name */
    private final Map<UCarCommon.AudioType, b> f144a = new HashMap();
    private final Map<UCarCommon.AudioType, a.b> b = new HashMap();
    private final Map<UCarCommon.AudioType, UCarCommon.AudioAttributes> c = new HashMap();
    private final Map<String, c> d = new HashMap();
    private volatile boolean e = true;
    private volatile boolean f = false;
    private final AudioManager g;
    private final a.c h;
    private c i;
    private final Handler j;
    private int k = 0;
    private final Context l;

    /* renamed from: a.c.d.i.a$a  reason: collision with other inner class name */
    public static /* synthetic */ class C0016a {

        /* renamed from: a  reason: collision with root package name */
        public static final /* synthetic */ int[] f145a;

        /* JADX WARNING: Can't wrap try/catch for region: R(8:0|(2:1|2)|3|5|6|7|8|(3:9|10|12)) */
        /* JADX WARNING: Failed to process nested try/catch */
        /* JADX WARNING: Missing exception handler attribute for start block: B:7:0x0016 */
        /* JADX WARNING: Missing exception handler attribute for start block: B:9:0x001c */
        static {
            a.d.values();
            int[] iArr = new int[4];
            f145a = iArr;
            try {
                a.d dVar = a.d.START_PLAYER;
                iArr[0] = 1;
            } catch (NoSuchFieldError unused) {
            }
            int[] iArr2 = f145a;
            a.d dVar2 = a.d.RESUME_PLAYER;
            iArr2[3] = 2;
            int[] iArr3 = f145a;
            a.d dVar3 = a.d.PAUSE_PLAYER;
            iArr3[2] = 3;
            try {
                int[] iArr4 = f145a;
                a.d dVar4 = a.d.STOP_PLAYER;
                iArr4[1] = 4;
            } catch (NoSuchFieldError unused2) {
            }
        }
    }

    public class b implements AudioManager.OnAudioFocusChangeListener {

        /* renamed from: a  reason: collision with root package name */
        private final List<UCarCommon.AudioType> f146a;
        private final UCarCommon.AudioAttributes b;

        private b(UCarCommon.AudioAttributes audioAttributes, UCarCommon.AudioType audioType) {
            ArrayList arrayList = new ArrayList();
            this.f146a = arrayList;
            arrayList.add(audioType);
            this.b = audioAttributes;
        }

        public /* synthetic */ b(a aVar, UCarCommon.AudioAttributes audioAttributes, UCarCommon.AudioType audioType, C0016a aVar2) {
            this(audioAttributes, audioType);
        }

        /* access modifiers changed from: private */
        public /* synthetic */ void a(int i) {
            String str;
            a.a.a.b.a(a.m, "onAudioFocusChange, audioTypes: " + this.f146a + " new focus: " + i);
            if (i != -3) {
                if (i == -2) {
                    a.a.a.b.a(a.m, "onAudioFocus loss transient, need pause");
                    a.this.a((a) this.b, (UCarCommon.AudioAttributes) this.f146a, (List) true);
                } else if (i == -1) {
                    a.a.a.b.a(a.m, "onAudioFocus loss, need stop");
                    a.this.a((a) this.b, (UCarCommon.AudioAttributes) this.f146a, (List) false);
                } else if (i != 1) {
                    str = "onAudioFocusChange:" + i;
                } else {
                    a.a.a.b.a(a.m, "onAudioFocus gain, now play");
                    a.this.a((a) this.b, (UCarCommon.AudioAttributes) this.f146a);
                }
                a.this.a((a) this.f146a, (List) i);
            }
            str = "onAudioFocus loss transient can duck, need pause or lower the volume";
            a.a.a.b.a(a.m, str);
            a.this.a((a) this.f146a, (List) i);
        }

        /* access modifiers changed from: private */
        /* access modifiers changed from: public */
        private void a(UCarCommon.AudioType audioType) {
            if (!this.f146a.contains(audioType)) {
                this.f146a.add(audioType);
                a.this.a((a) this.f146a, (List) 1);
            }
        }

        public void onAudioFocusChange(int i) {
            a.this.j.post(new Runnable(i) {
                /* class a.c.d.i.$$Lambda$a$b$YWri_t6XZxUcfiD1qAGH8tMQwF4 */
                public final /* synthetic */ int f$1;

                {
                    this.f$1 = r2;
                }

                public final void run() {
                    a.b.this.a((a.b) this.f$1);
                }
            });
        }
    }

    public static class c {

        /* renamed from: a  reason: collision with root package name */
        public b f147a;
        public AudioFocusRequest b;
        public boolean c;

        private c() {
        }

        public /* synthetic */ c(C0016a aVar) {
            this();
        }
    }

    public a(Context context, a.c cVar) {
        this.l = context;
        this.g = (AudioManager) context.getSystemService("audio");
        d.a(context);
        this.h = cVar;
        HandlerThread handlerThread = new HandlerThread("AudioPlayerManagerThread");
        handlerThread.start();
        this.j = new Handler(handlerThread.getLooper());
    }

    private b a(UCarCommon.AudioType audioType) {
        return this.f144a.get(audioType);
    }

    private static String a(UCarCommon.AudioAttributes audioAttributes) {
        return audioAttributes.getFocusGain() + "_" + audioAttributes.getAudioAttributes().getUsage() + "_" + audioAttributes.getAudioAttributes().getContentType();
    }

    /* access modifiers changed from: private */
    public /* synthetic */ void a(int i2) {
        this.k = i2;
    }

    /* access modifiers changed from: private */
    public /* synthetic */ void a(int i2, UCarCommon.AudioType audioType) {
        this.h.onAudioFocusChanged(audioType, i2);
    }

    /* access modifiers changed from: private */
    public /* synthetic */ void a(a.b bVar) {
        if (bVar.a() == UCarCommon.AudioType.STREAM_UNDEFINED) {
            for (Map.Entry<UCarCommon.AudioType, b> entry : this.f144a.entrySet()) {
                a(entry.getKey(), bVar.b(), bVar.c());
            }
        } else {
            a(bVar.a(), bVar.b(), bVar.c());
        }
        this.b.put(bVar.a(), bVar);
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void a(UCarCommon.AudioAttributes audioAttributes, List<UCarCommon.AudioType> list) {
        int i2 = (b(audioAttributes) || c()) ? 3 : 0;
        if (this.g.getMode() != i2 && this.f) {
            this.g.setMode(i2);
        }
        c cVar = this.d.get(a(audioAttributes));
        if (cVar != null) {
            cVar.c = true;
            if (b(audioAttributes)) {
                this.i = cVar;
            }
        }
        this.g.setSpeakerphoneOn(c());
        for (UCarCommon.AudioType audioType : list) {
            b a2 = a(audioType);
            if (a2 != null) {
                a2.e();
            }
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void a(UCarCommon.AudioAttributes audioAttributes, List<UCarCommon.AudioType> list, boolean z) {
        if (audioAttributes.getFocusGain() == 1 && !z) {
            UCarAdapter instance = UCarAdapter.getInstance();
            UCarCommon.KeyEventActionType keyEventActionType = UCarCommon.KeyEventActionType.KEY_EVENT_ACTION_DOWN;
            UCarCommon.KeyCodeType keyCodeType = UCarCommon.KeyCodeType.KEY_CODE_MEDIA_PAUSE;
            instance.sendKeyEvent(keyEventActionType, keyCodeType, 0);
            UCarAdapter.getInstance().sendKeyEvent(UCarCommon.KeyEventActionType.KEY_EVENT_ACTION_UP, keyCodeType, 0);
        }
        c cVar = this.d.get(a(audioAttributes));
        if (cVar != null && !z) {
            cVar.c = false;
        }
        c(audioAttributes);
        for (UCarCommon.AudioType audioType : list) {
            b bVar = this.f144a.get(audioType);
            if (bVar != null) {
                bVar.b();
            }
        }
    }

    /* access modifiers changed from: private */
    public /* synthetic */ void a(UCarCommon.AudioFormat audioFormat, UCarCommon.AudioType audioType, a.d dVar, UCarCommon.AudioAttributes audioAttributes) {
        if (audioFormat == null) {
            a.a.a.b.b(m, "format null");
            return;
        }
        a.a.a.b.a(m, "managePlayer, type: " + audioType + ", format: " + audioFormat + ", state: " + dVar);
        int ordinal = dVar.ordinal();
        if (ordinal == 0) {
            a(audioType, audioFormat, audioAttributes);
        } else if (ordinal == 1) {
            d(audioType);
        } else if (ordinal == 2) {
            b(audioType);
        } else if (ordinal != 3) {
            a.a.a.b.b(m, "player state is unknown!");
        } else {
            c(audioType);
        }
    }

    private void a(UCarCommon.AudioType audioType, int i2) {
        if (this.h != null) {
            a.a.a.b.a(m, "notifyAudioFocusChanged, audioType: " + audioType + ", focusChange: " + i2);
            this.h.onAudioFocusChanged(audioType, i2);
        }
    }

    private void a(UCarCommon.AudioType audioType, int i2, int i3) {
        b a2 = a(audioType);
        if (a2 == null) {
            a.a.a.b.e(m, "player is not created: " + audioType);
        } else {
            a2.a(i2, audioType == UCarCommon.AudioType.STREAM_CAST_MUSIC ? this.k : 0, i3);
        }
    }

    private void a(UCarCommon.AudioType audioType, c cVar, UCarCommon.AudioAttributes audioAttributes) {
        int i2;
        a.a.a.b.a(m, "requestAudioFocus, type: " + audioType);
        a(audioType, 0);
        if (Build.VERSION.SDK_INT >= 26) {
            if (cVar.b == null) {
                b bVar = new b(this, audioAttributes, audioType, null);
                cVar.b = new AudioFocusRequest.Builder(audioAttributes.getFocusGain()).setAudioAttributes(audioAttributes.getAudioAttributes()).setOnAudioFocusChangeListener(bVar).build();
                cVar.f147a = bVar;
            }
            i2 = this.g.requestAudioFocus(cVar.b);
        } else {
            if (cVar.f147a == null) {
                cVar.f147a = new b(this, audioAttributes, audioType, null);
            }
            i2 = this.g.requestAudioFocus(cVar.f147a, audioAttributes.getStreamType(), audioAttributes.getFocusGain());
        }
        if (i2 == 1) {
            a(audioAttributes, cVar.f147a.f146a);
            a(cVar.f147a.f146a, 1);
            return;
        }
        a(cVar.f147a.f146a, -4);
    }

    private void a(UCarCommon.AudioType audioType, UCarCommon.AudioFormat audioFormat, UCarCommon.AudioAttributes audioAttributes) {
        if (this.f144a.get(audioType) == null) {
            a.a.a.b.c(m, "createPlayer, type: " + audioType + ", format: " + audioFormat + ", audioAttributes: " + audioAttributes);
            if (audioAttributes == null) {
                audioAttributes = new UCarCommon.AudioAttributes(new AudioAttributes.Builder().setUsage(1).setContentType(2).build(), 1, 3);
            }
            int i2 = 0;
            if (b(audioAttributes)) {
                i2 = d.c();
            }
            try {
                b bVar = new b(this.l, audioAttributes.getAudioAttributes(), audioFormat, i2, audioType == UCarCommon.AudioType.STREAM_CAST_MUSIC ? new b.AbstractC0019b() {
                    /* class a.c.d.i.$$Lambda$a$SFmq8V62B2tMJJtrhAvBaI8VPo */

                    @Override // a.c.d.i.b.AbstractC0019b
                    public final void a(int i) {
                        a.this.a((a) i);
                    }
                } : null);
                this.f144a.put(audioType, bVar);
                a.b bVar2 = this.b.get(audioType);
                if (bVar2 != null) {
                    a(audioType, bVar2.b(), bVar2.c());
                }
                bVar.c();
                this.c.put(audioType, audioAttributes);
                this.d.putIfAbsent(a(audioAttributes), new c(null));
                if (audioAttributes.getFocusGain() != 1) {
                    return;
                }
            } catch (Exception e2) {
                a.a.a.b.b(m, "create audio player failed.", e2);
                return;
            }
        } else if (audioAttributes == null || audioAttributes.getFocusGain() != 1) {
            return;
        }
        this.e = true;
    }

    /* access modifiers changed from: private */
    public /* synthetic */ void a(UCarCommon.AudioType audioType, ByteBuffer byteBuffer) {
        StringBuilder sb;
        String str;
        b a2 = a(audioType);
        if (a2 != null) {
            UCarCommon.AudioAttributes audioAttributes = this.c.get(audioType);
            if (audioAttributes != null) {
                String a3 = a(audioAttributes);
                c cVar = this.d.get(a3);
                if (cVar == null) {
                    cVar = new c(null);
                    this.d.put(a3, cVar);
                }
                if (audioAttributes.getFocusGain() == 1) {
                    if (this.e && !cVar.c) {
                        a(audioType, cVar, audioAttributes);
                    }
                    this.e = false;
                } else if (!cVar.c) {
                    a(audioType, cVar, audioAttributes);
                } else {
                    b bVar = cVar.f147a;
                    if (bVar != null) {
                        bVar.a((b) audioType);
                    }
                }
                if (cVar.c) {
                    a2.a(byteBuffer);
                    return;
                }
            }
            sb = new StringBuilder();
            str = "no focus, drop data, type = ";
        } else {
            sb = new StringBuilder();
            str = "no player, drop data, type = ";
        }
        a.a.a.b.c(m, sb.append(str).append(audioType).toString());
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void a(List<UCarCommon.AudioType> list, int i2) {
        if (this.h != null) {
            a.a.a.b.a(m, "notifyAudioFocusChanged, audioTypes: " + list + ", focusChange: " + i2);
            list.forEach(new Consumer(i2) {
                /* class a.c.d.i.$$Lambda$a$mOoqvY7xQijIG9TyY0ykUO68Yk */
                public final /* synthetic */ int f$1;

                {
                    this.f$1 = r2;
                }

                @Override // java.util.function.Consumer
                public final void accept(Object obj) {
                    a.this.a((a) this.f$1, (int) ((UCarCommon.AudioType) obj));
                }
            });
        }
    }

    /* JADX WARNING: Removed duplicated region for block: B:13:0x0027  */
    /* JADX WARNING: Removed duplicated region for block: B:15:0x0059  */
    private boolean a(c cVar) {
        int i2;
        b bVar;
        AudioFocusRequest audioFocusRequest;
        if (Build.VERSION.SDK_INT >= 26) {
            if (!(cVar == null || (audioFocusRequest = cVar.b) == null)) {
                i2 = this.g.abandonAudioFocusRequest(audioFocusRequest);
                if (i2 == 1) {
                    a.a.a.b.a(m, "abandon audio focus succeed, audioAttributes: " + cVar.f147a.b);
                    cVar.c = false;
                    c(cVar.f147a.b);
                    a(cVar.f147a.f146a, -5);
                    return true;
                }
                a.a.a.b.b(m, (cVar == null || cVar.f147a == null) ? "abandon audio focus failed" : "abandon audio focus failed, audioAttributes: " + cVar.f147a.b);
                return false;
            }
        } else if (!(cVar == null || (bVar = cVar.f147a) == null)) {
            i2 = this.g.abandonAudioFocus(bVar);
            if (i2 == 1) {
            }
        }
        i2 = 0;
        if (i2 == 1) {
        }
    }

    private void b(UCarCommon.AudioType audioType) {
        b bVar = this.f144a.get(audioType);
        if (bVar != null) {
            bVar.b();
            a.a.a.b.a(m, "pausePlayer, audioType: " + audioType);
        }
    }

    private boolean b(UCarCommon.AudioAttributes audioAttributes) {
        return audioAttributes.getAudioAttributes().getUsage() == 2 && audioAttributes.getAudioAttributes().getContentType() == 1;
    }

    private void c(UCarCommon.AudioAttributes audioAttributes) {
        if (b(audioAttributes) && this.g.getMode() == 3 && this.f) {
            this.g.setMode(0);
        }
    }

    private void c(UCarCommon.AudioType audioType) {
        b bVar = this.f144a.get(audioType);
        if (bVar != null) {
            bVar.e();
            a.a.a.b.a(m, "resumePlayer, audioType: " + audioType);
        }
    }

    private boolean c() {
        c cVar = this.i;
        return (cVar != null && cVar.c) || this.g.getMode() == 3;
    }

    /* access modifiers changed from: private */
    public /* synthetic */ void d() {
        a.a.a.b.a(m, "abandonAudioFocus");
        this.e = false;
        for (c cVar : this.d.values()) {
            a(cVar);
        }
        this.i = null;
        this.d.clear();
        for (b bVar : this.f144a.values()) {
            if (bVar != null) {
                bVar.b();
            }
        }
    }

    private void d(UCarCommon.AudioType audioType) {
        String a2;
        c cVar;
        b bVar;
        a.a.a.b.a(m, "stopPlayer, audioType: " + audioType);
        b remove = this.f144a.remove(audioType);
        if (remove != null) {
            remove.f();
            remove.d();
            UCarCommon.AudioAttributes remove2 = this.c.remove(audioType);
            if (remove2 != null && (cVar = this.d.get((a2 = a(remove2)))) != null && (bVar = cVar.f147a) != null && bVar.f146a.contains(audioType)) {
                if (cVar.f147a.f146a.size() != 1) {
                    a(audioType, -5);
                    cVar.f147a.f146a.remove(audioType);
                } else if (a(cVar)) {
                    cVar.f147a.f146a.remove(audioType);
                    this.d.remove(a2);
                }
            }
        }
    }

    /* access modifiers changed from: private */
    public /* synthetic */ void e() {
        a.a.a.b.a(m, "stopAndClearAudioPlayers");
        for (b bVar : this.f144a.values()) {
            if (bVar != null) {
                bVar.f();
                bVar.d();
            }
        }
        this.f144a.clear();
        this.c.clear();
        a();
        this.k = 0;
    }

    public void a(UCarCommon.AudioType audioType, UCarCommon.AudioFormat audioFormat, a.d dVar, UCarCommon.AudioAttributes audioAttributes) {
        this.j.post(new Runnable(audioFormat, audioType, dVar, audioAttributes) {
            /* class a.c.d.i.$$Lambda$a$BnaU4wYU9PrsuRiK7Q3laEH2718 */
            public final /* synthetic */ UCarCommon.AudioFormat f$1;
            public final /* synthetic */ UCarCommon.AudioType f$2;
            public final /* synthetic */ a.d f$3;
            public final /* synthetic */ UCarCommon.AudioAttributes f$4;

            {
                this.f$1 = r2;
                this.f$2 = r3;
                this.f$3 = r4;
                this.f$4 = r5;
            }

            public final void run() {
                a.this.a((a) this.f$1, (UCarCommon.AudioFormat) this.f$2, (UCarCommon.AudioType) this.f$3, (a.d) this.f$4);
            }
        });
    }

    public void a(boolean z) {
        a.a.a.b.a(m, "allowSetCallMode: " + z);
        this.f = z;
    }

    public boolean a() {
        $$Lambda$a$ZxMqI_IUaXKeobAv_msiAOVVi8Y r0 = new Runnable() {
            /* class a.c.d.i.$$Lambda$a$ZxMqI_IUaXKeobAv_msiAOVVi8Y */

            public final void run() {
                a.this.d();
            }
        };
        if (Thread.currentThread() == this.j.getLooper().getThread()) {
            r0.run();
            return true;
        }
        this.j.post(r0);
        return true;
    }

    public void b() {
        a.a.a.b.a(m, "allowGainAudioFocus");
        this.e = true;
    }

    public void b(a.b bVar) {
        this.j.post(new Runnable(bVar) {
            /* class a.c.d.i.$$Lambda$a$5j45KUg35PEGJYqsHQTSNfzqPNc */
            public final /* synthetic */ a.b f$1;

            {
                this.f$1 = r2;
            }

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

    public void b(UCarCommon.AudioType audioType, ByteBuffer byteBuffer) {
        this.j.post(new Runnable(audioType, byteBuffer) {
            /* class a.c.d.i.$$Lambda$a$PbAUbFx9i0GrzvHfpMjENDEdmbs */
            public final /* synthetic */ UCarCommon.AudioType f$1;
            public final /* synthetic */ ByteBuffer f$2;

            {
                this.f$1 = r2;
                this.f$2 = r3;
            }

            public final void run() {
                a.this.a((a) this.f$1, (UCarCommon.AudioType) this.f$2);
            }
        });
    }

    public void f() {
        this.j.post(new Runnable() {
            /* class a.c.d.i.$$Lambda$a$pb4OkzmCZYPIt_2_FTRIC55deT4 */

            public final void run() {
                a.this.e();
            }
        });
    }
}
