diff --git a/library/src/androidTest/java/com/owncloud/android/lib/resources/shares/CreateShareRemoteOperationIT.kt b/library/src/androidTest/java/com/owncloud/android/lib/resources/shares/CreateShareRemoteOperationIT.kt index a239adf67d..0471536ed0 100644 --- a/library/src/androidTest/java/com/owncloud/android/lib/resources/shares/CreateShareRemoteOperationIT.kt +++ b/library/src/androidTest/java/com/owncloud/android/lib/resources/shares/CreateShareRemoteOperationIT.kt @@ -9,8 +9,6 @@ package com.owncloud.android.lib.resources.shares import com.owncloud.android.AbstractIT import com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation -import com.owncloud.android.lib.resources.shares.attributes.ShareAttributes -import com.owncloud.android.lib.resources.shares.attributes.ShareAttributesJsonHandler import com.owncloud.android.lib.resources.status.GetStatusRemoteOperation import com.owncloud.android.lib.resources.status.NextcloudVersion import com.owncloud.android.lib.resources.status.OwnCloudVersion @@ -30,18 +28,6 @@ class CreateShareRemoteOperationIT : AbstractIT() { Assume.assumeTrue(ownCloudVersion.isNewerOrEqual(NextcloudVersion.nextcloud_24)) } - @Test - fun createShareWithNoteAndAttributes() { - val attributes = listOf(ShareAttributes.createDownloadAttributes(true)) - val note = "Note with attributes" - val path = "/shareWithAttributes/" - - createFolder(path) - val share = createShare(path, "admin", note, ShareAttributesJsonHandler.toJson(attributes)) - assertEquals(note, share.note) - assertEquals(attributes, ShareAttributesJsonHandler.toList(share.attributes)) - } - @Test fun createShareWithNote() { val note = "This is the note" diff --git a/library/src/main/java/com/owncloud/android/lib/resources/shares/attributes/ShareAttributes.kt b/library/src/main/java/com/owncloud/android/lib/resources/shares/attributes/ShareAttributes.kt deleted file mode 100644 index 56c685e9fc..0000000000 --- a/library/src/main/java/com/owncloud/android/lib/resources/shares/attributes/ShareAttributes.kt +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Nextcloud Android Library - * - * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors - * SPDX-FileCopyrightText: 2025 Alper Ozturk - * SPDX-License-Identifier: MIT - */ - -package com.owncloud.android.lib.resources.shares.attributes - -data class ShareAttributes( - val scope: String, - val key: String, - var value: Boolean -) { - companion object { - const val DOWNLOAD_ATTRIBUTE_KEY = "download" - - fun createDownloadAttributes(value: Boolean): ShareAttributes = - ShareAttributes(scope = "permissions", key = DOWNLOAD_ATTRIBUTE_KEY, value = value) - } -} - -fun List?.getDownloadAttribute(): ShareAttributes? = - this?.find { it.key == ShareAttributes.DOWNLOAD_ATTRIBUTE_KEY } diff --git a/library/src/main/java/com/owncloud/android/lib/resources/shares/attributes/ShareAttributesDeserializer.kt b/library/src/main/java/com/owncloud/android/lib/resources/shares/attributes/ShareAttributesDeserializer.kt deleted file mode 100644 index 3457b1240a..0000000000 --- a/library/src/main/java/com/owncloud/android/lib/resources/shares/attributes/ShareAttributesDeserializer.kt +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Nextcloud Android Library - * - * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors - * SPDX-FileCopyrightText: 2025 Alper Ozturk - * SPDX-License-Identifier: MIT - */ - -package com.owncloud.android.lib.resources.shares.attributes - -import com.google.gson.JsonDeserializationContext -import com.google.gson.JsonDeserializer -import com.google.gson.JsonElement -import com.nextcloud.extensions.getBoolean -import java.lang.reflect.Type - -/** - * Custom serializer for the ShareAttributes class. - * This handles the deserialization and serialization of the ShareAttributes data class. - * Since Nextcloud 30, the enabled key have been renamed to value and supports more than boolean. - * - * https://docs.nextcloud.com/server/latest/developer_manual/client_apis/OCS/ocs-share-api.html#share-attributes - */ -class ShareAttributesDeserializer : JsonDeserializer { - override fun deserialize( - json: JsonElement?, - typeOfT: Type?, - context: JsonDeserializationContext? - ): ShareAttributes? { - val jsonObject = json?.asJsonObject - val scope = jsonObject?.get("scope")?.asString ?: "" - val key = jsonObject?.get("key")?.asString ?: "" - val value = (jsonObject.getBoolean("value") ?: jsonObject.getBoolean("enabled")) == true - return ShareAttributes(scope, key, value) - } -} diff --git a/library/src/main/java/com/owncloud/android/lib/resources/shares/attributes/ShareAttributesJsonHandler.kt b/library/src/main/java/com/owncloud/android/lib/resources/shares/attributes/ShareAttributesJsonHandler.kt deleted file mode 100644 index 50c4d5a68f..0000000000 --- a/library/src/main/java/com/owncloud/android/lib/resources/shares/attributes/ShareAttributesJsonHandler.kt +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Nextcloud Android Library - * - * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors - * SPDX-FileCopyrightText: 2025 Alper Ozturk - * SPDX-License-Identifier: MIT - */ - -package com.owncloud.android.lib.resources.shares.attributes - -import com.google.gson.GsonBuilder -import com.google.gson.reflect.TypeToken - -object ShareAttributesJsonHandler { - private val gson = - GsonBuilder() - .registerTypeAdapter(ShareAttributes::class.java, ShareAttributesDeserializer()) - .create() - - fun toList(jsonString: String?): List? { - if (jsonString == null) { - return null - } - - val listType = object : TypeToken>() {}.type - return gson.fromJson(jsonString, listType) - } - - fun toJson(shareAttributes: List?): String? { - if (shareAttributes == null) { - return null - } - - return gson.toJson(shareAttributes) - } -} diff --git a/library/src/main/java/com/owncloud/android/lib/resources/shares/extensions/OCShareExtensions.kt b/library/src/main/java/com/owncloud/android/lib/resources/shares/extensions/OCShareExtensions.kt new file mode 100644 index 0000000000..ec1380c5d9 --- /dev/null +++ b/library/src/main/java/com/owncloud/android/lib/resources/shares/extensions/OCShareExtensions.kt @@ -0,0 +1,69 @@ +/* + * Nextcloud Android Library + * + * SPDX-FileCopyrightText: 2025 Alper Ozturk + * SPDX-License-Identifier: MIT + */ + +package com.owncloud.android.lib.resources.shares.extensions + +import com.owncloud.android.lib.resources.shares.OCShare +import org.json.JSONArray +import org.json.JSONObject + +private const val KEY = "key" +private const val SCOPE_KEY = "scope" +private const val DOWNLOAD_KEY = "download" +private const val PERMISSIONS_KEY = "permissions" +private const val VALUE_KEY = "value" +private const val ENABLED_KEY = "enabled" + +fun toggleAllowDownloadAndSync( + attributes: String?, + isChecked: Boolean, + useV2DownloadAttributes: Boolean +): String? { + var jsonArray = JSONArray() + if (!attributes.isNullOrEmpty()) { + jsonArray = JSONArray(attributes) + } + + val downloadAttr = jsonArray.findDownloadAttribute() + val enabledKey = getEnabledKey(useV2DownloadAttributes) + + if (downloadAttr != null) { + downloadAttr.put(enabledKey, isChecked) + } else { + jsonArray.put( + JSONObject().apply { + put(KEY, DOWNLOAD_KEY) + put(SCOPE_KEY, PERMISSIONS_KEY) + put(enabledKey, isChecked) + } + ) + } + + return jsonArray.toString() +} + +@Suppress("ReturnCount") +fun OCShare?.isAllowDownloadAndSyncEnabled(useV2DownloadAttributes: Boolean): Boolean { + if (this?.attributes.isNullOrEmpty()) return false + + val jsonArray = JSONArray(this.attributes) + val downloadAttr = jsonArray.findDownloadAttribute() ?: return false + val enabledKey = getEnabledKey(useV2DownloadAttributes) + + return downloadAttr.optBoolean(enabledKey, false) +} + +private fun JSONArray.findDownloadAttribute(): JSONObject? = + (0 until length()) + .asSequence() + .map { getJSONObject(it) } + .find { + it.optString(KEY) == DOWNLOAD_KEY && + it.optString(SCOPE_KEY) == PERMISSIONS_KEY + } + +private fun getEnabledKey(isV2: Boolean): String = if (isV2) VALUE_KEY else ENABLED_KEY