From 18ed6c4e11274714fe32557444789911ec0266f4 Mon Sep 17 00:00:00 2001 From: Mariya Date: Thu, 4 Jan 2024 12:10:20 +0530 Subject: [PATCH] App update using api added --- app/src/main/AndroidManifest.xml | 2 +- .../dashboard/DashboardActivity.kt | 48 +++++++++++++++---- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c5913bd..f2029eb 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -129,7 +129,7 @@ { - // Handle error - val error = result.exception - // Handle error... + result.exception.let { message -> + Toast.makeText(this, "An error occurred: $message", Toast.LENGTH_LONG) + .show() + + } + } is Result.Loading -> { - // Handle loading state + } else -> { @@ -109,8 +119,17 @@ class DashboardActivity : AppCompatActivity() { return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp() } - private fun initiateUpdate(apkUrl: ResponseBody) { - val request = DownloadManager.Request(Uri.parse(apkUrl.toString())) + private fun initiateUpdate(responseBody: ResponseBody) { + // Extract the URL from the ResponseBody + val apkUrl = extractApkUrl(responseBody) + + // Check if the extracted APK URL is valid + if (!isValidHttpUrl(apkUrl)) { + // Handle the case where the URL is not valid + return + } + + val request = DownloadManager.Request(Uri.parse(apkUrl)) request.setTitle("App Update") request.setDescription("Downloading update...") request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED) @@ -124,6 +143,17 @@ class DashboardActivity : AppCompatActivity() { registerReceiver(downloadReceiver, filter) } + // Function to check if a URL has a valid HTTP/HTTPS scheme + // Function to extract the APK URL from the ResponseBody + private fun extractApkUrl(responseBody: ResponseBody): String { + // Assuming your ResponseBody contains the APK URL as a string + return responseBody.string() + } + + // Function to check if a URL has a valid HTTP/HTTPS scheme + 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) @@ -140,7 +170,7 @@ class DashboardActivity : AppCompatActivity() { val uri: Uri = FileProvider.getUriForFile( this, - "in.sminnovations.hpostesting.preprod.fileprovider", + "com.example.hpostesting.preprod.fileprovider", file )