package com.lc.myapplication.security.locks.tool;

import android.app.Service;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;

import com.lc.myapplication.security.locks.value.MeValues;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MyTools {
    private static SharedPreferences sharedPreferences;
    private static SharedPreferences.Editor editor;
    private static ExecutorService executorService;
    private static Handler myHandler;

    public static void init(Context context){
        if (sharedPreferences == null){
            sharedPreferences = context.getSharedPreferences(MeValues.SHARE_NAME, Service.MODE_PRIVATE);
        }
        if(editor == null){
            editor = sharedPreferences.edit();
        }
    }
    public static void setPassword(String pin){
        editor.putString(MeValues.sh_password,pin);
        editor.apply();
    }

    public static void settest(String key,boolean value){
        editor.putBoolean(key,value);
        editor.apply();
    }
    public static boolean getTest(String key){
        return sharedPreferences.getBoolean(key,false);
    }

    public static String getPassword(){
        return sharedPreferences.getString(MeValues.sh_password,"");
    }

    public static ExecutorService getExecutorService(){
        if (executorService == null){
            executorService = Executors.newSingleThreadExecutor();
        }
        return executorService;
    }
    public static void RUNIO(Runnable runnable){
        getExecutorService().execute(runnable);
    }
    private static android.os.Handler getMyHandler() {
        if (myHandler == null) {
            myHandler = new Handler(Looper.getMainLooper());
        }
        return myHandler;
    }
    public static void onRunMain(Runnable runnable){
        getMyHandler().post(runnable);
    }

}
