$30 off During Our Annual Pro Sale. View Details »

Cross-app communication and plugin architecture in Android

AppFoundry
March 08, 2016

Cross-app communication and plugin architecture in Android

A presentation by Siebe Sysmans.
This presentation was given for Mobel (http://www.mobel.io) and IT students.
Code: https://github.com/appfoundry/AndroidPluginArchitecture

AppFoundry

March 08, 2016
Tweet

More Decks by AppFoundry

Other Decks in Technology

Transcript

  1. Android Plugin Architecture

    View Slide

  2. Cross-app communication

    View Slide

  3. Cross-app communication
    • Intent and BroadcastReceiver
    • ContentResolver and ContentProvider
    • Services


    View Slide

  4. Android Plugin Architecture

    View Slide

  5. Android Plugin Architecture
    • Why?
    • User Plugin Discovery
    • Host App Plugin Discovery
    • Plugin Communication

    View Slide

  6. App Examples

    View Slide

  7. DashClock Widget

    View Slide

  8. DashClock Widget

    View Slide

  9. Muzei Live Wallpaper

    View Slide

  10. Muzei Live Wallpaper

    View Slide

  11. Why?

    View Slide

  12. Ecosystem-based marketing
    • Don’t create all your own plugins
    • Open up an API to extend your app
    • Indirectly promoting your app

    View Slide

  13. Business Model
    • Free app with basic functionality
    • Paid plugins

    View Slide

  14. Permission Mitigation
    • Standard Android is all or nothing
    • People might not install your permission rich app
    • Provide core app with less permissions
    • User can choose permissions by installing plugins
    (Before Android 6)

    View Slide

  15. User Plugin Discovery

    View Slide

  16. User Plugin Discovery
    • Hardcode references in core app
    • Link to a search term on Play Store
    • Download a JSON
    • Use a WebView
    • …

    View Slide

  17. App Plugin Discovery

    View Slide

  18. BroadcastReceiver
    Host app Plugin app
    BroadcastReceiver
    BroadcastReceiver
    Are there any plugins?
    Plugin app
    BroadcastReceiver

    View Slide

  19. BroadcastReceiver
    Host app Plugin app
    BroadcastReceiver
    BroadcastReceiver
    Yea, I’m a plugin!
    Plugin app
    BroadcastReceiver
    Yea, I’m a plugin!

    View Slide

  20. BroadcastReceiver
    • Not recommended
    • It wakes up every plugin app
    • Not sure when every plugin app responded

    View Slide

  21. PackageManager
    • Every plugin implements a custom intent action
    • PackageManager queries for every component
    using that custom action
    • Easily get metadata of that component

    View Slide

  22. Package Broadcasts
    • Android system sends package broadcasts
    • Host app checks if we are interested

    in that package change
    • Host app can update a live list of plugins

    View Slide

  23. Package Broadcasts
    Host app
    BroadcastReceiver
    ListView
    Android System
    App installs
    App replaces
    App deletes

    View Slide

  24. Package Broadcasts
    Host app
    BroadcastReceiver
    ListView
    Android System
    App installs
    App replaces
    App deletes PACKAGE_ADDED

    PACKAGE_REPLACED
    PACKAGE_REMOVED

    View Slide

  25. Package Broadcasts
    Host app
    ListView
    Android System
    App installs
    App replaces
    App deletes PACKAGE_ADDED

    PACKAGE_REPLACED
    PACKAGE_REMOVED
    Update

    View Slide

  26. Plugin Communication

    View Slide

  27. MetaData
    Great for getting static data
    • Plugin name
    • Plugin icon
    • Plugin api level

    View Slide

  28. Coding Example
    MetaData
    Using resources from other app
    PackageManager manager = getPackageManager();
    Resources pluginResources = manager.getResourcesForApplication("com.example.pluginapp");
    int drawableResID = pluginResources.getIdentifier("app_icon", "drawable", "com.example.pluginapp");
    Drawable drawable = pluginResources.getDrawable(drawableResID);

    View Slide

  29. BroadcastReceiver
    Host app Plugin app
    BroadcastReceiver
    BroadcastReceiver
    Provide me data

    View Slide

  30. BroadcastReceiver
    Host app Plugin app
    BroadcastReceiver
    BroadcastReceiver
    Here’s my data

    View Slide

  31. Remote Service Binding
    • Preferred way
    • Remote services found with PackageManager
    • Communication protocol using AIDL
    • Callback with Messenger for delayed responses

    View Slide

  32. Remote Service Binding - AIDL
    • All primitive types
    • int
    • long
    • char
    • …
    • String
    • Charsequence
    • List (ArrayList)
    • Map (HashMap)
    • Parcelable

    View Slide

  33. Remote Service Binding
    Host app Plugin app
    AIDL
    Compile
    Stub Stub
    Build

    View Slide

  34. Remote Service Binding
    Host app Plugin app
    Stub Stub
    Impl Impl
    Build

    View Slide

  35. Remote Service Binding
    Host app Plugin app
    Stub Stub
    Impl Impl
    Bind
    Run

    View Slide

  36. Remote Service Binding
    Host app Plugin app
    Stub Stub
    Impl Impl
    IPC
    Run

    View Slide

  37. GUI Plugins

    View Slide

  38. WebViews
    • Plugin provides data
    • Static URI using MetaData
    • Dynamic URI using IPC
    • Host has a WebView that loads the data
    • Easy
    • Not native
    • Security issues (javascript)

    View Slide

  39. RemoteViews
    • Native
    • Homescreen widgets
    • Lockscreen widgets
    • Notifications
    • Only basic View Widgets
    • Parcelable!

    View Slide

  40. Roll Your Own Parcelable
    • More flexible
    • A lot more work
    • Need to version your view building

    View Slide

  41. Some extra tips

    View Slide

  42. Some Extra Tips
    • Think about your versioning
    • Abstract to a library for distribution
    • Input validation in host
    • Make sure it’s secure
    • Don’t leak data

    →using signature level permissions

    View Slide

  43. Questions?
    Siebe Sysmans
    Consultant at AppFoundry
    [email protected]
    @ThisIsSiebe

    View Slide