diff --git a/app/src/main/java/com/example/hpostesting/presentation/dashboard/DashboardActivity.kt b/app/src/main/java/com/example/hpostesting/presentation/dashboard/DashboardActivity.kt index 8eb13bc..180a59f 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/dashboard/DashboardActivity.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/dashboard/DashboardActivity.kt @@ -35,7 +35,9 @@ import androidx.navigation.ui.setupActionBarWithNavController import androidx.navigation.ui.setupWithNavController import com.example.hpostesting.util.Result import com.example.hpostesting.data.constant.Constants +import com.example.hpostesting.data.constant.DataHolder import com.example.hpostesting.data.constant.LanguageManager +import com.example.hpostesting.data.model.login.LoginRequest import com.example.hpostesting.data.model.updates.DeviceUpdateRequest import com.example.hpostesting.presentation.utils.NatsManager import com.example.hpostesting.presentation.hemocube.HemoCubeViewModel @@ -49,6 +51,7 @@ import `in`.sminnovations.hpostesting.BuildConfig import `in`.sminnovations.hpostesting.R import `in`.sminnovations.hpostesting.databinding.ActivityDashboardBinding import java.io.File +import java.security.MessageDigest interface NatsMessageCallback { fun onMessageReceived(topic: String, message: String) @@ -106,43 +109,34 @@ class DashboardActivity : AppCompatActivity(), IDataCollector { nats.sub("server.hpos.${deviceId}.ping") nats.pub("server.hpos.${deviceId}.ping", "THIS IS A TEST MSG") - hemocubeViewModel.deviceUpdate.observe(this) { result -> - when (result) { - is Result.Success -> { - // Handle success - val apk = result.data - val file = File(getExternalFilesDir("Updates"), "update.apk") - file.setReadable(true, false) // Ensure the file is readable - apk.byteStream().use { input -> - file.outputStream().use { output -> - input.copyTo(output) - } - } - Log.d("Responsebodyformat", "Responsebodyformat: ") - installApk(file) - Log.e("ApI", "APK URL: $apk") - Toast.makeText( - this, - "${result.data}", - Toast.LENGTH_SHORT - ).show() - } + hemocubeViewModel.deviceUpdate.observe(this) { + Log.d("DashboardLogs",it.toString()) - is Result.Error -> { - result.exception.let { message -> - Toast.makeText(this, "An error occurred On Updating App: $message", Toast.LENGTH_LONG) - .show() - } - } + var apk = it + val file = File(getExternalFilesDir("Downloads"), "update.apk") + file.setReadable(true, false) - is Result.Loading -> { - } - - else -> { + // Write APK to file + apk.byteStream().use { input -> + file.outputStream().use { output -> + input.copyTo(output) } } - } + } + hemocubeViewModel.deviceUpdateheader.observe(this){ + val apkFile = File(getExternalFilesDir("Downloads"), "update.apk") + val expectedChecksum = it.get("Checksum") // Provide your expected checksum here + val algorithm = "SHA-256" // Choose the algorithm you want to use (e.g., MD5, SHA-1, SHA-256) + + val isIntegrityVerified = verifyChecksum(apkFile, expectedChecksum!!, algorithm) + if (isIntegrityVerified) { + println("APK integrity verified: The file has not been tampered with.") + installApk(apkFile) + } else { + println("APK integrity check failed: The file may have been tampered with.") + } + } val drawerLayout: DrawerLayout = binding.drawerLayout val navView: NavigationView = binding.navView @@ -175,7 +169,24 @@ class DashboardActivity : AppCompatActivity(), IDataCollector { return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp() } + fun calculateChecksum(file: File, algorithm: String): String { + val digest = MessageDigest.getInstance(algorithm) + val inputStream = file.inputStream() + val buffer = ByteArray(8192) + var bytesRead = inputStream.read(buffer) + while (bytesRead != -1) { + digest.update(buffer, 0, bytesRead) + bytesRead = inputStream.read(buffer) + } + val checksumBytes = digest.digest() + return checksumBytes.joinToString("") { "%02x".format(it) } + } + // Function to verify checksum + fun verifyChecksum(file: File, expectedChecksum: String, algorithm: String): Boolean { + val actualChecksum = calculateChecksum(file, algorithm) + return actualChecksum.equals(expectedChecksum, ignoreCase = true) + } private fun installApk(file: File) { val pInfo = baseContext.packageManager.getPackageInfo(baseContext.packageName, 0) val uri: Uri = FileProvider.getUriForFile( @@ -284,4 +295,6 @@ class DashboardActivity : AppCompatActivity(), IDataCollector { "N/A" } } + + } \ No newline at end of file diff --git a/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemoCubeViewModel.kt b/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemoCubeViewModel.kt index 4789b1a..d25bb18 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemoCubeViewModel.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemoCubeViewModel.kt @@ -53,6 +53,7 @@ import com.example.hpostesting.domain.LogFileManager import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import okhttp3.Headers import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.MultipartBody import okhttp3.RequestBody.Companion.asRequestBody @@ -93,6 +94,7 @@ class HemoCubeViewModel @Inject constructor( val checkUpdate = MutableLiveData>() val deviceUpdate = MutableLiveData>() + val deviceUpdateheader = MutableLiveData() val downloadcertificate = MutableLiveData>() val uploadLogs = MutableLiveData?>() @@ -261,6 +263,7 @@ class HemoCubeViewModel @Inject constructor( deviceUpdate.postValue(Result.Loading()) repository.deviceUpdate(deviceUpdateRequest).let { deviceUpdate.postValue(it) + deviceUpdateheader.postValue(it.headers()) } } diff --git a/app/src/main/java/com/example/hpostesting/presentation/utils/NatsManager.kt b/app/src/main/java/com/example/hpostesting/presentation/utils/NatsManager.kt index c003799..e294693 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/utils/NatsManager.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/utils/NatsManager.kt @@ -139,7 +139,7 @@ class NatsManager(datacollector: DashboardActivity) { if (nc?.status == Connection.Status.CONNECTED) { Log.d("NATSCONNECTION", "NATS is successfully connected.") nc?.subscribe("device.hpos.${deviceId}.ping") - // Log.d(TAG, "Nats subscribed with ping-"+d) + // Log.d(TAG, "Nats subscribed with ping-"+d) nc?.publish( "server.hpos.${deviceId}.ping", "ALIVE".toByteArray(StandardCharsets.UTF_8)