package com.bytedance.sysoptimizer;

import X.0HH;
import android.content.Context;
import android.os.Build;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;

public class NativeBitmap {
    public static boolean mEnabled;
    public static boolean mStarted;

    static {
        Covode.recordClassIndex(38418);
    }

    public static native boolean start(int i);

    public static double getJavaHeapUtilizaiton() {
        return (((double) Runtime.getRuntime().totalMemory()) * 1.0d) / ((double) Runtime.getRuntime().maxMemory());
    }

    public static synchronized boolean enable(Context context) {
        synchronized (NativeBitmap.class) {
            MethodCollector.i(18080);
            if (Build.VERSION.SDK_INT >= 22) {
                if (Build.VERSION.SDK_INT <= 25) {
                    boolean realStart = realStart(context);
                    MethodCollector.o(18080);
                    return realStart;
                }
            }
            MethodCollector.o(18080);
            return false;
        }
    }

    public static synchronized boolean realStart(Context context) {
        synchronized (NativeBitmap.class) {
            MethodCollector.i(18303);
            if (mEnabled) {
                MethodCollector.o(18303);
                return true;
            }
            if (SysOptimizer.loadOptimizerLibrary(context)) {
                try {
                    boolean start = start(Build.VERSION.SDK_INT);
                    mEnabled = start;
                    MethodCollector.o(18303);
                    return start;
                } catch (UnsatisfiedLinkError unused) {
                }
            }
            MethodCollector.o(18303);
            return false;
        }
    }

    public static synchronized boolean enable(final Context context, long j, double d, long j2) {
        synchronized (NativeBitmap.class) {
            MethodCollector.i(18223);
            if (Build.VERSION.SDK_INT < 22 || Build.VERSION.SDK_INT > 25) {
                MethodCollector.o(18223);
                return false;
            } else if (Runtime.getRuntime().maxMemory() > j2) {
                MethodCollector.o(18223);
                return false;
            } else if (mStarted || mEnabled) {
                MethodCollector.o(18223);
                return true;
            } else {
                mStarted = true;
                final long j3 = j;
                final double d2 = d;
                new Thread((ThreadGroup) null, new Runnable() {
                    static {
                        Covode.recordClassIndex(38419);
                    }

                    public final void run() {
                        MethodCollector.i(14144);
                        while (true) {
                            try {
                                Thread.sleep(j3);
                                if (NativeBitmap.mEnabled) {
                                    MethodCollector.o(14144);
                                    return;
                                } else if (NativeBitmap.getJavaHeapUtilizaiton() >= d2) {
                                    NativeBitmap.realStart(context);
                                    MethodCollector.o(14144);
                                    return;
                                }
                            } catch (InterruptedException e) {
                                0HH.LIZ(e);
                            }
                        }
                    }
                }, "NativeBitmap", 262144).start();
                MethodCollector.o(18223);
                return true;
            }
        }
    }
}
