content: package com.devlomi.recordview;
  
  import static androidx.core.content.PermissionChecker.PERMISSION_GRANTED;
  
  import android.Manifest;
  import android.graphics.Color;
  import android.os.Build;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.View;
  import android.widget.Button;
  import android.widget.Toast;
  
  import androidx.appcompat.app.AppCompatActivity;
  import androidx.core.app.ActivityCompat;
  import androidx.core.content.ContextCompat;
  
  import com.devlomi.record_view.OnBasketAnimationEnd;
  import com.devlomi.record_view.OnRecordClickListener;
  import com.devlomi.record_view.OnRecordListener;
  import com.devlomi.record_view.RecordButton;
  import com.devlomi.record_view.RecordPermissionHandler;
  import com.devlomi.record_view.RecordView;
  
  import java.io.File;
  import java.io.IOException;
  import java.util.UUID;
  import java.util.concurrent.TimeUnit;
  
  public class MainActivity extends AppCompatActivity {
  
      private AudioRecorder audioRecorder;
      private File recordFile;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          audioRecorder = new AudioRecorder();
  
          RecordView recordView = findViewById(R.id.record_view);
          final RecordButton recordButton = findViewById(R.id.record_button);
          Button btnChangeOnclick = findViewById(R.id.btn_change_onclick);
  
          // To Enable Record Lock
  //        recordView.setLockEnabled(true);
  //        recordView.setRecordLockImageView(findViewById(R.id.record_lock));
          //IMPORTANT
          recordButton.setRecordView(recordView);
  
          // if you want to click the button (in case if you want to make the record button a Send Button for example..)
  //        recordButton.setListenForRecord(false);
  
          btnChangeOnclick.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
                  if (recordButton.isListenForRecord()) {
                      recordButton.setListenForRecord(false);
                      Toast.makeText(MainActivity.this, "onClickEnabled", Toast.LENGTH_SHORT).show();
                  } else {
                      recordButton.setListenForRecord(true);
                      Toast.makeText(MainActivity.this, "onClickDisabled", Toast.LENGTH_SHORT).show();
                  }
              }
          });
  
          //ListenForRecord must be false ,otherwise onClick will not be called
          recordButton.setOnRecordClickListener(new OnRecordClickListener() {
              @Override
              public void onClick(View v) {
                  Toast.makeText(MainActivity.this, "RECORD BUTTON CLICKED", Toast.LENGTH_SHORT).show();
                  Log.d("RecordButton", "RECORD BUTTON CLICKED");
              }
          });
  
  
          //Cancel Bounds is when the Slide To Cancel text gets before the timer . default is 8
          recordView.setCancelBounds(8);
  
  
          recordView.setSmallMicColor(Color.parseColor("#c2185b"));
  
          //prevent recording under one Second
          recordView.setLessThanSecondAllowed(false);
  
  
          recordView.setSlideToCancelText("Slide To Cancel");
  
  
          recordView.setCustomSounds(R.raw.record_start, R.raw.record_finished, 0);
  
  
          recordView.setOnRecordListener(new OnRecordListener() {
              @Override
              public void onStart() {
                  recordFile = new File(getFilesDir(), UUID.randomUUID().toString() + ".3gp");
                  try {
                      audioRecorder.start(recordFile.getPath());
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
                  Log.d("RecordView", "onStart");
                  Toast.makeText(MainActivity.this, "OnStartRecord", Toast.LENGTH_SHORT).show();
  
              }
  
              @Override
              public void onCancel() {
                  stopRecording(true);
  
                  Toast.makeText(MainActivity.this, "onCancel", Toast.LENGTH_SHORT).show();
  
                  Log.d("RecordView", "onCancel");
  
              }
  
              @Override
              public void onFinish(long recordTime, boolean limitReached) {
                  stopRecording(false);
  
  
                  String time = getHumanTimeText(recordTime);
                  Toast.makeText(MainActivity.this, "onFinishRecord - Recorded Time is: " + time + " File saved at " + recordFile.getPath(), Toast.LENGTH_SHORT).show();
                  Log.d("RecordView", "onFinish" + " Limit Reached? " + limitReached);
                  Log.d("RecordTime", time);
              }
  
              @Override
              public void onLessThanSecond() {
                  stopRecording(true);
  
                  Toast.makeText(MainActivity.this, "OnLessThanSecond", Toast.LENGTH_SHORT).show();
                  Log.d("RecordView", "onLessThanSecond");
              }
  
              @Override
              public void onLock() {
                  Toast.makeText(MainActivity.this, "onLock", Toast.LENGTH_SHORT).show();
                  Log.d("RecordView", "onLock");
              }
          });
  
  
          recordView.setOnBasketAnimationEndListener(new OnBasketAnimationEnd() {
              @Override
              public void onAnimationEnd() {
                  Log.d("RecordView", "Basket Animation Finished");
              }
          });
  
          recordView.setRecordPermissionHandler(new RecordPermissionHandler() {
              @Override
              public boolean isPermissionGranted() {
                  if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
                      return true;
                  }
  
                  boolean recordPermissionAvailable = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.RECORD_AUDIO) == PERMISSION_GRANTED;
                  if (recordPermissionAvailable) {
                      return true;
                  }
  
  
                  ActivityCompat.
                          requestPermissions(MainActivity.this,
                                  new String[]{Manifest.permission.RECORD_AUDIO},
                                  0);
  
                  return false;
  
              }
          });
  
  
      }
  
      private void stopRecording(boolean deleteFile) {
          audioRecorder.stop();
          if (recordFile != null && deleteFile) {
              recordFile.delete();
          }
      }
  
  
      private String getHumanTimeText(long milliseconds) {
          return String.format("%02d:%02d",
                  TimeUnit.MILLISECONDS.toMinutes(milliseconds),
                  TimeUnit.MILLISECONDS.toSeconds(milliseconds) -
                          TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(milliseconds)));
      }
  
  
  }
  