FullScreenMediaActivity.kt 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Nextcloud Talk application
  3. *
  4. * @author Marcel Hibbe
  5. * @author Andy Scherzinger
  6. * Copyright (C) 2021 Andy Scherzinger (infoi@andy-scherzinger.de)
  7. * Copyright (C) 2021 Marcel Hibbe <dev@mhibbe.de>
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. package com.nextcloud.talk.activities
  23. import android.content.Intent
  24. import android.os.Bundle
  25. import android.view.Menu
  26. import android.view.MenuItem
  27. import android.view.View
  28. import android.view.WindowManager
  29. import androidx.appcompat.app.AppCompatActivity
  30. import androidx.core.content.FileProvider
  31. import autodagger.AutoInjector
  32. import com.google.android.exoplayer2.MediaItem
  33. import com.google.android.exoplayer2.Player
  34. import com.google.android.exoplayer2.SimpleExoPlayer
  35. import com.nextcloud.talk.BuildConfig
  36. import com.nextcloud.talk.R
  37. import com.nextcloud.talk.application.NextcloudTalkApplication
  38. import com.nextcloud.talk.databinding.ActivityFullScreenMediaBinding
  39. import java.io.File
  40. @AutoInjector(NextcloudTalkApplication::class)
  41. class FullScreenMediaActivity : AppCompatActivity(), Player.EventListener {
  42. lateinit var binding: ActivityFullScreenMediaBinding
  43. private lateinit var path: String
  44. private lateinit var player: SimpleExoPlayer
  45. override fun onCreateOptionsMenu(menu: Menu?): Boolean {
  46. menuInflater.inflate(R.menu.menu_preview, menu)
  47. return true
  48. }
  49. override fun onOptionsItemSelected(item: MenuItem): Boolean {
  50. return if (item.itemId == R.id.share) {
  51. val shareUri = FileProvider.getUriForFile(
  52. this,
  53. BuildConfig.APPLICATION_ID,
  54. File(path)
  55. )
  56. val shareIntent: Intent = Intent().apply {
  57. action = Intent.ACTION_SEND
  58. putExtra(Intent.EXTRA_STREAM, shareUri)
  59. type = "video/*"
  60. addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
  61. }
  62. startActivity(Intent.createChooser(shareIntent, resources.getText(R.string.send_to)))
  63. true
  64. } else {
  65. super.onOptionsItemSelected(item)
  66. }
  67. }
  68. override fun onCreate(savedInstanceState: Bundle?) {
  69. super.onCreate(savedInstanceState)
  70. val fileName = intent.getStringExtra("FILE_NAME")
  71. val isAudioOnly = intent.getBooleanExtra("AUDIO_ONLY", false)
  72. path = applicationContext.cacheDir.absolutePath + "/" + fileName
  73. binding = ActivityFullScreenMediaBinding.inflate(layoutInflater)
  74. setContentView(binding.root)
  75. setSupportActionBar(binding.mediaviewToolbar)
  76. supportActionBar?.setDisplayShowTitleEnabled(false)
  77. window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
  78. binding.playerView.showController()
  79. if (isAudioOnly) {
  80. binding.playerView.controllerShowTimeoutMs = 0
  81. }
  82. binding.playerView.setControllerVisibilityListener { v ->
  83. if (v != 0) {
  84. hideSystemUI()
  85. supportActionBar?.hide()
  86. } else {
  87. showSystemUI()
  88. supportActionBar?.show()
  89. }
  90. }
  91. }
  92. override fun onStart() {
  93. super.onStart()
  94. initializePlayer()
  95. val mediaItem: MediaItem = MediaItem.fromUri(path)
  96. player.setMediaItem(mediaItem)
  97. player.prepare()
  98. player.play()
  99. }
  100. override fun onStop() {
  101. super.onStop()
  102. releasePlayer()
  103. }
  104. private fun initializePlayer() {
  105. player = SimpleExoPlayer.Builder(applicationContext).build()
  106. binding.playerView.player = player
  107. player.playWhenReady = true
  108. player.addListener(this)
  109. }
  110. private fun releasePlayer() {
  111. player.release()
  112. }
  113. private fun hideSystemUI() {
  114. window.decorView.systemUiVisibility = (
  115. View.SYSTEM_UI_FLAG_IMMERSIVE
  116. or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
  117. or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
  118. or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
  119. or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
  120. or View.SYSTEM_UI_FLAG_FULLSCREEN
  121. )
  122. }
  123. private fun showSystemUI() {
  124. window.decorView.systemUiVisibility = (
  125. View.SYSTEM_UI_FLAG_LAYOUT_STABLE
  126. or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
  127. or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
  128. )
  129. }
  130. }