Skip to content

Commit

Permalink
Added update install function
Browse files Browse the repository at this point in the history
  • Loading branch information
Corewala committed Mar 12, 2022
1 parent 48c7339 commit 8d70218
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />

<application
android:name="corewala.buran.Buran"
Expand Down
67 changes: 65 additions & 2 deletions app/src/main/java/corewala/buran/io/update/BuranUpdates.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
package corewala.buran.io.update

import java.net.URL
import android.app.DownloadManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Context.DOWNLOAD_SERVICE
import android.content.Intent
import android.content.IntentFilter
import android.net.Uri
import android.os.Build
import android.os.Environment
import androidx.core.content.FileProvider
import corewala.buran.BuildConfig
import corewala.buran.R
import java.io.File
import java.net.HttpURLConnection
import java.net.URL


class BuranUpdates {

fun getLatestVersion(): String {
var latestVersion = ""
var latestVersion = BuildConfig.VERSION_NAME

val updateCheckThread = Thread {
val url = "https://github.com/Corewala/Buran/releases/latest"
Expand All @@ -25,4 +38,54 @@ class BuranUpdates {

return latestVersion
}



fun installUpdate(context: Context, latestVersion: String) {
val updateUrl = "https://github.com/Corewala/Buran/releases/download/$latestVersion/Buran-$latestVersion.apk"
val updateFileName = "Buran.apk"
val updateDestination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).path + "/$updateFileName"
val fileUri = Uri.parse("file://$updateDestination")
val packageFile = File(updateDestination)
if (packageFile.exists()){
packageFile.delete()
}

val request = DownloadManager.Request(Uri.parse(updateUrl))
request.setTitle(context.getString(R.string.app_name) )
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN)

request.setDestinationUri(fileUri)
request.setMimeType("application/vnd.android.package-archive")
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
val updateDownloadManager = context.getSystemService(DOWNLOAD_SERVICE) as DownloadManager
println("Downloading update")
updateDownloadManager.enqueue(request)

val contentUri = FileProvider.getUriForFile(
context,
BuildConfig.APPLICATION_ID + ".provider",
packageFile
)

val updateDownloadReceiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val intent = Intent(Intent.ACTION_VIEW)

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
intent.setDataAndType(contentUri, "application/vnd.android.package-archive")
} else {
intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
intent.setDataAndType(contentUri, "application/vnd.android.package-archive")
}

println("Installing update")
context.startActivity(intent)
}
}

context.registerReceiver(updateDownloadReceiver, IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE))
packageFile.delete()
}
}

0 comments on commit 8d70218

Please sign in to comment.