Skip to content

Commit

Permalink
LiveEffect: Request permissions before starting foreground service (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
robertwu1 authored Sep 18, 2024
1 parent b6a42bd commit 2de7552
Showing 1 changed file with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ public void onClick(View v) {
LiveEffectEngine.setDefaultStreamValues(this);
setVolumeControlStream(AudioManager.STREAM_MUSIC);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
Intent serviceIntent = new Intent(ACTION_START, null, this,
DuplexStreamForegroundService.class);
startForegroundService(serviceIntent);
if (!isRecordPermissionGranted()){
requestRecordPermission();
} else {
startForegroundService();
}

onStartTest();
Expand Down Expand Up @@ -206,11 +206,6 @@ public void toggleEffect() {
private void startEffect() {
Log.d(TAG, "Attempting to start");

if (!isRecordPermissionGranted()){
requestRecordPermission();
return;
}

boolean success = LiveEffectEngine.setEffectOn(true);
if (success) {
statusText.setText(R.string.status_playing);
Expand Down Expand Up @@ -267,6 +262,14 @@ private void resetStatusView() {
statusText.setText(R.string.status_warning);
}

private void startForegroundService() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
Intent serviceIntent = new Intent(ACTION_START, null, this,
DuplexStreamForegroundService.class);
startForegroundService(serviceIntent);
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
Expand All @@ -286,9 +289,11 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
getString(R.string.need_record_audio_permission),
Toast.LENGTH_SHORT)
.show();
EnableAudioApiUI(false);
toggleEffectButton.setEnabled(false);
} else {
// Permission was granted, start live effect
toggleEffect();
// Permission was granted, start foreground service.
startForegroundService();
}
}
}

0 comments on commit 2de7552

Please sign in to comment.