Skip to content

Commit

Permalink
add permission check util
Browse files Browse the repository at this point in the history
  • Loading branch information
aquamarine5 committed Apr 9, 2022
1 parent 641ee7d commit 3c4b8a2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.aquaapps.readtorecite;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
Expand All @@ -20,7 +20,6 @@
import androidx.camera.video.VideoCapture;
import androidx.camera.video.VideoRecordEvent;
import androidx.camera.view.PreviewView;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.util.Consumer;

Expand Down Expand Up @@ -68,10 +67,12 @@ public boolean isRecording() {
return recording != null;
}


@SuppressLint("MissingPermission")
public void startRecord(Consumer<VideoRecordEvent> videoRecordListener) {
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DISPLAY_NAME,
"ReadToRecite-" + new SimpleDateFormat("yyyyMMdd-HHmmss", Locale.ENGLISH).format(new Date())+".3gp");
"ReadToRecite-" + new SimpleDateFormat("yyyyMMdd-HHmmss", Locale.ENGLISH).format(new Date()) + ".3gp");
Uri videoCollection;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
videoCollection = MediaStore.Video.Media
Expand All @@ -84,9 +85,11 @@ public void startRecord(Consumer<VideoRecordEvent> videoRecordListener) {
.setContentValues(values)
.build();

if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.RECORD_AUDIO}, 114514);
}
PermissionController.requestPermissions(activity, new String[]{
Manifest.permission.RECORD_AUDIO,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA
});

recording = videoCapture.getOutput()
.prepareRecording(activity, options)
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/com/aquaapps/readtorecite/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.camera.video.OutputResults;
Expand Down Expand Up @@ -61,6 +62,12 @@ public boolean onCreateOptionsMenu(Menu menu) {
return true;
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
PermissionController.onRequestPermissionsResult(requestCode,permissions,grantResults);
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.aquaapps.readtorecite;

import android.content.pm.PackageManager;

import androidx.annotation.CheckResult;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresPermission;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.PermissionChecker;

public class PermissionController {
public static void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults){
// TODO:
}
public static void requestPermissions(AppCompatActivity activity,@NonNull String[] permissions){
for (String permission:
permissions) {
requestPermission(activity,permission);
}
}
public static void requestPermission(AppCompatActivity activity,String permission){
if(ActivityCompat.checkSelfPermission(activity,permission)== PackageManager.PERMISSION_DENIED &&
ActivityCompat.shouldShowRequestPermissionRationale(activity,permission)){
ActivityCompat.requestPermissions(activity,new String[]{permission},114514);
}
}
}

0 comments on commit 3c4b8a2

Please sign in to comment.