package com.ss.android.ugc.aweme.feed.background;

import X.2FD;
import X.2FV;
import X.2He;
import X.4Zx;
import X.7he;
import X.7s1;
import X.8jA;
import X.9Ik;
import X.9J5;
import X.C2822SAo;
import X.C3172SOa;
import X.C3179SOh;
import X.Crj;
import X.Mjy;
import X.S90;
import X.SOW;
import X.SOX;
import X.SOZ;
import X.SPX;
import android.content.ClipboardManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.media.AudioFocusRequest;
import android.media.AudioManager;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import com.bytedance.helios.sdk.utils.ServiceLifecycle;
import com.bytedance.ies.xelement.defaultimpl.player.impl.plugin.mediasession.session.MediaSessionService;
import java.lang.reflect.Field;
import kotlin.h.b.n;

public final class BackgroundAudioBrowserService extends MediaSessionService {
    public static final C3172SOa LJIIL = new C3172SOa((byte) 0);
    public final 9J5 LJI;
    public final SOW LJII;
    public final 9Ik LJIIIIZZ;
    public SPX LJIIIZ;
    public C3179SOh LJIIJ;
    public final Mjy LJIIJJI;
    public final int LJIILIIL = 2131232568;
    public AudioManager LJIILJJIL;
    public AudioFocusRequest LJIILL;
    public AudioManager.OnAudioFocusChangeListener LJIILLIIL;
    public final SOX LJIIZILJ;
    public 4Zx LJIJ;
    public final Crj LJIJI;
    public final Crj LJIJJ;

    static {
        Covode.recordClassIndex(78141);
    }

    private final ComponentName LIZIZ() {
        return (ComponentName) this.LJIJI.getValue();
    }

    /* JADX WARNING: type inference failed for: r1v0, types: [com.ss.android.ugc.aweme.feed.background.BackgroundAudioBrowserService, android.app.Service, com.bytedance.ies.xelement.defaultimpl.player.impl.plugin.mediasession.session.MediaSessionService] */
    public final int onStartCommand(Intent intent, int i, int i2) {
        ServiceLifecycle.onStartCommand(this, intent, i, i2);
        return super.onStartCommand(intent, i, i2);
    }

    public BackgroundAudioBrowserService() {
        9J5 r1 = new 9J5();
        this.LJI = r1;
        this.LJII = new SOW();
        this.LJIIIIZZ = new 9Ik();
        this.LJIIZILJ = new SOX(r1);
        this.LJIIJJI = new Mjy();
        this.LJIJI = 7he.LIZ(new C2822SAo(this));
        this.LJIJJ = 7he.LIZ(new SOZ(this));
    }

    public final void onDestroy() {
        super.onDestroy();
        C3179SOh sOh = this.LJIIJ;
        if (sOh == null) {
            n.LIZ("");
        }
        sOh.LIZ(false);
        SPX spx = this.LJIIIZ;
        if (spx == null) {
            n.LIZ("");
        }
        spx.LIZ(false);
        stopForeground(true);
        this.LJIIJJI.LIZ();
        unregisterReceiver(this.LJIIZILJ);
        4Zx r0 = this.LJIJ;
        if (r0 == null) {
            n.LIZ("");
        }
        unregisterReceiver(r0);
        SPX spx2 = this.LJIIIZ;
        if (spx2 == null) {
            n.LIZ("");
        }
        spx2.LIZ();
        C3179SOh sOh2 = this.LJIIJ;
        if (sOh2 == null) {
            n.LIZ("");
        }
        sOh2.LIZ();
        2FD.LIZ = false;
        if (Build.VERSION.SDK_INT >= 26) {
            AudioManager audioManager = this.LJIILJJIL;
            if (audioManager == null) {
                n.LIZ("");
            }
            AudioFocusRequest audioFocusRequest = this.LJIILL;
            if (audioFocusRequest == null) {
                n.LIZ("");
            }
            audioManager.abandonAudioFocusRequest(audioFocusRequest);
            return;
        }
        AudioManager audioManager2 = this.LJIILJJIL;
        if (audioManager2 == null) {
            n.LIZ("");
        }
        AudioManager.OnAudioFocusChangeListener onAudioFocusChangeListener = this.LJIILLIIL;
        if (onAudioFocusChangeListener == null) {
            n.LIZ("");
        }
        audioManager2.abandonAudioFocus(onAudioFocusChangeListener);
    }

    public final void LIZ() {
        C3179SOh sOh = this.LJIIJ;
        if (sOh == null) {
            n.LIZ("");
        }
        sOh.LIZ(true);
        SPX spx = this.LJIIIZ;
        if (spx == null) {
            n.LIZ("");
        }
        spx.LIZ(true);
        C3179SOh sOh2 = this.LJIIJ;
        if (sOh2 == null) {
            n.LIZ("");
        }
        sOh2.LIZLLL();
        C3179SOh sOh3 = this.LJIIJ;
        if (sOh3 == null) {
            n.LIZ("");
        }
        sOh3.LIZIZ();
        SPX spx2 = this.LJIIIZ;
        if (spx2 == null) {
            n.LIZ("");
        }
        C3179SOh sOh4 = this.LJIIJ;
        if (sOh4 == null) {
            n.LIZ("");
        }
        spx2.LIZ(sOh4.LIZIZ.LIZIZ());
    }

    /* JADX WARNING: type inference failed for: r5v1, types: [android.content.Context] */
    /* JADX WARNING: Multi-variable type inference failed */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public final void onCreate() {
        /*
            r11 = this;
            com.ss.android.ugc.aweme.feed.background.BackgroundAudioBrowserService.super.onCreate()
            X.SOh r3 = new X.SOh
            android.content.Context r4 = r11.getBaseContext()
            java.lang.String r2 = ""
            kotlin.h.b.n.LIZIZ(r4, r2)
            X.9J5 r5 = r11.LJI
            X.SOW r6 = r11.LJII
            X.9Ik r7 = r11.LJIIIIZZ
            android.content.ComponentName r8 = r11.LIZIZ()
            r3.<init>(r4, r5, r6, r7, r8)
            X.SPX r4 = new X.SPX
            r5 = r11
            android.support.v4.media.session.MediaSessionCompat$Token r6 = r3.LIZJ()
            X.SP2 r7 = r3.LIZJ
            android.content.ComponentName r8 = r11.LIZIZ()
            X.SL9 r9 = new X.SL9
            r9.<init>()
            int r10 = r11.LJIILIIL
            r4.<init>(r5, r6, r7, r8, r9, r10)
            r11.LJIIIZ = r4
            r11.LJIIJ = r3
            android.support.v4.media.session.MediaSessionCompat$Token r0 = r3.LIZJ()
            r11.LIZ(r0)
            androidx.localbroadcastmanager.content.LocalBroadcastManager r4 = androidx.localbroadcastmanager.content.LocalBroadcastManager.getInstance(r5)
            X.Crj r0 = r11.LJIJJ
            java.lang.Object r3 = r0.getValue()
            X.SOV r3 = (X.SOV) r3
            android.content.IntentFilter r1 = new android.content.IntentFilter
            java.lang.String r0 = "intent.filter.X_ELEMENT_X_AUDIO_MEDIA_SESSION_BROADCAST"
            r1.<init>(r0)
            r4.registerReceiver(r3, r1)
            X.SOX r3 = r11.LJIIZILJ
            android.content.IntentFilter r1 = new android.content.IntentFilter
            java.lang.String r0 = "android.media.AUDIO_BECOMING_NOISY"
            r1.<init>(r0)
            LIZ(r11, r3, r1)
            android.content.Context r1 = r11.getApplicationContext()
            boolean r0 = X.7lW.LIZJ
            if (r0 != 0) goto L_0x0116
        L_0x0067:
            java.lang.String r0 = "audio"
            java.lang.Object r1 = LIZ(r1, r0)
            java.lang.String r0 = "null cannot be cast to non-null type android.media.AudioManager"
            java.util.Objects.requireNonNull(r1, r0)
            android.media.AudioManager r1 = (android.media.AudioManager) r1
            r11.LJIILJJIL = r1
            X.4Zv.LJI(r5)
            X.4Zx r3 = new X.4Zx
            android.media.AudioManager r1 = r11.LJIILJJIL
            if (r1 != 0) goto L_0x0082
            kotlin.h.b.n.LIZ(r2)
        L_0x0082:
            X.4eH r0 = X.4eH.LIZ
            r3.<init>(r1, r0)
            r11.LJIJ = r3
            android.content.IntentFilter r1 = new android.content.IntentFilter
            java.lang.String r0 = "android.media.VOLUME_CHANGED_ACTION"
            r1.<init>(r0)
            LIZ(r11, r3, r1)
            boolean r0 = X.4Zv.LJ(r5)
            if (r0 == 0) goto L_0x009e
            X.4eG r0 = X.4eG.LJ
            r0.LIZ()
        L_0x009e:
            X.SOY r0 = new X.SOY
            r0.<init>(r11)
            r11.LJIILLIIL = r0
            int r1 = android.os.Build.VERSION.SDK_INT
            r0 = 26
            r4 = 2
            r3 = 1
            if (r1 < r0) goto L_0x0103
            android.media.AudioFocusRequest$Builder r1 = new android.media.AudioFocusRequest$Builder
            r1.<init>(r3)
            android.media.AudioManager$OnAudioFocusChangeListener r0 = r11.LJIILLIIL
            if (r0 != 0) goto L_0x00b9
            kotlin.h.b.n.LIZ(r2)
        L_0x00b9:
            r1.setOnAudioFocusChangeListener(r0)
            r1.setWillPauseWhenDucked(r3)
            android.media.AudioAttributes$Builder r0 = new android.media.AudioAttributes$Builder
            r0.<init>()
            r0.setContentType(r4)
            android.media.AudioAttributes r0 = r0.build()
            r1.setAudioAttributes(r0)
            android.media.AudioFocusRequest r0 = r1.build()
            kotlin.h.b.n.LIZIZ(r0, r2)
            r11.LJIILL = r0
            android.media.AudioManager r1 = r11.LJIILJJIL
            if (r1 != 0) goto L_0x00de
            kotlin.h.b.n.LIZ(r2)
        L_0x00de:
            android.media.AudioFocusRequest r0 = r11.LJIILL
            if (r0 != 0) goto L_0x00e5
            kotlin.h.b.n.LIZ(r2)
        L_0x00e5:
            int r0 = r1.requestAudioFocus(r0)
        L_0x00e9:
            if (r0 == r3) goto L_0x00ee
            r11.stopSelf()
        L_0x00ee:
            X.SfZ r0 = X.C3829SfZ.LJIJ
            X.PgU r1 = r0.LJI()
            X.4es r0 = new X.4es
            r0.<init>(r11)
            X.2cN r1 = r1.LIZLLL(r0)
            X.Mjy r0 = r11.LJIIJJI
            r0.LIZ(r1)
            return
        L_0x0103:
            android.media.AudioManager r1 = r11.LJIILJJIL
            if (r1 != 0) goto L_0x010a
            kotlin.h.b.n.LIZ(r2)
        L_0x010a:
            android.media.AudioManager$OnAudioFocusChangeListener r0 = r11.LJIILLIIL
            if (r0 != 0) goto L_0x0111
            kotlin.h.b.n.LIZ(r2)
        L_0x0111:
            int r0 = r1.requestAudioFocus(r0, r4, r3)
            goto L_0x00e9
        L_0x0116:
            if (r1 == 0) goto L_0x011a
            goto L_0x0067
        L_0x011a:
            android.app.Application r1 = X.7lW.LIZ
            goto L_0x0067
        */
        throw new UnsupportedOperationException("Method not decompiled: com.ss.android.ugc.aweme.feed.background.BackgroundAudioBrowserService.onCreate():void");
    }

    public final IBinder onBind(Intent intent) {
        return new S90(this, this);
    }

    public static final /* synthetic */ C3179SOh LIZ(BackgroundAudioBrowserService backgroundAudioBrowserService) {
        C3179SOh sOh = backgroundAudioBrowserService.LJIIJ;
        if (sOh == null) {
            n.LIZ("");
        }
        return sOh;
    }

    public static final /* synthetic */ SPX LIZIZ(BackgroundAudioBrowserService backgroundAudioBrowserService) {
        SPX spx = backgroundAudioBrowserService.LJIIIZ;
        if (spx == null) {
            n.LIZ("");
        }
        return spx;
    }

    public static Object LIZ(Context context, String str) {
        Object obj;
        MethodCollector.i(7542);
        if (Build.VERSION.SDK_INT > 27 || !"clipboard".equals(str)) {
            if (!2FV.LIZIZ && "connectivity".equals(str)) {
                try {
                    new 7s1().LIZ();
                    2FV.LIZIZ = true;
                    obj = context.getSystemService(str);
                } catch (Throwable unused) {
                }
            }
            obj = context.getSystemService(str);
        } else if (2FV.LIZ) {
            synchronized (ClipboardManager.class) {
                try {
                    obj = context.getSystemService(str);
                    if (Thread.currentThread() != Looper.getMainLooper().getThread()) {
                        Field declaredField = ClipboardManager.class.getDeclaredField("mHandler");
                        declaredField.setAccessible(true);
                        declaredField.set(obj, new 2He((Handler) declaredField.get(obj)));
                    }
                } catch (Exception e) {
                    8jA.LIZ(e, "ClipboardManager Handler Reflect Fail");
                } catch (Throwable th) {
                    MethodCollector.o(7542);
                    throw th;
                }
                2FV.LIZ = false;
            }
        } else {
            obj = context.getSystemService(str);
        }
        MethodCollector.o(7542);
        return obj;
    }

    /* JADX WARNING: type inference failed for: r2v0, types: [com.ss.android.ugc.aweme.feed.background.BackgroundAudioBrowserService, android.content.Context] */
    /* JADX WARNING: Unknown variable types count: 1 */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public static android.content.Intent LIZ(com.ss.android.ugc.aweme.feed.background.BackgroundAudioBrowserService r2, android.content.BroadcastReceiver r3, android.content.IntentFilter r4) {
        /*
            boolean r0 = com.bytedance.sysoptimizer.ReceiverRegisterCrashOptimizer.fixedOpen()
            if (r0 == 0) goto L_0x0009
            com.bytedance.sysoptimizer.ReceiverRegisterCrashOptimizer.doHWReceiverFix()
        L_0x0009:
            boolean r0 = com.bytedance.sysoptimizer.ReceiverRegisterCrashOptimizer.doRegisterHandler()     // Catch:{ Exception -> 0x001c }
            if (r0 == 0) goto L_0x0017
            r1 = 0
            android.os.Handler r0 = com.bytedance.sysoptimizer.ReceiverRegisterLancet.sReceiverHandler     // Catch:{ Exception -> 0x001c }
            android.content.Intent r0 = r2.registerReceiver(r3, r4, r1, r0)     // Catch:{ Exception -> 0x001c }
            return r0
        L_0x0017:
            android.content.Intent r0 = r2.registerReceiver(r3, r4)     // Catch:{ Exception -> 0x001c }
            return r0
        L_0x001c:
            r1 = move-exception
            boolean r0 = com.bytedance.sysoptimizer.ReceiverRegisterCrashOptimizer.fixedOpen()
            if (r0 == 0) goto L_0x0028
            android.content.Intent r0 = com.bytedance.sysoptimizer.ReceiverRegisterCrashOptimizer.registerReceiver(r3, r4)
            return r0
        L_0x0028:
            throw r1
        */
        throw new UnsupportedOperationException("Method not decompiled: com.ss.android.ugc.aweme.feed.background.BackgroundAudioBrowserService.LIZ(com.ss.android.ugc.aweme.feed.background.BackgroundAudioBrowserService, android.content.BroadcastReceiver, android.content.IntentFilter):android.content.Intent");
    }
}
