package com.example.sleep.service;

import android.app.Service;
import android.app.job.JobParameters;
import android.app.job.JobService;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;

import com.example.sleep.MainScreen;
import com.example.sleep.utils.SystemUtils;

/**
 * 用于保活的JobService
 */
public class AliveJobService extends JobService {
    private volatile static Service mKeepAliveService = null;

    public static boolean isJobServiceAlive() {
        return mKeepAliveService != null;
    }

    private static final int MESSAGE_ID_TASK = 0x01;

    /**
     * 用来保活的Handler
     */
    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            // 如果应用已经被关闭，则重新启动MainActivity
            if (!SystemUtils.isAPPALive(getApplicationContext(), "com.howard.sleephelper")) {
                Intent intent = new Intent(getApplicationContext(), MainScreen.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
            jobFinished((JobParameters) msg.obj, false);
            return true;
        }
    });

    @Override
    public boolean onStartJob(JobParameters params) {
        mKeepAliveService = this;
        Message msg = Message.obtain(mHandler, MESSAGE_ID_TASK, params);
        mHandler.sendMessage(msg);
        return true;
    }

    @Override
    public boolean onStopJob(JobParameters params) {
        // 移除保活的消息
        mHandler.removeMessages(MESSAGE_ID_TASK);
        return false;
    }
}
