-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
194 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
xsolla-payments-sdk/src/main/java/com/xsolla/android/payments/ui/utils/AsyncUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.xsolla.android.payments.ui.utils | ||
|
||
import android.os.Handler | ||
import android.os.Looper | ||
import android.util.Log | ||
import java.util.concurrent.Executor | ||
import java.util.concurrent.Executors | ||
import java.util.concurrent.Future | ||
import java.util.concurrent.ThreadFactory | ||
import java.util.concurrent.atomic.AtomicInteger | ||
|
||
object AsyncUtils { | ||
private val LOG_TAG = AsyncUtils.javaClass.simpleName | ||
|
||
private val sThreadFactory = InternalThreadFactory() | ||
private val sThreadPool = Executors.newCachedThreadPool(sThreadFactory) | ||
|
||
private val sMainThreadExecutor = object : Executor { | ||
private val mainHandler = Handler(Looper.getMainLooper()) | ||
|
||
override fun execute(r: Runnable) { | ||
mainHandler.post(r) | ||
} | ||
} | ||
|
||
fun run(runnable: Runnable) : Future<*>? { | ||
try { | ||
return sThreadPool.submit(runnable) | ||
} catch (e: Exception) { | ||
Log.d(LOG_TAG, "Failed to execute a task asynchronously.", e) | ||
return null | ||
} | ||
} | ||
|
||
fun runOnMainThread(runnable: Runnable) = | ||
sMainThreadExecutor.execute(runnable) | ||
|
||
class InternalThreadFactory : ThreadFactory { | ||
private val threadFactory = Executors.defaultThreadFactory() | ||
private val nextThreadId = AtomicInteger(1) | ||
|
||
override fun newThread(r: Runnable?): Thread { | ||
val thread = threadFactory.newThread(r) | ||
val threadId = nextThreadId.getAndIncrement() | ||
thread.name = "${THREAD_PREFIX}-${threadId}" | ||
return thread; | ||
} | ||
|
||
companion object { | ||
private const val THREAD_PREFIX = "XsollaPayments" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.