Skip to content

Commit 2cb2d34

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

File tree

10 files changed

+127
-32
lines changed

10 files changed

+127
-32
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
@@ -324,6 +324,11 @@ public void createFolder() {
324324

325325
}
326326

327+
@Override
328+
public void createEncryptedFolder() {
329+
330+
}
331+
327332
@Override
328333
public void uploadFromApp() {
329334

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

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

245245
private void filterEncrypt(List<Integer> toHide, boolean endToEndEncryptionEnabled) {
246246
if (files.isEmpty() || !isSingleSelection() || isSingleFile() || isEncryptedFolder() || isGroupFolder()
247-
|| !endToEndEncryptionEnabled || !isEmptyFolder() || isShared()) {
247+
|| !endToEndEncryptionEnabled || !isEmptyFolder() || isShared() || isInSubFolder()) {
248248
toHide.add(R.id.action_encrypted);
249249
}
250250
}
@@ -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
@@ -65,16 +65,31 @@ public class CreateFolderOperation extends SyncOperation implements OnRemoteOper
6565
private RemoteFile createdRemoteFolder;
6666
private User user;
6767
private Context context;
68+
private boolean encrypted;
6869

6970
/**
7071
* Constructor
7172
*/
72-
public CreateFolderOperation(String remotePath, User user, Context context, FileDataStorageManager storageManager) {
73+
public CreateFolderOperation(String remotePath,
74+
User user,
75+
Context context,
76+
FileDataStorageManager storageManager
77+
) {
78+
this(remotePath, false, user, context, storageManager);
79+
}
80+
81+
public CreateFolderOperation(String remotePath,
82+
boolean encrypted,
83+
User user,
84+
Context context,
85+
FileDataStorageManager storageManager
86+
) {
7387
super(storageManager);
7488

7589
this.remotePath = remotePath;
7690
this.user = user;
7791
this.context = context;
92+
this.encrypted = encrypted;
7893
}
7994

8095
@Override
@@ -102,7 +117,7 @@ protected RemoteOperationResult run(OwnCloudClient client) {
102117
if (encryptedAncestor) {
103118
return encryptedCreate(parent, client);
104119
} else {
105-
return normalCreate(client);
120+
return normalCreate(client, encrypted);
106121
}
107122
}
108123

@@ -288,7 +303,7 @@ private String createRandomFileName(DecryptedFolderMetadata metadata) {
288303
return encryptedFileName;
289304
}
290305

291-
private RemoteOperationResult normalCreate(OwnCloudClient client) {
306+
private RemoteOperationResult normalCreate(OwnCloudClient client, boolean encrypted) {
292307
RemoteOperationResult result = new CreateFolderRemoteOperation(remotePath, true).execute(client);
293308

294309
if (result.isSuccess()) {
@@ -297,6 +312,21 @@ private RemoteOperationResult normalCreate(OwnCloudClient client) {
297312

298313
createdRemoteFolder = (RemoteFile) remoteFolderOperationResult.getData().get(0);
299314
saveFolderInDB();
315+
316+
if (encrypted) {
317+
final OCFile folder = getStorageManager().getFileByDecryptedRemotePath(remotePath);
318+
319+
final RemoteOperationResult remoteOperationResult =
320+
new ToggleEncryptionRemoteOperation(folder.getLocalId(),
321+
remotePath,
322+
true)
323+
.execute(client);
324+
325+
if (remoteOperationResult.isSuccess()) {
326+
folder.setEncrypted(true);
327+
getStorageManager().saveFile(folder);
328+
}
329+
}
300330
} else {
301331
Log_OC.e(TAG, remotePath + " hasn't been created");
302332
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public class OperationsService extends Service {
9393
public static final String EXTRA_ACCOUNT = "ACCOUNT";
9494
public static final String EXTRA_SERVER_URL = "SERVER_URL";
9595
public static final String EXTRA_REMOTE_PATH = "REMOTE_PATH";
96+
public static final String EXTRA_ENCRYPTED = "ENCRYPTED";
9697
public static final String EXTRA_NEWNAME = "NEWNAME";
9798
public static final String EXTRA_REMOVE_ONLY_LOCAL = "REMOVE_LOCAL_COPY";
9899
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
@@ -570,6 +570,10 @@ private String generateFooterText(int filesCount, int foldersCount) {
570570

571571
public @Nullable
572572
OCFile getItem(int position) {
573+
if (position == -1) {
574+
return null;
575+
}
576+
573577
int newPosition = position;
574578

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

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public class CreateFolderDialogFragment
6363
extends DialogFragment implements DialogInterface.OnClickListener, Injectable {
6464

6565
private static final String ARG_PARENT_FOLDER = "PARENT_FOLDER";
66+
private static final String ARG_ENCRYPTED = "ENCRYPTED";
6667

6768
public static final String CREATE_FOLDER_FRAGMENT = "CREATE_FOLDER_FRAGMENT";
6869

@@ -71,8 +72,9 @@ public class CreateFolderDialogFragment
7172
@Inject KeyboardUtils keyboardUtils;
7273

7374

74-
private OCFile mParentFolder;
75+
private OCFile parentFolder;
7576
private MaterialButton positiveButton;
77+
private boolean encrypted;
7678

7779

7880
private EditBoxDialogBinding binding;
@@ -84,12 +86,16 @@ public class CreateFolderDialogFragment
8486
* @return Dialog ready to show.
8587
*/
8688
public static CreateFolderDialogFragment newInstance(OCFile parentFolder) {
89+
return newInstance(parentFolder, false);
90+
}
91+
92+
public static CreateFolderDialogFragment newInstance(OCFile parentFolder, boolean encrypted) {
8793
CreateFolderDialogFragment frag = new CreateFolderDialogFragment();
8894
Bundle args = new Bundle();
8995
args.putParcelable(ARG_PARENT_FOLDER, parentFolder);
96+
args.putBoolean(ARG_ENCRYPTED, encrypted);
9097
frag.setArguments(args);
9198
return frag;
92-
9399
}
94100

95101
@Override
@@ -121,7 +127,8 @@ public void onResume() {
121127
@NonNull
122128
@Override
123129
public Dialog onCreateDialog(Bundle savedInstanceState) {
124-
mParentFolder = getArguments().getParcelable(ARG_PARENT_FOLDER);
130+
parentFolder = getArguments().getParcelable(ARG_PARENT_FOLDER);
131+
encrypted = getArguments().getBoolean(ARG_ENCRYPTED);
125132

126133
// Inflate the layout for the dialog
127134
LayoutInflater inflater = requireActivity().getLayoutInflater();
@@ -222,9 +229,9 @@ public void onClick(DialogInterface dialog, int which) {
222229
return;
223230
}
224231

225-
String path = mParentFolder.getDecryptedRemotePath() + newFolderName + OCFile.PATH_SEPARATOR;
232+
String path = parentFolder.getDecryptedRemotePath() + newFolderName + OCFile.PATH_SEPARATOR;
226233

227-
((ComponentsGetter) requireActivity()).getFileOperationsHelper().createFolder(path);
234+
((ComponentsGetter) requireActivity()).getFileOperationsHelper().createFolder(path, encrypted);
228235
}
229236
}
230237
}

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
@@ -32,6 +32,11 @@ public interface OCFileListBottomSheetActions {
3232
*/
3333
void createFolder();
3434

35+
/**
36+
* creates an encrypted folder within the actual folder
37+
*/
38+
void createEncryptedFolder();
39+
3540
/**
3641
* offers a file upload with the Android OS file picker to the current folder.
3742
*/

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
@@ -99,9 +99,8 @@ protected void onCreate(Bundle savedInstanceState) {
9999
binding.addToCloud.setText(getContext().getResources().getString(R.string.add_to_cloud,
100100
themeUtils.getDefaultDisplayNameForRootFolder(getContext())));
101101

102-
OCCapability capability = fileActivity.getCapabilities();
103-
if (capability != null &&
104-
capability.getRichDocuments().isTrue() &&
102+
OCCapability capability = fileActivity.getStorageManager().getCapability(user.getAccountName());
103+
if (capability.getRichDocuments().isTrue() &&
105104
capability.getRichDocumentsDirectEditing().isTrue() &&
106105
capability.getRichDocumentsTemplatesAvailable().isTrue() &&
107106
!file.isEncrypted()) {
@@ -149,6 +148,12 @@ protected void onCreate(Bundle savedInstanceState) {
149148
binding.menuDirectCameraUpload.setVisibility(View.GONE);
150149
}
151150

151+
if (capability.getEndToEndEncryption().isTrue() && OCFile.ROOT_PATH.equals(file.getRemotePath())) {
152+
binding.menuEncryptedMkdir.setVisibility(View.VISIBLE);
153+
} else {
154+
binding.menuEncryptedMkdir.setVisibility(View.GONE);
155+
}
156+
152157
// create rich workspace
153158
if (editorUtils.isEditorAvailable(user,
154159
MimeTypeUtil.MIMETYPE_TEXT_MARKDOWN) &&
@@ -183,6 +188,11 @@ private void setupClickListener() {
183188
dismiss();
184189
});
185190

191+
binding.menuEncryptedMkdir.setOnClickListener(v -> {
192+
actions.createEncryptedFolder();
193+
dismiss();
194+
});
195+
186196
binding.menuUploadFromApp.setOnClickListener(v -> {
187197
actions.uploadFromApp();
188198
dismiss();

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

Lines changed: 35 additions & 20 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);
@@ -1125,10 +1133,11 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
11251133
int position = data.getIntExtra(SetupEncryptionDialogFragment.ARG_POSITION, -1);
11261134
OCFile file = mAdapter.getItem(position);
11271135

1128-
if (file != null) {
1129-
mContainerActivity.getFileOperationsHelper().toggleEncryption(file, true);
1130-
mAdapter.setEncryptionAttributeForItemID(file.getRemoteId(), true);
1136+
if (file == null) {
1137+
return;
11311138
}
1139+
mContainerActivity.getFileOperationsHelper().toggleEncryption(file, true);
1140+
mAdapter.setEncryptionAttributeForItemID(file.getRemoteId(), true);
11321141

11331142
// update state and view of this fragment
11341143
searchFragment = false;
@@ -1697,45 +1706,51 @@ protected RemoteOperation getSearchRemoteOperation(final User currentUser, final
16971706

16981707
@Subscribe(threadMode = ThreadMode.BACKGROUND)
16991708
public void onMessageEvent(EncryptionEvent event) {
1709+
if (checkEncryptionIsSetup(event.remoteId)) {
1710+
encryptFolder(event.localId, event.remoteId, event.remotePath, event.shouldBeEncrypted);
1711+
}
1712+
}
1713+
1714+
private boolean checkEncryptionIsSetup(@Nullable String remoteId) {
17001715
final User user = accountManager.getUser();
17011716

17021717
// check if keys are stored
17031718
String publicKey = arbitraryDataProvider.getValue(user, EncryptionUtils.PUBLIC_KEY);
17041719
String privateKey = arbitraryDataProvider.getValue(user, EncryptionUtils.PRIVATE_KEY);
17051720

1706-
FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
1707-
OCFile file = storageManager.getFileByRemoteId(event.remoteId);
1708-
17091721
if (publicKey.isEmpty() || privateKey.isEmpty()) {
17101722
Log_OC.d(TAG, "no public key for " + user.getAccountName());
17111723

1724+
FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
17121725
int position = -1;
1713-
if (file != null) {
1714-
position = mAdapter.getItemPosition(file);
1726+
if (remoteId != null) {
1727+
OCFile file = storageManager.getFileByRemoteId(remoteId);
1728+
if (file != null) {
1729+
position = mAdapter.getItemPosition(file);
1730+
}
17151731
}
17161732
SetupEncryptionDialogFragment dialog = SetupEncryptionDialogFragment.newInstance(user, position);
17171733
dialog.setTargetFragment(this, SETUP_ENCRYPTION_REQUEST_CODE);
17181734
dialog.show(getParentFragmentManager(), SETUP_ENCRYPTION_DIALOG_TAG);
1735+
1736+
return false;
17191737
} else {
1720-
encryptFolder(file,
1721-
event.localId,
1722-
event.remoteId,
1723-
event.remotePath,
1724-
event.shouldBeEncrypted,
1725-
publicKey,
1726-
privateKey);
1738+
return true;
17271739
}
17281740
}
17291741

1730-
private void encryptFolder(OCFile folder,
1731-
long localId,
1742+
private void encryptFolder(long localId,
17321743
String remoteId,
17331744
String remotePath,
1734-
boolean shouldBeEncrypted,
1735-
String publicKey,
1736-
String privateKey) {
1745+
boolean shouldBeEncrypted) {
17371746
try {
17381747
User user = accountManager.getUser();
1748+
String publicKey = arbitraryDataProvider.getValue(user, EncryptionUtils.PUBLIC_KEY);
1749+
String privateKey = arbitraryDataProvider.getValue(user, EncryptionUtils.PRIVATE_KEY);
1750+
1751+
FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
1752+
OCFile folder = storageManager.getFileByRemoteId(remoteId);
1753+
17391754
OwnCloudClient client = clientFactory.create(user);
17401755
RemoteOperationResult remoteOperationResult = new ToggleEncryptionRemoteOperation(localId,
17411756
remotePath,

app/src/main/java/com/owncloud/android/ui/helpers/FileOperationsHelper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,11 +971,16 @@ public void removeFiles(Collection<OCFile> files, boolean onlyLocalCopy, boolean
971971

972972

973973
public void createFolder(String remotePath) {
974+
createFolder(remotePath, false);
975+
}
976+
977+
public void createFolder(String remotePath, boolean encrypted) {
974978
// Create Folder
975979
Intent service = new Intent(fileActivity, OperationsService.class);
976980
service.setAction(OperationsService.ACTION_CREATE_FOLDER);
977981
service.putExtra(OperationsService.EXTRA_ACCOUNT, fileActivity.getAccount());
978982
service.putExtra(OperationsService.EXTRA_REMOTE_PATH, remotePath);
983+
service.putExtra(OperationsService.EXTRA_ENCRYPTED, encrypted);
979984
mWaitingForOpId = fileActivity.getOperationsServiceBinder().queueNewOperation(service);
980985

981986
fileActivity.showLoadingDialog(fileActivity.getString(R.string.wait_a_moment));

0 commit comments

Comments
 (0)