import org.json.JSONObject
import java.net.HttpURLConnection
import java.net.URL
+import kotlin.system.exitProcess
class ShareActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_share)
- when {
- intent?.action == Intent.ACTION_SEND -> {
+ when (intent?.action) {
+ Intent.ACTION_SEND -> {
if (intent.type?.startsWith("image/") == true) {
- Thread(Runnable {
+ Thread {
handleImage(intent)
- }).start()
+ }.start()
} else {
assert(false) { "Uhoh! Unsupported MIME" }
}
}
}
- fun handleImage(intent: Intent) {
+ private fun handleImage(intent: Intent) {
val hyphens = "--".toByteArray()
- val boundary = "o3C5AoN2J8vuyaYPh9Ewd4xvQwS5DjpJFSeRNF6sAPhS4QrbNL".toByteArray()
+ val boundary = "o3C5AoN2J8vyaYPh9Ewd4xvQwS5DjpJFSeRNF6sAPhS4QrbNL".toByteArray()
val crlf = "\r\n".toByteArray()
val url = URL("https://i.jfr.im/upload.php").openConnection() as HttpURLConnection
url.requestMethod = "POST"
url.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary.toString(Charsets.UTF_8))
val output = url.outputStream
- output.write(hyphens + boundary + crlf);
+ output.write(hyphens + boundary + crlf)
(intent.getParcelableExtra<Parcelable>(Intent.EXTRA_STREAM) as? Uri)?.let {
output.write("Content-Disposition: form-data; name=\"files[]\";filename=\"$it\"\r\n".toByteArray())
output.write(("Content-Type: "+intent.type+"\r\n").toByteArray())
len = input.read(buffer)
}
*/
+ input?.close()
output.write(crlf)
output.write(hyphens + boundary + hyphens + crlf)
}
val jsonStr = url.inputStream.readBytes().toString(Charsets.UTF_8)
val jsonObj = JSONObject(jsonStr)
assert(jsonObj.optBoolean("success", false)) { "Response JSON not success" }
- var uploadedLink = jsonObj.getJSONArray("files").getJSONObject(0).getString("url")
+ val uploadedLink = jsonObj.getJSONArray("files").getJSONObject(0).getString("url")
(getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager).setPrimaryClip(ClipData.newPlainText(uploadedLink, uploadedLink))
tv.post {
Toast.makeText(applicationContext, "Copied", Toast.LENGTH_SHORT).show()
runOnUiThread {
- System.exit(0)
+ exitProcess(0)
}
}
}