FullScreenTextViewerActivity.kt 4.2 KB

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