Skip to content

Commit f8eb49d

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

File tree

10 files changed

+134
-23
lines changed

10 files changed

+134
-23
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
@@ -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

@@ -283,7 +298,7 @@ private String createRandomFileName(DecryptedFolderMetadata metadata) {
283298
return encryptedFileName;
284299
}
285300

286-
private RemoteOperationResult normalCreate(OwnCloudClient client) {
301+
private RemoteOperationResult normalCreate(OwnCloudClient client, boolean encrypted) {
287302
RemoteOperationResult result = new CreateFolderRemoteOperation(remotePath, true).execute(client);
288303

289304
if (result.isSuccess()) {
@@ -292,6 +307,20 @@ private RemoteOperationResult normalCreate(OwnCloudClient client) {
292307

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

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: 12 additions & 5 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();
@@ -204,9 +211,9 @@ public void onClick(DialogInterface dialog, int which) {
204211
return;
205212
}
206213

207-
String path = mParentFolder.getDecryptedRemotePath() + newFolderName + OCFile.PATH_SEPARATOR;
214+
String path = parentFolder.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
@@ -95,9 +95,8 @@ protected void onCreate(Bundle savedInstanceState) {
9595
binding.addToCloud.setText(getContext().getResources().getString(R.string.add_to_cloud,
9696
themeUtils.getDefaultDisplayNameForRootFolder(getContext())));
9797

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

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

187+
binding.menuEncryptedMkdir.setOnClickListener(v -> {
188+
actions.createEncryptedFolder();
189+
dismiss();
190+
});
191+
182192
binding.menuUploadFromApp.setOnClickListener(v -> {
183193
actions.uploadFromApp();
184194
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
@@ -966,13 +966,17 @@ public void removeFiles(Collection<OCFile> files, boolean onlyLocalCopy, boolean
966966
}
967967
}
968968

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

978982
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)