diff --git a/apps/OboeTester/app/src/main/AndroidManifest.xml b/apps/OboeTester/app/src/main/AndroidManifest.xml index dccd991f1..142993db2 100644 --- a/apps/OboeTester/app/src/main/AndroidManifest.xml +++ b/apps/OboeTester/app/src/main/AndroidManifest.xml @@ -21,6 +21,9 @@ + + + + + + = Build.VERSION_CODES.O) { + manager.createNotificationChannel(new NotificationChannel( + "all", + "All Notifications", + NotificationManager.IMPORTANCE_NONE)); + + return new Notification.Builder(this, "all") + .setContentTitle("Playing/recording audio") + .setContentText("playing/recording...") + .setSmallIcon(R.drawable.ic_notification) + .build(); + } + return null; + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + Log.i(TAG, "Receive onStartCommand" + intent); + switch (intent.getAction()) { + case ACTION_START: + Log.i(TAG, "Receive ACTION_START" + intent.getExtras()); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + startForeground(1, buildNotification(), + ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK + | ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE); + } + break; + case ACTION_STOP: + Log.i(TAG, "Receive ACTION_STOP" + intent.getExtras()); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + stopForeground(STOP_FOREGROUND_REMOVE); + } + break; + } + return START_NOT_STICKY; + } +} diff --git a/apps/OboeTester/app/src/main/java/com/mobileer/oboetester/IntentBasedTestSupport.java b/apps/OboeTester/app/src/main/java/com/mobileer/oboetester/IntentBasedTestSupport.java index 3d54ade89..5f2d00e76 100644 --- a/apps/OboeTester/app/src/main/java/com/mobileer/oboetester/IntentBasedTestSupport.java +++ b/apps/OboeTester/app/src/main/java/com/mobileer/oboetester/IntentBasedTestSupport.java @@ -60,6 +60,7 @@ public class IntentBasedTestSupport { public static final String KEY_FILE_NAME = "file"; public static final String KEY_BUFFER_BURSTS = "buffer_bursts"; public static final String KEY_BACKGROUND = "background"; + public static final String KEY_FOREGROUND_SERVICE = "foreground_service"; public static final String KEY_VOLUME = "volume"; public static final String KEY_VOLUME_TYPE = "volume_type"; diff --git a/apps/OboeTester/app/src/main/java/com/mobileer/oboetester/MainActivity.java b/apps/OboeTester/app/src/main/java/com/mobileer/oboetester/MainActivity.java index a5ec76ef4..f11a3bc69 100644 --- a/apps/OboeTester/app/src/main/java/com/mobileer/oboetester/MainActivity.java +++ b/apps/OboeTester/app/src/main/java/com/mobileer/oboetester/MainActivity.java @@ -60,6 +60,7 @@ public class MainActivity extends BaseOboeTesterActivity { private Bundle mBundleFromIntent; private CheckBox mWorkaroundsCheckBox; private CheckBox mBackgroundCheckBox; + private CheckBox mForegroundServiceCheckBox; private static String mVersionText; @Override @@ -111,6 +112,7 @@ public void onNothingSelected(AdapterView adapterView) { NativeEngine.setWorkaroundsEnabled(false); mBackgroundCheckBox = (CheckBox) findViewById(R.id.boxEnableBackground); + mForegroundServiceCheckBox = (CheckBox) findViewById(R.id.boxEnableForegroundService); mBuildTextView = (TextView) findViewById(R.id.text_build_info); mBuildTextView.setText(Build.DISPLAY @@ -149,16 +151,19 @@ private void processBundleFromIntent() { } Intent intent = getTestIntent(mBundleFromIntent); if (intent != null) { - setBackgroundFromIntent(); + setTogglesFromIntent(); startActivity(intent); } mBundleFromIntent = null; } - private void setBackgroundFromIntent() { + private void setTogglesFromIntent() { boolean backgroundEnabled = mBundleFromIntent.getBoolean( IntentBasedTestSupport.KEY_BACKGROUND, false); TestAudioActivity.setBackgroundEnabled(backgroundEnabled); + boolean foregroundServiceEnabled = mBundleFromIntent.getBoolean( + IntentBasedTestSupport.KEY_FOREGROUND_SERVICE, false); + TestAudioActivity.setBackgroundEnabled(foregroundServiceEnabled); } private Intent getTestIntent(Bundle bundle) { @@ -255,6 +260,7 @@ private void applyUserOptions() { NativeEngine.setWorkaroundsEnabled(mWorkaroundsCheckBox.isChecked()); TestAudioActivity.setBackgroundEnabled(mBackgroundCheckBox.isChecked()); + TestAudioActivity.setForegroundServiceEnabled(mForegroundServiceCheckBox.isChecked()); } @Override diff --git a/apps/OboeTester/app/src/main/java/com/mobileer/oboetester/TestAudioActivity.java b/apps/OboeTester/app/src/main/java/com/mobileer/oboetester/TestAudioActivity.java index 987210123..677bff0b7 100644 --- a/apps/OboeTester/app/src/main/java/com/mobileer/oboetester/TestAudioActivity.java +++ b/apps/OboeTester/app/src/main/java/com/mobileer/oboetester/TestAudioActivity.java @@ -16,6 +16,9 @@ package com.mobileer.oboetester; +import static com.mobileer.oboetester.AudioForegroundService.ACTION_START; +import static com.mobileer.oboetester.AudioForegroundService.ACTION_STOP; + import android.content.Context; import android.content.Intent; import android.content.pm.PackageInfo; @@ -97,6 +100,7 @@ abstract class TestAudioActivity extends AppCompatActivity { private int mSampleRate; private int mSingleTestIndex = -1; private static boolean mBackgroundEnabled; + private static boolean mForegroundServiceEnabled; protected Bundle mBundleFromIntent; protected boolean mTestRunningByIntent; @@ -183,6 +187,14 @@ public static boolean isBackgroundEnabled() { return mBackgroundEnabled; } + public static void setForegroundServiceEnabled(boolean enabled) { + mForegroundServiceEnabled = enabled; + } + + public static boolean isForegroundServiceEnabled() { + return mForegroundServiceEnabled; + } + public void onStreamClosed() { } @@ -237,6 +249,9 @@ protected void onStart() { if (mCommunicationDeviceView != null) { mCommunicationDeviceView.onStart(); } + if (isForegroundServiceEnabled()) { + enableForegroundService(true); + } } protected void resetConfiguration() { @@ -300,6 +315,9 @@ protected void onStop() { if (!isBackgroundEnabled()) { Log.i(TAG, "onStop() called so stop the test ========================="); onStopTest(); + if (isForegroundServiceEnabled()) { + enableForegroundService(false); + } } if (mCommunicationDeviceView != null) { mCommunicationDeviceView.onStop(); @@ -312,11 +330,23 @@ protected void onDestroy() { if (isBackgroundEnabled()) { Log.i(TAG, "onDestroy() called so stop the test ========================="); onStopTest(); + if (isForegroundServiceEnabled()) { + enableForegroundService(false); + } } mAudioState = AUDIO_STATE_CLOSED; super.onDestroy(); } + public void enableForegroundService(boolean enabled) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + String action = enabled ? ACTION_START : ACTION_STOP; + Intent serviceIntent = new Intent(action, null, this, + AudioForegroundService.class); + startForegroundService(serviceIntent); + } + } + protected void updateEnabledWidgets() { if (mOpenButton != null) { mOpenButton.setBackgroundColor(mAudioState == AUDIO_STATE_OPEN ? COLOR_ACTIVE : COLOR_IDLE); diff --git a/apps/OboeTester/app/src/main/res/drawable/ic_notification.xml b/apps/OboeTester/app/src/main/res/drawable/ic_notification.xml new file mode 100644 index 000000000..61f9d9c3b --- /dev/null +++ b/apps/OboeTester/app/src/main/res/drawable/ic_notification.xml @@ -0,0 +1,83 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/apps/OboeTester/app/src/main/res/layout/activity_main.xml b/apps/OboeTester/app/src/main/res/layout/activity_main.xml index b764815ec..0d38bf41c 100644 --- a/apps/OboeTester/app/src/main/res/layout/activity_main.xml +++ b/apps/OboeTester/app/src/main/res/layout/activity_main.xml @@ -203,13 +203,23 @@ app:layout_constraintTop_toBottomOf="@+id/boxEnableWorkarounds" /> + + + app:layout_constraintStart_toStartOf="@+id/boxEnableForegroundService" /> + app:layout_constraintTop_toBottomOf="@+id/boxEnableForegroundService" />