Skip to content

Commit 3511d90

Browse files
feat: Pass on file info to also open the file for potential editing via Files app within the full screen activity
Resolves #1683 Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
1 parent 1689d07 commit 3511d90

File tree

3 files changed

+68
-13
lines changed

3 files changed

+68
-13
lines changed

app/src/main/java/com/nextcloud/talk/fullscreenfile/FullScreenTextScreen.kt

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,21 @@ import com.nextcloud.talk.components.StandardAppBar
3434
import io.noties.markwon.Markwon
3535

3636
@Composable
37-
fun FullScreenTextScreen(title: String, text: String, isMarkdown: Boolean, onShare: () -> Unit, onSave: () -> Unit) {
38-
val menuItems = listOf(
39-
stringResource(R.string.share) to onShare,
40-
stringResource(R.string.nc_save_message) to onSave
41-
)
37+
fun FullScreenTextScreen(
38+
title: String,
39+
text: String,
40+
isMarkdown: Boolean,
41+
onShare: () -> Unit,
42+
onSave: () -> Unit,
43+
onOpenInFilesApp: (() -> Unit)? = null
44+
) {
45+
val menuItems = buildList {
46+
add(stringResource(R.string.share) to onShare)
47+
add(stringResource(R.string.nc_save_message) to onSave)
48+
if (onOpenInFilesApp != null) {
49+
add(stringResource(R.string.open_in_files_app) to onOpenInFilesApp)
50+
}
51+
}
4252

4353
Scaffold(
4454
topBar = { StandardAppBar(title = title, menuItems = menuItems) },

app/src/main/java/com/nextcloud/talk/fullscreenfile/FullScreenTextViewerActivity.kt

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
*/
99
package com.nextcloud.talk.fullscreenfile
1010

11+
import android.content.ComponentName
1112
import android.content.Intent
1213
import android.os.Bundle
1314
import androidx.activity.compose.setContent
1415
import androidx.appcompat.app.AppCompatActivity
1516
import androidx.compose.material3.MaterialTheme
1617
import androidx.core.content.FileProvider
18+
import androidx.core.net.toUri
1719
import androidx.fragment.app.DialogFragment
1820
import autodagger.AutoInjector
1921
import com.nextcloud.talk.BuildConfig
@@ -22,8 +24,11 @@ import com.nextcloud.talk.application.NextcloudTalkApplication
2224
import com.nextcloud.talk.components.ColoredStatusBar
2325
import com.nextcloud.talk.ui.dialog.SaveToStorageDialogFragment
2426
import com.nextcloud.talk.ui.theme.ViewThemeUtils
27+
import com.nextcloud.talk.utils.AccountUtils.canWeOpenFilesApp
2528
import com.nextcloud.talk.utils.Mimetype.TEXT_PREFIX_GENERIC
2629
import com.nextcloud.talk.utils.adjustUIForAPILevel35
30+
import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_ACCOUNT
31+
import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_FILE_ID
2732
import java.io.File
2833
import javax.inject.Inject
2934

@@ -39,6 +44,10 @@ class FullScreenTextViewerActivity : AppCompatActivity() {
3944

4045
val fileName = intent.getStringExtra("FILE_NAME").orEmpty()
4146
val isMarkdown = intent.getBooleanExtra("IS_MARKDOWN", false)
47+
val fileId = intent.getStringExtra("FILE_ID").orEmpty()
48+
val link = intent.getStringExtra("LINK")
49+
val username = intent.getStringExtra("USERNAME").orEmpty()
50+
val baseUrl = intent.getStringExtra("BASE_URL").orEmpty()
4251
val path = applicationContext.cacheDir.absolutePath + "/" + fileName
4352
val text = readFile(path)
4453

@@ -53,12 +62,36 @@ class FullScreenTextViewerActivity : AppCompatActivity() {
5362
text = text,
5463
isMarkdown = isMarkdown,
5564
onShare = { shareFile(path) },
56-
onSave = { showSaveDialog(fileName) }
65+
onSave = { showSaveDialog(fileName) },
66+
onOpenInFilesApp = if (fileId.isNotEmpty()) {
67+
{ openInFilesApp(link, fileId, username, baseUrl) }
68+
} else {
69+
null
70+
}
5771
)
5872
}
5973
}
6074
}
6175

76+
private fun openInFilesApp(link: String?, fileId: String, username: String, baseUrl: String) {
77+
val accountString = "$username@${baseUrl.replace("https://", "").replace("http://", "")}"
78+
if (canWeOpenFilesApp(this, accountString)) {
79+
val filesAppIntent = Intent(Intent.ACTION_VIEW, null)
80+
val componentName = ComponentName(
81+
getString(R.string.nc_import_accounts_from),
82+
"com.owncloud.android.ui.activity.FileDisplayActivity"
83+
)
84+
filesAppIntent.component = componentName
85+
filesAppIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
86+
filesAppIntent.setPackage(getString(R.string.nc_import_accounts_from))
87+
filesAppIntent.putExtra(KEY_ACCOUNT, accountString)
88+
filesAppIntent.putExtra(KEY_FILE_ID, fileId)
89+
startActivity(filesAppIntent)
90+
} else if (!link.isNullOrEmpty()) {
91+
startActivity(Intent(Intent.ACTION_VIEW, link.toUri()))
92+
}
93+
}
94+
6295
private fun shareFile(path: String) {
6396
val shareUri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID, File(path))
6497
val shareIntent = Intent().apply {

app/src/main/java/com/nextcloud/talk/utils/FileViewerUtils.kt

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ class FileViewerUtils(private val context: Context, private val user: User) {
9797
openOrDownloadFile(
9898
fileInfo,
9999
path,
100+
link,
100101
mimetype,
101102
progressUi,
102103
openWhenDownloaded
@@ -125,25 +126,27 @@ class FileViewerUtils(private val context: Context, private val user: User) {
125126
private fun openOrDownloadFile(
126127
fileInfo: FileInfo,
127128
path: String,
129+
link: String?,
128130
mimetype: String?,
129131
progressUi: ProgressUi,
130132
openWhenDownloaded: Boolean
131133
) {
132134
val file = File(context.cacheDir, fileInfo.fileName)
133135
if (file.exists()) {
134-
openFileByMimetype(fileInfo.fileName, mimetype)
136+
openFileByMimetype(fileInfo.fileName, mimetype, link, fileInfo.fileId)
135137
} else {
136138
downloadFileToCache(
137139
fileInfo,
138140
path,
141+
link,
139142
mimetype,
140143
progressUi,
141144
openWhenDownloaded
142145
)
143146
}
144147
}
145148

146-
private fun openFileByMimetype(filename: String, mimetype: String?) {
149+
private fun openFileByMimetype(filename: String, mimetype: String?, link: String? = null, fileId: String = "") {
147150
if (mimetype != null) {
148151
when (mimetype) {
149152
AUDIO_MPEG,
@@ -161,7 +164,7 @@ class FileViewerUtils(private val context: Context, private val user: User) {
161164
-> openImageView(filename, mimetype)
162165
TEXT_MARKDOWN,
163166
TEXT_PLAIN
164-
-> openTextView(filename, mimetype)
167+
-> openTextView(filename, mimetype, link, fileId)
165168
else
166169
-> openFileByExternalApp(filename, mimetype)
167170
}
@@ -236,10 +239,14 @@ class FileViewerUtils(private val context: Context, private val user: User) {
236239
context.startActivity(fullScreenMediaIntent)
237240
}
238241

239-
private fun openTextView(filename: String, mimetype: String) {
242+
private fun openTextView(filename: String, mimetype: String, link: String?, fileId: String) {
240243
val fullScreenTextViewerIntent = Intent(context, FullScreenTextViewerActivity::class.java)
241244
fullScreenTextViewerIntent.putExtra("FILE_NAME", filename)
242245
fullScreenTextViewerIntent.putExtra("IS_MARKDOWN", isMarkdown(mimetype))
246+
fullScreenTextViewerIntent.putExtra("FILE_ID", fileId)
247+
fullScreenTextViewerIntent.putExtra("LINK", link)
248+
fullScreenTextViewerIntent.putExtra("USERNAME", user.username)
249+
fullScreenTextViewerIntent.putExtra("BASE_URL", user.baseUrl)
243250
context.startActivity(fullScreenTextViewerIntent)
244251
}
245252

@@ -265,6 +272,7 @@ class FileViewerUtils(private val context: Context, private val user: User) {
265272
private fun downloadFileToCache(
266273
fileInfo: FileInfo,
267274
path: String,
275+
link: String?,
268276
mimetype: String?,
269277
progressUi: ProgressUi,
270278
openWhenDownloaded: Boolean
@@ -316,7 +324,9 @@ class FileViewerUtils(private val context: Context, private val user: User) {
316324
mimetype,
317325
workInfo!!,
318326
progressUi,
319-
openWhenDownloaded
327+
openWhenDownloaded,
328+
link,
329+
fileInfo.fileId
320330
)
321331
}
322332
}
@@ -326,7 +336,9 @@ class FileViewerUtils(private val context: Context, private val user: User) {
326336
mimetype: String?,
327337
workInfo: WorkInfo,
328338
progressUi: ProgressUi,
329-
openWhenDownloaded: Boolean
339+
openWhenDownloaded: Boolean,
340+
link: String? = null,
341+
fileId: String = ""
330342
) {
331343
when (workInfo.state) {
332344
WorkInfo.State.RUNNING -> {
@@ -341,7 +353,7 @@ class FileViewerUtils(private val context: Context, private val user: User) {
341353
}
342354
WorkInfo.State.SUCCEEDED -> {
343355
if (progressUi.previewImage.isShown && openWhenDownloaded) {
344-
openFileByMimetype(fileName, mimetype)
356+
openFileByMimetype(fileName, mimetype, link, fileId)
345357
} else {
346358
Log.d(
347359
TAG,

0 commit comments

Comments
 (0)