Skip to content

Commit 6cc217b

Browse files
committed
fix potential crashes
Signed-off-by: alperozturk <alper_ozturk@proton.me>
1 parent 3717218 commit 6cc217b

3 files changed

Lines changed: 37 additions & 11 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Nextcloud - Android Client
3+
*
4+
* SPDX-FileCopyrightText: 2025 Alper Ozturk <alper.ozturk@nextcloud.com>
5+
* SPDX-License-Identifier: AGPL-3.0-or-later
6+
*/
7+
8+
package com.nextcloud.extensions
9+
10+
import com.google.gson.JsonObject
11+
12+
fun JsonObject?.getBoolean(key: String): Boolean? {
13+
if (this == null) {
14+
return null
15+
}
16+
17+
if (has(key) && get(key).isJsonPrimitive) {
18+
return get(key).asBoolean
19+
}
20+
21+
return null
22+
}

library/src/main/java/com/owncloud/android/lib/resources/shares/attributes/ShareAttributes.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,16 @@ package com.owncloud.android.lib.resources.shares.attributes
1010
data class ShareAttributes(
1111
val scope: String,
1212
val key: String,
13-
var isEnabled: Boolean
14-
)
13+
var value: Boolean
14+
) {
15+
companion object {
16+
const val DOWNLOAD_ATTRIBUTE_KEY = "download"
1517

16-
fun List<ShareAttributes>?.getDownloadAttribute(): ShareAttributes? = this?.find { it.key == "download" }
18+
fun createDownloadAttributes(value: Boolean): ShareAttributes {
19+
return ShareAttributes(scope = "permissions", key = DOWNLOAD_ATTRIBUTE_KEY, value = value)
20+
}
21+
}
22+
}
23+
24+
fun List<ShareAttributes>?.getDownloadAttribute(): ShareAttributes? =
25+
this?.find { it.key == ShareAttributes.DOWNLOAD_ATTRIBUTE_KEY }

library/src/main/java/com/owncloud/android/lib/resources/shares/attributes/ShareAttributesSerializer.kt

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ package com.owncloud.android.lib.resources.shares.attributes
1010
import com.google.gson.JsonDeserializationContext
1111
import com.google.gson.JsonDeserializer
1212
import com.google.gson.JsonElement
13+
import com.nextcloud.extensions.getBoolean
1314
import java.lang.reflect.Type
1415

1516
/**
@@ -28,13 +29,7 @@ class ShareAttributesDeserializer : JsonDeserializer<ShareAttributes> {
2829
val jsonObject = json?.asJsonObject
2930
val scope = jsonObject?.get("scope")?.asString ?: ""
3031
val key = jsonObject?.get("key")?.asString ?: ""
31-
32-
val status = when {
33-
jsonObject?.has("enabled") == true -> jsonObject.get("enabled").asBoolean
34-
jsonObject?.has("value") == true -> jsonObject.get("value").asBoolean
35-
else -> false
36-
}
37-
38-
return ShareAttributes(scope, key, status)
32+
val value = (jsonObject.getBoolean("value") ?: jsonObject.getBoolean("enabled")) == true
33+
return ShareAttributes(scope, key, value)
3934
}
4035
}

0 commit comments

Comments
 (0)