Apk update successfully added
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package com.example.hpostesting.presentation.dashboard
|
package com.example.hpostesting.presentation.dashboard
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.SharedPreferences
|
import android.content.SharedPreferences
|
||||||
@@ -74,6 +75,7 @@ class DashboardActivity : AppCompatActivity(), IDataCollector {
|
|||||||
Log.d(TAG, "Received message on topic $topic: $message")
|
Log.d(TAG, "Received message on topic $topic: $message")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressLint("SetWorldReadable")
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
|
|
||||||
@@ -96,15 +98,17 @@ class DashboardActivity : AppCompatActivity(), IDataCollector {
|
|||||||
is Result.Success -> {
|
is Result.Success -> {
|
||||||
// Handle success
|
// Handle success
|
||||||
val apk = result.data
|
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 ->
|
apk.byteStream().use { input ->
|
||||||
fileToWriteTo.outputStream().use { output ->
|
file.outputStream().use { output ->
|
||||||
input.copyTo(output)
|
input.copyTo(output)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Log.d("Responsebodyformat", "Responsebodyformat: ")
|
Log.d("Responsebodyformat", "Responsebodyformat: ")
|
||||||
installApk(fileToWriteTo)
|
installApk(file)
|
||||||
Log.e("ApI", "APK URL: $apk")
|
Log.e("ApI", "APK URL: $apk")
|
||||||
Toast.makeText(
|
Toast.makeText(
|
||||||
this,
|
this,
|
||||||
@@ -157,80 +161,29 @@ class DashboardActivity : AppCompatActivity(), IDataCollector {
|
|||||||
return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
|
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()
|
private fun installApk(file: File) {
|
||||||
outputStream = FileOutputStream(file)
|
val pInfo = baseContext.packageManager.getPackageInfo(baseContext.packageName, 0)
|
||||||
|
val uri: Uri = FileProvider.getUriForFile(
|
||||||
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(
|
|
||||||
this,
|
this,
|
||||||
"${BuildConfig.APPLICATION_ID}.provider",
|
"${BuildConfig.APPLICATION_ID}.fileprovider",
|
||||||
fileToWriteTo
|
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 {
|
// Start the installation
|
||||||
setDataAndType(apkUri, "application/vnd.android.package-archive")
|
startActivity(installIntent)
|
||||||
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_GRANT_READ_URI_PERMISSION
|
|
||||||
}
|
|
||||||
startActivity(intent)
|
|
||||||
|
|
||||||
|
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() {
|
override fun onDestroy() {
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user