From 833420f9afa9dd5b3e0206ace68cfd408b2f624b Mon Sep 17 00:00:00 2001 From: Carrie Hall Date: Wed, 17 Apr 2024 11:47:32 +0100 Subject: [PATCH 1/2] Fix ServiceManager crash --- .../service/manager/ServiceManager.java | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/AudioPlayerRecorderLibrary/src/main/java/com/heavyplayer/audioplayerrecorder/service/manager/ServiceManager.java b/AudioPlayerRecorderLibrary/src/main/java/com/heavyplayer/audioplayerrecorder/service/manager/ServiceManager.java index d44bcc2..d83b20a 100644 --- a/AudioPlayerRecorderLibrary/src/main/java/com/heavyplayer/audioplayerrecorder/service/manager/ServiceManager.java +++ b/AudioPlayerRecorderLibrary/src/main/java/com/heavyplayer/audioplayerrecorder/service/manager/ServiceManager.java @@ -1,8 +1,7 @@ package com.heavyplayer.audioplayerrecorder.service.manager; -import com.heavyplayer.audioplayerrecorder.BuildConfig; - import android.app.Activity; +import android.app.ActivityManager; import android.app.Service; import android.content.ComponentName; import android.content.Context; @@ -11,6 +10,10 @@ import android.os.IBinder; import android.util.Log; +import com.heavyplayer.audioplayerrecorder.BuildConfig; + +import java.util.List; + public class ServiceManager implements ServiceConnection { private IBinder mBinder; @@ -37,9 +40,16 @@ final public void onStop() { protected void onActivateService() { bindService(); - // Ensure service keeps running until we explicitly stop it, - // which is always except when configuration changes occur. - startService(); + ActivityManager activityManager = (ActivityManager) mActivity.getSystemService(Context.ACTIVITY_SERVICE); + List runningAppProcesses = activityManager.getRunningAppProcesses(); + if (runningAppProcesses != null) { + int importance = runningAppProcesses.get(0).importance; + if (importance <= ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { + // Only start the service if we are actually in the foreground. + // https://issuetracker.google.com/issues/110237673 + startService(); + } + } } protected void onDeactivateService(boolean stopService) { From a7179772071f212c12f7a27368608ec0bbf5c9c2 Mon Sep 17 00:00:00 2001 From: Carrie Hall Date: Thu, 2 May 2024 10:59:01 +0100 Subject: [PATCH 2/2] Add callbacks --- .../fragment/AudioRecorderFragment.java | 10 ++++++++++ .../service/manager/ServiceManager.java | 14 ++++++++++++++ .../sample/activity/PlayerActivity.java | 8 ++++++++ 3 files changed, 32 insertions(+) diff --git a/AudioPlayerRecorderLibrary/src/main/java/com/heavyplayer/audioplayerrecorder/fragment/AudioRecorderFragment.java b/AudioPlayerRecorderLibrary/src/main/java/com/heavyplayer/audioplayerrecorder/fragment/AudioRecorderFragment.java index 3b0102d..51fb898 100644 --- a/AudioPlayerRecorderLibrary/src/main/java/com/heavyplayer/audioplayerrecorder/fragment/AudioRecorderFragment.java +++ b/AudioPlayerRecorderLibrary/src/main/java/com/heavyplayer/audioplayerrecorder/fragment/AudioRecorderFragment.java @@ -138,6 +138,16 @@ public void onServiceStop() { // Purposely empty. } + @Override + public void onServiceStart() { + // Purposely empty. + } + + @Override + public void onServiceFailedToStart(String processName, int importance) { + // Purposely empty. + } + @Override public void onStartRecorder() { Activity activity = getActivity(); diff --git a/AudioPlayerRecorderLibrary/src/main/java/com/heavyplayer/audioplayerrecorder/service/manager/ServiceManager.java b/AudioPlayerRecorderLibrary/src/main/java/com/heavyplayer/audioplayerrecorder/service/manager/ServiceManager.java index d83b20a..39e51af 100644 --- a/AudioPlayerRecorderLibrary/src/main/java/com/heavyplayer/audioplayerrecorder/service/manager/ServiceManager.java +++ b/AudioPlayerRecorderLibrary/src/main/java/com/heavyplayer/audioplayerrecorder/service/manager/ServiceManager.java @@ -48,6 +48,13 @@ protected void onActivateService() { // Only start the service if we are actually in the foreground. // https://issuetracker.google.com/issues/110237673 startService(); + } else { + if (mStateListener != null) { + mStateListener.onServiceFailedToStart( + runningAppProcesses.get(0).processName, + importance + ); + } } } } @@ -61,6 +68,9 @@ protected void onDeactivateService(boolean stopService) { } protected void startService() { + if (mStateListener != null) { + mStateListener.onServiceStart(); + } mActivity.startService(new Intent(mActivity, mServiceClass)); } @@ -133,5 +143,9 @@ public interface StateListener { void onServiceUnbind(IBinder binder); void onServiceStop(); + + void onServiceStart(); + + void onServiceFailedToStart(String processName, int importance); } } diff --git a/AudioPlayerRecorderSamples/src/main/java/com/heavyplayer/audioplayerrecorder/sample/activity/PlayerActivity.java b/AudioPlayerRecorderSamples/src/main/java/com/heavyplayer/audioplayerrecorder/sample/activity/PlayerActivity.java index 9ee4670..dd1f054 100644 --- a/AudioPlayerRecorderSamples/src/main/java/com/heavyplayer/audioplayerrecorder/sample/activity/PlayerActivity.java +++ b/AudioPlayerRecorderSamples/src/main/java/com/heavyplayer/audioplayerrecorder/sample/activity/PlayerActivity.java @@ -47,6 +47,14 @@ public void onServiceUnbind(IBinder binder) { @Override public void onServiceStop() { } + + @Override + public void onServiceStart() { + } + + @Override + public void onServiceFailedToStart(String processName, int importance) { + } }); }