SendNoteActivity.kt 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package io.github.zadam.triliumsender
  2. import android.os.AsyncTask
  3. import android.os.Bundle
  4. import android.support.v7.app.AppCompatActivity
  5. import android.util.Log
  6. import android.widget.Toast
  7. import kotlinx.android.synthetic.main.activity_send_note.*
  8. import okhttp3.MediaType
  9. import okhttp3.OkHttpClient
  10. import okhttp3.Request
  11. import okhttp3.RequestBody
  12. import org.json.JSONObject
  13. import java.text.SimpleDateFormat
  14. import java.util.*
  15. class SendNoteActivity : AppCompatActivity() {
  16. override fun onCreate(savedInstanceState: Bundle?) {
  17. super.onCreate(savedInstanceState)
  18. setContentView(R.layout.activity_send_note)
  19. val settings = TriliumSettings(this)
  20. if (!settings.isConfigured()) {
  21. Toast.makeText(this, "Trilium Sender is not configured. Can't sent the image.", Toast.LENGTH_LONG).show()
  22. finish()
  23. return
  24. }
  25. sendNoteButton.setOnClickListener { view ->
  26. val sendImageTask = SendNoteTask(noteTitle.text.toString(), noteText.text.toString(), settings.triliumAddress, settings.apiToken)
  27. sendImageTask.execute(null as Void?)
  28. }
  29. }
  30. inner class SendNoteTask internal constructor(private val noteTitle: String,
  31. private val noteText: String,
  32. private val triliumAddress: String,
  33. private val apiToken: String) : AsyncTask<Void, Void, Boolean>() {
  34. val TAG : String = "SendNoteTask"
  35. val JSON = MediaType.parse("application/json; charset=utf-8")
  36. override fun doInBackground(vararg params: Void): Boolean {
  37. val client = OkHttpClient()
  38. val json = JSONObject()
  39. json.put("title", noteTitle)
  40. json.put("content", escape(noteText))
  41. val body = RequestBody.create(JSON, json.toString())
  42. val request = Request.Builder()
  43. .url(triliumAddress + "/api/sender/note")
  44. .addHeader("Authorization", apiToken)
  45. .addHeader("X-Local-Date", now())
  46. .post(body)
  47. .build()
  48. return try {
  49. val response = client.newCall(request).execute()
  50. response.code() == 200
  51. } catch (e: Exception) {
  52. Log.e(TAG, "Sending to Trilium failed", e)
  53. false
  54. }
  55. }
  56. override fun onPostExecute(success: Boolean) {
  57. if (success) {
  58. Toast.makeText(this@SendNoteActivity, "Note sent to Trilium", Toast.LENGTH_LONG).show()
  59. }
  60. else {
  61. Toast.makeText(this@SendNoteActivity, "Sending note to Trilium failed", Toast.LENGTH_LONG).show()
  62. }
  63. finish()
  64. }
  65. private fun now(): String {
  66. val dateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
  67. val date = dateFormat.format(Calendar.getInstance().getTime())
  68. return date!!
  69. }
  70. private fun escape(s: String): String {
  71. val builder = StringBuilder()
  72. var previousWasASpace = false
  73. for (c in s.toCharArray()) {
  74. if (c == ' ') {
  75. if (previousWasASpace) {
  76. builder.append("&nbsp;")
  77. previousWasASpace = false
  78. continue
  79. }
  80. previousWasASpace = true
  81. } else {
  82. previousWasASpace = false
  83. }
  84. when (c) {
  85. '<' -> builder.append("&lt;")
  86. '>' -> builder.append("&gt;")
  87. '&' -> builder.append("&amp;")
  88. '"' -> builder.append("&quot;")
  89. '\n' -> builder.append("<p>")
  90. // We need Tab support here, because we print StackTraces as HTML
  91. '\t' -> builder.append("&nbsp; &nbsp; &nbsp;")
  92. else -> if (c.toInt() < 128) {
  93. builder.append(c)
  94. } else {
  95. builder.append("&#").append(c.toInt()).append(";")
  96. }
  97. }
  98. }
  99. return builder.toString()
  100. }
  101. override fun onCancelled() {
  102. }
  103. }
  104. }