Skip to content

Commit

Permalink
Merge pull request #372 from smartdevicelink/bugfix/issue_371
Browse files Browse the repository at this point in the history
[Hotfix] RSVP Improvements
  • Loading branch information
joeygrover authored Dec 13, 2016
2 parents 65dc8c5 + 082c4cd commit fa3031b
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -556,12 +556,19 @@ public void forceHardwareConnectEvent(TransportType type){
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{
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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";
Expand All @@ -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;
Expand Down Expand Up @@ -320,6 +324,13 @@ private static List<SdlApp> findAllSdlApps(Context context){
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){
Expand Down Expand Up @@ -361,23 +372,14 @@ protected static boolean createTrustedListRequest(final Context context, boolean
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();
builder.append(REQUEST_PREFIX);

List<SdlApp> apps = findAllSdlApps(context);

JSONObject object = new JSONObject();
final JSONObject object = new JSONObject();
JSONArray array = new JSONArray();
JSONObject jsonApp;

Expand All @@ -395,6 +397,19 @@ protected static boolean createTrustedListRequest(final Context context, boolean

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() {

Expand All @@ -403,6 +418,7 @@ public void httpCallComplete(String response) {
// 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);}
}
Expand Down Expand Up @@ -522,8 +538,28 @@ protected static Long getTrustedAppListTimeStamp(Context context){
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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,13 @@ public void onReceive(Context context, Intent intent) {
@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);
}

});
Expand All @@ -123,18 +121,15 @@ public void onListObtained(boolean successful) {
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){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion sdl_android_tests/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
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;
Expand Down Expand Up @@ -93,7 +95,7 @@ public void testSetTrustedList(){
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();
Expand All @@ -104,7 +106,7 @@ public void testSetTrustedList(){
}

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);
Expand Down Expand Up @@ -203,5 +205,41 @@ public void httpFailure(int statusCode) {

}

/**
* 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);

}


}

0 comments on commit fa3031b

Please sign in to comment.