package com.hh.arome.util;

import android.os.Handler;
import android.os.Looper;

import java.lang.reflect.Method;

public class Utils {

    private static Handler sMainHandler = new Handler(Looper.getMainLooper());

    public static void runOnMain(Runnable runnable) {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            runnable.run();
        } else {
            sMainHandler.post(runnable);
        }
    }

    public static void runOnMain(Runnable runnable, long delay) {
        if (Looper.myLooper() == Looper.getMainLooper() && delay == 0) {
            runnable.run();
        } else {
            sMainHandler.postDelayed(runnable, delay);
        }
    }

    public static int parseInt(String value, int defaultValue) {
        try {
            return Integer.parseInt(value);
        } catch (Throwable t) {
            return defaultValue;
        }
    }

    public static String getIotVersion() {
        String serial = null;
        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method get = c.getMethod("get", String.class);
            serial = (String) get.invoke(c, "ro.serialno");
        } catch (Exception ignored) {

        }
        return serial;
    }
}
