Skip to content

Commit e97757f

Browse files
authored
Add pending intent to pings (#1844)
Removed cached pingIntent in favor of creating when needed to ensure pending intent was valid.
1 parent a924127 commit e97757f

2 files changed

Lines changed: 21 additions & 11 deletions

File tree

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public class SdlRouterService extends Service {
145145
/**
146146
* <b> NOTE: DO NOT MODIFY THIS UNLESS YOU KNOW WHAT YOU'RE DOING.</b>
147147
*/
148-
protected static final int ROUTER_SERVICE_VERSION_NUMBER = 16;
148+
protected static final int ROUTER_SERVICE_VERSION_NUMBER = 17;
149149

150150
private static final String ROUTER_SERVICE_PROCESS = "com.smartdevicelink.router";
151151

@@ -239,7 +239,6 @@ public class SdlRouterService extends Service {
239239
* Executor for making sure clients are still running during trying times
240240
*/
241241
private ScheduledExecutorService clientPingExecutor = null;
242-
Intent pingIntent = null;
243242
private boolean isPingingClients = false;
244243
int pingCount = 0;
245244

@@ -818,10 +817,8 @@ public void handleMessage(Message msg) {
818817
}
819818
}
820819
if (service.isPrimaryTransportConnected() && ((TransportConstants.ROUTER_STATUS_FLAG_TRIGGER_PING & flags) == TransportConstants.ROUTER_STATUS_FLAG_TRIGGER_PING)) {
821-
if (service.pingIntent == null) {
822-
service.initPingIntent();
823-
}
824-
AndroidTools.sendExplicitBroadcast(service.getApplicationContext(), service.pingIntent, null);
820+
AndroidTools.sendExplicitBroadcast(service.getApplicationContext(),
821+
service.createPingIntent(), null);
825822
}
826823
break;
827824
default:
@@ -2967,16 +2964,29 @@ protected PacketWriteTask getNextTask(TransportType transportType) {
29672964
return null;
29682965
}
29692966

2970-
private void initPingIntent() {
2971-
pingIntent = new Intent();
2967+
private Intent createPingIntent() {
2968+
Intent pingIntent = new Intent();
29722969
pingIntent.setAction(TransportConstants.START_ROUTER_SERVICE_ACTION);
29732970
pingIntent.putExtra(TransportConstants.START_ROUTER_SERVICE_SDL_ENABLED_EXTRA, true);
29742971
pingIntent.putExtra(TransportConstants.START_ROUTER_SERVICE_SDL_ENABLED_APP_PACKAGE, getBaseContext().getPackageName());
29752972
pingIntent.putExtra(TransportConstants.START_ROUTER_SERVICE_SDL_ENABLED_CMP_NAME, new ComponentName(SdlRouterService.this, SdlRouterService.this.getClass()));
29762973
pingIntent.putExtra(TransportConstants.START_ROUTER_SERVICE_SDL_ENABLED_PING, true);
2974+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
2975+
//Starting in Android 12 we need to start services from a foreground context
2976+
//To enable developers to be able to start their SdlService from the "background"
2977+
//we will attach a pendingIntent as an extra to the intent
2978+
//the developer can use this pendingIntent to start their SdlService from the context of
2979+
//the active RouterService
2980+
Intent pending = new Intent();
2981+
PendingIntent pendingIntent = PendingIntent.getForegroundService(this, (int) System.currentTimeMillis(), pending, PendingIntent.FLAG_MUTABLE | Intent.FILL_IN_COMPONENT);
2982+
pingIntent.putExtra(TransportConstants.PENDING_INTENT_EXTRA, pendingIntent);
2983+
}
2984+
29772985
if (receivedVehicleType != null) {
29782986
pingIntent.putExtra(TransportConstants.VEHICLE_INFO_EXTRA, receivedVehicleType.getStore());
29792987
}
2988+
2989+
return pingIntent;
29802990
}
29812991

29822992
private void startClientPings() {
@@ -3001,6 +3011,7 @@ private void startClientPings() {
30013011

30023012
clientPingExecutor.scheduleAtFixedRate(new Runnable() {
30033013
List<ResolveInfo> sdlApps;
3014+
Intent pingIntent;
30043015

30053016
@Override
30063017
public void run() {
@@ -3010,7 +3021,7 @@ public void run() {
30103021
return;
30113022
}
30123023
if (pingIntent == null) {
3013-
initPingIntent();
3024+
pingIntent = createPingIntent();
30143025
}
30153026

30163027
if (sdlApps == null) {
@@ -3039,7 +3050,6 @@ private void stopClientPings() {
30393050
clientPingExecutor = null;
30403051
isPingingClients = false;
30413052
}
3042-
pingIntent = null;
30433053
}
30443054

30453055
/* ****************************************************************************************************************************************

android/sdl_android/src/main/res/values/sdl.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<resources>
33
<string name="sdl_router_service_version_name" translatable="false">sdl_router_version</string>
44

5-
<integer name="sdl_router_service_version_value">16</integer>
5+
<integer name="sdl_router_service_version_value">17</integer>
66

77
<string name="sdl_router_service_is_custom_name" translatable="false">sdl_custom_router</string>
88
<string name="sdl_oem_vehicle_type_filter_name" translatable="false">sdl_oem_vehicle_type</string>

0 commit comments

Comments
 (0)