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

6923bdeb363961b064d2cdb6329982d6?s=128

Roberto Orgiu

October 18, 2020
Tweet

Transcript

  1. Fantastic API And where to Find them

  2. Application Programming Interface

  3. Should we request it? Request it! Do we have it?

    Permissions Can we handle the result? Do what we need!
  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
  5. ActivityResultContract

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

  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 **/ } } }
  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 **/ } } }
  9. Show me the code private val wantedPermissions = arrayOf( Manifest.permission.ACCESS_COARSE_LOCATION,

    Manifest.permission.ACCESS_FINE_LOCATION )
  10. Show me the code requestPermissionsContracts.launch( wantedPermissions )

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

  12. Show me the code class CustomContract : ActivityResultContract<Int, Boolean>() {

    override fun createIntent( context: Context, input: Int): Intent override fun parseResult( resultCode: Int, intent: Intent?): Boolean }
  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
  14. What if I wanted to know what app my user

    selected?
  15. IntentSender

  16. Intent To wire the Receiver Pending Intent Create the IntentSender

    Broadcast Receiver Receives the result How does it work?
  17. Show me the code class ShareComponentReceiver : BroadcastReceiver() { override

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

    fun onReceive( context: Context, intent: Intent ) { val chosenApp = intent.extras ?.get(EXTRA_CHOSEN_COMPONENT) .toString() } }
  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" }
  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 }
  21. Show me the code startActivity( Intent.createChooser( shareIntent(), "Share!", intentSender() )

    )
  22. The old way connectivityManager .activeNetworkInfo ?.isConnectedOrConnecting

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

  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
  25. Show me the code val specifier = WifiNetworkSpecifier.Builder() .setSsid(wiFiData.ssid) .setWpa2Passphrase(wiFiData.password)

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

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

    .setNetworkSpecifier(specifier) .build()
  28. Show me the code private fun networkCallback() = object :

    ConnectivityManager.NetworkCallback() { override fun onAvailable(network: Network) { /** DO THINGS **/ } override fun onUnavailable() { /** DO THINGS **/ } }
  29. Show me the code connectivityManager.requestNetwork( networkRequest, networkCallback() )

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

    AVAILABLE
  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? ) { }
  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 ) { } }
  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 ) { } }
  34. Show me the code val callbacks = LifecycleListener() supportFragmentManager .registerFragmentLifecycleCallbacks(callbacks,

    true) supportFragmentManager .unregisterFragmentLifecycleCallbacks(callbacks)
  35. Authenticate your users

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

    Create prompt Authenticate!
  37. Show me the code implementation "androidx.biometric:biometric:1.0.1"

  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 **/ }
  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()
  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 **/ } }
  41. Show me the code val biometricPrompt = BiometricPrompt( activity, executor,

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

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

  44. Q&A