package com.ss.android.ugc.aweme.live.livehostimpl;

import X.06H;
import X.0aP;
import X.2FV;
import X.2He;
import X.C08347s1;
import X.C28338jA;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import com.bytedance.android.livesdk.util.GlobalContext;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import com.bytedance.helios.sdk.utils.ServiceLifecycle;
import com.bytedance.ies.livehost_impl.R$drawable;
import java.lang.reflect.Field;

public class BgBroadcastService extends Service {
    public NotificationManager mNotificationManager;

    static {
        Covode.recordClassIndex(91189);
    }

    public void onDestroy() {
        0aP.LIZIZ("BgBroadcastService onDestroy");
        stopForeground(true);
        super.onDestroy();
    }

    public void onCreate() {
        super.onCreate();
        0aP.LIZIZ("BgBroadcastService onCreate");
        this.mNotificationManager = (NotificationManager) INVOKEVIRTUAL_com_ss_android_ugc_aweme_live_livehostimpl_BgBroadcastService_com_ss_android_ugc_aweme_lancet_GetSystemServiceLancet_getSystemService(this, "notification");
        startForeground(2131368091, buildNotification(this));
    }

    public IBinder onBind(Intent intent) {
        0aP.LIZIZ("BgBroadcastService onBind");
        return new Binder();
    }

    public void onTaskRemoved(Intent intent) {
        0aP.LIZIZ("BgBroadcastService onTaskRemoved");
        super.onTaskRemoved(intent);
    }

    public void onTrimMemory(int i) {
        0aP.LIZIZ("BgBroadcastService onTrimMemory level = ".concat(String.valueOf(i)));
        super.onTrimMemory(i);
    }

    private Notification buildNotification(Context context) {
        06H r4;
        if (Build.VERSION.SDK_INT >= 26 && this.mNotificationManager.getNotificationChannel("BgBroadcastService.notification") == null) {
            NotificationChannel notificationChannel = new NotificationChannel("BgBroadcastService.notification", getString(2131826677), 3);
            notificationChannel.enableVibration(false);
            this.mNotificationManager.createNotificationChannel(notificationChannel);
        }
        Intent intent = new Intent();
        intent.setClassName(context, "com.ss.android.ugc.aweme.live.LiveBgBroadcastActivity");
        PendingIntent activity = PendingIntent.getActivity(context, 0, intent, 134217728);
        if (Build.VERSION.SDK_INT >= 26) {
            r4 = new 06H(context, "BgBroadcastService.notification");
        } else {
            r4 = new 06H(context);
        }
        r4.LIZ(getString(2131826685));
        r4.LIZ(R$drawable.icon);
        r4.LIZ(2, true);
        r4.LIZ(System.currentTimeMillis());
        r4.LJFF = activity;
        r4.LJJIII = "service";
        r4.LIZIZ();
        r4.LIZIZ(true);
        r4.LIZ(2, true);
        r4.LJJIIZ = 1;
        return r4.LJ();
    }

    public static Object INVOKEVIRTUAL_com_ss_android_ugc_aweme_live_livehostimpl_BgBroadcastService_com_ss_android_ugc_aweme_lancet_GetSystemServiceLancet_getSystemService(BgBroadcastService bgBroadcastService, String str) {
        Object obj;
        MethodCollector.i(11654);
        if (Build.VERSION.SDK_INT > 27 || !"clipboard".equals(str)) {
            if (!2FV.LIZIZ && "connectivity".equals(str)) {
                try {
                    new C08347s1().LIZ();
                    2FV.LIZIZ = true;
                    obj = bgBroadcastService.getSystemService(str);
                } catch (Throwable unused) {
                }
            }
            obj = bgBroadcastService.getSystemService(str);
        } else if (2FV.LIZ) {
            synchronized (ClipboardManager.class) {
                try {
                    obj = bgBroadcastService.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) {
                    C28338jA.LIZ(e, "ClipboardManager Handler Reflect Fail");
                } catch (Throwable th) {
                    MethodCollector.o(11654);
                    throw th;
                }
                2FV.LIZ = false;
            }
        } else {
            obj = bgBroadcastService.getSystemService(str);
        }
        MethodCollector.o(11654);
        return obj;
    }

    public int onStartCommand(Intent intent, int i, int i2) {
        ServiceLifecycle.onStartCommand(this, intent, i, i2);
        0aP.LIZIZ("BgBroadcastService onStartCommand");
        if (intent != null && "ACTION_SERVICE_START_FOREGROUND".equals(intent.getAction())) {
            startForeground(2131368091, buildNotification(this));
            LocalBroadcastManager.getInstance(GlobalContext.getApplicationContext()).sendBroadcast(new Intent("ACTION_SERVICE_START_FOREGROUND_END"));
        }
        return super.onStartCommand(intent, i, i2);
    }
}
