App update using api added

This commit is contained in:
Mariya
2024-01-04 12:10:20 +05:30
parent b0d348cf6d
commit 18ed6c4e11
2 changed files with 40 additions and 10 deletions

View File

@@ -129,7 +129,7 @@
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="in.sminnovations.hpostesting.preprod.fileprovider"
android:authorities="com.example.hpostesting.preprod.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data

View File

@@ -10,6 +10,7 @@ import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.widget.Toast
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.FileProvider
import androidx.drawerlayout.widget.DrawerLayout
@@ -41,8 +42,8 @@ class DashboardActivity : AppCompatActivity() {
private lateinit var appBarConfiguration: AppBarConfiguration
private lateinit var binding: ActivityDashboardBinding
private var downloadId: Long = 0
@Inject
lateinit var hemocubeViewModel: HemoCubeViewModel
private val hemocubeViewModel: HemoCubeViewModel by viewModels()
override fun attachBaseContext(newBase: Context?) {
val languageCode = LanguageManager.getSavedLanguage(newBase!!)
LanguageManager.setLocale(newBase, languageCode)
@@ -62,17 +63,26 @@ class DashboardActivity : AppCompatActivity() {
// Handle success
val apkUrl = result.data
initiateUpdate(apkUrl)
Log.d("ApI", "APK URL: $apkUrl")
Toast.makeText(
this,
"APK UPLOAD ${result.data}",
Toast.LENGTH_SHORT
).show()
// The APK URL LiveData will be updated automatically
}
is Result.Error -> {
// 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
)