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