Skip to content

Commit 301071a

Browse files
committed
Merge branch 'develop' into feature/external_security_support
2 parents b9d21a2 + 1ee6edb commit 301071a

File tree

8 files changed

+240
-77
lines changed

8 files changed

+240
-77
lines changed

sdl_android_lib/src/com/smartdevicelink/SdlConnection/SdlConnection.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,6 @@ public void onTransportDisconnected(String info) {
362362
} catch (SdlException e) {
363363
e.printStackTrace();
364364
}
365-
((MultiplexTransport)_transport).forceHardwareConnectEvent(TransportType.BLUETOOTH);
366365
}
367366
}else{ //The service must be null or already consumed. Let's see if we can find the connection that consumed it
368367
for (SdlSession session : listenerList) {
@@ -532,12 +531,14 @@ public void forceHardwareConnectEvent(TransportType type){
532531
ComponentName tempCompName = SdlBroadcastReceiver.consumeQueuedRouterService();
533532
//Log.d(TAG, "Consumed component name: " +tempCompName );
534533
if(config.getService().equals(tempCompName)){ //If this is the same service that just connected that we are already looking at. Attempt to reconnect
535-
boolean forced = multi.forceHardwareConnectEvent(TransportType.BLUETOOTH);
536-
537-
if(!forced && multi.isDisconnecting() ){ //If we aren't able to force a connection it means the
534+
if(!multi.getIsConnected() && multi.isDisconnecting() ){ //If we aren't able to force a connection it means the
538535
//Log.d(TAG, "Recreating our multiplexing transport");
539536
_transport = new MultiplexTransport(config,this);
540-
((MultiplexTransport)_transport).forceHardwareConnectEvent(TransportType.BLUETOOTH);
537+
try {
538+
startTransport();
539+
} catch (SdlException e) {
540+
e.printStackTrace();
541+
}
541542
}//else{Log.w(TAG, "Guess we're just calling it a day");}
542543
}else if(tempCompName!=null){
543544
//We have a conflicting service request
@@ -552,7 +553,6 @@ public void forceHardwareConnectEvent(TransportType type){
552553
} catch (SdlException e) {
553554
e.printStackTrace();
554555
}
555-
((MultiplexTransport)_transport).forceHardwareConnectEvent(TransportType.BLUETOOTH);
556556

557557
}
558558
}else if(_transport.getTransportType()==TransportType.BLUETOOTH

sdl_android_lib/src/com/smartdevicelink/transport/BTTransport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public void openConnection () throws SdlException {
187187
throw new SdlException("Bluetooth adapter must be on to instantiate a SdlProxy object.", SdlExceptionCause.BLUETOOTH_DISABLED);
188188
}
189189

190-
if(((SdlException) ex).getSdlExceptionCause() == SdlExceptionCause.BLUETOOTH_SOCKET_UNAVAILABLE) {
190+
if(ex instanceof SdlException && ((SdlException) ex).getSdlExceptionCause() == SdlExceptionCause.BLUETOOTH_SOCKET_UNAVAILABLE) {
191191
SdlConnection.enableLegacyMode(false, null);
192192
throw new SdlException("Could not open connection to SDL.", SdlExceptionCause.BLUETOOTH_SOCKET_UNAVAILABLE);
193193

sdl_android_lib/src/com/smartdevicelink/transport/MultiplexTransport.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,6 @@ public MultiplexTransport(MultiplexTransportConfig transportConfig, final ITrans
3333
//brokerThread.start();
3434

3535
}
36-
37-
public boolean forceHardwareConnectEvent(TransportType type){
38-
if(brokerThread!=null){
39-
brokerThread.onHardwareConnected(type);
40-
return true;
41-
}
42-
Log.w(TAG, "Transport broker thread was null, nothing to force connect. Are we disconnecting? " + isDisconnecting);
43-
return false;
44-
45-
}
4636

4737
public boolean isDisconnecting(){
4838
return this.isDisconnecting;

sdl_android_lib/src/com/smartdevicelink/transport/SdlBroadcastReceiver.java

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -199,31 +199,18 @@ private static boolean isRouterServiceRunning(Context context, boolean pingServi
199199
/**
200200
* If a Router Service is running, this method determines if that service is connected to a device over some form of transport.
201201
* @param context A context to access Android system services through. If null is passed, this will always return false
202-
* @return True if a transport connection is established, false otherwise.
202+
* @param callback Use this callback to find out if the router service is connected or not.
203203
*/
204-
public static boolean isTransportConnected(Context context){
204+
public static void requestTransportStatus(Context context, SdlRouterStatusProvider.ConnectedStatusCallback callback){
205205
Log.d(TAG, "Checking to see if router service is transport connected");
206206
if(context == null){
207-
return false;
207+
if(callback!=null){
208+
callback.onConnectionStatusUpdate(false, context);
209+
}
208210
}
209211
if(isRouterServiceRunning(context,false)){ //So there is a service up, let's see if it's connected
210-
Context con;
211-
try {
212-
con = context.createPackageContext(runningBluetoothServicePackage.getPackageName(), 0);
213-
if(con==null ){
214-
Log.w(TAG, "Unable to check for service connection. Returning false. "+runningBluetoothServicePackage);
215-
return false; // =( well that sucks.
216-
}
217-
SharedPreferences pref = con.getSharedPreferences(
218-
con.getPackageName()+TRANSPORT_GLOBAL_PREFS , 4);
219-
boolean connected = pref.getBoolean(IS_TRANSPORT_CONNECTED, false);
220-
// Log.w(TAG, "Is Connected? Returning " + connected);
221-
return connected;
222-
} catch (NameNotFoundException e) {
223-
e.printStackTrace();
224-
return false;
225-
}
226-
212+
SdlRouterStatusProvider provider = new SdlRouterStatusProvider(context,runningBluetoothServicePackage,callback);
213+
provider.checkIsConnected();
227214
}else{
228215
Log.w(TAG, "Router service isn't running, returning false.");
229216
if(BluetoothAdapter.getDefaultAdapter()!=null && BluetoothAdapter.getDefaultAdapter().isEnabled()){
@@ -232,9 +219,10 @@ public static boolean isTransportConnected(Context context){
232219
serviceIntent.putExtra(TransportConstants.PING_ROUTER_SERVICE_EXTRA, true);
233220
context.sendBroadcast(serviceIntent);
234221
}
222+
if(callback!=null){
223+
callback.onConnectionStatusUpdate(false, context);
224+
}
235225
}
236-
237-
return false;
238226
}
239227

240228

sdl_android_lib/src/com/smartdevicelink/transport/SdlRouterService.java

Lines changed: 69 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ public class SdlRouterService extends Service{
104104

105105
private static boolean connectAsClient = false;
106106
private static boolean closing = false;
107-
private boolean isTarnsportConnected = false;
107+
private boolean isTransportConnected = false;
108+
private TransportType connectedTransportType = null;
108109
private static Context currentContext = null;
109110

110111
private Handler versionCheckTimeOutHandler, altTransportTimerHandler;
@@ -320,9 +321,10 @@ public void handleMessage(Message msg) {
320321
pingClients();
321322
Message message = Message.obtain();
322323
message.what = TransportConstants.ROUTER_REGISTER_CLIENT_RESPONSE;
324+
message.arg1 = TransportConstants.REGISTRATION_RESPONSE_SUCESS;
323325
long appId = receivedBundle.getLong(TransportConstants.APP_ID_EXTRA, -1);
324326
if(appId<0 || msg.replyTo == null){
325-
Log.w(TAG, "Unable to requster app as no id or messenger was included");
327+
Log.w(TAG, "Unable to register app as no id or messenger was included");
326328
if(msg.replyTo!=null){
327329
message.arg1 = TransportConstants.REGISTRATION_RESPONSE_DENIED_APP_ID_NOT_INCLUDED;
328330
try {
@@ -356,11 +358,14 @@ public void handleMessage(Message msg) {
356358
}
357359
}
358360
onAppRegistered(app);
359-
361+
360362
returnBundle = new Bundle();
361-
362-
if(MultiplexBluetoothTransport.currentlyConnectedDevice!=null){
363-
returnBundle.putString(CONNECTED_DEVICE_STRING_EXTRA_NAME, MultiplexBluetoothTransport.currentlyConnectedDevice);
363+
//Add params if connected
364+
if(SdlRouterService.this.isTransportConnected){
365+
returnBundle.putString(TransportConstants.HARDWARE_CONNECTED, SdlRouterService.this.connectedTransportType.name());
366+
if(MultiplexBluetoothTransport.currentlyConnectedDevice!=null){
367+
returnBundle.putString(CONNECTED_DEVICE_STRING_EXTRA_NAME, MultiplexBluetoothTransport.currentlyConnectedDevice);
368+
}
364369
}
365370
if(!returnBundle.isEmpty()){
366371
message.setData(returnBundle);
@@ -371,7 +376,6 @@ public void handleMessage(Message msg) {
371376
registeredApps.remove(appId);
372377
}
373378
}
374-
375379
break;
376380
case TransportConstants.ROUTER_UNREGISTER_CLIENT:
377381
long appIdToUnregister = receivedBundle.getLong(TransportConstants.APP_ID_EXTRA, -1);
@@ -520,7 +524,6 @@ public void handleMessage(Message msg) {
520524
&& altTransportService.equals(msg.replyTo)){
521525
//The same transport that was connected to the router service is now telling us it's disconnected. Let's inform clients and clear our saved messenger
522526
altTransportService = null;
523-
storeConnectedStatus(false);
524527
onTransportDisconnected(TransportType.valueOf(receivedBundle.getString(TransportConstants.HARDWARE_DISCONNECTED)));
525528
shouldServiceRemainOpen(null); //this will close the service if bluetooth is not available
526529
}
@@ -540,7 +543,6 @@ public void handleMessage(Message msg) {
540543
altTransportTimerHandler = null;
541544
altTransportTimerRunnable = null;
542545

543-
storeConnectedStatus(true);
544546
//Let the alt transport know they are good to go
545547
retMsg.arg1 = TransportConstants.ROUTER_REGISTER_ALT_TRANSPORT_RESPONSE_SUCESS;
546548
onTransportConnected(TransportType.valueOf(receivedBundle.getString(TransportConstants.HARDWARE_CONNECTED)));
@@ -576,6 +578,39 @@ public void handleMessage(Message msg) {
576578

577579
}
578580
};
581+
582+
/**
583+
* Target we publish for alternative transport (USB) clients to send messages to RouterHandler.
584+
*/
585+
final Messenger routerStatusMessenger = new Messenger(new RouterStatusHandler());
586+
587+
/**
588+
* Handler of incoming messages from an alternative transport (USB).
589+
*/
590+
class RouterStatusHandler extends Handler {
591+
ClassLoader loader = getClass().getClassLoader();
592+
@Override
593+
public void handleMessage(Message msg) {
594+
switch(msg.what){
595+
case TransportConstants.ROUTER_STATUS_CONNECTED_STATE_REQUEST:
596+
if(msg.replyTo==null){
597+
break;
598+
}
599+
Message message = Message.obtain();
600+
message.what = TransportConstants.ROUTER_STATUS_CONNECTED_STATE_RESPONSE;
601+
message.arg1 = (isTransportConnected == true) ? 1 : 0;
602+
try {
603+
msg.replyTo.send(message);
604+
} catch (RemoteException e) {
605+
e.printStackTrace();
606+
}
607+
break;
608+
default:
609+
Log.w(TAG, "Unsopported request: " + msg.what);
610+
break;
611+
}
612+
}
613+
};
579614

580615
/* **************************************************************************************************************************************
581616
*********************************************** Life Cycle **************************************************************
@@ -594,6 +629,8 @@ public IBinder onBind(Intent intent) {
594629
return this.altTransportMessenger.getBinder();
595630
}else if(TransportConstants.BIND_REQUEST_TYPE_CLIENT.equals(requestType)){
596631
return this.routerMessenger.getBinder();
632+
}else if(TransportConstants.BIND_REQUEST_TYPE_STATUS.equals(requestType)){
633+
return this.routerStatusMessenger.getBinder();
597634
}else{
598635
Log.w(TAG, "Uknown bind request type");
599636
}
@@ -691,7 +728,6 @@ public void onCreate() {
691728
}
692729
closing = false;
693730
currentContext = getBaseContext();
694-
storeConnectedStatus(false);
695731

696732
startVersionCheck();
697733
Log.i(TAG, "SDL Router Service has been created");
@@ -947,7 +983,6 @@ public boolean shouldServiceRemainOpen(Intent intent){
947983
*/
948984
public void closeSelf(){
949985
closing = true;
950-
storeConnectedStatus(false);
951986
if(getBaseContext()!=null){
952987
stopSelf();
953988
}else{
@@ -980,7 +1015,7 @@ private synchronized void initBluetoothSerialService(){
9801015
}
9811016

9821017
public void onTransportConnected(final TransportType type){
983-
isTarnsportConnected = true;
1018+
isTransportConnected = true;
9841019
enterForeground();
9851020
if(packetWriteTaskMaster!=null){
9861021
packetWriteTaskMaster.close();
@@ -989,6 +1024,8 @@ public void onTransportConnected(final TransportType type){
9891024
packetWriteTaskMaster = new PacketWriteTaskMaster();
9901025
packetWriteTaskMaster.start();
9911026

1027+
connectedTransportType = type;
1028+
9921029
Intent startService = new Intent();
9931030
startService.setAction(START_SERVICE_ACTION);
9941031
startService.putExtra(TransportConstants.START_ROUTER_SERVICE_SDL_ENABLED_EXTRA, true);
@@ -997,15 +1034,32 @@ public void onTransportConnected(final TransportType type){
9971034
startService.putExtra(TransportConstants.START_ROUTER_SERVICE_SDL_ENABLED_CMP_NAME, new ComponentName(this, this.getClass()));
9981035
sendBroadcast(startService);
9991036
//HARDWARE_CONNECTED
1037+
if(!(registeredApps== null || registeredApps.isEmpty())){
1038+
//If we have clients
1039+
notifyClients(createHardwareConnectedMessage(type));
1040+
}
1041+
}
1042+
1043+
private Message createHardwareConnectedMessage(final TransportType type){
1044+
Message message = Message.obtain();
1045+
message.what = TransportConstants.HARDWARE_CONNECTION_EVENT;
1046+
Bundle bundle = new Bundle();
1047+
bundle.putString(TransportConstants.HARDWARE_CONNECTED, type.name());
1048+
if(MultiplexBluetoothTransport.currentlyConnectedDevice!=null){
1049+
bundle.putString(CONNECTED_DEVICE_STRING_EXTRA_NAME, MultiplexBluetoothTransport.currentlyConnectedDevice);
1050+
}
1051+
message.setData(bundle);
1052+
return message;
1053+
10001054
}
10011055

10021056
public void onTransportDisconnected(TransportType type){
10031057
if(altTransportService!=null){ //If we still have an alt transport open, then we don't need to tell the clients to close
10041058
return;
10051059
}
10061060
Log.e(TAG, "Notifying client service of hardware disconnect.");
1007-
1008-
isTarnsportConnected = false;
1061+
connectedTransportType = null;
1062+
isTransportConnected = false;
10091063
stopClientPings();
10101064

10111065
exitForeground();//Leave our foreground state as we don't have a connection anymore
@@ -1076,18 +1130,15 @@ public void handleMessage(Message msg) {
10761130
case MESSAGE_STATE_CHANGE:
10771131
switch (msg.arg1) {
10781132
case MultiplexBluetoothTransport.STATE_CONNECTED:
1079-
storeConnectedStatus(true);
10801133
onTransportConnected(TransportType.BLUETOOTH);
10811134
break;
10821135
case MultiplexBluetoothTransport.STATE_CONNECTING:
10831136
// Currently attempting to connect - update UI?
10841137
break;
10851138
case MultiplexBluetoothTransport.STATE_LISTEN:
1086-
storeConnectedStatus(false);
10871139
break;
10881140
case MultiplexBluetoothTransport.STATE_NONE:
10891141
// We've just lost the connection
1090-
storeConnectedStatus(false);
10911142
if(!connectAsClient ){
10921143
if(!legacyModeEnabled && !closing){
10931144
initBluetoothSerialService();
@@ -1425,17 +1476,6 @@ private synchronized boolean bluetoothConnect(BluetoothDevice device){
14251476
//**************************************************************************************************************************************
14261477
//********************************************************* PREFERENCES ****************************************************************
14271478
//**************************************************************************************************************************************
1428-
1429-
@SuppressLint("WorldReadableFiles")
1430-
@SuppressWarnings("deprecation")
1431-
private void storeConnectedStatus(boolean isConnected){
1432-
SharedPreferences prefs = getApplicationContext().getSharedPreferences(getApplicationContext().getPackageName()+SdlBroadcastReceiver.TRANSPORT_GLOBAL_PREFS,
1433-
Context.MODE_WORLD_READABLE);
1434-
SharedPreferences.Editor editor = prefs.edit();
1435-
editor.putBoolean(SdlBroadcastReceiver.IS_TRANSPORT_CONNECTED, isConnected);
1436-
editor.commit();
1437-
}
1438-
14391479
/**
14401480
* This method will set the last known bluetooth connection method that worked with this phone.
14411481
* This helps speed up the process of connecting
@@ -1777,7 +1817,7 @@ protected PacketWriteTask getNextTask(){
17771817

17781818
private void startClientPings(){
17791819
synchronized(this){
1780-
if(!isTarnsportConnected){ //If we aren't connected, bail
1820+
if(!isTransportConnected){ //If we aren't connected, bail
17811821
return;
17821822
}
17831823
if(isPingingClients){

0 commit comments

Comments
 (0)