1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
package com.smartdevicelink.proxy;
import com.smartdevicelink.proxy.interfaces.OnSystemCapabilityListener;
import com.smartdevicelink.proxy.rpc.GetSystemCapability;
import com.smartdevicelink.proxy.rpc.GetSystemCapabilityResponse;
import com.smartdevicelink.proxy.rpc.RegisterAppInterfaceResponse;
import com.smartdevicelink.proxy.rpc.enums.SystemCapabilityType;
import com.smartdevicelink.proxy.rpc.listeners.OnRPCResponseListener;
import java.util.HashMap;
public class SystemCapabilityManager {
HashMap<SystemCapabilityType, Object> cachedSystemCapabilities = new HashMap<>();
ISystemCapabilityManager callback;
public interface ISystemCapabilityManager{
void onSendPacketRequest(RPCRequest message);
}
public SystemCapabilityManager(ISystemCapabilityManager callback){
this.callback = callback;
}
public void parseRAIResponse(RegisterAppInterfaceResponse response){
cachedSystemCapabilities.put(SystemCapabilityType.HMI, response.getHmiCapabilities());
cachedSystemCapabilities.put(SystemCapabilityType.DISPLAY, response.getDisplayCapabilities());
cachedSystemCapabilities.put(SystemCapabilityType.AUDIO_PASSTHROUGH, response.getAudioPassThruCapabilities());
cachedSystemCapabilities.put(SystemCapabilityType.BUTTON, response.getButtonCapabilities());
cachedSystemCapabilities.put(SystemCapabilityType.HMI_ZONE, response.getHmiZoneCapabilities());
cachedSystemCapabilities.put(SystemCapabilityType.PRESET_BANK, response.getPresetBankCapabilities());
cachedSystemCapabilities.put(SystemCapabilityType.SOFTBUTTON, response.getSoftButtonCapabilities());
cachedSystemCapabilities.put(SystemCapabilityType.SPEECH, response.getSpeechCapabilities());
}
/**
* @param systemCapabilityType Type of capability desired
* @param scListener callback for
* @return
*/
public Object getSystemCapability(final SystemCapabilityType systemCapabilityType, final OnSystemCapabilityListener scListener){
Object capability = cachedSystemCapabilities.get(systemCapabilityType);
if(capability != null){
scListener.onCapabilityRetrieved(capability);
return capability;
}else if(scListener == null){
return null;
}
capability = getSystemCapabilityCommon(systemCapabilityType);
scListener.onCapabilityRetrieved(capability);
return capability;
}
public Object getSystemCapability(final SystemCapabilityType systemCapabilityType){
Object capability = cachedSystemCapabilities.get(systemCapabilityType);
if(capability != null){
return capability;
}
return getSystemCapabilityCommon(systemCapabilityType);
}
/**
* @param systemCapabilityType Type of capability desired
* @return Synchronous result returned by GetSystemCapabilityResponse, null if systemCapabilityType is null
*/
private Object getSystemCapabilityCommon(final SystemCapabilityType systemCapabilityType){
final Object RETURN_LOCK = new Object();
GetSystemCapability request = new GetSystemCapability();
request.setSystemCapabilityType(systemCapabilityType);
request.setOnRPCResponseListener(new OnRPCResponseListener() {
@Override
public void onResponse(int correlationId, RPCResponse response) {
if(response.getSuccess()){
Object retrievedCapability = ((GetSystemCapabilityResponse) response).getSystemCapability().getCapabilityForType(systemCapabilityType);
cachedSystemCapabilities.put(systemCapabilityType, retrievedCapability);
}
synchronized(RETURN_LOCK){
RETURN_LOCK.notify();
}
}
});
new Thread(new Runnable() {
@Override
public void run() {
try {
synchronized(RETURN_LOCK){
RETURN_LOCK.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
callback.onSendPacketRequest(request);
return cachedSystemCapabilities.get(systemCapabilityType);
}
}
|