Skip to content

Commit 71dc5f6

Browse files
committed
Add PendingIntents to start services from RS
1 parent e5bb2cf commit 71dc5f6

5 files changed

Lines changed: 33 additions & 6 deletions

File tree

android/hello_sdl_android/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959

6060
<service
6161
android:name="com.sdl.hellosdlandroid.SdlService"
62+
android:exported="true"
6263
android:foregroundServiceType="connectedDevice">
6364
</service>
6465
<service

android/hello_sdl_android/src/main/java/com/sdl/hellosdlandroid/SdlReceiver.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.sdl.hellosdlandroid;
22

3+
import android.app.PendingIntent;
34
import android.content.Context;
45
import android.content.Intent;
56
import android.os.Build;
67

78
import com.smartdevicelink.transport.SdlBroadcastReceiver;
89
import com.smartdevicelink.transport.SdlRouterService;
10+
import com.smartdevicelink.transport.TransportConstants;
911
import com.smartdevicelink.util.DebugTool;
1012

1113
public class SdlReceiver extends SdlBroadcastReceiver {
@@ -16,17 +18,24 @@ public void onSdlEnabled(Context context, Intent intent) {
1618
DebugTool.logInfo(TAG, "SDL Enabled");
1719
intent.setClass(context, SdlService.class);
1820

19-
// SdlService needs to be foregrounded in Android O and above
20-
// This will prevent apps in the background from crashing when they try to start SdlService
21-
// Because Android O doesn't allow background apps to start background services
22-
try {
21+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
22+
if (intent.getParcelableExtra(TransportConstants.PENDING_INTENT_EXTRA) != null) {
23+
PendingIntent pendingIntent = (PendingIntent) intent.getParcelableExtra(TransportConstants.PENDING_INTENT_EXTRA);
24+
try {
25+
pendingIntent.send(context, 0, intent);
26+
} catch (PendingIntent.CanceledException e) {
27+
e.printStackTrace();
28+
}
29+
}
30+
} else {
31+
// SdlService needs to be foregrounded in Android O and above
32+
// This will prevent apps in the background from crashing when they try to start SdlService
33+
// Because Android O doesn't allow background apps to start background services
2334
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
2435
context.startForegroundService(intent);
2536
} else {
2637
context.startService(intent);
2738
}
28-
} catch (Exception e) {
29-
DebugTool.logError("RHENIGAN", "Failed to start SdlService!");
3039
}
3140
}
3241

android/sdl_android/src/main/java/com/smartdevicelink/transport/SdlRouterService.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,6 +1826,10 @@ public void onTransportConnected(final TransportRecord record) {
18261826

18271827
startService.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
18281828

1829+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
1830+
startService.putExtra(TransportConstants.PENDING_BOOLEAN_EXTRA, true);
1831+
}
1832+
18291833
AndroidTools.sendExplicitBroadcast(getApplicationContext(), startService, null);
18301834

18311835
//HARDWARE_CONNECTED
@@ -2902,6 +2906,9 @@ private void initPingIntent() {
29022906
if (receivedVehicleType != null) {
29032907
pingIntent.putExtra(TransportConstants.VEHICLE_INFO_EXTRA, receivedVehicleType.getStore());
29042908
}
2909+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
2910+
pingIntent.putExtra(TransportConstants.PENDING_BOOLEAN_EXTRA, true);
2911+
}
29052912
}
29062913

29072914
private void startClientPings() {

android/sdl_android/src/main/java/com/smartdevicelink/util/AndroidTools.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
package com.smartdevicelink.util;
3434

35+
import android.app.PendingIntent;
3536
import android.content.ComponentName;
3637
import android.content.Context;
3738
import android.content.Intent;
@@ -249,6 +250,13 @@ public static void sendExplicitBroadcast(Context context, Intent intent, List<Re
249250
for (ResolveInfo app : apps) {
250251
try {
251252
intent.setClassName(app.activityInfo.applicationInfo.packageName, app.activityInfo.name);
253+
254+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && intent.getBooleanExtra(TransportConstants.PENDING_BOOLEAN_EXTRA, false)) {
255+
Intent pending = new Intent();
256+
PendingIntent pendingIntent = PendingIntent.getForegroundService(context, (int) System.currentTimeMillis(), pending, PendingIntent.FLAG_MUTABLE | Intent.FILL_IN_COMPONENT);
257+
intent.putExtra(TransportConstants.PENDING_INTENT_EXTRA, pendingIntent);
258+
}
259+
252260
context.sendBroadcast(intent);
253261
} catch (Exception e) {
254262
//In case there is missing info in the app reference we want to keep moving

base/src/main/java/com/smartdevicelink/transport/TransportConstants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public class TransportConstants {
4646
public static final String CONFIRMED_SDL_DEVICE = "confirmed_sdl_device";
4747
public static final String VEHICLE_INFO_EXTRA = "vehicle_info";
4848
public static final String CONNECTION_TYPE_EXTRA = "connection_type";
49+
public static final String PENDING_BOOLEAN_EXTRA = "pending_true";
50+
public static final String PENDING_INTENT_EXTRA = "pending_intent";
4951

5052
public static final String BIND_LOCATION_PACKAGE_NAME_EXTRA = "BIND_LOCATION_PACKAGE_NAME_EXTRA";
5153
public static final String BIND_LOCATION_CLASS_NAME_EXTRA = "BIND_LOCATION_CLASS_NAME_EXTRA";

0 commit comments

Comments
 (0)