Skip to content

Commit 23613e3

Browse files
committed
wip
Signed-off-by: alperozturk96 <alper_ozturk@proton.me> # Conflicts: # app/src/main/java/com/owncloud/android/ui/activity/TextEditorWebView.kt
1 parent 2bb7e5f commit 23613e3

8 files changed

Lines changed: 94 additions & 5 deletions

File tree

app/src/androidTest/java/com/owncloud/android/files/FileMenuFilterIT.kt

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,39 @@ class FileMenuFilterIT : AbstractIT() {
330330
}
331331
}
332332

333+
@Test
334+
fun filter_openWithOnlyOffice_onlyForDownloadedFiles() {
335+
every {
336+
mockArbitraryDataProvider.getValue(any<User>(), ArbitraryDataProvider.DIRECT_EDITING)
337+
} returns ONLYOFFICE_DIRECT_EDITING_JSON
338+
339+
configureCapability(OCCapability())
340+
341+
val downloadedFile = OCFile("/downloaded.docx").apply {
342+
mimeType = OFFICE_MIMETYPE
343+
storagePath = getDummyFile("downloaded.docx").absolutePath
344+
}
345+
val onlineOnlyFile = OCFile("/online.docx").apply {
346+
mimeType = OFFICE_MIMETYPE
347+
}
348+
349+
launchActivity<TestActivity>().use {
350+
it.onActivity { activity ->
351+
val filterFactory = FileMenuFilter.Factory(mockStorageManager, activity, editorUtils)
352+
353+
val downloadedToHide = filterFactory
354+
.newInstance(downloadedFile, mockComponentsGetter, true, user)
355+
.getToHide(false)
356+
assertFalse(downloadedToHide.contains(R.id.action_open_with_office))
357+
358+
val onlineToHide = filterFactory
359+
.newInstance(onlineOnlyFile, mockComponentsGetter, true, user)
360+
.getToHide(false)
361+
assertTrue(onlineToHide.contains(R.id.action_open_with_office))
362+
}
363+
}
364+
}
365+
333366
private data class ExpectedLockVisibilities(val lockFile: Boolean, val unlockFile: Boolean)
334367

335368
private fun configureCapability(capability: OCCapability) {
@@ -363,4 +396,25 @@ class FileMenuFilterIT : AbstractIT() {
363396
}
364397
}
365398
}
399+
400+
companion object {
401+
private const val OFFICE_MIMETYPE =
402+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
403+
404+
private val ONLYOFFICE_DIRECT_EDITING_JSON =
405+
"""
406+
{
407+
"editors": {
408+
"onlyoffice": {
409+
"id": "onlyoffice",
410+
"name": "OnlyOffice",
411+
"mimetypes": [],
412+
"optionalMimetypes": ["$OFFICE_MIMETYPE"],
413+
"secure": false
414+
}
415+
},
416+
"creators": {}
417+
}
418+
""".trimIndent()
419+
}
366420
}

app/src/main/java/com/nextcloud/ui/fileactions/FileAction.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ enum class FileAction(
4848
SEND_SHARE_FILE(R.id.action_send_share_file, R.string.action_send_share, R.drawable.ic_share),
4949
SEND_FILE(R.id.action_send_file, R.string.common_send, R.drawable.ic_share),
5050
OPEN_FILE_WITH(R.id.action_open_file_with, R.string.actionbar_open_with, R.drawable.ic_external),
51+
OPEN_WITH_OFFICE(R.id.action_open_with_office, R.string.action_open_with_office, R.drawable.file_doc),
5152
STREAM_MEDIA(R.id.action_stream_media, R.string.stream, R.drawable.ic_play_arrow),
5253
SET_AS_WALLPAPER(R.id.action_set_as_wallpaper, R.string.set_picture_as, R.drawable.ic_wallpaper),
5354

@@ -87,6 +88,7 @@ enum class FileAction(
8788
SEND_SHARE_FILE,
8889
SEND_FILE,
8990
OPEN_FILE_WITH,
91+
OPEN_WITH_OFFICE,
9092
SYNC_FOLDER
9193
)
9294

app/src/main/java/com/nextcloud/utils/EditorUtils.kt

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,34 @@ import javax.inject.Inject
1818
class EditorUtils @Inject constructor(private val arbitraryDataProvider: ArbitraryDataProvider) {
1919

2020
/**
21-
* Returns only supported mimetypes
21+
* Returns an editor matching only the strictly supported mimetypes.
2222
*/
2323
fun getEditor(user: User?, mimeType: String?): Editor? {
2424
val editors = getEditors(user) ?: return null
2525
return editors.firstOrNull { mimeType in it.mimetypes }
2626
}
2727

28+
/**
29+
* Returns an editor matching the supported mimetypes or, as a fallback, the optional ones.
30+
*/
31+
fun getAvailableEditor(user: User?, mimeType: String?): Editor? {
32+
val editors = getEditors(user) ?: return null
33+
return editors.firstOrNull { mimeType in it.mimetypes }
34+
?: editors.firstOrNull { mimeType in it.optionalMimetypes }
35+
}
36+
2837
/**
2938
* Returns supported mimetypes along with optional ones
3039
*/
31-
fun isEditorAvailable(user: User?, mimeType: String?): Boolean {
40+
fun isEditorAvailable(user: User?, mimeType: String?): Boolean = getAvailableEditor(user, mimeType) != null
41+
42+
/**
43+
* Returns true when an office editor (e.g. OnlyOffice) can open the mimetype,
44+
* either as a supported or optional mimetype.
45+
*/
46+
fun isOfficeEditorAvailable(user: User?, mimeType: String?): Boolean {
3247
val editors = getEditors(user) ?: return false
33-
return editors.any { mimeType in it.mimetypes || mimeType in it.optionalMimetypes }
48+
return editors.any { usesOfficeUserAgent(it) && (mimeType in it.mimetypes || mimeType in it.optionalMimetypes) }
3449
}
3550

3651
private fun getEditors(user: User?): Collection<Editor>? {

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ private List<Integer> filter(boolean inSingleFileFragment) {
158158
filterSelectAll(toHide, inSingleFileFragment);
159159
filterDeselectAll(toHide, inSingleFileFragment);
160160
filterOpenWith(toHide, synchronizing);
161+
filterOpenWithOffice(toHide);
161162
filterCancelSync(toHide, synchronizing);
162163
filterSync(toHide, synchronizing);
163164
filterShareFile(toHide, capability);
@@ -339,6 +340,19 @@ private void filterOpenWith(Collection<Integer> toHide, boolean synchronizing) {
339340
}
340341
}
341342

343+
private void filterOpenWithOffice(Collection<Integer> toHide) {
344+
if (!isSingleFile()) {
345+
toHide.add(R.id.action_open_with_office);
346+
return;
347+
}
348+
349+
OCFile file = files.iterator().next();
350+
boolean canOpenLocally = file.isDown() && !file.isEncrypted();
351+
if (!canOpenLocally || !editorUtils.isOfficeEditorAvailable(user, file.getMimeType())) {
352+
toHide.add(R.id.action_open_with_office);
353+
}
354+
}
355+
342356
private void filterDeselectAll(List<Integer> toHide, boolean inSingleFileFragment) {
343357
if (inSingleFileFragment) {
344358
// Always hide in single file fragments

app/src/main/java/com/owncloud/android/ui/asynctasks/TextEditorLoadUrlTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ protected String doInBackground(Void... voids) {
4141
return "";
4242
}
4343

44-
Editor editor = editorUtils.getEditor(user, file.getMimeType());
44+
Editor editor = editorUtils.getAvailableEditor(user, file.getMimeType());
4545

4646
if (editor == null) {
4747
return "";

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@
148148
import androidx.fragment.app.FragmentManager;
149149
import androidx.media3.common.util.UnstableApi;
150150
import kotlin.Unit;
151-
import kotlin.jvm.functions.Function1;
152151

153152
import static com.owncloud.android.datamodel.OCFile.ROOT_PATH;
154153
import static com.owncloud.android.ui.dialog.setupEncryption.SetupEncryptionDialogFragment.SETUP_ENCRYPTION_DIALOG_TAG;
@@ -1355,6 +1354,9 @@ public boolean onFileActionChosen(@IdRes final int itemId, Set<OCFile> checkedFi
13551354
} else if (itemId == R.id.action_open_file_with) {
13561355
mContainerActivity.getFileOperationsHelper().openFile(singleFile);
13571356
return true;
1357+
} else if (itemId == R.id.action_open_with_office) {
1358+
mContainerActivity.getFileOperationsHelper().openFileWithTextEditor(singleFile, getContext());
1359+
return true;
13581360
} else if (itemId == R.id.action_stream_media) {
13591361
mContainerActivity.getFileOperationsHelper().streamMediaFile(singleFile);
13601362
return true;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<item name="action_send_share_file" type="id"/>
2424
<item name="action_send_file" type="id"/>
2525
<item name="action_open_file_with" type="id"/>
26+
<item name="action_open_with_office" type="id"/>
2627
<item name="action_sync_file" type="id"/>
2728
<item name="action_sync_all_files" type="id"/>
2829
<item name="action_cancel_sync" type="id"/>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,6 +1194,7 @@
11941194
<string name="share_internal_link_to_file_text">Internal share link only works for users with access to this file</string>
11951195
<string name="share_internal_link">Share internal link</string>
11961196
<string name="action_edit">Edit</string>
1197+
<string name="action_open_with_office">Open with office</string>
11971198
<string name="failed_to_start_editor">Failed to start editor</string>
11981199
<string name="create_rich_workspace">Add folder description</string>
11991200
<string name="uploader_file_not_found_on_server_message">We couldnt locate the file on server. Another user may have deleted the file</string>

0 commit comments

Comments
 (0)