Commit 56e788a8 authored by Mygod's avatar Mygod

Fix VerifyError

parent fa0669df
...@@ -43,6 +43,7 @@ import java.io.File ...@@ -43,6 +43,7 @@ import java.io.File
import java.io.IOException import java.io.IOException
import java.io.InputStream import java.io.InputStream
import java.net.HttpURLConnection import java.net.HttpURLConnection
import java.net.URL
class SubscriptionService : Service() { class SubscriptionService : Service() {
companion object { companion object {
...@@ -74,29 +75,9 @@ class SubscriptionService : Service() { ...@@ -74,29 +75,9 @@ class SubscriptionService : Service() {
setWhen(0) setWhen(0)
} }
Core.notification.notify(NOTIFICATION_ID, notification.build()) Core.notification.notify(NOTIFICATION_ID, notification.build())
var counter = 0 counter = 0
val workers = urls.asIterable().map { url -> val workers = urls.asIterable().map { url ->
async(Dispatchers.IO) { async(Dispatchers.IO) { work(url, urls.size(), notification) }
val tempFile = File.createTempFile("subscription-", ".json", cacheDir)
try {
(url.openConnection() as HttpURLConnection).useCancellable {
tempFile.outputStream().use { out -> inputStream.copyTo(out) }
}
tempFile
} catch (e: IOException) {
e.printStackTrace()
Toast.makeText(this@SubscriptionService, e.readableMessage, Toast.LENGTH_LONG).show()
if (!tempFile.delete()) tempFile.deleteOnExit()
null
} finally {
withContext(Dispatchers.Main) {
counter += 1
Core.notification.notify(NOTIFICATION_ID, notification.apply {
setProgress(urls.size(), counter, false)
}.build())
}
}
}
} }
try { try {
val localJsons = workers.awaitAll() val localJsons = workers.awaitAll()
...@@ -126,6 +107,31 @@ class SubscriptionService : Service() { ...@@ -126,6 +107,31 @@ class SubscriptionService : Service() {
return START_NOT_STICKY return START_NOT_STICKY
} }
private var counter = 0
private suspend fun work(url: URL, max: Int, notification: NotificationCompat.Builder): File? {
val tempFile = File.createTempFile("subscription-", ".json", cacheDir)
try {
(url.openConnection() as HttpURLConnection).useCancellable {
tempFile.outputStream().use { out -> inputStream.copyTo(out) }
}
return tempFile
} catch (e: IOException) {
e.printStackTrace()
GlobalScope.launch(Dispatchers.Main) {
Toast.makeText(this@SubscriptionService, e.readableMessage, Toast.LENGTH_LONG).show()
}
if (!tempFile.delete()) tempFile.deleteOnExit()
return null
} finally {
withContext(Dispatchers.Main) {
counter += 1
Core.notification.notify(NOTIFICATION_ID, notification.apply {
setProgress(max, counter, false)
}.build())
}
}
}
private fun createProfilesFromSubscription(jsons: Sequence<InputStream>) { private fun createProfilesFromSubscription(jsons: Sequence<InputStream>) {
val currentId = DataStore.profileId val currentId = DataStore.profileId
val profiles = ProfileManager.getAllProfiles() val profiles = ProfileManager.getAllProfiles()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment