Skip to content

Commit

Permalink
chore: remove deprecated Timer#scheduleAtFixedRate and use ContextCompat
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Feb 7, 2025
1 parent 23b9cfe commit 47e2735
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 29 deletions.
20 changes: 8 additions & 12 deletions app/src/main/java/com/bnyro/clock/util/services/AlarmService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE
import androidx.core.app.NotificationManagerCompat
import androidx.core.app.ServiceCompat
import androidx.core.content.ContextCompat
import androidx.core.net.toUri
import com.bnyro.clock.App
import com.bnyro.clock.R
Expand Down Expand Up @@ -69,18 +70,13 @@ class AlarmService : Service() {
@SuppressLint("UnspecifiedRegisterReceiverFlag")
override fun onCreate() {
vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
registerReceiver(
alarmActionReceiver,
IntentFilter(ALARM_INTENT_ACTION),
RECEIVER_EXPORTED
)
} else {
registerReceiver(
alarmActionReceiver,
IntentFilter(ALARM_INTENT_ACTION)
)
}
ContextCompat.registerReceiver(
this,
alarmActionReceiver,
IntentFilter(ALARM_INTENT_ACTION),
ContextCompat.RECEIVER_EXPORTED
)

super.onCreate()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import android.util.Log
import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat
import com.bnyro.clock.R
import com.bnyro.clock.domain.model.WatchState
import com.bnyro.clock.ui.MainActivity
Expand Down Expand Up @@ -69,10 +70,8 @@ class StopwatchService : Service() {
this,
Manifest.permission.POST_NOTIFICATIONS
) == PackageManager.PERMISSION_GRANTED
registerReceiver(receiver, IntentFilter(STOPWATCH_INTENT_ACTION), RECEIVER_EXPORTED)
} else {
registerReceiver(receiver, IntentFilter(STOPWATCH_INTENT_ACTION))
}
ContextCompat.registerReceiver(this, receiver, IntentFilter(STOPWATCH_INTENT_ACTION), ContextCompat.RECEIVER_EXPORTED)
}

override fun onDestroy() {
Expand Down Expand Up @@ -101,7 +100,7 @@ class StopwatchService : Service() {
}
}
}
timer.scheduleAtFixedRate(counterTask, 0, UPDATE_DELAY.toLong())
timer.schedule(counterTask, 0, UPDATE_DELAY.toLong())
}

private fun stop() {
Expand Down
20 changes: 7 additions & 13 deletions app/src/main/java/com/bnyro/clock/util/services/TimerService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import androidx.annotation.StringRes
import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat
import com.bnyro.clock.R
import com.bnyro.clock.domain.model.TimerDescriptor
import com.bnyro.clock.domain.model.TimerObject
Expand Down Expand Up @@ -56,7 +57,7 @@ class TimerService : Service() {
@SuppressLint("UnspecifiedRegisterReceiverFlag")
override fun onCreate() {
super.onCreate()
timer.scheduleAtFixedRate(
timer.schedule(
object : TimerTask() {
override fun run() {
handler.post(this@TimerService::updateState)
Expand All @@ -65,18 +66,11 @@ class TimerService : Service() {
0,
UPDATE_DELAY.toLong()
)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
registerReceiver(
receiver,
IntentFilter(UPDATE_STATE_ACTION),
RECEIVER_EXPORTED
)
} else {
registerReceiver(
receiver,
IntentFilter(UPDATE_STATE_ACTION)
)
}
ContextCompat.registerReceiver(
this, receiver,
IntentFilter(UPDATE_STATE_ACTION),
ContextCompat.RECEIVER_EXPORTED
)
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Expand Down

0 comments on commit 47e2735

Please sign in to comment.