Skip to content

Commit 492b474

Browse files
Split Install: Restore from broken session (#3040)
Co-authored-by: Marvin W <[email protected]>
1 parent f3e8998 commit 492b474

File tree

1 file changed

+11
-4
lines changed
  • vending-app/src/main/kotlin/com/android/vending/installer

1 file changed

+11
-4
lines changed

vending-app/src/main/kotlin/com/android/vending/installer/Install.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ private suspend fun Context.installPackagesInternal(
126126
} catch (e: Exception) {
127127
Log.w(TAG, "Error setting dontKillApp", e)
128128
}
129-
val sessionId: Int
129+
var sessionId: Int
130130
var session: PackageInstaller.Session? = null
131131
try {
132132
val sessionInfo = packageInstaller.mySessions.firstOrNull{ it.appLabel == key }
@@ -147,7 +147,7 @@ private suspend fun Context.installPackagesInternal(
147147

148148
for (component in componentNames) {
149149
val currentSize: Long = try {
150-
val inputStream = session.openRead(component)
150+
val inputStream = session!!.openRead(component)
151151
val totalSize = withContext(Dispatchers.IO) {
152152
val buffer = ByteArray(4096)
153153
var total = 0L
@@ -162,10 +162,17 @@ private suspend fun Context.installPackagesInternal(
162162
} catch (e: IOException) {
163163
Log.w(TAG, "installPackagesInternal session open read error, ${e.message}")
164164
0L
165+
} catch (e: SecurityException) {
166+
Log.w(TAG, "installPackagesInternal session open read error, ${e.message}")
167+
//Handling exceptions java.lang.SecurityException: openRead not allowed after commit
168+
session!!.abandon()
169+
sessionId = packageInstaller.createSession(params)
170+
session = packageInstaller.openSession(sessionId)
171+
0L
165172
}
166173

167174
Log.d(TAG, "installPackagesInternal component: $component currentSize:$currentSize")
168-
session.openWrite(component, currentSize, -1).use { outputStream ->
175+
session!!.openWrite(component, currentSize, -1).use { outputStream ->
169176
try {
170177
writeComponent(currentSize, notifyId, component, outputStream)
171178
session.fsync(outputStream)
@@ -197,7 +204,7 @@ private suspend fun Context.installPackagesInternal(
197204
)!!
198205

199206
emitProgress(notifyId, CommitingSession)
200-
session.commit(pendingIntent.intentSender)
207+
session!!.commit(pendingIntent.intentSender)
201208
// don't abandon if `finally` step is reached after this point
202209
//session.close()
203210

0 commit comments

Comments
 (0)