From bcf18f2218565b9fe4c45fcebe00956ad1cab05c Mon Sep 17 00:00:00 2001 From: Tuan Nguyen Date: Fri, 19 Jan 2018 14:43:39 -0800 Subject: #665 Fix comment/spacing, remove redundant variable selfValidationLevel, set swapping security level before attempting to validate in swap mode in SdlRouterService - Add swapping validation variables, methods to set security level and set swapping mode in RouterServiceValidator - Modify validate method to work with swapping mode - Modify shouldOverrideInstalledFrom method to work with swapping mode --- .../transport/RouterServiceValidator.java | 47 +++++++++++++++++----- .../transport/SdlRouterService.java | 20 ++++----- 2 files changed, 44 insertions(+), 23 deletions(-) diff --git a/sdl_android/src/main/java/com/smartdevicelink/transport/RouterServiceValidator.java b/sdl_android/src/main/java/com/smartdevicelink/transport/RouterServiceValidator.java index 92dce9dbe..4ec44cc39 100644 --- a/sdl_android/src/main/java/com/smartdevicelink/transport/RouterServiceValidator.java +++ b/sdl_android/src/main/java/com/smartdevicelink/transport/RouterServiceValidator.java @@ -87,6 +87,10 @@ public class RouterServiceValidator { private ComponentName service;//This is how we can save different routers over another in a waterfall method if we choose to. private static int securityLevel = -1; + + // router swapping validation variables + private int mSwappingSecurityLevel; + private boolean mIsSwappingMode; public RouterServiceValidator(Context context){ this.context = context; @@ -98,25 +102,43 @@ public class RouterServiceValidator { inDebugMode = inDebugMode(); this.service = service; } + + /** + * Sets the security level for swapping validation purpose + * @param level the security level to be set + */ + public void setSwappingSecurityLevel(int level) { + mSwappingSecurityLevel = level; + } + + /** + * Sets this class in swapping validation mode + * @param isSwapMode true if in swapping validation mode; false otherwise. + */ + public void setSwappingMode(boolean isSwapMode) { + mIsSwappingMode = isSwapMode; + } + /** * Main function to call to ensure we are connecting to a validated router service * @return whether or not the currently running router service can be trusted. */ public boolean validate(){ - - if(securityLevel == -1){ - securityLevel = getSecurityLevel(context); - } - - if(securityLevel == MultiplexTransportConfig.FLAG_MULTI_SECURITY_OFF){ //If security isn't an issue, just return true; - return true; + if (!mIsSwappingMode) { + if (securityLevel == -1) { + securityLevel = getSecurityLevel(context); + } + + if (securityLevel == MultiplexTransportConfig.FLAG_MULTI_SECURITY_OFF) { //If security isn't an issue, just return true; + return true; + } } PackageManager pm = context.getPackageManager(); //Grab the package for the currently running router service. We need this call regardless of if we are in debug mode or not. String packageName = null; - if(this.service != null){ + if(!mIsSwappingMode && this.service != null){ Log.d(TAG, "Supplied service name of " + this.service.getClassName()); if(Build.VERSION.SDK_INT < Build.VERSION_CODES.O && !isServiceRunning(context,this.service)){ //This means our service isn't actually running, so set to null. Hopefully we can find a real router service after this. @@ -129,7 +151,7 @@ public class RouterServiceValidator { } } } - if(this.service == null){ + if(!mIsSwappingMode && this.service == null){ if(Build.VERSION.SDK_INT < Build.VERSION_CODES.O ) { this.service = componentNameForServiceRunning(pm); //Change this to an array if multiple services are started? if (this.service == null) { //if this is still null we know there is no service running so we can return false @@ -155,7 +177,9 @@ public class RouterServiceValidator { } }//No running service found. Might need to attempt to start one //TODO spin up a known good router service - wakeUpRouterServices(); + if (!mIsSwappingMode) { + wakeUpRouterServices(); + } return false; } @@ -182,7 +206,8 @@ public class RouterServiceValidator { } private boolean shouldOverrideInstalledFrom(){ - return securityLevel< MultiplexTransportConfig.FLAG_MULTI_SECURITY_HIGH + int currentSecLevel = (mIsSwappingMode ? mSwappingSecurityLevel : securityLevel); + return currentSecLevel< MultiplexTransportConfig.FLAG_MULTI_SECURITY_HIGH || (this.inDebugMode && ((this.flags & FLAG_DEBUG_INSTALLED_FROM_CHECK) != FLAG_DEBUG_INSTALLED_FROM_CHECK)); } diff --git a/sdl_android/src/main/java/com/smartdevicelink/transport/SdlRouterService.java b/sdl_android/src/main/java/com/smartdevicelink/transport/SdlRouterService.java index 2757b1b5c..29032c44b 100644 --- a/sdl_android/src/main/java/com/smartdevicelink/transport/SdlRouterService.java +++ b/sdl_android/src/main/java/com/smartdevicelink/transport/SdlRouterService.java @@ -165,8 +165,6 @@ public class SdlRouterService extends Service{ private boolean isPingingClients = false; int pingCount = 0; - // swapping variables - private int selfValidationLevel; /* ************************************************************************************************************************************** **************************************************************************************************************************************** @@ -1086,7 +1084,7 @@ public class SdlRouterService extends Service{ } /** - * For each app (services) installed in the device, find the services that validate at the highest level that is greater than the current + * For each app (service) installed in the device, find the services that validate at the highest level that is greater than the current * level that this service is already validated at. * If there is at least one such service, attempt to swap with this service. */ @@ -1096,13 +1094,12 @@ public class SdlRouterService extends Service{ return; } - int selfValidationLevel = getLevelOfValidation(new ComponentName(this, this.getClass()), MultiplexTransportConfig.FLAG_MULTI_SECURITY_OFF); + int baseValidationLevel = getLevelOfValidation(new ComponentName(this, this.getClass()), MultiplexTransportConfig.FLAG_MULTI_SECURITY_OFF); // return as this service already validates at HIGH security setting level, no other service is better if (selfValidationLevel == MultiplexTransportConfig.FLAG_MULTI_SECURITY_HIGH) { return; } - int baseValidationLevel = selfValidationLevel; TreeMap> validatedApps = new TreeMap<>(); // assume that app developers place SdlReceiver.java and SdlRouterService.java in the same package // see more at https://github.com/smartdevicelink/sdl_android/issues/648 @@ -1135,16 +1132,15 @@ public class SdlRouterService extends Service{ * @see RouterServiceValidator#validate() * @param cn ComponentName, the service to be validated * @param baseLineSecSetting the minimum security setting limit - * @return the level of security setting (greate than the base line) this service is validated at or the default security off level + * @return the highest level of security setting the given service is validated at or the default security off level */ private int getLevelOfValidation (ComponentName cn, int baseLineSecSetting) { RouterServiceValidator rsv = new RouterServiceValidator(this, cn); + rsv.setSwappingMode(true); int securityLevel = MultiplexTransportConfig.FLAG_MULTI_SECURITY_HIGH; while (securityLevel > baseLineSecSetting) { Log.v(TAG, "SdlRouterService swap, getLevelOfValidation, app to validate: " + cn.getPackageName() + ", security level: " + securityLevel); - // TODO: set rsv in swap mode - // TODO: set rsv security setting for each iteration - // rsv.setSwappingSecurityLevel(securityLevel); + rsv.setSwappingSecurityLevel(securityLevel); if (rsv.validate()) { Log.v(TAG, "SdlRouterService swap, getLevelOfValidation, validated with package name: " + cn.getPackageName() + ", security level: " + securityLevel); return securityLevel; @@ -1157,7 +1153,7 @@ public class SdlRouterService extends Service{ /** - * Start a service using the most recently updated app from the list of ResolveInfo objects and kill self when done + * Starts a service using the most recently updated app from the list of ResolveInfo objects and kill self when done * @param routersList list of possible router services to be started */ private void swapRouter(List routersList) { @@ -1170,9 +1166,9 @@ public class SdlRouterService extends Service{ closeBluetoothSerialServer(); this.startService(i); notifyAltTransportOfClose(TransportConstants.ROUTER_SHUTTING_DOWN_REASON_NEWER_SERVICE); - if(getBaseContext()!=null){ + if (getBaseContext()!=null) { stopSelf(); - }else{ + } else { onDestroy(); } } -- cgit v1.2.1