]>
jfr.im git - i.jfr.im-android.git/blob - app/src/main/java/im/jfr/i/ShareActivity.kt
47d55f75e14beb02f5a6a9cfa800d1fb02bb3ace
3 import android
.content
.ClipData
4 import android
.content
.ClipboardManager
5 import android
.content
.Context
6 import android
.content
.Intent
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
18 class ShareActivity
: AppCompatActivity() {
19 override fun onCreate(savedInstanceState
: Bundle?
) {
20 super.onCreate(savedInstanceState
)
21 setContentView(R
.layout
.activity_share
)
24 intent?
.action
== Intent
.ACTION_SEND
-> {
25 if (intent
.type?
.startsWith("image/") == true) {
30 assert(false) { "Uhoh! Unsupported MIME" }
34 assert(false) { "Uhoh! Unsupported intent" }
39 fun handleImage(intent
: Intent
) {
40 val hyphens
= "--".toByteArray()
41 val boundary
= "o3C5AoN2J8vuyaYPh9Ewd4xvQwS5DjpJFSeRNF6sAPhS4QrbNL".toByteArray()
42 val crlf
= "\r\n".toByteArray()
44 val url
= URL("https://i.jfr.im/upload.php").openConnection() as HttpURLConnection
46 url
.requestMethod
= "POST"
47 url
.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary
.toString(Charsets
.UTF_8
))
48 val output
= url
.outputStream
49 output
.write(hyphens
+ boundary
+ crlf
);
50 (intent
.getParcelableExtra
<Parcelable
>(Intent
.EXTRA_STREAM
) as? Uri
)?
.let
{
51 output
.write("Content-Disposition: form-data; name=\"files[]\";filename=\"$it\"\r\n".toByteArray())
52 output
.write(("Content-Type: "+intent
.type
+"\r\n").toByteArray())
54 val input
= contentResolver
.openInputStream(it
)
57 val buffer = ByteArray(102400)
58 var len = input.read(buffer)
60 output.write(buffer, 0, len)
61 len = input.read(buffer)
65 output
.write(hyphens
+ boundary
+ hyphens
+ crlf
)
67 assert(url
.responseCode
== 200) { "Response was not 200, " + url
.responseCode
.toString() + " " + url
.responseMessage
}
68 val tv
= findViewById
<TextView
>(R
.id
.resultView
)
69 val jsonStr
= url
.inputStream
.readBytes().toString(Charsets
.UTF_8
)
70 val jsonObj
= JSONObject(jsonStr
)
71 assert(jsonObj
.optBoolean("success", false)) { "Response JSON not success" }
72 var uploadedLink
= jsonObj
.getJSONArray("files").getJSONObject(0).getString("url")
73 (getSystemService(Context
.CLIPBOARD_SERVICE
) as ClipboardManager
).setPrimaryClip(ClipData
.newPlainText(uploadedLink
, uploadedLink
))
76 tv
.text
= uploadedLink
77 if (Build
.VERSION
.SDK_INT
<= Build
.VERSION_CODES
.S_V2
)
78 Toast
.makeText(applicationContext
, "Copied", Toast
.LENGTH_SHORT
).show()