Improve event data classes
This commit is contained in:
@@ -21,53 +21,27 @@ package com.aurora.store.data.event
|
||||
|
||||
abstract class Event
|
||||
|
||||
sealed class BusEvent: Event() {
|
||||
data class InstallEvent(
|
||||
var packageName: String,
|
||||
var extra: String? = ""
|
||||
) : BusEvent()
|
||||
sealed class BusEvent : Event() {
|
||||
lateinit var extra: String
|
||||
lateinit var error: String
|
||||
|
||||
data class UninstallEvent(
|
||||
var packageName: String,
|
||||
var extra: String? = ""
|
||||
) : BusEvent()
|
||||
|
||||
data class Blacklisted(
|
||||
var packageName: String,
|
||||
var error: String? = ""
|
||||
) : BusEvent()
|
||||
|
||||
data class GoogleAAS(
|
||||
var success: Boolean,
|
||||
var email: String = String(),
|
||||
var aasToken: String = String()
|
||||
) : BusEvent()
|
||||
|
||||
data class ManualDownload(
|
||||
var packageName: String,
|
||||
var versionCode: Int
|
||||
) : BusEvent()
|
||||
data class Blacklisted(val packageName: String) : BusEvent()
|
||||
data class ManualDownload(val packageName: String, val versionCode: Int) : BusEvent()
|
||||
}
|
||||
|
||||
sealed class InstallerEvent: Event() {
|
||||
data class Success(
|
||||
var packageName: String? = "",
|
||||
var extra: String? = ""
|
||||
) : InstallerEvent()
|
||||
|
||||
data class Installing(
|
||||
var packageName: String? = "",
|
||||
var progress: Int = 0
|
||||
) : InstallerEvent()
|
||||
|
||||
data class Cancelled(
|
||||
var packageName: String? = "",
|
||||
var extra: String? = ""
|
||||
) : InstallerEvent()
|
||||
|
||||
data class Failed(
|
||||
var packageName: String? = "",
|
||||
var error: String? = "",
|
||||
var extra: String? = ""
|
||||
) : InstallerEvent()
|
||||
sealed class AuthEvent : Event() {
|
||||
data class GoogleLogin(val success: Boolean, val email: String, val token: String) : AuthEvent()
|
||||
}
|
||||
|
||||
sealed class InstallerEvent : Event() {
|
||||
lateinit var extra: String
|
||||
lateinit var error: String
|
||||
|
||||
var progress: Int = -1
|
||||
|
||||
data class Installed(val packageName: String) : InstallerEvent()
|
||||
data class Uninstalled(val packageName: String) : InstallerEvent()
|
||||
data class Installing(val packageName: String) : InstallerEvent()
|
||||
data class Cancelled(val packageName: String) : InstallerEvent()
|
||||
data class Failed(val packageName: String) : InstallerEvent()
|
||||
}
|
||||
|
||||
@@ -16,10 +16,14 @@ class FlowEvent {
|
||||
private val _installerEvent = MutableSharedFlow<InstallerEvent>(extraBufferCapacity = 1)
|
||||
val installerEvent = _installerEvent.asSharedFlow()
|
||||
|
||||
private val _authEvent = MutableSharedFlow<AuthEvent>(extraBufferCapacity = 1)
|
||||
val authEvent = _authEvent.asSharedFlow()
|
||||
|
||||
fun emitEvent(event: Event) {
|
||||
when (event) {
|
||||
is InstallerEvent -> _installerEvent.tryEmit(event)
|
||||
is BusEvent -> _busEvent.tryEmit(event)
|
||||
is AuthEvent -> _authEvent.tryEmit(event)
|
||||
else -> Log.e(TAG, "Got an unhandled event")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,16 +66,19 @@ abstract class InstallerBase(protected var context: Context) : IInstaller {
|
||||
open fun postError(packageName: String, error: String?, extra: String?) {
|
||||
Log.e("Service Error :$error")
|
||||
|
||||
val event = InstallerEvent.Failed(
|
||||
packageName,
|
||||
error,
|
||||
extra
|
||||
)
|
||||
val event = InstallerEvent.Failed(packageName).apply {
|
||||
this.error = error ?: ""
|
||||
this.extra = extra ?: ""
|
||||
}
|
||||
|
||||
AuroraApp.flowEvent.emitEvent(event)
|
||||
}
|
||||
|
||||
open fun getFiles(packageName: String, versionCode: Int, sharedLibPackageName: String = ""): List<File> {
|
||||
open fun getFiles(
|
||||
packageName: String,
|
||||
versionCode: Int,
|
||||
sharedLibPackageName: String = ""
|
||||
): List<File> {
|
||||
val downloadDir = if (sharedLibPackageName.isNotBlank()) {
|
||||
PathUtil.getLibDownloadDir(context, packageName, versionCode, sharedLibPackageName)
|
||||
} else {
|
||||
|
||||
@@ -104,11 +104,10 @@ class RootInstaller @Inject constructor(
|
||||
if (packageName == download?.packageName) onInstallationSuccess()
|
||||
} else {
|
||||
removeFromInstallQueue(packageName)
|
||||
val event = InstallerEvent.Failed(
|
||||
packageName,
|
||||
context.getString(R.string.installer_status_failure),
|
||||
parseError(shellResult)
|
||||
)
|
||||
val event = InstallerEvent.Failed(packageName).apply {
|
||||
this.extra = context.getString(R.string.installer_status_failure)
|
||||
this.error = parseError(shellResult)
|
||||
}
|
||||
AuroraApp.flowEvent.emitEvent(event)
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -35,7 +35,6 @@ import com.aurora.services.IPrivilegedService
|
||||
import com.aurora.store.AuroraApp
|
||||
import com.aurora.store.BuildConfig
|
||||
import com.aurora.store.R
|
||||
import com.aurora.store.data.event.BusEvent
|
||||
import com.aurora.store.data.event.InstallerEvent
|
||||
import com.aurora.store.data.model.InstallerInfo
|
||||
import com.aurora.store.data.room.download.Download
|
||||
@@ -91,6 +90,7 @@ class ServiceInstaller @Inject constructor(
|
||||
fileList.map { it.absolutePath }
|
||||
)
|
||||
}
|
||||
|
||||
else -> {
|
||||
postError(
|
||||
download.packageName,
|
||||
@@ -148,7 +148,11 @@ class ServiceInstaller @Inject constructor(
|
||||
)
|
||||
} catch (e: RemoteException) {
|
||||
removeFromInstallQueue(packageName)
|
||||
postError(packageName, e.localizedMessage, e.stackTraceToString())
|
||||
postError(
|
||||
packageName,
|
||||
e.localizedMessage,
|
||||
e.stackTraceToString()
|
||||
)
|
||||
readyWithAction.set(true)
|
||||
}
|
||||
} else {
|
||||
@@ -166,7 +170,11 @@ class ServiceInstaller @Inject constructor(
|
||||
)
|
||||
} catch (e: RemoteException) {
|
||||
removeFromInstallQueue(packageName)
|
||||
postError(packageName, e.localizedMessage, e.stackTraceToString())
|
||||
postError(
|
||||
packageName,
|
||||
e.localizedMessage,
|
||||
e.stackTraceToString()
|
||||
)
|
||||
readyWithAction.set(true)
|
||||
}
|
||||
}
|
||||
@@ -211,12 +219,12 @@ class ServiceInstaller @Inject constructor(
|
||||
when (returnCode) {
|
||||
PackageInstaller.STATUS_SUCCESS -> {
|
||||
AuroraApp.flowEvent.emitEvent(
|
||||
BusEvent.UninstallEvent(
|
||||
packageName,
|
||||
context.getString(R.string.installer_status_success)
|
||||
)
|
||||
InstallerEvent.Uninstalled(packageName).apply {
|
||||
this.extra = context.getString(R.string.action_uninstall_success)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
else -> {
|
||||
val error = AppInstaller.getErrorString(
|
||||
context,
|
||||
@@ -241,14 +249,14 @@ class ServiceInstaller @Inject constructor(
|
||||
when (returnCode) {
|
||||
PackageInstaller.STATUS_SUCCESS -> {
|
||||
AuroraApp.flowEvent.emitEvent(
|
||||
InstallerEvent.Success(
|
||||
packageName,
|
||||
context.getString(R.string.installer_status_success)
|
||||
)
|
||||
InstallerEvent.Installed(packageName).apply {
|
||||
this.extra = context.getString(R.string.installer_status_success)
|
||||
}
|
||||
)
|
||||
// Installation is not yet finished if this is a shared library
|
||||
if (packageName == download?.packageName) onInstallationSuccess()
|
||||
}
|
||||
|
||||
else -> {
|
||||
val error = AppInstaller.getErrorString(
|
||||
context,
|
||||
|
||||
@@ -80,10 +80,9 @@ class SessionInstaller @Inject constructor(
|
||||
|
||||
if (packageName != null && progress > 0.0) {
|
||||
AuroraApp.flowEvent.emitEvent(
|
||||
InstallerEvent.Installing(
|
||||
packageName = packageName,
|
||||
progress = (progress * 100).toInt()
|
||||
)
|
||||
InstallerEvent.Installing(packageName).apply {
|
||||
this.progress = (progress * 100).toInt()
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,7 +80,12 @@ class InstallerStatusReceiver : BroadcastReceiver() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun notifyUser(context: Context, packageName: String, displayName: String, status: Int) {
|
||||
private fun notifyUser(
|
||||
context: Context,
|
||||
packageName: String,
|
||||
displayName: String,
|
||||
status: Int
|
||||
) {
|
||||
val notificationManager =
|
||||
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
val notification = NotificationUtil.getInstallerStatusNotification(
|
||||
@@ -109,21 +114,22 @@ class InstallerStatusReceiver : BroadcastReceiver() {
|
||||
private fun postStatus(status: Int, packageName: String?, extra: String?, context: Context) {
|
||||
val event = when (status) {
|
||||
PackageInstaller.STATUS_SUCCESS -> {
|
||||
InstallerEvent.Success(
|
||||
packageName, context.getString(R.string.installer_status_success)
|
||||
)
|
||||
InstallerEvent.Installed(packageName!!).apply {
|
||||
this.extra = context.getString(R.string.installer_status_success)
|
||||
}
|
||||
}
|
||||
|
||||
PackageInstaller.STATUS_FAILURE_ABORTED -> {
|
||||
InstallerEvent.Cancelled(
|
||||
packageName, AppInstaller.getErrorString(context, status)
|
||||
)
|
||||
InstallerEvent.Cancelled(packageName!!).apply {
|
||||
this.extra = AppInstaller.getErrorString(context, status)
|
||||
}
|
||||
}
|
||||
|
||||
else -> {
|
||||
InstallerEvent.Failed(
|
||||
packageName, AppInstaller.getErrorString(context, status), extra
|
||||
)
|
||||
InstallerEvent.Failed(packageName!!).apply {
|
||||
this.error = AppInstaller.getErrorString(context, status)
|
||||
this.extra = extra ?: ""
|
||||
}
|
||||
}
|
||||
}
|
||||
AuroraApp.flowEvent.emitEvent(event)
|
||||
|
||||
@@ -23,8 +23,7 @@ import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import com.aurora.store.AuroraApp
|
||||
import com.aurora.store.data.event.BusEvent.InstallEvent
|
||||
import com.aurora.store.data.event.BusEvent.UninstallEvent
|
||||
import com.aurora.store.data.event.InstallerEvent
|
||||
import com.aurora.store.data.installer.AppInstaller
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import javax.inject.Inject
|
||||
@@ -41,11 +40,11 @@ open class PackageManagerReceiver : BroadcastReceiver() {
|
||||
|
||||
when (intent.action) {
|
||||
Intent.ACTION_PACKAGE_ADDED -> {
|
||||
AuroraApp.flowEvent.emitEvent(InstallEvent(packageName, ""))
|
||||
AuroraApp.flowEvent.emitEvent(InstallerEvent.Installed(packageName))
|
||||
}
|
||||
|
||||
Intent.ACTION_PACKAGE_REMOVED -> {
|
||||
AuroraApp.flowEvent.emitEvent(UninstallEvent(packageName, ""))
|
||||
AuroraApp.flowEvent.emitEvent(InstallerEvent.Uninstalled(packageName))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@ import androidx.lifecycle.lifecycleScope
|
||||
import androidx.navigation.fragment.findNavController
|
||||
import com.aurora.store.AuroraApp
|
||||
import com.aurora.store.R
|
||||
import com.aurora.store.data.event.BusEvent
|
||||
import com.aurora.store.data.event.AuthEvent
|
||||
import com.aurora.store.databinding.FragmentGoogleBinding
|
||||
import com.aurora.store.util.AC2DMUtil
|
||||
import com.aurora.store.view.ui.commons.BaseFragment
|
||||
@@ -110,15 +110,15 @@ class GoogleFragment : BaseFragment<FragmentGoogleBinding>() {
|
||||
}
|
||||
|
||||
viewLifecycleOwner.lifecycleScope.launch {
|
||||
AuroraApp.flowEvent.busEvent.collect { onEventReceived(it) }
|
||||
AuroraApp.flowEvent.authEvent.collect { onEventReceived(it) }
|
||||
}
|
||||
}
|
||||
|
||||
private fun onEventReceived(event: BusEvent) {
|
||||
private fun onEventReceived(event: AuthEvent) {
|
||||
when (event) {
|
||||
is BusEvent.GoogleAAS -> {
|
||||
is AuthEvent.GoogleLogin -> {
|
||||
if (event.success) {
|
||||
viewModel.buildGoogleAuthData(event.email, event.aasToken)
|
||||
viewModel.buildGoogleAuthData(event.email, event.token)
|
||||
} else {
|
||||
Toast.makeText(
|
||||
requireContext(),
|
||||
|
||||
@@ -27,6 +27,8 @@ import androidx.lifecycle.lifecycleScope
|
||||
import com.aurora.gplayapi.data.models.App
|
||||
import com.aurora.store.AuroraApp
|
||||
import com.aurora.store.data.event.BusEvent
|
||||
import com.aurora.store.data.event.Event
|
||||
import com.aurora.store.data.event.InstallerEvent
|
||||
import com.aurora.store.databinding.FragmentAppsBinding
|
||||
import com.aurora.store.view.epoxy.views.HeaderViewModel_
|
||||
import com.aurora.store.view.epoxy.views.app.AppListViewModel_
|
||||
@@ -49,10 +51,10 @@ class InstalledAppsFragment : BaseFragment<FragmentAppsBinding>() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun onEvent(event: BusEvent) {
|
||||
private fun onEvent(event: Event) {
|
||||
when (event) {
|
||||
is BusEvent.InstallEvent,
|
||||
is BusEvent.UninstallEvent,
|
||||
is InstallerEvent.Installed,
|
||||
is InstallerEvent.Uninstalled,
|
||||
is BusEvent.Blacklisted -> {
|
||||
viewModel.fetchApps()
|
||||
}
|
||||
|
||||
@@ -136,7 +136,7 @@ class AppDetailsFragment : BaseFragment<FragmentDetailsBinding>() {
|
||||
|
||||
private fun onEvent(event: Event) {
|
||||
when (event) {
|
||||
is BusEvent.InstallEvent -> {
|
||||
is InstallerEvent.Installed -> {
|
||||
if (app.packageName == event.packageName) {
|
||||
attachActions()
|
||||
binding.layoutDetailsToolbar.toolbar.menu.apply {
|
||||
@@ -148,7 +148,7 @@ class AppDetailsFragment : BaseFragment<FragmentDetailsBinding>() {
|
||||
}
|
||||
}
|
||||
|
||||
is BusEvent.UninstallEvent -> {
|
||||
is InstallerEvent.Uninstalled -> {
|
||||
if (app.packageName == event.packageName) {
|
||||
attachActions()
|
||||
binding.layoutDetailsToolbar.toolbar.menu.apply {
|
||||
|
||||
@@ -95,7 +95,7 @@ class AppMenuSheet : BaseDialogSheet<SheetAppMenuBinding>() {
|
||||
|
||||
dismissAllowingStateLoss()
|
||||
AuroraApp.flowEvent.emitEvent(
|
||||
BusEvent.Blacklisted(args.app.packageName, "")
|
||||
BusEvent.Blacklisted(args.app.packageName)
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -37,7 +37,8 @@ import com.aurora.gplayapi.data.models.App
|
||||
import com.aurora.store.AuroraApp
|
||||
import com.aurora.store.MobileNavigationDirections
|
||||
import com.aurora.store.R
|
||||
import com.aurora.store.data.event.BusEvent
|
||||
import com.aurora.store.data.event.Event
|
||||
import com.aurora.store.data.event.InstallerEvent
|
||||
import com.aurora.store.data.room.download.Download
|
||||
import com.aurora.store.databinding.FragmentUpdatesBinding
|
||||
import com.aurora.store.util.PackageUtil
|
||||
@@ -147,9 +148,9 @@ class UpdatesFragment : BaseFragment<FragmentUpdatesBinding>() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun onEvent(event: BusEvent) {
|
||||
private fun onEvent(event: Event) {
|
||||
when (event) {
|
||||
is BusEvent.InstallEvent, is BusEvent.UninstallEvent -> {
|
||||
is InstallerEvent.Installed, is InstallerEvent.Uninstalled -> {
|
||||
viewModel.fetchUpdates()
|
||||
}
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ import com.aurora.gplayapi.data.providers.DeviceInfoProvider
|
||||
import com.aurora.gplayapi.helpers.AuthHelper
|
||||
import com.aurora.store.AuroraApp
|
||||
import com.aurora.store.R
|
||||
import com.aurora.store.data.event.BusEvent
|
||||
import com.aurora.store.data.event.AuthEvent
|
||||
import com.aurora.store.data.model.AccountType
|
||||
import com.aurora.store.data.model.AuthState
|
||||
import com.aurora.store.data.model.InsecureAuth
|
||||
@@ -201,18 +201,18 @@ class AuthViewModel @Inject constructor(
|
||||
if (aasToken != null) {
|
||||
Preferences.putString(context, Constants.ACCOUNT_EMAIL_PLAIN, email)
|
||||
Preferences.putString(context, Constants.ACCOUNT_AAS_PLAIN, aasToken)
|
||||
AuroraApp.flowEvent.emitEvent(BusEvent.GoogleAAS(true, email, aasToken))
|
||||
AuroraApp.flowEvent.emitEvent(AuthEvent.GoogleLogin(true, email, aasToken))
|
||||
} else {
|
||||
Preferences.putString(context, Constants.ACCOUNT_EMAIL_PLAIN, "")
|
||||
Preferences.putString(context, Constants.ACCOUNT_AAS_PLAIN, "")
|
||||
AuroraApp.flowEvent.emitEvent(BusEvent.GoogleAAS(false))
|
||||
AuroraApp.flowEvent.emitEvent(AuthEvent.GoogleLogin(false, "", ""))
|
||||
}
|
||||
} else {
|
||||
AuroraApp.flowEvent.emitEvent(BusEvent.GoogleAAS(false))
|
||||
AuroraApp.flowEvent.emitEvent(AuthEvent.GoogleLogin(false, "", ""))
|
||||
}
|
||||
} catch (exception: Exception) {
|
||||
Log.e(TAG, "Failed to build AuthData", exception)
|
||||
AuroraApp.flowEvent.emitEvent(BusEvent.GoogleAAS(false))
|
||||
AuroraApp.flowEvent.emitEvent(AuthEvent.GoogleLogin(false, "", ""))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -238,6 +238,7 @@ class AuthViewModel @Inject constructor(
|
||||
)
|
||||
buildGoogleAuthData(email, aasToken)
|
||||
}
|
||||
|
||||
AccountType.ANONYMOUS -> {
|
||||
buildAnonymousAuthData()
|
||||
}
|
||||
@@ -248,9 +249,11 @@ class AuthViewModel @Inject constructor(
|
||||
is UnknownHostException -> {
|
||||
context.getString(R.string.title_no_network)
|
||||
}
|
||||
|
||||
is ConnectException -> {
|
||||
context.getString(R.string.server_unreachable)
|
||||
}
|
||||
|
||||
else -> {
|
||||
context.getString(R.string.bad_request)
|
||||
}
|
||||
|
||||
@@ -93,6 +93,7 @@
|
||||
<string name="action_search">"Search"</string>
|
||||
<string name="action_share">"Share"</string>
|
||||
<string name="action_uninstall">"Uninstall"</string>
|
||||
<string name="action_uninstall_success">"Successfully uninstalled"</string>
|
||||
<string name="action_home_screen">Add to Home screen</string>
|
||||
<string name="action_uninstall_confirmation">"Do you want to uninstall this app ?"</string>
|
||||
<string name="action_info">"App Info"</string>
|
||||
|
||||
Reference in New Issue
Block a user