11package com .smartdevicelink .transport ;
22
33import java .util .ArrayList ;
4+ import java .util .Collections ;
5+ import java .util .Comparator ;
46import java .util .List ;
57import java .util .Locale ;
68
@@ -40,7 +42,7 @@ public class RouterServiceValidator {
4042
4143 private static final String REQUEST_PREFIX = "https://woprjr.smartdevicelink.com/api/1/applications/queryTrustedRouters" ;
4244
43- private static final String DEFAULT_APP_LIST = "{\" response\" : {\" com.livio.sdl\" : { \" versionBlacklist\" :[] }, \" com.lexus.tcapp\" : { \" versionBlacklist\" :[] }, \" com.toyota.tcapp\" : { \" versionBlacklist\" : [] } , \" com.sdl.router\" :{\" versionBlacklist\" : [] } }}" ;
45+ 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 \" :[] } }}" ;
4446
4547
4648 private static final String JSON_RESPONSE_OBJECT_TAG = "response" ;
@@ -51,12 +53,14 @@ public class RouterServiceValidator {
5153 private static final String JSON_APP_VERSION_TAG = "version" ;
5254
5355
54- private static final long REFRESH_TRUSTED_APP_LIST_TIME = 3600000 * 24 ; // 24 hours in ms
56+ private static final long REFRESH_TRUSTED_APP_LIST_TIME = 3600000 * 24 * 7 ; // A week in ms
5557
5658 private static final String SDL = "sdl" ;
5759 private static final String SDL_PACKAGE_LIST = "sdl_package_list" ;
5860 private static final String SDL_PACKAGE_LIST_TIMESTAMP = "sdl_package_list_timestamp" ;
61+ private static final String SDL_LAST_REQUEST = "sdl_last_request" ;
5962
63+
6064 //Flags to aid in debugging and production checks
6165 public static final int FLAG_DEBUG_NONE = 0x00 ;
6266 public static final int FLAG_DEBUG_PACKAGE_CHECK = 0x01 ;
@@ -320,6 +324,13 @@ private static List<SdlApp> findAllSdlApps(Context context){
320324 Intent intent = new Intent ();
321325 intent .setAction ("sdl.router.startservice" );
322326 List <ResolveInfo > infoList = packageManager .queryBroadcastReceivers (intent , 0 );
327+ //We want to sort our list so that we know it's the same everytime
328+ Collections .sort (infoList ,new Comparator <ResolveInfo >() {
329+ @ Override
330+ public int compare (ResolveInfo lhs , ResolveInfo rhs ) {
331+ return lhs .activityInfo .packageName .compareTo (rhs .activityInfo .packageName );
332+ }
333+ });
323334 if (infoList !=null ){
324335 String packageName ;
325336 for (ResolveInfo info : infoList ){
@@ -361,23 +372,14 @@ protected static boolean createTrustedListRequest(final Context context, boolean
361372 return false ;
362373 }
363374
364- if (!forceRefresh && (System .currentTimeMillis ()-getTrustedAppListTimeStamp (context ))<REFRESH_TRUSTED_APP_LIST_TIME ){
365- //Our list should still be ok for now so we will skip the request
366- pendingListRefresh = false ;
367- if (listCallback !=null ){
368- listCallback .onListObtained (true );
369- }
370- return false ;
371- }
372-
373375 pendingListRefresh = true ;
374376 //Might want to store a flag letting this class know a request is currently pending
375377 StringBuilder builder = new StringBuilder ();
376378 builder .append (REQUEST_PREFIX );
377379
378380 List <SdlApp > apps = findAllSdlApps (context );
379381
380- JSONObject object = new JSONObject ();
382+ final JSONObject object = new JSONObject ();
381383 JSONArray array = new JSONArray ();
382384 JSONObject jsonApp ;
383385
@@ -395,6 +397,19 @@ protected static boolean createTrustedListRequest(final Context context, boolean
395397
396398 try {object .put (JSON_PUT_ARRAY_TAG , array );} catch (JSONException e ) {e .printStackTrace ();}
397399
400+ if (!forceRefresh && (System .currentTimeMillis ()-getTrustedAppListTimeStamp (context ))<REFRESH_TRUSTED_APP_LIST_TIME ){
401+ if (object .toString ().equals (getLastRequest (context ))){
402+ //Our list should still be ok for now so we will skip the request
403+ pendingListRefresh = false ;
404+ if (listCallback !=null ){
405+ listCallback .onListObtained (true );
406+ }
407+ return false ;
408+ }else {
409+ Log .d (TAG , "Sdl apps have changed. Need to request new trusted router service list." );
410+ }
411+ }
412+
398413 if (cb == null ) {
399414 cb = new HttpRequestTaskCallback () {
400415
@@ -403,6 +418,7 @@ public void httpCallComplete(String response) {
403418 // Might want to check if this list is ok
404419 //Log.d(TAG, "APPS! " + response);
405420 setTrustedList (context , response );
421+ setLastRequest (context , object .toString ()); //Save our last request
406422 pendingListRefresh = false ;
407423 if (listCallback !=null ){listCallback .onListObtained (true );}
408424 }
@@ -522,8 +538,28 @@ protected static Long getTrustedAppListTimeStamp(Context context){
522538 return -1L ;
523539 }
524540
541+ protected static boolean setLastRequest (Context context , String request ){
542+ if (context !=null ){
543+ SharedPreferences pref = context .getSharedPreferences (SDL , Context .MODE_PRIVATE );
544+ SharedPreferences .Editor prefAdd = pref .edit ();
545+ prefAdd .putString (SDL_LAST_REQUEST , request );
546+ return prefAdd .commit ();
547+ }
548+ return false ;
549+ }
525550
526-
551+ /**
552+ * Gets the last request JSON object we sent to the RSVP server. It basically contains a list of sdl enabled apps
553+ * @param context
554+ * @return
555+ */
556+ protected static String getLastRequest (Context context ){
557+ if (context !=null ){
558+ SharedPreferences pref = context .getSharedPreferences (SDL , Context .MODE_PRIVATE );
559+ return pref .getString (SDL_LAST_REQUEST , null );
560+ }
561+ return null ;
562+ }
527563 /**
528564 * Class that holds all the info we want to send/receive from the validation server
529565 */
0 commit comments