Reworked file viewer related code
This commit is contained in:
parent
eade07060a
commit
7b23edd393
5 changed files with 73 additions and 119 deletions
|
@ -21,17 +21,14 @@ package org.linphone.activities.main.chat.fragments
|
|||
|
||||
import android.app.Activity
|
||||
import android.app.Dialog
|
||||
import android.content.ActivityNotFoundException
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import android.os.Parcelable
|
||||
import android.provider.MediaStore
|
||||
import android.view.MenuInflater
|
||||
import android.view.MenuItem
|
||||
import android.view.View
|
||||
import android.webkit.MimeTypeMap
|
||||
import androidx.activity.addCallback
|
||||
import androidx.appcompat.view.menu.MenuBuilder
|
||||
import androidx.appcompat.view.menu.MenuPopupHelper
|
||||
|
@ -231,13 +228,18 @@ class DetailChatRoomFragment : MasterFragment<ChatRoomDetailFragmentBinding, Cha
|
|||
} else {
|
||||
Log.i("[Chat Message] Opening file: $path")
|
||||
sharedViewModel.fileToOpen.value = path
|
||||
val preventScreenshots = viewModel.chatRoom.currentParams.encryptionEnabled()
|
||||
when {
|
||||
FileUtils.isExtensionImage(path) -> navigateToImageFileViewer(viewModel.chatRoom.currentParams.encryptionEnabled())
|
||||
FileUtils.isExtensionVideo(path) -> navigateToVideoFileViewer(viewModel.chatRoom.currentParams.encryptionEnabled())
|
||||
FileUtils.isExtensionAudio(path) -> navigateToAudioFileViewer(viewModel.chatRoom.currentParams.encryptionEnabled())
|
||||
FileUtils.isExtensionPdf(path) -> navigateToPdfFileViewer(viewModel.chatRoom.currentParams.encryptionEnabled())
|
||||
FileUtils.isPlainTextFile(path) -> navigateToTextFileViewer(viewModel.chatRoom.currentParams.encryptionEnabled())
|
||||
else -> openFile(path)
|
||||
FileUtils.isExtensionImage(path) -> navigateToImageFileViewer(preventScreenshots)
|
||||
FileUtils.isExtensionVideo(path) -> navigateToVideoFileViewer(preventScreenshots)
|
||||
FileUtils.isExtensionAudio(path) -> navigateToAudioFileViewer(preventScreenshots)
|
||||
FileUtils.isExtensionPdf(path) -> navigateToPdfFileViewer(preventScreenshots)
|
||||
FileUtils.isPlainTextFile(path) -> navigateToTextFileViewer(preventScreenshots)
|
||||
else -> {
|
||||
if (!FileUtils.openFileInThirdPartyApp(requireActivity(), path)) {
|
||||
showDialogToSuggestOpeningFileAsText()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -548,57 +550,22 @@ class DetailChatRoomFragment : MasterFragment<ChatRoomDetailFragmentBinding, Cha
|
|||
startActivityForResult(chooserIntent, 0)
|
||||
}
|
||||
|
||||
private fun openFile(contentFilePath: String) {
|
||||
val intent = Intent(Intent.ACTION_VIEW)
|
||||
val contentUri: Uri = FileUtils.getPublicFilePath(requireContext(), contentFilePath)
|
||||
val filePath: String = contentUri.toString()
|
||||
Log.i("[Chat Message] Trying to open file: $filePath")
|
||||
var type: String? = null
|
||||
val extension = FileUtils.getExtensionFromFileName(filePath)
|
||||
private fun showDialogToSuggestOpeningFileAsText() {
|
||||
val dialogViewModel = DialogViewModel(
|
||||
requireContext().getString(R.string.dialog_try_open_file_as_text_body),
|
||||
requireContext().getString(R.string.dialog_try_open_file_as_text_title)
|
||||
)
|
||||
val dialog = DialogUtils.getDialog(requireContext(), dialogViewModel)
|
||||
|
||||
if (extension.isNotEmpty()) {
|
||||
Log.i("[Chat Message] Found extension $extension")
|
||||
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension)
|
||||
} else {
|
||||
Log.e("[Chat Message] Couldn't find extension")
|
||||
dialogViewModel.showCancelButton {
|
||||
dialog.dismiss()
|
||||
}
|
||||
|
||||
if (type != null) {
|
||||
Log.i("[Chat Message] Found matching MIME type $type")
|
||||
} else {
|
||||
type = "file/$extension"
|
||||
Log.e("[Chat Message] Can't get MIME type from extension: $extension, will use $type")
|
||||
}
|
||||
dialogViewModel.showOkButton({
|
||||
dialog.dismiss()
|
||||
navigateToTextFileViewer(true)
|
||||
})
|
||||
|
||||
intent.setDataAndType(contentUri, type)
|
||||
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||
|
||||
try {
|
||||
startActivity(intent)
|
||||
|
||||
if (corePreferences.enableAnimations) {
|
||||
requireActivity().overridePendingTransition(R.anim.enter_right, R.anim.exit_left)
|
||||
}
|
||||
} catch (anfe: ActivityNotFoundException) {
|
||||
Log.e("[Chat Message] Couldn't find an activity to handle MIME type: $type")
|
||||
|
||||
val dialogViewModel = DialogViewModel(
|
||||
getString(R.string.dialog_try_open_file_as_text_body), getString(
|
||||
R.string.dialog_try_open_file_as_text_title
|
||||
)
|
||||
)
|
||||
val dialog = DialogUtils.getDialog(requireContext(), dialogViewModel)
|
||||
|
||||
dialogViewModel.showCancelButton {
|
||||
dialog.dismiss()
|
||||
}
|
||||
|
||||
dialogViewModel.showOkButton({
|
||||
dialog.dismiss()
|
||||
navigateToTextFileViewer(viewModel.chatRoom.currentParams.encryptionEnabled())
|
||||
})
|
||||
|
||||
dialog.show()
|
||||
}
|
||||
dialog.show()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,19 +19,13 @@
|
|||
*/
|
||||
package org.linphone.activities.main.files.fragments
|
||||
|
||||
import android.content.ActivityNotFoundException
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import android.webkit.MimeTypeMap
|
||||
import androidx.navigation.fragment.findNavController
|
||||
import org.linphone.LinphoneApplication
|
||||
import org.linphone.R
|
||||
import org.linphone.activities.GenericFragment
|
||||
import org.linphone.activities.main.viewmodels.DialogViewModel
|
||||
import org.linphone.activities.SnackBarActivity
|
||||
import org.linphone.core.tools.Log
|
||||
import org.linphone.databinding.FileViewerTopBarFragmentBinding
|
||||
import org.linphone.utils.DialogUtils
|
||||
import org.linphone.utils.FileUtils
|
||||
|
||||
class TopBarFragment : GenericFragment<FileViewerTopBarFragmentBinding>() {
|
||||
|
@ -49,7 +43,9 @@ class TopBarFragment : GenericFragment<FileViewerTopBarFragmentBinding>() {
|
|||
}
|
||||
|
||||
binding.setExportClickListener {
|
||||
openFile(filePath)
|
||||
if (!FileUtils.openFileInThirdPartyApp(requireActivity(), filePath)) {
|
||||
(requireActivity() as SnackBarActivity).showSnackBar(R.string.chat_message_no_app_found_to_handle_file_mime_type)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -67,57 +63,4 @@ class TopBarFragment : GenericFragment<FileViewerTopBarFragmentBinding>() {
|
|||
super.onViewStateRestored(savedInstanceState)
|
||||
filePath = savedInstanceState?.getString("FilePath") ?: filePath
|
||||
}
|
||||
|
||||
private fun openFile(contentFilePath: String) {
|
||||
val intent = Intent(Intent.ACTION_VIEW)
|
||||
val contentUri: Uri = FileUtils.getPublicFilePath(requireContext(), contentFilePath)
|
||||
val filePath: String = contentUri.toString()
|
||||
Log.i("[File Viewer] Trying to open file: $filePath")
|
||||
var type: String? = null
|
||||
val extension = FileUtils.getExtensionFromFileName(filePath)
|
||||
|
||||
if (extension.isNotEmpty()) {
|
||||
Log.i("[File Viewer] Found extension $extension")
|
||||
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension)
|
||||
} else {
|
||||
Log.e("[File Viewer] Couldn't find extension")
|
||||
}
|
||||
|
||||
if (type != null) {
|
||||
Log.i("[File Viewer] Found matching MIME type $type")
|
||||
} else {
|
||||
type = "file/$extension"
|
||||
Log.e("[File Viewer] Can't get MIME type from extension: $extension, will use $type")
|
||||
}
|
||||
|
||||
intent.setDataAndType(contentUri, type)
|
||||
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||
|
||||
try {
|
||||
startActivity(intent)
|
||||
|
||||
if (LinphoneApplication.corePreferences.enableAnimations) {
|
||||
requireActivity().overridePendingTransition(R.anim.enter_right, R.anim.exit_left)
|
||||
}
|
||||
} catch (anfe: ActivityNotFoundException) {
|
||||
Log.e("[File Viewer] Couldn't find an activity to handle MIME type: $type")
|
||||
|
||||
val dialogViewModel = DialogViewModel(
|
||||
getString(R.string.dialog_try_open_file_as_text_body), getString(
|
||||
R.string.dialog_try_open_file_as_text_title
|
||||
)
|
||||
)
|
||||
val dialog = DialogUtils.getDialog(requireContext(), dialogViewModel)
|
||||
|
||||
dialogViewModel.showCancelButton {
|
||||
dialog.dismiss()
|
||||
}
|
||||
|
||||
dialogViewModel.showOkButton({
|
||||
dialog.dismiss()
|
||||
})
|
||||
|
||||
dialog.show()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,7 +19,10 @@
|
|||
*/
|
||||
package org.linphone.utils
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.ActivityNotFoundException
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.database.CursorIndexOutOfBoundsException
|
||||
import android.net.Uri
|
||||
import android.os.Environment
|
||||
|
@ -33,6 +36,7 @@ import kotlinx.coroutines.Dispatchers
|
|||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.linphone.LinphoneApplication
|
||||
import org.linphone.LinphoneApplication.Companion.coreContext
|
||||
import org.linphone.R
|
||||
import org.linphone.core.tools.Log
|
||||
|
@ -292,5 +296,42 @@ class FileUtils {
|
|||
}
|
||||
return contentUri
|
||||
}
|
||||
|
||||
fun openFileInThirdPartyApp(activity: Activity, contentFilePath: String): Boolean {
|
||||
val intent = Intent(Intent.ACTION_VIEW)
|
||||
val contentUri: Uri = getPublicFilePath(activity, contentFilePath)
|
||||
val filePath: String = contentUri.toString()
|
||||
Log.i("[File Viewer] Trying to open file: $filePath")
|
||||
var type: String? = null
|
||||
val extension = getExtensionFromFileName(filePath)
|
||||
|
||||
if (extension.isNotEmpty()) {
|
||||
Log.i("[File Viewer] Found extension $extension")
|
||||
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension)
|
||||
} else {
|
||||
Log.e("[File Viewer] Couldn't find extension")
|
||||
}
|
||||
|
||||
if (type != null) {
|
||||
Log.i("[File Viewer] Found matching MIME type $type")
|
||||
} else {
|
||||
type = "file/$extension"
|
||||
Log.e("[File Viewer] Can't get MIME type from extension: $extension, will use $type")
|
||||
}
|
||||
|
||||
intent.setDataAndType(contentUri, type)
|
||||
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||
|
||||
try {
|
||||
activity.startActivity(intent)
|
||||
if (LinphoneApplication.corePreferences.enableAnimations) {
|
||||
activity.overridePendingTransition(R.anim.enter_right, R.anim.exit_left)
|
||||
}
|
||||
return true
|
||||
} catch (anfe: ActivityNotFoundException) {
|
||||
Log.e("[File Viewer] Can't open file in third party app: $anfe")
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -566,4 +566,6 @@
|
|||
<string name="assistant_privacy_policy">la politique de confidentialité</string>
|
||||
<string name="assistant_read_and_agree_terms">J\'accepte %1$s et %2$s de Belledonne Communications</string>
|
||||
<string name="content_description_open_app">Ouvrir les conversations dans l\'application au lieu de la bulle</string>
|
||||
<string name="content_description_export">Ouvrir le fichier dans une application tierce</string>
|
||||
<string name="chat_message_no_app_found_to_handle_file_mime_type">Aucune application n\'est disponible pour ouvrir ce type de fichier</string>
|
||||
</resources>
|
|
@ -200,6 +200,7 @@
|
|||
<item quantity="other">@string/chat_room_delete_many_dialog</item>
|
||||
</plurals>
|
||||
<string name="chat_message_notification_hidden_content"><Redacted></string>
|
||||
<string name="chat_message_no_app_found_to_handle_file_mime_type">No app available for this kind of file</string>
|
||||
|
||||
<!-- Recordings -->
|
||||
<string name="recordings_empty_list">No recordings</string>
|
||||
|
@ -718,5 +719,5 @@
|
|||
<string name="content_description_take_screenshot">Take a screenshot of received video</string>
|
||||
<string name="content_description_close_bubble">Close notification bubble</string>
|
||||
<string name="content_description_open_app">Open conversation in app instead of bubble</string>
|
||||
<string name="content_description_export">Export file in another app</string>
|
||||
<string name="content_description_export">Open file in third-party app</string>
|
||||
</resources>
|
||||
|
|
Loading…
Reference in a new issue