Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import javax.inject.Singleton


interface AppComponentsProvider {
val currentAppId: String
val originalAppId: String

val klickrServiceComponentName: ComponentName
Expand All @@ -33,6 +34,10 @@ interface AppComponentsProvider {
@Singleton
class AppComponentsManager @Inject constructor() : AppComponentsProvider {

private lateinit var _currentAppId: String
override val currentAppId: String
get() = _currentAppId

private lateinit var _originalAppId: String
override val originalAppId: String
get() = _originalAppId
Expand All @@ -47,10 +52,14 @@ class AppComponentsManager @Inject constructor() : AppComponentsProvider {

override val tutorialActivityComponentName: ComponentName
get() = ComponentName(
"com.buzbuz.smartautoclicker",
currentAppId,
"com.buzbuz.smartautoclicker.feature.tutorial.ui.TutorialActivity",
)

fun registerCurrentAppId(appId: String) {
_currentAppId = appId
}

fun registerOriginalAppId(appId: String) {
_originalAppId = appId
}
Expand All @@ -62,4 +71,4 @@ class AppComponentsManager @Inject constructor() : AppComponentsProvider {
fun registerSmartAutoClickerService(componentName: ComponentName) {
_klickrServiceComponentName = componentName
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ fun getOpenWebBrowserPickerIntent(uri: Uri): Intent =
}

@RequiresApi(Build.VERSION_CODES.O)
fun getNotificationSettingsIntent(): Intent =
fun getNotificationSettingsIntent(appPackageName: String): Intent =
Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
putExtra(Settings.EXTRA_APP_PACKAGE, "com.buzbuz.smartautoclicker")
}
putExtra(Settings.EXTRA_APP_PACKAGE, appPackageName)
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ sealed class Permission(internal val isOptional: Boolean) {
/** The Android permission string value. */
protected abstract val permissionString: String

protected open val fallbackSettingsIntent: Intent? = null
protected open fun getFallbackSettingsIntent(context: Context): Intent? = null

internal fun initResultLauncher(fragment: Fragment, onResult: (isGranted: Boolean) -> Unit) {
permissionLauncher = fragment
Expand All @@ -88,7 +88,7 @@ sealed class Permission(internal val isOptional: Boolean) {
} ?: false
}

fallbackSettingsIntent?.let { intent ->
getFallbackSettingsIntent(context)?.let { intent ->
try {
context.startActivity(intent)
return true
Expand Down Expand Up @@ -127,4 +127,4 @@ sealed class Permission(internal val isOptional: Boolean) {

/** Get the shared preferences file for the permissions. */
private fun Context.getPermissionSharedPrefs() =
getSharedPreferences("permissions", Context.MODE_PRIVATE)
getSharedPreferences("permissions", Context.MODE_PRIVATE)
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ data class PermissionPostNotification(
override val permissionString: String
get() = Manifest.permission.POST_NOTIFICATIONS

override val fallbackSettingsIntent: Intent
get() = getNotificationSettingsIntent()
override fun getFallbackSettingsIntent(context: Context): Intent =
getNotificationSettingsIntent(context.packageName)

override fun isGranted(context: Context): Boolean =
context.getSystemService(NotificationManager::class.java).areNotificationsEnabled()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class ServiceNotificationController(
private val notificationManager: NotificationManagerCompat =
NotificationManagerCompat.from(context)
private val notificationActionReceiver: NotificationActionsReceiver =
NotificationActionsReceiver(listener::notifyAction)
NotificationActionsReceiver(appComponentsProvider, listener::notifyAction)
private val nightModeReceiver: NightModeReceiver =
NightModeReceiver(::updateNotification)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,22 @@ internal sealed class NotificationActionPendingIntent {
data class Activity(val componentName: ComponentName) : NotificationActionPendingIntent()
}

internal fun getAllActionsBroadcastIntentFilter(): IntentFilter =
internal fun getAllActionsBroadcastIntentFilter(appPackageName: String): IntentFilter =
IntentFilter().apply {
addAction(ServiceNotificationAction.Play.getBroadcastAction())
addAction(ServiceNotificationAction.Pause.getBroadcastAction())
addAction(ServiceNotificationAction.Show.getBroadcastAction())
addAction(ServiceNotificationAction.Hide.getBroadcastAction())
addAction(ServiceNotificationAction.Stop.getBroadcastAction())
addAction(ServiceNotificationAction.Play.getBroadcastAction(appPackageName))
addAction(ServiceNotificationAction.Pause.getBroadcastAction(appPackageName))
addAction(ServiceNotificationAction.Show.getBroadcastAction(appPackageName))
addAction(ServiceNotificationAction.Hide.getBroadcastAction(appPackageName))
addAction(ServiceNotificationAction.Stop.getBroadcastAction(appPackageName))
}

internal fun Intent.toServiceNotificationAction(): ServiceNotificationAction? =
internal fun Intent.toServiceNotificationAction(appPackageName: String): ServiceNotificationAction? =
when (action) {
ServiceNotificationAction.Play.getBroadcastAction() -> ServiceNotificationAction.Play
ServiceNotificationAction.Pause.getBroadcastAction() -> ServiceNotificationAction.Pause
ServiceNotificationAction.Show.getBroadcastAction() -> ServiceNotificationAction.Show
ServiceNotificationAction.Hide.getBroadcastAction() -> ServiceNotificationAction.Hide
ServiceNotificationAction.Stop.getBroadcastAction() -> ServiceNotificationAction.Stop
ServiceNotificationAction.Play.getBroadcastAction(appPackageName) -> ServiceNotificationAction.Play
ServiceNotificationAction.Pause.getBroadcastAction(appPackageName) -> ServiceNotificationAction.Pause
ServiceNotificationAction.Show.getBroadcastAction(appPackageName) -> ServiceNotificationAction.Show
ServiceNotificationAction.Hide.getBroadcastAction(appPackageName) -> ServiceNotificationAction.Hide
ServiceNotificationAction.Stop.getBroadcastAction(appPackageName) -> ServiceNotificationAction.Stop
else -> null
}

Expand All @@ -108,20 +108,20 @@ internal fun ServiceNotificationAction.getPendingIntent(context: Context, appCom

private fun ServiceNotificationAction.getIntent(appComponentsProvider: AppComponentsProvider): NotificationActionPendingIntent =
when (this) {
ServiceNotificationAction.Play -> NotificationActionPendingIntent.Broadcast(getBroadcastAction())
ServiceNotificationAction.Pause -> NotificationActionPendingIntent.Broadcast(getBroadcastAction())
ServiceNotificationAction.Show -> NotificationActionPendingIntent.Broadcast(getBroadcastAction())
ServiceNotificationAction.Hide -> NotificationActionPendingIntent.Broadcast(getBroadcastAction())
ServiceNotificationAction.Stop -> NotificationActionPendingIntent.Broadcast(getBroadcastAction())
ServiceNotificationAction.Play -> NotificationActionPendingIntent.Broadcast(getBroadcastAction(appComponentsProvider.currentAppId))
ServiceNotificationAction.Pause -> NotificationActionPendingIntent.Broadcast(getBroadcastAction(appComponentsProvider.currentAppId))
ServiceNotificationAction.Show -> NotificationActionPendingIntent.Broadcast(getBroadcastAction(appComponentsProvider.currentAppId))
ServiceNotificationAction.Hide -> NotificationActionPendingIntent.Broadcast(getBroadcastAction(appComponentsProvider.currentAppId))
ServiceNotificationAction.Stop -> NotificationActionPendingIntent.Broadcast(getBroadcastAction(appComponentsProvider.currentAppId))
ServiceNotificationAction.Config -> NotificationActionPendingIntent.Activity(appComponentsProvider.scenarioActivityComponentName)
}

private fun ServiceNotificationAction.getBroadcastAction(): String =
private fun ServiceNotificationAction.getBroadcastAction(appPackageName: String): String =
when (this) {
ServiceNotificationAction.Play -> "com.buzbuz.smartautoclicker.PLAY"
ServiceNotificationAction.Pause -> "com.buzbuz.smartautoclicker.PAUSE"
ServiceNotificationAction.Show -> "com.buzbuz.smartautoclicker.SHOW"
ServiceNotificationAction.Hide -> "com.buzbuz.smartautoclicker.HIDE"
ServiceNotificationAction.Stop -> "com.buzbuz.smartautoclicker.STOP"
ServiceNotificationAction.Play -> "$appPackageName.PLAY"
ServiceNotificationAction.Pause -> "$appPackageName.PAUSE"
ServiceNotificationAction.Show -> "$appPackageName.SHOW"
ServiceNotificationAction.Hide -> "$appPackageName.HIDE"
ServiceNotificationAction.Stop -> "$appPackageName.STOP"
ServiceNotificationAction.Config -> throw IllegalArgumentException("This action doesn't use broadcasts")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,23 @@ import android.content.Intent
import android.util.Log

import com.buzbuz.smartautoclicker.core.base.SafeBroadcastReceiver
import com.buzbuz.smartautoclicker.core.base.data.AppComponentsProvider
import com.buzbuz.smartautoclicker.feature.notifications.model.ServiceNotificationAction
import com.buzbuz.smartautoclicker.feature.notifications.model.getAllActionsBroadcastIntentFilter
import com.buzbuz.smartautoclicker.feature.notifications.model.toServiceNotificationAction


internal class NotificationActionsReceiver(
appComponentsProvider: AppComponentsProvider,
private val onReceived: (ServiceNotificationAction) -> Unit,
): SafeBroadcastReceiver(getAllActionsBroadcastIntentFilter()) {
): SafeBroadcastReceiver(getAllActionsBroadcastIntentFilter(appComponentsProvider.currentAppId)) {

override fun onReceive(context: Context, intent: Intent) {
intent.toServiceNotificationAction()?.let { action ->
intent.toServiceNotificationAction(context.packageName)?.let { action ->
Log.i(TAG, "Notification action received: ${intent.action}")
onReceived(action)
}
}
}

private const val TAG = "NotificationActionsReceiver"
private const val TAG = "NotificationActionsReceiver"
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ class NotificationDialog(

overlayManager.navigateTo(
context = context,
newOverlay = newNotificationSettingsStarterOverlay(),
newOverlay = newNotificationSettingsStarterOverlay(context),
hideCurrent = true,
)
}
Expand All @@ -227,4 +227,4 @@ class NotificationDialog(
}
}

private const val TAG = "PauseDialog"
private const val TAG = "PauseDialog"
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ internal fun newNotificationPermissionStarterOverlay(context: Context) = Activit
)

@RequiresApi(Build.VERSION_CODES.O)
internal fun newNotificationSettingsStarterOverlay() = ActivityStarterOverlayMenu(
intent = getNotificationSettingsIntent(),
)
internal fun newNotificationSettingsStarterOverlay(context: Context) = ActivityStarterOverlayMenu(
intent = getNotificationSettingsIntent(context.packageName),
)
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ class SmartAutoClickerApplication : Application() {

val componentConfig = ComponentConfig
appComponentsManager.apply {
registerCurrentAppId(packageName)
registerOriginalAppId(componentConfig.ORIGINAL_APP_ID)
registerSmartAutoClickerService(componentConfig.smartAutoClickerService)
registerScenarioActivity(componentConfig.scenarioActivity)
}

DynamicColors.applyToActivitiesIfAvailable(this)
}
}
}
Loading