|
|
|
@ -1,27 +1,18 @@
|
|
|
|
package com.cyb3rko.techniklogger
|
|
|
|
package com.cyb3rko.techniklogger
|
|
|
|
|
|
|
|
|
|
|
|
import android.Manifest
|
|
|
|
import android.Manifest
|
|
|
|
import android.app.DownloadManager
|
|
|
|
|
|
|
|
import android.content.BroadcastReceiver
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.Intent
|
|
|
|
|
|
|
|
import android.content.IntentFilter
|
|
|
|
|
|
|
|
import android.content.pm.PackageManager
|
|
|
|
import android.content.pm.PackageManager
|
|
|
|
import android.net.Uri
|
|
|
|
|
|
|
|
import android.os.Build
|
|
|
|
|
|
|
|
import android.os.Environment
|
|
|
|
|
|
|
|
import android.util.Base64
|
|
|
|
import android.util.Base64
|
|
|
|
import android.util.Log
|
|
|
|
import android.util.Log
|
|
|
|
import android.webkit.URLUtil
|
|
|
|
|
|
|
|
import androidx.core.app.ActivityCompat
|
|
|
|
import androidx.core.app.ActivityCompat
|
|
|
|
import androidx.core.content.ContextCompat
|
|
|
|
import androidx.core.content.ContextCompat
|
|
|
|
import androidx.core.content.FileProvider
|
|
|
|
|
|
|
|
import com.afollestad.materialdialogs.MaterialDialog
|
|
|
|
import com.afollestad.materialdialogs.MaterialDialog
|
|
|
|
import com.androidnetworking.AndroidNetworking
|
|
|
|
import com.androidnetworking.AndroidNetworking
|
|
|
|
import com.androidnetworking.error.ANError
|
|
|
|
import com.androidnetworking.error.ANError
|
|
|
|
import com.androidnetworking.interfaces.StringRequestListener
|
|
|
|
import com.androidnetworking.interfaces.StringRequestListener
|
|
|
|
|
|
|
|
import com.cyb3rko.installer.DownloadApk
|
|
|
|
import es.dmoral.toasty.Toasty
|
|
|
|
import es.dmoral.toasty.Toasty
|
|
|
|
import java.io.File
|
|
|
|
|
|
|
|
import java.nio.charset.StandardCharsets
|
|
|
|
import java.nio.charset.StandardCharsets
|
|
|
|
|
|
|
|
|
|
|
|
internal fun updateCheck(activity: MainActivity) {
|
|
|
|
internal fun updateCheck(activity: MainActivity) {
|
|
|
|
@ -32,15 +23,15 @@ internal fun updateCheck(activity: MainActivity) {
|
|
|
|
.build()
|
|
|
|
.build()
|
|
|
|
.getAsString(object : StringRequestListener {
|
|
|
|
.getAsString(object : StringRequestListener {
|
|
|
|
override fun onResponse(response: String?) {
|
|
|
|
override fun onResponse(response: String?) {
|
|
|
|
var parts = response!!.split("content\":\"".toRegex()).toTypedArray()
|
|
|
|
var parts = response!!.split("content\":\"")
|
|
|
|
var parts2 = parts[1].split("\",\"target".toRegex()).toTypedArray()
|
|
|
|
var parts2 = parts[1].split("\",\"target")
|
|
|
|
val content = String(Base64.decode(parts2[0], Base64.DEFAULT), StandardCharsets.UTF_8)
|
|
|
|
val content = String(Base64.decode(parts2[0], Base64.DEFAULT), StandardCharsets.UTF_8)
|
|
|
|
|
|
|
|
|
|
|
|
parts = content.split("versionCode ".toRegex()).toTypedArray()
|
|
|
|
parts = content.split("versionCode ")
|
|
|
|
parts2 = parts[1].split("\n".toRegex()).toTypedArray()
|
|
|
|
parts2 = parts[1].split("\n")
|
|
|
|
val neuesterVersionCode = parts2[0].toInt()
|
|
|
|
val neuesterVersionCode = parts2[0].toInt()
|
|
|
|
parts = parts2[1].split("\"".toRegex()).toTypedArray()
|
|
|
|
parts = parts2[1].split("\"")
|
|
|
|
parts2 = parts[1].split("\"".toRegex()).toTypedArray()
|
|
|
|
parts2 = parts[1].split("\"")
|
|
|
|
val newestVersion = parts2[0]
|
|
|
|
val newestVersion = parts2[0]
|
|
|
|
|
|
|
|
|
|
|
|
if (BuildConfig.VERSION_CODE != neuesterVersionCode) {
|
|
|
|
if (BuildConfig.VERSION_CODE != neuesterVersionCode) {
|
|
|
|
@ -48,10 +39,7 @@ internal fun updateCheck(activity: MainActivity) {
|
|
|
|
showDownloadDialog(activity, newestVersion)
|
|
|
|
showDownloadDialog(activity, newestVersion)
|
|
|
|
|
|
|
|
|
|
|
|
ActivityCompat.requestPermissions(
|
|
|
|
ActivityCompat.requestPermissions(
|
|
|
|
activity, arrayOf(
|
|
|
|
activity, arrayOf(Manifest.permission.INTERNET, Manifest.permission.WRITE_EXTERNAL_STORAGE), 1
|
|
|
|
Manifest.permission.INTERNET, Manifest.permission.READ_EXTERNAL_STORAGE,
|
|
|
|
|
|
|
|
Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.REQUEST_INSTALL_PACKAGES
|
|
|
|
|
|
|
|
), 1
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
Log.d(activity.toString(), "App auf dem neuesten Stand")
|
|
|
|
Log.d(activity.toString(), "App auf dem neuesten Stand")
|
|
|
|
@ -69,6 +57,7 @@ internal fun showDownloadDialog(context: Context, newestVersion: String) {
|
|
|
|
title(0, "Neues Update verfügbar")
|
|
|
|
title(0, "Neues Update verfügbar")
|
|
|
|
message(0, "Das Update '$newestVersion' steht zum Download bereit!\n\nAktuell installierte Version: '${BuildConfig.VERSION_NAME}'")
|
|
|
|
message(0, "Das Update '$newestVersion' steht zum Download bereit!\n\nAktuell installierte Version: '${BuildConfig.VERSION_NAME}'")
|
|
|
|
positiveButton(0, "Herunterladen") {
|
|
|
|
positiveButton(0, "Herunterladen") {
|
|
|
|
|
|
|
|
it.cancel()
|
|
|
|
downloadApk(context, newestVersion)
|
|
|
|
downloadApk(context, newestVersion)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -77,44 +66,8 @@ internal fun showDownloadDialog(context: Context, newestVersion: String) {
|
|
|
|
private fun downloadApk(context: Context, newestVersion: String) {
|
|
|
|
private fun downloadApk(context: Context, newestVersion: String) {
|
|
|
|
if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
|
|
|
|
if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
|
|
|
|
val link = "https://cdn.cyb3rko.de/Apps/Technik-Logger/Technik-Logger%20v$newestVersion.apk"
|
|
|
|
val link = "https://cdn.cyb3rko.de/Apps/Technik-Logger/Technik-Logger%20v$newestVersion.apk"
|
|
|
|
println("$newestVersion: $link")
|
|
|
|
DownloadApk(context).startDownloadingApk(link)
|
|
|
|
val request = DownloadManager.Request(Uri.parse(link))
|
|
|
|
|
|
|
|
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(link, null, null))
|
|
|
|
|
|
|
|
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
|
|
|
|
|
|
|
|
downloadManager.enqueue(request)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
|
|
|
|
|
|
|
|
val onCompleteReceiver = object: BroadcastReceiver() {
|
|
|
|
|
|
|
|
override fun onReceive(context: Context?, intent: Intent?) {
|
|
|
|
|
|
|
|
val apkPath = "${Environment.getExternalStorageDirectory()}/download/Technik-Logger v$newestVersion.apk"
|
|
|
|
|
|
|
|
val file = File(apkPath)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
installBelowAndroid10(context!!, file, this)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
context.registerReceiver(onCompleteReceiver, IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
Toasty.error(context, "Fehlende Berechtigung, erteilen Sie diese beim nächsten App-Start", Toasty.LENGTH_LONG).show()
|
|
|
|
Toasty.error(context, "Fehlende Berechtigung, erteilen Sie diese beim nächsten App-Start", Toasty.LENGTH_LONG).show()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private fun uriFromFile(context: Context, file: File): Uri {
|
|
|
|
|
|
|
|
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
|
|
|
|
|
|
FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
Uri.fromFile(file)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun installBelowAndroid10(context: Context, file: File, broadcastReceiver: BroadcastReceiver) {
|
|
|
|
|
|
|
|
val installIntent = Intent(Intent.ACTION_VIEW)
|
|
|
|
|
|
|
|
installIntent.setDataAndType(uriFromFile(context, file), "application/vnd.android.package-archive")
|
|
|
|
|
|
|
|
installIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
|
|
|
|
|
|
|
installIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
|
|
|
|
|
|
|
context.startActivity(installIntent)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
context.unregisterReceiver(broadcastReceiver)
|
|
|
|
|
|
|
|
}
|
|
|
|
|