From 5cdc156f33746334fbd5b52b7b34ff9288ee194f Mon Sep 17 00:00:00 2001 From: Mariya Date: Tue, 27 Feb 2024 16:49:25 +0530 Subject: [PATCH] Apk Update Code Added --- app/src/main/AndroidManifest.xml | 1 + .../dashboard/DashboardActivity.kt | 34 +++++++++++++------ app/src/main/res/xml/file_paths.xml | 2 +- gradle/wrapper/gradle-wrapper.properties | 4 +-- 4 files changed, 27 insertions(+), 14 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 6a31cea..fdb4286 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -25,6 +25,7 @@ { // 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() { diff --git a/app/src/main/res/xml/file_paths.xml b/app/src/main/res/xml/file_paths.xml index 72a05d1..f6fa670 100644 --- a/app/src/main/res/xml/file_paths.xml +++ b/app/src/main/res/xml/file_paths.xml @@ -1,3 +1,3 @@ - + diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 6c81b1c..2f0e0a2 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -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