-
-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathNoteShareDetailActivity.kt
More file actions
630 lines (550 loc) · 23.6 KB
/
NoteShareDetailActivity.kt
File metadata and controls
630 lines (550 loc) · 23.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
/*
* Nextcloud Notes - Android Client
*
* SPDX-FileCopyrightText: 2015-2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package it.niedermann.owncloud.notes.share
import android.content.Intent
import android.content.res.Configuration
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.nextcloud.android.common.ui.theme.utils.ColorRole
import com.nextcloud.android.sso.helper.SingleAccountHelper
import com.owncloud.android.lib.common.utils.Log_OC
import com.owncloud.android.lib.resources.shares.OCShare
import com.owncloud.android.lib.resources.shares.SharePermissionsBuilder
import com.owncloud.android.lib.resources.shares.ShareType
import it.niedermann.owncloud.notes.R
import it.niedermann.owncloud.notes.branding.BrandedActivity
import it.niedermann.owncloud.notes.branding.BrandingUtil
import it.niedermann.owncloud.notes.databinding.ActivityNoteShareDetailBinding
import it.niedermann.owncloud.notes.persistence.entity.Note
import it.niedermann.owncloud.notes.persistence.isSuccess
import it.niedermann.owncloud.notes.share.dialog.ExpirationDatePickerDialogFragment
import it.niedermann.owncloud.notes.share.helper.SharingMenuHelper
import it.niedermann.owncloud.notes.share.model.SharePasswordRequest
import it.niedermann.owncloud.notes.share.repository.ShareRepository
import it.niedermann.owncloud.notes.shared.util.DisplayUtils
import it.niedermann.owncloud.notes.shared.util.clipboard.ClipboardUtil
import it.niedermann.owncloud.notes.shared.util.extensions.getParcelableArgument
import it.niedermann.owncloud.notes.shared.util.extensions.getSerializableArgument
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.text.SimpleDateFormat
import java.util.Date
/**
* Activity class to show share permission options, set expiration date, change label, set password, send note
*
* This activity handles following:
* 1. This will be shown while creating new internal and external share. So that user can set every share
* configuration at one time.
* 2. This will handle both Advanced Permissions and Send New Email functionality for existing shares to modify them.
*/
@Suppress("TooManyFunctions")
class NoteShareDetailActivity :
BrandedActivity(),
ExpirationDatePickerDialogFragment.OnExpiryDateListener {
companion object {
const val TAG = "NoteShareDetailActivity"
const val ARG_NOTE = "arg_sharing_note"
const val ARG_SHAREE_NAME = "arg_sharee_name"
const val ARG_SHARE_TYPE = "arg_share_type"
const val ARG_OCSHARE = "arg_ocshare"
const val ARG_SCREEN_TYPE = "arg_screen_type"
const val ARG_RESHARE_SHOWN = "arg_reshare_shown"
const val ARG_EXP_DATE_SHOWN = "arg_exp_date_shown"
private const val ARG_SECURE_SHARE = "secure_share"
// types of screens to be displayed
const val SCREEN_TYPE_PERMISSION = 1 // permissions screen
const val SCREEN_TYPE_NOTE = 2 // note screen
}
private lateinit var binding: ActivityNoteShareDetailBinding
private var note: Note? = null // note to be share
private var shareeName: String? = null
private lateinit var shareType: ShareType
private var shareProcessStep = SCREEN_TYPE_PERMISSION // default screen type
private var permission = OCShare.NO_PERMISSION // no permission
private var chosenExpDateInMills: Long = -1 // for no expiry date
private var share: OCShare? = null
private var isReShareShown: Boolean = true // show or hide reShare option
private var isExpDateShown: Boolean = true // show or hide expiry date option
private var isSecureShare: Boolean = false
private var expirationDatePickerFragment: ExpirationDatePickerDialogFragment? = null
private lateinit var repository: ShareRepository
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityNoteShareDetailBinding.inflate(layoutInflater)
setContentView(binding.root)
setSupportActionBar(binding.toolbar)
binding.toolbar.setNavigationOnClickListener({ v -> backPressed() })
val arguments = intent.extras
arguments?.let {
note = it.getSerializableArgument(ARG_NOTE, Note::class.java)
shareeName = it.getString(ARG_SHAREE_NAME)
share = it.getParcelableArgument(ARG_OCSHARE, OCShare::class.java)
if (it.containsKey(ARG_SHARE_TYPE)) {
shareType = ShareType.fromValue(it.getInt(ARG_SHARE_TYPE))
} else if (share != null) {
shareType = share!!.shareType!!
}
shareProcessStep = it.getInt(ARG_SCREEN_TYPE, SCREEN_TYPE_PERMISSION)
isReShareShown = it.getBoolean(ARG_RESHARE_SHOWN, true)
isExpDateShown = it.getBoolean(ARG_EXP_DATE_SHOWN, true)
isSecureShare = it.getBoolean(ARG_SECURE_SHARE, false)
}
lifecycleScope.launch(Dispatchers.IO) {
val ssoAcc =
SingleAccountHelper.getCurrentSingleSignOnAccount(this@NoteShareDetailActivity)
repository = ShareRepository(this@NoteShareDetailActivity, ssoAcc)
permission = repository.getCapabilities().defaultPermission
withContext(Dispatchers.Main) {
if (shareProcessStep == SCREEN_TYPE_PERMISSION) {
showShareProcessFirst()
} else {
showShareProcessSecond()
}
implementClickEvents()
}
}
}
private fun backPressed() {
finish()
}
override fun applyBrand(color: Int) {
val util = BrandingUtil.of(color, this)
binding.run {
util.platform.run {
themeRadioButton(shareProcessPermissionReadOnly)
themeRadioButton(shareProcessPermissionUploadEditing)
themeRadioButton(shareProcessPermissionFileDrop)
colorTextView(shareProcessEditShareLink)
colorTextView(shareProcessAdvancePermissionTitle)
themeCheckbox(shareProcessAllowResharingCheckbox)
colorTextView(title, ColorRole.ON_SURFACE)
}
util.androidx.run {
colorSwitchCompat(shareProcessSetPasswordSwitch)
colorSwitchCompat(shareProcessSetExpDateSwitch)
colorSwitchCompat(shareProcessHideDownloadCheckbox)
colorSwitchCompat(shareProcessChangeNameSwitch)
}
util.material.run {
colorTextInputLayout(shareProcessEnterPasswordContainer)
colorTextInputLayout(shareProcessChangeNameContainer)
colorTextInputLayout(noteContainer)
colorMaterialButtonPrimaryFilled(shareProcessBtnNext)
colorMaterialButtonPrimaryOutlined(shareProcessBtnCancel)
themeToolbar(toolbar)
}
}
util.platform.colorViewBackground(window.decorView)
util.platform.colorViewBackground(binding.getRoot())
}
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
// Force recreation of dialog activity when screen rotates
// This is needed because the calendar layout should be different in portrait and landscape,
// but as FDA persists through config changes, the dialog is not recreated automatically
val datePicker = expirationDatePickerFragment
if (datePicker?.dialog?.isShowing == true) {
val currentSelectionMillis = datePicker.currentSelectionMillis
datePicker.dismiss()
showExpirationDateDialog(currentSelectionMillis)
}
}
private fun showShareProcessFirst() {
binding.shareProcessGroupOne.visibility = View.VISIBLE
binding.shareProcessEditShareLink.visibility = View.VISIBLE
binding.shareProcessGroupTwo.visibility = View.GONE
if (share != null) {
setupModificationUI()
} else {
setupUpdateUI()
}
if (isSecureShare) {
binding.shareProcessAdvancePermissionTitle.visibility = View.GONE
}
// show or hide expiry date
if (isExpDateShown && !isSecureShare) {
binding.shareProcessSetExpDateSwitch.visibility = View.VISIBLE
} else {
binding.shareProcessSetExpDateSwitch.visibility = View.GONE
}
binding.noteText.setText(share?.note)
shareProcessStep = SCREEN_TYPE_PERMISSION
}
private fun setupModificationUI() {
if (share?.isFolder == true) updateViewForFolder() else updateViewForFile()
// read only / allow upload and editing / file drop
if (SharingMenuHelper.isUploadAndEditingAllowed(share)) {
binding.shareProcessPermissionUploadEditing.isChecked = true
} else if (SharingMenuHelper.isFileDrop(share) && share?.isFolder == true) {
binding.shareProcessPermissionFileDrop.isChecked = true
} else if (SharingMenuHelper.isReadOnly(share)) {
binding.shareProcessPermissionReadOnly.isChecked = true
}
shareType = share?.shareType ?: ShareType.NO_SHARED
// show different text for link share and other shares
// because we have link to share in Public Link
binding.shareProcessBtnNext.text = getString(
if (shareType == ShareType.PUBLIC_LINK) {
R.string.note_share_detail_activity_share_copy_link
} else {
R.string.note_share_detail_activity_common_confirm
}
)
updateViewForShareType()
binding.shareProcessSetPasswordSwitch.isChecked = share?.isPasswordProtected == true
showPasswordInput(binding.shareProcessSetPasswordSwitch.isChecked)
updateExpirationDateView()
showExpirationDateInput(binding.shareProcessSetExpDateSwitch.isChecked)
}
private fun setupUpdateUI() {
binding.shareProcessBtnNext.text =
getString(R.string.note_share_detail_activity_common_next)
note.let {
updateViewForFile()
updateViewForShareType()
}
showPasswordInput(binding.shareProcessSetPasswordSwitch.isChecked)
showExpirationDateInput(binding.shareProcessSetExpDateSwitch.isChecked)
}
private fun updateViewForShareType() {
when (shareType) {
ShareType.EMAIL -> {
updateViewForExternalShare()
}
ShareType.PUBLIC_LINK -> {
updateViewForLinkShare()
}
else -> {
updateViewForInternalShare()
}
}
}
private fun updateViewForExternalShare() {
binding.shareProcessChangeNameSwitch.visibility = View.GONE
binding.shareProcessChangeNameContainer.visibility = View.GONE
updateViewForExternalAndLinkShare()
}
private fun updateViewForLinkShare() {
updateViewForExternalAndLinkShare()
binding.shareProcessChangeNameSwitch.visibility = View.VISIBLE
if (share != null) {
binding.shareProcessChangeName.setText(share?.label)
binding.shareProcessChangeNameSwitch.isChecked = !TextUtils.isEmpty(share?.label)
}
showChangeNameInput(binding.shareProcessChangeNameSwitch.isChecked)
}
private fun updateViewForInternalShare() {
binding.run {
shareProcessChangeNameSwitch.visibility = View.GONE
shareProcessChangeNameContainer.visibility = View.GONE
shareProcessHideDownloadCheckbox.visibility = View.GONE
if (isSecureShare) {
shareProcessAllowResharingCheckbox.visibility = View.GONE
} else {
shareProcessAllowResharingCheckbox.visibility = View.VISIBLE
}
shareProcessSetPasswordSwitch.visibility = View.GONE
if (share != null) {
if (!isReShareShown) {
shareProcessAllowResharingCheckbox.visibility = View.GONE
}
shareProcessAllowResharingCheckbox.isChecked =
SharingMenuHelper.canReshare(share)
}
}
}
/**
* update views where share type external or link share
*/
private fun updateViewForExternalAndLinkShare() {
binding.run {
shareProcessHideDownloadCheckbox.visibility = View.VISIBLE
shareProcessAllowResharingCheckbox.visibility = View.GONE
shareProcessSetPasswordSwitch.visibility = View.VISIBLE
if (share != null) {
if (SharingMenuHelper.isFileDrop(share)) {
shareProcessHideDownloadCheckbox.visibility = View.GONE
} else {
shareProcessHideDownloadCheckbox.visibility = View.VISIBLE
shareProcessHideDownloadCheckbox.isChecked =
share?.isHideFileDownload == true
}
}
}
}
/**
* update expiration date view while modifying the share
*/
private fun updateExpirationDateView() {
share?.let { share ->
if (share.expirationDate > 0) {
chosenExpDateInMills = share.expirationDate
binding.shareProcessSetExpDateSwitch.isChecked = true
binding.shareProcessSelectExpDate.text = getString(
R.string.share_expiration_date_format,
SimpleDateFormat.getDateInstance().format(Date(share.expirationDate))
)
}
}
}
private fun updateViewForFile() {
binding.shareProcessPermissionUploadEditing.text = getString(R.string.link_share_editing)
binding.shareProcessPermissionFileDrop.visibility = View.GONE
}
private fun updateViewForFolder() {
binding.run {
shareProcessPermissionUploadEditing.text =
getString(R.string.link_share_allow_upload_and_editing)
shareProcessPermissionFileDrop.visibility = View.VISIBLE
if (isSecureShare) {
shareProcessPermissionFileDrop.visibility = View.GONE
shareProcessAllowResharingCheckbox.visibility = View.GONE
shareProcessSetExpDateSwitch.visibility = View.GONE
}
}
}
/**
* update views for screen type Note
*/
private fun showShareProcessSecond() {
binding.run {
shareProcessGroupOne.visibility = View.GONE
shareProcessEditShareLink.visibility = View.GONE
shareProcessGroupTwo.visibility = View.VISIBLE
if (share != null) {
shareProcessBtnNext.text =
getString(R.string.note_share_detail_activity_set_note)
noteText.setText(share?.note)
} else {
shareProcessBtnNext.text =
getString(R.string.note_share_detail_activity_send_share)
noteText.setText(R.string.empty)
}
shareProcessStep = SCREEN_TYPE_NOTE
shareProcessBtnNext.performClick()
}
}
private fun implementClickEvents() {
binding.run {
shareProcessBtnCancel.setOnClickListener {
onCancelClick()
}
shareProcessBtnNext.setOnClickListener {
if (shareProcessStep == SCREEN_TYPE_PERMISSION) {
validateShareProcessFirst()
} else {
createOrUpdateShare()
}
}
shareProcessSetPasswordSwitch.setOnCheckedChangeListener { _, isChecked ->
showPasswordInput(isChecked)
}
shareProcessSetExpDateSwitch.setOnCheckedChangeListener { _, isChecked ->
showExpirationDateInput(isChecked)
}
shareProcessChangeNameSwitch.setOnCheckedChangeListener { _, isChecked ->
showChangeNameInput(isChecked)
}
shareProcessSelectExpDate.setOnClickListener {
showExpirationDateDialog()
}
}
}
private fun showExpirationDateDialog(chosenDateInMillis: Long = chosenExpDateInMills) {
val dialog = ExpirationDatePickerDialogFragment.newInstance(chosenDateInMillis)
dialog.setOnExpiryDateListener(this)
expirationDatePickerFragment = dialog
dialog.show(supportFragmentManager, ExpirationDatePickerDialogFragment.DATE_PICKER_DIALOG)
}
private fun showChangeNameInput(isChecked: Boolean) {
binding.shareProcessChangeNameContainer.visibility =
if (isChecked) View.VISIBLE else View.GONE
if (!isChecked) {
binding.shareProcessChangeName.setText(R.string.empty)
}
}
private fun onCancelClick() {
// if modifying the existing share then on back press remove the current activity
if (share != null) {
finish()
}
// else we have to check if user is in step 2(note screen) then show step 1 (permission screen)
// and if user is in step 1 (permission screen) then remove the activity
else {
if (shareProcessStep == SCREEN_TYPE_NOTE) {
showShareProcessFirst()
} else {
finish()
}
}
}
private fun showExpirationDateInput(isChecked: Boolean) {
binding.shareProcessSelectExpDate.visibility = if (isChecked) View.VISIBLE else View.GONE
binding.shareProcessExpDateDivider.visibility = if (isChecked) View.VISIBLE else View.GONE
// reset the expiration date if switch is unchecked
if (!isChecked) {
chosenExpDateInMills = -1
binding.shareProcessSelectExpDate.text = getString(R.string.empty)
}
}
private fun showPasswordInput(isChecked: Boolean) {
binding.shareProcessEnterPasswordContainer.visibility =
if (isChecked) View.VISIBLE else View.GONE
// reset the password if switch is unchecked
if (!isChecked) {
binding.shareProcessEnterPassword.setText(R.string.empty)
}
}
private fun getReSharePermission(): Int = SharePermissionsBuilder().apply {
setSharePermission(true)
}.build()
/**
* method to validate the step 1 screen information
*/
@Suppress("ReturnCount")
private fun validateShareProcessFirst() {
permission = getSelectedPermission()
if (permission == OCShare.NO_PERMISSION) {
DisplayUtils.showSnackMessage(
binding.root,
R.string.note_share_detail_activity_no_share_permission_selected
)
return
}
if (binding.shareProcessSetPasswordSwitch.isChecked &&
binding.shareProcessEnterPassword.text?.trim().isNullOrEmpty()
) {
DisplayUtils.showSnackMessage(
binding.root,
R.string.note_share_detail_activity_share_link_empty_password
)
return
}
if (binding.shareProcessSetExpDateSwitch.isChecked &&
binding.shareProcessSelectExpDate.text?.trim().isNullOrEmpty()
) {
showExpirationDateDialog()
return
}
if (binding.shareProcessChangeNameSwitch.isChecked &&
binding.shareProcessChangeName.text?.trim().isNullOrEmpty()
) {
DisplayUtils.showSnackMessage(
binding.root,
R.string.note_share_detail_activity_label_empty
)
return
}
// if modifying existing share information then execute the process
if (share != null) {
lifecycleScope.launch(Dispatchers.IO) {
val noteText = binding.noteText.text.toString().trim()
val password = binding.shareProcessEnterPassword.text.toString().trim()
updateShare(noteText, password, false)
}
} else {
// else show step 2 (note screen)
showShareProcessSecond()
}
}
/**
* get the permissions on the basis of selection
*/
private fun getSelectedPermission() = when {
binding.shareProcessAllowResharingCheckbox.isChecked -> getReSharePermission()
binding.shareProcessPermissionReadOnly.isChecked -> OCShare.READ_PERMISSION_FLAG
binding.shareProcessPermissionUploadEditing.isChecked -> OCShare.MAXIMUM_PERMISSIONS_FOR_FILE
binding.shareProcessPermissionFileDrop.isChecked -> OCShare.CREATE_PERMISSION_FLAG
else -> permission
}
/**
* method to validate step 2 (note screen) information
*/
private fun createOrUpdateShare() {
val noteText = binding.noteText.text.toString().trim()
val password = binding.shareProcessEnterPassword.text.toString().trim()
lifecycleScope.launch(Dispatchers.IO) {
if (share != null && share?.note != noteText) {
updateShare(noteText, password, true)
} else {
createShare(noteText, password)
}
}
}
private suspend fun updateShare(noteText: String, password: String, sendEmail: Boolean) {
val downloadPermission = !binding.shareProcessHideDownloadCheckbox.isChecked
val requestBody = repository.getUpdateShareRequest(
downloadPermission,
share,
noteText,
password,
sendEmail,
chosenExpDateInMills,
permission
)
val updateShareResult = repository.updateShare(share!!.id, requestBody)
if (updateShareResult.isSuccess() && sendEmail) {
val sendEmailResult = repository.sendEmail(share!!.id, SharePasswordRequest(password))
handleResult(sendEmailResult)
} else {
handleResult(updateShareResult.isSuccess())
}
if (!sendEmail) {
withContext(Dispatchers.Main) {
if (!TextUtils.isEmpty(share?.shareLink)) {
ClipboardUtil.copyToClipboard(this@NoteShareDetailActivity, share?.shareLink)
}
}
}
}
private suspend fun createShare(noteText: String, password: String) {
if (note == null || shareeName == null) {
Log_OC.d(TAG, "validateShareProcessSecond cancelled")
return
}
val result = repository.addShare(
note!!,
shareType,
shareeName!!,
"false", // TODO: Check how to determine it
password,
permission,
noteText
)
if (result.isSuccess()) {
repository.getSharesForNotesAndSaveShareEntities()
}
handleResult(result.isSuccess())
}
private suspend fun handleResult(success: Boolean) {
withContext(Dispatchers.Main) {
if (success) {
val resultIntent = Intent()
setResult(RESULT_OK, resultIntent)
finish()
} else {
DisplayUtils.showSnackMessage(
this@NoteShareDetailActivity,
getString(R.string.note_share_detail_activity_create_share_error)
)
}
}
}
override fun onDateSet(year: Int, monthOfYear: Int, dayOfMonth: Int, chosenDateInMillis: Long) {
binding.shareProcessSelectExpDate.text = getString(
R.string.share_expiration_date_format,
SimpleDateFormat.getDateInstance().format(Date(chosenDateInMillis))
)
this.chosenExpDateInMills = chosenDateInMillis
}
override fun onDateUnSet() {
binding.shareProcessSetExpDateSwitch.isChecked = false
}
}