summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoey Grover <joeygrover@gmail.com>2016-03-16 17:13:57 -0400
committerJoey Grover <joeygrover@gmail.com>2016-03-16 17:13:57 -0400
commit774175b0c30fd6a70c7b2b12a9490d6a8b022f4b (patch)
tree76776390be48dde88987dc97283bc946a61746bb
parent6b3f737bbcc2e1cfde64f7965d7828b801193c43 (diff)
downloadsdl_android-774175b0c30fd6a70c7b2b12a9490d6a8b022f4b.tar.gz
Improving alt transport lifecycle.
-rw-r--r--sdl_android_lib/src/com/smartdevicelink/transport/SdlBroadcastReceiver.java25
-rw-r--r--sdl_android_lib/src/com/smartdevicelink/transport/SdlRouterService.java100
2 files changed, 90 insertions, 35 deletions
diff --git a/sdl_android_lib/src/com/smartdevicelink/transport/SdlBroadcastReceiver.java b/sdl_android_lib/src/com/smartdevicelink/transport/SdlBroadcastReceiver.java
index 143ba735e..943369ce4 100644
--- a/sdl_android_lib/src/com/smartdevicelink/transport/SdlBroadcastReceiver.java
+++ b/sdl_android_lib/src/com/smartdevicelink/transport/SdlBroadcastReceiver.java
@@ -31,7 +31,7 @@ public abstract class SdlBroadcastReceiver extends BroadcastReceiver{
public static final String TRANSPORT_GLOBAL_PREFS = "SdlTransportPrefs";
public static final String IS_TRANSPORT_CONNECTED = "isTransportConnected";
- public static String runningBluetoothServicePackage = null;
+ public static ComponentName runningBluetoothServicePackage = null;
@SuppressWarnings("rawtypes")
private static Class localRouterClass;
@@ -95,7 +95,8 @@ public abstract class SdlBroadcastReceiver extends BroadcastReceiver{
}else if(intent.getBooleanExtra(TransportConstants.PING_ROUTER_SERVICE_EXTRA, false)){
//We were told to wake up our router services
Log.d(TAG, "Starting router service off ping");
- didStart = wakeUpRouterService(context, false);
+ boolean altServiceWake = intent.getBooleanExtra(TransportConstants.BIND_REQUEST_TYPE_ALT_TRANSPORT, false);
+ didStart = wakeUpRouterService(context, false,altServiceWake );
}
@@ -122,7 +123,7 @@ public abstract class SdlBroadcastReceiver extends BroadcastReceiver{
if(!didStart){
Log.d(TAG, "Waking up router service");
- didStart = wakeUpRouterService(context, true);
+ didStart = wakeUpRouterService(context, true,false);
}
//So even though we started our own version, on some older phones we find that two services are started up so we want to make sure we send our version that we are working with
@@ -137,19 +138,29 @@ public abstract class SdlBroadcastReceiver extends BroadcastReceiver{
}
}
- private boolean wakeUpRouterService(Context context, boolean ping){
+ private boolean wakeUpRouterService(Context context, boolean ping, boolean altTransportWake){
Log.d(TAG, "Waking up router service");
- if(!isRouterServiceRunning(context, ping)){
+ if(!isRouterServiceRunning(context, ping)){
//If there isn't a service running we should try to start one
Log.i(TAG, "Attempting to start an instance of the Router Service");
//The under class should have implemented this....
//So let's start up our service since no copy is running
Intent serviceIntent = new Intent(context, localRouterClass);
+ if(altTransportWake){
+ serviceIntent.setAction(TransportConstants.BIND_REQUEST_TYPE_ALT_TRANSPORT);
+ }
context.startService(serviceIntent);
return true;
}else{
Log.i(TAG, "An instance of the Router Service is already running");
+ if(altTransportWake){
+ Intent serviceIntent = new Intent();
+ serviceIntent.setComponent(runningBluetoothServicePackage);
+ serviceIntent.setAction(TransportConstants.BIND_REQUEST_TYPE_ALT_TRANSPORT);
+ context.startService(serviceIntent);
+ return true;
+ }
return false;
}
}
@@ -171,7 +182,7 @@ public abstract class SdlBroadcastReceiver extends BroadcastReceiver{
//We will check to see if it contains this name, should be pretty specific
//Log.d(TAG, "Found Service: "+ service.service.getClassName());
if ((service.service.getClassName()).toLowerCase().contains(SDL_ROUTER_SERVICE_CLASS_NAME)) {
- runningBluetoothServicePackage = service.service.getPackageName(); //Store which instance is running
+ runningBluetoothServicePackage = service.service; //Store which instance is running
if(pingService){
Intent intent = new Intent();
intent.setClassName(service.service.getPackageName(), service.service.getClassName());
@@ -196,7 +207,7 @@ public abstract class SdlBroadcastReceiver extends BroadcastReceiver{
if(isRouterServiceRunning(context,false)){ //So there is a service up, let's see if it's connected
Context con;
try {
- con = context.createPackageContext(runningBluetoothServicePackage, 0);
+ con = context.createPackageContext(runningBluetoothServicePackage.getPackageName(), 0);
if(con==null ){
Log.w(TAG, "Unable to check for service connection. Returning false. "+runningBluetoothServicePackage);
return false; // =( well that sucks.
diff --git a/sdl_android_lib/src/com/smartdevicelink/transport/SdlRouterService.java b/sdl_android_lib/src/com/smartdevicelink/transport/SdlRouterService.java
index aaee74e47..46efb1322 100644
--- a/sdl_android_lib/src/com/smartdevicelink/transport/SdlRouterService.java
+++ b/sdl_android_lib/src/com/smartdevicelink/transport/SdlRouterService.java
@@ -98,10 +98,11 @@ public class SdlRouterService extends Service{
private static boolean closing = false;
private static Context currentContext = null;
- private Handler versionCheckTimeOutHandler;
- private Runnable versionCheckRunable;
+ private Handler versionCheckTimeOutHandler, altTransportTimerHandler;
+ private Runnable versionCheckRunable, altTransportTimerRunnable;
private LocalRouterService localCompareTo = null;
- private final static int VERSION_TIMEOUT_RUNNABLE = 750;
+ private final static int VERSION_TIMEOUT_RUNNABLE = 750;
+ private final static int ALT_TRANSPORT_TIMEOUT_RUNNABLE = 30000;
private boolean wrongProcess = false;
@@ -157,24 +158,26 @@ public class SdlRouterService extends Service{
//Log.enableBluetoothTraceLogging(receivedIntent.getBooleanExtra(LOG_TRACE_BT_DEBUG_BOOLEAN_EXTRA, false));
//Ok this is where we should do some authenticating...maybe.
//Should we ask for all relevant data in this packet?
+ if(BluetoothAdapter.getDefaultAdapter()!=null && BluetoothAdapter.getDefaultAdapter().isEnabled()){
+
+ if(startSequenceComplete &&
+ !connectAsClient && (mSerialService ==null
+ || mSerialService.getState() == MultiplexBluetoothTransport.STATE_NONE)){
+ Log.e(TAG, "Serial service not initliazed while registering app");
+ //Maybe we should try to do a connect here instead
+ Log.d(TAG, "Serial service being restarted");
+ if(mSerialService ==null){
+ Log.e(TAG, "Local copy of BT Server is null");
+ mSerialService = MultiplexBluetoothTransport.getBluetoothSerialServerInstance();
+ if(mSerialService==null){
+ Log.e(TAG, "Local copy of BT Server is still null and so is global");
+ mSerialService = MultiplexBluetoothTransport.getBluetoothSerialServerInstance(mHandlerBT);
- if(startSequenceComplete &&
- !connectAsClient && (mSerialService ==null
- || mSerialService.getState() == MultiplexBluetoothTransport.STATE_NONE)){
- Log.e(TAG, "Serial service not initliazed while registering app");
- //Maybe we should try to do a connect here instead
- Log.d(TAG, "Serial service being restarted");
- if(mSerialService ==null){
- Log.e(TAG, "Local copy of BT Server is null");
- mSerialService = MultiplexBluetoothTransport.getBluetoothSerialServerInstance();
- if(mSerialService==null){
- Log.e(TAG, "Local copy of BT Server is still null and so is global");
- mSerialService = MultiplexBluetoothTransport.getBluetoothSerialServerInstance(mHandlerBT);
-
+ }
}
- }
- mSerialService.start();
+ mSerialService.start();
+ }
}
Log.i(TAG, app.appId + " has just been registered with SDL Router Service");
@@ -244,7 +247,7 @@ public class SdlRouterService extends Service{
Log.d(TAG, "Bluetooth is shutting off, SDL Router Service is closing.");
//Since BT is shutting off...there's no reason for us to be on now.
//Let's take a break...I'm sleepy
- shouldServiceKeepRunning(intent);
+ shouldServiceRemainOpen(intent);
}
else{//So we just got d/c'ed from the bluetooth...alright...Let the client know
if(legacyModeEnabled){
@@ -503,7 +506,7 @@ public class SdlRouterService extends Service{
altTransportService = null;
storeConnectedStatus(false);
onTransportDisconnected(TransportType.valueOf(receivedBundle.getString(TransportConstants.HARDWARE_DISCONNECTED)));
- shouldServiceKeepRunning(null); //this will close the service if bluetooth is not available
+ shouldServiceRemainOpen(null); //this will close the service if bluetooth is not available
}
}else if(receivedBundle.containsKey(TransportConstants.HARDWARE_CONNECTED)){
Message retMsg = Message.obtain();
@@ -514,6 +517,13 @@ public class SdlRouterService extends Service{
break;
}
altTransportService = msg.replyTo;
+ //Clear out the timer to make sure the service knows we're good to go
+ if(altTransportTimerHandler!=null && altTransportTimerRunnable!=null){
+ altTransportTimerHandler.removeCallbacks(altTransportTimerRunnable);
+ }
+ altTransportTimerHandler = null;
+ altTransportTimerRunnable = null;
+
storeConnectedStatus(true);
//Let the alt transport know they are good to go
retMsg.arg1 = TransportConstants.ROUTER_REGISTER_ALT_TRANSPORT_RESPONSE_SUCESS;
@@ -676,8 +686,19 @@ public class SdlRouterService extends Service{
IntentFilter filter = new IntentFilter();
filter.addAction(REGISTER_WITH_ROUTER_ACTION);
registerReceiver(mainServiceReceiver,filter);
-
- if(!connectAsClient){initBluetoothSerialService();}
+
+ if(!connectAsClient){
+ if(bluetoothAvailable()){
+ initBluetoothSerialService();
+ }
+ }
+
+ if(altTransportTimerHandler!=null){
+ //There's an alt transport waiting for this service to be started
+ Intent intent = new Intent(TransportConstants.ALT_TRANSPORT_RECEIVER);
+ sendBroadcast(intent);
+ }
+
startSequenceComplete= true;
}
@@ -698,13 +719,18 @@ public class SdlRouterService extends Service{
}
}
}
- shouldServiceKeepRunning(intent);
+ shouldServiceRemainOpen(intent);
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy(){
if(versionCheckTimeOutHandler!=null){versionCheckTimeOutHandler.removeCallbacks(versionCheckRunable);}
+ if(altTransportTimerHandler!=null){
+ altTransportTimerHandler.removeCallbacks(versionCheckRunable);
+ altTransportTimerHandler = null;
+ versionCheckRunable = null;
+ }
Log.w(TAG, "Sdl Router Service Destroyed");
closing = true;
currentContext = null;
@@ -839,17 +865,19 @@ public class SdlRouterService extends Service{
* 3. If Bluetooth is off/NA shut down
* 4. Anything else
*/
- public boolean shouldServiceKeepRunning(Intent intent){
+ public boolean shouldServiceRemainOpen(Intent intent){
//Log.d(TAG, "Determining if this service should remain open");
- if(altTransportService!=null
- || (intent!=null && intent.hasExtra(TransportConstants.ALT_TRANSPORT_ADDRESS_EXTRA))){ //FIXME how to handle 'service starts'
+ if(altTransportService!=null || altTransportTimerHandler !=null){
//We have been started by an alt transport, we must remain open. "My life for Auir...."
Log.d(TAG, "Alt Transport connected, remaining open");
return true;
- }
- else if(!bluetoothAvailable()){//If bluetooth isn't on...there's nothing to see here
+ }else if(intent!=null && TransportConstants.BIND_REQUEST_TYPE_ALT_TRANSPORT.equals(intent.getAction())){
+ Log.i(TAG, "Received start intent with alt transprt request.");
+ startAltTransportTimer();
+ return true;
+ }else if(!bluetoothAvailable()){//If bluetooth isn't on...there's nothing to see here
//Bluetooth is off, we should shut down
Log.d(TAG, "Bluetooth not available, shutting down service");
closeSelf();
@@ -1391,6 +1419,22 @@ public class SdlRouterService extends Service{
versionCheckTimeOutHandler.postDelayed(versionCheckRunable, VERSION_TIMEOUT_RUNNABLE);
}
+ /**
+ * This method is used to check for the newest version of this class to make sure the latest and greatest is up and running.
+ * @param context
+ */
+ private void startAltTransportTimer(){
+ altTransportTimerHandler = new Handler();
+ altTransportTimerRunnable = new Runnable() {
+ public void run() {
+ altTransportTimerHandler = null;
+ altTransportTimerRunnable = null;
+ shouldServiceRemainOpen(null);
+ }
+ };
+ altTransportTimerHandler.postDelayed(altTransportTimerRunnable, ALT_TRANSPORT_TIMEOUT_RUNNABLE);
+ }
+
private Intent getLastReceivedStartIntent(){
return lastReceivedStartIntent;
}