Skip to content

Commit 4cb0e39

Browse files
committed
Allow to create encrypted folder directly from bottom sheet dialog from NC PR: nextcloud#10782
1 parent 2e7430b commit 4cb0e39

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
@@ -236,7 +236,7 @@ private void filterUnlock(List<Integer> toHide, boolean fileLockingEnabled) {
236236

237237
private void filterEncrypt(List<Integer> toHide, boolean endToEndEncryptionEnabled) {
238238
if (files.isEmpty() || !isSingleSelection() || isSingleFile() || isEncryptedFolder() || isGroupFolder()
239-
|| !endToEndEncryptionEnabled || !isEmptyFolder() || isShared()) {
239+
|| !endToEndEncryptionEnabled || !isEmptyFolder() || isShared() || isInSubFolder()) {
240240
toHide.add(R.id.action_encrypted);
241241
}
242242
}
@@ -574,4 +574,15 @@ private boolean isShared() {
574574
}
575575
return false;
576576
}
577+
578+
private boolean isInSubFolder() {
579+
OCFile folder = files.iterator().next();
580+
OCFile parent = storageManager.getFileById(folder.getParentId());
581+
582+
if (parent == null) {
583+
return false;
584+
}
585+
586+
return !OCFile.ROOT_PATH.equals(parent.getRemotePath());
587+
}
577588
}

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

324324
@Nullable
325325
public OCFile getItem(int position) {
326+
if (position == -1) {
327+
return null;
328+
}
329+
326330
int newPosition = position;
327331

328332
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
@@ -60,6 +60,7 @@ class CreateFolderDialogFragment : DialogFragment(), DialogInterface.OnClickList
6060

6161
private var mParentFolder: OCFile? = null
6262
private var positiveButton: MaterialButton? = null
63+
private var encrypted = false
6364

6465
private lateinit var binding: EditBoxDialogBinding
6566

@@ -94,6 +95,7 @@ class CreateFolderDialogFragment : DialogFragment(), DialogInterface.OnClickList
9495
@Suppress("EmptyFunctionBlock")
9596
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
9697
mParentFolder = arguments?.getParcelableArgument(ARG_PARENT_FOLDER, OCFile::class.java)
98+
encrypted = arguments?.getBoolean(ARG_ENCRYPTED) ?: false
9799

98100
val inflater = requireActivity().layoutInflater
99101
binding = EditBoxDialogBinding.inflate(inflater, null, false)
@@ -175,24 +177,30 @@ class CreateFolderDialogFragment : DialogFragment(), DialogInterface.OnClickList
175177
}
176178

177179
val path = mParentFolder?.decryptedRemotePath + newFolderName + OCFile.PATH_SEPARATOR
178-
typedActivity<ComponentsGetter>()?.fileOperationsHelper?.createFolder(path)
180+
typedActivity<ComponentsGetter>()?.fileOperationsHelper?.createFolder(path, encrypted)
179181
}
180182
}
181183

182184
companion object {
183185
private const val ARG_PARENT_FOLDER = "PARENT_FOLDER"
186+
private const val ARG_ENCRYPTED = "ENCRYPTED"
184187
const val CREATE_FOLDER_FRAGMENT = "CREATE_FOLDER_FRAGMENT"
185188

189+
@JvmStatic
190+
fun newInstance(parentFolder: OCFile?): CreateFolderDialogFragment {
191+
return newInstance(parentFolder, false)
192+
}
186193
/**
187194
* Public factory method to create new CreateFolderDialogFragment instances.
188195
*
189196
* @param parentFolder Folder to create
190197
* @return Dialog ready to show.
191198
*/
192199
@JvmStatic
193-
fun newInstance(parentFolder: OCFile?): CreateFolderDialogFragment {
200+
fun newInstance(parentFolder: OCFile?, encrypted: Boolean): CreateFolderDialogFragment {
194201
val bundle = Bundle().apply {
195202
putParcelable(ARG_PARENT_FOLDER, parentFolder)
203+
putBoolean(ARG_ENCRYPTED, encrypted)
196204
}
197205

198206
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) &&
@@ -170,6 +175,11 @@ private void setupClickListener() {
170175
dismiss();
171176
});
172177

178+
binding.menuEncryptedMkdir.setOnClickListener(v -> {
179+
actions.createEncryptedFolder();
180+
dismiss();
181+
});
182+
173183
binding.menuUploadFromApp.setOnClickListener(v -> {
174184
actions.uploadFromApp();
175185
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
@@ -510,6 +510,14 @@ public void createFolder() {
510510
.show(getActivity().getSupportFragmentManager(), DIALOG_CREATE_FOLDER);
511511
}
512512

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

1162-
if (file != null) {
1163-
mContainerActivity.getFileOperationsHelper().toggleEncryption(file, true);
1164-
mAdapter.setEncryptionAttributeForItemID(file.getRemoteId(), true);
1170+
if (file == null) {
1171+
return;
11651172
}
1173+
mContainerActivity.getFileOperationsHelper().toggleEncryption(file, true);
1174+
mAdapter.setEncryptionAttributeForItemID(file.getRemoteId(), true);
11661175

11671176
// update state and view of this fragment
11681177
searchFragment = false;
@@ -1776,49 +1785,52 @@ protected RemoteOperation getSearchRemoteOperation(final User currentUser, final
17761785

17771786
@Subscribe(threadMode = ThreadMode.BACKGROUND)
17781787
public void onMessageEvent(EncryptionEvent event) {
1788+
if (checkEncryptionIsSetup(event.remoteId)) {
1789+
encryptFolder(event.localId, event.remoteId, event.remotePath, event.shouldBeEncrypted);
1790+
}
1791+
}
1792+
1793+
private boolean checkEncryptionIsSetup(@Nullable String remoteId) {
17791794
final User user = accountManager.getUser();
17801795

17811796
// check if keys are stored
17821797
String publicKey = arbitraryDataProvider.getValue(user, EncryptionUtils.PUBLIC_KEY);
17831798
String privateKey = arbitraryDataProvider.getValue(user, EncryptionUtils.PRIVATE_KEY);
17841799

1785-
FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
1786-
OCFile file = storageManager.getFileByRemoteId(event.getRemoteId());
1787-
17881800
if (publicKey.isEmpty() || privateKey.isEmpty()) {
17891801
Log_OC.d(TAG, "no public key for " + user.getAccountName());
17901802

1803+
FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
17911804
int position = -1;
1792-
if (file != null) {
1793-
position = mAdapter.getItemPosition(file);
1805+
if (remoteId != null) {
1806+
OCFile file = storageManager.getFileByRemoteId(remoteId);
1807+
if (file != null) {
1808+
position = mAdapter.getItemPosition(file);
1809+
}
17941810
}
17951811
SetupEncryptionDialogFragment dialog = SetupEncryptionDialogFragment.newInstance(user, position);
17961812
dialog.setTargetFragment(this, SETUP_ENCRYPTION_REQUEST_CODE);
17971813
dialog.show(getParentFragmentManager(), SETUP_ENCRYPTION_DIALOG_TAG);
1814+
1815+
return false;
17981816
} else {
1799-
// TODO E2E: if encryption fails, to not set it as encrypted!
1800-
encryptFolder(file,
1801-
event.getLocalId(),
1802-
event.getRemoteId(),
1803-
event.getRemotePath(),
1804-
event.getShouldBeEncrypted(),
1805-
publicKey,
1806-
privateKey,
1807-
storageManager);
1817+
return true;
18081818
}
18091819
}
18101820

1811-
private void encryptFolder(OCFile folder,
1812-
long localId,
1821+
private void encryptFolder(long localId,
18131822
String remoteId,
18141823
String remotePath,
1815-
boolean shouldBeEncrypted,
1816-
String publicKeyString,
1817-
String privateKeyString,
1818-
FileDataStorageManager storageManager) {
1824+
boolean shouldBeEncrypted) {
18191825
try {
1820-
Log_OC.d(TAG, "encrypt folder " + folder.getRemoteId());
1826+
Log_OC.d(TAG, "encrypt folder " + remoteId);
18211827
User user = accountManager.getUser();
1828+
String publicKey = arbitraryDataProvider.getValue(user, EncryptionUtils.PUBLIC_KEY);
1829+
String privateKey = arbitraryDataProvider.getValue(user, EncryptionUtils.PRIVATE_KEY);
1830+
1831+
FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
1832+
OCFile folder = storageManager.getFileByRemoteId(remoteId);
1833+
18221834
OwnCloudClient client = clientFactory.create(user);
18231835
RemoteOperationResult remoteOperationResult = new ToggleEncryptionRemoteOperation(localId,
18241836
remotePath,
@@ -1835,8 +1847,8 @@ private void encryptFolder(OCFile folder,
18351847
// Update metadata
18361848
Pair<Boolean, DecryptedFolderMetadataFile> metadataPair = EncryptionUtils.retrieveMetadata(folder,
18371849
client,
1838-
privateKeyString,
1839-
publicKeyString,
1850+
privateKey,
1851+
publicKey,
18401852
storageManager,
18411853
user,
18421854
requireContext(),

0 commit comments

Comments
 (0)