feat: implement PackageInstaller session API for streamlined in-app updates and bump to v1.0.2
This commit is contained in:
@@ -47,8 +47,13 @@ jobs:
|
|||||||
elif [[ "${{ github.ref }}" == refs/tags/v* ]]; then
|
elif [[ "${{ github.ref }}" == refs/tags/v* ]]; then
|
||||||
TAG="${GITHUB_REF#refs/tags/}"
|
TAG="${GITHUB_REF#refs/tags/}"
|
||||||
else
|
else
|
||||||
# On pushes to main, generate a version based on run number
|
# Extract versionName from app/build.gradle.kts
|
||||||
TAG="v1.0.${{ github.run_number }}"
|
APP_VERSION=$(grep 'versionName = ' app/build.gradle.kts | head -n 1 | cut -d '"' -f 2)
|
||||||
|
if [ -n "$APP_VERSION" ]; then
|
||||||
|
TAG="v$APP_VERSION"
|
||||||
|
else
|
||||||
|
TAG="v1.0.${{ github.run_number }}"
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
echo "TAG=$TAG" >> $GITHUB_OUTPUT
|
echo "TAG=$TAG" >> $GITHUB_OUTPUT
|
||||||
echo "Release tag will be $TAG"
|
echo "Release tag will be $TAG"
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ android {
|
|||||||
applicationId = "com.expapp.trigger"
|
applicationId = "com.expapp.trigger"
|
||||||
minSdk = 24
|
minSdk = 24
|
||||||
targetSdk = 34
|
targetSdk = 34
|
||||||
versionCode = 1
|
versionCode = 2
|
||||||
versionName = "1.0"
|
versionName = "1.0.2"
|
||||||
|
|
||||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||||
vectorDrawables {
|
vectorDrawables {
|
||||||
|
|||||||
@@ -31,6 +31,10 @@
|
|||||||
android:name="android.support.FILE_PROVIDER_PATHS"
|
android:name="android.support.FILE_PROVIDER_PATHS"
|
||||||
android:resource="@xml/file_paths" />
|
android:resource="@xml/file_paths" />
|
||||||
</provider>
|
</provider>
|
||||||
|
|
||||||
|
<receiver
|
||||||
|
android:name=".update.InstallStatusReceiver"
|
||||||
|
android:exported="false" />
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
</manifest>
|
</manifest>
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package com.expapp.trigger.update
|
||||||
|
|
||||||
|
import android.content.BroadcastReceiver
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import android.content.pm.PackageInstaller
|
||||||
|
import android.os.Build
|
||||||
|
import android.widget.Toast
|
||||||
|
|
||||||
|
class InstallStatusReceiver : BroadcastReceiver() {
|
||||||
|
override fun onReceive(context: Context, intent: Intent) {
|
||||||
|
val status = intent.getIntExtra(PackageInstaller.EXTRA_STATUS, PackageInstaller.STATUS_FAILURE)
|
||||||
|
when (status) {
|
||||||
|
PackageInstaller.STATUS_PENDING_USER_ACTION -> {
|
||||||
|
val confirmationIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||||
|
intent.getParcelableExtra(Intent.EXTRA_INTENT, Intent::class.java)
|
||||||
|
} else {
|
||||||
|
@Suppress("DEPRECATION")
|
||||||
|
intent.getParcelableExtra(Intent.EXTRA_INTENT)
|
||||||
|
}
|
||||||
|
if (confirmationIntent != null) {
|
||||||
|
confirmationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||||
|
context.startActivity(confirmationIntent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
PackageInstaller.STATUS_SUCCESS -> {
|
||||||
|
Toast.makeText(context, "Update installed successfully!", Toast.LENGTH_SHORT).show()
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
val message = intent.getStringExtra(PackageInstaller.EXTRA_STATUS_MESSAGE)
|
||||||
|
if (!message.isNullOrBlank()) {
|
||||||
|
Toast.makeText(context, "Update: $message", Toast.LENGTH_SHORT).show()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,9 @@
|
|||||||
package com.expapp.trigger.update
|
package com.expapp.trigger.update
|
||||||
|
|
||||||
|
import android.app.PendingIntent
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
|
import android.content.pm.PackageInstaller
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.Handler
|
import android.os.Handler
|
||||||
@@ -251,17 +253,60 @@ class UpdateManager(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val apkUri = FileProvider.getUriForFile(
|
try {
|
||||||
context,
|
val packageInstaller = context.packageManager.packageInstaller
|
||||||
"${context.packageName}.fileprovider",
|
val params = PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL).apply {
|
||||||
apkFile
|
setAppPackageName(context.packageName)
|
||||||
)
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
||||||
|
setRequireUserAction(PackageInstaller.SessionParams.USER_ACTION_NOT_REQUIRED)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
val installIntent = Intent(Intent.ACTION_VIEW).apply {
|
val sessionId = packageInstaller.createSession(params)
|
||||||
setDataAndType(apkUri, "application/vnd.android.package-archive")
|
val session = packageInstaller.openSession(sessionId)
|
||||||
flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_ACTIVITY_NEW_TASK
|
|
||||||
|
apkFile.inputStream().use { input ->
|
||||||
|
session.openWrite("package", 0, apkFile.length()).use { output ->
|
||||||
|
input.copyTo(output)
|
||||||
|
session.fsync(output)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val flags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
||||||
|
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
|
||||||
|
} else {
|
||||||
|
PendingIntent.FLAG_UPDATE_CURRENT
|
||||||
|
}
|
||||||
|
val receiverIntent = Intent(context, InstallStatusReceiver::class.java)
|
||||||
|
val pendingIntent = PendingIntent.getBroadcast(
|
||||||
|
context,
|
||||||
|
sessionId,
|
||||||
|
receiverIntent,
|
||||||
|
flags
|
||||||
|
)
|
||||||
|
|
||||||
|
session.commit(pendingIntent.intentSender)
|
||||||
|
session.close()
|
||||||
|
} catch (e: Exception) {
|
||||||
|
// Fallback to FileProvider VIEW intent if session API is unsupported by vendor ROM
|
||||||
|
fallbackInstall(apkFile)
|
||||||
}
|
}
|
||||||
context.startActivity(installIntent)
|
}
|
||||||
|
|
||||||
|
private fun fallbackInstall(apkFile: File) {
|
||||||
|
try {
|
||||||
|
val apkUri = FileProvider.getUriForFile(
|
||||||
|
context,
|
||||||
|
"${context.packageName}.fileprovider",
|
||||||
|
apkFile
|
||||||
|
)
|
||||||
|
|
||||||
|
val installIntent = Intent(Intent.ACTION_VIEW).apply {
|
||||||
|
setDataAndType(apkUri, "application/vnd.android.package-archive")
|
||||||
|
flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_ACTIVITY_NEW_TASK
|
||||||
|
}
|
||||||
|
context.startActivity(installIntent)
|
||||||
|
} catch (_: Exception) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|||||||
Reference in New Issue
Block a user