Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Fantastic API and where to find them

Fantastic API and where to find them

Slides of the talk I kept at:
- Mobile Z Days 2020 Online
- DevFest Italy 2020 Online

Roberto Orgiu

October 18, 2020
Tweet

More Decks by Roberto Orgiu

Other Decks in Technology

Transcript

  1. Fantastic API
    And where to
    Find them

    View Slide

  2. Application

    Programming

    Interface

    View Slide

  3. Should we request it? Request it!
    Do we have it?
    Permissions
    Can we handle the
    result?
    Do what we need!

    View Slide

  4. Should we request it? Request it!
    Do we have it?
    More permissions!
    Can we handle the
    result?
    Do what we need!
    INSERT
    COMPLICATED
    LOGIC HERE

    View Slide

  5. ActivityResultContract

    View Slide

  6. Show me the code
    implementation 'androidx.activity:activity-ktx:1.2.0-alpha08'
    implementation 'androidx.fragment:fragment-ktx:1.3.0-alpha08'

    View Slide

  7. Show me the code
    private val requestPermissionsContracts =
    registerForActivityResult(
    ActivityResultContracts.RequestMultiplePermissions()
    ) { permissions ->
    permissions.entries.forEach {
    if (it.key.contains("FINE")) {
    /** DO THINGS **/
    } else {
    /** DO THINGS **/
    }
    }
    }

    View Slide

  8. Show me the code
    private val requestPermissionsContracts =
    registerForActivityResult(
    ActivityResultContracts.RequestMultiplePermissions()
    ) { permissions ->
    permissions.entries.forEach {
    if (it.key.contains("FINE")) {
    /** DO THINGS **/
    } else {
    /** DO THINGS **/
    }
    }
    }

    View Slide

  9. Show me the code
    private val wantedPermissions = arrayOf(
    Manifest.permission.ACCESS_COARSE_LOCATION,
    Manifest.permission.ACCESS_FINE_LOCATION
    )

    View Slide

  10. Show me the code
    requestPermissionsContracts.launch( wantedPermissions )

    View Slide

  11. Show me the code
    requestPermissionsContracts.launch(wantedPermissions)

    View Slide

  12. Show me the code
    class CustomContract :
    ActivityResultContract() {
    override fun createIntent(
    context: Context,
    input: Int): Intent
    override fun parseResult(
    resultCode: Int,
    intent: Intent?): Boolean
    }

    View Slide

  13. Pick a contact
    Contacts
    Get content (or more), create
    or open a document, open
    multiple documents or a
    document tree
    Content
    Start Activities or Intent
    Senders for result
    System
    Take a picture, a preview, or a
    video
    Multimedia
    Request one or more
    permissions
    Permissions
    More use cases

    View Slide

  14. What if I wanted
    to know
    what app my user selected?

    View Slide

  15. IntentSender

    View Slide

  16. Intent
    To wire the Receiver
    Pending

    Intent
    Create the
    IntentSender
    Broadcast

    Receiver
    Receives the result
    How does it work?

    View Slide

  17. Show me the code
    class ShareComponentReceiver : BroadcastReceiver() {
    override fun onReceive(
    context: Context,
    intent: Intent
    ) {
    val chosenApp = intent.extras
    ?.get(EXTRA_CHOSEN_COMPONENT)
    .toString()
    }
    }

    View Slide

  18. Show me the code
    class ShareComponentReceiver : BroadcastReceiver() {
    override fun onReceive(
    context: Context,
    intent: Intent
    ) {
    val chosenApp = intent.extras
    ?.get(EXTRA_CHOSEN_COMPONENT)
    .toString()
    }
    }

    View Slide

  19. Show me the code
    private fun shareIntent() = Intent().apply {
    action = Intent.ACTION_SEND
    putExtra(Intent.EXTRA_TEXT, "Sample text to share!")
    type = "text/plain"
    }

    View Slide

  20. Show me the code
    private fun intentSender(): IntentSender {
    val receiverIntent = Intent(
    this,
    ShareComponentReceiver::class.java
    )
    val pendingIntent = PendingIntent.getBroadcast(
    this, 0,
    receiverIntent, PendingIntent.FLAG_UPDATE_CURRENT
    )
    return pendingIntent.intentSender
    }

    View Slide

  21. Show me the code
    startActivity(
    Intent.createChooser(
    shareIntent(),
    "Share!",
    intentSender()
    )
    )

    View Slide

  22. The old way
    connectivityManager
    .activeNetworkInfo
    ?.isConnectedOrConnecting

    View Slide

  23. Network stuff
    has been
    DEPRECATED!!1!one!

    View Slide

  24. Network

    Request
    It holds the
    requirements
    Network

    Callback
    We still want to know
    the info
    Connectivity

    Manager
    We still need it
    The new way

    View Slide

  25. Show me the code
    val specifier = WifiNetworkSpecifier.Builder()
    .setSsid(wiFiData.ssid)
    .setWpa2Passphrase(wiFiData.password)
    .build()

    View Slide

  26. Show me the code
    val networkRequest = NetworkRequest.Builder()
    .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
    .removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
    .setNetworkSpecifier(specifier)
    .build()

    View Slide

  27. Show me the code
    val networkRequest = NetworkRequest.Builder()
    .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
    .removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
    .setNetworkSpecifier(specifier)
    .build()

    View Slide

  28. Show me the code
    private fun networkCallback() =
    object : ConnectivityManager.NetworkCallback() {
    override fun onAvailable(network: Network) {
    /** DO THINGS **/
    }
    override fun onUnavailable() {
    /** DO THINGS **/
    }
    }

    View Slide

  29. Show me the code
    connectivityManager.requestNetwork(
    networkRequest,
    networkCallback()
    )

    View Slide

  30. I want to do magic
    when Fragments
    are created!
    NOW
    AVAILABLE

    View Slide

  31. Show me the code
    class LifecycleListener :
    FragmentManager.FragmentLifecycleCallbacks() {
    override fun onFragmentViewCreated(
    fm: FragmentManager,
    f: Fragment,
    v: View,
    savedInstanceState: Bundle?
    ) {
    }
    override fun onFragmentCreated(
    fm: FragmentManager,
    f: Fragment,
    savedInstanceState: Bundle?
    ) {
    }

    View Slide

  32. Show me the code
    savedInstanceState: Bundle?
    ) {
    }
    override fun onFragmentCreated(
    fm: FragmentManager,
    f: Fragment,
    savedInstanceState: Bundle?
    ) {
    }
    override fun onFragmentDestroyed(
    fm: FragmentManager,
    f: Fragment
    ) {
    }
    override fun onFragmentViewDestroyed(
    fm: FragmentManager,
    f: Fragment
    ) {
    }
    }

    View Slide

  33. Show me the code
    class LifecycleListener :
    FragmentManager.FragmentLifecycleCallbacks() {
    override fun onFragmentViewCreated(
    fm: FragmentManager,
    f: Fragment,
    v: View,
    savedInstanceState: Bundle?
    ) {
    }
    override fun onFragmentCreated(
    fm: FragmentManager,
    f: Fragment,
    savedInstanceState: Bundle?
    ) {
    }
    override fun onFragmentDestroyed(
    fm: FragmentManager,
    f: Fragment
    ) {
    }
    override fun onFragmentViewDestroyed(
    fm: FragmentManager,
    f: Fragment
    ) {
    }
    }

    View Slide

  34. Show me the code
    val callbacks = LifecycleListener()
    supportFragmentManager
    .registerFragmentLifecycleCallbacks(callbacks, true)
    supportFragmentManager
    .unregisterFragmentLifecycleCallbacks(callbacks)

    View Slide

  35. Authenticate
    your
    users

    View Slide

  36. Create UI info Manage callbacks
    Can use biometric
    HW?
    Authentication
    Create prompt
    Authenticate!

    View Slide

  37. Show me the code
    implementation "androidx.biometric:biometric:1.0.1"

    View Slide

  38. Show me the code
    val biometricManager = BiometricManager.from(this)
    when (biometricManager.canAuthenticate()) {
    BiometricManager.BIOMETRIC_SUCCESS -> /** DO THINGS **/
    BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE,
    BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE,
    BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED -> /** ERROR **/
    }

    View Slide

  39. Show me the code
    fun buildPromptInfo() =
    BiometricPrompt.PromptInfo
    .Builder()
    .setTitle("Biometric authentication sample.")
    .setSubtitle("I'm a subtitle.")
    .setDescription("I'm a description")
    .setNegativeButtonText("Cancel")
    .build()

    View Slide

  40. Show me the code
    fun callback() = object : BiometricPrompt.AuthenticationCallback() {
    override fun onAuthenticationError(errorCode: Int,
    errString: CharSequence) {
    /** DO THINGS **/
    }
    override fun onAuthenticationSucceeded(result: AuthenticationResult) {
    /** DO THINGS **/
    }
    override fun onAuthenticationFailed() {
    /** DO THINGS **/
    }
    }

    View Slide

  41. Show me the code
    val biometricPrompt = BiometricPrompt(
    activity,
    executor,
    callback())

    View Slide

  42. Show me the code
    biometricPrompt.authenticate(buildPromptInfo())

    View Slide

  43. Hello!
    I Am Rob
    Follow me at @_tiwiz

    View Slide

  44. Q&A

    View Slide