Created
October 27, 2021 09:01
-
-
Save SeungwonLee/a20cfc702c37c7a4cbf03b5ed647f144 to your computer and use it in GitHub Desktop.
FidoActivity.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
import android.app.Activity | |
import android.content.Context | |
import android.content.Intent | |
import android.media.RingtoneManager | |
import android.net.Uri | |
import android.os.Bundle | |
import android.util.Log | |
import androidx.activity.result.contract.ActivityResultContract | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.lifecycle.lifecycleScope | |
import kotlinx.coroutines.Dispatchers | |
import kotlinx.coroutines.launch | |
import kotlinx.coroutines.withContext | |
import kotlin.coroutines.suspendCoroutine | |
class FidoActivity : AppCompatActivity() { | |
private val controller:FidoResultController by lazy { FidoResultController() } | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_fido) | |
lifecycleScope.launch { | |
val result = controller.launch(this@FidoActivity) | |
Log.d("TAG", "onCreate: $result") | |
} | |
} | |
class FidoResultController { | |
private val delegator: FidoDelegator = FidoDelegator() | |
suspend fun launch(componentActivity: androidx.activity.ComponentActivity) { | |
val intent = delegator.resister() | |
suspendCoroutine<Uri> { | |
val content = componentActivity.registerForActivityResult(FidoContract(intent)) { uri: Uri? -> | |
uri ?: return@registerForActivityResult it.resumeWith(Result.failure(Error("empty uri"))) | |
it.resumeWith(Result.success(uri)) | |
} | |
content.launch(0) | |
} | |
} | |
class FidoContract(private val intent: Intent) : ActivityResultContract<Int, Uri?>() { // TODO Fix input, output types | |
override fun createIntent(context: Context, ringtoneType: Int) = intent | |
override fun parseResult(resultCode: Int, result: Intent?): Uri? { | |
if (resultCode != Activity.RESULT_OK) { | |
return null | |
} | |
return result?.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI) | |
} | |
} | |
class FidoDelegator { | |
suspend fun resister(): Intent = withContext(Dispatchers.IO) { | |
// ... | |
return@withContext Intent() | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment