1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2025-01-23 10:25:54 +00:00

RestApi: Avoid creating duplicate Gson() instances

This commit is contained in:
Catfriend1 2019-01-26 21:46:23 +01:00
parent f9927aa603
commit 56bdd740b5

View file

@ -144,6 +144,8 @@ public class RestApi {
*/ */
private Completion mCompletion = new Completion(); private Completion mCompletion = new Completion();
private Gson mGson;
@Inject NotificationHandler mNotificationHandler; @Inject NotificationHandler mNotificationHandler;
public RestApi(Context context, URL url, String apiKey, OnApiAvailableListener apiListener, public RestApi(Context context, URL url, String apiKey, OnApiAvailableListener apiListener,
@ -154,6 +156,7 @@ public class RestApi {
mApiKey = apiKey; mApiKey = apiKey;
mOnApiAvailableListener = apiListener; mOnApiAvailableListener = apiListener;
mOnConfigChangedListener = configListener; mOnConfigChangedListener = configListener;
mGson = getGson();
} }
public interface OnApiAvailableListener { public interface OnApiAvailableListener {
@ -215,15 +218,15 @@ public class RestApi {
private void onReloadConfigComplete(String result) { private void onReloadConfigComplete(String result) {
Boolean configParseSuccess; Boolean configParseSuccess;
synchronized(mConfigLock) { synchronized(mConfigLock) {
mConfig = new Gson().fromJson(result, Config.class); mConfig = mGson.fromJson(result, Config.class);
configParseSuccess = mConfig != null; configParseSuccess = mConfig != null;
} }
if (!configParseSuccess) { if (!configParseSuccess) {
throw new RuntimeException("config is null: " + result); throw new RuntimeException("config is null: " + result);
} }
Log.d(TAG, "onReloadConfigComplete: Successfully parsed configuration."); Log.d(TAG, "onReloadConfigComplete: Successfully parsed configuration.");
LogV("mConfig.pendingDevices = " + new Gson().toJson(mConfig.pendingDevices)); LogV("mConfig.pendingDevices = " + mGson.toJson(mConfig.pendingDevices));
LogV("mConfig.remoteIgnoredDevices = " + new Gson().toJson(mConfig.remoteIgnoredDevices)); LogV("mConfig.remoteIgnoredDevices = " + mGson.toJson(mConfig.remoteIgnoredDevices));
// Update cached device and folder information stored in the mCompletion model. // Update cached device and folder information stored in the mCompletion model.
mCompletion.updateFromConfig(getDevices(true), getFolders()); mCompletion.updateFromConfig(getDevices(true), getFolders());
@ -340,8 +343,8 @@ public class RestApi {
} }
} }
device.ignoredFolders.add(ignoredFolder); device.ignoredFolders.add(ignoredFolder);
LogV("device.pendingFolders = " + new Gson().toJson(device.pendingFolders)); LogV("device.pendingFolders = " + mGson.toJson(device.pendingFolders));
LogV("device.ignoredFolders = " + new Gson().toJson(device.ignoredFolders)); LogV("device.ignoredFolders = " + mGson.toJson(device.ignoredFolders));
sendConfig(); sendConfig();
Log.d(TAG, "Ignored folder [" + folderId + "] announced by device [" + deviceId + "]"); Log.d(TAG, "Ignored folder [" + folderId + "] announced by device [" + deviceId + "]");
@ -383,8 +386,9 @@ public class RestApi {
private void sendConfig() { private void sendConfig() {
String jsonConfig; String jsonConfig;
synchronized (mConfigLock) { synchronized (mConfigLock) {
jsonConfig = new Gson().toJson(mConfig); jsonConfig = mGson.toJson(mConfig);
} }
// Log.v(TAG, "sendConfig: config=" + jsonConfig);
new PostRequest(mContext, mUrl, PostRequest.URI_SYSTEM_CONFIG, mApiKey, new PostRequest(mContext, mUrl, PostRequest.URI_SYSTEM_CONFIG, mApiKey,
null, jsonConfig, null); null, jsonConfig, null);
mOnConfigChangedListener.onConfigChanged(); mOnConfigChangedListener.onConfigChanged();
@ -396,7 +400,7 @@ public class RestApi {
public void saveConfigAndRestart() { public void saveConfigAndRestart() {
String jsonConfig; String jsonConfig;
synchronized (mConfigLock) { synchronized (mConfigLock) {
jsonConfig = new Gson().toJson(mConfig); jsonConfig = mGson.toJson(mConfig);
} }
new PostRequest(mContext, mUrl, PostRequest.URI_SYSTEM_CONFIG, mApiKey, new PostRequest(mContext, mUrl, PostRequest.URI_SYSTEM_CONFIG, mApiKey,
null, jsonConfig, result -> { null, jsonConfig, result -> {
@ -600,7 +604,7 @@ public class RestApi {
new GetRequest(mContext, mUrl, GetRequest.URI_SYSTEM_STATUS, mApiKey, null, result -> { new GetRequest(mContext, mUrl, GetRequest.URI_SYSTEM_STATUS, mApiKey, null, result -> {
SystemStatus systemStatus; SystemStatus systemStatus;
try { try {
systemStatus = new Gson().fromJson(result, SystemStatus.class); systemStatus = mGson.fromJson(result, SystemStatus.class);
listener.onResult(systemStatus); listener.onResult(systemStatus);
} catch (Exception e) { } catch (Exception e) {
Log.e(TAG, "getSystemStatus: Parsing REST API result failed. result=" + result); Log.e(TAG, "getSystemStatus: Parsing REST API result failed. result=" + result);
@ -620,7 +624,7 @@ public class RestApi {
public void getFolderIgnoreList(String folderId, OnResultListener1<FolderIgnoreList> listener) { public void getFolderIgnoreList(String folderId, OnResultListener1<FolderIgnoreList> listener) {
new GetRequest(mContext, mUrl, GetRequest.URI_DB_IGNORES, mApiKey, new GetRequest(mContext, mUrl, GetRequest.URI_DB_IGNORES, mApiKey,
ImmutableMap.of("folder", folderId), result -> { ImmutableMap.of("folder", folderId), result -> {
FolderIgnoreList folderIgnoreList = new Gson().fromJson(result, FolderIgnoreList.class); FolderIgnoreList folderIgnoreList = mGson.fromJson(result, FolderIgnoreList.class);
listener.onResult(folderIgnoreList); listener.onResult(folderIgnoreList);
}); });
} }
@ -632,7 +636,7 @@ public class RestApi {
FolderIgnoreList folderIgnoreList = new FolderIgnoreList(); FolderIgnoreList folderIgnoreList = new FolderIgnoreList();
folderIgnoreList.ignore = ignore; folderIgnoreList.ignore = ignore;
new PostRequest(mContext, mUrl, PostRequest.URI_DB_IGNORES, mApiKey, new PostRequest(mContext, mUrl, PostRequest.URI_DB_IGNORES, mApiKey,
ImmutableMap.of("folder", folderId), new Gson().toJson(folderIgnoreList), null); ImmutableMap.of("folder", folderId), mGson.toJson(folderIgnoreList), null);
} }
/** /**
@ -640,7 +644,7 @@ public class RestApi {
*/ */
public void getSystemVersion(OnResultListener1<SystemVersion> listener) { public void getSystemVersion(OnResultListener1<SystemVersion> listener) {
new GetRequest(mContext, mUrl, GetRequest.URI_VERSION, mApiKey, null, result -> { new GetRequest(mContext, mUrl, GetRequest.URI_VERSION, mApiKey, null, result -> {
SystemVersion systemVersion = new Gson().fromJson(result, SystemVersion.class); SystemVersion systemVersion = mGson.fromJson(result, SystemVersion.class);
listener.onResult(systemVersion); listener.onResult(systemVersion);
}); });
} }
@ -658,7 +662,7 @@ public class RestApi {
} }
mPreviousConnectionTime = now; mPreviousConnectionTime = now;
Connections connections = new Gson().fromJson(result, Connections.class); Connections connections = mGson.fromJson(result, Connections.class);
for (Map.Entry<String, Connections.Connection> e : connections.connections.entrySet()) { for (Map.Entry<String, Connections.Connection> e : connections.connections.entrySet()) {
e.getValue().completion = mCompletion.getDeviceCompletion(e.getKey()); e.getValue().completion = mCompletion.getDeviceCompletion(e.getKey());
@ -682,7 +686,7 @@ public class RestApi {
public void getFolderStatus(final String folderId, final OnResultListener2<String, FolderStatus> listener) { public void getFolderStatus(final String folderId, final OnResultListener2<String, FolderStatus> listener) {
new GetRequest(mContext, mUrl, GetRequest.URI_DB_STATUS, mApiKey, new GetRequest(mContext, mUrl, GetRequest.URI_DB_STATUS, mApiKey,
ImmutableMap.of("folder", folderId), result -> { ImmutableMap.of("folder", folderId), result -> {
FolderStatus m = new Gson().fromJson(result, FolderStatus.class); FolderStatus m = mGson.fromJson(result, FolderStatus.class);
mCachedFolderStatuses.put(folderId, m); mCachedFolderStatuses.put(folderId, m);
listener.onResult(folderId, m); listener.onResult(folderId, m);
}); });
@ -702,7 +706,7 @@ public class RestApi {
JsonArray jsonDiskEvents = new JsonParser().parse(result).getAsJsonArray(); JsonArray jsonDiskEvents = new JsonParser().parse(result).getAsJsonArray();
for (int i = jsonDiskEvents.size()-1; i >= 0; i--) { for (int i = jsonDiskEvents.size()-1; i >= 0; i--) {
JsonElement jsonDiskEvent = jsonDiskEvents.get(i); JsonElement jsonDiskEvent = jsonDiskEvents.get(i);
diskEvents.add(new Gson().fromJson(jsonDiskEvent, DiskEvent.class)); diskEvents.add(mGson.fromJson(jsonDiskEvent, DiskEvent.class));
} }
listener.onResult(diskEvents); listener.onResult(diskEvents);
} catch (Exception e) { } catch (Exception e) {
@ -743,7 +747,7 @@ public class RestApi {
for (int i = 0; i < jsonEvents.size(); i++) { for (int i = 0; i < jsonEvents.size(); i++) {
JsonElement json = jsonEvents.get(i); JsonElement json = jsonEvents.get(i);
Event event = new Gson().fromJson(json, Event.class); Event event = mGson.fromJson(json, Event.class);
if (lastId < event.id) if (lastId < event.id)
lastId = event.id; lastId = event.id;
@ -882,6 +886,12 @@ public class RestApi {
} }
} }
private Gson getGson() {
Gson gson = new GsonBuilder()
.create();
return gson;
}
private void LogV(String logMessage) { private void LogV(String logMessage) {
if (ENABLE_VERBOSE_LOG) { if (ENABLE_VERBOSE_LOG) {
Log.v(TAG, logMessage); Log.v(TAG, logMessage);