Skip to content

Commit 4445bc3

Browse files
committed
Allow to create encrypted folder directly from bottom sheet dialog from NC PR: nextcloud#10782
1 parent 9b69807 commit 4445bc3

File tree

11 files changed

+132
-35
lines changed

11 files changed

+132
-35
lines changed

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
@@ -344,6 +344,11 @@ public void createFolder() {
344344

345345
}
346346

347+
@Override
348+
public void createEncryptedFolder() {
349+
350+
}
351+
347352
@Override
348353
public void uploadFromApp() {
349354

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
@@ -337,6 +337,10 @@ public int getItemCount() {
337337

338338
@Nullable
339339
public OCFile getItem(int position) {
340+
if (position == -1) {
341+
return null;
342+
}
343+
340344
int newPosition = position;
341345

342346
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
@@ -66,6 +66,7 @@ class CreateFolderDialogFragment : DialogFragment(), DialogInterface.OnClickList
6666

6767
private var parentFolder: OCFile? = null
6868
private var positiveButton: MaterialButton? = null
69+
private var encrypted = false
6970

7071
private lateinit var binding: EditBoxDialogBinding
7172

@@ -100,6 +101,7 @@ class CreateFolderDialogFragment : DialogFragment(), DialogInterface.OnClickList
100101
@Suppress("EmptyFunctionBlock")
101102
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
102103
parentFolder = arguments?.getParcelableArgument(ARG_PARENT_FOLDER, OCFile::class.java)
104+
encrypted = arguments?.getBoolean(ARG_ENCRYPTED) ?: false
103105

104106
val inflater = requireActivity().layoutInflater
105107
binding = EditBoxDialogBinding.inflate(inflater, null, false)
@@ -183,7 +185,7 @@ class CreateFolderDialogFragment : DialogFragment(), DialogInterface.OnClickList
183185
val path = parentFolder?.decryptedRemotePath + newFolderName + OCFile.PATH_SEPARATOR
184186
connectivityService.isNetworkAndServerAvailable { result ->
185187
if (result) {
186-
typedActivity<ComponentsGetter>()?.fileOperationsHelper?.createFolder(path)
188+
typedActivity<ComponentsGetter>()?.fileOperationsHelper?.createFolder(path, encrypted)
187189
} else {
188190
Log_OC.d(TAG, "Network not available, creating offline operation")
189191
fileDataStorageManager.addCreateFolderOfflineOperation(
@@ -201,18 +203,24 @@ class CreateFolderDialogFragment : DialogFragment(), DialogInterface.OnClickList
201203
companion object {
202204
private const val TAG = "CreateFolderDialogFragment"
203205
private const val ARG_PARENT_FOLDER = "PARENT_FOLDER"
206+
private const val ARG_ENCRYPTED = "ENCRYPTED"
204207
const val CREATE_FOLDER_FRAGMENT = "CREATE_FOLDER_FRAGMENT"
205208

209+
@JvmStatic
210+
fun newInstance(parentFolder: OCFile?): CreateFolderDialogFragment {
211+
return newInstance(parentFolder, false)
212+
}
206213
/**
207214
* Public factory method to create new CreateFolderDialogFragment instances.
208215
*
209216
* @param parentFolder Folder to create
210217
* @return Dialog ready to show.
211218
*/
212219
@JvmStatic
213-
fun newInstance(parentFolder: OCFile?): CreateFolderDialogFragment {
220+
fun newInstance(parentFolder: OCFile?, encrypted: Boolean): CreateFolderDialogFragment {
214221
val bundle = Bundle().apply {
215222
putParcelable(ARG_PARENT_FOLDER, parentFolder)
223+
putBoolean(ARG_ENCRYPTED, encrypted)
216224
}
217225

218226
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
@@ -515,6 +515,14 @@ public void createFolder() {
515515
.show(getActivity().getSupportFragmentManager(), DIALOG_CREATE_FOLDER);
516516
}
517517

518+
@Override
519+
public void createEncryptedFolder() {
520+
if (checkEncryptionIsSetup(null)) {
521+
CreateFolderDialogFragment.newInstance(mFile, true)
522+
.show(getActivity().getSupportFragmentManager(), DIALOG_CREATE_FOLDER);
523+
}
524+
}
525+
518526
@Override
519527
public void uploadFromApp() {
520528
Intent action = new Intent(Intent.ACTION_GET_CONTENT);
@@ -1271,10 +1279,11 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
12711279
int position = data.getIntExtra(SetupEncryptionDialogFragment.ARG_POSITION, -1);
12721280
OCFile file = mAdapter.getItem(position);
12731281

1274-
if (file != null) {
1275-
mContainerActivity.getFileOperationsHelper().toggleEncryption(file, true);
1276-
mAdapter.setEncryptionAttributeForItemID(file.getRemoteId(), true);
1282+
if (file == null) {
1283+
return;
12771284
}
1285+
mContainerActivity.getFileOperationsHelper().toggleEncryption(file, true);
1286+
mAdapter.setEncryptionAttributeForItemID(file.getRemoteId(), true);
12781287

12791288
// update state and view of this fragment
12801289
searchFragment = false;
@@ -1891,49 +1900,52 @@ protected RemoteOperation getSearchRemoteOperation(final User currentUser, final
18911900

18921901
@Subscribe(threadMode = ThreadMode.BACKGROUND)
18931902
public void onMessageEvent(EncryptionEvent event) {
1903+
if (checkEncryptionIsSetup(event.remoteId)) {
1904+
encryptFolder(event.localId, event.remoteId, event.remotePath, event.shouldBeEncrypted);
1905+
}
1906+
}
1907+
1908+
private boolean checkEncryptionIsSetup(@Nullable String remoteId) {
18941909
final User user = accountManager.getUser();
18951910

18961911
// check if keys are stored
18971912
String publicKey = arbitraryDataProvider.getValue(user, EncryptionUtils.PUBLIC_KEY);
18981913
String privateKey = arbitraryDataProvider.getValue(user, EncryptionUtils.PRIVATE_KEY);
18991914

1900-
FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
1901-
OCFile file = storageManager.getFileByRemoteId(event.getRemoteId());
1902-
19031915
if (publicKey.isEmpty() || privateKey.isEmpty()) {
19041916
Log_OC.d(TAG, "no public key for " + user.getAccountName());
19051917

1918+
FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
19061919
int position = -1;
1907-
if (file != null) {
1908-
position = mAdapter.getItemPosition(file);
1920+
if (remoteId != null) {
1921+
OCFile file = storageManager.getFileByRemoteId(remoteId);
1922+
if (file != null) {
1923+
position = mAdapter.getItemPosition(file);
1924+
}
19091925
}
19101926
SetupEncryptionDialogFragment dialog = SetupEncryptionDialogFragment.newInstance(user, position);
19111927
dialog.setTargetFragment(this, SETUP_ENCRYPTION_REQUEST_CODE);
19121928
dialog.show(getParentFragmentManager(), SETUP_ENCRYPTION_DIALOG_TAG);
1929+
1930+
return false;
19131931
} else {
1914-
// TODO E2E: if encryption fails, to not set it as encrypted!
1915-
encryptFolder(file,
1916-
event.getLocalId(),
1917-
event.getRemoteId(),
1918-
event.getRemotePath(),
1919-
event.getShouldBeEncrypted(),
1920-
publicKey,
1921-
privateKey,
1922-
storageManager);
1932+
return true;
19231933
}
19241934
}
19251935

1926-
private void encryptFolder(OCFile folder,
1927-
long localId,
1936+
private void encryptFolder(long localId,
19281937
String remoteId,
19291938
String remotePath,
1930-
boolean shouldBeEncrypted,
1931-
String publicKeyString,
1932-
String privateKeyString,
1933-
FileDataStorageManager storageManager) {
1939+
boolean shouldBeEncrypted) {
19341940
try {
1935-
Log_OC.d(TAG, "encrypt folder " + folder.getRemoteId());
1941+
Log_OC.d(TAG, "encrypt folder " + remoteId);
19361942
User user = accountManager.getUser();
1943+
String publicKey = arbitraryDataProvider.getValue(user, EncryptionUtils.PUBLIC_KEY);
1944+
String privateKey = arbitraryDataProvider.getValue(user, EncryptionUtils.PRIVATE_KEY);
1945+
1946+
FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
1947+
OCFile folder = storageManager.getFileByRemoteId(remoteId);
1948+
19371949
OwnCloudClient client = clientFactory.create(user);
19381950
RemoteOperationResult remoteOperationResult = new ToggleEncryptionRemoteOperation(localId,
19391951
remotePath,
@@ -1950,8 +1962,8 @@ private void encryptFolder(OCFile folder,
19501962
// Update metadata
19511963
Pair<Boolean, DecryptedFolderMetadataFile> metadataPair = EncryptionUtils.retrieveMetadata(folder,
19521964
client,
1953-
privateKeyString,
1954-
publicKeyString,
1965+
privateKey,
1966+
publicKey,
19551967
storageManager,
19561968
user,
19571969
requireContext(),

0 commit comments

Comments
 (0)