Apk Update Code Added
This commit is contained in:
@@ -34,9 +34,12 @@ import `in`.sminnovations.hpostesting.BuildConfig
|
||||
import `in`.sminnovations.hpostesting.R
|
||||
import `in`.sminnovations.hpostesting.databinding.ActivityDashboardBinding
|
||||
import okhttp3.ResponseBody
|
||||
import java.io.BufferedInputStream
|
||||
import java.io.File
|
||||
import java.io.FileInputStream
|
||||
import java.io.FileOutputStream
|
||||
import java.io.InputStream
|
||||
import java.util.zip.ZipInputStream
|
||||
|
||||
interface NatsMessageCallback {
|
||||
fun onMessageReceived(topic: String, message: String)
|
||||
@@ -92,20 +95,17 @@ class DashboardActivity : AppCompatActivity(), IDataCollector {
|
||||
when (result) {
|
||||
is Result.Success -> {
|
||||
// Handle success
|
||||
val apkUrl = result.data
|
||||
val responseBodyString = apkUrl.string()
|
||||
|
||||
|
||||
val apk = result.data
|
||||
val fileToWriteTo = File(this.getExternalFilesDir(null), "downloaded_file.apk")
|
||||
|
||||
apkUrl.byteStream().use { inputStream ->
|
||||
FileOutputStream(fileToWriteTo).use { outputStream ->
|
||||
inputStream.copyTo(outputStream)
|
||||
apk.byteStream().use { input ->
|
||||
fileToWriteTo.outputStream().use { output ->
|
||||
input.copyTo(output)
|
||||
}
|
||||
}
|
||||
Log.d(TAG, "Responsebody: $responseBodyString")
|
||||
Log.d("Responsebodyformat", "Responsebodyformat: ")
|
||||
installApk(fileToWriteTo)
|
||||
Log.e("ApI", "APK URL: $apkUrl")
|
||||
Log.e("ApI", "APK URL: $apk")
|
||||
Toast.makeText(
|
||||
this,
|
||||
"${result.data}",
|
||||
@@ -203,6 +203,34 @@ class DashboardActivity : AppCompatActivity(), IDataCollector {
|
||||
|
||||
}
|
||||
|
||||
|
||||
fun unzip(zipFilePath: String, outputFolderPath: String) {
|
||||
ZipInputStream(BufferedInputStream(FileInputStream(zipFilePath))).use { zis ->
|
||||
var zipEntry = zis.nextEntry
|
||||
val buffer = ByteArray(1024)
|
||||
|
||||
while (zipEntry != null) {
|
||||
val fileName = zipEntry.name
|
||||
val newFile = File(outputFolderPath, fileName)
|
||||
|
||||
if (zipEntry.isDirectory) {
|
||||
newFile.mkdirs()
|
||||
} else {
|
||||
// Create all parent directories
|
||||
newFile.parent?.let { File(it).mkdirs() }
|
||||
|
||||
FileOutputStream(newFile).use { fos ->
|
||||
var len: Int
|
||||
while (zis.read(buffer).also { len = it } > 0) {
|
||||
fos.write(buffer, 0, len)
|
||||
}
|
||||
}
|
||||
}
|
||||
zipEntry = zis.nextEntry
|
||||
}
|
||||
zis.closeEntry()
|
||||
}
|
||||
}
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user