FullScreenTextViewerActivity.kt 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.appcompat.app.AppCompatActivity
  28. import androidx.core.content.FileProvider
  29. import androidx.core.content.res.ResourcesCompat
  30. import autodagger.AutoInjector
  31. import com.nextcloud.talk.BuildConfig
  32. import com.nextcloud.talk.R
  33. import com.nextcloud.talk.application.NextcloudTalkApplication
  34. import com.nextcloud.talk.databinding.ActivityFullScreenTextBinding
  35. import com.nextcloud.talk.utils.DisplayUtils
  36. import io.noties.markwon.Markwon
  37. import java.io.File
  38. @AutoInjector(NextcloudTalkApplication::class)
  39. class FullScreenTextViewerActivity : AppCompatActivity() {
  40. lateinit var binding: ActivityFullScreenTextBinding
  41. private lateinit var path: String
  42. override fun onCreateOptionsMenu(menu: Menu?): Boolean {
  43. menuInflater.inflate(R.menu.menu_preview, menu)
  44. return true
  45. }
  46. override fun onOptionsItemSelected(item: MenuItem): Boolean {
  47. return if (item.itemId == android.R.id.home) {
  48. onBackPressed()
  49. true
  50. } else 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 = "text/*"
  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. binding = ActivityFullScreenTextBinding.inflate(layoutInflater)
  71. setContentView(binding.root)
  72. setSupportActionBar(binding.textviewToolbar)
  73. val fileName = intent.getStringExtra("FILE_NAME")
  74. val isMarkdown = intent.getBooleanExtra("IS_MARKDOWN", false)
  75. path = applicationContext.cacheDir.absolutePath + "/" + fileName
  76. val text = readFile(path)
  77. if (isMarkdown) {
  78. val markwon = Markwon.create(applicationContext)
  79. markwon.setMarkdown(binding.textView, text)
  80. } else {
  81. binding.textView.text = text
  82. }
  83. supportActionBar?.title = fileName
  84. supportActionBar?.setDisplayHomeAsUpEnabled(true)
  85. if (resources != null) {
  86. DisplayUtils.applyColorToStatusBar(
  87. this,
  88. ResourcesCompat.getColor(resources, R.color.appbar, null)
  89. )
  90. DisplayUtils.applyColorToNavigationBar(
  91. this.window,
  92. ResourcesCompat.getColor(resources, R.color.bg_default, null)
  93. )
  94. }
  95. }
  96. private fun readFile(fileName: String) = File(fileName).inputStream().readBytes().toString(Charsets.UTF_8)
  97. }