Skip to content

Commit 02783e7

Browse files
committed
use json array object
Signed-off-by: alperozturk <alper_ozturk@proton.me>
1 parent 51f57b8 commit 02783e7

4 files changed

Lines changed: 87 additions & 26 deletions

File tree

app/src/androidTest/java/com/nextcloud/utils/SharePermissionManagerTest.kt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@
77

88
package com.nextcloud.utils
99

10-
import com.google.gson.Gson
1110
import com.owncloud.android.datamodel.quickPermission.QuickPermissionType
1211
import com.owncloud.android.lib.resources.shares.OCShare
1312
import com.owncloud.android.lib.resources.shares.ShareType
14-
import com.owncloud.android.lib.resources.shares.attributes.ShareAttributes
1513
import com.owncloud.android.ui.fragment.util.SharePermissionManager
1614
import junit.framework.TestCase.assertEquals
1715
import junit.framework.TestCase.assertFalse
18-
import junit.framework.TestCase.assertNotNull
1916
import junit.framework.TestCase.assertTrue
2017
import org.junit.Test
2118

@@ -252,17 +249,18 @@ class SharePermissionManagerTest {
252249
// region Attributes Tests
253250
@Test
254251
fun testToggleAllowDownloadAndSyncShouldCreateAttributeJsonIfNoneExists() {
255-
val json = SharePermissionManager.toggleAllowDownloadAndSync(true, null)
256-
assertNotNull(json)
257-
val downloadAttribute = ShareAttributes.createDownloadAttributes(true)
258-
val expectedJson = Gson().toJson(listOf(downloadAttribute))
259-
assertEquals(json, expectedJson)
252+
val attributes = SharePermissionManager.toggleAllowDownloadAndSync(
253+
isChecked = true,
254+
useV2DownloadAttributes = false,
255+
share = null
256+
)
257+
assertTrue(SharePermissionManager.isAllowDownloadAndSyncEnabled(attributes, false))
260258
}
261259

262260
@Test
263261
fun testIsAllowDownloadAndSyncEnabledShouldReturnFalseIfAttributeIsMissing() {
264262
val share = createShare(OCShare.READ_PERMISSION_FLAG, attributesJson = null)
265-
assertFalse(SharePermissionManager.isAllowDownloadAndSyncEnabled(share))
263+
assertFalse(SharePermissionManager.isAllowDownloadAndSyncEnabled(share.attributes, false))
266264
}
267265
// endregion
268266
}

app/src/main/java/com/owncloud/android/ui/fragment/FileDetailsSharingProcessFragment.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import com.owncloud.android.datamodel.quickPermission.QuickPermissionType
2828
import com.owncloud.android.lib.common.utils.Log_OC
2929
import com.owncloud.android.lib.resources.shares.OCShare
3030
import com.owncloud.android.lib.resources.shares.ShareType
31+
import com.owncloud.android.lib.resources.status.NextcloudVersion
3132
import com.owncloud.android.lib.resources.status.OCCapability
3233
import com.owncloud.android.ui.activity.FileActivity
3334
import com.owncloud.android.ui.dialog.ExpirationDatePickerDialogFragment
@@ -629,7 +630,8 @@ class FileDetailsSharingProcessFragment :
629630
}
630631

631632
if (!isPublicShare()) {
632-
shareAllowDownloadAndSyncCheckbox.isChecked = isAllowDownloadAndSyncEnabled(share)
633+
shareAllowDownloadAndSyncCheckbox.isChecked =
634+
isAllowDownloadAndSyncEnabled(share?.attributes, useV2DownloadAttributes())
633635
}
634636
}
635637
}
@@ -652,7 +654,9 @@ class FileDetailsSharingProcessFragment :
652654

653655
if (!isPublicShare()) {
654656
binding.shareAllowDownloadAndSyncCheckbox.setOnCheckedChangeListener { _, isChecked ->
655-
val result = SharePermissionManager.toggleAllowDownloadAndSync(isChecked, share)
657+
val result =
658+
SharePermissionManager
659+
.toggleAllowDownloadAndSync(isChecked, useV2DownloadAttributes(), share)
656660
share?.attributes = result
657661
downloadAttribute = result
658662
}
@@ -899,5 +903,9 @@ class FileDetailsSharingProcessFragment :
899903
(isPublicShare() && capabilities.filesDownloadLimit.isTrue && share?.isFolder == false)
900904

901905
private fun isPublicShare(): Boolean = (shareType == ShareType.PUBLIC_LINK)
906+
907+
private fun useV2DownloadAttributes(): Boolean = capabilities.version
908+
.isNewerOrEqual(NextcloudVersion.nextcloud_30)
909+
902910
// endregion
903911
}

app/src/main/java/com/owncloud/android/ui/fragment/util/SharePermissionManager.kt

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ import com.owncloud.android.lib.common.utils.Log_OC
1212
import com.owncloud.android.lib.resources.shares.OCShare
1313
import com.owncloud.android.lib.resources.shares.attributes.ShareAttributes
1414
import com.owncloud.android.lib.resources.shares.attributes.ShareAttributesJsonHandler
15-
import com.owncloud.android.lib.resources.shares.attributes.getDownloadAttribute
1615
import com.owncloud.android.ui.fragment.FileDetailsSharingProcessFragment.Companion.TAG
16+
import org.json.JSONArray
17+
import org.json.JSONObject
1718

1819
object SharePermissionManager {
1920

@@ -67,28 +68,66 @@ object SharePermissionManager {
6768
// endregion
6869

6970
// region DownloadAttribute
70-
fun toggleAllowDownloadAndSync(isChecked: Boolean, share: OCShare?): String? {
71-
val shareAttributes = getShareAttributes(share)?.toMutableList()
72-
if (shareAttributes == null) {
73-
val downloadAttribute = ShareAttributes.createDownloadAttributes(isChecked)
74-
val updatedShareAttributes = listOf(downloadAttribute)
75-
return ShareAttributesJsonHandler.toJson(updatedShareAttributes)
71+
fun toggleAllowDownloadAndSync(
72+
isChecked: Boolean,
73+
useV2DownloadAttributes: Boolean,
74+
share: OCShare?
75+
): String? {
76+
val jsonArray = if (share?.attributes.isNullOrEmpty()) {
77+
JSONArray()
78+
} else {
79+
JSONArray(share.attributes)
7680
}
7781

78-
val downloadAttributeIndex = shareAttributes.indexOf(shareAttributes.getDownloadAttribute())
79-
if (downloadAttributeIndex >= 0) {
80-
val updatedAttribute = shareAttributes[downloadAttributeIndex].copy(value = isChecked)
81-
shareAttributes[downloadAttributeIndex] = updatedAttribute
82+
val downloadAttr = getDownloadPermissionAttribute(jsonArray)
83+
84+
if (downloadAttr != null) {
85+
if (useV2DownloadAttributes) {
86+
downloadAttr.put("value", isChecked)
87+
} else {
88+
downloadAttr.put("enabled", isChecked)
89+
}
90+
} else {
91+
val newAttr = JSONObject().apply {
92+
put("key", "download")
93+
put("scope", "permissions")
94+
if (useV2DownloadAttributes) {
95+
put("value", isChecked)
96+
} else {
97+
put("enabled", isChecked)
98+
}
99+
}
100+
jsonArray.put(newAttr)
82101
}
83102

84-
return ShareAttributesJsonHandler.toJson(shareAttributes)
103+
return jsonArray.toString()
85104
}
86105

87-
fun isAllowDownloadAndSyncEnabled(share: OCShare?): Boolean {
88-
return getShareAttributes(share).getDownloadAttribute()?.value == true
106+
107+
private fun getDownloadPermissionAttribute(jsonArray: JSONArray): JSONObject? {
108+
for (i in 0 until jsonArray.length()) {
109+
val obj = jsonArray.getJSONObject(i)
110+
if (obj.optString("key") == "download" && obj.optString("scope") == "permissions") {
111+
return obj
112+
}
113+
}
114+
return null
115+
}
116+
117+
fun isAllowDownloadAndSyncEnabled(attributes: String?, useV2DownloadAttributes: Boolean): Boolean {
118+
if (attributes.isNullOrEmpty()) return false
119+
120+
val jsonArray = JSONArray(attributes)
121+
val downloadAttr = getDownloadPermissionAttribute(jsonArray)
122+
123+
return if (useV2DownloadAttributes) {
124+
downloadAttr?.optBoolean("value", false) ?: false
125+
} else {
126+
downloadAttr?.optBoolean("enabled", false) ?: false
127+
}
89128
}
90129

91-
private fun getShareAttributes(share: OCShare?): List<ShareAttributes>? {
130+
private fun getShareDownloadAttributes(share: OCShare?): List<ShareAttributes>? {
92131
return share?.attributes?.let { ShareAttributesJsonHandler.toList(it) }
93132
}
94133
// endregion

gradle/verification-metadata.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12090,6 +12090,14 @@
1209012090
<sha256 value="cd34092733bea9249fbe600b31e018c7c4c344f4201df45a1c16035338a505ca" origin="Generated by Gradle" reason="Artifact is not signed"/>
1209112091
</artifact>
1209212092
</component>
12093+
<component group="com.github.nextcloud" name="android-library" version="42c5fbca05">
12094+
<artifact name="android-library-42c5fbca05.aar">
12095+
<sha256 value="361914f8ec4a86afe93ceeb5af7a2db100acfb7a6d05add8c80e80bfa1d70343" origin="Generated by Gradle" reason="Artifact is not signed"/>
12096+
</artifact>
12097+
<artifact name="android-library-42c5fbca05.module">
12098+
<sha256 value="2f00698736587390e30f3ffb5ed9e801c1f0e80f1304590488624be969186483" origin="Generated by Gradle" reason="Artifact is not signed"/>
12099+
</artifact>
12100+
</component>
1209312101
<component group="com.github.nextcloud" name="android-library" version="451cddeba122ff4d17dc8f88feb2fd7589f77567">
1209412102
<artifact name="android-library-451cddeba122ff4d17dc8f88feb2fd7589f77567.aar">
1209512103
<sha256 value="c253a126ca3c32dd5b373ab1c64668e4603305b3113b052fc0fc5e3c833a913c" origin="Generated by Gradle" reason="Artifact is not signed"/>
@@ -12450,6 +12458,14 @@
1245012458
<sha256 value="af2c66e6fa0f282bb91b53849ff2af8aba66ed301269aa9a71783af49a0e99f4" origin="Generated by Gradle" reason="Artifact is not signed"/>
1245112459
</artifact>
1245212460
</component>
12461+
<component group="com.github.nextcloud" name="android-library" version="e0fb84cbb0">
12462+
<artifact name="android-library-e0fb84cbb0.aar">
12463+
<sha256 value="195b58450aae3eac4b8f24a8e6bb0e7795ab613a026d1c80cf096ee2df36cbd8" origin="Generated by Gradle" reason="Artifact is not signed"/>
12464+
</artifact>
12465+
<artifact name="android-library-e0fb84cbb0.module">
12466+
<sha256 value="548cc8a9156d576dcc46fee694ce3a04eba68c8a5d477796117f806b27c98ed4" origin="Generated by Gradle" reason="Artifact is not signed"/>
12467+
</artifact>
12468+
</component>
1245312469
<component group="com.github.nextcloud" name="android-library" version="e77e21da5928674243f5e329f9eaaa3db1b1c09a">
1245412470
<artifact name="android-library-e77e21da5928674243f5e329f9eaaa3db1b1c09a.aar">
1245512471
<sha256 value="7026ad9ce3b66726f71b97acd19b6aa104ab38cf0b072ccd624900878e5e48bf" origin="Generated by Gradle" reason="Artifact is not signed"/>

0 commit comments

Comments
 (0)