package owenhsieh.tldr.audiorecorddemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

public class MainActivity extends AppCompatActivity {

    private String FILE_DIR;
    private MiniRecorder recorder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FILE_DIR = getExternalCacheDir().getPath();
        File file = new File(FILE_DIR);
        if (!file.exists()) file.mkdir();

        recorder = new MiniRecorder();

        final View pushToTalkButton = findViewById(R.id.push_to_talk_btn);
        final ImageView pushToTalkImg = (ImageView) findViewById(R.id.push_to_talk_img);
        if (pushToTalkButton != null) {
            pushToTalkButton.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            recorder.setEnable(true);
                            if (pushToTalkImg != null) {
                                pushToTalkImg.setImageResource(R.drawable.ic_mic);
                            }
                            return true;
                        case MotionEvent.ACTION_UP:
                        case MotionEvent.ACTION_CANCEL:
                            if (recorder.isEnable()) {
                                recorder.setEnable(false);
                                saveFile();
                            }
                            if (pushToTalkImg != null) {
                                pushToTalkImg.setImageResource(R.drawable.ic_mic_off);
                            }
                            return true;
                    }
                    return false;
                }
            });
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        recorder.resume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        recorder.release();
    }

    private void saveFile() {
        ByteBuffer rawBuffer = ByteBuffer.allocate(recorder.getBufferSize());
        recorder.getBuffer(rawBuffer);

        try {
            File file;
            DataOutputStream outputStream;

            /* save raw audio data (pcm) */
            file = new File(FILE_DIR, "example.pcm");
            outputStream = new DataOutputStream(new FileOutputStream(file));
            outputStream.write(rawBuffer.array(), 0, rawBuffer.position());
            outputStream.close();

            /* save wave file (wave header + pcm) */
            file = new File(FILE_DIR, "example.wav");
            outputStream = new DataOutputStream(new FileOutputStream(file));
            WaveHeaderWriter.rawToWave(1, recorder.getCurrentRate(), 16, rawBuffer, outputStream);
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        recorder.clearBuffer();
    }
}

@@ -53,17 +53,179 @@
 oid.
-os.Bundle
+Manifest;%0Aimport android.content.pm.PackageManager;%0Aimport android.os.Bundle;%0Aimport android.support.v4.app.ActivityCompat;%0Aimport android.support.v4.content.ContextCompat
 ;%0Aim
@@ -563,16 +563,71 @@
 vity %7B%0A%0A
+    private final static int PERMISSIONS_REQUESTS = 1;%0A
     priv
@@ -843,24 +843,879 @@
 ity_main);%0A%0A
+        checkPermissions();%0A    %7D%0A%0A    private void checkPermissions() %7B%0A        int permissionCheck;%0A        permissionCheck = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.RECORD_AUDIO);%0A        if (permissionCheck == PackageManager.PERMISSION_DENIED) %7B%0A            ActivityCompat.requestPermissions(this, new String%5B%5D%7BManifest.permission.RECORD_AUDIO%7D, PERMISSIONS_REQUESTS);%0A            return;%0A        %7D%0A%0A        permissionCheck = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE);%0A        if (permissionCheck == PackageManager.PERMISSION_DENIED) %7B%0A            ActivityCompat.requestPermissions(this, new String%5B%5D%7BManifest.permission.WRITE_EXTERNAL_STORAGE%7D, PERMISSIONS_REQUESTS);%0A            return;%0A        %7D%0A%0A        setup();%0A    %7D%0A%0A    private void setup() %7B%0A
         FILE
@@ -3226,24 +3226,560 @@
 );%0A        %7D
+%0A%0A        recorder.resume();%0A    %7D%0A%0A    @Override%0A    public void onRequestPermissionsResult(int requestCode,%0A                                           String%5B%5D permissions, int%5B%5D grantResults) %7B%0A        super.onRequestPermissionsResult(requestCode, permissions, grantResults);%0A%0A        if (requestCode == PERMISSIONS_REQUESTS) %7B%0A            if (grantResults.length %3E 0 && grantResults%5B0%5D == PackageManager.PERMISSION_GRANTED) %7B%0A                checkPermissions();%0A            %7D else %7B%0A                finish();%0A            %7D%0A        %7D
 %0A    %7D%0A%0A    
@@ -3842,24 +3842,58 @@
 onResume();%0A
+        if (recorder != null)%0A    
         reco
@@ -3899,32 +3899,32 @@
 order.resume();%0A
-
     %7D%0A%0A    @Over
@@ -3984,24 +3984,58 @@
 nDestroy();%0A
+        if (recorder != null)%0A    
         reco

add permission checkings