Skip to content

Commit 3f04eba

Browse files
committed
Create and use SESSION_LOCK object
Use SESSION_LOCK on synchronized blocks for session
1 parent 37b307f commit 3f04eba

2 files changed

Lines changed: 26 additions & 24 deletions

File tree

android/sdl_android/src/main/java/com/smartdevicelink/managers/lifecycle/LifecycleManager.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ void cycle(SdlDisconnectedReason disconnectedReason) {
9393
//We don't want to alert higher if we are just cycling for legacy bluetooth
9494
onClose("Sdl Proxy Cycled", new SdlException("Sdl Proxy Cycled", SdlExceptionCause.SDL_PROXY_CYCLED), disconnectedReason);
9595
}
96-
synchronized (session) {
96+
synchronized (SESSION_LOCK) {
9797
if (session != null) {
9898
try {
9999
session.startSession();
@@ -165,7 +165,7 @@ void onTransportDisconnected(String info, boolean availablePrimary, BaseTranspor
165165
*/
166166
@Override
167167
void startVideoService(boolean isEncrypted, VideoStreamingParameters parameters, boolean afterPendingRestart) {
168-
synchronized (session) {
168+
synchronized (SESSION_LOCK) {
169169
if (session == null) {
170170
DebugTool.logWarning(TAG, "SdlSession is not created yet.");
171171
return;
@@ -189,7 +189,7 @@ void startVideoService(boolean isEncrypted, VideoStreamingParameters parameters,
189189
* @param parameters VideoStreamingParameters that are desired. Does not guarantee this is what will be accepted.
190190
*/
191191
private void tryStartVideoStream(boolean isEncrypted, VideoStreamingParameters parameters, boolean afterPendingRestart) {
192-
synchronized (session) {
192+
synchronized (SESSION_LOCK) {
193193
if (session == null) {
194194
DebugTool.logWarning(TAG, "SdlSession is not created yet.");
195195
return;
@@ -204,7 +204,7 @@ private void tryStartVideoStream(boolean isEncrypted, VideoStreamingParameters p
204204
return;
205205
}
206206

207-
synchronized (session) {
207+
synchronized (SESSION_LOCK) {
208208
if (afterPendingRestart || !videoServiceStartResponseReceived || !videoServiceStartResponse //If we haven't started the service before
209209
|| (videoServiceStartResponse && isEncrypted && !session.isServiceProtected(SessionType.NAV))) { //Or the service has been started but we'd like to start an encrypted one
210210
if (session != null) {
@@ -247,7 +247,7 @@ public void onServiceError(SdlSession session, SessionType type, String reason)
247247
}
248248
};
249249

250-
synchronized (session) {
250+
synchronized (SESSION_LOCK) {
251251
if (session != null) {
252252
session.addServiceListener(SessionType.NAV, videoServiceListener);
253253
}
@@ -257,7 +257,7 @@ public void onServiceError(SdlSession session, SessionType type, String reason)
257257

258258
@Override
259259
void startAudioService(boolean isEncrypted) {
260-
synchronized (session) {
260+
synchronized (SESSION_LOCK) {
261261
if (session == null) {
262262
DebugTool.logWarning(TAG, "SdlSession is not created yet.");
263263
return;

base/src/main/java/com/smartdevicelink/managers/lifecycle/BaseLifecycleManager.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ abstract class BaseLifecycleManager {
116116
ON_UPDATE_LISTENER_LOCK = new Object(),
117117
ON_REQUEST_LISTENER_LOCK = new Object(),
118118
ON_NOTIFICATION_LISTENER_LOCK = new Object();
119+
protected static final Object SESSION_LOCK = new Object();
120+
119121

120122
SdlSession session;
121123
final AppConfig appConfig;
@@ -151,7 +153,7 @@ abstract class BaseLifecycleManager {
151153

152154
public void start() {
153155
try {
154-
synchronized (session) {
156+
synchronized (SESSION_LOCK) {
155157
if (session != null) {
156158
session.startSession();
157159
}
@@ -165,15 +167,15 @@ public void start() {
165167
* Start a secured RPC service
166168
*/
167169
public void startRPCEncryption() {
168-
synchronized (session) {
170+
synchronized (SESSION_LOCK) {
169171
if (session != null) {
170172
session.startService(SessionType.RPC, true);
171173
}
172174
}
173175
}
174176

175177
public synchronized void stop() {
176-
synchronized (session) {
178+
synchronized (SESSION_LOCK) {
177179
if (session != null) {
178180
session.close();
179181
session = null;
@@ -202,7 +204,7 @@ Taskmaster getTaskmaster() {
202204
}
203205

204206
Version getProtocolVersion() {
205-
synchronized (session) {
207+
synchronized (SESSION_LOCK) {
206208
if (session != null && session.getProtocolVersion() != null) {
207209
return session.getProtocolVersion();
208210
}
@@ -311,7 +313,7 @@ public SystemCapabilityManager getSystemCapabilityManager(SdlManager sdlManager)
311313
}
312314

313315
private boolean isConnected() {
314-
synchronized (session) {
316+
synchronized (SESSION_LOCK) {
315317
if (session != null) {
316318
return session.getIsConnected();
317319
} else {
@@ -413,7 +415,7 @@ public void onReceived(RPCMessage message) {
413415
if (vehicleType != null || systemSoftwareVersion != null) {
414416

415417
List<TransportRecord> activeTransports = null;
416-
synchronized (session) {
418+
synchronized (SESSION_LOCK) {
417419
if (session != null) {
418420
activeTransports = session.getActiveTransports();
419421
}
@@ -818,7 +820,7 @@ private void sendRPCMessagePrivate(RPCMessage message, boolean isInternalMessage
818820

819821
final ProtocolMessage pm = new ProtocolMessage();
820822
pm.setData(msgBytes);
821-
synchronized (session) {
823+
synchronized (SESSION_LOCK) {
822824
if (session != null) {
823825
pm.setSessionID((byte) session.getSessionId());
824826
}
@@ -889,7 +891,7 @@ private void sendRPCMessagePrivate(RPCMessage message, boolean isInternalMessage
889891
pm.setPriorityCoefficient(1);
890892
}
891893

892-
synchronized (session) {
894+
synchronized (SESSION_LOCK) {
893895
if (session != null) {
894896
session.sendMessage(pm);
895897
}
@@ -965,7 +967,7 @@ public void onSessionStarted(int sessionID, Version version, SystemInfo systemIn
965967
DebugTool.logInfo(TAG, "on protocol session started");
966968
if (minimumProtocolVersion != null && minimumProtocolVersion.isNewerThan(version) == 1) {
967969
DebugTool.logWarning(TAG, String.format("Disconnecting from head unit, the configured minimum protocol version %s is greater than the supported protocol version %s", minimumProtocolVersion, getProtocolVersion()));
968-
synchronized (session) {
970+
synchronized (SESSION_LOCK) {
969971
if (session != null) {
970972
session.endService(SessionType.RPC);
971973
}
@@ -978,7 +980,7 @@ public void onSessionStarted(int sessionID, Version version, SystemInfo systemIn
978980
if (systemInfo != null && lifecycleListener != null) {
979981
didCheckSystemInfo = true;
980982
List<TransportRecord> activeTransports = null;
981-
synchronized (session) {
983+
synchronized (SESSION_LOCK) {
982984
if (session != null) {
983985
activeTransports = session.getActiveTransports();
984986
}
@@ -987,7 +989,7 @@ public void onSessionStarted(int sessionID, Version version, SystemInfo systemIn
987989
boolean validSystemInfo = lifecycleListener.onSystemInfoReceived(systemInfo);
988990
if (!validSystemInfo) {
989991
DebugTool.logWarning(TAG, "Disconnecting from head unit, the system info was not accepted.");
990-
synchronized (session) {
992+
synchronized (SESSION_LOCK) {
991993
if (session != null) {
992994
session.endService(SessionType.RPC);
993995
}
@@ -1063,7 +1065,7 @@ public void stop() {
10631065
@Override
10641066
public boolean isConnected() {
10651067
synchronized (BaseLifecycleManager.this) {
1066-
synchronized (BaseLifecycleManager.this.session) {
1068+
synchronized (SESSION_LOCK) {
10671069
if (BaseLifecycleManager.this.session != null) {
10681070
return BaseLifecycleManager.this.session.getIsConnected();
10691071
}
@@ -1075,7 +1077,7 @@ public boolean isConnected() {
10751077
@Override
10761078
public void addServiceListener(SessionType serviceType, ISdlServiceListener sdlServiceListener) {
10771079
synchronized (BaseLifecycleManager.this) {
1078-
synchronized (BaseLifecycleManager.this.session) {
1080+
synchronized (SESSION_LOCK) {
10791081
if (BaseLifecycleManager.this.session != null) {
10801082
BaseLifecycleManager.this.session.addServiceListener(serviceType, sdlServiceListener);
10811083
}
@@ -1086,7 +1088,7 @@ public void addServiceListener(SessionType serviceType, ISdlServiceListener sdlS
10861088
@Override
10871089
public void removeServiceListener(SessionType serviceType, ISdlServiceListener sdlServiceListener) {
10881090
synchronized (BaseLifecycleManager.this) {
1089-
synchronized (BaseLifecycleManager.this.session) {
1091+
synchronized (SESSION_LOCK) {
10901092
if (BaseLifecycleManager.this.session != null) {
10911093
BaseLifecycleManager.this.session.removeServiceListener(serviceType, sdlServiceListener);
10921094
}
@@ -1159,7 +1161,7 @@ public RegisterAppInterfaceResponse getRegisterAppInterfaceResponse() {
11591161
@Override
11601162
public boolean isTransportForServiceAvailable(SessionType serviceType) {
11611163
synchronized (BaseLifecycleManager.this) {
1162-
synchronized (BaseLifecycleManager.this.session) {
1164+
synchronized (SESSION_LOCK) {
11631165
if (BaseLifecycleManager.this.session != null) {
11641166
return BaseLifecycleManager.this.session.isTransportForServiceAvailable(serviceType);
11651167
}
@@ -1191,7 +1193,7 @@ public Version getProtocolVersion() {
11911193
@Override
11921194
public long getMtu(SessionType serviceType) {
11931195
synchronized (BaseLifecycleManager.this) {
1194-
synchronized (BaseLifecycleManager.this.session) {
1196+
synchronized (SESSION_LOCK) {
11951197
if (BaseLifecycleManager.this.session != null) {
11961198
return BaseLifecycleManager.this.session.getMtu(serviceType);
11971199
}
@@ -1295,7 +1297,7 @@ void clean() {
12951297
if (rpcRequestListeners != null) {
12961298
rpcRequestListeners.clear();
12971299
}
1298-
synchronized (session) {
1300+
synchronized (SESSION_LOCK) {
12991301
if (session != null && session.getIsConnected()) {
13001302
session.close();
13011303
}
@@ -1353,7 +1355,7 @@ private void setSecurityLibraryIfAvailable(VehicleType vehicleType) {
13531355
if ((sec != null) && (sec.getMakeList() != null)) {
13541356
if (sec.getMakeList().contains(make)) {
13551357
sec.setAppId(appConfig.getAppID());
1356-
synchronized (session) {
1358+
synchronized (SESSION_LOCK) {
13571359
if (session != null) {
13581360
session.setSdlSecurity(sec);
13591361
sec.handleSdlSession(session);

0 commit comments

Comments
 (0)