Skip to content

Commit

Permalink
App crashes on Selfie video widget if microphone permissions are not …
Browse files Browse the repository at this point in the history
…set (getodk#2566)
  • Loading branch information
grzesiek2010 authored and lognaturel committed Sep 21, 2018
1 parent e3c901d commit dc0d08b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.odk.collect.android.fragments.Camera2VideoFragment;
import org.odk.collect.android.utilities.ToastUtils;

import static org.odk.collect.android.utilities.PermissionUtils.checkIfCameraPermissionGranted;
import static org.odk.collect.android.utilities.PermissionUtils.checkIfCameraAndRecordAudioPermissionsGranted;

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class CaptureSelfieVideoActivityNewApi extends Activity {
Expand All @@ -35,7 +35,7 @@ public class CaptureSelfieVideoActivityNewApi extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

if (!checkIfCameraPermissionGranted(this)) {
if (!checkIfCameraAndRecordAudioPermissionsGranted(this)) {
finish();
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,39 @@ public void onPermissionRationaleShouldBeShown(PermissionRequest permission, Per
.check();
}

public static void requestCameraAndRecordAudioPermissions(@NonNull Activity activity, @NonNull PermissionListener action) {
MultiplePermissionsListener multiplePermissionsListener = new MultiplePermissionsListener() {
@Override
public void onPermissionsChecked(MultiplePermissionsReport report) {
if (report.areAllPermissionsGranted()) {
action.granted();
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(activity, R.style.Theme_AppCompat_Light_Dialog);

builder.setTitle(R.string.camera_runtime_permission_denied_title)
.setMessage(R.string.camera_runtime_permission_denied_desc)
.setPositiveButton(android.R.string.ok, (dialogInterface, i) -> action.denied())
.setCancelable(false)
.setIcon(R.drawable.ic_photo_camera)
.show();
}
}

@Override
public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {
token.continuePermissionRequest();
}
};

Dexter.withActivity(activity)
.withPermissions(
Manifest.permission.CAMERA,
Manifest.permission.RECORD_AUDIO
).withListener(multiplePermissionsListener)
.withErrorListener(error -> Timber.i(error.name()))
.check();
}

public static void requestGetAccountsPermission(@NonNull Activity activity, @NonNull PermissionListener action) {
com.karumi.dexter.listener.single.PermissionListener permissionListener = new com.karumi.dexter.listener.single.PermissionListener() {
@Override
Expand Down Expand Up @@ -312,6 +345,14 @@ public static boolean checkIfLocationPermissionsGranted(Context context) {
&& accessCoarseLocation == PackageManager.PERMISSION_GRANTED;
}

public static boolean checkIfCameraAndRecordAudioPermissionsGranted(Context context) {
int cameraPermission = ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA);
int recordAudioPermission = ContextCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO);

return cameraPermission == PackageManager.PERMISSION_GRANTED
&& recordAudioPermission == PackageManager.PERMISSION_GRANTED;
}

public static boolean checkIfGetAccountsPermissionGranted(Context context) {
return ContextCompat.checkSelfPermission(context, Manifest.permission.GET_ACCOUNTS) == PackageManager.PERMISSION_GRANTED;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@

import static android.os.Build.MODEL;
import static org.odk.collect.android.utilities.ApplicationConstants.RequestCodes;
import static org.odk.collect.android.utilities.PermissionUtils.requestCameraAndRecordAudioPermissions;
import static org.odk.collect.android.utilities.PermissionUtils.requestCameraPermission;

/**
Expand Down Expand Up @@ -321,16 +322,29 @@ public void cancelLongPress() {
public void onButtonClick(int id) {
switch (id) {
case R.id.capture_video:
requestCameraPermission((FormEntryActivity) getContext(), new PermissionListener() {
@Override
public void granted() {
captureVideo();
}

@Override
public void denied() {
}
});
if (selfie) {
requestCameraAndRecordAudioPermissions((FormEntryActivity) getContext(), new PermissionListener() {
@Override
public void granted() {
captureVideo();
}

@Override
public void denied() {
}
});
} else {
requestCameraPermission((FormEntryActivity) getContext(), new PermissionListener() {
@Override
public void granted() {
captureVideo();
}

@Override
public void denied() {
}
});
}
break;
case R.id.choose_video:
chooseVideo();
Expand Down

0 comments on commit dc0d08b

Please sign in to comment.