LibreTube/app/src/main/java/com/github/libretube/services/UpdateService.kt

99 lines
3.3 KiB
Kotlin
Raw Normal View History

2022-07-18 22:45:35 +05:30
package com.github.libretube.services
import android.app.DownloadManager
import android.app.Service
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.Uri
import android.os.Environment
import android.os.IBinder
2022-08-08 15:31:25 +05:30
import android.widget.Toast
2022-07-24 01:31:37 +05:30
import com.github.libretube.R
import com.github.libretube.helpers.DownloadHelper
2022-07-18 22:45:35 +05:30
import java.io.File
class UpdateService : Service() {
private lateinit var downloadUrl: String
private var downloadId: Long = -1
private lateinit var file: File
private lateinit var downloadManager: DownloadManager
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
downloadUrl = intent?.getStringExtra("downloadUrl")!!
downloadApk(downloadUrl)
return super.onStartCommand(intent, flags, startId)
}
private fun downloadApk(downloadUrl: String) {
2022-11-19 20:54:38 +05:30
file = File(getDownloadDirectory(), "release.apk")
2022-07-18 22:45:35 +05:30
val request: DownloadManager.Request =
DownloadManager.Request(Uri.parse(downloadUrl))
2022-07-24 01:31:37 +05:30
.setTitle(getString(R.string.downloading_apk))
2022-07-18 22:45:35 +05:30
.setDescription("")
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)
.setDestinationUri(Uri.fromFile(file))
.setAllowedOverMetered(true)
.setAllowedOverRoaming(true)
downloadManager =
applicationContext.getSystemService(DOWNLOAD_SERVICE) as DownloadManager
downloadId = downloadManager.enqueue(request)
// listener for the download to end
registerReceiver(
onDownloadComplete,
IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)
)
}
private val onDownloadComplete: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1)
if (downloadId == id) {
// install the apk after download finished
2022-09-20 23:30:15 +05:30
val installIntent = Intent(Intent.ACTION_VIEW).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
setDataAndType(
Uri.fromFile(file),
downloadManager.getMimeTypeForDownloadedFile(downloadId)
)
}
2022-08-08 15:31:25 +05:30
try {
startActivity(installIntent)
} catch (e: Exception) {
Toast.makeText(
context,
R.string.downloadsucceeded,
Toast.LENGTH_SHORT
).show()
}
2022-07-18 22:45:35 +05:30
}
}
}
2022-11-19 20:54:38 +05:30
private fun getDownloadDirectory(): File {
2022-12-19 21:28:34 +05:30
val downloadsDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS
)
2022-11-19 20:54:38 +05:30
if (!downloadsDir.canWrite()) return DownloadHelper.getOfflineStorageDir(this)
return downloadsDir
}
2022-07-18 22:45:35 +05:30
override fun onDestroy() {
unregisterReceiver(onDownloadComplete)
super.onDestroy()
}
override fun onBind(p0: Intent?): IBinder? {
TODO("Not yet implemented")
}
}