10
10
import android .util .Log ;
11
11
12
12
import com .crashlytics .android .Crashlytics ;
13
+ import com .google .gson .Gson ;
14
+ import com .google .gson .GsonBuilder ;
15
+ import com .google .gson .JsonDeserializationContext ;
16
+ import com .google .gson .JsonDeserializer ;
17
+ import com .google .gson .JsonElement ;
18
+ import com .google .gson .JsonParseException ;
19
+ import com .google .gson .TypeAdapter ;
20
+ import com .google .gson .stream .JsonReader ;
21
+ import com .google .gson .stream .JsonWriter ;
13
22
import com .j256 .ormlite .dao .Dao ;
14
23
import com .j256 .ormlite .dao .RuntimeExceptionDao ;
15
24
import com .j256 .ormlite .misc .TransactionManager ;
22
31
import com .zulip .android .models .Stream ;
23
32
import com .zulip .android .networking .AsyncUnreadMessagesUpdate ;
24
33
import com .zulip .android .networking .ZulipInterceptor ;
34
+ import com .zulip .android .networking .response .UserConfigurationResponse ;
35
+ import com .zulip .android .networking .response .events .EventsBranch ;
36
+ import com .zulip .android .networking .response .events .GetEventResponse ;
25
37
import com .zulip .android .service .ZulipServices ;
26
38
import com .zulip .android .util .ZLog ;
27
39
28
- import org .json .JSONArray ;
29
- import org .json .JSONException ;
30
-
31
40
import java .io .IOException ;
41
+ import java .lang .reflect .Type ;
32
42
import java .sql .SQLException ;
33
43
import java .util .HashSet ;
44
+ import java .util .List ;
34
45
import java .util .Map ;
35
46
import java .util .Queue ;
36
47
import java .util .Set ;
41
52
42
53
import io .fabric .sdk .android .Fabric ;
43
54
import okhttp3 .OkHttpClient ;
44
- import okhttp3 .Request ;
45
55
import okhttp3 .ResponseBody ;
46
56
import okhttp3 .logging .HttpLoggingInterceptor ;
47
57
import retrofit2 .Call ;
@@ -74,18 +84,6 @@ public class ZulipApp extends Application {
74
84
private static final String MUTED_TOPIC_KEY = "mutedTopics" ;
75
85
private ZulipServices zulipServices ;
76
86
77
- public Request goodRequest ;
78
- public Request badRequest ;
79
- private ZulipActivity zulipActivity ;
80
-
81
- public ZulipActivity getZulipActivity () {
82
- return zulipActivity ;
83
- }
84
-
85
- public void setZulipActivity (ZulipActivity zulipActivity ) {
86
- this .zulipActivity = zulipActivity ;
87
- }
88
-
89
87
/**
90
88
* Handler to manage batching of unread messages
91
89
*/
@@ -111,6 +109,7 @@ public void setZulipActivity(ZulipActivity zulipActivity) {
111
109
* every couple of seconds
112
110
*/
113
111
public final Queue <Integer > unreadMessageQueue = new ConcurrentLinkedQueue <>();
112
+ public String tester ;
114
113
115
114
public static ZulipApp get () {
116
115
return instance ;
@@ -185,14 +184,60 @@ public ZulipServices getZulipServices() {
185
184
.addInterceptor (new ZulipInterceptor ())
186
185
.addInterceptor (logging )
187
186
.build ())
188
- .addConverterFactory (GsonConverterFactory .create ())
187
+ .addConverterFactory (GsonConverterFactory .create (buildGson () ))
189
188
.baseUrl (getServerURI ())
190
189
.build ()
191
190
.create (ZulipServices .class );
192
191
}
193
192
return zulipServices ;
194
193
}
195
194
195
+ private Gson buildGson () {
196
+ final Gson gson = new Gson ();
197
+ return new GsonBuilder ()
198
+ .registerTypeAdapter (UserConfigurationResponse .class , new TypeAdapter <UserConfigurationResponse >() {
199
+
200
+ @ Override
201
+ public void write (JsonWriter out , UserConfigurationResponse value ) throws IOException {
202
+ gson .toJson (gson .toJsonTree (value ), out );
203
+ }
204
+
205
+ @ Override
206
+ public UserConfigurationResponse read (JsonReader in ) throws IOException {
207
+ UserConfigurationResponse res = gson .fromJson (in , UserConfigurationResponse .class );
208
+
209
+ RuntimeExceptionDao <Person , Object > personDao = ZulipApp .this .getDao (Person .class );
210
+ for (int i = 0 ; i < res .getRealmUsers ().size (); i ++) {
211
+
212
+ Person currentPerson = res .getRealmUsers ().get (i );
213
+ Person foundPerson = null ;
214
+ try {
215
+ foundPerson = personDao .queryBuilder ().where ().eq (Person .EMAIL_FIELD , currentPerson .getEmail ()).queryForFirst ();
216
+ if (foundPerson != null ) {
217
+ currentPerson .setId (foundPerson .getId ());
218
+ }
219
+ } catch (SQLException e ) {
220
+ e .printStackTrace ();
221
+ }
222
+ }
223
+ return res ;
224
+ }
225
+ })
226
+ .registerTypeAdapter (GetEventResponse .class , new JsonDeserializer <EventsBranch >() {
227
+ @ Override
228
+ public EventsBranch deserialize (JsonElement json , Type typeOfT , JsonDeserializationContext context ) throws JsonParseException {
229
+ EventsBranch invalid = gson .fromJson (json , EventsBranch .class );
230
+ Class <? extends EventsBranch > t = EventsBranch .BranchType .fromRawType (invalid );
231
+ if (t != null ) {
232
+ return gson .fromJson (json , t );
233
+ }
234
+ Log .w ("GSON" , "Attempted to deserialize and unregistered EventBranch... See EventBranch.BranchType" );
235
+ return invalid ;
236
+ }
237
+ })
238
+ .create ();
239
+ }
240
+
196
241
/**
197
242
* Fills the Emoji Table with the existing emoticons saved in the assets folder.
198
243
*/
@@ -249,20 +294,18 @@ public String getUserAgent() {
249
294
}
250
295
}
251
296
252
- public void addToMutedTopics (JSONArray jsonArray ) {
297
+ public void addToMutedTopics (List < List < String >> mutedTopics ) {
253
298
Stream stream ;
254
299
255
- for (int i = 0 ; i < jsonArray .length (); i ++) {
256
- try {
257
- JSONArray mutedTopic = jsonArray .getJSONArray (i );
258
- stream = Stream .getByName (this , mutedTopic .get (0 ).toString ());
259
- mutedTopics .add (stream .getId () + mutedTopic .get (1 ).toString ());
260
- } catch (JSONException e ) {
261
- Log .e ("JSONException" , "JSON Is not correct" , e );
300
+ if (mutedTopics != null ) {
301
+ for (int i = 0 ; i < mutedTopics .size (); i ++) {
302
+ List <String > mutedTopic = mutedTopics .get (i );
303
+ stream = Stream .getByName (this , mutedTopic .get (0 ));
304
+ this .mutedTopics .add (stream .getId () + mutedTopic .get (1 ));
262
305
}
263
306
}
264
307
SharedPreferences .Editor editor = settings .edit ();
265
- editor .putStringSet (MUTED_TOPIC_KEY , new HashSet <>(mutedTopics ));
308
+ editor .putStringSet (MUTED_TOPIC_KEY , new HashSet <>(this . mutedTopics ));
266
309
editor .apply ();
267
310
}
268
311
0 commit comments