|
@@ -0,0 +1,127 @@
|
|
|
+package io.github.zadam.triliumsender
|
|
|
+
|
|
|
+import android.content.Context
|
|
|
+import android.os.AsyncTask
|
|
|
+import android.os.Bundle
|
|
|
+import android.support.v7.app.AppCompatActivity
|
|
|
+import android.util.Log
|
|
|
+import android.widget.Toast
|
|
|
+import kotlinx.android.synthetic.main.activity_send_note.*
|
|
|
+import okhttp3.MediaType
|
|
|
+import okhttp3.OkHttpClient
|
|
|
+import okhttp3.Request
|
|
|
+import okhttp3.RequestBody
|
|
|
+import org.json.JSONObject
|
|
|
+import java.text.SimpleDateFormat
|
|
|
+import java.util.*
|
|
|
+
|
|
|
+class SendNoteActivity : AppCompatActivity() {
|
|
|
+
|
|
|
+ override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
+ super.onCreate(savedInstanceState)
|
|
|
+ setContentView(R.layout.activity_send_note)
|
|
|
+
|
|
|
+ val prefs = this.getSharedPreferences(MainActivity.PREFRENCES_NAME, Context.MODE_PRIVATE);
|
|
|
+
|
|
|
+ val triliumAddress = prefs.getString(MainActivity.PREF_TRILIUM_ADDRESS, "");
|
|
|
+ val token = prefs.getString(MainActivity.PREF_TOKEN, "");
|
|
|
+
|
|
|
+ if (triliumAddress.isBlank() || token.isBlank()) {
|
|
|
+ Toast.makeText(this, "Trilium Sender is not configured. Can't sent the image.", Toast.LENGTH_LONG).show()
|
|
|
+ finish()
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ sendNoteButton.setOnClickListener { view ->
|
|
|
+ val sendImageTask = SendNoteTask(noteTitle.text.toString(), noteText.text.toString(), triliumAddress, token)
|
|
|
+ sendImageTask.execute(null as Void?)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ inner class SendNoteTask internal constructor(private val noteTitle: String, private val noteText: String,
|
|
|
+ private val triliumAddress: String, private val token: String) : AsyncTask<Void, Void, Boolean>() {
|
|
|
+
|
|
|
+ val TAG : String = "SendNoteTask"
|
|
|
+ val JSON = MediaType.parse("application/json; charset=utf-8")
|
|
|
+
|
|
|
+ override fun doInBackground(vararg params: Void): Boolean {
|
|
|
+ val client = OkHttpClient()
|
|
|
+
|
|
|
+ val json = JSONObject()
|
|
|
+ json.put("title", noteTitle)
|
|
|
+ json.put("content", escape(noteText))
|
|
|
+
|
|
|
+ val body = RequestBody.create(JSON, json.toString())
|
|
|
+
|
|
|
+ val request = Request.Builder()
|
|
|
+ .url(triliumAddress + "/api/sender/note")
|
|
|
+ .addHeader("Authorization", token)
|
|
|
+ .addHeader("X-Local-Date", now())
|
|
|
+ .post(body)
|
|
|
+ .build()
|
|
|
+
|
|
|
+ return try {
|
|
|
+ val response = client.newCall(request).execute()
|
|
|
+
|
|
|
+ response.code() == 200
|
|
|
+ } catch (e: Exception) {
|
|
|
+ Log.e(TAG, "Sending to Trilium failed", e)
|
|
|
+
|
|
|
+ false
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onPostExecute(success: Boolean) {
|
|
|
+ if (success) {
|
|
|
+ Toast.makeText(this@SendNoteActivity, "Note sent to Trilium", Toast.LENGTH_LONG).show()
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ Toast.makeText(this@SendNoteActivity, "Sending note to Trilium failed", Toast.LENGTH_LONG).show()
|
|
|
+ }
|
|
|
+
|
|
|
+ finish()
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun now(): String {
|
|
|
+ val dateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
|
|
|
+ val date = dateFormat.format(Calendar.getInstance().getTime())
|
|
|
+
|
|
|
+ return date!!
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun escape(s: String): String {
|
|
|
+ val builder = StringBuilder()
|
|
|
+ var previousWasASpace = false
|
|
|
+ for (c in s.toCharArray()) {
|
|
|
+ if (c == ' ') {
|
|
|
+ if (previousWasASpace) {
|
|
|
+ builder.append(" ")
|
|
|
+ previousWasASpace = false
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ previousWasASpace = true
|
|
|
+ } else {
|
|
|
+ previousWasASpace = false
|
|
|
+ }
|
|
|
+ when (c) {
|
|
|
+ '<' -> builder.append("<")
|
|
|
+ '>' -> builder.append(">")
|
|
|
+ '&' -> builder.append("&")
|
|
|
+ '"' -> builder.append(""")
|
|
|
+ '\n' -> builder.append("<p>")
|
|
|
+ // We need Tab support here, because we print StackTraces as HTML
|
|
|
+ '\t' -> builder.append(" ")
|
|
|
+ else -> if (c.toInt() < 128) {
|
|
|
+ builder.append(c)
|
|
|
+ } else {
|
|
|
+ builder.append("&#").append(c.toInt()).append(";")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return builder.toString()
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onCancelled() {
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|