package com.fxy.service;

import android.app.job.JobParameters;
import android.app.job.JobService;
import android.content.Context;

import com.elvishew.xlog.XLog;
import com.fxy.base.UploadContentProvider;
import com.fxy.common.AsyncFileUpload;

public class UploadJobService  extends JobService {
    public static final int CHARGING_JOB_ID = 100;
    public static final int DEVICE_IDLE_JOB_ID = 101;

    public  AsyncFileUpload asyncFileUpload;

    @Override
    public boolean onStartJob(JobParameters jobParameters) {
        // 在这里执行你的任务
        // 1. 查询Realm数据库
        // 2. 上传文件到OSS

        Context context = UploadContentProvider.getCurrentContext();
        XLog.d("------------执行异步上传图片--------------------"+jobParameters.getJobId());


        asyncFileUpload = new AsyncFileUpload(context);
        Integer[] pushStatus = {0,1};
        int awaitNum = asyncFileUpload.getAllAwaitNum(pushStatus);
        if (awaitNum == 0){
            // 标记任务为已完成（如果你不需要重试）
            jobFinished(jobParameters, false);
            return false;
        }else{
            asyncFileUpload.setStartUpload();
            jobFinished(jobParameters, true);
            // 返回 true 表示任务正在执行中
            return true;
        }

    }

    @Override
    public boolean onStopJob(JobParameters jobParameters) {
        if (asyncFileUpload!=null){
            asyncFileUpload.taskCancel();
        }
        return false;
    }




}
