3

Background

Suppose I have a relatively simple live wallpaper app which shows simple content via Canvas, by using surfaceHolder.lockCanvas() (sample here).

The live wallpaper class (that extends WallpaperService) has to have an Engine returned to it just once via onCreateEngine function.

The problem

Things get more complicated when I want to support more complex content, such as videos. For this, I've found this nice library:

https://github.com/AlynxZhou/alynx-live-wallpaper/

The library uses its own implementation of an Engine class, which is of OpenGL, to show video content.

Sadly, as it seems, there is no way on Android to switch between Engine instances during runtime.

What I've found

  1. I tried to call stopSelf, but as the Service is bound to the OS, it's not possible. I also tried to override onBind, but I can't because it's marked as final. I also don't see any possible reflection solution.

  2. I think the only possible way to do it that I've found, is to do one of the least recommended things on Android: calling System.exit() on the process that the live wallpaper uses. This causes it to restart, and re-create the Engine it was supposed to use.

  3. I could probably create a new class of a live wallpaper in the same app, but then the user will have to choose it again, which is a bad UX and I wish to avoid it.

The question

Is there any way to switch Engine during runtime for a live wallpaper app? Any workaround, even, other than what I've found?

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • You could try implementing a 'switching' Engine that forwards every method call to either one of two (or more) 'real' Engine implementations (both of which would be injected into the switching Engine's constructor). – Ruud Helderman Oct 13 '21 at 17:42
  • @RuudHelderman But there is a huge difference between an engine that works with OpenGL and one that uses a simple Canvas. – android developer Oct 13 '21 at 19:35
  • Both engines implement the same interface (subclass `Engine`), right? Main challenge I see is that each engine should refrain from doing its paint job when deselected. (Switching engine will need a way to inform the actual engines whose turn it is.) Am I overlooking something? – Ruud Helderman Oct 13 '21 at 20:33
  • @RuudHelderman They are very different. Please have a look at them. – android developer Oct 14 '21 at 21:04

0 Answers0