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