1919import android .app .ActivityManager ;
2020import android .app .WallpaperManager ;
2121import android .content .BroadcastReceiver ;
22+ import android .content .ComponentCallbacks2 ;
2223import android .content .Context ;
2324import android .content .Intent ;
2425import android .graphics .Bitmap ;
@@ -62,6 +63,8 @@ public class ImageWallpaper extends WallpaperService {
6263
6364 WallpaperManager mWallpaperManager ;
6465
66+ DrawableEngine mEngine ;
67+
6568 boolean mIsHwAccelerated ;
6669
6770 @ Override
@@ -77,12 +80,20 @@ public void onCreate() {
7780 }
7881 }
7982
83+ @ Override
84+ public void onTrimMemory (int level ) {
85+ if (mEngine != null ) {
86+ mEngine .trimMemory (level );
87+ }
88+ }
89+
8090 private static boolean isEmulator () {
8191 return "1" .equals (SystemProperties .get (PROPERTY_KERNEL_QEMU , "0" ));
8292 }
8393
8494 public Engine onCreateEngine () {
85- return new DrawableEngine ();
95+ mEngine = new DrawableEngine ();
96+ return mEngine ;
8697 }
8798
8899 class DrawableEngine extends Engine {
@@ -155,6 +166,15 @@ public DrawableEngine() {
155166 setFixedSizeAllowed (true );
156167 }
157168
169+ public void trimMemory (int level ) {
170+ if (level >= ComponentCallbacks2 .TRIM_MEMORY_RUNNING_LOW &&
171+ mBackground != null && mIsHwAccelerated ) {
172+ mBackground .recycle ();
173+ mBackground = null ;
174+ mWallpaperManager .forgetLoadedWallpaper ();
175+ }
176+ }
177+
158178 @ Override
159179 public void onCreate (SurfaceHolder surfaceHolder ) {
160180 if (DEBUG ) {
@@ -329,16 +349,17 @@ void drawFrameLocked() {
329349 }
330350 } else {
331351 drawWallpaperWithCanvas (sh , availw , availh , xPixels , yPixels );
352+ if (FIXED_SIZED_SURFACE ) {
353+ // If the surface is fixed-size, we should only need to
354+ // draw it once and then we'll let the window manager
355+ // position it appropriately. As such, we no longer needed
356+ // the loaded bitmap. Yay!
357+ // hw-accelerated path retains bitmap for faster rotation
358+ mBackground = null ;
359+ mWallpaperManager .forgetLoadedWallpaper ();
360+ }
332361 }
333362
334- if (FIXED_SIZED_SURFACE ) {
335- // If the surface is fixed-size, we should only need to
336- // draw it once and then we'll let the window manager
337- // position it appropriately. As such, we no longer needed
338- // the loaded bitmap. Yay!
339- mBackground = null ;
340- mWallpaperManager .forgetLoadedWallpaper ();
341- }
342363 }
343364
344365 void updateWallpaperLocked () {
@@ -489,8 +510,6 @@ private int loadTexture(Bitmap bitmap) {
489510 GLUtils .texImage2D (GL_TEXTURE_2D , 0 , GL_RGBA , bitmap , GL_UNSIGNED_BYTE , 0 );
490511 checkGlError ();
491512
492- bitmap .recycle ();
493-
494513 return texture ;
495514 }
496515
0 commit comments