Skip to content

Commit f409039

Browse files
committed
Allow to create encrypted folder directly from bottom sheet dialog from NC PR: nextcloud#10782
1 parent 8f42be4 commit f409039

11 files changed

Lines changed: 132 additions & 35 deletions

File tree

app/src/androidTest/java/com/owncloud/android/ui/dialog/DialogFragmentIT.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,11 @@ public void createFolder() {
342342

343343
}
344344

345+
@Override
346+
public void createEncryptedFolder() {
347+
348+
}
349+
345350
@Override
346351
public void uploadFromApp() {
347352

app/src/main/java/com/owncloud/android/files/FileMenuFilter.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ private void filterUnlock(List<Integer> toHide, boolean fileLockingEnabled) {
237237

238238
private void filterEncrypt(List<Integer> toHide, boolean endToEndEncryptionEnabled) {
239239
if (files.isEmpty() || !isSingleSelection() || isSingleFile() || isEncryptedFolder() || isGroupFolder()
240-
|| !endToEndEncryptionEnabled || !isEmptyFolder() || isShared()) {
240+
|| !endToEndEncryptionEnabled || !isEmptyFolder() || isShared() || isInSubFolder()) {
241241
toHide.add(R.id.action_encrypted);
242242
}
243243
}
@@ -581,4 +581,15 @@ private boolean isShared() {
581581
}
582582
return false;
583583
}
584+
585+
private boolean isInSubFolder() {
586+
OCFile folder = files.iterator().next();
587+
OCFile parent = storageManager.getFileById(folder.getParentId());
588+
589+
if (parent == null) {
590+
return false;
591+
}
592+
593+
return !OCFile.ROOT_PATH.equals(parent.getRemotePath());
594+
}
584595
}

app/src/main/java/com/owncloud/android/operations/CreateFolderOperation.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,31 @@ public class CreateFolderOperation extends SyncOperation implements OnRemoteOper
6060
private RemoteFile createdRemoteFolder;
6161
private User user;
6262
private Context context;
63+
private boolean encrypted;
6364

6465
/**
6566
* Constructor
6667
*/
67-
public CreateFolderOperation(String remotePath, User user, Context context, FileDataStorageManager storageManager) {
68+
public CreateFolderOperation(String remotePath,
69+
User user,
70+
Context context,
71+
FileDataStorageManager storageManager
72+
) {
73+
this(remotePath, false, user, context, storageManager);
74+
}
75+
76+
public CreateFolderOperation(String remotePath,
77+
boolean encrypted,
78+
User user,
79+
Context context,
80+
FileDataStorageManager storageManager
81+
) {
6882
super(storageManager);
6983

7084
this.remotePath = remotePath;
7185
this.user = user;
7286
this.context = context;
87+
this.encrypted = encrypted;
7388
}
7489

7590
@Override
@@ -105,7 +120,7 @@ protected RemoteOperationResult run(OwnCloudClient client) {
105120
}
106121
return new RemoteOperationResult(new IllegalStateException("E2E not supported"));
107122
} else {
108-
return normalCreate(client);
123+
return normalCreate(client, encrypted);
109124
}
110125
}
111126

@@ -473,7 +488,7 @@ private String createRandomFileName(DecryptedFolderMetadataFileV1 metadata) {
473488
return encryptedFileName;
474489
}
475490

476-
private RemoteOperationResult normalCreate(OwnCloudClient client) {
491+
private RemoteOperationResult normalCreate(OwnCloudClient client, boolean encrypted) {
477492
RemoteOperationResult result = new CreateFolderRemoteOperation(remotePath, true).execute(client);
478493

479494
if (result.isSuccess()) {
@@ -482,6 +497,21 @@ private RemoteOperationResult normalCreate(OwnCloudClient client) {
482497

483498
createdRemoteFolder = (RemoteFile) remoteFolderOperationResult.getData().get(0);
484499
saveFolderInDB();
500+
501+
if (encrypted) {
502+
final OCFile folder = getStorageManager().getFileByDecryptedRemotePath(remotePath);
503+
504+
final RemoteOperationResult remoteOperationResult =
505+
new ToggleEncryptionRemoteOperation(folder.getLocalId(),
506+
remotePath,
507+
true)
508+
.execute(client);
509+
510+
if (remoteOperationResult.isSuccess()) {
511+
folder.setEncrypted(true);
512+
getStorageManager().saveFile(folder);
513+
}
514+
}
485515
} else {
486516
Log_OC.e(TAG, remotePath + " hasn't been created");
487517
}

app/src/main/java/com/owncloud/android/services/OperationsService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public class OperationsService extends Service {
8484
public static final String EXTRA_ACCOUNT = "ACCOUNT";
8585
public static final String EXTRA_SERVER_URL = "SERVER_URL";
8686
public static final String EXTRA_REMOTE_PATH = "REMOTE_PATH";
87+
public static final String EXTRA_ENCRYPTED = "ENCRYPTED";
8788
public static final String EXTRA_NEWNAME = "NEWNAME";
8889
public static final String EXTRA_REMOVE_ONLY_LOCAL = "REMOVE_LOCAL_COPY";
8990
public static final String EXTRA_SYNC_FILE_CONTENTS = "SYNC_FILE_CONTENTS";
@@ -685,7 +686,9 @@ private Pair<Target, RemoteOperation> newOperation(Intent operationIntent) {
685686

686687
case ACTION_CREATE_FOLDER:
687688
remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
689+
boolean encrypted = operationIntent.getBooleanExtra(EXTRA_ENCRYPTED, false);
688690
operation = new CreateFolderOperation(remotePath,
691+
encrypted,
689692
user,
690693
getApplicationContext(),
691694
fileDataStorageManager);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,10 @@ public int getItemCount() {
326326

327327
@Nullable
328328
public OCFile getItem(int position) {
329+
if (position == -1) {
330+
return null;
331+
}
332+
329333
int newPosition = position;
330334

331335
if (shouldShowHeader() && position > 0) {

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class CreateFolderDialogFragment : DialogFragment(), DialogInterface.OnClickList
6868

6969
private var parentFolder: OCFile? = null
7070
private var positiveButton: MaterialButton? = null
71+
private var encrypted = false
7172

7273
private lateinit var binding: EditBoxDialogBinding
7374

@@ -102,6 +103,7 @@ class CreateFolderDialogFragment : DialogFragment(), DialogInterface.OnClickList
102103
@Suppress("EmptyFunctionBlock")
103104
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
104105
parentFolder = arguments?.getParcelableArgument(ARG_PARENT_FOLDER, OCFile::class.java)
106+
encrypted = arguments?.getBoolean(ARG_ENCRYPTED) ?: false
105107

106108
val inflater = requireActivity().layoutInflater
107109
binding = EditBoxDialogBinding.inflate(inflater, null, false)
@@ -185,7 +187,7 @@ class CreateFolderDialogFragment : DialogFragment(), DialogInterface.OnClickList
185187
val path = parentFolder?.decryptedRemotePath + newFolderName + OCFile.PATH_SEPARATOR
186188
lifecycleScope.launch(Dispatchers.IO) {
187189
if (connectivityService.isNetworkAndServerAvailable()) {
188-
(requireActivity() as ComponentsGetter).fileOperationsHelper.createFolder(path)
190+
(requireActivity() as ComponentsGetter).fileOperationsHelper.createFolder(path, encrypted)
189191
} else {
190192
Log_OC.d(TAG, "Network not available, creating offline operation")
191193
fileDataStorageManager.addCreateFolderOfflineOperation(
@@ -207,18 +209,24 @@ class CreateFolderDialogFragment : DialogFragment(), DialogInterface.OnClickList
207209
companion object {
208210
private const val TAG = "CreateFolderDialogFragment"
209211
private const val ARG_PARENT_FOLDER = "PARENT_FOLDER"
212+
private const val ARG_ENCRYPTED = "ENCRYPTED"
210213
const val CREATE_FOLDER_FRAGMENT = "CREATE_FOLDER_FRAGMENT"
211214

215+
@JvmStatic
216+
fun newInstance(parentFolder: OCFile?): CreateFolderDialogFragment {
217+
return newInstance(parentFolder, false)
218+
}
212219
/**
213220
* Public factory method to create new CreateFolderDialogFragment instances.
214221
*
215222
* @param parentFolder Folder to create
216223
* @return Dialog ready to show.
217224
*/
218225
@JvmStatic
219-
fun newInstance(parentFolder: OCFile?): CreateFolderDialogFragment {
226+
fun newInstance(parentFolder: OCFile?, encrypted: Boolean): CreateFolderDialogFragment {
220227
val bundle = Bundle().apply {
221228
putParcelable(ARG_PARENT_FOLDER, parentFolder)
229+
putBoolean(ARG_ENCRYPTED, encrypted)
222230
}
223231

224232
return CreateFolderDialogFragment().apply {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ package com.owncloud.android.ui.events
1010
* Event for set folder as encrypted/decrypted
1111
*/
1212
class EncryptionEvent(
13+
@JvmField
1314
val localId: Long,
15+
@JvmField
1416
val remoteId: String,
17+
@JvmField
1518
val remotePath: String,
19+
@JvmField
1620
val shouldBeEncrypted: Boolean
1721
)

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ public interface OCFileListBottomSheetActions {
1818
*/
1919
void createFolder();
2020

21+
/**
22+
* creates an encrypted folder within the actual folder
23+
*/
24+
void createEncryptedFolder();
25+
2126
/**
2227
* offers a file upload with the Android OS file picker to the current folder.
2328
*/

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,8 @@ protected void onCreate(Bundle savedInstanceState) {
8686
binding.addToCloud.setText(getContext().getResources().getString(R.string.add_to_cloud,
8787
themeUtils.getDefaultDisplayNameForRootFolder(getContext())));
8888

89-
OCCapability capability = fileActivity.getCapabilities();
90-
if (capability != null &&
91-
capability.getRichDocuments().isTrue() &&
89+
OCCapability capability = fileActivity.getStorageManager().getCapability(user.getAccountName());
90+
if (capability.getRichDocuments().isTrue() &&
9291
capability.getRichDocumentsDirectEditing().isTrue() &&
9392
capability.getRichDocumentsTemplatesAvailable().isTrue() &&
9493
!file.isEncrypted()) {
@@ -136,6 +135,12 @@ protected void onCreate(Bundle savedInstanceState) {
136135
binding.menuDirectCameraUpload.setVisibility(View.GONE);
137136
}
138137

138+
if (capability.getEndToEndEncryption().isTrue() && OCFile.ROOT_PATH.equals(file.getRemotePath())) {
139+
binding.menuEncryptedMkdir.setVisibility(View.VISIBLE);
140+
} else {
141+
binding.menuEncryptedMkdir.setVisibility(View.GONE);
142+
}
143+
139144
// create rich workspace
140145
if (editorUtils.isEditorAvailable(user,
141146
MimeTypeUtil.MIMETYPE_TEXT_MARKDOWN) &&
@@ -171,6 +176,11 @@ private void setupClickListener() {
171176
dismiss();
172177
});
173178

179+
binding.menuEncryptedMkdir.setOnClickListener(v -> {
180+
actions.createEncryptedFolder();
181+
dismiss();
182+
});
183+
174184
binding.menuUploadFromApp.setOnClickListener(v -> {
175185
actions.uploadFromApp();
176186
dismiss();

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

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,14 @@ public void createFolder() {
511511
.show(getActivity().getSupportFragmentManager(), DIALOG_CREATE_FOLDER);
512512
}
513513

514+
@Override
515+
public void createEncryptedFolder() {
516+
if (checkEncryptionIsSetup(null)) {
517+
CreateFolderDialogFragment.newInstance(mFile, true)
518+
.show(getActivity().getSupportFragmentManager(), DIALOG_CREATE_FOLDER);
519+
}
520+
}
521+
514522
@Override
515523
public void uploadFromApp() {
516524
Intent action = new Intent(Intent.ACTION_GET_CONTENT);
@@ -1162,10 +1170,11 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
11621170
int position = data.getIntExtra(SetupEncryptionDialogFragment.ARG_POSITION, -1);
11631171
OCFile file = mAdapter.getItem(position);
11641172

1165-
if (file != null) {
1166-
mContainerActivity.getFileOperationsHelper().toggleEncryption(file, true);
1167-
mAdapter.setEncryptionAttributeForItemID(file.getRemoteId(), true);
1173+
if (file == null) {
1174+
return;
11681175
}
1176+
mContainerActivity.getFileOperationsHelper().toggleEncryption(file, true);
1177+
mAdapter.setEncryptionAttributeForItemID(file.getRemoteId(), true);
11691178

11701179
// update state and view of this fragment
11711180
searchFragment = false;
@@ -1782,49 +1791,52 @@ protected RemoteOperation getSearchRemoteOperation(final User currentUser, final
17821791

17831792
@Subscribe(threadMode = ThreadMode.BACKGROUND)
17841793
public void onMessageEvent(EncryptionEvent event) {
1794+
if (checkEncryptionIsSetup(event.remoteId)) {
1795+
encryptFolder(event.localId, event.remoteId, event.remotePath, event.shouldBeEncrypted);
1796+
}
1797+
}
1798+
1799+
private boolean checkEncryptionIsSetup(@Nullable String remoteId) {
17851800
final User user = accountManager.getUser();
17861801

17871802
// check if keys are stored
17881803
String publicKey = arbitraryDataProvider.getValue(user, EncryptionUtils.PUBLIC_KEY);
17891804
String privateKey = arbitraryDataProvider.getValue(user, EncryptionUtils.PRIVATE_KEY);
17901805

1791-
FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
1792-
OCFile file = storageManager.getFileByRemoteId(event.getRemoteId());
1793-
17941806
if (publicKey.isEmpty() || privateKey.isEmpty()) {
17951807
Log_OC.d(TAG, "no public key for " + user.getAccountName());
17961808

1809+
FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
17971810
int position = -1;
1798-
if (file != null) {
1799-
position = mAdapter.getItemPosition(file);
1811+
if (remoteId != null) {
1812+
OCFile file = storageManager.getFileByRemoteId(remoteId);
1813+
if (file != null) {
1814+
position = mAdapter.getItemPosition(file);
1815+
}
18001816
}
18011817
SetupEncryptionDialogFragment dialog = SetupEncryptionDialogFragment.newInstance(user, position);
18021818
dialog.setTargetFragment(this, SETUP_ENCRYPTION_REQUEST_CODE);
18031819
dialog.show(getParentFragmentManager(), SETUP_ENCRYPTION_DIALOG_TAG);
1820+
1821+
return false;
18041822
} else {
1805-
// TODO E2E: if encryption fails, to not set it as encrypted!
1806-
encryptFolder(file,
1807-
event.getLocalId(),
1808-
event.getRemoteId(),
1809-
event.getRemotePath(),
1810-
event.getShouldBeEncrypted(),
1811-
publicKey,
1812-
privateKey,
1813-
storageManager);
1823+
return true;
18141824
}
18151825
}
18161826

1817-
private void encryptFolder(OCFile folder,
1818-
long localId,
1827+
private void encryptFolder(long localId,
18191828
String remoteId,
18201829
String remotePath,
1821-
boolean shouldBeEncrypted,
1822-
String publicKeyString,
1823-
String privateKeyString,
1824-
FileDataStorageManager storageManager) {
1830+
boolean shouldBeEncrypted) {
18251831
try {
1826-
Log_OC.d(TAG, "encrypt folder " + folder.getRemoteId());
1832+
Log_OC.d(TAG, "encrypt folder " + remoteId);
18271833
User user = accountManager.getUser();
1834+
String publicKey = arbitraryDataProvider.getValue(user, EncryptionUtils.PUBLIC_KEY);
1835+
String privateKey = arbitraryDataProvider.getValue(user, EncryptionUtils.PRIVATE_KEY);
1836+
1837+
FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
1838+
OCFile folder = storageManager.getFileByRemoteId(remoteId);
1839+
18281840
OwnCloudClient client = clientFactory.create(user);
18291841
RemoteOperationResult remoteOperationResult = new ToggleEncryptionRemoteOperation(localId,
18301842
remotePath,
@@ -1841,8 +1853,8 @@ private void encryptFolder(OCFile folder,
18411853
// Update metadata
18421854
Pair<Boolean, DecryptedFolderMetadataFile> metadataPair = EncryptionUtils.retrieveMetadata(folder,
18431855
client,
1844-
privateKeyString,
1845-
publicKeyString,
1856+
privateKey,
1857+
publicKey,
18461858
storageManager,
18471859
user,
18481860
requireContext(),

0 commit comments

Comments
 (0)