Browse Source

add ability to send new notes

azivner 6 years ago
parent
commit
46965fec34

+ 15 - 4
app/src/main/AndroidManifest.xml

@@ -13,13 +13,12 @@
         android:theme="@style/AppTheme">
         <activity
             android:name=".LoginActivity"
-            android:label="Trilium connection setup"></activity>
-
+            android:label="Trilium connection setup" />
         <activity
             android:name=".MainActivity"
+            android:taskAffinity=".MainActivity"
             android:label="@string/app_name"
             android:theme="@style/AppTheme.NoActionBar">
-
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
 
@@ -29,13 +28,25 @@
         <activity
             android:name=".ShareActivity"
             android:theme="@style/Theme.AppCompat.Translucent">
-
             <intent-filter>
                 <action android:name="android.intent.action.SEND" />
+
                 <category android:name="android.intent.category.DEFAULT" />
+
                 <data android:mimeType="image/*" />
             </intent-filter>
         </activity>
+        <activity
+            android:name=".SendNoteActivity"
+            android:taskAffinity=".SendNoteActivity"
+            android:label="Add note"
+            android:windowSoftInputMode="adjustResize">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
     </application>
 
 </manifest>

+ 1 - 2
app/src/main/java/io/github/zadam/triliumsender/MainActivity.kt

@@ -9,6 +9,7 @@ import android.widget.Button
 import android.widget.TextView
 
 import kotlinx.android.synthetic.main.activity_main.*
+import kotlinx.android.synthetic.main.content_main.*
 
 class MainActivity : AppCompatActivity() {
 
@@ -33,8 +34,6 @@ class MainActivity : AppCompatActivity() {
         setContentView(R.layout.activity_main)
         setSupportActionBar(toolbar)
 
-        val setupConnectionButton = findViewById<Button>(R.id.setupConnectionButton);
-
         setupConnectionButton.setOnClickListener {
             val intent = Intent(this@MainActivity, LoginActivity::class.java)
             startActivity(intent)

+ 127 - 0
app/src/main/java/io/github/zadam/triliumsender/SendNoteActivity.kt

@@ -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("&nbsp;")
+                        previousWasASpace = false
+                        continue
+                    }
+                    previousWasASpace = true
+                } else {
+                    previousWasASpace = false
+                }
+                when (c) {
+                    '<' -> builder.append("&lt;")
+                    '>' -> builder.append("&gt;")
+                    '&' -> builder.append("&amp;")
+                    '"' -> builder.append("&quot;")
+                    '\n' -> builder.append("<p>")
+                    // We need Tab support here, because we print StackTraces as HTML
+                    '\t' -> builder.append("&nbsp; &nbsp; &nbsp;")
+                    else -> if (c.toInt() < 128) {
+                        builder.append(c)
+                    } else {
+                        builder.append("&#").append(c.toInt()).append(";")
+                    }
+                }
+            }
+            return builder.toString()
+        }
+
+        override fun onCancelled() {
+        }
+    }
+}

+ 47 - 0
app/src/main/res/layout/activity_send_note.xml

@@ -0,0 +1,47 @@
+<?xml version="1.0" encoding="utf-8"?>
+<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:fitsSystemWindows="true"
+    tools:context=".SendNoteActivity">
+
+    <LinearLayout
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:orientation="vertical">
+
+        <EditText
+            android:id="@+id/noteTitle"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:ems="10"
+            android:inputType="textPersonName"
+            android:text=""
+            android:hint="Note title" />
+
+        <EditText
+            android:id="@+id/noteText"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:layout_marginTop="8dp"
+            android:ems="10"
+            android:lines="10"
+            android:inputType="textMultiLine"
+            android:gravity="top"
+            android:layout_gravity="top"
+            android:hint="Note text" />
+    </LinearLayout>
+
+    <android.support.design.widget.FloatingActionButton
+        android:id="@+id/sendNoteButton"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_marginBottom="32dp"
+        android:layout_marginEnd="32dp"
+        android:clickable="true"
+        app:layout_constraintBottom_toBottomOf="parent"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:srcCompat="@android:drawable/ic_menu_send" />
+</android.support.constraint.ConstraintLayout>