Skip to content

Commit

Permalink
Add logging and log views on MainScreen.
Browse files Browse the repository at this point in the history
  • Loading branch information
atsushieno committed Apr 21, 2024
1 parent e24c52a commit f3ebc01
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ abstract class ApplicationModel {
var findExistingPackages: (Context) -> List<String> = { listOf() }

var isExistingPackageListReliable: () -> Boolean = { false }

val logger = Logger()
}

object AppModelFactory {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class DownloadAndInstallWorker(context: Context, parameters: WorkerParameters)
downloadAndInstall()
return Result.success()
} catch (ex: Exception) {
Log.e(LOG_TAG, ex.toString())
Log.e(LOG_TAG, "DownloadAndInstallWorker failed: $ex")
return Result.failure()
}
}
Expand Down Expand Up @@ -88,6 +88,6 @@ class DownloadAndInstallWorker(context: Context, parameters: WorkerParameters)
session.commit(pendingIntent.intentSender)
session.close()

Log.d(LOG_TAG, "InstallWorker completed for ${repo.info.appLabel}")
Log.i(LOG_TAG, "InstallWorker completed for ${repo.info.appLabel}")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ object DownloadStatusNotificationManager {

val target = Intent(context, CIPackageInstallerActivity.mainActivityClass)
val bubbleIntent =
PendingIntent.getActivity(context, bubbleRequestCode, target, PendingIntent.FLAG_IMMUTABLE)
PendingIntent.getActivity(context, bubbleRequestCode, target, PendingIntent.FLAG_MUTABLE)

// Create a sharing shortcut.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ class GitHubRepositoryCatalogProvider : RepositoryCatalogProvider() {
.build()
} catch (e: Exception) {
// keep using current github connection
// FIXME: there should be some way to notify user that the authentication token is invalid.
Log.e(AppModel.LOG_TAG, "GitHub authentication failed. Error details are being dumped.")
Log.e(AppModel.LOG_TAG, e.toString())
AppModel.logger.logError("GitHub authentication failed: ${e.message}", e)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package dev.atsushieno.cipackageinstaller

import android.util.Log
import androidx.compose.runtime.mutableStateListOf

// Collects log entries that should be shown to the user
class Logger {
class LogEntry(val text: String, val artifact: ApplicationArtifact? = null)

val logs = mutableStateListOf<LogEntry>()

fun logError(message: String, ex: Exception? = null, artifact: ApplicationArtifact? = null) {
logs.add(LogEntry(message, artifact))
Log.e(AppModel.LOG_TAG, message)
if (ex != null)
Log.e(AppModel.LOG_TAG, ex.toString())
}

fun logInfo(message: String, artifact: ApplicationArtifact? = null) {
logs.add(LogEntry(message, artifact))
Log.i(AppModel.LOG_TAG, message)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class PackageInstallerReceiver : BroadcastReceiver() {
}
else -> {
val message = intent.getStringExtra(PackageInstaller.EXTRA_STATUS_MESSAGE)
Log.e(AppModel.LOG_TAG, "Installation result: $message")
AppModel.logger.logError("Installation result: $message")
val sessionId = intent.getIntExtra(PackageInstaller.EXTRA_SESSION_ID, 0)
if (sessionId > 0) {
try {
Expand All @@ -34,9 +34,7 @@ class PackageInstallerReceiver : BroadcastReceiver() {
context.packageManager.packageInstaller.abandonSession(sessionId)
} catch (ex: RuntimeException) {
// otherwise, "java.lang.SecurityException: Caller has no access to session"
Log.e(AppModel.LOG_TAG,
"Attempt to abandon the session $sessionId caused an exception: $ex.message"
)
AppModel.logger.logError("Attempt to abandon the session $sessionId caused an exception: ${ex.message}", ex)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ class PreapprovalReceiver : BroadcastReceiver() {
context.startActivity(i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
}
PackageInstaller.STATUS_SUCCESS -> {
Log.d(AppModel.LOG_TAG, "Preapproval succeeded!")
Log.d(AppModel.LOG_TAG, "Preapproval succeeded")
}
else -> {
val message = intent.getStringExtra(PackageInstaller.EXTRA_STATUS_MESSAGE)
Log.e(AppModel.LOG_TAG, "Preapproval result: $message")
val details = intent.getParcelableExtra(PackageInstaller.EXTRA_PRE_APPROVAL, PackageInstaller.PreapprovalDetails::class.java)
AppModel.logger.logError("Preapproval failed: $message (${details?.packageName}")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ fun MainScreen(onItemClicked: (repo: Int) -> Unit) {
Column {
GitHubUserCredentialsConfigUI()
PermissionRequester()
OperationLogViewer()
LazyColumn(content = {
AppModel.applicationStore.repositories.forEachIndexed { index, repo ->
item {
Expand All @@ -51,6 +52,35 @@ fun MainScreen(onItemClicked: (repo: Int) -> Unit) {
}
}

@Composable
fun OperationLogViewer() {
val hasSomeLogText = "There are some operation logs, tap to view"

val logs = remember { AppModel.logger.logs }

if (logs.isEmpty())
return

var toggleViewerState by remember { mutableStateOf(false) }
Column(modifier = Modifier.border(2.dp, color = Color.Gray)) {
Row(modifier = Modifier
.padding(4.dp)
.clickable { toggleViewerState = !toggleViewerState }) {
Text(hasSomeLogText)
}
if (toggleViewerState) {
LazyColumn {
items(logs.size) { index ->
val entry = logs[index]
if (entry.artifact != null)
Text(entry.artifact.artifactName, fontSize = 12.sp)
Text(entry.text)
}
}
}
}
}

@Composable
fun GitHubUserCredentialsConfigUI() {
val context = LocalContext.current
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fun RepositoryDetailsContent(navController: NavController, index: Int) {
try {
repoState = repoInfo.createRepository()
} catch (ex: CIPackageInstallerException) {
Log.e(AppModel.LOG_TAG, "Failed to retrieve repository data", ex)
AppModel.logger.logError("Failed to retrieve repository data: ${ex.message}", ex)
Dispatchers.Main.dispatch(coroutineScope.coroutineContext) {
navController.navigate(Routes.Home.route) { popUpTo(Routes.Home.route) }
Toast.makeText(context, ex.message, Toast.LENGTH_LONG).show()
Expand Down Expand Up @@ -87,14 +87,7 @@ fun RepositoryDetailsContent(navController: NavController, index: Int) {
Toast.makeText(context, "Downloading ${repoInfo.name} ...", Toast.LENGTH_LONG).show()
}
Dispatchers.IO.dispatch(coroutineScope.coroutineContext) {
try {
AppModel.performDownloadAndInstallation(context, variant)
} catch (ex: CIPackageInstallerException) {
Log.e(AppModel.LOG_TAG, "Failed to retrieve repository data", ex)
Dispatchers.Main.dispatch(coroutineScope.coroutineContext) {
Toast.makeText(context, ex.message, Toast.LENGTH_LONG).show()
}
}
AppModel.performDownloadAndInstallation(context, variant)
}
}) {
Text(if (alreadyExists) "Download and Update" else "Download and Install")
Expand All @@ -112,7 +105,8 @@ fun RepositoryDetailsContent(navController: NavController, index: Int) {
try {
AppModel.performUninstallPackage(context, repo)
} catch (ex: CIPackageInstallerException) {
Log.e(AppModel.LOG_TAG, "Failed to retrieve repository data", ex)
AppModel.logger.logError("Failed to retrieve repository data: ${ex.message}", ex)
Log.e(AppModel.LOG_TAG, "Failed to retrieve repository data: $ex")
Dispatchers.Main.dispatch(coroutineScope.coroutineContext) {
Toast.makeText(context, ex.message, Toast.LENGTH_LONG).show()
}
Expand Down

0 comments on commit f3ebc01

Please sign in to comment.