blob: e88ab8055fbc03fe7c0457412a4faf0ed8dde338 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
package moe.yuuta.workmode.update
import com.elvishew.xlog.XLog
import moe.yuuta.workmode.BuildConfig
import moe.yuuta.workmode.async.Runnable
import org.json.JSONObject
import java.io.BufferedReader
import java.io.InputStreamReader
import java.net.HttpURLConnection
import java.net.URL
class UpdateChecker : Runnable<Update> {
private val logger = XLog.tag("UpdateChecker").build()
override fun run(): Update {
logger.d("Start checking")
var httpURLConnection: HttpURLConnection? = null
try {
val url = URL("https://raw.githubusercontent.com/Trumeet/_priv/master/update/${BuildConfig.APPLICATION_ID}.json")
httpURLConnection = url.openConnection() as HttpURLConnection
httpURLConnection.requestMethod = "GET"
httpURLConnection.connect()
val inputStream = httpURLConnection.inputStream
val buffer = StringBuffer()
if (inputStream == null) {
throw IllegalStateException("Str is null.")
}
val reader = BufferedReader(InputStreamReader(inputStream))
var line: String?
while (true) {
line = reader.readLine()
if (line == null) break
buffer.append(line + "\n")
}
if (buffer.isEmpty()) {
throw IllegalStateException("Buffer is empty")
}
val response = buffer.toString()
logger.i("Response: ${httpURLConnection.responseCode} (${httpURLConnection.responseMessage}), data: $response")
val json = JSONObject(response)
return Update(json.getInt("version"),
json.getString("name"),
json.getString("alt_url"),
json.getBoolean("alt_url_enable"),
json.getBoolean("alt_url_force"))
} catch (e: Exception) {
logger.e("Unable to check", e)
throw e
} finally {
// this is done so that there are no open connections left when this task is going to complete
if (httpURLConnection != null)
httpURLConnection.disconnect()
}
}
}
data class Update(
val version: Int,
val name: String,
val altUrl: String,
val altUrlEnabled: Boolean,
val altUrlForce: Boolean
)
|