processing-android icon indicating copy to clipboard operation
processing-android copied to clipboard

getContext() is not working properly when running a Wallpaper

Open SanielDan opened this issue 3 years ago • 2 comments

getContext() seems to be null when using the sketch as a Wallpaper. Maybe the context could be the Service used for the wallpaper itself.

SanielDan avatar Sep 29 '20 08:09 SanielDan

I can also confirm this, so there is no way to use sensors in wallpapers basically.

misigun avatar May 26 '22 19:05 misigun

Wallpapers were broken by commit 71bdb3; I believe that reverting the change from there to PSurfaceNone would fix the issue. For the time being, I have been using a very hacky solution - you can add the following file to your project:

PWallpaperSurface.java
package processing.core;
import android.service.wallpaper.WallpaperService;


public class PWallpaperSurface extends PSurfaceNone {
  public PWallpaperSurface (PSurfaceNone _super) {
    sketch = _super.sketch;
    graphics = _super.graphics;
    component = _super.component;
    surfaceReady = _super.surfaceReady;
    surfaceView = _super.surfaceView;
    view = _super.view;
    wallpaper = _super.wallpaper;
    requestedThreadStart = _super.requestedThreadStart;
    thread = _super.thread;
    paused = _super.paused;
    pauseObject = _super.pauseObject;
    frameRateTarget = _super.frameRateTarget;
    frameRatePeriod = _super.frameRatePeriod;
  }
  
  public WallpaperService getWallpaper() {
    return wallpaper;
  }
}

Then, where you were using getContext() previously, you can do (new PWallpaperSurface((PSurfaceNone)surface)).getWallpaper(). (I tend to overwrite the surface object with the PWallpaperService version)

Starwort avatar Jul 17 '22 12:07 Starwort