From 05cfe09cc74e0fd206e2e317fac6cf652129db17 Mon Sep 17 00:00:00 2001 From: Colin Liang Date: Wed, 29 May 2024 08:14:31 -0700 Subject: [PATCH] Remove MediaPlaybackService (#3324) b/342254474 b/282813379 Change-Id: I4ae8c67b51d3fb7ef1efc8dce8600af2300824d5 Co-authored-by: Colin Liang --- starboard/android/apk/apk_sources.gni | 1 - .../apk/app/src/app/AndroidManifest.xml | 5 - .../dev/cobalt/coat/MediaPlaybackService.java | 155 ------------------ .../java/dev/cobalt/coat/StarboardBridge.java | 46 ------ starboard/android/shared/android_main.cc | 8 - .../android/shared/application_android.cc | 10 -- .../android/shared/application_android.h | 4 - 7 files changed, 229 deletions(-) delete mode 100644 starboard/android/apk/app/src/main/java/dev/cobalt/coat/MediaPlaybackService.java diff --git a/starboard/android/apk/apk_sources.gni b/starboard/android/apk/apk_sources.gni index 7f85c40f50df..29a454cbfd9f 100644 --- a/starboard/android/apk/apk_sources.gni +++ b/starboard/android/apk/apk_sources.gni @@ -30,7 +30,6 @@ apk_sources = [ "//starboard/android/apk/app/src/main/java/dev/cobalt/coat/CobaltTextToSpeechHelper.java", "//starboard/android/apk/app/src/main/java/dev/cobalt/coat/CrashContextUpdateHandler.java", "//starboard/android/apk/app/src/main/java/dev/cobalt/coat/ErrorDialog.java", - "//starboard/android/apk/app/src/main/java/dev/cobalt/coat/MediaPlaybackService.java", "//starboard/android/apk/app/src/main/java/dev/cobalt/coat/NetworkStatus.java", "//starboard/android/apk/app/src/main/java/dev/cobalt/coat/NullCobaltFactory.java", "//starboard/android/apk/app/src/main/java/dev/cobalt/coat/PlatformError.java", diff --git a/starboard/android/apk/app/src/app/AndroidManifest.xml b/starboard/android/apk/app/src/app/AndroidManifest.xml index 279494d88773..4c17fd1910a7 100644 --- a/starboard/android/apk/app/src/app/AndroidManifest.xml +++ b/starboard/android/apk/app/src/app/AndroidManifest.xml @@ -72,11 +72,6 @@ - - diff --git a/starboard/android/apk/app/src/main/java/dev/cobalt/coat/MediaPlaybackService.java b/starboard/android/apk/app/src/main/java/dev/cobalt/coat/MediaPlaybackService.java deleted file mode 100644 index 8f7742bf9f99..000000000000 --- a/starboard/android/apk/app/src/main/java/dev/cobalt/coat/MediaPlaybackService.java +++ /dev/null @@ -1,155 +0,0 @@ -// Copyright 2020 The Cobalt Authors. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package dev.cobalt.coat; - -import static dev.cobalt.media.Log.TAG; - -import android.app.Notification; -import android.app.NotificationChannel; -import android.app.NotificationManager; -import android.app.Service; -import android.content.Context; -import android.content.Intent; -import android.content.pm.ServiceInfo; -import android.os.Build.VERSION; -import android.os.IBinder; -import android.os.RemoteException; -import androidx.annotation.RequiresApi; -import androidx.core.app.NotificationCompat; -import dev.cobalt.util.Log; -import dev.cobalt.util.UsedByNative; - -/** Implementation of the MediaPlaybackService used for Background mode media playing. */ -public class MediaPlaybackService extends Service { - - private static final int NOTIFICATION_ID = 193266736; // CL number for uniqueness. - private static final String NOTIFICATION_CHANNEL_ID = "dev.cobalt.coat media playback service"; - private static final String NOTIFICATION_CHANNEL_NAME = "Media playback service"; - private boolean channelCreated = true; - private NotificationManager notificationManager = null; - - @Override - public void onCreate() { - Log.i(TAG, "Creating a Media playback foreground service."); - super.onCreate(); - if (getStarboardBridge() != null) { - getStarboardBridge().onServiceStart(this); - } - this.notificationManager = - (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); - this.channelCreated = createNotificationChannel(); - } - - @Override - public int onStartCommand(Intent intent, int flags, int startId) { - Log.i(TAG, "Cold start - Starting the service."); - startService(); - // It is better for background media playback service. - return START_STICKY; - } - - @Override - public IBinder onBind(Intent intent) { - // Do not support binding. - return null; - } - - @Override - public void onDestroy() { - Log.i(TAG, "Destroying the Media playback service."); - - if (VERSION.SDK_INT >= 26 && this.channelCreated) { - this.notificationManager.deleteNotificationChannel(NOTIFICATION_CHANNEL_ID); - } - - if (getStarboardBridge() != null) { - getStarboardBridge().onServiceDestroy(this); - } - super.onDestroy(); - } - - public void startService() { - if (this.channelCreated) { - try { - if (VERSION.SDK_INT >= 29) { - startForeground( - NOTIFICATION_ID, buildNotification(), ServiceInfo.FOREGROUND_SERVICE_TYPE_MANIFEST); - } else { - startForeground(NOTIFICATION_ID, buildNotification()); - } - } catch (IllegalStateException e) { - Log.e(TAG, "Failed to start Foreground Service", e); - } - } - } - - public void stopService() { - // Let service itself handle notification deletion. - if (this.channelCreated) { - stopForeground(true); - } - stopSelf(); - } - - private boolean createNotificationChannel() { - if (VERSION.SDK_INT >= 26) { - try { - createNotificationChannelInternalV26(); - } catch (RemoteException e) { - Log.e(TAG, "Failed to create Notification Channel.", e); - return false; - } - } - return true; - } - - @RequiresApi(26) - private void createNotificationChannelInternalV26() throws RemoteException { - NotificationChannel channel = - new NotificationChannel( - NOTIFICATION_CHANNEL_ID, - NOTIFICATION_CHANNEL_NAME, - notificationManager.IMPORTANCE_DEFAULT); - channel.setDescription("Channel for showing persistent notification"); - this.notificationManager.createNotificationChannel(channel); - } - - Notification buildNotification() { - String channelId = ""; - if (VERSION.SDK_INT >= 26) { - // Channel with ID=NOTIFICATION_CHANNEL_ID is created for version >= 26 - channelId = NOTIFICATION_CHANNEL_ID; - } - - NotificationCompat.Builder builder = - new NotificationCompat.Builder(this, channelId) - .setShowWhen(false) - .setPriority(NotificationCompat.PRIORITY_MIN) - .setSmallIcon(android.R.drawable.stat_sys_warning) - .setContentTitle("Media playback service") - .setContentText("Media playback service is running"); - - return builder.build(); - } - - @UsedByNative - protected StarboardBridge getStarboardBridge() { - if (getApplication() == null) { - Log.e(TAG, "Application already destroyed."); - return null; - } - return ((StarboardBridge.HostApplication) getApplication()).getStarboardBridge(); - } -} diff --git a/starboard/android/apk/app/src/main/java/dev/cobalt/coat/StarboardBridge.java b/starboard/android/apk/app/src/main/java/dev/cobalt/coat/StarboardBridge.java index d730b15d52a7..a6114598794f 100644 --- a/starboard/android/apk/app/src/main/java/dev/cobalt/coat/StarboardBridge.java +++ b/starboard/android/apk/app/src/main/java/dev/cobalt/coat/StarboardBridge.java @@ -31,7 +31,6 @@ import android.net.Network; import android.net.NetworkCapabilities; import android.os.Build; -import android.os.Build.VERSION; import android.util.Pair; import android.util.Size; import android.util.SizeF; @@ -184,48 +183,6 @@ protected void onServiceDestroy(Service service) { } } - @SuppressWarnings("unused") - @UsedByNative - protected void startMediaPlaybackService() { - if (cobaltMediaSession == null || !cobaltMediaSession.isActive()) { - Log.w(TAG, "Do not start a MediaPlaybackService when the MediSsession is null or inactive."); - return; - } - - Service service = serviceHolder.get(); - if (service == null) { - if (appContext == null) { - Log.w(TAG, "Activiy already destroyed."); - return; - } - Log.i(TAG, "Cold start - Instantiating a MediaPlaybackService."); - Intent intent = new Intent(appContext, MediaPlaybackService.class); - try { - if (VERSION.SDK_INT >= 26) { - appContext.startForegroundService(intent); - } else { - appContext.startService(intent); - } - } catch (SecurityException e) { - Log.e(TAG, "Failed to start MediaPlaybackService with intent.", e); - return; - } - } else { - Log.i(TAG, "Warm start - Restarting the MediaPlaybackService."); - ((MediaPlaybackService) service).startService(); - } - } - - @SuppressWarnings("unused") - @UsedByNative - protected void stopMediaPlaybackService() { - Service service = serviceHolder.get(); - if (service != null) { - Log.i(TAG, "Stopping the MediaPlaybackService."); - ((MediaPlaybackService) service).stopService(); - } - } - @SuppressWarnings("unused") @UsedByNative protected void beforeStartOrResume() { @@ -253,9 +210,6 @@ protected void beforeSuspend() { for (CobaltService service : cobaltServices.values()) { service.beforeSuspend(); } - // We need to stop MediaPlaybackService before suspending so that this foreground service - // would not prevent releasing activity's memory consumption. - stopMediaPlaybackService(); } catch (Throwable e) { Log.i(TAG, "Caught exception in beforeSuspend: " + e.getMessage()); } diff --git a/starboard/android/shared/android_main.cc b/starboard/android/shared/android_main.cc index e303604ac0b6..4de29227a1d9 100644 --- a/starboard/android/shared/android_main.cc +++ b/starboard/android/shared/android_main.cc @@ -270,20 +270,12 @@ void OnStart(GameActivity* activity) { void OnResume(GameActivity* activity) { if (g_app_running.load()) { - // Stop the MediaPlaybackService if activity state transits from background - // to foreground. Note that the MediaPlaybackService may already have - // been stopped before Cobalt's lifecycle state transits from Concealed - // to Frozen. - ApplicationAndroid::Get()->StopMediaPlaybackService(); ApplicationAndroid::Get()->SendAndroidCommand(AndroidCommand::kResume); } } void OnPause(GameActivity* activity) { if (g_app_running.load()) { - // Start the MediaPlaybackService before activity state transits from - // foreground to background. - ApplicationAndroid::Get()->StartMediaPlaybackService(); ApplicationAndroid::Get()->SendAndroidCommand(AndroidCommand::kPause); } } diff --git a/starboard/android/shared/application_android.cc b/starboard/android/shared/application_android.cc index ce2ae6965bcc..527fa0a7fa1a 100644 --- a/starboard/android/shared/application_android.cc +++ b/starboard/android/shared/application_android.cc @@ -257,16 +257,6 @@ void ApplicationAndroid::OnSuspend() { env->CallStarboardVoidMethodOrAbort("beforeSuspend", "()V"); } -void ApplicationAndroid::StartMediaPlaybackService() { - JniEnvExt* env = JniEnvExt::Get(); - env->CallStarboardVoidMethodOrAbort("startMediaPlaybackService", "()V"); -} - -void ApplicationAndroid::StopMediaPlaybackService() { - JniEnvExt* env = JniEnvExt::Get(); - env->CallStarboardVoidMethodOrAbort("stopMediaPlaybackService", "()V"); -} - void ApplicationAndroid::ProcessAndroidCommand() { JniEnvExt* env = JniEnvExt::Get(); AndroidCommand cmd; diff --git a/starboard/android/shared/application_android.h b/starboard/android/shared/application_android.h index 0b7d34cc3fac..1ceefd919350 100644 --- a/starboard/android/shared/application_android.h +++ b/starboard/android/shared/application_android.h @@ -107,10 +107,6 @@ class ApplicationAndroid std::string GetOverlayedStringValue(const char* var_name); bool GetOverlayedBoolValue(const char* var_name); - // Methods to start/stop Media playback service. - void StartMediaPlaybackService(); - void StopMediaPlaybackService(); - protected: // --- Application overrides --- void Initialize() override;