Skip to content

Commit 5032491

Browse files
authored
Merge pull request #330 from smartdevicelink/feature/multiplexing_cleanup
Multiplexing cleanup
2 parents a5139a5 + 3688c6c commit 5032491

11 files changed

+350
-190
lines changed

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,13 @@ private void constructor(BaseTransportConfig transportConfig,RouterServiceValida
102102
cachedMultiConfig.setService(null);
103103
}
104104
enableLegacyMode(true,TransportType.BLUETOOTH); //We will use legacy bluetooth connection for this attempt
105-
Log.d(TAG, "Legacy transport : " + legacyTransportRequest);
106105
}
107106
}
108107

109108
if(!isLegacyModeEnabled() && //Make sure legacy mode is not enabled
110109
(transportConfig.getTransportType() == TransportType.MULTIPLEX)){
111110
_transport = new MultiplexTransport((MultiplexTransportConfig)transportConfig,this);
112111
}else if(isLegacyModeEnabled() && legacyTransportRequest == TransportType.BLUETOOTH){
113-
Log.d(TAG, "Creating legacy bluetooth connection");
114112
_transport = new BTTransport(this, true);
115113
}else if(transportConfig.getTransportType() == TransportType.BLUETOOTH){
116114
_transport = new BTTransport(this,((BTTransportConfig)transportConfig).getKeepSocketActive());
@@ -529,22 +527,20 @@ public void forceHardwareConnectEvent(TransportType type){
529527
MultiplexTransport multi = ((MultiplexTransport)_transport);
530528
MultiplexTransportConfig config = multi.getConfig();
531529
ComponentName tempCompName = SdlBroadcastReceiver.consumeQueuedRouterService();
532-
//Log.d(TAG, "Consumed component name: " +tempCompName );
533530
if(config.getService().equals(tempCompName)){ //If this is the same service that just connected that we are already looking at. Attempt to reconnect
534531
if(!multi.getIsConnected() && multi.isDisconnecting() ){ //If we aren't able to force a connection it means the
535-
//Log.d(TAG, "Recreating our multiplexing transport");
536532
_transport = new MultiplexTransport(config,this);
537533
try {
538534
startTransport();
539535
} catch (SdlException e) {
540536
e.printStackTrace();
541537
}
542-
}//else{Log.w(TAG, "Guess we're just calling it a day");}
538+
}
543539
}else if(tempCompName!=null){
544540
//We have a conflicting service request
545541
Log.w(TAG, "Conflicting services. Disconnecting from current and connecting to new");
546-
Log.w(TAG, "Old service " + config.getService().toShortString());
547-
Log.w(TAG, "New Serivce " + tempCompName.toString());
542+
//Log.w(TAG, "Old service " + config.getService().toShortString());
543+
//Log.w(TAG, "New Serivce " + tempCompName.toString());
548544
multi.disconnect();
549545
config.setService(tempCompName);
550546
_transport = new MultiplexTransport(config,this);

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class MultiplexBluetoothTransport {
4949
private static final String TAG = "Bluetooth Transport";
5050
private static final UUID SERVER_UUID= new UUID(0x936DA01F9ABD4D9DL, 0x80C702AF85C822A8L);
5151
// Name for the SDP record when creating server socket
52-
private static final String NAME_SECURE =" SdlProxy";// = "LIVIO_CONNECT";
52+
private static final String NAME_SECURE =" SdlRouterService";
5353

5454

5555
protected static final String SHARED_PREFS = "sdl.bluetoothprefs";
@@ -696,7 +696,7 @@ public ConnectedWriteThread(BluetoothSocket socket) {
696696
//Log.d(TAG, "Creating a Connected - Write Thread");
697697
mmSocket = socket;
698698
OutputStream tmpOut = null;
699-
setName(" Livio Bluetooth Write Thread");
699+
setName("SDL Router BT Write Thread");
700700
// Get the BluetoothSocket input and output streams
701701
try {
702702
tmpOut = socket.getOutputStream();
@@ -731,7 +731,6 @@ public void write(byte[] buffer, int offset, int count) {
731731

732732
public synchronized void cancel() {
733733
try {
734-
Log.d(TAG, "Calling Cancel in the write thread");
735734
if(mmOutStream!=null){
736735
mmOutStream.flush();
737736
mmOutStream.close();
@@ -756,7 +755,7 @@ public ConnectedThread(BluetoothSocket socket) {
756755
//Log.d(TAG, "Creating a Connected - Read Thread");
757756
mmSocket = socket;
758757
InputStream tmpIn = null;
759-
setName(" Livio Bluetooth Read Thread");
758+
setName("SDL Router BT Read Thread");
760759
// Get the BluetoothSocket input and output streams
761760
try {
762761
tmpIn = socket.getInputStream();

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
public class MultiplexTransport extends SdlTransport{
1717
private final static String TAG = "Multiplex Transport";
18-
private String sComment = "I'm_a_little_teapot";
18+
private String sComment = "Multiplexing";
1919

2020
TransportBrokerThread brokerThread;
2121
protected boolean isDisconnecting = false;
@@ -213,7 +213,7 @@ public void removeSession(long sessionId){
213213
public void run() {
214214
Looper.prepare();
215215

216-
if(broker==null){Log.d("JOEY", "Starting broker");
216+
if(broker==null){
217217
synchronized(this){
218218
initTransportBroker();
219219
if(queueStart){
@@ -228,7 +228,6 @@ public void run() {
228228
}
229229
threadLooper = Looper.myLooper();
230230
Looper.loop();
231-
Log.i(TAG, "Looper has finished. Thread should be sutting down");
232231

233232
}
234233

@@ -242,9 +241,8 @@ public boolean onHardwareConnected(TransportType type) {
242241
Log.d(TAG, "On transport connected...");
243242
if(!connected){
244243
connected = true;
245-
Log.d(TAG, "Handling transport connected");
246244
handleTransportConnected();
247-
}else{Log.d(TAG, "Already connected");}
245+
}//else{Log.d(TAG, "Already connected");}
248246
return true;
249247
}else{
250248
try{

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
*
3636
*/
3737
public class RouterServiceValidator {
38-
private static final String TAG = "PackageCheckUtl";
38+
private static final String TAG = "RSVP";
3939
public static final String ROUTER_SERVICE_PACKAGE = "com.sdl.router";
4040

4141
private static final String REQUEST_PREFIX = "https://woprjr.smartdevicelink.com/api/1/applications/queryTrustedRouters";
@@ -116,7 +116,7 @@ public boolean validate(){
116116
}
117117
}
118118

119-
Log.d(TAG, "Checking app package: " + service.getClassName());
119+
//Log.d(TAG, "Checking app package: " + service.getClassName());
120120
packageName = this.appPackageForComponentName(service, pm);
121121

122122

@@ -328,10 +328,9 @@ private static List<SdlApp> findAllSdlApps(Context context){
328328
intent.setAction("sdl.router.startservice");
329329
List<ResolveInfo> infoList = packageManager.queryBroadcastReceivers(intent, 0);
330330
if(infoList!=null){
331-
Log.i(TAG, "Number of SDL apps: " + infoList.size());
332331
String packageName;
333332
for(ResolveInfo info : infoList){
334-
Log.i(TAG, "SDL apps: " + info.activityInfo.packageName);
333+
//Log.i(TAG, "SDL apps: " + info.activityInfo.packageName);
335334
packageName = info.activityInfo.packageName;
336335
try {
337336
apps.add(new SdlApp(packageName,packageManager.getPackageInfo(packageName,0).versionCode));
@@ -362,7 +361,6 @@ protected static boolean createTrustedListRequest(final Context context, boolean
362361
}
363362

364363
if(!forceRefresh && (System.currentTimeMillis()-getTrustedAppListTimeStamp(context))<REFRESH_TRUSTED_APP_LIST_TIME){
365-
Log.d(TAG, "Don't need to get new list");
366364
//Our list should still be ok for now so we will skip the request
367365
pendingListRefresh = false;
368366
return false;
@@ -393,15 +391,13 @@ protected static boolean createTrustedListRequest(final Context context, boolean
393391

394392
try {object.put(JSON_PUT_ARRAY_TAG, array);} catch (JSONException e) {e.printStackTrace();}
395393

396-
Log.d(TAG, "Request of apps: " + object.toString());
397-
398394
if (cb == null) {
399395
cb = new HttpRequestTaskCallback() {
400396

401397
@Override
402398
public void httpCallComplete(String response) {
403399
// Might want to check if this list is ok
404-
Log.d(TAG, "APPS! " + response);
400+
//Log.d(TAG, "APPS! " + response);
405401
setTrustedList(context, response);
406402
pendingListRefresh = false;
407403
}

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

Lines changed: 72 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.smartdevicelink.transport;
22

33
import java.util.Locale;
4+
import java.util.Vector;
5+
import java.util.concurrent.ConcurrentLinkedQueue;
46

57
import android.app.ActivityManager;
68
import android.app.ActivityManager.RunningServiceInfo;
@@ -9,8 +11,6 @@
911
import android.content.ComponentName;
1012
import android.content.Context;
1113
import android.content.Intent;
12-
import android.content.SharedPreferences;
13-
import android.content.pm.PackageManager.NameNotFoundException;
1414
import android.util.Log;
1515

1616
public abstract class SdlBroadcastReceiver extends BroadcastReceiver{
@@ -29,7 +29,7 @@ public abstract class SdlBroadcastReceiver extends BroadcastReceiver{
2929
public static final String TRANSPORT_GLOBAL_PREFS = "SdlTransportPrefs";
3030
public static final String IS_TRANSPORT_CONNECTED = "isTransportConnected";
3131

32-
public static ComponentName runningBluetoothServicePackage = null;
32+
public static Vector<ComponentName> runningBluetoothServicePackage = null;
3333

3434
@SuppressWarnings("rawtypes")
3535
private static Class localRouterClass;
@@ -72,15 +72,15 @@ public void onReceive(Context context, Intent intent) {
7272
String packageName = intent.getStringExtra(TransportConstants.START_ROUTER_SERVICE_SDL_ENABLED_APP_PACKAGE);
7373
ComponentName componentName = intent.getParcelableExtra(TransportConstants.START_ROUTER_SERVICE_SDL_ENABLED_CMP_NAME);
7474
if(componentName!=null){
75-
Log.v(TAG, "SDL enabled by router service from " + packageName + " compnent package " + componentName.getPackageName() + " - " + componentName.getClassName());
75+
//Log.v(TAG, "SDL enabled by router service from " + packageName + " compnent package " + componentName.getPackageName() + " - " + componentName.getClassName());
7676
RouterServiceValidator vlad = new RouterServiceValidator(context,componentName);
7777
if(vlad.validate()){
78-
Log.d(TAG, "Router service trusted!");
78+
//Log.d(TAG, "Router service trusted!");
7979
queuedService = componentName;
8080
intent.setAction("com.sdl.noaction"); //Replace what's there so we do go into some unintended loop
8181
onSdlEnabled(context, intent);
8282
}else{
83-
Log.e(TAG, "RouterService was not trusted. Ignoring intent from : "+ componentName.getClassName());
83+
Log.w(TAG, "RouterService was not trusted. Ignoring intent from : "+ componentName.getClassName());
8484
}
8585

8686
}
@@ -93,7 +93,6 @@ public void onReceive(Context context, Intent intent) {
9393
return;
9494
}else if(intent.getBooleanExtra(TransportConstants.PING_ROUTER_SERVICE_EXTRA, false)){
9595
//We were told to wake up our router services
96-
Log.d(TAG, "Starting router service off ping");
9796
boolean altServiceWake = intent.getBooleanExtra(TransportConstants.BIND_REQUEST_TYPE_ALT_TRANSPORT, false);
9897
didStart = wakeUpRouterService(context, false,altServiceWake );
9998

@@ -113,15 +112,13 @@ public void onReceive(Context context, Intent intent) {
113112
return;
114113
}else if(state == BluetoothAdapter.STATE_TURNING_ON){
115114
//We started bluetooth, we should check for a new valid router list
116-
Log.d(TAG, "Attempting to get list of approved router services");
117115
RouterServiceValidator.createTrustedListRequest(context,true);
118116
}
119117
}
120118

121119
if(localRouterClass!=null){ //If there is a supplied router service lets run some logic regarding starting one
122120

123121
if(!didStart){
124-
Log.d(TAG, "Waking up router service");
125122
didStart = wakeUpRouterService(context, true,false);
126123
}
127124

@@ -138,10 +135,8 @@ public void onReceive(Context context, Intent intent) {
138135
}
139136

140137
private boolean wakeUpRouterService(Context context, boolean ping, boolean altTransportWake){
141-
Log.d(TAG, "Waking up router service");
142138
if(!isRouterServiceRunning(context, ping)){
143139
//If there isn't a service running we should try to start one
144-
Log.i(TAG, "Attempting to start an instance of the Router Service");
145140
//The under class should have implemented this....
146141

147142
//So let's start up our service since no copy is running
@@ -152,18 +147,21 @@ private boolean wakeUpRouterService(Context context, boolean ping, boolean altTr
152147
context.startService(serviceIntent);
153148
return true;
154149
}else{
155-
Log.i(TAG, "An instance of the Router Service is already running");
156-
if(altTransportWake){
150+
if(altTransportWake && runningBluetoothServicePackage!=null && runningBluetoothServicePackage.size()>0){
157151
Intent serviceIntent = new Intent();
158-
serviceIntent.setComponent(runningBluetoothServicePackage);
159152
serviceIntent.setAction(TransportConstants.BIND_REQUEST_TYPE_ALT_TRANSPORT);
160-
context.startService(serviceIntent);
153+
//context.startService(serviceIntent);
154+
for(ComponentName compName: runningBluetoothServicePackage){
155+
serviceIntent.setComponent(compName);
156+
context.startService(serviceIntent);
157+
158+
}
161159
return true;
162160
}
163161
return false;
164162
}
165163
}
166-
164+
167165
/**
168166
* Determines if an instance of the Router Service is currently running on the device.
169167
* @param context A context to access Android system services through.
@@ -175,13 +173,18 @@ private static boolean isRouterServiceRunning(Context context, boolean pingServi
175173
Log.e(TAG, "Can't look for router service, context supplied was null");
176174
return false;
177175
}
178-
Log.d(TAG, "Looking for Service: "+ SDL_ROUTER_SERVICE_CLASS_NAME);
179176
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
177+
if(runningBluetoothServicePackage==null){
178+
runningBluetoothServicePackage = new Vector<ComponentName>();
179+
}else{
180+
runningBluetoothServicePackage.clear();
181+
}
180182
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
181183
//We will check to see if it contains this name, should be pretty specific
182184
//Log.d(TAG, "Found Service: "+ service.service.getClassName());
183185
if ((service.service.getClassName()).toLowerCase(Locale.US).contains(SDL_ROUTER_SERVICE_CLASS_NAME)) {
184-
runningBluetoothServicePackage = service.service; //Store which instance is running
186+
187+
runningBluetoothServicePackage.add(service.service); //Store which instance is running
185188
if(pingService){
186189
Intent intent = new Intent();
187190
intent.setClassName(service.service.getPackageName(), service.service.getClassName());
@@ -196,21 +199,64 @@ private static boolean isRouterServiceRunning(Context context, boolean pingServi
196199

197200
}
198201

202+
/**
203+
* This call will reach out to all SDL related router services to check if they're connected. If a the router service is connected, it will react by pinging all clients. This receiver will then
204+
* receive that ping and if the router service is trusted, the onSdlEnabled method will be called.
205+
* @param context
206+
*/
207+
public static void queryForConnectedService(Context context){
208+
//Leverage existing call. Include ping bit
209+
requestTransportStatus(context,null,true);
210+
}
199211
/**
200212
* If a Router Service is running, this method determines if that service is connected to a device over some form of transport.
201213
* @param context A context to access Android system services through. If null is passed, this will always return false
202214
* @param callback Use this callback to find out if the router service is connected or not.
203215
*/
204-
public static void requestTransportStatus(Context context, SdlRouterStatusProvider.ConnectedStatusCallback callback){
205-
Log.d(TAG, "Checking to see if router service is transport connected");
216+
public static void requestTransportStatus(Context context, final SdlRouterStatusProvider.ConnectedStatusCallback callback){
217+
requestTransportStatus(context,callback,false);
218+
}
219+
220+
private static void requestTransportStatus(Context context, final SdlRouterStatusProvider.ConnectedStatusCallback callback, final boolean triggerRouterServicePing){
206221
if(context == null){
207222
if(callback!=null){
208-
callback.onConnectionStatusUpdate(false, context);
223+
callback.onConnectionStatusUpdate(false, null,context);
209224
}
225+
return;
210226
}
211-
if(isRouterServiceRunning(context,false)){ //So there is a service up, let's see if it's connected
212-
SdlRouterStatusProvider provider = new SdlRouterStatusProvider(context,runningBluetoothServicePackage,callback);
213-
provider.checkIsConnected();
227+
if(isRouterServiceRunning(context,false) && !runningBluetoothServicePackage.isEmpty()){ //So there is a service up, let's see if it's connected
228+
final ConcurrentLinkedQueue<ComponentName> list = new ConcurrentLinkedQueue<ComponentName>(runningBluetoothServicePackage);
229+
if(runningBluetoothServicePackage.size()>0){ //TODO for testing do this for all cases
230+
final SdlRouterStatusProvider.ConnectedStatusCallback sdlBrCallback = new SdlRouterStatusProvider.ConnectedStatusCallback() {
231+
232+
@Override
233+
public void onConnectionStatusUpdate(boolean connected, ComponentName service,Context context) {
234+
if(!connected && !list.isEmpty()){
235+
SdlRouterStatusProvider provider = new SdlRouterStatusProvider(context,list.poll(), this);
236+
if(triggerRouterServicePing){provider.setFlags(TransportConstants.ROUTER_STATUS_FLAG_TRIGGER_PING); }
237+
provider.checkIsConnected();
238+
}else{
239+
Log.d(TAG, service.getPackageName() + " is connected = " + connected);
240+
if(callback!=null){
241+
callback.onConnectionStatusUpdate(connected, service,context);
242+
}
243+
list.clear();
244+
}
245+
246+
}
247+
};
248+
SdlRouterStatusProvider provider = new SdlRouterStatusProvider(context,list.poll(),sdlBrCallback);
249+
if(triggerRouterServicePing){
250+
provider.setFlags(TransportConstants.ROUTER_STATUS_FLAG_TRIGGER_PING);
251+
}
252+
provider.checkIsConnected();
253+
}else{ //If only one service is running, just check that
254+
SdlRouterStatusProvider provider = new SdlRouterStatusProvider(context,runningBluetoothServicePackage.get(0),callback);
255+
if(triggerRouterServicePing){
256+
provider.setFlags(TransportConstants.ROUTER_STATUS_FLAG_TRIGGER_PING);
257+
}
258+
provider.checkIsConnected();
259+
}
214260
}else{
215261
Log.w(TAG, "Router service isn't running, returning false.");
216262
if(BluetoothAdapter.getDefaultAdapter()!=null && BluetoothAdapter.getDefaultAdapter().isEnabled()){
@@ -220,10 +266,11 @@ public static void requestTransportStatus(Context context, SdlRouterStatusProvid
220266
context.sendBroadcast(serviceIntent);
221267
}
222268
if(callback!=null){
223-
callback.onConnectionStatusUpdate(false, context);
269+
callback.onConnectionStatusUpdate(false, null,context);
224270
}
225271
}
226272
}
273+
227274

228275

229276
public static ComponentName consumeQueuedRouterService(){

0 commit comments

Comments
 (0)