App update using api added
This commit is contained in:
@@ -129,7 +129,7 @@
|
|||||||
|
|
||||||
<provider
|
<provider
|
||||||
android:name="androidx.core.content.FileProvider"
|
android:name="androidx.core.content.FileProvider"
|
||||||
android:authorities="in.sminnovations.hpostesting.preprod.fileprovider"
|
android:authorities="com.example.hpostesting.preprod.fileprovider"
|
||||||
android:exported="false"
|
android:exported="false"
|
||||||
android:grantUriPermissions="true">
|
android:grantUriPermissions="true">
|
||||||
<meta-data
|
<meta-data
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import android.os.Bundle
|
|||||||
import android.util.Log
|
import android.util.Log
|
||||||
import android.view.Menu
|
import android.view.Menu
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
|
import androidx.activity.viewModels
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.core.content.FileProvider
|
import androidx.core.content.FileProvider
|
||||||
import androidx.drawerlayout.widget.DrawerLayout
|
import androidx.drawerlayout.widget.DrawerLayout
|
||||||
@@ -41,8 +42,8 @@ class DashboardActivity : AppCompatActivity() {
|
|||||||
private lateinit var appBarConfiguration: AppBarConfiguration
|
private lateinit var appBarConfiguration: AppBarConfiguration
|
||||||
private lateinit var binding: ActivityDashboardBinding
|
private lateinit var binding: ActivityDashboardBinding
|
||||||
private var downloadId: Long = 0
|
private var downloadId: Long = 0
|
||||||
@Inject
|
private val hemocubeViewModel: HemoCubeViewModel by viewModels()
|
||||||
lateinit var hemocubeViewModel: HemoCubeViewModel
|
|
||||||
override fun attachBaseContext(newBase: Context?) {
|
override fun attachBaseContext(newBase: Context?) {
|
||||||
val languageCode = LanguageManager.getSavedLanguage(newBase!!)
|
val languageCode = LanguageManager.getSavedLanguage(newBase!!)
|
||||||
LanguageManager.setLocale(newBase, languageCode)
|
LanguageManager.setLocale(newBase, languageCode)
|
||||||
@@ -62,17 +63,26 @@ class DashboardActivity : AppCompatActivity() {
|
|||||||
// Handle success
|
// Handle success
|
||||||
val apkUrl = result.data
|
val apkUrl = result.data
|
||||||
initiateUpdate(apkUrl)
|
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
|
// The APK URL LiveData will be updated automatically
|
||||||
}
|
}
|
||||||
|
|
||||||
is Result.Error -> {
|
is Result.Error -> {
|
||||||
// Handle error
|
result.exception.let { message ->
|
||||||
val error = result.exception
|
Toast.makeText(this, "An error occurred: $message", Toast.LENGTH_LONG)
|
||||||
// Handle error...
|
.show()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
is Result.Loading -> {
|
is Result.Loading -> {
|
||||||
// Handle loading state
|
|
||||||
}
|
}
|
||||||
|
|
||||||
else -> {
|
else -> {
|
||||||
@@ -109,8 +119,17 @@ class DashboardActivity : AppCompatActivity() {
|
|||||||
return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
|
return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun initiateUpdate(apkUrl: ResponseBody) {
|
private fun initiateUpdate(responseBody: ResponseBody) {
|
||||||
val request = DownloadManager.Request(Uri.parse(apkUrl.toString()))
|
// 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.setTitle("App Update")
|
||||||
request.setDescription("Downloading update...")
|
request.setDescription("Downloading update...")
|
||||||
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
|
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
|
||||||
@@ -124,6 +143,17 @@ class DashboardActivity : AppCompatActivity() {
|
|||||||
registerReceiver(downloadReceiver, filter)
|
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() {
|
private val downloadReceiver = object : BroadcastReceiver() {
|
||||||
override fun onReceive(context: Context?, intent: Intent?) {
|
override fun onReceive(context: Context?, intent: Intent?) {
|
||||||
val id = intent?.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1)
|
val id = intent?.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1)
|
||||||
@@ -140,7 +170,7 @@ class DashboardActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
val uri: Uri = FileProvider.getUriForFile(
|
val uri: Uri = FileProvider.getUriForFile(
|
||||||
this,
|
this,
|
||||||
"in.sminnovations.hpostesting.preprod.fileprovider",
|
"com.example.hpostesting.preprod.fileprovider",
|
||||||
file
|
file
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user