package com.ss.android.ugc.aweme.legoImp.task;

import X.7s1;
import X.8Zd;
import X.8Ze;
import X.8ec;
import X.8jA;
import X.9Ft;
import X.9Fx;
import X.9LK;
import X.AnonymousClass2FV;
import X.AnonymousClass2d4;
import X.C00802He;
import X.C16342te;
import X.C16372th;
import android.content.ClipboardManager;
import android.content.Context;
import android.net.ConnectivityManager;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import java.lang.reflect.Field;
import java.util.List;
import kotlin.h.b.n;
import org.json.JSONObject;

public final class NetworkRestrictionTask implements 8Ze {
    static {
        Covode.recordClassIndex(90536);
    }

    public final boolean meetTrigger() {
        return true;
    }

    public final String prefix() {
        return "task_";
    }

    public final boolean serialExecute() {
        return false;
    }

    public final int targetProcess() {
        return 1048575;
    }

    public final List triggerOtherLegoComponents() {
        return null;
    }

    public final 9Ft triggerType() {
        return 8Zd.LIZ(this);
    }

    public final 9Fx scenesType() {
        return 9Fx.DEFAULT;
    }

    public final 8ec type() {
        return 8ec.IDLE;
    }

    public final String key() {
        return getClass().getSimpleName();
    }

    public final void run(Context context) {
        int intValue;
        Object obj;
        if (context != null && (intValue = ((Number) AnonymousClass2d4.LIZ.getValue()).intValue()) != 0) {
            try {
                Object LIZ = LIZ(context, "connectivity");
                if (!(LIZ instanceof ConnectivityManager)) {
                    LIZ = null;
                }
                ConnectivityManager connectivityManager = (ConnectivityManager) LIZ;
                if (connectivityManager != null && connectivityManager.isActiveNetworkMetered()) {
                    int restrictBackgroundStatus = connectivityManager.getRestrictBackgroundStatus();
                    int i = 2;
                    if (restrictBackgroundStatus != 2) {
                        if (restrictBackgroundStatus == 3) {
                            i = 1;
                        } else {
                            return;
                        }
                    }
                    obj = Integer.valueOf(i);
                    C16342te.m1constructorimpl(obj);
                    if (C16342te.m7isSuccessimpl(obj)) {
                        int intValue2 = ((Number) obj).intValue();
                        if ((intValue & intValue2) == intValue2) {
                            JSONObject put = new JSONObject().put("restrict_status", intValue2);
                            n.LIZIZ(put, "");
                            9LK.LIZ("restrict_background_status", put, (JSONObject) null, (JSONObject) null);
                        }
                    }
                    Throwable r0 = C16342te.m4exceptionOrNullimpl(obj);
                    if (r0 != null && (intValue & 4) == 4) {
                        JSONObject put2 = new JSONObject().put("e", r0.getMessage());
                        n.LIZIZ(put2, "");
                        JSONObject put3 = new JSONObject().put("restrict_status", 4);
                        n.LIZIZ(put3, "");
                        9LK.LIZ("restrict_background_status", put3, (JSONObject) null, put2);
                    }
                }
            } catch (Throwable th) {
                obj = C16372th.LIZ(th);
                C16342te.m1constructorimpl(obj);
            }
        }
    }

    public static Object LIZ(Context context, String str) {
        Object obj;
        MethodCollector.i(3299);
        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(3299);
                    throw th;
                }
                AnonymousClass2FV.LIZ = false;
            }
        } else {
            obj = context.getSystemService(str);
        }
        MethodCollector.o(3299);
        return obj;
    }
}
