Skip to content

Commit

Permalink
store apks in private dir and remove them after update (#346)
Browse files Browse the repository at this point in the history
  • Loading branch information
pazos committed Dec 26, 2021
1 parent f58fe01 commit 5855067
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
6 changes: 2 additions & 4 deletions app/src/main/java/org/koreader/launcher/ApkUpdater.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@ class ApkUpdater {

fun download(context: Context, url: String, name: String): Int {
return if (MainApp.has_ota_updates) {
@Suppress("DEPRECATION") val file = File(
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS).toString() + File.separator + name)
val file = File(context.getExternalFilesDir(null), name)
val result = try {
if (file.exists()) {
DOWNLOAD_EXISTS
} else {
val request = DownloadManager.Request(Uri.parse(url))
request.setMimeType("application/vnd.android.package-archive")
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN)
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, name)
request.setDestinationInExternalFilesDir(context, null, name)
val manager = context.getSystemService(Context.DOWNLOAD_SERVICE)
as DownloadManager
manager.enqueue(request)
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/org/koreader/launcher/Assets.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.view.Gravity
import android.view.ViewGroup
import android.widget.ProgressBar
import androidx.core.content.ContextCompat
import org.koreader.launcher.extensions.pruneCacheDir
import org.koreader.launcher.extensions.symlink
import java.io.*

Expand All @@ -26,6 +27,7 @@ class Assets {
val result = bootstrap(activity)
val elapsedTime = System.nanoTime() - startTime
Log.i(tag, "update installed in ${elapsedTime / 1000000} milliseconds")
activity.pruneCacheDir()
result
} else {
true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,18 @@ fun Activity.openWifi() {
startActivityCompat(this, openWifiIntent)
}

fun Activity.pruneCacheDir() {
try {
getExternalFilesDir(null)?.let {
it.listFiles()?.forEach { file ->
file.delete()
}
}
} catch (e: Exception) {
e.printStackTrace()
}
}

fun Activity.requestSpecialPermission(intent: Intent, rationale: String,
okButton: String?, cancelButton: String?) {
runOnUiThread {
Expand Down

0 comments on commit 5855067

Please sign in to comment.