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

import X.0HH;
import X.0aP;
import X.0by;
import X.0bz;
import X.0cm;
import X.0cp;
import X.0cr;
import X.0dj;
import X.0dk;
import X.15W;
import X.1MH;
import X.1MI;
import X.1jc;
import X.2W2;
import X.7g6;
import X.7lW;
import X.7us;
import X.7xc;
import X.8eL;
import X.9EH;
import X.9Wi;
import X.9bx;
import X.9hJ;
import X.C1313Fz8;
import X.C1899GKm;
import X.C2409Gbi;
import X.C2412Gbl;
import X.C4233HCg;
import X.C4373HHq;
import X.C4374HHr;
import X.C4375HHs;
import X.C4376HHt;
import X.C4378HHv;
import X.C4379HHw;
import X.C4380HHx;
import X.C4381HHy;
import X.C4382HHz;
import X.ESC;
import X.FLB;
import X.FLI;
import X.GMH;
import X.IIM;
import X.JMm;
import X.JXH;
import X.LnX;
import X.PgU;
import X.PhE;
import X.PlG;
import X.PlI;
import X.S9B;
import X.SfZ;
import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.CookieManager;
import com.bytedance.android.feed.api.ILiveFeedApiService;
import com.bytedance.android.live_settings.SettingsManager;
import com.bytedance.android.livesdk.init.BadPhonesCommonOptTask;
import com.bytedance.android.livesdk.init.FeedDelayInitTask;
import com.bytedance.android.livesdk.init.I18nInitTask;
import com.bytedance.android.livesdk.init.InternalServiceInitTask;
import com.bytedance.android.livesdk.init.MonitorInitTask;
import com.bytedance.android.livesdk.init.OptUtilsTask;
import com.bytedance.android.livesdk.init.PBInitTask;
import com.bytedance.android.livesdk.init.ParasiticModulesTask;
import com.bytedance.android.livesdk.init.PipoInitTask;
import com.bytedance.android.livesdk.init.SDKServiceInitTask;
import com.bytedance.android.livesdk.init.ServiceInitTask;
import com.bytedance.android.livesdk.init.TaskManagerInitTask;
import com.bytedance.android.livesdk.livesetting.other.LiveEnablePopupQueueSettings;
import com.bytedance.android.livesdk.livesetting.performance.LiveOptimizeThreadUsageSetting;
import com.bytedance.android.livesdk.livesetting.roomfunction.LiveUseEmojiCompatSetting;
import com.bytedance.android.livesdk.utils.LiveAppBundleUtils;
import com.bytedance.android.livesdkapi.host.IHostContext;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import com.bytedance.livesdk.api.base.ILiveALoggerService;
import com.bytedance.livesdk.api.base.ILiveDialogService;
import com.bytedance.livesdk.api.base.ILiveImageMonitorService;
import com.bytedance.livesdk.api.base.ILiveResourcesService;
import com.bytedance.livesdk.api.base.ILiveSettingService;
import com.bytedance.livesdk.api.base.ILiveSharedPreferencesService;
import com.bytedance.livesdk.api.base.ILiveSlardarMonitorService;
import com.bytedance.livesdk.api.feed.ILiveFeedContextService;
import com.bytedance.livesdk.api.feed.ILiveFeedUiService;
import com.bytedance.livesdk.impl.base.LiveALoggerService;
import com.bytedance.livesdk.impl.base.LiveDialogService;
import com.bytedance.livesdk.impl.base.LiveImageMonitorService;
import com.bytedance.livesdk.impl.base.LiveResourcesService;
import com.bytedance.livesdk.impl.base.LiveSettingService;
import com.bytedance.livesdk.impl.base.LiveSharedPreferencesService;
import com.bytedance.livesdk.impl.base.LiveSlardarMonitorService;
import com.bytedance.livesdk.impl.feed.LiveFeedContextService;
import com.bytedance.livesdk.impl.feed.LiveFeedUiService;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.ugc.aweme.out.AVExternalServiceImpl;
import com.ss.android.ugc.aweme.profile.model.User;
import com.ss.android.ugc.aweme.services.external.ui.LiveParams;
import com.ss.android.ugc.aweme.services.external.ui.RecordConfig;
import com.ss.android.ugc.aweme.services.external.ui.ReshootConfig;
import java.util.HashMap;
import java.util.Locale;
import org.json.JSONObject;

public class Live {
    public static C4381HHy hostService;
    public static volatile boolean sInitedLiteLiveSDK;
    public static volatile boolean sInitedLiveSDK;

    static {
        Covode.recordClassIndex(90941);
    }

    public static final /* synthetic */ void lambda$refreshLoginState$1$Live(Object obj) {
    }

    public static final /* synthetic */ void lambda$refreshLoginState$2$Live(Throwable th) {
    }

    public static C4233HCg getService() {
        return getService(false);
    }

    public static C4233HCg getServiceInternal() {
        return getServiceInternal(false);
    }

    public static boolean openLiveRecord() {
        return openLiveRecordBySchema("");
    }

    public static String getLiveDomain() {
        if (S9B.LIZJ()) {
            return "webcast.tiktokv.com";
        }
        return "webcast-va.tiktokv.com";
    }

    public static boolean invalid() {
        if (getServiceInternal() == null) {
            return true;
        }
        return false;
    }

    public static synchronized C4381HHy getHostService() {
        C4381HHy hHy;
        synchronized (Live.class) {
            MethodCollector.i(2741);
            if (hostService == null) {
                hostService = new C4382HHz();
            }
            hHy = hostService;
            MethodCollector.o(2741);
        }
        return hHy;
    }

    public static void refreshLoginState() {
        PlG LIZJ;
        PgU LIZ = PgU.LIZ(C4374HHr.LIZ);
        if (LiveOptimizeThreadUsageSetting.INSTANCE.getValue()) {
            LIZJ = PlI.LIZ(8eL.LIZ());
        } else {
            LIZJ = PhE.LIZJ(PlI.LJ);
        }
        LIZ.LIZIZ(LIZJ).LIZ(C4378HHv.LIZ, C4375HHs.LIZ);
    }

    public static void switchLocale() {
        C4233HCg service;
        Locale LIZ;
        Context LIZ2 = 9EH.LJJIFFI.LIZ();
        if (LIZ2 != null && (service = getService()) != null && (LIZ = LnX.LIZ((String) null, (String) null, LIZ2)) != null && !TextUtils.isEmpty(LIZ.getLanguage())) {
            service.LIZ(LIZ);
        }
    }

    public static C4233HCg getLiteService() {
        if (!7xc.LIZ(9EH.LJJIFFI.LIZ())) {
            0aP.LIZ(6, "Live", "cannot use LiveService in non-main process");
            HashMap hashMap = new HashMap(2);
            hashMap.put("error_msg", Log.getStackTraceString(new Throwable("")));
            GMH.LIZ.LIZ("ttlive_init_non_main_process_error", 1, hashMap);
            return null;
        }
        C4233HCg liteServiceInternal = getLiteServiceInternal();
        if (liteServiceInternal == null) {
            0aP.LIZ(6, "Live", "Live getLiteService is null");
            HashMap hashMap2 = new HashMap(2);
            hashMap2.put("error_msg", Log.getStackTraceString(new Throwable("")));
            GMH.LIZ.LIZ("ttlive_live_get_lite_service_null_error", 1, hashMap2);
        }
        return liteServiceInternal;
    }

    public static C4233HCg getLiteServiceInternal() {
        MethodCollector.i(2546);
        if (!sInitedLiteLiveSDK) {
            synchronized (Live.class) {
                try {
                    if (sInitedLiteLiveSDK) {
                        C4233HCg LIZ = C4380HHx.LIZ();
                        return LIZ;
                    }
                    0bz.LIZ("init_live_host_service");
                    initService();
                    C4381HHy hostService2 = getHostService();
                    0bz.LIZIZ("init_live_host_service");
                    C4380HHx.LIZ(hostService2);
                    0bz.LIZ("init_live_refresh_login_state");
                    refreshLoginState();
                    0bz.LIZIZ("init_live_refresh_login_state");
                    sInitedLiteLiveSDK = true;
                } finally {
                    MethodCollector.o(2546);
                }
            }
        }
        C4233HCg LIZ2 = C4380HHx.LIZ();
        MethodCollector.o(2546);
        return LIZ2;
    }

    public static void initService() {
        0cm.LIZ(ILiveInnerService.class, new LiveInnerService());
        0cm.LIZ(ILiveFeedUiService.class, new LiveFeedUiService());
        0cm.LIZ(ILiveFeedContextService.class, new LiveFeedContextService());
        0cm.LIZ(ILiveSlardarMonitorService.class, new LiveSlardarMonitorService());
        0cm.LIZ(ILiveSharedPreferencesService.class, new LiveSharedPreferencesService());
        0cm.LIZ(ILiveSettingService.class, new LiveSettingService());
        0cm.LIZ(ILiveResourcesService.class, new LiveResourcesService());
        0cm.LIZ(ILiveImageMonitorService.class, new LiveImageMonitorService());
        0cm.LIZ(ILiveDialogService.class, new LiveDialogService());
        0cm.LIZ(ILiveALoggerService.class, new LiveALoggerService());
    }

    public static void syncShareCookieToTarget() {
        String cookie;
        try {
            String LIZ = 0HH.LIZ("https://%s/", new Object[]{getLiveDomain()});
            if (!9Wi.LIZ() || LIZ.toLowerCase(Locale.ROOT).startsWith("https")) {
                String cookie2 = CookieManager.getInstance().getCookie(NetworkUtils.getShareCookieHost());
                CookieManager.getInstance().setAcceptCookie(true);
                if (!TextUtils.isEmpty(cookie2)) {
                    for (String cookie3 : cookie2.split("; ")) {
                        CookieManager.getInstance().setCookie(LIZ, cookie3);
                        if (7xc.LIZ(FLI.LIZ()) && 7us.LIZJ.LIZ() && (cookie = CookieManager.getInstance().getCookie(9hJ.LIZIZ.LIZ())) != null && !TextUtils.equals(cookie, 9hJ.LIZIZ.LIZIZ())) {
                            9hJ r1 = 9hJ.LIZIZ;
                            r1.LIZ(r1.LIZ(), cookie);
                        }
                    }
                    return;
                }
                return;
            }
            throw new IllegalArgumentException("Cookie url must be https");
        } catch (Exception unused) {
        }
    }

    public static boolean isPrivateAccount(User user) {
        if (user == null || !user.isSecret()) {
            return false;
        }
        return true;
    }

    public static boolean hasFollowed(User user) {
        if (user == null) {
            return false;
        }
        if (user.getFollowStatus() == 1 || user.getFollowStatus() == 2) {
            return true;
        }
        return false;
    }

    public static boolean showLive(User user) {
        if (user == null || !user.isLive() || !FLB.LIZ()) {
            return false;
        }
        if (isPrivateAccount(user)) {
            return hasFollowed(user);
        }
        return true;
    }

    public static C4233HCg getService(boolean z) {
        if (!7xc.LIZ(9EH.LJJIFFI.LIZ())) {
            0aP.LIZ(6, "Live", "cannot use LiveService in non-main process");
            HashMap hashMap = new HashMap(2);
            hashMap.put("error_msg", Log.getStackTraceString(new Throwable("")));
            GMH.LIZ.LIZ("ttlive_init_non_main_process_error", 1, hashMap);
            return null;
        }
        C4233HCg serviceInternal = getServiceInternal(z);
        if (serviceInternal == null) {
            0aP.LIZ(6, "Live", "Live getService is null");
            HashMap hashMap2 = new HashMap(2);
            hashMap2.put("error_msg", Log.getStackTraceString(new Throwable("")));
            GMH.LIZ.LIZ("ttlive_live_get_service_null_error", 1, hashMap2);
        }
        return serviceInternal;
    }

    public static boolean openLiveRecordBySchema(final String str) {
        final Activity LJIIIZ = SfZ.LJIJ.LJIIIZ();
        if (LJIIIZ == null) {
            return false;
        }
        if (C2409Gbi.LJFF) {
            C1313Fz8.LIZ((Context) LJIIIZ, "Your link jump method is about to be abandoned, please contact yanpeng.p to solve it", 1);
        }
        if (ESC.LIZLLL()) {
            9bx.LIZ("ttlive_minor_mode_live", "", (JSONObject) null);
        }
        if (IIM.LIZ(LJIIIZ) == 0 && IIM.LIZIZ(LJIIIZ) == 0 && IIM.LIZJ(LJIIIZ) == 0) {
            jumpToLive(LJIIIZ, str);
            return true;
        }
        JXH.LIZ(LJIIIZ, new String[]{"android.permission.CAMERA", "android.permission.RECORD_AUDIO"}, new JMm() {
            static {
                Covode.recordClassIndex(90942);
            }

            public final void LIZ(String[] strArr, int[] iArr) {
                int length = iArr.length;
                int i = 0;
                while (i < length) {
                    if (iArr[i] == 0) {
                        i++;
                    } else {
                        return;
                    }
                }
                Live.jumpToLive(LJIIIZ, str);
            }
        });
        return true;
    }

    public static C4233HCg getServiceInternal(boolean z) {
        MethodCollector.i(2460);
        if (!sInitedLiveSDK) {
            synchronized (Live.class) {
                try {
                    if (sInitedLiveSDK) {
                        C4233HCg LIZ = C4380HHx.LIZ();
                        MethodCollector.o(2460);
                        return LIZ;
                    }
                    0bz.LIZ("init_live_all_duration");
                    getLiteServiceInternal();
                    0bz.LIZ("init_live_service");
                    0bz.LIZ("init_live_sdk_service");
                    C4381HHy hHy = hostService;
                    boolean z2 = true;
                    if (!C4380HHx.LIZ) {
                        synchronized (C4380HHx.class) {
                            if (!C4380HHx.LIZ) {
                                C4380HHx.LIZ(hHy);
                                0bz.LIZ("init_live_setting_init");
                                SettingsManager.INSTANCE.init(0cm.LIZ(IHostContext.class).isLocalTest(), new C1899GKm(), new C2412Gbl());
                                0bz.LIZIZ("init_live_setting_init");
                                C4376HHt.LIZ.LIZ((7g6) new SDKServiceInitTask());
                                C4376HHt.LIZ.LIZ((7g6) new TaskManagerInitTask());
                                C4376HHt.LIZ.LIZIZ(new ServiceInitTask());
                                C4376HHt.LIZ.LIZIZ(new I18nInitTask());
                                C4376HHt.LIZ.LIZIZ(new PBInitTask());
                                C4376HHt.LIZ.LIZIZ(new InternalServiceInitTask());
                                C4376HHt.LIZ.LIZIZ(new FeedDelayInitTask());
                                C4376HHt.LIZ.LIZIZ(new PipoInitTask());
                                C4376HHt.LIZ.LIZIZ(new MonitorInitTask());
                                C4376HHt.LIZ.LIZIZ(new OptUtilsTask());
                                C4376HHt.LIZ.LIZIZ(new BadPhonesCommonOptTask());
                                C4376HHt.LIZ.LIZIZ(new ParasiticModulesTask());
                                C4379HHw.LIZ = LiveEnablePopupQueueSettings.INSTANCE.getValue();
                                0bz.LIZIZ("init_live_sdk_service");
                                0bz.LIZ("init_live_feed_service");
                                0cm.LIZ(ILiveFeedApiService.class).init();
                                0bz.LIZIZ("init_live_feed_service");
                                0bz.LIZIZ("init_live_service");
                                0bz.LIZ("init_live_launch_task");
                                C4376HHt.LIZ.LIZ();
                                2W2.LIZ();
                                0bz.LIZIZ("init_live_launch_task");
                                0cr r2 = 0cp.LIZ;
                                Context applicationContext = 0cm.LIZ(IHostContext.class).context().getApplicationContext();
                                if (7lW.LIZJ) {
                                    if (applicationContext == null) {
                                        applicationContext = 7lW.LIZ;
                                    }
                                }
                                r2.LIZJ = applicationContext;
                                0cp.LIZ.LIZ = LiveUseEmojiCompatSetting.INSTANCE.getValue();
                                0cp.LIZ.LIZIZ = true;
                                0cp.LIZ.LIZLLL = C4373HHq.LIZ;
                                0cp.LIZ.LIZ(2131234945, new 1jc());
                                if (!15W.LIZ) {
                                    0dj LIZ2 = 0dj.LIZ();
                                    0dk r1 = new 0dk();
                                    r1.LIZ(2131829471);
                                    r1.LIZIZ(2131829472);
                                    r1.LIZ();
                                    r1.LIZJ(2131234997);
                                    r1.LIZLLL(2131233349);
                                    LIZ2.LIZ = new 1MH(r1.LIZIZ());
                                    0dj LIZ3 = 0dj.LIZ();
                                    0dk r12 = new 0dk();
                                    r12.LIZ(2131829979);
                                    r12.LIZIZ(2131829978);
                                    r12.LIZ();
                                    r12.LIZJ(2131234966);
                                    r12.LIZLLL(2131234384);
                                    LIZ3.LIZIZ = new 1MI(r12.LIZIZ());
                                    15W.LIZ = true;
                                }
                                0bz.LIZ("init_live_so_loader");
                                LiveAppBundleUtils.initCoreSOLoader();
                                0bz.LIZIZ("init_live_so_loader");
                                C4380HHx.LIZ = true;
                            }
                        }
                    }
                    0bz.LIZ("init_live_launch_parasitic_modules");
                    sInitedLiveSDK = true;
                    0bz.LIZIZ("init_live_launch_parasitic_modules");
                    0bz.LIZIZ("init_live_all_duration");
                    if (Thread.currentThread() != Looper.getMainLooper().getThread()) {
                        z2 = false;
                    }
                    8eL.LIZJ().execute(new 0by(z2, z, Log.getStackTraceString(new Throwable(""))));
                } catch (Throwable th) {
                    MethodCollector.o(2460);
                    throw th;
                }
            }
        }
        C4233HCg LIZ4 = C4380HHx.LIZ();
        MethodCollector.o(2460);
        return LIZ4;
    }

    public static void jumpToLive(Activity activity, String str) {
        RecordConfig.Builder builder = new RecordConfig.Builder();
        builder.shootWay("direct_shoot");
        builder.translationType(3);
        builder.defaultTab(2);
        builder.enterFrom(Uri.parse(str).getQueryParameter("enter_from"));
        builder.liveParams(new LiveParams(Uri.parse(str).getQueryParameter("source_params"), false));
        builder.reshootConfig(new ReshootConfig(true, true));
        AVExternalServiceImpl.LIZ().asyncService("Live", new 2(activity, builder));
    }
}
