Make Nexus handle rotation properly (no crash)

Also tune up the shadows in the leaves and
try out a new water background.
diff --git a/res/drawable-hdpi/pond.jpg b/res/drawable-hdpi/pond.jpg
new file mode 100644
index 0000000..39b1f8f
--- /dev/null
+++ b/res/drawable-hdpi/pond.jpg
Binary files differ
diff --git a/res/drawable-hdpi/water_thumb.jpg b/res/drawable-hdpi/water_thumb.jpg
index 56be4bc..90def45 100644
--- a/res/drawable-hdpi/water_thumb.jpg
+++ b/res/drawable-hdpi/water_thumb.jpg
Binary files differ
diff --git a/res/raw/fall.rs b/res/raw/fall.rs
index b094962..450d60a 100644
--- a/res/raw/fall.rs
+++ b/res/raw/fall.rs
@@ -201,14 +201,21 @@
 
     float matrix[16];
     if (a > 0.0f) {
-        color(0.0f, 0.0f, 0.0f, 0.15f);
+    
+		float alpha = 1.0f;
+        if (a >= 0.4f) alpha = 1.0f - (a - 0.4f) / 0.1f;
+        
+        color(0.0f, 0.0f, 0.0f, alpha * 0.15f);
 
         if (State->rotate) {
             matrixLoadRotate(matrix, 90.0f, 0.0f, 0.0f, 1.0f);
         } else {
             matrixLoadIdentity(matrix);
         }
-        matrixTranslate(matrix, x - State->xOffset * 2, y, 0.0f);
+        
+        float shadowOffet = a / 5;
+        
+        matrixTranslate(matrix, (x - State->xOffset * 2) + (shadowOffet / 2), y - shadowOffet, tz);
         matrixScale(matrix, s, s, 1.0f);
         matrixRotate(matrix, r, 0.0f, 0.0f, 1.0f);
         vpLoadModelMatrix(matrix);
@@ -218,8 +225,6 @@
                            LEAF_SIZE,  LEAF_SIZE, 0, u2, 0.0f,
                           -LEAF_SIZE,  LEAF_SIZE, 0, u1, 0.0f);
 
-        float alpha = 1.0f;
-        if (a >= 0.4f) alpha = 1.0f - (a - 0.5f) / 0.1f;
         color(1.0f, 1.0f, 1.0f, alpha);
     } else {
         color(1.0f, 1.0f, 1.0f, 1.0f);
@@ -371,7 +376,7 @@
     }
 
     drawRiverbed();
-    drawSky();
+    // drawSky();
     drawLeaves();
 
     return 1;
diff --git a/src/com/android/wallpaper/fall/FallRS.java b/src/com/android/wallpaper/fall/FallRS.java
index dc785fe..394669b 100644
--- a/src/com/android/wallpaper/fall/FallRS.java
+++ b/src/com/android/wallpaper/fall/FallRS.java
@@ -304,7 +304,7 @@
 
     private void loadTextures() {
         final Allocation[] textures = new Allocation[TEXTURES_COUNT];
-        textures[RSID_TEXTURE_RIVERBED] = loadTexture(R.drawable.riverbed, "TRiverbed");
+        textures[RSID_TEXTURE_RIVERBED] = loadTexture(R.drawable.pond, "TRiverbed");
         textures[RSID_TEXTURE_LEAVES] = loadTextureARGB(R.drawable.leaves, "TLeaves");
         textures[RSID_TEXTURE_SKY] = loadTextureARGB(R.drawable.clouds, "TSky");
 
diff --git a/src/com/android/wallpaper/nexus/NexusWallpaper.java b/src/com/android/wallpaper/nexus/NexusWallpaper.java
index 1fc4d84..62983a3 100644
--- a/src/com/android/wallpaper/nexus/NexusWallpaper.java
+++ b/src/com/android/wallpaper/nexus/NexusWallpaper.java
@@ -16,38 +16,19 @@
 
 package com.android.wallpaper.nexus;
 
-import android.service.wallpaper.WallpaperService;
+import android.content.res.Resources;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.graphics.Canvas;
-import android.graphics.Rect;
 import android.graphics.Paint;
-import android.graphics.Color;
-import android.graphics.RectF;
+import android.graphics.Rect;
+import android.os.Handler;
+import android.service.wallpaper.WallpaperService;
+import android.util.MathUtils;
 import android.view.SurfaceHolder;
 import android.view.animation.AnimationUtils;
-import android.content.IntentFilter;
-import android.content.Intent;
-import android.content.BroadcastReceiver;
-import android.content.Context;
-import android.content.SharedPreferences;
-import android.content.res.Resources;
-import android.content.res.XmlResourceParser;
-
-import android.os.Handler;
-import android.os.SystemClock;
-import android.text.format.Time;
-import android.util.MathUtils;
-import android.util.Log;
 
 import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.TimeZone;
-import java.io.IOException;
-
-import org.apache.harmony.misc.SystemUtils;
-import org.xmlpull.v1.XmlPullParserException;
-import static org.xmlpull.v1.XmlPullParser.*;
 
 import com.android.wallpaper.R;
 
@@ -87,15 +68,15 @@
             int length;
             Bitmap led;
             
-            public void reset(long now, int width, int height) {
+            public void reset(long now) {
                 vertical = Math.random() > 0.5;
                 reverse = Math.random() > 0.5;
                 
                 startTime = now + (long)(Math.random() * PULSE_DELAY);
                 if (vertical) {
-                    x = (int) (Math.random() * (width / mCellSize));
+                    x = (int) (Math.random() * (mBackgroundWidth / mCellSize));
                 } else {
-                    y = (int) (Math.random() * (height / mCellSize));
+                    y = (int) (Math.random() * (mBackgroundHeight / mCellSize));
                 }
                 length = PULSE_SIZE_MIN + (int)(Math.random() * PULSE_SIZE_EXTRA);
                 final double color = Math.random();
@@ -145,9 +126,9 @@
         private Bitmap mYellowLed;
         private Bitmap mGreenLed;
         
-        private ArrayList<Pulse> mPulses = new ArrayList<Pulse>();
+        private ArrayList<Pulse> mPulses;
         
-        private ArrayList<Collision> mCollisions = new ArrayList<Collision>();
+        private ArrayList<Collision> mCollisions;
 
         private int[][] mState = null;
 
@@ -157,6 +138,10 @@
 
         private int mCellSize;
         
+        private int mBackgroundWidth;
+        
+        private int mBackgroundHeight;
+        
         NexusEngine() {
         }
 
@@ -169,6 +154,10 @@
             Resources r = getResources();
             
             mBackground = BitmapFactory.decodeResource(r, R.drawable.pyramid_background, null);
+            
+            mBackgroundWidth = mBackground.getWidth();
+            mBackgroundHeight = mBackground.getHeight();
+            
             mBlueLed = BitmapFactory.decodeResource(r, R.drawable.led_blue, null);
             mRedLed = BitmapFactory.decodeResource(r, R.drawable.led_red, null);
             mYellowLed = BitmapFactory.decodeResource(r, R.drawable.led_yellow, null);
@@ -176,16 +165,26 @@
             
             mCellSize = mGreenLed.getWidth();
             
-            for (int i=0; i<NUM_PULSES; i++) {
-                Pulse p = new Pulse();
-                mPulses.add(p);
-            }
+            initializeState();
             
             if (isPreview()) {
                 mOffsetX = 0.5f;            
             }
         }
 
+        private void initializeState() {
+            mColumnCount = mBackgroundWidth / mCellSize;
+            mRowCount = mBackgroundHeight / mCellSize;
+            mState = new int[mColumnCount][mRowCount];
+            mCollisions = new ArrayList<Collision>();
+            mPulses = new ArrayList<Pulse>();
+            
+            for (int i=0; i<NUM_PULSES; i++) {
+                Pulse p = new Pulse();
+                mPulses.add(p);
+            }
+        }
+
         @Override
         public void onDestroy() {
             super.onDestroy();
@@ -204,6 +203,7 @@
         @Override
         public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
             super.onSurfaceChanged(holder, format, width, height);
+            initializeState();
             drawFrame();
         }
 
@@ -236,16 +236,16 @@
             try {
                 c = holder.lockCanvas();
                 if (c != null) {
-                    if (mState == null && width > 0 && height > 0) {
-                        mColumnCount = (width * 2) / mCellSize;
-                        mRowCount = height / mCellSize;
-                        mState = new int[mColumnCount][mRowCount];
-                    }
+                    final int dw = frame.width();
+                    final int bw = mBackgroundWidth;
+                    final int availw = dw-bw;
+                    int xPixels = availw < 0 ? (int)(availw*mOffsetX+.5f) : (availw/2);
+
+                    c.translate(xPixels, 0);
                     
-                    c.translate(-MathUtils.lerp(0, width, mOffsetX), 0);
                     c.drawBitmap(mBackground, 0, 0, null);
                     final long now = AnimationUtils.currentAnimationTimeMillis();
-                    drawPulses(c, now, width, height);
+                    drawPulses(c, now);
                     drawCollisions(c, now);
                     clearState();
                 }
@@ -274,8 +274,7 @@
             }
         }
                 
-        private void drawPulses(Canvas c, final long now, final int width,
-                final int height) {
+        private void drawPulses(Canvas c, final long now) {
             for (int i=0; i<NUM_PULSES; i++) {
                 Pulse p = mPulses.get(i);
                 final long startTime = p.startTime;
@@ -310,11 +309,11 @@
                         }
                         if (p.reverse) {
                             if (lastOffset < 0) {
-                                p.reset(now, width, height);
+                                p.reset(now);
                             }
                         } else {
                             if (lastOffset > mRowCount) {
-                                p.reset(now, width, height);
+                                p.reset(now);
                             }
                         }
 
@@ -338,16 +337,16 @@
                         }
                         if (p.reverse) {
                             if (lastOffset < 0) {
-                                p.reset(now, width * 2, height); 
+                                p.reset(now); 
                             }
                         } else {
                             if (lastOffset > mColumnCount) {
-                                p.reset(now, width * 2, height); 
+                                p.reset(now); 
                             }
                         }
                     }
                 } else if (startTime == 0) {
-                    p.reset(now, width * 2, height);
+                    p.reset(now);
                 }
             }
         }