package com.samsung.sdk.sperf;

import X.0HH;
import android.content.Context;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import java.util.HashMap;

public class SPerfManager {
    public static BoostLevelControl boostLevelControl;
    public static Context mContext = null;
    public static SPerfManager mInstance;
    public static int nextBoostObjectID;
    public static HashMap<Integer, TaskBoostReleaseHandler> releaserHash = new HashMap<>();
    public static SPerfInitThread sPerfThread = new SPerfInitThread();
    public static SessionControl sessionControl;

    static {
        Covode.recordClassIndex(47333);
    }

    public static SPerfManager createInstance(Context context) {
        SPerfManager sPerfManager;
        Class<SPerfManager> cls = SPerfManager.class;
        MethodCollector.i(5479);
        synchronized (cls) {
            try {
                sPerfManager = mInstance;
                if (sPerfManager == null) {
                    sPerfManager = null;
                    if (context != null) {
                        Thread thread = new Thread(sPerfThread);
                        thread.start();
                        thread.join();
                        sPerfManager = sPerfThread.getSPerfManager();
                        mContext = context;
                        int i = 0;
                        while (true) {
                            if (CustomParams.getBoostType(i) == 1 && mContext != null) {
                                releaserHash.put(Integer.valueOf(i), new TaskBoostReleaseHandler(mContext, sPerfManager, i));
                            }
                            i++;
                            if (i >= 11) {
                                break;
                            }
                        }
                    } else {
                        SPerfUtil.logAlways("ERROR : Context is Null");
                    }
                }
            } catch (InterruptedException e) {
                e = e;
                SPerfUtil.logAlways("ERROR : InterruptedException");
                0HH.LIZ(e);
                MethodCollector.o(5479);
                return sPerfManager;
            } catch (IllegalThreadStateException e2) {
                e = e2;
                SPerfUtil.logAlways("ERROR : IllegalThreadStateException");
                0HH.LIZ(e);
                MethodCollector.o(5479);
                return sPerfManager;
            } catch (Throwable th) {
                MethodCollector.o(5479);
                throw th;
            }
        }
        MethodCollector.o(5479);
        return sPerfManager;
    }

    public static SPerfManager initSPerfManager() {
        String str;
        if (mInstance == null) {
            SessionControl createInstance = SessionControl.createInstance();
            sessionControl = createInstance;
            if (createInstance == null) {
                str = "ERROR : Initialize Fail (Session)";
            } else {
                BoostLevelControl createInstance2 = BoostLevelControl.createInstance(createInstance);
                boostLevelControl = createInstance2;
                if (createInstance2 == null) {
                    str = "ERROR : Initialize Fail (BoostingLevelSet)";
                } else {
                    mInstance = new SPerfManager();
                }
            }
            SPerfUtil.logAlways(str);
            return null;
        }
        return mInstance;
    }

    public static void startTaskReleaseHandler(int i, int i2) {
        HashMap<Integer, TaskBoostReleaseHandler> hashMap = releaserHash;
        if (hashMap != null) {
            hashMap.get(Integer.valueOf(i)).startBoostTimeout(i2);
        }
    }

    public void addSPerfListerner(SPerfListener sPerfListener) {
        String str;
        if (sPerfListener == null) {
            str = "ERROR : Null listener (addPerfSDKListerner)";
        } else {
            SessionControl sessionControl2 = sessionControl;
            if (sessionControl2 == null) {
                str = "ERROR : SessionControl Should be initialized";
            } else {
                sessionControl2.addListerner(sPerfListener);
                return;
            }
        }
        SPerfUtil.log(str);
    }

    public int createBoostID() {
        int i;
        MethodCollector.i(5676);
        synchronized (this) {
            try {
                i = nextBoostObjectID;
                nextBoostObjectID = i + 1;
            } finally {
                MethodCollector.o(5676);
            }
        }
        return i;
    }

    public int startBoost(BoostObject boostObject) {
        if (boostObject == null) {
            SPerfUtil.log("ERROR : null boost object (doBoost)");
            return -1;
        }
        if (boostObject.getID() == -1) {
            boostObject.setID(createBoostID());
        }
        return Boost.requestNativeBoost(boostObject);
    }

    public int startPresetBoost(int i, int i2) {
        String str;
        BoostLevelControl boostLevelControl2 = boostLevelControl;
        if (boostLevelControl2 == null) {
            str = "ERROR : BoostLevelControl Should be initialized";
        } else if (i2 >= 0) {
            return startBoost(boostLevelControl2.getBoostObjectByScenario(i, i2));
        } else {
            str = "ERROR : timeout coundn't be less than 0";
        }
        SPerfUtil.log(str);
        return -1;
    }

    public int stopBoost() {
        if (boostLevelControl != null) {
            return Boost.releaseNativeBoost();
        }
        SPerfUtil.log("ERROR : BoostLevelControl Should be initialized");
        return -1;
    }

    public int stopBoost(int i, int i2) {
        if (boostLevelControl != null) {
            return Boost.releaseNativeBoost(i, i2);
        }
        SPerfUtil.log("ERROR : BoostLevelControl Should be initialized");
        return -1;
    }

    public int updateBoost(int i, int i2, int i3, BoostObject boostObject) {
        BoostLevelControl boostLevelControl2 = boostLevelControl;
        if (boostLevelControl2 != null) {
            return boostLevelControl2.updateBoost(i, i2, i3, boostObject);
        }
        SPerfUtil.log("ERROR : Initialize Fail boostLevel not exist (updateBoost)");
        return -1;
    }
}
