Apk update successfully added

This commit is contained in:
Mariya
2024-02-28 14:46:06 +05:30
parent 70b0b93f57
commit 626dd380ab

View File

@@ -1,5 +1,6 @@
package com.example.hpostesting.presentation.dashboard
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
@@ -74,6 +75,7 @@ class DashboardActivity : AppCompatActivity(), IDataCollector {
Log.d(TAG, "Received message on topic $topic: $message")
}
@SuppressLint("SetWorldReadable")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@@ -96,15 +98,17 @@ class DashboardActivity : AppCompatActivity(), IDataCollector {
is Result.Success -> {
// Handle success
val apk = result.data
val fileToWriteTo = File(this.getExternalFilesDir(null), "downloaded_file.apk")
val file = File(getExternalFilesDir("Updates"), "update.apk")
file.setReadable(true, false) // Ensure the file is readable
apk.byteStream().use { input ->
fileToWriteTo.outputStream().use { output ->
file.outputStream().use { output ->
input.copyTo(output)
}
}
Log.d("Responsebodyformat", "Responsebodyformat: ")
installApk(fileToWriteTo)
installApk(file)
Log.e("ApI", "APK URL: $apk")
Toast.makeText(
this,
@@ -157,80 +161,29 @@ class DashboardActivity : AppCompatActivity(), IDataCollector {
return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
}
private fun downloadApk(responseBody: ResponseBody) {
val file = File(getExternalFilesDir(null), "Update.apk")
var inputStream: InputStream? = null
var outputStream: FileOutputStream? = null
try {
val fileReader = ByteArray(4096)
val fileSize = responseBody.contentLength()
var fileSizeDownloaded: Long = 0
inputStream = responseBody.byteStream()
outputStream = FileOutputStream(file)
while (true) {
val read = inputStream.read(fileReader)
if (read == -1) {
break
}
outputStream.write(fileReader, 0, read)
fileSizeDownloaded += read.toLong()
Log.d(TAG, "File download: $fileSizeDownloaded of $fileSize")
}
outputStream.flush()
} catch (e: Exception) {
Log.e(TAG, "Error saving APK: ${e.message}")
} finally {
inputStream?.close()
outputStream?.close()
}
}
private fun installApk(fileToWriteTo: File) {
val apkUri = FileProvider.getUriForFile(
private fun installApk(file: File) {
val pInfo = baseContext.packageManager.getPackageInfo(baseContext.packageName, 0)
val uri: Uri = FileProvider.getUriForFile(
this,
"${BuildConfig.APPLICATION_ID}.provider",
fileToWriteTo
"${BuildConfig.APPLICATION_ID}.fileprovider",
file
)
val installIntent = Intent(Intent.ACTION_INSTALL_PACKAGE)
installIntent.data = uri
installIntent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or
Intent.FLAG_ACTIVITY_NEW_TASK or
Intent.FLAG_ACTIVITY_CLEAR_TOP
installIntent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true)
val intent = Intent(Intent.ACTION_VIEW).apply {
setDataAndType(apkUri, "application/vnd.android.package-archive")
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_GRANT_READ_URI_PERMISSION
}
startActivity(intent)
// Start the installation
startActivity(installIntent)
Log.d("InstallApk", "Install Intent URI: $uri")
Log.d("InstallApk", "Package Name: $packageName")
}
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()
}