1
1
package com .smartdevicelink .transport ;
2
2
3
3
import java .util .ArrayList ;
4
+ import java .util .Collections ;
5
+ import java .util .Comparator ;
4
6
import java .util .List ;
5
7
import java .util .Locale ;
6
8
@@ -40,7 +42,7 @@ public class RouterServiceValidator {
40
42
41
43
private static final String REQUEST_PREFIX = "https://woprjr.smartdevicelink.com/api/1/applications/queryTrustedRouters" ;
42
44
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 \" :[] } }}" ;
44
46
45
47
46
48
private static final String JSON_RESPONSE_OBJECT_TAG = "response" ;
@@ -51,12 +53,14 @@ public class RouterServiceValidator {
51
53
private static final String JSON_APP_VERSION_TAG = "version" ;
52
54
53
55
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
55
57
56
58
private static final String SDL = "sdl" ;
57
59
private static final String SDL_PACKAGE_LIST = "sdl_package_list" ;
58
60
private static final String SDL_PACKAGE_LIST_TIMESTAMP = "sdl_package_list_timestamp" ;
61
+ private static final String SDL_LAST_REQUEST = "sdl_last_request" ;
59
62
63
+
60
64
//Flags to aid in debugging and production checks
61
65
public static final int FLAG_DEBUG_NONE = 0x00 ;
62
66
public static final int FLAG_DEBUG_PACKAGE_CHECK = 0x01 ;
@@ -320,6 +324,13 @@ private static List<SdlApp> findAllSdlApps(Context context){
320
324
Intent intent = new Intent ();
321
325
intent .setAction ("sdl.router.startservice" );
322
326
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
+ });
323
334
if (infoList !=null ){
324
335
String packageName ;
325
336
for (ResolveInfo info : infoList ){
@@ -361,23 +372,14 @@ protected static boolean createTrustedListRequest(final Context context, boolean
361
372
return false ;
362
373
}
363
374
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
-
373
375
pendingListRefresh = true ;
374
376
//Might want to store a flag letting this class know a request is currently pending
375
377
StringBuilder builder = new StringBuilder ();
376
378
builder .append (REQUEST_PREFIX );
377
379
378
380
List <SdlApp > apps = findAllSdlApps (context );
379
381
380
- JSONObject object = new JSONObject ();
382
+ final JSONObject object = new JSONObject ();
381
383
JSONArray array = new JSONArray ();
382
384
JSONObject jsonApp ;
383
385
@@ -395,6 +397,19 @@ protected static boolean createTrustedListRequest(final Context context, boolean
395
397
396
398
try {object .put (JSON_PUT_ARRAY_TAG , array );} catch (JSONException e ) {e .printStackTrace ();}
397
399
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
+
398
413
if (cb == null ) {
399
414
cb = new HttpRequestTaskCallback () {
400
415
@@ -403,6 +418,7 @@ public void httpCallComplete(String response) {
403
418
// Might want to check if this list is ok
404
419
//Log.d(TAG, "APPS! " + response);
405
420
setTrustedList (context , response );
421
+ setLastRequest (context , object .toString ()); //Save our last request
406
422
pendingListRefresh = false ;
407
423
if (listCallback !=null ){listCallback .onListObtained (true );}
408
424
}
@@ -522,8 +538,28 @@ protected static Long getTrustedAppListTimeStamp(Context context){
522
538
return -1L ;
523
539
}
524
540
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
+ }
525
550
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
+ }
527
563
/**
528
564
* Class that holds all the info we want to send/receive from the validation server
529
565
*/
0 commit comments