Skip to content

Commit 0fe0b49

Browse files
Merge pull request #16590 from nextcloud/backport/16531/stable-3.36
[stable-3.36] fix(unified-search): e2ee folder action
2 parents 981054c + d8c62cd commit 0fe0b49

File tree

7 files changed

+84
-89
lines changed

7 files changed

+84
-89
lines changed

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, 0)
31+
sut = SetupEncryptionDialogFragment.newInstance(user, 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, 0)
67+
sut = SetupEncryptionDialogFragment.newInstance(user, null)
6868
sut.show(activity.supportFragmentManager, "1")
6969
sut.errorSavingKeys()
7070
}

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

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3018,24 +3018,20 @@ class FileDisplayActivity :
30183018
}
30193019

30203020
fun showFile(selectedFile: OCFile?, message: String?) {
3021-
dismissLoadingDialog()
3022-
30233021
getOCFileListFragmentFromFile(object : TransactionInterface {
3024-
override fun onOCFileListFragmentComplete(listOfFiles: OCFileListFragment) {
3025-
if (TextUtils.isEmpty(message)) {
3026-
val temp = file
3027-
file = getCurrentDir()
3028-
listOfFiles.listDirectory(getCurrentDir(), temp, MainApp.isOnlyOnDevice())
3022+
override fun onOCFileListFragmentComplete(fragment: OCFileListFragment) {
3023+
dismissLoadingDialog()
3024+
3025+
if (message.isNullOrEmpty()) {
3026+
val current = getCurrentDir()
3027+
fragment.listDirectory(current, file, MainApp.isOnlyOnDevice())
3028+
file = current
30293029
updateActionBarTitleAndHomeButton(null)
30303030
} else {
3031-
val view = listOfFiles.view
3032-
if (view != null) {
3033-
DisplayUtils.showSnackMessage(view, message)
3034-
}
3035-
}
3036-
if (selectedFile != null) {
3037-
listOfFiles.onItemClicked(selectedFile)
3031+
fragment.view?.let { DisplayUtils.showSnackMessage(it, message) }
30383032
}
3033+
3034+
selectedFile?.let(fragment::onItemClicked)
30393035
}
30403036
})
30413037
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class SetupEncryptionActivity : AppCompatActivity() {
2727
finish()
2828
}
2929

30-
val setupEncryptionDialogFragment = SetupEncryptionDialogFragment.newInstance(user, -1)
30+
val setupEncryptionDialogFragment = SetupEncryptionDialogFragment.newInstance(user, null)
3131
supportFragmentManager.setFragmentResultListener(
3232
SetupEncryptionDialogFragment.RESULT_REQUEST_KEY,
3333
this
@@ -52,8 +52,8 @@ class SetupEncryptionActivity : AppCompatActivity() {
5252
result.getBoolean(SetupEncryptionDialogFragment.SUCCESS)
5353
)
5454
intent.putExtra(
55-
SetupEncryptionDialogFragment.ARG_POSITION,
56-
result.getInt(SetupEncryptionDialogFragment.ARG_POSITION)
55+
SetupEncryptionDialogFragment.ARG_FILE_PATH,
56+
result.getInt(SetupEncryptionDialogFragment.ARG_FILE_PATH)
5757
)
5858
return intent
5959
}

app/src/main/java/com/owncloud/android/ui/adapter/OCFileListAdapter.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,21 @@ public int getItemCount() {
335335

336336
@Nullable
337337
public OCFile getItem(int position) {
338+
if (mFiles == null || mFiles.isEmpty()) {
339+
return null;
340+
}
341+
342+
if (position < 0) {
343+
return null;
344+
}
345+
338346
int newPosition = position;
339347

340-
if (shouldShowHeader() && position > 0) {
348+
if (shouldShowHeader()) {
349+
if (position == 0) {
350+
// Header position — no file here
351+
return null;
352+
}
341353
newPosition = position - 1;
342354
}
343355

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

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ package com.owncloud.android.ui.dialog.setupEncryption
99
import android.accounts.AccountManager
1010
import android.app.Dialog
1111
import android.content.DialogInterface
12-
import android.content.Intent
1312
import android.os.Bundle
1413
import android.view.View
1514
import androidx.annotation.VisibleForTesting
@@ -44,7 +43,6 @@ import kotlinx.coroutines.launch
4443
import kotlinx.coroutines.withContext
4544
import java.io.IOException
4645
import java.lang.ref.WeakReference
47-
import java.util.Arrays
4846
import javax.inject.Inject
4947

5048
/*
@@ -215,15 +213,15 @@ class SetupEncryptionDialogFragment :
215213
)
216214
val secondKey = EncryptionUtils.decodeStringToBase64Bytes(decryptedString)
217215

218-
if (!Arrays.equals(firstKey, secondKey)) {
216+
if (!firstKey.contentEquals(secondKey)) {
219217
EncryptionUtils.reportE2eError(arbitraryDataProvider, user)
220218
throw Exception("Keys do not match")
221219
}
222220

223221
notifyResult()
224222
} catch (e: Exception) {
225223
binding.encryptionStatus.setText(R.string.end_to_end_encryption_wrong_password)
226-
Log_OC.d(TAG, "Error while decrypting private key: " + e.message)
224+
Log_OC.e(TAG, "Error while decrypting private key: " + e.message)
227225
}
228226
}
229227

@@ -240,27 +238,14 @@ class SetupEncryptionDialogFragment :
240238
}
241239

242240
private fun notifyResult() {
243-
val targetFragment = targetFragment
244-
targetFragment?.onActivityResult(
245-
targetRequestCode,
246-
SETUP_ENCRYPTION_RESULT_CODE,
247-
resultIntent
248-
)
249241
parentFragmentManager.setFragmentResult(RESULT_REQUEST_KEY, resultBundle)
250242
}
251243

252-
private val resultIntent: Intent
253-
get() {
254-
return Intent().apply {
255-
putExtra(SUCCESS, true)
256-
putExtra(ARG_POSITION, requireArguments().getInt(ARG_POSITION))
257-
}
258-
}
259244
private val resultBundle: Bundle
260245
get() {
261246
return Bundle().apply {
262247
putBoolean(SUCCESS, true)
263-
putInt(ARG_POSITION, requireArguments().getInt(ARG_POSITION))
248+
putString(ARG_FILE_PATH, requireArguments().getString(ARG_FILE_PATH))
264249
}
265250
}
266251

@@ -544,9 +529,8 @@ class SetupEncryptionDialogFragment :
544529
companion object {
545530
const val SUCCESS = "SUCCESS"
546531
const val SETUP_ENCRYPTION_RESULT_CODE = 101
547-
const val SETUP_ENCRYPTION_REQUEST_CODE = 100
548532
const val SETUP_ENCRYPTION_DIALOG_TAG = "SETUP_ENCRYPTION_DIALOG_TAG"
549-
const val ARG_POSITION = "ARG_POSITION"
533+
const val ARG_FILE_PATH = "ARG_FILE_PATH"
550534
const val RESULT_REQUEST_KEY = "RESULT_REQUEST"
551535
const val RESULT_KEY_CANCELLED = "IS_CANCELLED"
552536
private const val NUMBER_OF_WORDS = 12
@@ -557,16 +541,11 @@ class SetupEncryptionDialogFragment :
557541
private const val KEY_FAILED = "KEY_FAILED"
558542
private const val KEY_GENERATE = "KEY_GENERATE"
559543

560-
/**
561-
* Public factory method to create new SetupEncryptionDialogFragment instance
562-
*
563-
* @return Dialog ready to show.
564-
*/
565544
@JvmStatic
566-
fun newInstance(user: User?, position: Int): SetupEncryptionDialogFragment {
545+
fun newInstance(user: User?, filePath: String?): SetupEncryptionDialogFragment {
567546
val bundle = Bundle().apply {
568547
putParcelable(ARG_USER, user)
569-
putInt(ARG_POSITION, position)
548+
putString(ARG_FILE_PATH, filePath)
570549
}
571550

572551
return SetupEncryptionDialogFragment().apply {

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

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@
148148

149149
import static com.owncloud.android.datamodel.OCFile.ROOT_PATH;
150150
import static com.owncloud.android.ui.dialog.setupEncryption.SetupEncryptionDialogFragment.SETUP_ENCRYPTION_DIALOG_TAG;
151-
import static com.owncloud.android.ui.dialog.setupEncryption.SetupEncryptionDialogFragment.SETUP_ENCRYPTION_REQUEST_CODE;
152151
import static com.owncloud.android.ui.fragment.SearchType.FAVORITE_SEARCH;
153152
import static com.owncloud.android.ui.fragment.SearchType.FILE_SEARCH;
154153
import static com.owncloud.android.ui.fragment.SearchType.NO_SEARCH;
@@ -254,6 +253,12 @@ public void onCreate(Bundle savedInstanceState) {
254253
searchFragment = currentSearchType != null && isSearchEventSet(searchEvent);
255254
}
256255

256+
@Override
257+
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
258+
super.onViewCreated(view, savedInstanceState);
259+
listenSetupEncryptionDialogResult();
260+
}
261+
257262
@Override
258263
public void onResume() {
259264
// Don't handle search events if we're coming back from back stack
@@ -1132,8 +1137,7 @@ private void folderOnItemClick(OCFile file, int position) {
11321137
if (fragmentManager.findFragmentByTag(SETUP_ENCRYPTION_DIALOG_TAG) == null && requireActivity() instanceof FileActivity fileActivity) {
11331138
fileActivity.connectivityService.isNetworkAndServerAvailable(result -> {
11341139
if (result) {
1135-
SetupEncryptionDialogFragment dialog = SetupEncryptionDialogFragment.newInstance(user, position);
1136-
dialog.setTargetFragment(this, SETUP_ENCRYPTION_REQUEST_CODE);
1140+
SetupEncryptionDialogFragment dialog = SetupEncryptionDialogFragment.newInstance(user, file.getRemotePath());
11371141
dialog.show(fragmentManager, SETUP_ENCRYPTION_DIALOG_TAG);
11381142
} else {
11391143
DisplayUtils.showSnackMessage(fileActivity, R.string.internet_connection_required_for_encrypted_folder_setup);
@@ -1255,30 +1259,38 @@ private void browseToFolder(OCFile file, int position) {
12551259
saveIndexAndTopPosition(position);
12561260
}
12571261

1258-
@Override
1259-
public void onActivityResult(int requestCode, int resultCode, Intent data) {
1260-
if (requestCode == SETUP_ENCRYPTION_REQUEST_CODE &&
1261-
resultCode == SetupEncryptionDialogFragment.SETUP_ENCRYPTION_RESULT_CODE &&
1262-
data.getBooleanExtra(SetupEncryptionDialogFragment.SUCCESS, false)) {
1262+
private void listenSetupEncryptionDialogResult() {
1263+
getParentFragmentManager().setFragmentResultListener(
1264+
SetupEncryptionDialogFragment.RESULT_REQUEST_KEY,
1265+
this,
1266+
(requestKey, bundle) -> {
1267+
boolean result = bundle.getBoolean(SetupEncryptionDialogFragment.SUCCESS, false);
1268+
if (!result) {
1269+
Log_OC.d(TAG, "setup encryption dialog is dismissed");
1270+
return;
1271+
}
12631272

1264-
int position = data.getIntExtra(SetupEncryptionDialogFragment.ARG_POSITION, -1);
1265-
OCFile file = mAdapter.getItem(position);
1273+
String fileRemotePath = bundle.getString(SetupEncryptionDialogFragment.ARG_FILE_PATH, null);
1274+
if (fileRemotePath == null) {
1275+
Log_OC.e(TAG, "file path is null");
1276+
return;
1277+
}
1278+
1279+
OCFile file = mContainerActivity.getStorageManager().getFileByDecryptedRemotePath(fileRemotePath);
1280+
if (file == null) {
1281+
Log_OC.e(TAG,"file is null, cannot toggle encryption");
1282+
return;
1283+
}
12661284

1267-
if (file != null) {
12681285
mContainerActivity.getFileOperationsHelper().toggleEncryption(file, true);
12691286
mAdapter.setEncryptionAttributeForItemID(file.getRemoteId(), true);
1270-
}
1287+
searchFragment = false;
1288+
listDirectory(file, MainApp.isOnlyOnDevice());
1289+
mContainerActivity.onBrowsedDownTo(file);
12711290

1272-
// update state and view of this fragment
1273-
searchFragment = false;
1274-
listDirectory(file, MainApp.isOnlyOnDevice());
1275-
// then, notify parent activity to let it update its state and view
1276-
mContainerActivity.onBrowsedDownTo(file);
1277-
// save index and top position
1278-
saveIndexAndTopPosition(position);
1279-
} else {
1280-
super.onActivityResult(requestCode, resultCode, data);
1281-
}
1291+
int position = mAdapter.getItemPosition(file);
1292+
saveIndexAndTopPosition(position);
1293+
});
12821294
}
12831295

12841296
/**
@@ -1924,16 +1936,9 @@ public void onMessageEvent(EncryptionEvent event) {
19241936
if (publicKey.isEmpty() || privateKey.isEmpty()) {
19251937
Log_OC.d(TAG, "no public key for " + user.getAccountName());
19261938

1927-
int position;
1928-
if (file != null) {
1929-
position = mAdapter.getItemPosition(file);
1930-
} else {
1931-
position = -1;
1932-
}
19331939

19341940
requireActivity().runOnUiThread(() -> {
1935-
SetupEncryptionDialogFragment dialog = SetupEncryptionDialogFragment.newInstance(user, position);
1936-
dialog.setTargetFragment(OCFileListFragment.this, SETUP_ENCRYPTION_REQUEST_CODE);
1941+
SetupEncryptionDialogFragment dialog = SetupEncryptionDialogFragment.newInstance(user, file.getRemotePath());
19371942
dialog.show(getParentFragmentManager(), SETUP_ENCRYPTION_DIALOG_TAG);
19381943
});
19391944
} else {

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -351,18 +351,13 @@ class UnifiedSearchFragment :
351351
}
352352
}
353353

354-
private fun showFile(file: OCFile, showFileActions: Boolean) {
355-
activity.let {
356-
if (activity is FileDisplayActivity) {
357-
val fda = activity as FileDisplayActivity
358-
fda.file = file
359-
360-
if (showFileActions) {
361-
fda.showFileActions(file)
362-
} else {
363-
fda.showFile(file, "")
364-
}
354+
private fun showFile(file: OCFile, showFileActions: Boolean, updateCurrentFile: Boolean = true) {
355+
(activity as? FileDisplayActivity)?.apply {
356+
if (updateCurrentFile) {
357+
this.file = file
365358
}
359+
360+
if (showFileActions) showFileActions(file) else showFile(file, "")
366361
}
367362
}
368363

@@ -406,7 +401,15 @@ class UnifiedSearchFragment :
406401

407402
override fun onSearchResultClicked(searchResultEntry: SearchResultEntry) {
408403
showMoreActions = false
409-
vm.openResult(searchResultEntry)
404+
405+
val remotePath = searchResultEntry.remotePath() + OCFile.PATH_SEPARATOR
406+
val file = storageManager.getFileByDecryptedRemotePath(remotePath)
407+
408+
if (file?.isEncrypted == true) {
409+
showFile(file, showMoreActions, updateCurrentFile = false)
410+
} else {
411+
vm.openResult(searchResultEntry)
412+
}
410413
}
411414

412415
override fun onLoadMoreClicked(providerID: ProviderID) {

0 commit comments

Comments
 (0)