Skip to content

Commit fa3031b

Browse files
authored
Merge pull request #372 from smartdevicelink/bugfix/issue_371
[Hotfix] RSVP Improvements
2 parents 65dc8c5 + 082c4cd commit fa3031b

File tree

6 files changed

+112
-34
lines changed

6 files changed

+112
-34
lines changed

sdl_android_lib/src/com/smartdevicelink/SdlConnection/SdlConnection.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -556,12 +556,19 @@ public void forceHardwareConnectEvent(TransportType type){
556556
if(cachedMultiConfig!=null){
557557
//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
558558
ComponentName tempCompName = SdlBroadcastReceiver.consumeQueuedRouterService();
559-
cachedMultiConfig.setService(tempCompName);
560-
//We are not connected yet so we should be able to close down
561-
_transport.disconnect(); //This will force us into the
559+
RouterServiceValidator vlad = new RouterServiceValidator(cachedMultiConfig.getContext(),tempCompName);
560+
if(vlad.validate()){
561+
cachedMultiConfig.setService(tempCompName);
562+
//We are not connected yet so we should be able to close down
563+
_transport.disconnect(); //This will force us into the
564+
}else{
565+
//Log.d(TAG, "Router service not trusted during force connect. Ignoring.");
566+
return;
567+
}
562568
}else{
563-
Log.i(TAG, "No cached multiplexing config, transport error being called");
564-
_transport.disconnect();
569+
//Log.i(TAG, "No cached multiplexing config, ignoring");
570+
//_transport.disconnect();
571+
return;
565572
}
566573
Log.w(TAG, "Using own transport, but not connected. Attempting to join multiplexing");
567574
}else{

sdl_android_lib/src/com/smartdevicelink/transport/RouterServiceValidator.java

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.smartdevicelink.transport;
22

33
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.Comparator;
46
import java.util.List;
57
import 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
*/

sdl_android_lib/src/com/smartdevicelink/transport/SdlBroadcastReceiver.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,13 @@ public void onReceive(Context context, Intent intent) {
8989
@Override
9090
public void onListObtained(boolean successful) {
9191
//Log.v(TAG, "SDL enabled by router service from " + packageName + " compnent package " + componentName.getPackageName() + " - " + componentName.getClassName());
92+
//List obtained. Let's start our service
93+
queuedService = componentName;
94+
finalIntent.setAction("com.sdl.noaction"); //Replace what's there so we do go into some unintended loop
95+
//Validate the router service so the service knows if this is a trusted router service
9296
RouterServiceValidator vlad = new RouterServiceValidator(finalContext,componentName);
93-
if(vlad.validate()){
94-
//Log.d(TAG, "Router service trusted!");
95-
queuedService = componentName;
96-
finalIntent.setAction("com.sdl.noaction"); //Replace what's there so we do go into some unintended loop
97-
onSdlEnabled(finalContext, finalIntent);
98-
}else{
99-
Log.w(TAG, "RouterService was not trusted. Ignoring intent from : "+ componentName.getClassName());
100-
}
97+
finalIntent.putExtra(TransportConstants.ROUTER_SERVICE_VALIDATED, vlad.validate());
98+
onSdlEnabled(finalContext, finalIntent);
10199
}
102100

103101
});
@@ -123,18 +121,15 @@ public void onListObtained(boolean successful) {
123121
if (intent.getAction().contains("android.bluetooth.adapter.action.STATE_CHANGED")){
124122
int state = intent.getIntExtra("android.bluetooth.adapter.extra.STATE",-1);
125123
if (state == BluetoothAdapter.STATE_OFF ||
126-
state == BluetoothAdapter.STATE_TURNING_OFF ){
124+
state == BluetoothAdapter.STATE_TURNING_OFF){
127125
//onProtocolDisabled(context);
128126
//Let's let the service that is running manage what to do for this
129127
//If we were to do it here, for every instance of this BR it would send
130128
//an intent to stop service, where it's only one that is needed.
131129
return;
132-
}else if(state == BluetoothAdapter.STATE_TURNING_ON){
133-
//We started bluetooth, we should check for a new valid router list
134-
RouterServiceValidator.createTrustedListRequest(context,true);
135130
}
136131
}
137-
132+
138133
if(localRouterClass!=null){ //If there is a supplied router service lets run some logic regarding starting one
139134

140135
if(!didStart){

sdl_android_lib/src/com/smartdevicelink/transport/TransportConstants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public class TransportConstants {
2727
public static final String START_ROUTER_SERVICE_SDL_ENABLED_CMP_NAME = "component_name";
2828
public static final String START_ROUTER_SERVICE_SDL_ENABLED_PING = "ping";
2929
public static final String FORCE_TRANSPORT_CONNECTED = "force_connect"; //This is legacy, do not refactor this.
30+
public static final String ROUTER_SERVICE_VALIDATED = "router_service_validated";
31+
3032

3133
public static final String REPLY_TO_INTENT_EXTRA = "ReplyAddress";
3234
public static final String CONNECT_AS_CLIENT_BOOLEAN_EXTRA = "connectAsClient";

sdl_android_tests/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
<instrumentation
1717
android:name="android.test.InstrumentationTestRunner"
18-
android:targetPackage="com.smartdevicelink" />
18+
android:targetPackage="com.smartdevicelink.test" />
1919
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2020

2121
<application

sdl_android_tests/src/com/smartdevicelink/transport/RSVTestCase.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.util.List;
44

55
import org.json.JSONArray;
6+
import org.json.JSONException;
7+
import org.json.JSONObject;
68

79
import android.content.pm.PackageInfo;
810
import android.content.pm.PackageManager;
@@ -93,7 +95,7 @@ public void testSetTrustedList(){
9395
assertFalse(RouterServiceValidator.setTrustedList(mContext,null));
9496
assertFalse(RouterServiceValidator.setTrustedList(null,"test"));
9597
assertTrue(RouterServiceValidator.setTrustedList(mContext,"test"));
96-
String test= "{\"response\": {\"com.livio.sdl\" : { \"versionBlacklist\":[] }, \"com.lexus.tcapp\" : { \"versionBlacklist\":[] }, \"com.toyota.tcapp\" : { \"versionBlacklist\": [] } , \"com.sdl.router\":{\"versionBlacklist\": [] } }}";
98+
String test = "{\"response\": {\"com.livio.sdl\" : { \"versionBlacklist\":[] }, \"com.lexus.tcapp\" : { \"versionBlacklist\":[] }, \"com.toyota.tcapp\" : { \"versionBlacklist\": [] } , \"com.sdl.router\":{\"versionBlacklist\": [] },\"com.ford.fordpass\" : { \"versionBlacklist\":[] } }}";
9799
assertTrue(RouterServiceValidator.setTrustedList(mContext,test));
98100
assertTrue(RouterServiceValidator.setTrustedList(mContext,test+test+test+test+test));
99101
StringBuilder builder = new StringBuilder();
@@ -104,7 +106,7 @@ public void testSetTrustedList(){
104106
}
105107

106108
public void testTrustedListSetAndGet(){
107-
String test= "{\"response\": {\"com.livio.sdl\" : { \"versionBlacklist\":[] }, \"com.lexus.tcapp\" : { \"versionBlacklist\":[] }, \"com.toyota.tcapp\" : { \"versionBlacklist\": [] } , \"com.sdl.router\":{\"versionBlacklist\": [] } }}";
109+
String test = "{\"response\": {\"com.livio.sdl\" : { \"versionBlacklist\":[] }, \"com.lexus.tcapp\" : { \"versionBlacklist\":[] }, \"com.toyota.tcapp\" : { \"versionBlacklist\": [] } , \"com.sdl.router\":{\"versionBlacklist\": [] },\"com.ford.fordpass\" : { \"versionBlacklist\":[] } }}";
108110
assertTrue(RouterServiceValidator.setTrustedList(mContext,test));
109111
String retVal = RouterServiceValidator.getTrustedList(mContext);
110112
assertNotNull(retVal);
@@ -203,5 +205,41 @@ public void httpFailure(int statusCode) {
203205

204206
}
205207

208+
/**
209+
* Test to check that we can save our last request which actually houses all the previous known sdl enabled apps
210+
*/
211+
public void testRequestChange(){
212+
assertNull(RouterServiceValidator.getLastRequest(mContext));
213+
String test = "{\"response\": {\"com.livio.sdl\" : { \"versionBlacklist\":[] }, \"com.lexus.tcapp\" : { \"versionBlacklist\":[] }, \"com.toyota.tcapp\" : { \"versionBlacklist\": [] } , \"com.sdl.router\":{\"versionBlacklist\": [] },\"com.ford.fordpass\" : { \"versionBlacklist\":[] } }}";
214+
JSONObject object = null;
215+
try {
216+
object = new JSONObject(test);
217+
} catch (JSONException e) {
218+
e.printStackTrace();
219+
}
220+
assertNotNull(object);
221+
assertFalse(object.equals(RouterServiceValidator.getLastRequest(mContext)));
222+
223+
assertTrue(RouterServiceValidator.setLastRequest(mContext, object.toString()));
224+
225+
String oldRequest = RouterServiceValidator.getLastRequest(mContext);
226+
assertNotNull(oldRequest);
227+
assertTrue(object.toString().equals(oldRequest));
228+
229+
//Now test a new list
230+
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\":[] } }}";
231+
object = null;
232+
try {
233+
object = new JSONObject(test);
234+
} catch (JSONException e) {
235+
e.printStackTrace();
236+
}
237+
assertNotNull(object);
238+
assertFalse(object.equals(RouterServiceValidator.getLastRequest(mContext)));
239+
//Clear it for next test
240+
RouterServiceValidator.setLastRequest(mContext, null);
241+
242+
}
243+
206244

207245
}

0 commit comments

Comments
 (0)