Skip to content

Commit 88f5663

Browse files
committed
fix(e2ee): event listen
Signed-off-by: alperozturk96 <alper_ozturk@proton.me>
1 parent e35894d commit 88f5663

7 files changed

Lines changed: 55 additions & 31 deletions

File tree

app/src/androidTest/java/com/owncloud/android/ui/dialog/SetupEncryptionDialogFragmentIT.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class SetupEncryptionDialogFragmentIT : AbstractIT() {
2828
launchActivity<TestActivity>().use { scenario ->
2929
var sut: SetupEncryptionDialogFragment? = null
3030
scenario.onActivity { activity ->
31-
sut = SetupEncryptionDialogFragment.newInstance(user, null)
31+
sut = SetupEncryptionDialogFragment.newInstance(user, null, null)
3232
sut.show(activity.supportFragmentManager, "1")
3333
val keyWords = arrayListOf(
3434
"ability",
@@ -64,7 +64,7 @@ class SetupEncryptionDialogFragmentIT : AbstractIT() {
6464
launchActivity<TestActivity>().use { scenario ->
6565
var sut: SetupEncryptionDialogFragment? = null
6666
scenario.onActivity { activity ->
67-
sut = SetupEncryptionDialogFragment.newInstance(user, null)
67+
sut = SetupEncryptionDialogFragment.newInstance(user, null, null)
6868
sut.show(activity.supportFragmentManager, "1")
6969
sut.errorSavingKeys()
7070
}

app/src/main/java/com/nextcloud/utils/extensions/OCFileListFragmentExtensions.kt

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ package com.nextcloud.utils.extensions
99

1010
import android.os.Bundle
1111
import androidx.lifecycle.lifecycleScope
12+
import com.nextcloud.utils.e2ee.model.E2EEAction
1213
import com.owncloud.android.R
1314
import com.owncloud.android.datamodel.OCFile
1415
import com.owncloud.android.lib.common.utils.Log_OC
1516
import com.owncloud.android.ui.activity.FileActivity
1617
import com.owncloud.android.ui.dialog.setupEncryption.SetupEncryptionDialogFragment
18+
import com.owncloud.android.ui.dialog.setupEncryption.SetupEncryptionDialogFragment.Companion.ARG_ACTION
1719
import com.owncloud.android.ui.dialog.setupEncryption.SetupEncryptionDialogFragment.Companion.ARG_FILE_PATH
1820
import com.owncloud.android.ui.dialog.setupEncryption.SetupEncryptionDialogFragment.Companion.RESULT_REQUEST_KEY
1921
import com.owncloud.android.ui.dialog.setupEncryption.SetupEncryptionDialogFragment.Companion.SUCCESS
@@ -23,16 +25,16 @@ import kotlinx.coroutines.launch
2325

2426
private const val TAG = "OCFileListFragmentExtensions"
2527

26-
fun OCFileListFragment.showEncryptionDialog(remotePath: String?) {
28+
fun OCFileListFragment.showEncryptionDialog(remotePath: String?, action: E2EEAction) {
2729
if (parentFragmentManager.findFragmentByTag(SetupEncryptionDialogFragment.SETUP_ENCRYPTION_DIALOG_TAG) != null) {
2830
return
2931
}
3032

3133
val user = accountManager.user
3234
val connectivityService = typedActivity<FileActivity>()?.connectivityService
3335
connectivityService?.isNetworkAndServerAvailable { result ->
34-
if (result == true) {
35-
SetupEncryptionDialogFragment.newInstance(user, remotePath)
36+
if (result) {
37+
SetupEncryptionDialogFragment.newInstance(user, remotePath, action)
3638
.show(parentFragmentManager, SetupEncryptionDialogFragment.SETUP_ENCRYPTION_DIALOG_TAG)
3739
return@isNetworkAndServerAvailable
3840
}
@@ -55,30 +57,37 @@ fun OCFileListFragment.listenEncryptionDialogResult() {
5557
return@setFragmentResultListener
5658
}
5759

58-
val fileRemotePath = bundle.getString(ARG_FILE_PATH, null)
59-
if (fileRemotePath == null) {
60-
Log_OC.e(TAG, "file path is null")
60+
val action = bundle.getSerializableArgument(ARG_ACTION, E2EEAction::class.java)
61+
if (action == null) {
62+
Log_OC.e(TAG, "no pending encryption action, nothing to continue with")
6163
return@setFragmentResultListener
6264
}
6365

64-
val file: OCFile? = mContainerActivity.getStorageManager().getFileByDecryptedRemotePath(fileRemotePath)
65-
if (file == null) {
66-
Log_OC.e(TAG, "file is null, cannot toggle encryption")
67-
return@setFragmentResultListener
68-
}
66+
when (action) {
67+
E2EEAction.NEW_FOLDER -> createFolder(true)
6968

70-
if (file.isRootDirectory) {
71-
Log_OC.d(
72-
TAG,
73-
"result of setup encryption triggered in root directory, this call is for " +
74-
"creating encrypted folder"
75-
)
76-
createFolder(true)
77-
return@setFragmentResultListener
78-
}
69+
E2EEAction.OPEN -> folderFromResult(bundle)?.let { clickHandler.openAfterKeySetup(it) }
7970

80-
lifecycleScope.launch {
81-
folderEncryption.toggle(file.toEncryptionEvent(true))
71+
E2EEAction.ENCRYPT -> folderFromResult(bundle)?.let { file ->
72+
lifecycleScope.launch {
73+
folderEncryption.toggle(file.toEncryptionEvent(true))
74+
}
75+
}
8276
}
8377
}
8478
}
79+
80+
private fun OCFileListFragment.folderFromResult(bundle: Bundle): OCFile? {
81+
val remotePath = bundle.getString(ARG_FILE_PATH, null)
82+
if (remotePath == null) {
83+
Log_OC.e(TAG, "file path is null")
84+
return null
85+
}
86+
87+
val file = mContainerActivity.storageManager.getFileByEncryptedRemotePath(remotePath)
88+
if (file == null) {
89+
Log_OC.e(TAG, "file is null, cannot continue encryption action")
90+
}
91+
92+
return file
93+
}

app/src/main/java/com/owncloud/android/ui/activity/SetupEncryptionActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class SetupEncryptionActivity : AppCompatActivity() {
3030
finish()
3131
}
3232

33-
val setupEncryptionDialogFragment = SetupEncryptionDialogFragment.newInstance(user, null)
33+
val setupEncryptionDialogFragment = SetupEncryptionDialogFragment.newInstance(user, null, null)
3434
supportFragmentManager.setFragmentResultListener(
3535
SetupEncryptionDialogFragment.RESULT_REQUEST_KEY,
3636
this

app/src/main/java/com/owncloud/android/ui/dialog/setupEncryption/SetupEncryptionDialogFragment.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ import com.google.android.material.textfield.TextInputLayout
2121
import com.nextcloud.client.account.User
2222
import com.nextcloud.client.di.Injectable
2323
import com.nextcloud.client.network.ClientFactory
24+
import com.nextcloud.utils.e2ee.model.E2EEAction
2425
import com.nextcloud.utils.extensions.getParcelableArgument
26+
import com.nextcloud.utils.extensions.getSerializableArgument
2527
import com.owncloud.android.BuildConfig
2628
import com.owncloud.android.R
2729
import com.owncloud.android.databinding.SetupEncryptionDialogBinding
@@ -243,6 +245,7 @@ class SetupEncryptionDialogFragment :
243245
return Bundle().apply {
244246
putBoolean(SUCCESS, true)
245247
putString(ARG_FILE_PATH, requireArguments().getString(ARG_FILE_PATH))
248+
putSerializable(ARG_ACTION, arguments.getSerializableArgument(ARG_ACTION, E2EEAction::class.java))
246249
}
247250
}
248251

@@ -526,6 +529,7 @@ class SetupEncryptionDialogFragment :
526529
const val SETUP_ENCRYPTION_RESULT_CODE = 101
527530
const val SETUP_ENCRYPTION_DIALOG_TAG = "SETUP_ENCRYPTION_DIALOG_TAG"
528531
const val ARG_FILE_PATH = "ARG_FILE_PATH"
532+
const val ARG_ACTION = "ARG_ACTION"
529533
const val RESULT_REQUEST_KEY = "RESULT_REQUEST"
530534
const val RESULT_KEY_CANCELLED = "IS_CANCELLED"
531535
private const val NUMBER_OF_WORDS = 12
@@ -537,11 +541,12 @@ class SetupEncryptionDialogFragment :
537541
private const val KEY_GENERATE = "KEY_GENERATE"
538542

539543
@JvmStatic
540-
fun newInstance(user: User?, filePath: String?): SetupEncryptionDialogFragment =
544+
fun newInstance(user: User?, filePath: String?, action: E2EEAction?): SetupEncryptionDialogFragment =
541545
SetupEncryptionDialogFragment().apply {
542546
arguments = Bundle().apply {
543547
putParcelable(ARG_USER, user)
544548
putString(ARG_FILE_PATH, filePath)
549+
putSerializable(ARG_ACTION, action)
545550
}
546551
}
547552
}

app/src/main/java/com/owncloud/android/ui/events/EncryptionEvent.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class EncryptionEvent(val localId: Long, val remoteId: String, val remotePath: S
2626
}
2727

2828
E2EEKeyCheck.ONLY_ON_SERVER, E2EEKeyCheck.MISSING_EVERYWHERE -> {
29-
fragment.showEncryptionDialog(remotePath)
29+
fragment.showEncryptionDialog(remotePath, E2EEAction.ENCRYPT)
3030
}
3131

3232
E2EEKeyCheck.ONLY_ON_DEVICE, E2EEKeyCheck.DIFFERS_FROM_SERVER -> {

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class EncryptedFolderClickHandler(private val fragment: OCFileListFragment) {
4848

4949
E2EEKeyCheck.ONLY_ON_SERVER, E2EEKeyCheck.MISSING_EVERYWHERE -> {
5050
Log_OC.d(TAG, "keys found on server but missing locally, redirecting to encryption setup")
51-
fragment.showEncryptionDialog(OCFile.ROOT_PATH)
51+
fragment.showEncryptionDialog(OCFile.ROOT_PATH, E2EEAction.NEW_FOLDER)
5252
}
5353

5454
E2EEKeyCheck.SAME_AS_SERVER -> {
@@ -86,7 +86,7 @@ class EncryptedFolderClickHandler(private val fragment: OCFileListFragment) {
8686
}
8787

8888
E2EEKeyCheck.ONLY_ON_SERVER -> {
89-
fragment.showEncryptionDialog(file.remotePath)
89+
fragment.showEncryptionDialog(file.remotePath, E2EEAction.OPEN)
9090
}
9191

9292
E2EEKeyCheck.MISSING_EVERYWHERE -> {
@@ -114,6 +114,16 @@ class EncryptedFolderClickHandler(private val fragment: OCFileListFragment) {
114114
}
115115
}
116116

117+
fun openAfterKeySetup(file: OCFile) {
118+
fragment.lifecycleScope.launch {
119+
if (fragment.e2eeActionResolver.checkFolderMetadataKey(file)) {
120+
onEncryptionSetupComplete(file, fragment.adapter.getItemPosition(file))
121+
} else {
122+
DisplayUtils.showSnackMessage(fragment, R.string.encryption_open_key_mismatch)
123+
}
124+
}
125+
}
126+
117127
private fun dismissCheckingSnackbar() {
118128
DisplayUtils.dismissSnackMessage(checkingKeysSnackbar)
119129
checkingKeysSnackbar = null
@@ -131,7 +141,7 @@ class EncryptedFolderClickHandler(private val fragment: OCFileListFragment) {
131141
if (FileOperationsHelper.isEndToEndEncryptionSetup(fragment.context, user)) {
132142
onEncryptionSetupComplete(file, position)
133143
} else {
134-
fragment.showEncryptionDialog(file.remotePath)
144+
fragment.showEncryptionDialog(file.remotePath, E2EEAction.OPEN)
135145
}
136146
}
137147

app/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public class OCFileListFragment extends ExtendedListFragment implements
201201
@Inject ThumbnailGenerator thumbnailGenerator;
202202
@Inject public E2EEActionResolver e2eeActionResolver;
203203
public E2EEDialogPresenter e2eeDialogPresenter;
204-
private EncryptedFolderClickHandler clickHandler;
204+
public EncryptedFolderClickHandler clickHandler;
205205
public FolderEncryption folderEncryption;
206206
public FileFragment.ContainerActivity mContainerActivity;
207207

0 commit comments

Comments
 (0)