Skip to content

Commit

Permalink
add synchronization to external actions handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Razeeman committed Nov 16, 2024
1 parent adb3043 commit de472e9
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ object Base {
const val namespace = "com.example.util.simpletimetracker"

// Raise by 2 to account for wear version code.
const val versionCode = 75
const val versionCode = 77
const val versionName = "1.46"
const val minSDK = 21
const val currentSDK = 34
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ const val SHORTCUT_NAVIGATION_RECORDS = "recordsTab"
const val SHORTCUT_NAVIGATION_STATISTICS = "statisticsTab"
const val SHORTCUT_NAVIGATION_SETTINGS = "settingsTab"

const val ACTION_START_ACTIVITY = "com.razeeman.util.simpletimetracker.ACTION_START_ACTIVITY"
const val ACTION_STOP_ACTIVITY = "com.razeeman.util.simpletimetracker.ACTION_STOP_ACTIVITY"
const val ACTION_STOP_ALL_ACTIVITIES = "com.razeeman.util.simpletimetracker.ACTION_STOP_ALL_ACTIVITIES"
const val ACTION_STOP_SHORTEST_ACTIVITY = "com.razeeman.util.simpletimetracker.ACTION_STOP_SHORTEST_ACTIVITY"
const val ACTION_STOP_LONGEST_ACTIVITY = "com.razeeman.util.simpletimetracker.ACTION_STOP_LONGEST_ACTIVITY"
const val ACTION_RESTART_ACTIVITY = "com.razeeman.util.simpletimetracker.ACTION_RESTART_ACTIVITY"
const val ACTION_ADD_RECORD = "com.razeeman.util.simpletimetracker.ACTION_ADD_RECORD"
const val ACTION_EXTERNAL_START_ACTIVITY = "com.razeeman.util.simpletimetracker.ACTION_START_ACTIVITY"
const val ACTION_EXTERNAL_STOP_ACTIVITY = "com.razeeman.util.simpletimetracker.ACTION_STOP_ACTIVITY"
const val ACTION_EXTERNAL_STOP_ALL_ACTIVITIES = "com.razeeman.util.simpletimetracker.ACTION_STOP_ALL_ACTIVITIES"
const val ACTION_EXTERNAL_STOP_SHORTEST_ACTIVITY = "com.razeeman.util.simpletimetracker.ACTION_STOP_SHORTEST_ACTIVITY"
const val ACTION_EXTERNAL_STOP_LONGEST_ACTIVITY = "com.razeeman.util.simpletimetracker.ACTION_STOP_LONGEST_ACTIVITY"
const val ACTION_EXTERNAL_RESTART_ACTIVITY = "com.razeeman.util.simpletimetracker.ACTION_RESTART_ACTIVITY"
const val ACTION_EXTERNAL_ADD_RECORD = "com.razeeman.util.simpletimetracker.ACTION_ADD_RECORD"

const val EVENT_STARTED_ACTIVITY = "com.razeeman.util.simpletimetracker.EVENT_STARTED_ACTIVITY"
const val EVENT_STOPPED_ACTIVITY = "com.razeeman.util.simpletimetracker.EVENT_STOPPED_ACTIVITY"
const val EXTRA_ACTIVITY_NAME = "extra_activity_name"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import android.widget.RemoteViews
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import com.example.util.simpletimetracker.core.extension.allowVmViolations
import com.example.util.simpletimetracker.core.mapper.ColorMapper
import com.example.util.simpletimetracker.core.utils.PendingIntents
import com.example.util.simpletimetracker.feature_notification.R
import com.example.util.simpletimetracker.feature_notification.recordType.customView.NotificationIconView
Expand All @@ -30,7 +29,6 @@ import javax.inject.Singleton
@Singleton
class NotificationActivitySwitchManager @Inject constructor(
@ApplicationContext private val context: Context,
private val colorMapper: ColorMapper,
private val router: Router,
private val controlsManager: NotificationControlsManager,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.example.util.simpletimetracker.core.extension.goAsync
import com.example.util.simpletimetracker.core.utils.ACTION_ADD_RECORD
import com.example.util.simpletimetracker.core.utils.ACTION_RESTART_ACTIVITY
import com.example.util.simpletimetracker.core.utils.ACTION_START_ACTIVITY
import com.example.util.simpletimetracker.core.utils.ACTION_STOP_ACTIVITY
import com.example.util.simpletimetracker.core.utils.ACTION_STOP_ALL_ACTIVITIES
import com.example.util.simpletimetracker.core.utils.ACTION_STOP_LONGEST_ACTIVITY
import com.example.util.simpletimetracker.core.utils.ACTION_STOP_SHORTEST_ACTIVITY
import com.example.util.simpletimetracker.core.utils.ACTION_EXTERNAL_ADD_RECORD
import com.example.util.simpletimetracker.core.utils.ACTION_EXTERNAL_RESTART_ACTIVITY
import com.example.util.simpletimetracker.core.utils.ACTION_EXTERNAL_START_ACTIVITY
import com.example.util.simpletimetracker.core.utils.ACTION_EXTERNAL_STOP_ACTIVITY
import com.example.util.simpletimetracker.core.utils.ACTION_EXTERNAL_STOP_ALL_ACTIVITIES
import com.example.util.simpletimetracker.core.utils.ACTION_EXTERNAL_STOP_LONGEST_ACTIVITY
import com.example.util.simpletimetracker.core.utils.ACTION_EXTERNAL_STOP_SHORTEST_ACTIVITY
import com.example.util.simpletimetracker.core.utils.EXTRA_ACTIVITY_NAME
import com.example.util.simpletimetracker.core.utils.EXTRA_RECORD_COMMENT
import com.example.util.simpletimetracker.core.utils.EXTRA_RECORD_TAG_NAME
Expand Down Expand Up @@ -128,47 +128,47 @@ class NotificationReceiver : BroadcastReceiver() {
finally = { automaticExportController.onFinished() },
block = { automaticExportController.onReminder() },
)
ACTION_START_ACTIVITY -> {
ACTION_EXTERNAL_START_ACTIVITY -> {
val name = intent.getStringExtra(EXTRA_ACTIVITY_NAME)
val comment = intent.getStringExtra(EXTRA_RECORD_COMMENT)
val tagNames = intent.getStringExtra(EXTRA_RECORD_TAG_NAME)
?.slipTagNames().orEmpty()
typeController.onActionActivityStart(
typeController.onActionExternalActivityStart(
name = name,
comment = comment,
tagNames = tagNames,
)
}
ACTION_STOP_ACTIVITY -> {
ACTION_EXTERNAL_STOP_ACTIVITY -> {
val name = intent.getStringExtra(EXTRA_ACTIVITY_NAME)
typeController.onActionActivityStop(name)
typeController.onActionExternalActivityStop(name)
}
ACTION_STOP_ALL_ACTIVITIES -> {
typeController.onActionActivityStopAll()
ACTION_EXTERNAL_STOP_ALL_ACTIVITIES -> {
typeController.onActionExternalActivityStopAll()
}
ACTION_STOP_SHORTEST_ACTIVITY -> {
typeController.onActionActivityStopShortest()
ACTION_EXTERNAL_STOP_SHORTEST_ACTIVITY -> {
typeController.onActionExternalActivityStopShortest()
}
ACTION_STOP_LONGEST_ACTIVITY -> {
typeController.onActionActivityStopLongest()
ACTION_EXTERNAL_STOP_LONGEST_ACTIVITY -> {
typeController.onActionExternalActivityStopLongest()
}
ACTION_RESTART_ACTIVITY -> {
ACTION_EXTERNAL_RESTART_ACTIVITY -> {
val comment = intent.getStringExtra(EXTRA_RECORD_COMMENT)
val tagNames = intent.getStringExtra(EXTRA_RECORD_TAG_NAME)
?.slipTagNames().orEmpty()
typeController.onActionActivityRestart(
typeController.onActionExternalActivityRestart(
comment = comment,
tagNames = tagNames,
)
}
ACTION_ADD_RECORD -> {
ACTION_EXTERNAL_ADD_RECORD -> {
val name = intent.getStringExtra(EXTRA_ACTIVITY_NAME)
val timeStarted = intent.getStringExtra(EXTRA_TIME_STARTED)
val timeEnded = intent.getStringExtra(EXTRA_TIME_ENDED)
val comment = intent.getStringExtra(EXTRA_RECORD_COMMENT)
val tagNames = intent.getStringExtra(EXTRA_RECORD_TAG_NAME)
?.slipTagNames().orEmpty()
typeController.onActionRecordAdd(
typeController.onActionExternalRecordAdd(
name = name,
timeStarted = timeStarted,
timeEnded = timeEnded,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,40 @@ import com.example.util.simpletimetracker.feature_notification.recordType.intera
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import javax.inject.Inject
import javax.inject.Singleton

@OptIn(DelicateCoroutinesApi::class)
@Singleton
class NotificationTypeBroadcastController @Inject constructor(
private val notificationTypeInteractor: NotificationTypeInteractor,
private val notificationActivitySwitchInteractor: NotificationActivitySwitchInteractor,
private val activityStartStopFromBroadcastInteractor: ActivityStartStopFromBroadcastInteractor,
private val notificationControlsMapper: NotificationControlsMapper,
) {

fun onActionActivityStart(
private val mutex = Mutex()

fun onActionExternalActivityStart(
name: String?,
comment: String?,
tagNames: List<String>,
) {
name ?: return
GlobalScope.launch {
) = GlobalScope.launch {
name ?: return@launch
mutex.withLock {
activityStartStopFromBroadcastInteractor.onActionActivityStart(
name = name, comment = comment, tagNames = tagNames,
)
}
}

fun onActionActivityStop(
fun onActionExternalActivityStop(
name: String?,
) {
name ?: return
GlobalScope.launch {
) = GlobalScope.launch {
name ?: return@launch
mutex.withLock {
activityStartStopFromBroadcastInteractor.onActionActivityStop(name)
}
}
Expand All @@ -48,46 +54,46 @@ class NotificationTypeBroadcastController @Inject constructor(
}
}

fun onActionActivityStopAll() {
GlobalScope.launch {
fun onActionExternalActivityStopAll() = GlobalScope.launch {
mutex.withLock {
activityStartStopFromBroadcastInteractor.onActionActivityStopAll()
}
}

fun onActionActivityStopShortest() {
GlobalScope.launch {
fun onActionExternalActivityStopShortest() = GlobalScope.launch {
mutex.withLock {
activityStartStopFromBroadcastInteractor.onActionActivityStopShortest()
}
}

fun onActionActivityStopLongest() {
GlobalScope.launch {
fun onActionExternalActivityStopLongest() = GlobalScope.launch {
mutex.withLock {
activityStartStopFromBroadcastInteractor.onActionActivityStopLongest()
}
}

fun onActionActivityRestart(
fun onActionExternalActivityRestart(
comment: String?,
tagNames: List<String>,
) {
GlobalScope.launch {
) = GlobalScope.launch {
mutex.withLock {
activityStartStopFromBroadcastInteractor.onActionActivityRestart(
comment = comment, tagNames = tagNames,
)
}
}

fun onActionRecordAdd(
fun onActionExternalRecordAdd(
name: String?,
timeStarted: String?,
timeEnded: String?,
comment: String?,
tagNames: List<String>,
) {
name ?: return
timeStarted ?: return
timeEnded ?: return
GlobalScope.launch {
) = GlobalScope.launch {
name ?: return@launch
timeStarted ?: return@launch
timeEnded ?: return@launch
mutex.withLock {
activityStartStopFromBroadcastInteractor.onRecordAdd(
name = name,
timeStarted = timeStarted,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import com.example.util.simpletimetracker.core.manager.ClipboardManager
import com.example.util.simpletimetracker.core.mapper.TimeMapper
import com.example.util.simpletimetracker.core.provider.ApplicationDataProvider
import com.example.util.simpletimetracker.core.repo.ResourceRepo
import com.example.util.simpletimetracker.core.utils.ACTION_ADD_RECORD
import com.example.util.simpletimetracker.core.utils.ACTION_RESTART_ACTIVITY
import com.example.util.simpletimetracker.core.utils.ACTION_START_ACTIVITY
import com.example.util.simpletimetracker.core.utils.ACTION_STOP_ACTIVITY
import com.example.util.simpletimetracker.core.utils.ACTION_STOP_ALL_ACTIVITIES
import com.example.util.simpletimetracker.core.utils.ACTION_STOP_LONGEST_ACTIVITY
import com.example.util.simpletimetracker.core.utils.ACTION_STOP_SHORTEST_ACTIVITY
import com.example.util.simpletimetracker.core.utils.ACTION_EXTERNAL_ADD_RECORD
import com.example.util.simpletimetracker.core.utils.ACTION_EXTERNAL_RESTART_ACTIVITY
import com.example.util.simpletimetracker.core.utils.ACTION_EXTERNAL_START_ACTIVITY
import com.example.util.simpletimetracker.core.utils.ACTION_EXTERNAL_STOP_ACTIVITY
import com.example.util.simpletimetracker.core.utils.ACTION_EXTERNAL_STOP_ALL_ACTIVITIES
import com.example.util.simpletimetracker.core.utils.ACTION_EXTERNAL_STOP_LONGEST_ACTIVITY
import com.example.util.simpletimetracker.core.utils.ACTION_EXTERNAL_STOP_SHORTEST_ACTIVITY
import com.example.util.simpletimetracker.core.utils.EVENT_STARTED_ACTIVITY
import com.example.util.simpletimetracker.core.utils.EVENT_STOPPED_ACTIVITY
import com.example.util.simpletimetracker.core.utils.EXTRA_ACTIVITY_NAME
Expand Down Expand Up @@ -123,17 +123,17 @@ class SettingsMapper @Inject constructor(
R.string.settings_automated_tracking_text,
isDarkTheme,
listOf(
HelpText(ACTION_START_ACTIVITY, canCopy = true),
HelpText(ACTION_STOP_ACTIVITY, canCopy = true),
HelpText(ACTION_EXTERNAL_START_ACTIVITY, canCopy = true),
HelpText(ACTION_EXTERNAL_STOP_ACTIVITY, canCopy = true),
HelpText(EXTRA_ACTIVITY_NAME, canCopy = true),
HelpText(applicationDataProvider.getPackageName(), canCopy = true),
HelpText(EXTRA_RECORD_COMMENT, canCopy = true),
HelpText(EXTRA_RECORD_TAG_NAME, canCopy = true),
HelpText(ACTION_STOP_ALL_ACTIVITIES, canCopy = true),
HelpText(ACTION_STOP_SHORTEST_ACTIVITY, canCopy = true),
HelpText(ACTION_STOP_LONGEST_ACTIVITY, canCopy = true),
HelpText(ACTION_RESTART_ACTIVITY, canCopy = true),
HelpText(ACTION_ADD_RECORD, canCopy = true),
HelpText(ACTION_EXTERNAL_STOP_ALL_ACTIVITIES, canCopy = true),
HelpText(ACTION_EXTERNAL_STOP_SHORTEST_ACTIVITY, canCopy = true),
HelpText(ACTION_EXTERNAL_STOP_LONGEST_ACTIVITY, canCopy = true),
HelpText(ACTION_EXTERNAL_RESTART_ACTIVITY, canCopy = true),
HelpText(ACTION_EXTERNAL_ADD_RECORD, canCopy = true),
HelpText(EXTRA_ACTIVITY_NAME, canCopy = true),
HelpText(EXTRA_RECORD_COMMENT, canCopy = true),
HelpText(EXTRA_RECORD_TAG_NAME, canCopy = true),
Expand Down

0 comments on commit de472e9

Please sign in to comment.