forked from microg/GmsCore
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor SplitInstallManager: structural pt. 2
- Loading branch information
Showing
5 changed files
with
98 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
vending-app/src/main/kotlin/com/android/vending/installer/Constants.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.android.vending.installer | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import kotlinx.coroutines.CompletableDeferred | ||
import java.io.File | ||
|
||
private const val FILE_SAVE_PATH = "phonesky-download-service" | ||
internal const val TAG = "GmsPackageInstaller" | ||
|
||
const val KEY_BYTES_DOWNLOADED = "bytes_downloaded" | ||
|
||
|
||
fun Context.packageDownloadLocation() = File(filesDir, FILE_SAVE_PATH).apply { | ||
if (!exists()) mkdir() | ||
} |
73 changes: 73 additions & 0 deletions
73
vending-app/src/main/kotlin/com/android/vending/installer/Install.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.android.vending.installer | ||
|
||
import android.annotation.SuppressLint | ||
import android.app.PendingIntent | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.content.pm.PackageInfo | ||
import android.content.pm.PackageInstaller | ||
import android.content.pm.PackageInstaller.SessionParams | ||
import android.os.Build | ||
import android.util.Log | ||
import androidx.annotation.RequiresApi | ||
import com.google.android.finsky.splitinstallservice.SplitInstallManager.InstallResultReceiver | ||
import kotlinx.coroutines.CompletableDeferred | ||
import java.io.File | ||
import java.io.FileInputStream | ||
import java.io.IOException | ||
|
||
@RequiresApi(Build.VERSION_CODES.M) | ||
public suspend fun installPackages(context: Context, callingPackage: String, componentFiles: List<File>, isUpdate: Boolean = false, deferredMap: MutableMap<Int, CompletableDeferred<Intent>> = mutableMapOf()): Intent { | ||
Log.v(TAG, "installPackages start") | ||
|
||
val packageInstaller = context.packageManager.packageInstaller | ||
val installed = context.packageManager.getInstalledPackages(0).any { | ||
it.applicationInfo.packageName == callingPackage | ||
} | ||
// Contrary to docs, MODE_INHERIT_EXISTING cannot be used if package is not yet installed. | ||
val params = SessionParams( | ||
if (!installed || isUpdate) SessionParams.MODE_FULL_INSTALL | ||
else SessionParams.MODE_INHERIT_EXISTING | ||
) | ||
params.setAppPackageName(callingPackage) | ||
params.setAppLabel(callingPackage + "Subcontracting") | ||
params.setInstallLocation(PackageInfo.INSTALL_LOCATION_INTERNAL_ONLY) | ||
try { | ||
@SuppressLint("PrivateApi") val method = SessionParams::class.java.getDeclaredMethod( | ||
"setDontKillApp", Boolean::class.javaPrimitiveType | ||
) | ||
method.invoke(params, true) | ||
} catch (e: Exception) { | ||
Log.w(TAG, "Error setting dontKillApp", e) | ||
} | ||
val sessionId: Int | ||
var session: PackageInstaller.Session? = null | ||
var totalDownloaded = 0L | ||
try { | ||
sessionId = packageInstaller.createSession(params) | ||
session = packageInstaller.openSession(sessionId) | ||
componentFiles.forEach { file -> | ||
session.openWrite(file.name, 0, -1).use { outputStream -> | ||
FileInputStream(file).use { inputStream -> inputStream.copyTo(outputStream) } | ||
session.fsync(outputStream) | ||
} | ||
totalDownloaded += file.length() | ||
file.delete() | ||
} | ||
val deferred = CompletableDeferred<Intent>() | ||
deferredMap[sessionId] = deferred | ||
val intent = Intent(context, InstallResultReceiver::class.java).apply { | ||
putExtra(KEY_BYTES_DOWNLOADED, totalDownloaded) | ||
} | ||
val pendingIntent = PendingIntent.getBroadcast(context, sessionId, intent, | ||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE) | ||
session.commit(pendingIntent.intentSender) | ||
Log.d(TAG, "installPackages session commit") | ||
return deferred.await() | ||
} catch (e: IOException) { | ||
Log.w(TAG, "Error installing packages", e) | ||
throw e | ||
} finally { | ||
session?.close() | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...finsky/splitinstallservice/Uninstaller.kt → ...om/android/vending/installer/Uninstall.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters