-
Notifications
You must be signed in to change notification settings - Fork 354
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add play purchasing android ipc calls
- Loading branch information
1 parent
0ae3eb1
commit 833d79c
Showing
4 changed files
with
79 additions
and
2 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
64 changes: 64 additions & 0 deletions
64
...id/service/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/PlayPurchaseHandler.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,64 @@ | ||
package net.mullvad.mullvadvpn.service.endpoint | ||
|
||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.GlobalScope | ||
import kotlinx.coroutines.channels.Channel | ||
import kotlinx.coroutines.channels.ClosedReceiveChannelException | ||
import kotlinx.coroutines.channels.actor | ||
import kotlinx.coroutines.channels.trySendBlocking | ||
import net.mullvad.mullvadvpn.lib.ipc.Event | ||
import net.mullvad.mullvadvpn.lib.ipc.Request | ||
import net.mullvad.mullvadvpn.model.PlayPurchase | ||
|
||
// WIP - adapted copy of VoucherRedeemer | ||
class PlayPurchaseHandler(private val endpoint: ServiceEndpoint) { | ||
private val daemon | ||
get() = endpoint.intermittentDaemon | ||
|
||
private val playPurchaseChannel = spawnActor() | ||
|
||
init { | ||
endpoint.dispatcher.registerHandler(Request.InitPlayPurchase::class) { | ||
playPurchaseChannel.trySendBlocking(Command.InitPlayPurchase) | ||
} | ||
endpoint.dispatcher.registerHandler(Request.VerifyPlayPurchase::class) { request -> | ||
playPurchaseChannel.trySendBlocking(Command.VerifyPlayPurchase(request.playPurchase)) | ||
} | ||
} | ||
|
||
fun onDestroy() { | ||
playPurchaseChannel.close() | ||
} | ||
|
||
private fun spawnActor() = | ||
GlobalScope.actor<Command>(Dispatchers.Default, Channel.UNLIMITED) { | ||
try { | ||
for (command in channel) { | ||
when (command) { | ||
is Command.InitPlayPurchase -> initializePurchase() | ||
is Command.VerifyPlayPurchase -> verifyPlayPurchase(command.playPurchase) | ||
} | ||
} | ||
} catch (exception: ClosedReceiveChannelException) { | ||
// Channel was closed, stop the actor | ||
} | ||
} | ||
|
||
private suspend fun initializePurchase() { | ||
val result = daemon.await().initPlayPurchase() | ||
endpoint.sendEvent(Event.PlayPurchaseInitResultEvent(result)) | ||
} | ||
|
||
private suspend fun verifyPlayPurchase(playPurchase: PlayPurchase) { | ||
val result = daemon.await().verifyPlayPurchase(playPurchase) | ||
endpoint.sendEvent(Event.PlayPurchaseVerifyResultEvent(result)) | ||
} | ||
|
||
companion object { | ||
private sealed class Command { | ||
data object InitPlayPurchase : Command() | ||
|
||
data class VerifyPlayPurchase(val playPurchase: PlayPurchase) : Command() | ||
} | ||
} | ||
} |
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