Skip to content

Commit 21be8b3

Browse files
allow to create encrypted folder directly via bottom sheet dialog
Signed-off-by: tobiasKaminsky <[email protected]>
1 parent 13071ed commit 21be8b3

File tree

10 files changed

+133
-22
lines changed

10 files changed

+133
-22
lines changed

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

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,31 @@ public class CreateFolderOperation extends SyncOperation implements OnRemoteOper
6464
private RemoteFile createdRemoteFolder;
6565
private User user;
6666
private Context context;
67+
private boolean encrypted;
6768

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

7488
this.remotePath = remotePath;
7589
this.user = user;
7690
this.context = context;
91+
this.encrypted = encrypted;
7792
}
7893

7994
@Override
@@ -101,7 +116,7 @@ protected RemoteOperationResult run(OwnCloudClient client) {
101116
if (encryptedAncestor) {
102117
return encryptedCreate(parent, client);
103118
} else {
104-
return normalCreate(client);
119+
return normalCreate(client, encrypted);
105120
}
106121
}
107122

@@ -282,7 +297,7 @@ private String createRandomFileName(DecryptedFolderMetadata metadata) {
282297
return encryptedFileName;
283298
}
284299

285-
private RemoteOperationResult normalCreate(OwnCloudClient client) {
300+
private RemoteOperationResult normalCreate(OwnCloudClient client, boolean encrypted) {
286301
RemoteOperationResult result = new CreateFolderRemoteOperation(remotePath, true).execute(client);
287302

288303
if (result.isSuccess()) {
@@ -291,6 +306,20 @@ private RemoteOperationResult normalCreate(OwnCloudClient client) {
291306

292307
createdRemoteFolder = (RemoteFile) remoteFolderOperationResult.getData().get(0);
293308
saveFolderInDB();
309+
310+
OCFile folder = getStorageManager().getFileByDecryptedRemotePath(remotePath);
311+
312+
if (encrypted) {
313+
RemoteOperationResult remoteOperationResult = new ToggleEncryptionRemoteOperation(folder.getLocalId(),
314+
remotePath,
315+
true)
316+
.execute(client);
317+
318+
if (remoteOperationResult.isSuccess()) {
319+
folder.setEncrypted(true);
320+
getStorageManager().saveFile(folder);
321+
}
322+
}
294323
} else {
295324
Log_OC.e(TAG, remotePath + " hasn't been created");
296325
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public class OperationsService extends Service {
9292
public static final String EXTRA_ACCOUNT = "ACCOUNT";
9393
public static final String EXTRA_SERVER_URL = "SERVER_URL";
9494
public static final String EXTRA_REMOTE_PATH = "REMOTE_PATH";
95+
public static final String EXTRA_ENCRYPTED = "ENCRYPTED";
9596
public static final String EXTRA_NEWNAME = "NEWNAME";
9697
public static final String EXTRA_REMOVE_ONLY_LOCAL = "REMOVE_LOCAL_COPY";
9798
public static final String EXTRA_SYNC_FILE_CONTENTS = "SYNC_FILE_CONTENTS";
@@ -676,7 +677,9 @@ private Pair<Target, RemoteOperation> newOperation(Intent operationIntent) {
676677

677678
case ACTION_CREATE_FOLDER:
678679
remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
680+
boolean encrypted = operationIntent.getBooleanExtra(EXTRA_ENCRYPTED, false);
679681
operation = new CreateFolderOperation(remotePath,
682+
encrypted,
680683
user,
681684
getApplicationContext(),
682685
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
@@ -532,6 +532,10 @@ private String generateFooterText(int filesCount, int foldersCount) {
532532

533533
public @Nullable
534534
OCFile getItem(int position) {
535+
if (position == -1) {
536+
return null;
537+
}
538+
535539
int newPosition = position;
536540

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

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public class CreateFolderDialogFragment
6262
extends DialogFragment implements DialogInterface.OnClickListener, Injectable {
6363

6464
private static final String ARG_PARENT_FOLDER = "PARENT_FOLDER";
65+
private static final String ARG_ENCRYPTED = "ENCRYPTED";
6566

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

@@ -70,8 +71,9 @@ public class CreateFolderDialogFragment
7071
@Inject KeyboardUtils keyboardUtils;
7172

7273

73-
private OCFile mParentFolder;
74+
private OCFile parentFolder;
7475
private Button positiveButton;
76+
private boolean encrypted;
7577

7678

7779
private EditBoxDialogBinding binding;
@@ -83,12 +85,16 @@ public class CreateFolderDialogFragment
8385
* @return Dialog ready to show.
8486
*/
8587
public static CreateFolderDialogFragment newInstance(OCFile parentFolder) {
88+
return newInstance(parentFolder, false);
89+
}
90+
91+
public static CreateFolderDialogFragment newInstance(OCFile parentFolder, boolean encrypted) {
8692
CreateFolderDialogFragment frag = new CreateFolderDialogFragment();
8793
Bundle args = new Bundle();
8894
args.putParcelable(ARG_PARENT_FOLDER, parentFolder);
95+
args.putBoolean(ARG_ENCRYPTED, encrypted);
8996
frag.setArguments(args);
9097
return frag;
91-
9298
}
9399

94100
@Override
@@ -114,7 +120,8 @@ public void onResume() {
114120
@NonNull
115121
@Override
116122
public Dialog onCreateDialog(Bundle savedInstanceState) {
117-
mParentFolder = getArguments().getParcelable(ARG_PARENT_FOLDER);
123+
parentFolder = getArguments().getParcelable(ARG_PARENT_FOLDER);
124+
encrypted = getArguments().getBoolean(ARG_ENCRYPTED);
118125

119126
// Inflate the layout for the dialog
120127
LayoutInflater inflater = requireActivity().getLayoutInflater();
@@ -206,7 +213,7 @@ public void onClick(DialogInterface dialog, int which) {
206213

207214
String path = mParentFolder.getDecryptedRemotePath() + newFolderName + OCFile.PATH_SEPARATOR;
208215

209-
((ComponentsGetter) getActivity()).getFileOperationsHelper().createFolder(path);
216+
((ComponentsGetter) getActivity()).getFileOperationsHelper().createFolder(path, encrypted);
210217
}
211218
}
212219
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,19 @@
2323
import com.owncloud.android.lib.common.Creator;
2424

2525
/**
26-
* Actions interface to be implemented by any class that makes use of
27-
* {@link com.owncloud.android.ui.fragment.OCFileListBottomSheetDialog}.
26+
* Actions interface to be implemented by any class that makes use of {@link com.owncloud.android.ui.fragment.OCFileListBottomSheetDialog}.
2827
*/
2928
public interface OCFileListBottomSheetActions {
3029
/**
3130
* creates a folder within the actual folder.
3231
*/
3332
void createFolder();
3433

34+
/**
35+
* creates an encrypted folder within the actual folder
36+
*/
37+
void createEncryptedFolder();
38+
3539
/**
3640
* offers a file upload with the Android OS file picker to the current folder.
3741
*/

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

97-
OCCapability capability = fileActivity.getCapabilities();
98-
if (capability != null &&
99-
capability.getRichDocuments().isTrue() &&
97+
OCCapability capability = fileActivity.getStorageManager().getCapability(user.getAccountName());
98+
if (capability.getRichDocuments().isTrue() &&
10099
capability.getRichDocumentsDirectEditing().isTrue() &&
101100
capability.getRichDocumentsTemplatesAvailable().isTrue() &&
102101
!file.isEncrypted()) {
@@ -144,6 +143,12 @@ protected void onCreate(Bundle savedInstanceState) {
144143
binding.menuDirectCameraUpload.setVisibility(View.GONE);
145144
}
146145

146+
if (capability.getEndToEndEncryption().isTrue()) {
147+
binding.menuEncryptedMkdir.setVisibility(View.VISIBLE);
148+
} else {
149+
binding.menuEncryptedMkdir.setVisibility(View.GONE);
150+
}
151+
147152
// create rich workspace
148153
if (editorUtils.isEditorAvailable(user,
149154
MimeTypeUtil.MIMETYPE_TEXT_MARKDOWN) &&
@@ -178,6 +183,11 @@ private void setupClickListener() {
178183
dismiss();
179184
});
180185

186+
binding.menuEncryptedMkdir.setOnClickListener(v -> {
187+
actions.createEncryptedFolder();
188+
dismiss();
189+
});
190+
181191
binding.menuUploadFromApp.setOnClickListener(v -> {
182192
actions.uploadFromApp();
183193
dismiss();

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

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,15 @@ public void registerFabListener() {
482482
@Override
483483
public void createFolder() {
484484
CreateFolderDialogFragment.newInstance(mFile)
485+
.show(getActivity().getSupportFragmentManager(), DIALOG_CREATE_FOLDER);
486+
}
487+
488+
@Override
489+
public void createEncryptedFolder() {
490+
if (checkEncryptionIsSetup(null)) {
491+
CreateFolderDialogFragment.newInstance(mFile, true)
485492
.show(getActivity().getSupportFragmentManager(), DIALOG_CREATE_FOLDER);
493+
}
486494
}
487495

488496
@Override
@@ -492,8 +500,8 @@ public void uploadFromApp() {
492500
action.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
493501

494502
getActivity().startActivityForResult(
495-
Intent.createChooser(action, getString(R.string.upload_chooser_title)),
496-
FileDisplayActivity.REQUEST_CODE__SELECT_CONTENT_FROM_APPS
503+
Intent.createChooser(action, getString(R.string.upload_chooser_title)),
504+
FileDisplayActivity.REQUEST_CODE__SELECT_CONTENT_FROM_APPS
497505
);
498506
}
499507

@@ -1072,9 +1080,11 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
10721080
int position = data.getIntExtra(SetupEncryptionDialogFragment.ARG_POSITION, -1);
10731081
OCFile file = mAdapter.getItem(position);
10741082

1075-
if (file != null) {
1076-
mContainerActivity.getFileOperationsHelper().toggleEncryption(file, true);
1083+
if (file == null) {
1084+
return;
10771085
}
1086+
1087+
encryptFolder(file.getLocalId(), file.getRemoteId(), file.getRemotePath(), true);
10781088

10791089
// update state and view of this fragment
10801090
searchFragment = false;
@@ -1639,6 +1649,12 @@ protected RemoteOperation getSearchRemoteOperation(final User currentUser, final
16391649

16401650
@Subscribe(threadMode = ThreadMode.BACKGROUND)
16411651
public void onMessageEvent(EncryptionEvent event) {
1652+
if (checkEncryptionIsSetup(event.remoteId)) {
1653+
encryptFolder(event.localId, event.remoteId, event.remotePath, event.shouldBeEncrypted);
1654+
}
1655+
}
1656+
1657+
private boolean checkEncryptionIsSetup(@Nullable String remoteId) {
16421658
final User user = accountManager.getUser();
16431659

16441660
// check if keys are stored
@@ -1649,16 +1665,20 @@ public void onMessageEvent(EncryptionEvent event) {
16491665
Log_OC.d(TAG, "no public key for " + user.getAccountName());
16501666

16511667
FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
1652-
OCFile file = storageManager.getFileByRemoteId(event.remoteId);
16531668
int position = -1;
1654-
if (file != null) {
1655-
position = mAdapter.getItemPosition(file);
1669+
if (remoteId != null) {
1670+
OCFile file = storageManager.getFileByRemoteId(remoteId);
1671+
if (file != null) {
1672+
position = mAdapter.getItemPosition(file);
1673+
}
16561674
}
16571675
SetupEncryptionDialogFragment dialog = SetupEncryptionDialogFragment.newInstance(user, position);
16581676
dialog.setTargetFragment(this, SetupEncryptionDialogFragment.SETUP_ENCRYPTION_REQUEST_CODE);
16591677
dialog.show(getParentFragmentManager(), SetupEncryptionDialogFragment.SETUP_ENCRYPTION_DIALOG_TAG);
1678+
1679+
return false;
16601680
} else {
1661-
encryptFolder(event.localId, event.remoteId, event.remotePath, event.shouldBeEncrypted);
1681+
return true;
16621682
}
16631683
}
16641684

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -965,13 +965,17 @@ public void removeFiles(Collection<OCFile> files, boolean onlyLocalCopy, boolean
965965
}
966966
}
967967

968-
969968
public void createFolder(String remotePath) {
969+
createFolder(remotePath, false);
970+
}
971+
972+
public void createFolder(String remotePath, boolean encrypted) {
970973
// Create Folder
971974
Intent service = new Intent(fileActivity, OperationsService.class);
972975
service.setAction(OperationsService.ACTION_CREATE_FOLDER);
973976
service.putExtra(OperationsService.EXTRA_ACCOUNT, fileActivity.getAccount());
974977
service.putExtra(OperationsService.EXTRA_REMOTE_PATH, remotePath);
978+
service.putExtra(OperationsService.EXTRA_ENCRYPTED, encrypted);
975979
mWaitingForOpId = fileActivity.getOperationsServiceBinder().queueNewOperation(service);
976980

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

app/src/main/res/layout/file_list_actions_bottom_sheet_fragment.xml

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@
178178
android:layout_height="wrap_content"
179179
android:contentDescription="@null"
180180
android:src="@drawable/ic_action_create_dir"
181-
app:tint="@color/primary"/>
181+
app:tint="@color/primary" />
182182

183183
<TextView
184184
android:layout_width="wrap_content"
@@ -188,7 +188,36 @@
188188
android:text="@string/create_new_folder"
189189
android:textColor="@color/text_color"
190190
android:textSize="@dimen/bottom_sheet_text_size" />
191+
</LinearLayout>
192+
193+
<LinearLayout
194+
android:id="@+id/menu_encrypted_mkdir"
195+
android:layout_width="match_parent"
196+
android:layout_height="wrap_content"
197+
android:orientation="horizontal"
198+
android:background="?android:attr/selectableItemBackground"
199+
android:paddingLeft="@dimen/standard_padding"
200+
android:paddingTop="@dimen/standard_half_padding"
201+
android:paddingRight="@dimen/standard_padding"
202+
android:paddingBottom="@dimen/standard_half_padding"
203+
tools:ignore="UseCompoundDrawables">
204+
205+
<ImageView
206+
android:id="@+id/menu_icon_encrypted_mkdir"
207+
android:layout_width="wrap_content"
208+
android:layout_height="wrap_content"
209+
android:contentDescription="@null"
210+
android:src="@drawable/folder_encrypted"
211+
app:tint="@color/primary" />
191212

213+
<TextView
214+
android:layout_width="wrap_content"
215+
android:layout_height="wrap_content"
216+
android:layout_gravity="center_vertical"
217+
android:layout_marginStart="@dimen/standard_margin"
218+
android:text="@string/create_new_encrypted_folder"
219+
android:textColor="@color/text_color"
220+
android:textSize="@dimen/bottom_sheet_text_size" />
192221
</LinearLayout>
193222

194223
<LinearLayout

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,7 @@
742742
<string name="upload_scan_doc_upload">Scan document from camera</string>
743743
<string name="upload_content_from_other_apps">Upload content from other apps</string>
744744
<string name="create_new_folder">Create new folder</string>
745+
<string name="create_new_encrypted_folder">Create new encrypted folder</string>
745746
<string name="uploads_view_upload_status_virus_detected">Virus detected. Upload cannot be completed!</string>
746747
<string name="tags">Tags</string>
747748
<string name="sharee_add_failed">Adding sharee failed</string>

0 commit comments

Comments
 (0)