diff options
author | Joey Grover <joeygrover@gmail.com> | 2016-12-13 13:34:54 -0500 |
---|---|---|
committer | Joey Grover <joeygrover@gmail.com> | 2016-12-13 13:34:54 -0500 |
commit | a2264a5e013efd121541b5f895da85805615bcba (patch) | |
tree | 3c20c8a49d0a61374789b94cb981e9ca6c604139 | |
parent | 77a2be92877a040f7531e06b7cc50048a2d9738f (diff) | |
parent | 082c4cd2b664060e99937ccf6b4086aa8b02e38a (diff) | |
download | sdl_android-a2264a5e013efd121541b5f895da85805615bcba.tar.gz |
Merge branch 'bugfix/issue_371' of https://github.com/smartdevicelink/sdl_android into develop
6 files changed, 112 insertions, 34 deletions
diff --git a/sdl_android_lib/src/com/smartdevicelink/SdlConnection/SdlConnection.java b/sdl_android_lib/src/com/smartdevicelink/SdlConnection/SdlConnection.java index c9a5c13f2..3c51b9e7e 100644 --- a/sdl_android_lib/src/com/smartdevicelink/SdlConnection/SdlConnection.java +++ b/sdl_android_lib/src/com/smartdevicelink/SdlConnection/SdlConnection.java @@ -556,12 +556,19 @@ public class SdlConnection implements IProtocolListener, ITransportListener { if(cachedMultiConfig!=null){
//We are in legacy mode, but just received a force connect. The router service should never be pointing us here if we are truely in legacy mode
ComponentName tempCompName = SdlBroadcastReceiver.consumeQueuedRouterService();
- cachedMultiConfig.setService(tempCompName);
- //We are not connected yet so we should be able to close down
- _transport.disconnect(); //This will force us into the
+ RouterServiceValidator vlad = new RouterServiceValidator(cachedMultiConfig.getContext(),tempCompName);
+ if(vlad.validate()){
+ cachedMultiConfig.setService(tempCompName);
+ //We are not connected yet so we should be able to close down
+ _transport.disconnect(); //This will force us into the
+ }else{
+ //Log.d(TAG, "Router service not trusted during force connect. Ignoring.");
+ return;
+ }
}else{
- Log.i(TAG, "No cached multiplexing config, transport error being called");
- _transport.disconnect();
+ //Log.i(TAG, "No cached multiplexing config, ignoring");
+ //_transport.disconnect();
+ return;
}
Log.w(TAG, "Using own transport, but not connected. Attempting to join multiplexing");
}else{
diff --git a/sdl_android_lib/src/com/smartdevicelink/transport/RouterServiceValidator.java b/sdl_android_lib/src/com/smartdevicelink/transport/RouterServiceValidator.java index 5c7027180..081fb01c8 100644 --- a/sdl_android_lib/src/com/smartdevicelink/transport/RouterServiceValidator.java +++ b/sdl_android_lib/src/com/smartdevicelink/transport/RouterServiceValidator.java @@ -1,6 +1,8 @@ package com.smartdevicelink.transport; import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; import java.util.List; import java.util.Locale; @@ -40,7 +42,7 @@ public class RouterServiceValidator { private static final String REQUEST_PREFIX = "https://woprjr.smartdevicelink.com/api/1/applications/queryTrustedRouters"; - private static final String DEFAULT_APP_LIST = "{\"response\": {\"com.livio.sdl\" : { \"versionBlacklist\":[] }, \"com.lexus.tcapp\" : { \"versionBlacklist\":[] }, \"com.toyota.tcapp\" : { \"versionBlacklist\": [] } , \"com.sdl.router\":{\"versionBlacklist\": [] } }}"; + private static final String DEFAULT_APP_LIST = "{\"response\": {\"com.livio.sdl\" : { \"versionBlacklist\":[] }, \"com.lexus.tcapp\" : { \"versionBlacklist\":[] }, \"com.toyota.tcapp\" : { \"versionBlacklist\": [] } , \"com.sdl.router\":{\"versionBlacklist\": [] },\"com.ford.fordpass\" : { \"versionBlacklist\":[] } }}"; private static final String JSON_RESPONSE_OBJECT_TAG = "response"; @@ -51,12 +53,14 @@ public class RouterServiceValidator { private static final String JSON_APP_VERSION_TAG = "version"; - private static final long REFRESH_TRUSTED_APP_LIST_TIME = 3600000 * 24; // 24 hours in ms + private static final long REFRESH_TRUSTED_APP_LIST_TIME = 3600000 * 24 * 7; // A week in ms private static final String SDL = "sdl"; private static final String SDL_PACKAGE_LIST = "sdl_package_list"; private static final String SDL_PACKAGE_LIST_TIMESTAMP = "sdl_package_list_timestamp"; + private static final String SDL_LAST_REQUEST = "sdl_last_request"; + //Flags to aid in debugging and production checks public static final int FLAG_DEBUG_NONE = 0x00; public static final int FLAG_DEBUG_PACKAGE_CHECK = 0x01; @@ -320,6 +324,13 @@ public class RouterServiceValidator { Intent intent = new Intent(); intent.setAction("sdl.router.startservice"); List<ResolveInfo> infoList = packageManager.queryBroadcastReceivers(intent, 0); + //We want to sort our list so that we know it's the same everytime + Collections.sort(infoList,new Comparator<ResolveInfo>() { + @Override + public int compare(ResolveInfo lhs, ResolveInfo rhs) { + return lhs.activityInfo.packageName.compareTo(rhs.activityInfo.packageName); + } + }); if(infoList!=null){ String packageName; for(ResolveInfo info : infoList){ @@ -361,15 +372,6 @@ public class RouterServiceValidator { return false; } - if(!forceRefresh && (System.currentTimeMillis()-getTrustedAppListTimeStamp(context))<REFRESH_TRUSTED_APP_LIST_TIME){ - //Our list should still be ok for now so we will skip the request - pendingListRefresh = false; - if(listCallback!=null){ - listCallback.onListObtained(true); - } - return false; - } - pendingListRefresh = true; //Might want to store a flag letting this class know a request is currently pending StringBuilder builder = new StringBuilder(); @@ -377,7 +379,7 @@ public class RouterServiceValidator { List<SdlApp> apps = findAllSdlApps(context); - JSONObject object = new JSONObject(); + final JSONObject object = new JSONObject(); JSONArray array = new JSONArray(); JSONObject jsonApp; @@ -395,6 +397,19 @@ public class RouterServiceValidator { try {object.put(JSON_PUT_ARRAY_TAG, array);} catch (JSONException e) {e.printStackTrace();} + if(!forceRefresh && (System.currentTimeMillis()-getTrustedAppListTimeStamp(context))<REFRESH_TRUSTED_APP_LIST_TIME){ + if(object.toString().equals(getLastRequest(context))){ + //Our list should still be ok for now so we will skip the request + pendingListRefresh = false; + if(listCallback!=null){ + listCallback.onListObtained(true); + } + return false; + }else{ + Log.d(TAG, "Sdl apps have changed. Need to request new trusted router service list."); + } + } + if (cb == null) { cb = new HttpRequestTaskCallback() { @@ -403,6 +418,7 @@ public class RouterServiceValidator { // Might want to check if this list is ok //Log.d(TAG, "APPS! " + response); setTrustedList(context, response); + setLastRequest(context, object.toString()); //Save our last request pendingListRefresh = false; if(listCallback!=null){listCallback.onListObtained(true);} } @@ -522,8 +538,28 @@ public class RouterServiceValidator { return -1L; } + protected static boolean setLastRequest(Context context, String request){ + if(context!=null){ + SharedPreferences pref = context.getSharedPreferences(SDL, Context.MODE_PRIVATE); + SharedPreferences.Editor prefAdd = pref.edit(); + prefAdd.putString(SDL_LAST_REQUEST, request); + return prefAdd.commit(); + } + return false; + } - + /** + * Gets the last request JSON object we sent to the RSVP server. It basically contains a list of sdl enabled apps + * @param context + * @return + */ + protected static String getLastRequest(Context context){ + if(context!=null){ + SharedPreferences pref = context.getSharedPreferences(SDL, Context.MODE_PRIVATE); + return pref.getString(SDL_LAST_REQUEST, null); + } + return null; + } /** * Class that holds all the info we want to send/receive from the validation server */ diff --git a/sdl_android_lib/src/com/smartdevicelink/transport/SdlBroadcastReceiver.java b/sdl_android_lib/src/com/smartdevicelink/transport/SdlBroadcastReceiver.java index fa243c585..17bb3fd54 100644 --- a/sdl_android_lib/src/com/smartdevicelink/transport/SdlBroadcastReceiver.java +++ b/sdl_android_lib/src/com/smartdevicelink/transport/SdlBroadcastReceiver.java @@ -89,15 +89,13 @@ public abstract class SdlBroadcastReceiver extends BroadcastReceiver{ @Override public void onListObtained(boolean successful) { //Log.v(TAG, "SDL enabled by router service from " + packageName + " compnent package " + componentName.getPackageName() + " - " + componentName.getClassName()); + //List obtained. Let's start our service + queuedService = componentName; + finalIntent.setAction("com.sdl.noaction"); //Replace what's there so we do go into some unintended loop + //Validate the router service so the service knows if this is a trusted router service RouterServiceValidator vlad = new RouterServiceValidator(finalContext,componentName); - if(vlad.validate()){ - //Log.d(TAG, "Router service trusted!"); - queuedService = componentName; - finalIntent.setAction("com.sdl.noaction"); //Replace what's there so we do go into some unintended loop - onSdlEnabled(finalContext, finalIntent); - }else{ - Log.w(TAG, "RouterService was not trusted. Ignoring intent from : "+ componentName.getClassName()); - } + finalIntent.putExtra(TransportConstants.ROUTER_SERVICE_VALIDATED, vlad.validate()); + onSdlEnabled(finalContext, finalIntent); } }); @@ -123,18 +121,15 @@ public abstract class SdlBroadcastReceiver extends BroadcastReceiver{ if (intent.getAction().contains("android.bluetooth.adapter.action.STATE_CHANGED")){ int state = intent.getIntExtra("android.bluetooth.adapter.extra.STATE",-1); if (state == BluetoothAdapter.STATE_OFF || - state == BluetoothAdapter.STATE_TURNING_OFF ){ + state == BluetoothAdapter.STATE_TURNING_OFF){ //onProtocolDisabled(context); //Let's let the service that is running manage what to do for this //If we were to do it here, for every instance of this BR it would send //an intent to stop service, where it's only one that is needed. return; - }else if(state == BluetoothAdapter.STATE_TURNING_ON){ - //We started bluetooth, we should check for a new valid router list - RouterServiceValidator.createTrustedListRequest(context,true); } } - + if(localRouterClass!=null){ //If there is a supplied router service lets run some logic regarding starting one if(!didStart){ diff --git a/sdl_android_lib/src/com/smartdevicelink/transport/TransportConstants.java b/sdl_android_lib/src/com/smartdevicelink/transport/TransportConstants.java index 612b5775a..3e3421af9 100644 --- a/sdl_android_lib/src/com/smartdevicelink/transport/TransportConstants.java +++ b/sdl_android_lib/src/com/smartdevicelink/transport/TransportConstants.java @@ -27,6 +27,8 @@ public class TransportConstants { public static final String START_ROUTER_SERVICE_SDL_ENABLED_CMP_NAME = "component_name"; public static final String START_ROUTER_SERVICE_SDL_ENABLED_PING = "ping"; public static final String FORCE_TRANSPORT_CONNECTED = "force_connect"; //This is legacy, do not refactor this. + public static final String ROUTER_SERVICE_VALIDATED = "router_service_validated"; + public static final String REPLY_TO_INTENT_EXTRA = "ReplyAddress"; public static final String CONNECT_AS_CLIENT_BOOLEAN_EXTRA = "connectAsClient"; diff --git a/sdl_android_tests/AndroidManifest.xml b/sdl_android_tests/AndroidManifest.xml index 85ad00245..87a9fc7c3 100644 --- a/sdl_android_tests/AndroidManifest.xml +++ b/sdl_android_tests/AndroidManifest.xml @@ -15,7 +15,7 @@ <instrumentation android:name="android.test.InstrumentationTestRunner" - android:targetPackage="com.smartdevicelink" /> + android:targetPackage="com.smartdevicelink.test" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <application diff --git a/sdl_android_tests/src/com/smartdevicelink/transport/RSVTestCase.java b/sdl_android_tests/src/com/smartdevicelink/transport/RSVTestCase.java index 8fda666e0..824071b8b 100644 --- a/sdl_android_tests/src/com/smartdevicelink/transport/RSVTestCase.java +++ b/sdl_android_tests/src/com/smartdevicelink/transport/RSVTestCase.java @@ -3,6 +3,8 @@ package com.smartdevicelink.transport; import java.util.List; import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; @@ -93,7 +95,7 @@ public class RSVTestCase extends AndroidTestCase { assertFalse(RouterServiceValidator.setTrustedList(mContext,null)); assertFalse(RouterServiceValidator.setTrustedList(null,"test")); assertTrue(RouterServiceValidator.setTrustedList(mContext,"test")); - String test= "{\"response\": {\"com.livio.sdl\" : { \"versionBlacklist\":[] }, \"com.lexus.tcapp\" : { \"versionBlacklist\":[] }, \"com.toyota.tcapp\" : { \"versionBlacklist\": [] } , \"com.sdl.router\":{\"versionBlacklist\": [] } }}"; + String test = "{\"response\": {\"com.livio.sdl\" : { \"versionBlacklist\":[] }, \"com.lexus.tcapp\" : { \"versionBlacklist\":[] }, \"com.toyota.tcapp\" : { \"versionBlacklist\": [] } , \"com.sdl.router\":{\"versionBlacklist\": [] },\"com.ford.fordpass\" : { \"versionBlacklist\":[] } }}"; assertTrue(RouterServiceValidator.setTrustedList(mContext,test)); assertTrue(RouterServiceValidator.setTrustedList(mContext,test+test+test+test+test)); StringBuilder builder = new StringBuilder(); @@ -104,7 +106,7 @@ public class RSVTestCase extends AndroidTestCase { } public void testTrustedListSetAndGet(){ - String test= "{\"response\": {\"com.livio.sdl\" : { \"versionBlacklist\":[] }, \"com.lexus.tcapp\" : { \"versionBlacklist\":[] }, \"com.toyota.tcapp\" : { \"versionBlacklist\": [] } , \"com.sdl.router\":{\"versionBlacklist\": [] } }}"; + String test = "{\"response\": {\"com.livio.sdl\" : { \"versionBlacklist\":[] }, \"com.lexus.tcapp\" : { \"versionBlacklist\":[] }, \"com.toyota.tcapp\" : { \"versionBlacklist\": [] } , \"com.sdl.router\":{\"versionBlacklist\": [] },\"com.ford.fordpass\" : { \"versionBlacklist\":[] } }}"; assertTrue(RouterServiceValidator.setTrustedList(mContext,test)); String retVal = RouterServiceValidator.getTrustedList(mContext); assertNotNull(retVal); @@ -203,5 +205,41 @@ public class RSVTestCase extends AndroidTestCase { } + /** + * Test to check that we can save our last request which actually houses all the previous known sdl enabled apps + */ + public void testRequestChange(){ + assertNull(RouterServiceValidator.getLastRequest(mContext)); + String test = "{\"response\": {\"com.livio.sdl\" : { \"versionBlacklist\":[] }, \"com.lexus.tcapp\" : { \"versionBlacklist\":[] }, \"com.toyota.tcapp\" : { \"versionBlacklist\": [] } , \"com.sdl.router\":{\"versionBlacklist\": [] },\"com.ford.fordpass\" : { \"versionBlacklist\":[] } }}"; + JSONObject object = null; + try { + object = new JSONObject(test); + } catch (JSONException e) { + e.printStackTrace(); + } + assertNotNull(object); + assertFalse(object.equals(RouterServiceValidator.getLastRequest(mContext))); + + assertTrue(RouterServiceValidator.setLastRequest(mContext, object.toString())); + + String oldRequest = RouterServiceValidator.getLastRequest(mContext); + assertNotNull(oldRequest); + assertTrue(object.toString().equals(oldRequest)); + + //Now test a new list + test = "{\"response\": {\"com.livio.sdl\" : { \"versionBlacklist\":[] }, \"com.lexus.tcapp\" : { \"versionBlacklist\":[] }, \"com.test.test\" : { \"versionBlacklist\":[] },\"com.toyota.tcapp\" : { \"versionBlacklist\": [] } , \"com.sdl.router\":{\"versionBlacklist\": [] },\"com.ford.fordpass\" : { \"versionBlacklist\":[] } }}"; + object = null; + try { + object = new JSONObject(test); + } catch (JSONException e) { + e.printStackTrace(); + } + assertNotNull(object); + assertFalse(object.equals(RouterServiceValidator.getLastRequest(mContext))); + //Clear it for next test + RouterServiceValidator.setLastRequest(mContext, null); + + } + } |