package com.bytedance.bae.webrtc.observer;

import X.7s1;
import X.8jA;
import X.AnonymousClass2FV;
import X.C00802He;
import android.content.BroadcastReceiver;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import com.bytedance.bae.base.ContextUtils;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import com.bytedance.sysoptimizer.ReceiverRegisterCrashOptimizer;
import com.bytedance.sysoptimizer.ReceiverRegisterLancet;
import java.lang.reflect.Field;

public class VolumeChangedObserver {
    public VolumeBroadcastReceiver mVolumeBroadcastReceiver;
    public OnVolumeChangedListener onVolumeChangedListener;

    public interface OnVolumeChangedListener {
        static {
            Covode.recordClassIndex(24265);
        }

        void onVolumeChanged(int i, int i2);
    }

    static {
        Covode.recordClassIndex(24264);
    }

    public void release() {
        unregisterReceiver();
        this.onVolumeChangedListener = null;
    }

    private void registerVolumeChangeAction() {
        Context applicationContext = ContextUtils.getApplicationContext();
        if (applicationContext != null) {
            this.mVolumeBroadcastReceiver = new VolumeBroadcastReceiver();
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction("android.media.VOLUME_CHANGED_ACTION");
            INVOKEVIRTUAL_com_bytedance_bae_webrtc_observer_VolumeChangedObserver_com_bytedance_sysoptimizer_ReceiverRegisterLancet_registerReceiver(applicationContext, this.mVolumeBroadcastReceiver, intentFilter);
        }
    }

    private void unregisterReceiver() {
        try {
            Context applicationContext = ContextUtils.getApplicationContext();
            VolumeBroadcastReceiver volumeBroadcastReceiver = this.mVolumeBroadcastReceiver;
            if (volumeBroadcastReceiver != null) {
                applicationContext.unregisterReceiver(volumeBroadcastReceiver);
                this.mVolumeBroadcastReceiver = null;
            }
            if (this.onVolumeChangedListener != null) {
                this.onVolumeChangedListener = null;
            }
            this.mVolumeBroadcastReceiver = null;
        } catch (Exception unused) {
            if (this.onVolumeChangedListener != null) {
                this.onVolumeChangedListener = null;
            }
            this.mVolumeBroadcastReceiver = null;
        } catch (Throwable th) {
            if (this.onVolumeChangedListener != null) {
                this.onVolumeChangedListener = null;
            }
            this.mVolumeBroadcastReceiver = null;
            throw th;
        }
    }

    public class VolumeBroadcastReceiver extends BroadcastReceiver {
        static {
            Covode.recordClassIndex(24266);
        }

        public VolumeBroadcastReceiver() {
        }

        public void onReceive(Context context, Intent intent) {
            VolumeChangedObserver volumeChangedObserver = VolumeChangedObserver.this;
            if (volumeChangedObserver.onVolumeChangedListener != null && "android.media.VOLUME_CHANGED_ACTION".equals(intent.getAction())) {
                int intExtra = intent.getIntExtra("android.media.EXTRA_VOLUME_STREAM_TYPE", -1);
                if (intExtra == 3 || intExtra == 0 || intExtra == 6) {
                    volumeChangedObserver.onVolumeChangedListener.onVolumeChanged(intExtra, ((AudioManager) INVOKEVIRTUAL_com_bytedance_bae_webrtc_observer_VolumeChangedObserver$VolumeBroadcastReceiver_com_ss_android_ugc_aweme_lancet_GetSystemServiceLancet_getSystemService(ContextUtils.getApplicationContext(), "audio")).getStreamVolume(intExtra));
                }
            }
        }

        public static Object INVOKEVIRTUAL_com_bytedance_bae_webrtc_observer_VolumeChangedObserver$VolumeBroadcastReceiver_com_ss_android_ugc_aweme_lancet_GetSystemServiceLancet_getSystemService(Context context, String str) {
            Object obj;
            MethodCollector.i(14901);
            if (Build.VERSION.SDK_INT > 27 || !"clipboard".equals(str)) {
                if (!AnonymousClass2FV.LIZIZ && "connectivity".equals(str)) {
                    try {
                        new 7s1().LIZ();
                        AnonymousClass2FV.LIZIZ = true;
                        obj = context.getSystemService(str);
                    } catch (Throwable unused) {
                    }
                }
                obj = context.getSystemService(str);
            } else if (AnonymousClass2FV.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 C00802He((Handler) declaredField.get(obj)));
                        }
                    } catch (Exception e) {
                        8jA.LIZ(e, "ClipboardManager Handler Reflect Fail");
                    } catch (Throwable th) {
                        MethodCollector.o(14901);
                        throw th;
                    }
                    AnonymousClass2FV.LIZ = false;
                }
            } else {
                obj = context.getSystemService(str);
            }
            MethodCollector.o(14901);
            return obj;
        }
    }

    public VolumeChangedObserver(OnVolumeChangedListener onVolumeChangedListener2) {
        this.onVolumeChangedListener = onVolumeChangedListener2;
        registerVolumeChangeAction();
    }

    public static Intent INVOKEVIRTUAL_com_bytedance_bae_webrtc_observer_VolumeChangedObserver_com_bytedance_sysoptimizer_ReceiverRegisterLancet_registerReceiver(Context context, BroadcastReceiver broadcastReceiver, IntentFilter intentFilter) {
        if (ReceiverRegisterCrashOptimizer.fixedOpen()) {
            ReceiverRegisterCrashOptimizer.doHWReceiverFix();
        }
        try {
            if (ReceiverRegisterCrashOptimizer.doRegisterHandler()) {
                return context.registerReceiver(broadcastReceiver, intentFilter, (String) null, ReceiverRegisterLancet.sReceiverHandler);
            }
            return context.registerReceiver(broadcastReceiver, intentFilter);
        } catch (Exception e) {
            if (ReceiverRegisterCrashOptimizer.fixedOpen()) {
                return ReceiverRegisterCrashOptimizer.registerReceiver(broadcastReceiver, intentFilter);
            }
            throw e;
        }
    }
}
