LibreTube/app/src/main/java/com/github/libretube/dialogs/UpdateDialog.kt

57 lines
2.3 KiB
Kotlin
Raw Normal View History

2022-06-09 12:45:51 +05:30
package com.github.libretube.dialogs
import android.app.Dialog
import android.content.Intent
import android.net.Uri
2022-07-18 22:45:35 +05:30
import android.os.Build
2022-06-09 12:45:51 +05:30
import android.os.Bundle
2022-07-18 22:45:35 +05:30
import android.util.Log
2022-07-19 01:12:50 +05:30
import androidx.appcompat.app.AppCompatActivity
2022-06-09 12:45:51 +05:30
import androidx.fragment.app.DialogFragment
import com.github.libretube.R
2022-07-18 22:45:35 +05:30
import com.github.libretube.services.UpdateService
import com.github.libretube.update.UpdateInfo
2022-07-19 01:12:50 +05:30
import com.github.libretube.util.PermissionHelper
2022-06-09 12:45:51 +05:30
import com.google.android.material.dialog.MaterialAlertDialogBuilder
2022-07-18 22:45:35 +05:30
class UpdateDialog(
private val updateInfo: UpdateInfo
2022-06-09 12:45:51 +05:30
) : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let {
MaterialAlertDialogBuilder(requireContext())
2022-07-18 22:45:35 +05:30
.setTitle(context?.getString(R.string.update_available, updateInfo.name))
.setMessage(context?.getString(R.string.update_now))
2022-06-09 12:45:51 +05:30
.setNegativeButton(context?.getString(R.string.cancel)) { _, _ ->
dismiss()
}
.setPositiveButton(context?.getString(R.string.okay)) { _, _ ->
2022-07-18 22:45:35 +05:30
val downloadUrl = getDownloadUrl(updateInfo)
Log.i("downloadUrl", downloadUrl.toString())
if (downloadUrl != null) {
2022-07-19 01:12:50 +05:30
PermissionHelper.requestReadWrite(activity as AppCompatActivity)
2022-07-18 22:45:35 +05:30
val intent = Intent(context, UpdateService::class.java)
intent.putExtra("downloadUrl", downloadUrl)
context?.startService(intent)
} else {
val uri = Uri.parse(updateInfo.html_url)
val intent = Intent(Intent.ACTION_VIEW).setData(uri)
startActivity(intent)
}
2022-06-09 12:45:51 +05:30
}
.create()
} ?: throw IllegalStateException("Activity cannot be null")
}
2022-07-18 22:45:35 +05:30
private fun getDownloadUrl(updateInfo: UpdateInfo): String? {
val supportedArchitectures = Build.SUPPORTED_ABIS
supportedArchitectures.forEach { arch ->
2022-07-19 01:12:50 +05:30
updateInfo.assets.forEach { asset ->
if (asset.name.contains(arch)) return asset.browser_download_url
2022-07-18 22:45:35 +05:30
}
}
return null
2022-06-09 12:45:51 +05:30
}
}