diff options
author | Joey Grover <joeygrover@gmail.com> | 2017-09-22 15:15:18 -0400 |
---|---|---|
committer | Joey Grover <joeygrover@gmail.com> | 2017-09-22 15:15:18 -0400 |
commit | c4e84dfa37b693e580d72c00921ef5df8da780ab (patch) | |
tree | 1ca6a785ab3516465f629d9cefe5404f8dda7ba0 | |
parent | f215e736202f1b82f3ececb6325fa4d8992bd3f9 (diff) | |
download | sdl_android-feature/issue_566.tar.gz |
Update convertToList method to take in class and properly convert to list, not just cast blindlyfeature/issue_566
3 files changed, 27 insertions, 12 deletions
diff --git a/sdl_android/src/androidTest/java/com/smartdevicelink/test/proxy/SystemCapabilityManagerTests.java b/sdl_android/src/androidTest/java/com/smartdevicelink/test/proxy/SystemCapabilityManagerTests.java index c539f97a6..89c15ab16 100644 --- a/sdl_android/src/androidTest/java/com/smartdevicelink/test/proxy/SystemCapabilityManagerTests.java +++ b/sdl_android/src/androidTest/java/com/smartdevicelink/test/proxy/SystemCapabilityManagerTests.java @@ -128,7 +128,7 @@ public class SystemCapabilityManagerTests extends AndroidTestCase { SystemCapabilityManager systemCapabilityManager = createSampleManager(); Object capability = systemCapabilityManager.getCapability(SystemCapabilityType.SOFTBUTTON); assertNotNull(capability); - List<SoftButtonCapabilities> list = (List<SoftButtonCapabilities>)SystemCapabilityManager.convertToList(capability); + List<SoftButtonCapabilities> list = SystemCapabilityManager.convertToList(capability, SoftButtonCapabilities.class); assertNotNull(list); diff --git a/sdl_android/src/main/java/com/smartdevicelink/proxy/SdlProxyALM.java b/sdl_android/src/main/java/com/smartdevicelink/proxy/SdlProxyALM.java index 8a8437db1..a9cec4f10 100644 --- a/sdl_android/src/main/java/com/smartdevicelink/proxy/SdlProxyALM.java +++ b/sdl_android/src/main/java/com/smartdevicelink/proxy/SdlProxyALM.java @@ -1643,7 +1643,7 @@ public class SdlProxyALM extends SdlProxyBase<IProxyListenerALM> { throw new SdlException("SDL is unavailable. Unable to get the buttonCapabilities.", SdlExceptionCause.SDL_UNAVAILABLE);
}
- return ( List<ButtonCapabilities> ) convertToList(_systemCapabilityManager.getCapability(SystemCapabilityType.BUTTON));
+ return convertToList(_systemCapabilityManager.getCapability(SystemCapabilityType.BUTTON), ButtonCapabilities.class);
}
/**
@@ -1664,7 +1664,7 @@ public class SdlProxyALM extends SdlProxyBase<IProxyListenerALM> { if (!_appInterfaceRegisterd || _systemCapabilityManager == null) {
throw new SdlException("SDL is not connected. Unable to get the softButtonCapabilities.", SdlExceptionCause.SDL_UNAVAILABLE);
}
- return ( List<SoftButtonCapabilities> ) convertToList(_systemCapabilityManager.getCapability(SystemCapabilityType.SOFTBUTTON));
+ return convertToList(_systemCapabilityManager.getCapability(SystemCapabilityType.SOFTBUTTON),SoftButtonCapabilities.class);
}
/**
@@ -1726,7 +1726,7 @@ public class SdlProxyALM extends SdlProxyBase<IProxyListenerALM> { if (!_appInterfaceRegisterd || _systemCapabilityManager == null) {
throw new SdlException("SDL is unavailable. Unable to get the displayCapabilities.", SdlExceptionCause.SDL_UNAVAILABLE);
}
- return ( DisplayCapabilities ) convertToList(_systemCapabilityManager.getCapability(SystemCapabilityType.DISPLAY));
+ return ( DisplayCapabilities ) _systemCapabilityManager.getCapability(SystemCapabilityType.DISPLAY);
}
/**
@@ -1747,7 +1747,7 @@ public class SdlProxyALM extends SdlProxyBase<IProxyListenerALM> { if (!_appInterfaceRegisterd || _systemCapabilityManager == null) {
throw new SdlException("SDL is unavailable. Unable to get the hmiZoneCapabilities.", SdlExceptionCause.SDL_UNAVAILABLE);
}
- return ( List<HmiZoneCapabilities> )convertToList(_systemCapabilityManager.getCapability(SystemCapabilityType.BUTTON));
+ return convertToList(_systemCapabilityManager.getCapability(SystemCapabilityType.HMI_ZONE), HmiZoneCapabilities.class);
}
/**
@@ -1769,7 +1769,7 @@ public class SdlProxyALM extends SdlProxyBase<IProxyListenerALM> { throw new SdlException("SDL is unavailable. Unable to get the speechCapabilities.", SdlExceptionCause.SDL_UNAVAILABLE);
}
- return ( List<SpeechCapabilities> )convertToList(_systemCapabilityManager.getCapability(SystemCapabilityType.BUTTON));
+ return convertToList(_systemCapabilityManager.getCapability(SystemCapabilityType.SPEECH), SpeechCapabilities.class);
}
/**
* Gets PrerecordedSpeech set when application interface is registered.
@@ -1865,7 +1865,7 @@ public class SdlProxyALM extends SdlProxyBase<IProxyListenerALM> { if (!_appInterfaceRegisterd || _systemCapabilityManager == null) {
throw new SdlException("SDL is unavailable. Unable to get the vrCapabilities.", SdlExceptionCause.SDL_UNAVAILABLE);
}
- return ( List<VrCapabilities> )convertToList(_systemCapabilityManager.getCapability(SystemCapabilityType.BUTTON));
+ return convertToList(_systemCapabilityManager.getCapability(SystemCapabilityType.VOICE_RECOGNITION), VrCapabilities.class);
}
/**
@@ -1905,7 +1905,7 @@ public class SdlProxyALM extends SdlProxyBase<IProxyListenerALM> { if (!_appInterfaceRegisterd || _systemCapabilityManager == null) {
throw new SdlException("SDL is not connected. Unable to get the vehicleType.", SdlExceptionCause.SDL_UNAVAILABLE);
}
- return ( List<AudioPassThruCapabilities> )convertToList(_systemCapabilityManager.getCapability(SystemCapabilityType.BUTTON));
+ return convertToList(_systemCapabilityManager.getCapability(SystemCapabilityType.AUDIO_PASSTHROUGH), AudioPassThruCapabilities.class);
}
public List<Integer> getSupportedDiagModes() throws SdlException {
@@ -1939,7 +1939,7 @@ public class SdlProxyALM extends SdlProxyBase<IProxyListenerALM> { if (!_appInterfaceRegisterd || _systemCapabilityManager == null) {
throw new SdlException("SDL is not connected. Unable to get the HMICapabilities.", SdlExceptionCause.SDL_UNAVAILABLE);
}
- return ( HMICapabilities ) _systemCapabilityManager.getCapability(SystemCapabilityType.BUTTON);
+ return ( HMICapabilities ) _systemCapabilityManager.getCapability(SystemCapabilityType.HMI);
}
diff --git a/sdl_android/src/main/java/com/smartdevicelink/proxy/SystemCapabilityManager.java b/sdl_android/src/main/java/com/smartdevicelink/proxy/SystemCapabilityManager.java index 325a61c98..bd0edb48b 100644 --- a/sdl_android/src/main/java/com/smartdevicelink/proxy/SystemCapabilityManager.java +++ b/sdl_android/src/main/java/com/smartdevicelink/proxy/SystemCapabilityManager.java @@ -11,6 +11,7 @@ import com.smartdevicelink.proxy.rpc.listeners.OnRPCResponseListener; import com.smartdevicelink.util.CorrelationIdGenerator; import java.security.InvalidParameterException; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -139,12 +140,26 @@ public class SystemCapabilityManager { /** * Converts a capability object into a list. * @param object the capability that needs to be converted + * @param classType The class type of that should be contained in the list * @return a List of capabilities if object is instance of List, otherwise it will return null. */ @SuppressWarnings({"unchecked"}) - public static List<?> convertToList(Object object){ - if(object instanceof List<?>){ - return (List<?>) object; + public static <T> List<T> convertToList(Object object, Class<T> classType){ + if(classType!=null && object!=null && object instanceof List ){ + List list = (List)object; + if(!list.isEmpty()){ + if(classType.isInstance(list.get(0))){ + return (List<T>)object; + }else{ + //The list is not of the correct list type + return null; + } + }else { + //We return a new list of type T instead of null because while we don't know if + //the original list was of type T we want to ensure that we don't throw a cast class exception + //but still + return new ArrayList<T>(); + } }else{ return null; } |