package org.literacyapp.service.synchronization;

import android.app.job.JobParameters;
import android.app.job.JobService;
import android.content.Intent;
import android.util.Log;

import org.literacyapp.authentication.AuthenticationActivity;
import org.literacyapp.authentication.TrainingHelper;
import org.literacyapp.receiver.BootReceiver;
import org.literacyapp.service.FaceRecognitionTrainingJobService;

/**
 * Created by sladomic on 26.11.16.
 */

public class AuthenticationJobService extends JobService {
    @Override
    public boolean onStartJob(JobParameters jobParameters) {
        Log.i(getClass().getName(), "onStartJob");
        if (!rescheduleIfTrainingJobServiceIsRunning()){
            Intent authenticationIntent = new Intent(getApplicationContext(), AuthenticationActivity.class);
            authenticationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(authenticationIntent);
        }
        return false;
    }

    @Override
    public boolean onStopJob(JobParameters jobParameters) {
        Log.i(getClass().getName(), "onStopJob");
        return false;
    }

    private boolean rescheduleIfTrainingJobServiceIsRunning(){
        boolean isFaceRecognitionTrainingJobServiceRunning = FaceRecognitionTrainingJobService.isRunning;
        Log.i(getClass().getName(), "isFaceRecognitionTrainingJobServiceRunning: " + isFaceRecognitionTrainingJobServiceRunning);
        if (isFaceRecognitionTrainingJobServiceRunning){
            BootReceiver.scheduleAuthentication(getApplicationContext(), BootReceiver.MINUTES_BETWEEN_AUTHENTICATIONS);
            Log.i(getClass().getName(), "The AuthenticationJobService has been rescheduled, because the CPU hungry FaceRecognitionTrainingJobService is running at the moment.");
        }
        return isFaceRecognitionTrainingJobServiceRunning;
    }
}

@@ -143,39 +143,148 @@
 ent.
-Intent;%0Aimport android.util.Log
+Context;%0Aimport android.content.Intent;%0Aimport android.hardware.display.DisplayManager;%0Aimport android.util.Log;%0Aimport android.view.Display
 ;%0A%0Ai
@@ -737,24 +737,63 @@
 StartJob%22);%0A
+        if (!isScreenTurnedOff())%7B%0A    
         if (
@@ -833,24 +833,28 @@
 Running())%7B%0A
+    
             
@@ -962,16 +962,20 @@
         
+ 
+   
 authenti
@@ -1040,16 +1040,20 @@
         
+    
 startAct
@@ -1081,16 +1081,242 @@
 ntent);%0A
+                Log.i(getClass().getName(), %22The Authentication has been started.%22);%0A            %7D%0A        %7D else %7B%0A            Log.i(getClass().getName(), %22The Authentication was skipped because the screen was turned off.%22);%0A
         
@@ -1309,32 +1309,33 @@
 f.%22);%0A        %7D%0A
+%0A
         return f
@@ -2228,11 +2228,486 @@
 g;%0A    %7D
+%0A%0A    private boolean isScreenTurnedOff()%7B%0A        boolean isScreenTurnedOff = false;%0A        DisplayManager displayManager = (DisplayManager) getApplicationContext().getSystemService(Context.DISPLAY_SERVICE);%0A        for (Display display : displayManager.getDisplays()) %7B%0A            if (display.getState() != Display.STATE_ON) %7B%0A                // Skip authentication%0A                isScreenTurnedOff = true;%0A            %7D%0A        %7D%0A        return isScreenTurnedOff;%0A    %7D
 %0A%7D%0A

Fix #308 AuthenticationActivity crashes if it starts while the screen is turned off and then turned on