Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Add method handleStartServiceException to handle exceptions when tryi…
…ng to start routerService in foreground
  • Loading branch information
JulianKast committed Aug 15, 2022
commit 4f8e48376303cad3509061a4516a0367a969d735
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@

package com.smartdevicelink.transport;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.ForegroundServiceStartNotAllowedException;
import android.app.ServiceStartNotAllowedException;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
Expand Down Expand Up @@ -284,10 +284,8 @@ private static void startRouterService(Context context, ComponentName componentN
restart.putExtra(LOCAL_ROUTER_SERVICE_DID_START_OWN, true);
context.sendBroadcast(restart);

} catch (SecurityException e) {
DebugTool.logError(TAG, "Security exception, process is bad");
} catch (@SuppressLint({"NewApi", "LocalSuppress"}) ForegroundServiceStartNotAllowedException e) {
DebugTool.logError(TAG, "Not allowed to start service in Foreground");
} catch (SecurityException | IllegalStateException e) {
handleStartServiceException(e);
}
}

Expand Down Expand Up @@ -482,11 +480,8 @@ protected static void pingRouterService(Context context, String packageName, Str
} else {
context.startService(intent);
}
} catch (SecurityException e) {
DebugTool.logError(TAG, "Security exception, process is bad");
// This service could not be started
} catch (@SuppressLint({"NewApi", "LocalSuppress"}) ForegroundServiceStartNotAllowedException e) {
DebugTool.logError(TAG, "Not allowed to start service in Foreground");
} catch (SecurityException | IllegalStateException e) {
handleStartServiceException(e);
}
}

Expand Down Expand Up @@ -605,6 +600,22 @@ private static boolean isBluetoothConnected() {
return false;
}

private static void handleStartServiceException(Exception e) {
if (e instanceof SecurityException) {
DebugTool.logError(TAG, "Security exception, process is bad");
return;
} else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.R) {
if (e instanceof ForegroundServiceStartNotAllowedException) {
DebugTool.logError(TAG, "Not allowed to start service in foreground");
return;
} else if (e instanceof ServiceStartNotAllowedException) {
DebugTool.logError(TAG, "Not allowed to start service in current state");
return;
}
}
DebugTool.logError(TAG, "Unable to start service for unknown reason");
}


private static SdlDeviceListener getSdlDeviceListener(Context context, BluetoothDevice bluetoothDevice) {

Expand Down