Skip to content

Commit

Permalink
[Android] Implement launcher zoom callback
Browse files Browse the repository at this point in the history
Added launcher zoom callback for live wallpapers introduced in API 30, closes libgdx#7055
  • Loading branch information
patzly committed Aug 12, 2023
1 parent c9ed23f commit d0b2044
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[1.12.1]
- LWJGL3 Improvement: Audio device is automatically switched if it was changed in the operating system.
- Tiled Fix: TiledLayer parallax default values fix
- Android: Added AndroidWallpaperListener#zoomChange() for listening to launcher zoom changes on Android 11+.

[1.12.0]
- [BREAKING CHANGE] Added #touchCancelled to InputProcessor interface, see #6871.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,51 @@ public void run () {
}
}

// zoom from last onZoomChanged
boolean zoomConsumed = true;
float zoom = 0.0f;

@Override
public void onZoomChanged(float zoom) {

// it spawns too frequent on some devices - its annoying!
// if (DEBUG)
// Log.d(TAG, " > AndroidWallpaperEngine - onZoomChanged(" + zoom + ") " + hashCode()
// + ", linkedApp: " + (linkedApp != null));

this.zoomConsumed = false;
this.zoom = zoom;

// can fail if linkedApp == null, so we repeat it in Engine.onResume
notifyZoomChanged();
if (!Gdx.graphics.isContinuousRendering()) {
Gdx.graphics.requestRendering();
}

super.onZoomChanged(zoom);
}

protected void notifyZoomChanged() {
if (linkedEngine == this && app.listener instanceof AndroidWallpaperListener) {
if (!zoomConsumed) { // no need for more sophisticated synchronization - zoomChanged can be called multiple
// times and with various patterns on various devices - user application must be prepared for that
zoomConsumed = true;

app.postRunnable(new Runnable() {
@Override
public void run () {
boolean isCurrent = false;
synchronized (sync) {
isCurrent = (linkedEngine == AndroidWallpaperEngine.this); // without this app can crash when fast
// switching between engines (tested!)
}
if (isCurrent) ((AndroidWallpaperListener)app.listener).zoomChange(zoom);
}
});
}
}
}

protected void notifyPreviewState () {
// notify preview state to app listener
if (linkedEngine == this && app.listener instanceof AndroidWallpaperListener) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public interface AndroidWallpaperListener {
* @param yPixelOffset */
void offsetChange (float xOffset, float yOffset, float xOffsetStep, float yOffsetStep, int xPixelOffset, int yPixelOffset);

/** Called on the rendering thread after the live wallpaper's zoom had changed.
* @param zoom */
void zoomChange (float zoom);

/** Called after 'isPreview' state had changed. First time called just after application initialization.
* @param isPreview current status, save this value and update always when this method is called if you want track live
* wallpaper isPreview status. */
Expand Down

0 comments on commit d0b2044

Please sign in to comment.