]> jfr.im git - i.jfr.im-android.git/blob - 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
1 package im.jfr.i
2
3 import android.content.ClipData
4 import android.content.ClipboardManager
5 import android.content.Context
6 import android.content.Intent
7 import android.net.Uri
8 import android.os.Build
9 import androidx.appcompat.app.AppCompatActivity
10 import android.os.Bundle
11 import android.os.Parcelable
12 import android.widget.TextView
13 import android.widget.Toast
14 import org.json.JSONObject
15 import java.net.HttpURLConnection
16 import java.net.URL
17 import kotlin.system.exitProcess
18
19 class ShareActivity : AppCompatActivity() {
20 override fun onCreate(savedInstanceState: Bundle?) {
21 super.onCreate(savedInstanceState)
22 setContentView(R.layout.activity_share)
23
24 when (intent?.action) {
25 Intent.ACTION_SEND -> {
26 if (intent.type?.startsWith("image/") == true) {
27 Thread {
28 handleImage(intent)
29 }.start()
30 } else {
31 assert(false) { "Uhoh! Unsupported MIME" }
32 }
33 }
34 else -> {
35 assert(false) { "Uhoh! Unsupported intent" }
36 }
37 }
38 }
39
40 private fun handleImage(intent: Intent) {
41 val hyphens = "--".toByteArray()
42 val boundary = "o3C5AoN2J8vyaYPh9Ewd4xvQwS5DjpJFSeRNF6sAPhS4QrbNL".toByteArray()
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
50 output.write(hyphens + boundary + crlf)
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 */
65 input?.close()
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" }
74 val uploadedLink = jsonObj.getJSONArray("files").getJSONObject(0).getString("url")
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 {
83 exitProcess(0)
84 }
85 }
86 }
87 }