/*
 * Audio recording service that captures audio from the mic to files in
 * internal storage.
 *
 * The application needs to have the permission to write to external storage
 * if the output file is written to the external storage, and also the
 * permission to record audio. These permissions must be set in the
 * application's AndroidManifest.xml file, with something like:
 *
 * <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 * <uses-permission android:name="android.permission.RECORD_AUDIO" />
 *
 */
package com.spaceout;

import android.app.Service;
import android.os.Bundle;
import android.os.Environment;
import android.view.ViewGroup;
import android.util.Log;
import android.media.MediaRecorder;

import java.io.IOException;

import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


public class AudioRecorderService extends Service
{
    private static final String LOG_TAG = "AudioRecorderService";
    private static String mFileNamePrefix = "spaceout_";


    private MediaRecorder mRecorder = null;

	// audio must be broken into chunks because Wit cannot consume audio clips
	// longer than 10 seconds
	
	// the duration of each audio chunk in seconds; since Wit accepts a
	// maximum of 10s, we define our chunk duration slightly less because Java
	// does not guarantee scheduled events will fire exactly on time so we
	// give Java a half second of padding
	private static final double CHUNK_DURATION_S = 9.5

	// index number of current audio chunk
	private int latestFileChunk = -1;

	// Unix timestamp at which current audio chunk starts
	private long latestFileChunkStartTimestamp = 0;

	private ScheduledThreadPoolExecutor scheduler =
		new ScheduledThreadPoolExecutor(2)


    private void onRecord(boolean start) {
        if (start) {
            startRecording();
        } else {
            stopRecording();
        }
    }

    private void startRecording() {
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

		this.scheduler.scheduleAtFixedRate(
			<command>,
			0,
			CHUNK_DURATION,
			TimeUnit.SECONDS
		)
    }

	private void startRecordingChunk() {

		this.latestFileChunk++;
        mRecorder.setOutputFile(mFileName + latestFileChunk.toString());

        try {
            mRecorder.prepare();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }

		this.latestFileChunkStartTimestamp = System.getCurrentTimeMillis();
        mRecorder.start();
	}

	private void stopRecordingChunk() {
        mRecorder.stop();
	}

    private void stopRecording() {
		this.stopRecordingChunk();
        mRecorder.release();
        mRecorder = null;
    }
}


@@ -1035,17 +1035,16 @@
 out_%22;%0A%0A
-%0A
     priv
@@ -1566,118 +1566,19 @@
 hunk
- = -1;%0A%0A%09// Unix timestamp at which current audio chunk starts%0A%09private long latestFileChunkStartTimestamp = 0
+Number = -1
 ;%0A%0A%09
@@ -1663,10 +1663,11 @@
 tor(
-2)
+1);
 %0A%0A%0A 
@@ -1895,24 +1895,199 @@
 Recorder();%0A
+%0A%09%09this.scheduler.scheduleAtFixedRate(%0A%09%09%09%3Ccommand%3E,%0A%09%09%090,%0A%09%09%09CHUNK_DURATION,%0A%09%09%09TimeUnit.SECONDS%0A%09%09)%0A    %7D%0A%0A%09private void startRecordingChunk() %7B%0A%0A%09%09this.latestFileChunk++;%0A%0A
         mRec
@@ -2285,182 +2285,8 @@
 NB);
-%0A%0A%09%09this.scheduler.scheduleAtFixedRate(%0A%09%09%09%3Ccommand%3E,%0A%09%09%090,%0A%09%09%09CHUNK_DURATION,%0A%09%09%09TimeUnit.SECONDS%0A%09%09)%0A    %7D%0A%0A%09private void startRecordingChunk() %7B%0A%0A%09%09this.latestFileChunk++;
 %0A   
@@ -2341,16 +2341,22 @@
 ileChunk
+Number
 .toStrin
@@ -2506,104 +2506,245 @@
  %7D%0A%0A
-%09%09this.latestFileChunkStartTimestamp = System.getCurrentTimeMillis();%0A        mRecorder.start();
+        mRecorder.start();%0A%09%7D%0A%0A%09public class TransitionToNextChunk implements Runnable %7B%0A%0A%09%09@Override%0A%09%09public void run() %7B%0A%09%09%09// FIXME: I have no idea how to call the outer class'%0A%09%09%09// stopRecordingChunk() and startRecordingChunk()%0A%09%09%7D
 %0A%09%7D%0A

Make progress on building the audio recorder, but still not complete.