package io.github.privacystreams.audio;

import android.Manifest;

import io.github.privacystreams.core.exceptions.PSException;
import io.github.privacystreams.core.PStreamProvider;
import io.github.privacystreams.utils.Globals;

import java.io.IOException;

/**
 * Record audio periodically with the microphone.
 */

class AudioPeriodicRecorder extends PStreamProvider {
    private final Long durationPerRecord;
    private final Long interval;

    AudioPeriodicRecorder(long durationPerRecord, long interval) {
        this.durationPerRecord = durationPerRecord;
        this.interval = interval;
        this.addParameters(durationPerRecord, interval);
        this.addRequiredPermissions(Manifest.permission.RECORD_AUDIO);
    }

    @Override
    protected void provide() {
        if (Globals.AudioConfig.useAlarmScheduler) {

        } else {
            while (!this.isCancelled) {
                Audio audioItem = null;
                try {
                    audioItem = AudioRecorder.recordAudio(this.getUQI(), this.durationPerRecord);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (RuntimeException e) {
                    e.printStackTrace();
                    this.raiseException(this.getUQI(), PSException.INTERRUPTED("AudioPeriodicRecorder failed. Perhaps the audio duration is too short."));
                }
                if (audioItem != null) this.output(audioItem);
                try {
                    Thread.sleep(this.interval);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            this.finish();
        }
    }

}

@@ -60,16 +60,58 @@
 ifest;%0A%0A
+import io.github.privacystreams.core.UQI;%0A
 import i
@@ -255,23 +255,124 @@
 s.utils.
-Globals
+AlarmScheduler;%0Aimport io.github.privacystreams.utils.Globals;%0Aimport io.github.privacystreams.utils.Logging
 ;%0A%0Aimpor
@@ -872,16 +872,70 @@
 %0A    %7D%0A%0A
+    private transient AlarmScheduler alarmScheduler;%0A%0A
     @Ove
@@ -1028,104 +1028,752 @@
 ) %7B%0A
-%0A        %7D else %7B%0A            while (!this.isCancelled) %7B%0A                Audio audioItem = null
+            recordOnce();%0A            alarmScheduler = new AlarmScheduler(getContext(), this.getClass().getName()) %7B%0A                @Override%0A                protected void run() %7B%0A                    if (!isCancelled) %7B%0A                        recordOnce();%0A                        alarmScheduler.schedule(interval);%0A                    %7D else %7B%0A                        finish();%0A                    %7D%0A                %7D%0A            %7D;%0A        %7D else %7B%0A            while (!this.isCancelled) %7B%0A                recordOnce();%0A                try %7B%0A                    Thread.sleep(this.interval);%0A                %7D catch (InterruptedException e) %7B%0A                    e.printStackTrace();%0A                %7D%0A            %7D%0A            this.finish()
 ;%0A  
@@ -1778,38 +1778,103 @@
         
+%7D%0A
     
+%7D%0A%0A
     
-try %7B%0A        
+private void recordOnce() %7B%0A        Audio audioItem = null;%0A        try %7B%0A
         
@@ -1959,32 +1959,24 @@
 d);%0A        
-      
-  
 %7D catch (IOE
@@ -1997,32 +1997,24 @@
             
-        
 e.printStack
@@ -2026,32 +2026,24 @@
 ();%0A        
-      
-  
 %7D catch (Run
@@ -2069,32 +2069,24 @@
             
-        
 e.printStack
@@ -2102,32 +2102,24 @@
             
-      
-  
 this.raiseEx
@@ -2249,34 +2249,18 @@
         
-        %7D%0A        
+%7D%0A
         
@@ -2306,39 +2306,130 @@
 oItem);%0A    
+%7D%0A%0A
     
-        try
+@Override%0A    protected void onCancel(UQI uqi) %7B%0A        if (Globals.AudioConfig.useAlarmScheduler)
  %7B%0A         
@@ -2427,32 +2427,38 @@
             
+try %7B%0A
         
 Thread.sleep
@@ -2449,34 +2449,39 @@
     
-Thread.sleep(this.interval
+        alarmScheduler.destroy(
 );%0A 
@@ -2483,36 +2483,32 @@
 ();%0A            
-  
-  
 %7D catch (Interru
@@ -2496,35 +2496,24 @@
    %7D catch (
-Interrupted
 Exception e)
@@ -2531,39 +2531,45 @@
         
-   
- e.printStackTrac
+Logging.error(e.getMessag
 e()
+)
 ;%0A      
@@ -2566,36 +2566,32 @@
 ));%0A            
-    
 %7D%0A            %7D%0A
@@ -2576,36 +2576,32 @@
       %7D%0A        
- 
-   
 %7D%0A            th
@@ -2598,42 +2598,33 @@
     
-    this.finish();%0A        %7D%0A    %7D%0A
+super.onCancel(uqi);%0A    %7D
 %0A%7D%0A

add AlarmScheduler to Audio.recordPeriodic provider