Skip to content

Commit

Permalink
Merge pull request #5 from Doist/carrie/fix-service-manager-crash
Browse files Browse the repository at this point in the history
Fix service manager crash
  • Loading branch information
carriehall authored May 2, 2024
2 parents 75182eb + a717977 commit 3d51954
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
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;
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

0 comments on commit 3d51954

Please sign in to comment.