Added export button in each file viewer

This commit is contained in:
Sylvain Berfini 2021-03-31 17:29:45 +02:00
parent b818bf0155
commit d4229711e3
8 changed files with 123 additions and 2 deletions

View file

@ -55,6 +55,9 @@ class ImageViewerFragment : SecureFragment<ImageViewerFragmentBinding>() {
val filePath = sharedViewModel.fileToOpen.value
filePath ?: return
(childFragmentManager.findFragmentById(R.id.top_bar_fragment) as? TopBarFragment)
?.setFilePath(filePath)
viewModel = ViewModelProvider(
this,
ImageFileViewModelFactory(filePath)

View file

@ -46,6 +46,9 @@ class PdfViewerFragment : SecureFragment<PdfViewerFragmentBinding>() {
val filePath = sharedViewModel.fileToOpen.value
filePath ?: return
(childFragmentManager.findFragmentById(R.id.top_bar_fragment) as? TopBarFragment)
?.setFilePath(filePath)
viewModel = ViewModelProvider(
this,
PdfFileViewModelFactory(filePath)

View file

@ -46,6 +46,9 @@ class TextViewerFragment : SecureFragment<TextViewerFragmentBinding>() {
val filePath = sharedViewModel.fileToOpen.value
filePath ?: return
(childFragmentManager.findFragmentById(R.id.top_bar_fragment) as? TopBarFragment)
?.setFilePath(filePath)
viewModel = ViewModelProvider(
this,
TextFileViewModelFactory(filePath)

View file

@ -19,13 +19,24 @@
*/
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.core.tools.Log
import org.linphone.databinding.FileViewerTopBarFragmentBinding
import org.linphone.utils.DialogUtils
import org.linphone.utils.FileUtils
class TopBarFragment : GenericFragment<FileViewerTopBarFragmentBinding>() {
private var filePath: String = ""
override fun getLayoutId(): Int = R.layout.file_viewer_top_bar_fragment
override fun onActivityCreated(savedInstanceState: Bundle?) {
@ -36,5 +47,77 @@ class TopBarFragment : GenericFragment<FileViewerTopBarFragmentBinding>() {
binding.setBackClickListener {
findNavController().popBackStack()
}
binding.setExportClickListener {
openFile(filePath)
}
}
fun setFilePath(newFilePath: String) {
Log.i("[File Viewer] File path is: $newFilePath")
filePath = newFilePath
}
override fun onSaveInstanceState(outState: Bundle) {
outState.putString("FilePath", filePath)
super.onSaveInstanceState(outState)
}
override fun onViewStateRestored(savedInstanceState: Bundle?) {
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()
}
}
}

View file

@ -47,6 +47,9 @@ class VideoViewerFragment : SecureFragment<VideoViewerFragmentBinding>() {
val filePath = sharedViewModel.fileToOpen.value
filePath ?: return
(childFragmentManager.findFragmentById(R.id.top_bar_fragment) as? TopBarFragment)
?.setFilePath(filePath)
viewModel = ViewModelProvider(
this,
VideoFileViewModelFactory(filePath)
@ -58,6 +61,18 @@ class VideoViewerFragment : SecureFragment<VideoViewerFragmentBinding>() {
isSecure = arguments?.getBoolean("Secure") ?: false
}
override fun onResume() {
super.onResume()
binding.videoView.start()
}
override fun onPause() {
binding.videoView.pause()
super.onPause()
}
private fun initVideoControls() {
val mediaController = MediaController(requireContext())
viewModel.initMediaController(mediaController, binding.videoView)

View file

@ -55,6 +55,5 @@ class VideoFileViewModel(val filePath: String) : ViewModel() {
}
videoView.setVideoPath(filePath)
videoView.start()
}
}

View file

@ -6,6 +6,9 @@
<variable
name="backClickListener"
type="android.view.View.OnClickListener"/>
<variable
name="exportClickListener"
type="android.view.View.OnClickListener"/>
</data>
<LinearLayout
@ -30,9 +33,20 @@
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.8"
android:layout_weight="0.6"
android:visibility="invisible" />
<ImageView
android:id="@+id/export"
android:onClick="@{exportClickListener}"
android:contentDescription="@string/content_description_export"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:background="?attr/button_background_drawable"
android:padding="18dp"
android:src="@drawable/settings_network" />
</LinearLayout>
</layout>

View file

@ -718,4 +718,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>
</resources>