Skip to content

Commit 25a222f

Browse files
committed
implement allow download and sync
Signed-off-by: alperozturk96 <alper_ozturk@proton.me>
1 parent e99109c commit 25a222f

File tree

9 files changed

+123
-10
lines changed

9 files changed

+123
-10
lines changed

app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRepository.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import com.nextcloud.android.sso.helper.SingleAccountHelper;
5050
import com.nextcloud.android.sso.model.SingleSignOnAccount;
5151
import com.owncloud.android.lib.common.utils.Log_OC;
52+
import com.owncloud.android.lib.resources.shares.OCShare;
5253

5354
import java.util.ArrayList;
5455
import java.util.Calendar;
@@ -1006,8 +1007,12 @@ public void addShareEntities(List<ShareEntity> entities) {
10061007
db.getShareDao().addShareEntities(entities);
10071008
}
10081009

1009-
public List<ShareEntity> getShareEntities(String path) {
1010-
return db.getShareDao().getShareEntities(path);
1010+
public ShareEntity getShareByPathAndDisplayName(@NonNull OCShare share) {
1011+
if (share.getPath() == null || share.getSharedWithDisplayName() == null) {
1012+
return null;
1013+
}
1014+
1015+
return db.getShareDao().getShareByPathAndDisplayName(share.getPath(), share.getSharedWithDisplayName());
10111016
}
10121017

10131018
public void deleteShareById(int id) {

app/src/main/java/it/niedermann/owncloud/notes/persistence/dao/ShareDao.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,16 @@ interface ShareDao {
1818
@Insert(onConflict = OnConflictStrategy.REPLACE)
1919
fun addShareEntities(entities: List<ShareEntity>)
2020

21-
@Query("SELECT * FROM share_table WHERE path = :path")
22-
fun getShareEntities(path: String): List<ShareEntity>
21+
@Query("""
22+
SELECT * FROM share_table
23+
WHERE path = :path
24+
AND share_with_displayname = :displayName
25+
LIMIT 1
26+
""")
27+
fun getShareByPathAndDisplayName(
28+
path: String,
29+
displayName: String
30+
): ShareEntity?
2331

2432
@Query("DELETE FROM share_table WHERE id = :id")
2533
fun deleteById(id: Int)

app/src/main/java/it/niedermann/owncloud/notes/persistence/entity/ShareEntity.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ data class ShareEntity(
2626
val displayname_owner: String? = null,
2727
val url: String? = null,
2828
val expiration_date: Long? = null,
29-
val permissions: Double? = null
29+
val permissions: Double? = null,
30+
val attributes: String? = null
3031
)

app/src/main/java/it/niedermann/owncloud/notes/share/NoteShareActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -865,8 +865,8 @@ public void setPasswordToShare(@NotNull OCShare share, @Nullable String password
865865
share.getNote(),
866866
share.getLabel(),
867867
DateUtil.INSTANCE.getExpirationDate(share.getExpirationDate()),
868-
"[]",
869-
Boolean.toString(share.isHideFileDownload())
868+
Boolean.toString(share.isHideFileDownload()),
869+
""
870870
);
871871
final var result = repository.updateShare(share.getId(), requestBody);
872872

app/src/main/java/it/niedermann/owncloud/notes/share/NoteShareDetailActivity.kt

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import it.niedermann.owncloud.notes.persistence.isSuccess
2727
import it.niedermann.owncloud.notes.share.dialog.ExpirationDatePickerDialogFragment
2828
import it.niedermann.owncloud.notes.share.helper.SharePermissionManager
2929
import it.niedermann.owncloud.notes.share.model.QuickPermissionType
30+
import it.niedermann.owncloud.notes.share.model.ShareAttributesV1
31+
import it.niedermann.owncloud.notes.share.model.ShareAttributesV2
3032
import it.niedermann.owncloud.notes.share.model.UpdateShareRequest
3133
import it.niedermann.owncloud.notes.share.repository.ShareRepository
3234
import it.niedermann.owncloud.notes.shared.util.DisplayUtils
@@ -149,6 +151,7 @@ class NoteShareDetailActivity :
149151
}
150152

151153
util.androidx.run {
154+
colorSwitchCompat(allowDownloadAndSync)
152155
colorSwitchCompat(shareProcessSetPasswordSwitch)
153156
colorSwitchCompat(shareProcessSetExpDateSwitch)
154157
colorSwitchCompat(shareProcessHideDownloadCheckbox)
@@ -326,6 +329,23 @@ class NoteShareDetailActivity :
326329
shareProcessChangeNameContainer.visibility = View.GONE
327330
shareProcessHideDownloadCheckbox.visibility = View.GONE
328331
shareProcessSetPasswordSwitch.visibility = View.GONE
332+
allowDownloadAndSync.visibility = View.VISIBLE
333+
334+
share?.let {
335+
val entity = repository.getShareByPathAndDisplayName(it)
336+
val attributes = entity?.attributes ?: return
337+
share?.attributes = attributes
338+
339+
val capabilities = repository.getCapabilities()
340+
val shouldUseShareAttributesV2 = (capabilities.nextcloudMajorVersion?.toInt() ?: 0) >= 30
341+
val isDownloadAndAllowsSyncEnabled = if (shouldUseShareAttributesV2) {
342+
ShareAttributesV2.getAttributes(attributes).first().value
343+
} else {
344+
ShareAttributesV1.getAttributes(attributes).first().enabled
345+
}
346+
347+
binding.allowDownloadAndSync.isChecked = isDownloadAndAllowsSyncEnabled
348+
}
329349
}
330350
}
331351

@@ -577,7 +597,11 @@ class NoteShareDetailActivity :
577597
expireDate = DateUtil.getExpirationDate(chosenExpDateInMills),
578598
label = label,
579599
note = noteText,
580-
attributes = "[]",
600+
attributes = UpdateShareRequest.createAttributes(
601+
repository.getCapabilities(),
602+
binding.allowDownloadAndSync.isChecked,
603+
shareType
604+
),
581605
hideDownload = binding.shareProcessHideDownloadCheckbox.isChecked.toString()
582606
)
583607

app/src/main/java/it/niedermann/owncloud/notes/share/model/UpdateShareRequest.kt

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
*/
77
package it.niedermann.owncloud.notes.share.model
88

9+
import com.google.gson.Gson
910
import com.google.gson.annotations.Expose
1011
import com.google.gson.annotations.SerializedName
12+
import com.google.gson.reflect.TypeToken
13+
import com.owncloud.android.lib.resources.shares.ShareType
14+
import it.niedermann.owncloud.notes.shared.model.Capabilities
1115

1216
data class UpdateShareRequest(
1317
@Expose
@@ -37,4 +41,58 @@ data class UpdateShareRequest(
3741
@Expose
3842
@SerializedName("attributes")
3943
val attributes: String? = null
40-
)
44+
) {
45+
companion object {
46+
fun createAttributes(capabilities: Capabilities, allowDownloadAndSync: Boolean, type: ShareType): String {
47+
if (type != ShareType.INTERNAL && type != ShareType.USER) {
48+
return "[]"
49+
}
50+
51+
val shouldUseShareAttributesV2 = (capabilities.nextcloudMajorVersion?.toInt() ?: 0) >= 30
52+
53+
val shareAttributes = arrayOf(
54+
if (shouldUseShareAttributesV2) {
55+
ShareAttributesV2(
56+
scope = "permissions",
57+
key = "download",
58+
value = allowDownloadAndSync
59+
)
60+
} else {
61+
ShareAttributesV1(
62+
scope = "permissions",
63+
key = "download",
64+
enabled = allowDownloadAndSync
65+
)
66+
}
67+
)
68+
69+
return Gson().toJson(shareAttributes)
70+
}
71+
}
72+
}
73+
74+
data class ShareAttributesV2(
75+
var scope: String,
76+
var key: String,
77+
var value: Boolean
78+
) {
79+
companion object {
80+
fun getAttributes(json: String): List<ShareAttributesV2> {
81+
val type = object : TypeToken<List<ShareAttributesV2>>() {}.type
82+
return Gson().fromJson(json, type)
83+
}
84+
}
85+
}
86+
87+
data class ShareAttributesV1(
88+
var scope: String,
89+
var key: String,
90+
var enabled: Boolean
91+
) {
92+
companion object {
93+
fun getAttributes(json: String): List<ShareAttributesV1> {
94+
val typeV1 = object : TypeToken<List<ShareAttributesV1>>() {}.type
95+
return Gson().fromJson(json, typeV1)
96+
}
97+
}
98+
}

app/src/main/java/it/niedermann/owncloud/notes/share/repository/ShareRepository.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ class ShareRepository(private val applicationContext: Context, private val accou
9494
val url = map?.get("url") as? String
9595
val expirationDateString = map?.get("expiration") as? String
9696
val permissions = map?.get("permissions") as? Double
97+
val attributes = map?.get("attributes") as? String
9798

9899
id?.toInt()?.let {
99100
val entity = ShareEntity(
@@ -109,7 +110,8 @@ class ShareRepository(private val applicationContext: Context, private val accou
109110
displayname_owner = displayNameOwner,
110111
url = url,
111112
expiration_date = expirationDateString?.toExpirationDateLong(),
112-
permissions = permissions
113+
permissions = permissions,
114+
attributes = attributes
113115
)
114116

115117
entities.add(entity)
@@ -123,6 +125,10 @@ class ShareRepository(private val applicationContext: Context, private val accou
123125
}
124126
}
125127

128+
fun getShareByPathAndDisplayName(share: OCShare): ShareEntity? {
129+
return notesRepository.getShareByPathAndDisplayName(share)
130+
}
131+
126132
private fun LinkedTreeMap<*, *>.getList(key: String): ArrayList<*>? = this[key] as? ArrayList<*>
127133

128134
/**

app/src/main/res/layout/activity_note_share_detail.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,16 @@
135135
android:background="@color/text_color" />
136136

137137

138+
<com.google.android.material.materialswitch.MaterialSwitch
139+
android:id="@+id/allow_download_and_sync"
140+
android:layout_width="match_parent"
141+
android:layout_height="wrap_content"
142+
android:layout_marginTop="@dimen/spacer_1x"
143+
android:minHeight="@dimen/minimum_size_for_touchable_area"
144+
android:text="@string/note_share_detail_activity_allow_download_and_sync"
145+
android:visibility="gone"
146+
tools:visibility="visible" />
147+
138148
<com.google.android.material.materialswitch.MaterialSwitch
139149
android:id="@+id/share_process_hide_download_checkbox"
140150
android:layout_width="match_parent"

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
<!-- NoteShareDetailActivity -->
7272
<string name="note_share_detail_activity_password_error_message">This password looks a bit weak. For better security, try using a longer password with a mix of letters, numbers, and symbols.</string>
7373
<string name="note_share_detail_activity_share_via_link_hide_download">Hide download</string>
74+
<string name="note_share_detail_activity_allow_download_and_sync">Allow download and sync</string>
7475
<string name="note_share_detail_activity_share_send_note">Note to recipient</string>
7576
<string name="note_share_detail_activity_hint_note">Note</string>
7677
<string name="note_share_detail_activity_common_next">Next</string>

0 commit comments

Comments
 (0)