package ir.gemacc.admin;

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

public class SmsSyncJobService extends JobService {
    @Override
    public boolean onStartJob(final JobParameters params) {
        new Thread(() -> {
            boolean success;
            try {
                if (params.getJobId() == SmsUploader.JOB_PERIODIC_SCAN) {
                    SmsUploader.scanRecentSmsBlocking(getApplicationContext(), 72);
                    SmsUploader.pingServerBlocking(getApplicationContext());
                }
                success = SmsUploader.processQueueBlocking(getApplicationContext());
                SmsUploader.schedulePeriodicSafetyScan(getApplicationContext());
            } catch (Exception ignored) {
                success = false;
            }
            jobFinished(params, !success && params.getJobId() == SmsUploader.JOB_IMMEDIATE);
        }).start();
        return true;
    }

    @Override
    public boolean onStopJob(JobParameters params) {
        return params.getJobId() == SmsUploader.JOB_IMMEDIATE;
    }
}
