Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix service manager crash #5

Merged
merged 2 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -37,9 +40,23 @@ 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<ActivityManager.RunningAppProcessInfo> runningAppProcesses = activityManager.getRunningAppProcesses();
if (runningAppProcesses != null) {
int importance = runningAppProcesses.get(0).importance;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we find our process or are we certain it's always the first one?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be the first one as that's the foreground one. I've added that in the failure callback though so we can log it, just in case there's something else amiss.

if (importance <= ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
// 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
);
}
}
}
}

protected void onDeactivateService(boolean stopService) {
Expand All @@ -51,6 +68,9 @@ protected void onDeactivateService(boolean stopService) {
}

protected void startService() {
if (mStateListener != null) {
mStateListener.onServiceStart();
}
mActivity.startService(new Intent(mActivity, mServiceClass));
}

Expand Down Expand Up @@ -123,5 +143,9 @@ public interface StateListener {
void onServiceUnbind(IBinder binder);

void onServiceStop();

void onServiceStart();

void onServiceFailedToStart(String processName, int importance);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
}
});
}

Expand Down
Loading