From 71505071bef2610d8d5a62b2cc4e94febb15b342 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Tue, 19 Dec 2023 16:28:37 +0100 Subject: [PATCH] Use interfaceSerializer Signed-off-by: alperozturk --- .../files/downloader/FilesDownloadWorker.kt | 12 +++- .../client/jobs/BackgroundJobManagerImpl.kt | 5 +- .../nextcloud/utils/InterfaceSerializer.java | 56 +++++++++++++++++++ 3 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/com/nextcloud/utils/InterfaceSerializer.java diff --git a/app/src/main/java/com/nextcloud/client/files/downloader/FilesDownloadWorker.kt b/app/src/main/java/com/nextcloud/client/files/downloader/FilesDownloadWorker.kt index 787bcb4f10ff..d81c61358b80 100644 --- a/app/src/main/java/com/nextcloud/client/files/downloader/FilesDownloadWorker.kt +++ b/app/src/main/java/com/nextcloud/client/files/downloader/FilesDownloadWorker.kt @@ -39,9 +39,12 @@ import androidx.localbroadcastmanager.content.LocalBroadcastManager import androidx.work.Worker import androidx.work.WorkerParameters import com.google.gson.Gson +import com.google.gson.GsonBuilder import com.nextcloud.client.account.User import com.nextcloud.client.account.UserAccountManager import com.nextcloud.java.util.Optional +import com.nextcloud.utils.InterfaceSerializer +import com.nextcloud.utils.InterfaceSerializer.interfaceSerializer import com.owncloud.android.R import com.owncloud.android.authentication.AuthenticatorActivity import com.owncloud.android.datamodel.FileDataStorageManager @@ -85,7 +88,6 @@ class FilesDownloadWorker( companion object { private val TAG = FilesDownloadWorker::class.java.simpleName - var user: User? = null const val USER = "USER" const val FILE = "FILE" const val BEHAVIOUR = "BEHAVIOUR" @@ -144,9 +146,17 @@ class FilesDownloadWorker( } } + // FIXME stackoverflow + private fun getUserGson(): Gson { + return GsonBuilder() + .registerTypeAdapter(User::class.java, interfaceSerializer(User::class.java)) + .create() + } + private fun getRequestDownloads(): AbstractList { conflictUploadId = inputData.keyValueMap[CONFLICT_UPLOAD_ID] as Long? val file = gson.fromJson(inputData.keyValueMap[FILE] as String, OCFile::class.java) + val user = getUserGson().fromJson(inputData.keyValueMap[USER] as String, User::class.java) val downloadTypeAsString = inputData.keyValueMap[DOWNLOAD_TYPE] as String? val downloadType = if (downloadTypeAsString != null) { if (downloadTypeAsString == DownloadType.DOWNLOAD.toString()) { diff --git a/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt b/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt index 49a2f50e23c4..58d55606f79c 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt @@ -473,9 +473,8 @@ internal class BackgroundJobManagerImpl( ) { val gson = Gson() - // FIXME user interface cant serialize and deserialize val data = workDataOf( - //FilesDownloadWorker.USER to gson.toJson(user), + FilesDownloadWorker.USER to gson.toJson(user), FilesDownloadWorker.FILE to gson.toJson(ocFile), FilesDownloadWorker.BEHAVIOUR to behaviour, FilesDownloadWorker.DOWNLOAD_TYPE to downloadType.toString(), @@ -484,8 +483,6 @@ internal class BackgroundJobManagerImpl( FilesDownloadWorker.CONFLICT_UPLOAD_ID to conflictUploadId, ) - FilesDownloadWorker.user = user - val request = oneTimeRequestBuilder(FilesDownloadWorker::class, JOB_FILES_DOWNLOAD, user) .setInputData(data) .build() diff --git a/app/src/main/java/com/nextcloud/utils/InterfaceSerializer.java b/app/src/main/java/com/nextcloud/utils/InterfaceSerializer.java new file mode 100644 index 000000000000..da253c14d9b5 --- /dev/null +++ b/app/src/main/java/com/nextcloud/utils/InterfaceSerializer.java @@ -0,0 +1,56 @@ +/* + * Nextcloud Android client application + * + * @author Alper Ozturk + * Copyright (C) 2023 Alper Ozturk + * Copyright (C) 2023 Nextcloud GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package com.nextcloud.utils; + +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; + +import java.lang.reflect.Type; + +final public class InterfaceSerializer implements JsonSerializer, JsonDeserializer { + + private final Class implementationClass; + + private InterfaceSerializer(final Class implementationClass) { + this.implementationClass = implementationClass; + } + + public static InterfaceSerializer interfaceSerializer(final Class implementationClass) { + return new InterfaceSerializer<>(implementationClass); + } + + @Override + public JsonElement serialize(final T value, final Type type, final JsonSerializationContext context) { + final Type targetType = value != null + ? value.getClass() + : type; + return context.serialize(value, targetType); + } + + @Override + public T deserialize(final JsonElement jsonElement, final Type typeOfT, final JsonDeserializationContext context) { + return context.deserialize(jsonElement, implementationClass); + } +}