Explorar o código

Use interfaceSerializer

Signed-off-by: alperozturk <alper_ozturk@proton.me>
alperozturk hai 1 ano
pai
achega
2b595910b4

+ 11 - 1
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<String> {
         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()) {

+ 1 - 4
app/src/main/java/com/nextcloud/client/jobs/BackgroundJobManagerImpl.kt

@@ -520,9 +520,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(),
@@ -531,8 +530,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()

+ 56 - 0
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 <https://www.gnu.org/licenses/>.
+ */
+
+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<T> implements JsonSerializer<T>, JsonDeserializer<T> {
+
+    private final Class<T> implementationClass;
+
+    private InterfaceSerializer(final Class<T> implementationClass) {
+        this.implementationClass = implementationClass;
+    }
+
+    public static <T> InterfaceSerializer<T> interfaceSerializer(final Class<T> 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);
+    }
+}