diff options
author | Joey Grover <joeygrover@gmail.com> | 2019-04-02 17:49:04 -0400 |
---|---|---|
committer | Joey Grover <joeygrover@gmail.com> | 2019-04-02 17:49:04 -0400 |
commit | 4fc9987d49f4bd8090186d1fd36a20903d7c2069 (patch) | |
tree | 8383d548169b04a88e1feb6c058d76c6d3c99263 | |
parent | 7eff21bab6624f739ed43ccffb089b1c0b146847 (diff) | |
download | sdl_android-4fc9987d49f4bd8090186d1fd36a20903d7c2069.tar.gz |
SCM listens for notification, ASCaps updated
3 files changed, 153 insertions, 21 deletions
diff --git a/base/src/main/java/com/smartdevicelink/proxy/SystemCapabilityManager.java b/base/src/main/java/com/smartdevicelink/proxy/SystemCapabilityManager.java index e0c40727c..f47a17a4f 100644 --- a/base/src/main/java/com/smartdevicelink/proxy/SystemCapabilityManager.java +++ b/base/src/main/java/com/smartdevicelink/proxy/SystemCapabilityManager.java @@ -34,11 +34,15 @@ package com.smartdevicelink.proxy; import com.smartdevicelink.protocol.enums.FunctionID; import com.smartdevicelink.proxy.interfaces.ISdl; import com.smartdevicelink.proxy.interfaces.OnSystemCapabilityListener; +import com.smartdevicelink.proxy.rpc.AppServiceCapability; +import com.smartdevicelink.proxy.rpc.AppServicesCapabilities; import com.smartdevicelink.proxy.rpc.GetSystemCapability; import com.smartdevicelink.proxy.rpc.GetSystemCapabilityResponse; import com.smartdevicelink.proxy.rpc.HMICapabilities; +import com.smartdevicelink.proxy.rpc.OnSystemCapabilityUpdated; import com.smartdevicelink.proxy.rpc.RegisterAppInterfaceResponse; import com.smartdevicelink.proxy.rpc.SetDisplayLayoutResponse; +import com.smartdevicelink.proxy.rpc.SystemCapability; import com.smartdevicelink.proxy.rpc.enums.Result; import com.smartdevicelink.proxy.rpc.enums.SystemCapabilityType; import com.smartdevicelink.proxy.rpc.listeners.OnRPCListener; @@ -82,28 +86,57 @@ public class SystemCapabilityManager { } } - private void setupRpcListeners(){ - rpcListener = new OnRPCListener() { - @Override - public void onReceived(RPCMessage message) { - if (message != null && RPCMessage.KEY_RESPONSE.equals(message.getMessageType())) { - switch (message.getFunctionID()) { - case SET_DISPLAY_LAYOUT: - SetDisplayLayoutResponse response = (SetDisplayLayoutResponse)message; - setCapability(SystemCapabilityType.DISPLAY, response.getDisplayCapabilities()); - setCapability(SystemCapabilityType.BUTTON, response.getButtonCapabilities()); - setCapability(SystemCapabilityType.PRESET_BANK, response.getPresetBankCapabilities()); - setCapability(SystemCapabilityType.SOFTBUTTON, response.getSoftButtonCapabilities()); - break; - } - } - } - }; + private void setupRpcListeners(){ + rpcListener = new OnRPCListener() { + @Override + public void onReceived(RPCMessage message) { + if (message != null) { + if (RPCMessage.KEY_RESPONSE.equals(message.getMessageType())) { + switch (message.getFunctionID()) { + case SET_DISPLAY_LAYOUT: + SetDisplayLayoutResponse response = (SetDisplayLayoutResponse) message; + setCapability(SystemCapabilityType.DISPLAY, response.getDisplayCapabilities()); + setCapability(SystemCapabilityType.BUTTON, response.getButtonCapabilities()); + setCapability(SystemCapabilityType.PRESET_BANK, response.getPresetBankCapabilities()); + setCapability(SystemCapabilityType.SOFTBUTTON, response.getSoftButtonCapabilities()); + break; + } + } else if (RPCMessage.KEY_NOTIFICATION.equals(message.getMessageType())){ + switch (message.getFunctionID()) { + case ON_SYSTEM_CAPABILITY_UPDATED: + OnSystemCapabilityUpdated onSystemCapabilityUpdated =(OnSystemCapabilityUpdated)message; + if(onSystemCapabilityUpdated.getSystemCapability() != null){ + SystemCapability systemCapability = onSystemCapabilityUpdated.getSystemCapability(); + SystemCapabilityType systemCapabilityType = systemCapability.getSystemCapabilityType(); + Object capability = systemCapability.getCapabilityForType(systemCapabilityType); + if(cachedSystemCapabilities.containsKey(systemCapabilityType)) { //The capability already exists + switch (systemCapabilityType) { + case APP_SERVICES: + // App services only updates what was changed so we need + // to update the capability rather than override it + AppServicesCapabilities appServicesCapabilities = (AppServicesCapabilities) capability; + List<AppServiceCapability> appServicesCapabilitiesList = appServicesCapabilities.getAppServices(); + AppServicesCapabilities cachedAppServicesCapabilities = (AppServicesCapabilities) cachedSystemCapabilities.get(systemCapabilityType); - if(callback != null){ - callback.addOnRPCListener(FunctionID.SET_DISPLAY_LAYOUT, rpcListener); - } - } + //Update the cached app services + cachedAppServicesCapabilities.updateAppServices(appServicesCapabilitiesList); + //Set the new capability object to the updated cached capabilities + capability = cachedAppServicesCapabilities; + } + } + setCapability(systemCapabilityType, capability); + } + } + } + } + } + }; + + if(callback != null){ + callback.addOnRPCListener(FunctionID.SET_DISPLAY_LAYOUT, rpcListener); + callback.addOnRPCListener(FunctionID.ON_SYSTEM_CAPABILITY_UPDATED, rpcListener); + } + } /** * Sets a capability in the cached map. This should only be done when an RPC is received and contains updates to the capability @@ -115,6 +148,28 @@ public class SystemCapabilityManager { cachedSystemCapabilities.put(systemCapabilityType, capability); notifyListeners(systemCapabilityType, capability); } + /** + * Updates a capability in the cached map. Unlike setCapability, this method will update the + * existing capability instead if is supported. This should only be done when an RPC is received and + * contains updates to the capability that is being cached in the SystemCapabilityManager. + * @param systemCapabilityType the system capability type that will be set + * @param capability the value of the capability that will be set + */ + private synchronized void updateCapability(SystemCapabilityType systemCapabilityType, Object capability){ + if(cachedSystemCapabilities.containsKey(systemCapabilityType)){ + switch (systemCapabilityType){ + case APP_SERVICES: + AppServicesCapabilities appServicesCapabilities = (AppServicesCapabilities) capability; + List<AppServiceCapability> appServicesCapabilitiesList = appServicesCapabilities.getAppServices(); + AppServicesCapabilities cachedAppServiceCapabilies = (AppServicesCapabilities)cachedSystemCapabilities.get(systemCapabilityType); + + + } + }else{ + cachedSystemCapabilities.put(systemCapabilityType, capability); + } + notifyListeners(systemCapabilityType, capability); + } /** * Notify listners in the list about the new retrieved capability diff --git a/base/src/main/java/com/smartdevicelink/proxy/rpc/AppServiceCapability.java b/base/src/main/java/com/smartdevicelink/proxy/rpc/AppServiceCapability.java index 056962028..73120d2ca 100644 --- a/base/src/main/java/com/smartdevicelink/proxy/rpc/AppServiceCapability.java +++ b/base/src/main/java/com/smartdevicelink/proxy/rpc/AppServiceCapability.java @@ -97,4 +97,32 @@ public class AppServiceCapability extends RPCStruct { return (AppServiceRecord) getObject(AppServiceRecord.class, KEY_UPDATED_APP_SERVICE_RECORD); } + /** + * Helper method to compare an AppServiceCapability to this instance. + * @param capability the AppServiceCapability to compare to this one + * @return if both AppServiceCapability objects refer to the same service + */ + public boolean matchesAppService(AppServiceCapability capability){ + if(capability != null){ + AppServiceRecord appServiceRecord = getUpdatedAppServiceRecord(); + AppServiceRecord otherASR = capability.getUpdatedAppServiceRecord(); + + if(appServiceRecord != null && otherASR != null) { + //Check service IDs, if they are the same we can assume these are the same + if (appServiceRecord.getServiceID() != null && appServiceRecord.getServiceID().equalsIgnoreCase(otherASR.getServiceID())) { + return true; + }else{ + AppServiceManifest manifest = appServiceRecord.getServiceManifest(); + AppServiceManifest otherManifest = otherASR.getServiceManifest(); + if(manifest != null && otherManifest != null){ + //Check the service names, if they are the same it can be assumed they are the same service + return (manifest.getServiceName() != null && manifest.getServiceName().equalsIgnoreCase(otherManifest.getServiceName())); + } + } + } + } + // If it got to this point it was not the same + return false; + } + } diff --git a/base/src/main/java/com/smartdevicelink/proxy/rpc/AppServicesCapabilities.java b/base/src/main/java/com/smartdevicelink/proxy/rpc/AppServicesCapabilities.java index 43b99dc56..6ac49ba9b 100644 --- a/base/src/main/java/com/smartdevicelink/proxy/rpc/AppServicesCapabilities.java +++ b/base/src/main/java/com/smartdevicelink/proxy/rpc/AppServicesCapabilities.java @@ -31,8 +31,11 @@ */ package com.smartdevicelink.proxy.rpc; +import android.support.annotation.NonNull; + import com.smartdevicelink.proxy.RPCStruct; +import java.util.ArrayList; import java.util.Hashtable; import java.util.List; @@ -82,4 +85,50 @@ public class AppServicesCapabilities extends RPCStruct { return (List<AppServiceCapability>) getObject(AppServiceCapability.class,KEY_APP_SERVICES); } + /** + * This method will update the current List<AppServiceCapability> with the udpated items. If the + * items don't exist in the original ist they will be added. If the original list is null or + * empty, the new list will simply be set as the list. + * @param updatedAppServiceCapabilities the List<AppServiceCapability> that have been updated + * @return if the list was updated + */ + public boolean updateAppServices(@NonNull List<AppServiceCapability> updatedAppServiceCapabilities){ + if(updatedAppServiceCapabilities == null){ + return false; + } + final List<AppServiceCapability> appServiceCapabilities = getAppServices(); + + if(appServiceCapabilities == null || appServiceCapabilities.isEmpty()){ + //If this list is null or empty, just copy the updated list into this object + setAppServices(updatedAppServiceCapabilities); + return true; + } + + //Create a shallow copy for us to alter while iterating through the original list + List<AppServiceCapability> tempList = new ArrayList<>(appServiceCapabilities); + + boolean updated; + for(AppServiceCapability updatedAppServiceCapability: updatedAppServiceCapabilities){ + updated = false; + if(updatedAppServiceCapability != null) { + for (AppServiceCapability appServiceCapability : appServiceCapabilities) { + if (updatedAppServiceCapability.matchesAppService(appServiceCapability)) { + tempList.remove(appServiceCapability); //Remove the old entry + tempList.add(updatedAppServiceCapability); //Add the new entry + updated = true; + break; + } + } + } + + if(!updated){ + //If the updated App Service capability doesn't exist in the old list, just add it + tempList.add(updatedAppServiceCapability); + } + } + + setAppServices(tempList); + return tempList.equals(appServiceCapabilities); + } + } |