Apk Update Code Added
This commit is contained in:
@@ -25,6 +25,7 @@
|
||||
|
||||
<application
|
||||
android:name="com.example.hpostesting.HPOSTestingApplication"
|
||||
android:largeHeap="true"
|
||||
android:allowBackup="true"
|
||||
android:dataExtractionRules="@xml/data_extraction_rules"
|
||||
android:fullBackupContent="@xml/backup_rules"
|
||||
|
||||
@@ -30,6 +30,7 @@ import com.google.firebase.appdistribution.FirebaseAppDistribution
|
||||
import com.google.firebase.appdistribution.FirebaseAppDistributionException
|
||||
import com.google.firebase.crashlytics.FirebaseCrashlytics
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import `in`.sminnovations.hpostesting.BuildConfig
|
||||
import `in`.sminnovations.hpostesting.R
|
||||
import `in`.sminnovations.hpostesting.databinding.ActivityDashboardBinding
|
||||
import okhttp3.ResponseBody
|
||||
@@ -92,7 +93,18 @@ class DashboardActivity : AppCompatActivity(), IDataCollector {
|
||||
is Result.Success -> {
|
||||
// Handle success
|
||||
val apkUrl = result.data
|
||||
downloadApk(apkUrl)
|
||||
val responseBodyString = apkUrl.string()
|
||||
|
||||
|
||||
val fileToWriteTo = File(this.getExternalFilesDir(null), "downloaded_file.apk")
|
||||
|
||||
apkUrl.byteStream().use { inputStream ->
|
||||
FileOutputStream(fileToWriteTo).use { outputStream ->
|
||||
inputStream.copyTo(outputStream)
|
||||
}
|
||||
}
|
||||
Log.d(TAG, "Responsebody: $responseBodyString")
|
||||
installApk(fileToWriteTo)
|
||||
Log.e("ApI", "APK URL: $apkUrl")
|
||||
Toast.makeText(
|
||||
this,
|
||||
@@ -167,7 +179,6 @@ class DashboardActivity : AppCompatActivity(), IDataCollector {
|
||||
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 {
|
||||
@@ -176,19 +187,20 @@ class DashboardActivity : AppCompatActivity(), IDataCollector {
|
||||
}
|
||||
}
|
||||
|
||||
private fun installApk(file: File) {
|
||||
val uri: Uri = FileProvider.getUriForFile(
|
||||
private fun installApk(fileToWriteTo: File) {
|
||||
val apkUri = FileProvider.getUriForFile(
|
||||
this,
|
||||
"${applicationContext.packageName}.provider", // Update this with your FileProvider authority
|
||||
file
|
||||
"${BuildConfig.APPLICATION_ID}.provider",
|
||||
fileToWriteTo
|
||||
)
|
||||
|
||||
val installIntent = Intent(Intent.ACTION_VIEW).apply {
|
||||
data = uri
|
||||
flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or
|
||||
Intent.FLAG_ACTIVITY_CLEAR_TOP
|
||||
|
||||
val intent = Intent(Intent.ACTION_VIEW).apply {
|
||||
setDataAndType(apkUri, "application/vnd.android.package-archive")
|
||||
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_GRANT_READ_URI_PERMISSION
|
||||
}
|
||||
startActivity(installIntent)
|
||||
startActivity(intent)
|
||||
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
<paths xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<external-files-path name="Update" path="." />
|
||||
<external-files-path name="downloaded_file" path="." />
|
||||
</paths>
|
||||
|
||||
4
gradle/wrapper/gradle-wrapper.properties
vendored
4
gradle/wrapper/gradle-wrapper.properties
vendored
@@ -1,6 +1,6 @@
|
||||
#Mon Jun 12 17:07:47 IST 2023
|
||||
#Tue Feb 27 16:09:58 IST 2024
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
|
||||
Reference in New Issue
Block a user