Skip to content

Commit

Permalink
refactor some code to eliminate println().
Browse files Browse the repository at this point in the history
  • Loading branch information
atsushieno committed Apr 17, 2024
1 parent 1527cf7 commit 48d6b59
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.atsushieno.cipackageinstaller

import android.annotation.SuppressLint
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
Expand Down Expand Up @@ -46,7 +45,7 @@ abstract class ApplicationStore(val referrer: String) {

object AppModel {
const val LOG_TAG: String = "CIPackageInstaller"
private const val PENDING_INTENT_REQEST_CODE = 1
private const val PENDING_INTENT_REQUEST_CODE = 1
private const val PENDING_PREAPPROVAL_REQUEST_CODE = 2

private const val FILE_APK_PROVIDER_AUTHORITY_SUFFIX = ".fileprovider"
Expand Down Expand Up @@ -98,20 +97,22 @@ object AppModel {
}
}

fun performInstallPackage(context: Context, download: ApplicationArtifact) {
// Process permissions and then download and launch pending installation intent
// (that may involve user interaction).
fun performDownloadAndInstallation(context: Context, download: ApplicationArtifact) {
val repo = download.repository
val installer = context.packageManager.packageInstaller

val existing = installer.mySessions.firstOrNull { it.appPackageName == repo.info.packageName && !it.isActive }
if (existing != null)
// It seems better to abandon existing session and restart than throwing, when we perform install->uninstall->install...
// abandon existing session and restart than throwing, when we perform install->uninstall->install...
installer.openSession(existing.sessionId).abandon()
//throw CIPackageInstallerException("Another operation for the package '${repo.info.packageName}' is in progress. Please wait for its completion.")

val params = download.toPackageInstallerSessionParams()
val sessionId = installer.createSession(params)
val session = installer.openSession(sessionId)

// Pre-approval is available only in Android 14 or later.
if (preApprovalEnabled) {
val preapprovalIntent = Intent(context, PreapprovalReceiver::class.java)
val preapprovalPendingIntent = PendingIntent.getBroadcast(context,
Expand All @@ -132,7 +133,7 @@ object AppModel {
outStream.close()

val intent = Intent(context, PackageInstallerReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(context, PENDING_INTENT_REQEST_CODE,
val pendingIntent = PendingIntent.getBroadcast(context, PENDING_INTENT_REQUEST_CODE,
intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE)
Log.d(LOG_TAG, "ready to install ${repo.info.appLabel} ...")
session.commit(pendingIntent.intentSender)
Expand All @@ -142,7 +143,7 @@ object AppModel {
fun performUninstallPackage(context: Context, repo: Repository) {
val installer = context.packageManager.packageInstaller
val intent = Intent(context, PackageInstallerReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(context, PENDING_INTENT_REQEST_CODE,
val pendingIntent = PendingIntent.getBroadcast(context, PENDING_INTENT_REQUEST_CODE,
intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE)
installer.uninstall(repo.info.packageName, pendingIntent.intentSender)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.content.pm.PackageInstaller
import android.graphics.Bitmap
import android.net.Uri
import android.os.Build
import android.util.Log
import okhttp3.OkHttpClient
import okhttp3.Request
import org.kohsuke.github.GHArtifact
Expand Down Expand Up @@ -88,7 +89,7 @@ class GitHubArtifactApplicationArtifact internal constructor(repository: GitHubR
if (entry != null) {
val tmpAppFile = File.createTempFile("GHTempApp", "." + File(entry.name).extension) // apk or aab

println("Downloading ${entry.name} ...")
Log.d(AppModel.LOG_TAG, "Downloading ${entry.name} ...")
val inAppStream = zipFile.getInputStream(entry)
AppModel.copyStream(inAppStream, tmpAppFile)
if (!tmpAppFile.exists() || tmpAppFile.length() != entry.size)
Expand Down Expand Up @@ -116,18 +117,7 @@ class GitHubArtifactApplicationArtifact internal constructor(repository: GitHubR
.firstOrNull { r -> r.listArtifacts().toArray().any { a -> !a.isExpired } }
?: throw CIPackageInstallerException("GitHub repository $repoName does not have any workflow runs yet")
workflowRun = run

println("---- Workflow Run ----")
println(run.headCommit.id)
println(run.url)

artifact = run.listArtifacts().toArray().first()

println("---- artifact ----")
println(artifact.name)
println(artifact.createdAt)
println(artifact.sizeInBytes)
println(artifact.archiveDownloadUrl)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import android.os.Build
import android.util.Log
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.scrollable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
Expand Down Expand Up @@ -87,7 +85,7 @@ fun RepositoryDetailsContent(navController: NavController, index: Int) {
}
Dispatchers.IO.dispatch(coroutineScope.coroutineContext) {
try {
AppModel.performInstallPackage(context, variant)
AppModel.performDownloadAndInstallation(context, variant)
} catch (ex: CIPackageInstallerException) {
Log.e(AppModel.LOG_TAG, "Failed to retrieve repository data", ex)
Dispatchers.Main.dispatch(coroutineScope.coroutineContext) {
Expand Down

0 comments on commit 48d6b59

Please sign in to comment.