4
4
import android .content .SharedPreferences ;
5
5
import android .util .JsonReader ;
6
6
import android .util .JsonWriter ;
7
+ import com .matter .tv .app .api .SupportedCluster ;
7
8
import com .matter .tv .server .model .ContentApp ;
8
9
import java .io .IOException ;
9
10
import java .io .StringReader ;
10
11
import java .io .StringWriter ;
11
12
import java .util .Collections ;
12
13
import java .util .HashMap ;
14
+ import java .util .HashSet ;
13
15
import java .util .Map ;
16
+ import java .util .Set ;
17
+ import java .util .List ;
14
18
15
19
public class EndpointsDataStore {
16
20
@@ -21,6 +25,11 @@ public class EndpointsDataStore {
21
25
private static final String KEY_PRODUCTID = "PID" ;
22
26
private static final String KEY_VERSION = "VER" ;
23
27
private static final String KEY_ENDPOINTID = "EPID" ;
28
+ private static final String KEY_SUPPORTED_CLUSTERS = "supportedClusters" ;
29
+ private static final String KEY_CLUSTER_IDENTIFIER = "clusterIdentifier" ;
30
+ private static final String KEY_FEATURES = "features" ;
31
+ private static final String KEY_OPTIONAL_COMMAND_IDENTIFIERS = "optionalCommandIdentifiers" ;
32
+ private static final String KEY_OPTIONAL_ATTRIBUTES_IDENTIFIERS = "optionalAttributesIdentifiers" ;
24
33
private static EndpointsDataStore instance ;
25
34
private final SharedPreferences discoveredEndpoints ;
26
35
Map <String , ContentApp > persistedContentApps = new HashMap <>();
@@ -57,19 +66,20 @@ private String serializeContentApp(ContentApp app) {
57
66
StringWriter stringWriter = new StringWriter ();
58
67
JsonWriter jsonWriter = new JsonWriter (stringWriter );
59
68
try {
60
- jsonWriter
61
- .beginObject ()
62
- .name (KEY_VENDORID )
63
- .value (app .getVendorId ())
64
- .name (KEY_VENDORNAME )
65
- .value (app .getVendorName ())
66
- .name (KEY_PRODUCTID )
67
- .value (app .getProductId ())
68
- .name (KEY_VERSION )
69
- .value (app .getVersion ())
70
- .name (KEY_ENDPOINTID )
71
- .value (app .getEndpointId ())
72
- .endObject ();
69
+ jsonWriter .beginObject ();
70
+ jsonWriter .name (KEY_VENDORID );
71
+ jsonWriter .value (app .getVendorId ());
72
+ jsonWriter .name (KEY_VENDORNAME );
73
+ jsonWriter .value (app .getVendorName ());
74
+ jsonWriter .name (KEY_PRODUCTID );
75
+ jsonWriter .value (app .getProductId ());
76
+ jsonWriter .name (KEY_VERSION );
77
+ jsonWriter .value (app .getVersion ());
78
+ jsonWriter .name (KEY_ENDPOINTID );
79
+ jsonWriter .value (app .getEndpointId ());
80
+ jsonWriter .name (KEY_SUPPORTED_CLUSTERS );
81
+ serializeSupportedClusters (jsonWriter , app .getSupportedClusters ());
82
+ jsonWriter .endObject ();
73
83
jsonWriter .flush ();
74
84
jsonWriter .close ();
75
85
} catch (IOException e ) {
@@ -88,6 +98,7 @@ private ContentApp deserializeContentApp(String appName, String appMetadata) {
88
98
int vendorId = 0 ;
89
99
int productId = 0 ;
90
100
int endpoint = ContentApp .INVALID_ENDPOINTID ;
101
+ Set <SupportedClusters > supportedClusters = new HashSet <>();
91
102
while (jsonReader .hasNext ()) {
92
103
String name = jsonReader .nextName ();
93
104
switch (name ) {
@@ -106,14 +117,94 @@ private ContentApp deserializeContentApp(String appName, String appMetadata) {
106
117
case KEY_ENDPOINTID :
107
118
endpoint = jsonReader .nextInt ();
108
119
break ;
120
+ case KEY_SUPPORTED_CLUSTERS :
121
+ supportedClusters = deserializeSupportedClusters (jsonReader );
122
+ break ;
109
123
}
110
124
}
111
- app = new ContentApp (appName , vendorName , vendorId , productId , version );
125
+ app = new ContentApp (appName , vendorName , vendorId , productId , version , supportedClusters );
112
126
jsonReader .endObject ();
113
127
jsonReader .close ();
114
128
} catch (IOException e ) {
115
129
// Cannot happen
116
130
}
117
131
return app ;
118
132
}
133
+
134
+ private void serializeSupportedClusters (JsonWriter jsonWriter , Set <SupportedCluster > supportedClusters ) {
135
+ if (supportedClusters != null ) {
136
+ jsonWriter .beginArray ();
137
+ for (SupportedCluster supportedCluster : supportedClusters ) {
138
+ if (supportedCluster != null ) {
139
+ serializeSupportedCluster (jsonWriter , supportedCluster );
140
+ }
141
+ }
142
+ jsonWriter .endArray ();
143
+ }
144
+ }
145
+
146
+ private Set <SupportedCluster > deserializeSupportedClusters (JsonReader jsonReader ) {
147
+ Set <SupportedCluster > supportedClusters = new HashSet <>();
148
+ jsonReader .beginArray ();
149
+ while (jsonReader .hasNext ()) {
150
+ supportedClusters .add (deserializeSupportedCluster (jsonReader ));
151
+ }
152
+ jsonReader .endArray ();
153
+ return supportedClusters ;
154
+ }
155
+
156
+ private void serializeSupportedCluster (JsonWriter jsonWriter ) {
157
+ jsonWriter .beginObject ();
158
+ jsonWriter .name (KEY_CLUSTER_IDENTIFIER );
159
+ jsonWriter .value (supportedCluster .clusterIdentifier );
160
+ jsonWriter .name (KEY_FEATURES );
161
+ jsonWriter .value (supportedCluster .features );
162
+ jsonWriter .name (KEY_OPTIONAL_COMMAND_IDENTIFIERS );
163
+ serializeIntArray (jsonWriter , supportedCluster .optionalCommandIdentifiers );
164
+ jsonWriter .name (KEY_OPTIONAL_ATTRIBUTES_IDENTIFIERS );
165
+ serializeIntArray (jsonWriter , supportedCluster .optionalAttributesIdentifiers );
166
+ jsonWriter .endObject ();
167
+ }
168
+
169
+ private SupportedCluster deserializeSupportedCluster (JsonReader jsonReader ) {
170
+ SupportedCluster supportedCluster = new SupportedCluster ();
171
+ jsonReader .beginObject ();
172
+ while (jsonReader .hasNext ()) {
173
+ String name = jsonReader .nextName ();
174
+ switch (name ) {
175
+ case KEY_CLUSTER_IDENTIFIER :
176
+ supportedCluster .clusterIdentifier = jsonReader .nextInt ();
177
+ break ;
178
+ case KEY_FEATURES :
179
+ supportedCluster .features = jsonReader .nextInt ();
180
+ break ;
181
+ case KEY_OPTIONAL_COMMAND_IDENTIFIERS :
182
+ supportedCluster .optionalCommandIdentifiers = deserializeIntArray (jsonReader );
183
+ break ;
184
+ case KEY_OPTIONAL_ATTRIBUTES_IDENTIFIERS :
185
+ supportedCluster .optionalAttributesIdentifiers = deserializeIntArray (jsonReader );
186
+ break ;
187
+ }
188
+ }
189
+ jsonReader .endObject ();
190
+ return supportedCluster ;
191
+ }
192
+
193
+ private void serializeIntArray (JsonWriter jsonWriter , int [] array ) {
194
+ jsonWriter .beginArray ();
195
+ for (int value : array ) {
196
+ jsonWriter .value (value );
197
+ }
198
+ jsonWriter .endArray ();
199
+ }
200
+
201
+ private int [] deserializeIntArray (JsonReader jsonReader ) {
202
+ List <Integer > dynamicArray = new ArrayList <>();
203
+ jsonReader .beginArray ();
204
+ while (jsonReader .hasNext ()) {
205
+ dynamicArray .add (jsonReader .nextInt ());
206
+ }
207
+ jsonReader .endArray ();
208
+ return dynamicArray .toStream ().mapToInt (Integer ::intValue ).toArray ();
209
+ }
119
210
}
0 commit comments