diff options
author | Joey Grover <joeygrover@gmail.com> | 2016-12-07 18:34:28 -0500 |
---|---|---|
committer | Joey Grover <joeygrover@gmail.com> | 2016-12-07 18:34:28 -0500 |
commit | b9b7e29a9de424af608719a5ea88f54fe2a651b1 (patch) | |
tree | f021defc88047cb0133817cf53a07703961b3cbb | |
parent | b2a58e6c7bc6353a2e80a10638531eeebb170b50 (diff) | |
download | sdl_android-b9b7e29a9de424af608719a5ea88f54fe2a651b1.tar.gz |
Added ability to save last reqest sent to RSVP server.
If we are not facing a cached list timeout but our request info is different than what was cached, we will perform the request.
-rw-r--r-- | sdl_android_lib/src/com/smartdevicelink/transport/RouterServiceValidator.java | 58 | ||||
-rw-r--r-- | sdl_android_tests/src/com/smartdevicelink/transport/RSVTestCase.java | 38 |
2 files changed, 85 insertions, 11 deletions
diff --git a/sdl_android_lib/src/com/smartdevicelink/transport/RouterServiceValidator.java b/sdl_android_lib/src/com/smartdevicelink/transport/RouterServiceValidator.java index e2ac95a5f..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; @@ -56,7 +58,9 @@ public class RouterServiceValidator { 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_tests/src/com/smartdevicelink/transport/RSVTestCase.java b/sdl_android_tests/src/com/smartdevicelink/transport/RSVTestCase.java index 4066c23a7..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; @@ -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); + + } + } |