package com.tool.applockpro.tool;

import android.app.IntentService;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import androidx.annotation.Nullable;

import com.tool.applockpro.data.MyDataBase;
import com.tool.applockpro.data.Mydata;

import java.util.Objects;

public class Myservice extends IntentService {
    private String lastLockPackName = "";
    private Mymanager myinstance;
    private Handler mHandler;

    @Override
    public void onCreate() {
        super.onCreate();
        mHandler = new Handler(Looper.getMainLooper());
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        myinstance = Mymanager.getInstance(this);
        while (true) {
            String appname = Mytools.checkTopapp(this);
//            Log.e("zzj","--- ----"+appname);
            if (appname.isEmpty()) {
                continue;
            }
            Mydata mydata = MyDataBase.getINSTANCE().mydao().getappislock(appname);

            if (mydata == null) {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        myinstance.unLock();
                    }
                });
                lastLockPackName = appname;
                continue;
            }
            if (mydata.isLock()) {
                if (!Objects.equals(lastLockPackName, appname)) {
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            myinstance.showLockView();
                        }
                    });
                }
            } else {
            }

            lastLockPackName = appname;
        }
    }

    public Myservice() {
        super("");
    }
}
