|
|
|
|
@@ -1,7 +1,5 @@
|
|
|
|
|
package com.example.hpostesting.presentation.dashboard
|
|
|
|
|
|
|
|
|
|
import android.app.DownloadManager
|
|
|
|
|
import android.content.BroadcastReceiver
|
|
|
|
|
import android.content.Context
|
|
|
|
|
import android.content.Intent
|
|
|
|
|
import android.content.SharedPreferences
|
|
|
|
|
@@ -23,6 +21,7 @@ import androidx.navigation.ui.setupWithNavController
|
|
|
|
|
import com.example.hpostesting.data.Result
|
|
|
|
|
import com.example.hpostesting.data.constant.Constants
|
|
|
|
|
import com.example.hpostesting.data.constant.LanguageManager
|
|
|
|
|
import com.example.hpostesting.data.model.updates.DeviceUpdateRequest
|
|
|
|
|
import com.example.hpostesting.presentation.NatsManager
|
|
|
|
|
import com.example.hpostesting.presentation.hemocube.HemoCubeViewModel
|
|
|
|
|
import com.example.hpostesting.presentation.jig.JigActivity
|
|
|
|
|
@@ -35,6 +34,8 @@ import `in`.sminnovations.hpostesting.R
|
|
|
|
|
import `in`.sminnovations.hpostesting.databinding.ActivityDashboardBinding
|
|
|
|
|
import okhttp3.ResponseBody
|
|
|
|
|
import java.io.File
|
|
|
|
|
import java.io.FileOutputStream
|
|
|
|
|
import java.io.InputStream
|
|
|
|
|
|
|
|
|
|
interface NatsMessageCallback {
|
|
|
|
|
fun onMessageReceived(topic: String, message: String)
|
|
|
|
|
@@ -57,7 +58,7 @@ class DashboardActivity : AppCompatActivity(), IDataCollector {
|
|
|
|
|
private var downloadId: Long = 0
|
|
|
|
|
// TODO: Remove hemocube viewmodel
|
|
|
|
|
private val hemocubeViewModel: HemoCubeViewModel by viewModels()
|
|
|
|
|
|
|
|
|
|
private lateinit var sharedPreference: SharedPreferences
|
|
|
|
|
override fun attachBaseContext(newBase: Context?) {
|
|
|
|
|
val languageCode = LanguageManager.getSavedLanguage(newBase!!)
|
|
|
|
|
LanguageManager.setLocale(newBase, languageCode)
|
|
|
|
|
@@ -91,14 +92,13 @@ class DashboardActivity : AppCompatActivity(), IDataCollector {
|
|
|
|
|
is Result.Success -> {
|
|
|
|
|
// Handle success
|
|
|
|
|
val apkUrl = result.data
|
|
|
|
|
// val apkUrl = "https://dl.dropboxusercontent.com/s/fi/1c3nn7t0co431hicl3hrt/app-debug.apk?rlkey=e4uf13ty1dpcked614vy1aaqp&dl=0"
|
|
|
|
|
initiateUpdate(apkUrl.toString())
|
|
|
|
|
Log.d("ApI", "APK URL: $apkUrl")
|
|
|
|
|
// Toast.makeText(
|
|
|
|
|
// this,
|
|
|
|
|
// "APK UPLOAD ${result.data}",
|
|
|
|
|
// Toast.LENGTH_SHORT
|
|
|
|
|
// ).show()
|
|
|
|
|
downloadApk(apkUrl)
|
|
|
|
|
Log.e("ApI", "APK URL: $apkUrl")
|
|
|
|
|
Toast.makeText(
|
|
|
|
|
this,
|
|
|
|
|
"${result.data}",
|
|
|
|
|
Toast.LENGTH_SHORT
|
|
|
|
|
).show()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
is Result.Error -> {
|
|
|
|
|
@@ -145,69 +145,54 @@ class DashboardActivity : AppCompatActivity(), IDataCollector {
|
|
|
|
|
return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun initiateUpdate(responseBody: String) {
|
|
|
|
|
val apkUrl = responseBody
|
|
|
|
|
if (!isValidHttpUrl(apkUrl)) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
val request = DownloadManager.Request(Uri.parse(apkUrl))
|
|
|
|
|
request.setTitle("App Update")
|
|
|
|
|
request.setDescription("Downloading update...")
|
|
|
|
|
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
|
|
|
|
|
request.setDestinationInExternalFilesDir(this, "Updates", "update.apk")
|
|
|
|
|
inputStream = responseBody.byteStream()
|
|
|
|
|
outputStream = FileOutputStream(file)
|
|
|
|
|
|
|
|
|
|
val downloadManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
|
|
|
|
|
downloadId = downloadManager.enqueue(request)
|
|
|
|
|
|
|
|
|
|
// Register a BroadcastReceiver to receive the download complete event
|
|
|
|
|
// val filter = IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)
|
|
|
|
|
// registerReceiver(downloadReceiver, filter)
|
|
|
|
|
}
|
|
|
|
|
private fun extractApkUrl(responseBody: ResponseBody): String {
|
|
|
|
|
return responseBody.string()
|
|
|
|
|
}
|
|
|
|
|
private fun isValidHttpUrl(url: String): Boolean {
|
|
|
|
|
return url.startsWith("http://") || url.startsWith("https://")
|
|
|
|
|
}
|
|
|
|
|
private val downloadReceiver = object : BroadcastReceiver() {
|
|
|
|
|
override fun onReceive(context: Context?, intent: Intent?) {
|
|
|
|
|
val id = intent?.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1)
|
|
|
|
|
if (id == downloadId) {
|
|
|
|
|
installApk()
|
|
|
|
|
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()
|
|
|
|
|
installApk(file) // Call installApk directly with the file
|
|
|
|
|
} catch (e: Exception) {
|
|
|
|
|
Log.e(TAG, "Error saving APK: ${e.message}")
|
|
|
|
|
} finally {
|
|
|
|
|
inputStream?.close()
|
|
|
|
|
outputStream?.close()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun installApk() {
|
|
|
|
|
val file = File(getExternalFilesDir("Updates"), "update.apk")
|
|
|
|
|
file.setReadable(true, false) // Ensure the file is readable
|
|
|
|
|
|
|
|
|
|
val pInfo = baseContext.packageManager.getPackageInfo(baseContext.packageName, 0)
|
|
|
|
|
private fun installApk(file: File) {
|
|
|
|
|
val uri: Uri = FileProvider.getUriForFile(
|
|
|
|
|
this,
|
|
|
|
|
"${pInfo}.fileprovider",
|
|
|
|
|
"${applicationContext.packageName}.provider", // Update this with your FileProvider authority
|
|
|
|
|
file
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Create an intent to install the APK
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
// Start the installation
|
|
|
|
|
val installIntent = Intent(Intent.ACTION_VIEW).apply {
|
|
|
|
|
data = uri
|
|
|
|
|
flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or
|
|
|
|
|
Intent.FLAG_ACTIVITY_CLEAR_TOP
|
|
|
|
|
}
|
|
|
|
|
startActivity(installIntent)
|
|
|
|
|
|
|
|
|
|
Log.d("InstallApk", "Install Intent URI: $uri")
|
|
|
|
|
Log.d("InstallApk", "Package Name: $packageName")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onDestroy() {
|
|
|
|
|
super.onDestroy()
|
|
|
|
|
// unregisterReceiver(downloadReceiver)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onResume() {
|
|
|
|
|
@@ -260,5 +245,14 @@ class DashboardActivity : AppCompatActivity(), IDataCollector {
|
|
|
|
|
|
|
|
|
|
responses = responses+response+"\n"
|
|
|
|
|
println(responses)
|
|
|
|
|
if (response.contains("checkUpdate")) {
|
|
|
|
|
hemocubeViewModel.deviceUpdate(createDeviceUpdateRequestData())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun createDeviceUpdateRequestData(): DeviceUpdateRequest {
|
|
|
|
|
return DeviceUpdateRequest(
|
|
|
|
|
serial_no = sharedPreference.getString(Constants.DEVICE_ID, "")
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|