Page MenuHomePhabricator

[Spike] Exploring Widgets in Android
Closed, ResolvedPublic

Description

We are looking to add an Android widget to the app. This spike should cover:

  1. Limitations of Android Widgets: what can and can't we do, what are the platform restrictions, any unknowns?
  2. XML vs Jetpack Compose (Glance Library from Google): which approach gives us more productivity without introducing new problems?
  3. Update Frequency: how often can we update the widget? what is the minimum platform allows and can WorkManager work around any of the limits?
  4. UI exploration: what APIs are available, are custom components supported and if so how easy it is to build?
  5. Performance with large database query: how does the widget handle large data query?

6. Interactivity: Need to understand what gestures and interaction are actually possible .

  1. Battery and performance impact: what is the cost of frequent updates?
  2. Testing: can widgets be tested?

Event Timeline

Limitations of Android Widgets

  • They run in a separate process from the app through RemoteViews. RemoteViews does not support every kind of layout or view. Link
  • limited update frequency from platform (30 minute)
  • limited gesture support (because it lives on home screen), touch or vertical swipe is only available.
  • No easy way to add animations and Gif support and updating widget contents are computationally expensive

XML vs Glance
Both are solid choices. Google officially recommends glance, and our codebase already integrates well with compose. Glance speeds up UI development thanks to its declarative approach. However it is not fully interoperable with standard Compose, we must import it from
androidx.glance namespace and XML is still needed for widget previews and metadata. Given that our widget will have 10+ states, I believe Glance's structure will pay off despite these limitations. We can leverage google samples which has lots of example of widgets using glance.

Update Frequency
from google

  • platform enforces minimum 30 minutes for periodic updates using updatePeriodMillis
  • WorkManager can go down to 15 minutes for more frequent periodic updates
  • there is also an event driven frequency update using functions like update, updateAll etc.
  • Our widget is lightweight in terms of updates, it only needs one scheduled refresh per day, with additional updates being event-driven. This means no unnecessary background activity and no impact on battery life.
  • Glance internally uses WorkManager to run provideGlance inside a worker when updateAll is called and Worker has a 10 minute time limit, composition runs for up to 45 seconds. Link

UI Exploration
Google official doc

  • The available components cover most of our needs. The only gap is the static progress bar, which will require a custom layout since Glance doesn't offer one out of the box.
  • colors need to be hardcoded as we cannot use our WIkipediaTheme colors

Performance with large database query

  • for this feature preferences are sufficient so we dont need to query our database
  • We need to track Streak, join date, last read date, completion date all fit as simple key-value pairs
  • i was also considering of switching to dataStore over our preference class. It natively supports Flow and it is already included as part of the Glance library.

Interactivity

  • Only PendingIntent based interactions are supported
  • Glance abstracts this through actionStartActivity, actionRunCallback, actionSendBroadcast
  • Our widget only requires actionStartActivity. For instrumentation, we have two options, capturing the event when the user taps the widget, or when they navigate to the activity.

Battery and Performance Impact

  • for our widget we don't need periodic update as updates are event-driven and once a day maybe at beginning of the day
  • the update happens through work manager so it handles doze mode and battery optimization
  • Overall battery impact is minimal given the once or twice maximum update pattern

Testing

  • yes it is possible to test using glance testing library