Permalink
57f4623 Mar 26, 2018
2 contributors

Users who have contributed to this file

@objcode @JoseAlcerreca
122 lines (96 sloc) 3.22 KB
/*
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.samples.apps.iosched.shared.domain.internal
import android.os.Handler
import android.os.Looper
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
private const val NUMBER_OF_THREADS = 4 // TODO: Make this depend on device's hw
interface Scheduler {
fun execute(task: () -> Unit)
fun postToMainThread(task: () -> Unit)
fun postDelayedToMainThread(delay: Long, task: () -> Unit)
}
/**
* A shim [Scheduler] that by default handles operations in the [AsyncScheduler].
*/
object DefaultScheduler : Scheduler {
private var delegate: Scheduler = AsyncScheduler
/**
* Sets the new delegate scheduler, null to revert to the default async one.
*/
fun setDelegate(newDelegate: Scheduler?) {
delegate = newDelegate ?: AsyncScheduler
}
override fun execute(task: () -> Unit) {
delegate.execute(task)
}
override fun postToMainThread(task: () -> Unit) {
delegate.postToMainThread(task)
}
override fun postDelayedToMainThread(delay: Long, task: () -> Unit) {
delegate.postDelayedToMainThread(delay, task)
}
}
/**
* Runs tasks in a [ExecutorService] with a fixed thread of pools
*/
internal object AsyncScheduler : Scheduler {
private val executorService: ExecutorService = Executors.newFixedThreadPool(NUMBER_OF_THREADS)
override fun execute(task: () -> Unit) {
executorService.execute(task)
}
override fun postToMainThread(task: () -> Unit) {
if (isMainThread()) {
task()
} else {
val mainThreadHandler = Handler(Looper.getMainLooper())
mainThreadHandler.post(task)
}
}
private fun isMainThread(): Boolean {
return Looper.getMainLooper().thread === Thread.currentThread()
}
override fun postDelayedToMainThread(delay: Long, task: () -> Unit) {
val mainThreadHandler = Handler(Looper.getMainLooper())
mainThreadHandler.postDelayed(task, delay)
}
}
/**
* Runs tasks synchronously.
*/
object SyncScheduler : Scheduler {
private val postDelayedTasks = mutableListOf<() -> Unit>()
override fun execute(task: () -> Unit) {
task()
}
override fun postToMainThread(task: () -> Unit) {
task()
}
override fun postDelayedToMainThread(delay: Long, task: () -> Unit) {
postDelayedTasks.add(task)
}
fun runAllScheduledPostDelayedTasks() {
val tasks = postDelayedTasks.toList()
clearScheduledPostdelayedTasks()
for (task in tasks) {
task()
}
}
fun clearScheduledPostdelayedTasks() {
postDelayedTasks.clear()
}
}