Commit e1e9372c authored by Sylvain Berfini's avatar Sylvain Berfini 🐮
Browse files

Ensure Service is started when a call is received or is initiated

parent 4db329c3
......@@ -20,6 +20,8 @@
package org.linphone.core.tools.service;
import android.app.Application;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
......@@ -145,6 +147,22 @@ public class CoreManager {
}
mListener = new CoreListenerStub() {
@Override
public void onFirstCallStarted(Core core) {
Log.i("[Core Manager] First call started");
// Ensure Service is running. It will take care by itself to start as foreground.
if (!isServiceRunning()) {
Log.w("[Core Manager] Service isn't running, let's start it");
try {
startService();
} catch (IllegalStateException ise) {
Log.w("[Core Manager] Failed to start service: ", ise);
}
} else {
Log.i("[Core Manager] Service appears to be running, everything is fine");
}
}
@Override
public void onLastCallEnded(Core core) {
Log.i("[Core Manager] Last call ended");
......@@ -187,8 +205,7 @@ public class CoreManager {
try {
mServiceClass = getServiceClass();
if (mServiceClass == null) mServiceClass = CoreService.class;
mContext.startService(new Intent().setClass(mContext, mServiceClass));
Log.i("[Core Manager] Starting service ", mServiceClass.getName());
//startService();
} catch (IllegalStateException ise) {
Log.w("[Core Manager] Failed to start service: ", ise);
// On Android > 8, if app in background, startService will trigger an IllegalStateException when called from background
......@@ -291,6 +308,21 @@ public class CoreManager {
return null;
}
private void startService() {
mContext.startService(new Intent().setClass(mContext, mServiceClass));
Log.i("[Core Manager] Starting service ", mServiceClass.getName());
}
private boolean isServiceRunning() {
ActivityManager manager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (mServiceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
private boolean isAndroidXMediaAvailable() {
boolean available = false;
try {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment