FullScreenTextViewerActivity.kt 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Nextcloud Talk application
  3. *
  4. * @author Marcel Hibbe
  5. * @author Andy Scherzinger
  6. * Copyright (C) 2021 Andy Scherzinger <info@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 androidx.activity.OnBackPressedCallback
  28. import androidx.appcompat.app.AppCompatActivity
  29. import androidx.core.content.FileProvider
  30. import androidx.core.content.res.ResourcesCompat
  31. import autodagger.AutoInjector
  32. import com.nextcloud.talk.BuildConfig
  33. import com.nextcloud.talk.R
  34. import com.nextcloud.talk.application.NextcloudTalkApplication
  35. import com.nextcloud.talk.databinding.ActivityFullScreenTextBinding
  36. import com.nextcloud.talk.ui.theme.ViewThemeUtils
  37. import com.nextcloud.talk.utils.DisplayUtils
  38. import com.nextcloud.talk.utils.Mimetype.TEXT_PREFIX_GENERIC
  39. import io.noties.markwon.Markwon
  40. import java.io.File
  41. import javax.inject.Inject
  42. @AutoInjector(NextcloudTalkApplication::class)
  43. class FullScreenTextViewerActivity : AppCompatActivity() {
  44. lateinit var binding: ActivityFullScreenTextBinding
  45. private val callback = object : OnBackPressedCallback(true) {
  46. override fun handleOnBackPressed() {
  47. finish()
  48. }
  49. }
  50. @Inject
  51. lateinit var viewThemeUtils: ViewThemeUtils
  52. private lateinit var path: String
  53. override fun onCreateOptionsMenu(menu: Menu): Boolean {
  54. menuInflater.inflate(R.menu.menu_preview, menu)
  55. return true
  56. }
  57. override fun onOptionsItemSelected(item: MenuItem): Boolean {
  58. return if (item.itemId == android.R.id.home) {
  59. // onBackPressed()
  60. onBackPressedDispatcher.addCallback(this, callback)
  61. true
  62. } else if (item.itemId == R.id.share) {
  63. val shareUri = FileProvider.getUriForFile(
  64. this,
  65. BuildConfig.APPLICATION_ID,
  66. File(path)
  67. )
  68. val shareIntent: Intent = Intent().apply {
  69. action = Intent.ACTION_SEND
  70. putExtra(Intent.EXTRA_STREAM, shareUri)
  71. type = TEXT_PREFIX_GENERIC
  72. addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
  73. }
  74. startActivity(Intent.createChooser(shareIntent, resources.getText(R.string.send_to)))
  75. true
  76. } else {
  77. super.onOptionsItemSelected(item)
  78. }
  79. }
  80. override fun onCreate(savedInstanceState: Bundle?) {
  81. super.onCreate(savedInstanceState)
  82. NextcloudTalkApplication.sharedApplication!!.componentApplication.inject(this)
  83. binding = ActivityFullScreenTextBinding.inflate(layoutInflater)
  84. setContentView(binding.root)
  85. setSupportActionBar(binding.textviewToolbar)
  86. val fileName = intent.getStringExtra("FILE_NAME")
  87. val isMarkdown = intent.getBooleanExtra("IS_MARKDOWN", false)
  88. path = applicationContext.cacheDir.absolutePath + "/" + fileName
  89. val text = readFile(path)
  90. if (isMarkdown) {
  91. val markwon = Markwon.create(applicationContext)
  92. markwon.setMarkdown(binding.textView, text)
  93. } else {
  94. binding.textView.text = text
  95. }
  96. supportActionBar?.title = fileName
  97. supportActionBar?.setDisplayHomeAsUpEnabled(true)
  98. viewThemeUtils.platform.themeStatusBar(this)
  99. viewThemeUtils.material.themeToolbar(binding.textviewToolbar)
  100. viewThemeUtils.material.colorToolbarOverflowIcon(binding.textviewToolbar)
  101. if (resources != null) {
  102. DisplayUtils.applyColorToNavigationBar(
  103. this.window,
  104. ResourcesCompat.getColor(resources, R.color.bg_default, null)
  105. )
  106. }
  107. }
  108. private fun readFile(fileName: String) = File(fileName).inputStream().readBytes().toString(Charsets.UTF_8)
  109. }