]> jfr.im git - i.jfr.im-android.git/blame - app/src/main/java/im/jfr/i/ShareActivity.kt
add stuff
[i.jfr.im-android.git] / app / src / main / java / im / jfr / i / ShareActivity.kt
CommitLineData
e9a08ae2
JR
1package im.jfr.i
2
3import android.content.ClipData
4import android.content.ClipboardManager
5import android.content.Context
6import android.content.Intent
7import android.net.Uri
8import android.os.Build
9import androidx.appcompat.app.AppCompatActivity
10import android.os.Bundle
11import android.os.Parcelable
12import android.widget.TextView
13import android.widget.Toast
14import org.json.JSONObject
15import java.net.HttpURLConnection
16import java.net.URL
732657bf 17import kotlin.system.exitProcess
e9a08ae2
JR
18
19class ShareActivity : AppCompatActivity() {
20 override fun onCreate(savedInstanceState: Bundle?) {
21 super.onCreate(savedInstanceState)
22 setContentView(R.layout.activity_share)
23
732657bf
JR
24 when (intent?.action) {
25 Intent.ACTION_SEND -> {
e9a08ae2 26 if (intent.type?.startsWith("image/") == true) {
732657bf 27 Thread {
e9a08ae2 28 handleImage(intent)
732657bf 29 }.start()
e9a08ae2
JR
30 } else {
31 assert(false) { "Uhoh! Unsupported MIME" }
32 }
33 }
34 else -> {
35 assert(false) { "Uhoh! Unsupported intent" }
36 }
37 }
38 }
39
732657bf 40 private fun handleImage(intent: Intent) {
e9a08ae2 41 val hyphens = "--".toByteArray()
732657bf 42 val boundary = "o3C5AoN2J8vyaYPh9Ewd4xvQwS5DjpJFSeRNF6sAPhS4QrbNL".toByteArray()
e9a08ae2
JR
43 val crlf = "\r\n".toByteArray()
44
45 val url = URL("https://i.jfr.im/upload.php").openConnection() as HttpURLConnection
46 url.doOutput = true
47 url.requestMethod = "POST"
48 url.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary.toString(Charsets.UTF_8))
49 val output = url.outputStream
732657bf 50 output.write(hyphens + boundary + crlf)
e9a08ae2
JR
51 (intent.getParcelableExtra<Parcelable>(Intent.EXTRA_STREAM) as? Uri)?.let {
52 output.write("Content-Disposition: form-data; name=\"files[]\";filename=\"$it\"\r\n".toByteArray())
53 output.write(("Content-Type: "+intent.type+"\r\n").toByteArray())
54 output.write(crlf)
55 val input = contentResolver.openInputStream(it)
56 input?.copyTo(output)
57 /*
58 val buffer = ByteArray(102400)
59 var len = input.read(buffer)
60 while (len != -1) {
61 output.write(buffer, 0, len)
62 len = input.read(buffer)
63 }
64 */
732657bf 65 input?.close()
e9a08ae2
JR
66 output.write(crlf)
67 output.write(hyphens + boundary + hyphens + crlf)
68 }
69 assert(url.responseCode == 200) { "Response was not 200, " + url.responseCode.toString() + " " + url.responseMessage }
70 val tv = findViewById<TextView>(R.id.resultView)
71 val jsonStr = url.inputStream.readBytes().toString(Charsets.UTF_8)
72 val jsonObj = JSONObject(jsonStr)
73 assert(jsonObj.optBoolean("success", false)) { "Response JSON not success" }
732657bf 74 val uploadedLink = jsonObj.getJSONArray("files").getJSONObject(0).getString("url")
e9a08ae2
JR
75 (getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager).setPrimaryClip(ClipData.newPlainText(uploadedLink, uploadedLink))
76
77 tv.post {
78 tv.text = uploadedLink
79 if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.S_V2)
80 Toast.makeText(applicationContext, "Copied", Toast.LENGTH_SHORT).show()
81
82 runOnUiThread {
732657bf 83 exitProcess(0)
e9a08ae2
JR
84 }
85 }
86 }
87}