From 0f17e3f2d2622863cf709d5c9addbf84e2960b91 Mon Sep 17 00:00:00 2001 From: Robert Wu Date: Wed, 4 Sep 2024 13:44:25 -0700 Subject: [PATCH 1/3] LiveEffect: Request permissions before starting foreground service --- .../java/com/google/oboe/samples/liveEffect/MainActivity.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/samples/LiveEffect/src/main/java/com/google/oboe/samples/liveEffect/MainActivity.java b/samples/LiveEffect/src/main/java/com/google/oboe/samples/liveEffect/MainActivity.java index ce4ac652c..40408dc93 100644 --- a/samples/LiveEffect/src/main/java/com/google/oboe/samples/liveEffect/MainActivity.java +++ b/samples/LiveEffect/src/main/java/com/google/oboe/samples/liveEffect/MainActivity.java @@ -130,6 +130,10 @@ public void onClick(View v) { LiveEffectEngine.setDefaultStreamValues(this); setVolumeControlStream(AudioManager.STREAM_MUSIC); + if (!isRecordPermissionGranted()){ + requestRecordPermission(); + } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { Intent serviceIntent = new Intent(ACTION_START, null, this, DuplexStreamForegroundService.class); From c8182b84098bae13f74dd7f6f42d2ba186c426bf Mon Sep 17 00:00:00 2001 From: Robert Wu Date: Thu, 5 Sep 2024 10:39:51 -0700 Subject: [PATCH 2/3] disable when permissions are not granted --- .../oboe/samples/liveEffect/MainActivity.java | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/samples/LiveEffect/src/main/java/com/google/oboe/samples/liveEffect/MainActivity.java b/samples/LiveEffect/src/main/java/com/google/oboe/samples/liveEffect/MainActivity.java index 40408dc93..95bde00ab 100644 --- a/samples/LiveEffect/src/main/java/com/google/oboe/samples/liveEffect/MainActivity.java +++ b/samples/LiveEffect/src/main/java/com/google/oboe/samples/liveEffect/MainActivity.java @@ -134,12 +134,6 @@ public void onClick(View v) { requestRecordPermission(); } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { - Intent serviceIntent = new Intent(ACTION_START, null, this, - DuplexStreamForegroundService.class); - startForegroundService(serviceIntent); - } - onStartTest(); } @@ -210,11 +204,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); @@ -290,9 +279,15 @@ 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. + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + Intent serviceIntent = new Intent(ACTION_START, null, this, + DuplexStreamForegroundService.class); + startForegroundService(serviceIntent); + } } } } From 261fa3f9521b021ec2432b2ddc71300ca0d5a1dc Mon Sep 17 00:00:00 2001 From: Robert Wu Date: Tue, 10 Sep 2024 16:36:12 -0700 Subject: [PATCH 3/3] add else for permissions already exist --- .../oboe/samples/liveEffect/MainActivity.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/samples/LiveEffect/src/main/java/com/google/oboe/samples/liveEffect/MainActivity.java b/samples/LiveEffect/src/main/java/com/google/oboe/samples/liveEffect/MainActivity.java index 95bde00ab..339fe9367 100644 --- a/samples/LiveEffect/src/main/java/com/google/oboe/samples/liveEffect/MainActivity.java +++ b/samples/LiveEffect/src/main/java/com/google/oboe/samples/liveEffect/MainActivity.java @@ -132,6 +132,8 @@ public void onClick(View v) { if (!isRecordPermissionGranted()){ requestRecordPermission(); + } else { + startForegroundService(); } onStartTest(); @@ -260,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) { @@ -283,11 +293,7 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis toggleEffectButton.setEnabled(false); } else { // Permission was granted, start foreground service. - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { - Intent serviceIntent = new Intent(ACTION_START, null, this, - DuplexStreamForegroundService.class); - startForegroundService(serviceIntent); - } + startForegroundService(); } } }