Improve Grass' performance, add thumbnails for all wallpapers, better previews.

Change-Id: I2a02a74ec2df628f77de2536e875941a796ad5de
diff --git a/res/drawable-hdpi/galaxy_thumb.jpg b/res/drawable-hdpi/galaxy_thumb.jpg
new file mode 100644
index 0000000..70c38a1
--- /dev/null
+++ b/res/drawable-hdpi/galaxy_thumb.jpg
Binary files differ
diff --git a/res/drawable-hdpi/grass_thumb.jpg b/res/drawable-hdpi/grass_thumb.jpg
new file mode 100644
index 0000000..0383053
--- /dev/null
+++ b/res/drawable-hdpi/grass_thumb.jpg
Binary files differ
diff --git a/res/drawable-hdpi/polarclock_thumb.jpg b/res/drawable-hdpi/polarclock_thumb.jpg
new file mode 100644
index 0000000..c568bc6
--- /dev/null
+++ b/res/drawable-hdpi/polarclock_thumb.jpg
Binary files differ
diff --git a/res/drawable-hdpi/polarclock_thumb.png b/res/drawable-hdpi/polarclock_thumb.png
deleted file mode 100644
index 7994f4f..0000000
--- a/res/drawable-hdpi/polarclock_thumb.png
+++ /dev/null
Binary files differ
diff --git a/res/drawable-hdpi/water_thumb.jpg b/res/drawable-hdpi/water_thumb.jpg
new file mode 100644
index 0000000..56be4bc
--- /dev/null
+++ b/res/drawable-hdpi/water_thumb.jpg
Binary files differ
diff --git a/res/raw/fall.rs b/res/raw/fall.rs
index c60711d..b6984dd 100644
--- a/res/raw/fall.rs
+++ b/res/raw/fall.rs
@@ -34,6 +34,8 @@
 float skyOffsetX;
 float skyOffsetY;
 
+int lastDrop;
+
 struct vert_s {
     float x;
     float y;
@@ -50,18 +52,21 @@
 }
 
 void initLeaves() {
+    if (State->isPreview) lastDrop = uptimeMillis();
+
     struct Leaves_s *leaf = Leaves;
     int leavesCount = State->leavesCount;
+    float width = State->glWidth;
     float height = State->glHeight;
 
     int i;
     for (i = 0; i < leavesCount; i ++) {
         int sprite = randf(LEAVES_TEXTURES_COUNT);
-        leaf->x = randf2(-1.0f, 1.0f);
-        leaf->y = randf2(-height / 2.0f, height / 2.0f);
+        leaf->x = randf2(-width * 0.5f, width * 0.5f);
+        leaf->y = randf2(-height * 0.5f, height * 0.5f);
         leaf->scale = randf2(0.4f, 0.5f);
         leaf->angle = randf2(0.0f, 360.0f);
-        leaf->spin = degf(randf2(-0.02f, 0.02f)) / 4.0f;
+        leaf->spin = degf(randf2(-0.02f, 0.02f)) * 0.25f;
         leaf->u1 = sprite / (float) LEAVES_TEXTURES_COUNT;
         leaf->u2 = (sprite + 1) / (float) LEAVES_TEXTURES_COUNT;
         leaf->altitude = -1.0f;
@@ -332,10 +337,10 @@
             LEAF_SIZE * s + y < -glHeight / 2.0f) {
 
         int sprite = randf(LEAVES_TEXTURES_COUNT);
-        leaf->x = randf2(-1.0f, 1.0f);
-        leaf->y = randf2(-glHeight / 2.0f, glHeight / 2.0f);
+        leaf->x = randf2(-glWidth * 0.5f, glWidth * 0.5f);
+        leaf->y = randf2(-glHeight * 0.5f, glHeight * 0.5f);
         leaf->scale = randf2(0.4f, 0.5f);
-        leaf->spin = degf(randf2(-0.02f, 0.02f)) / 4.0f;
+        leaf->spin = degf(randf2(-0.02f, 0.02f)) * 0.25f;
         leaf->u1 = sprite / (float) LEAVES_TEXTURES_COUNT;
         leaf->u2 = (sprite + 1) / (float) LEAVES_TEXTURES_COUNT;
         leaf->altitude = 0.6f;
@@ -453,6 +458,18 @@
         Drop->dropX = -1;
         Drop->dropY = -1;
     }
+    
+    if (State->isPreview) {
+        int now = uptimeMillis();
+        if (now - lastDrop > 2000) {
+            float x = randf(State->meshWidth);
+            float y = randf(State->meshHeight);
+
+            drop(x, y, DROP_RADIUS);
+
+            lastDrop = now;
+        }
+    }    
 
     updateRipples();
     generateRipples();
diff --git a/res/raw/grass.rs b/res/raw/grass.rs
index 56405b8..1d34ac2 100644
--- a/res/raw/grass.rs
+++ b/res/raw/grass.rs
@@ -48,11 +48,11 @@
 
 #define REAL_TIME 1
 
-float time() {
-    if (REAL_TIME) {
+float time(int isPreview) {
+    if (REAL_TIME && !isPreview) {
         return (hour() * 3600.0f + minute() * 60.0f + second()) / SECONDS_IN_DAY;
     }
-    float t = uptimeMillis() / 40000.0f;
+    float t = uptimeMillis() / 30000.0f;
     return t - (int) t;
 }
 
@@ -89,7 +89,7 @@
 }
 
 int drawBlade(float *bladeStruct, float *bladeBuffer, int *bladeColor,
-        float brightness, float xOffset) {
+        float brightness, float xOffset, float now) {
 
     float offset = bladeStruct[BLADE_STRUCT_OFFSET];
     float scale = bladeStruct[BLADE_STRUCT_SCALE];
@@ -111,8 +111,7 @@
 
     int color = hsbToAbgr(h, s, lerpf(0, b, brightness), 1.0f);
 
-    float newAngle = turbulencef2(turbulenceX, uptimeMillis() * 0.00004f, 4.0f) - 0.5f;
-    newAngle *= 0.5f;
+    float newAngle = (turbulencef2(turbulenceX, now, 4.0f) - 0.5f) * 0.5f;
     angle = clampf(angle + (newAngle + offset - angle) * 0.15f, -MAX_BEND, MAX_BEND);
 
     float currentAngle = HALF_PI;
@@ -198,9 +197,11 @@
     float *bladeStruct = loadArrayF(RSID_BLADES, 0);
     float *bladeBuffer = loadArrayF(RSID_BLADES_BUFFER, 0);
     int *bladeColor = loadArrayI32(RSID_BLADES_BUFFER, 0);
+    
+    float now = uptimeMillis() * 0.00004f;
 
     for ( ; i < bladesCount; i += 1) {
-        int offset = drawBlade(bladeStruct, bladeBuffer, bladeColor, brightness, xOffset);
+        int offset = drawBlade(bladeStruct, bladeBuffer, bladeColor, brightness, xOffset, now);
         bladeBuffer += offset;
         bladeColor += offset;
         bladeStruct += BLADE_STRUCT_FIELDS_COUNT;
@@ -216,7 +217,7 @@
 
     float x = lerpf(width, 0, State->xOffset);
 
-    float now = time();
+    float now = time(State->isPreview);
     alpha(1.0f);
 
     float newB = 1.0f;
diff --git a/res/xml/fall.xml b/res/xml/fall.xml
index fedfa7e..9c1b0d3 100644
--- a/res/xml/fall.xml
+++ b/res/xml/fall.xml
@@ -21,4 +21,4 @@
 <!-- about the fall wallpaper. -->
 
 <wallpaper xmlns:android="http://schemas.android.com/apk/res/android"
-/>
+    android:thumbnail="@drawable/water_thumb" />
diff --git a/res/xml/galaxy.xml b/res/xml/galaxy.xml
index 2fc7e70..b88eec5 100644
--- a/res/xml/galaxy.xml
+++ b/res/xml/galaxy.xml
@@ -21,4 +21,4 @@
 <!-- about the galaxy wallpaper. -->
 
 <wallpaper xmlns:android="http://schemas.android.com/apk/res/android"
-/>
+    android:thumbnail="@drawable/galaxy_thumb" />
\ No newline at end of file
diff --git a/res/xml/grass.xml b/res/xml/grass.xml
index d1d6f1b..9c88e67 100644
--- a/res/xml/grass.xml
+++ b/res/xml/grass.xml
@@ -21,4 +21,4 @@
 <!-- about the grass wallpaper. -->
 
 <wallpaper xmlns:android="http://schemas.android.com/apk/res/android"
-/>
+    android:thumbnail="@drawable/grass_thumb" />
diff --git a/res/xml/polar_clock.xml b/res/xml/polar_clock.xml
index 56702ac..4066968 100644
--- a/res/xml/polar_clock.xml
+++ b/res/xml/polar_clock.xml
@@ -21,6 +21,5 @@
 <!-- about the polar clock. -->
 
 <wallpaper xmlns:android="http://schemas.android.com/apk/res/android"
-		android:thumbnail="@drawable/polarclock_thumb"
-        android:settingsActivity="com.android.wallpaper.polarclock.PolarClockSettings"
-/>
+    android:thumbnail="@drawable/polarclock_thumb"
+    android:settingsActivity="com.android.wallpaper.polarclock.PolarClockSettings" />
diff --git a/src/com/android/wallpaper/fall/FallRS.java b/src/com/android/wallpaper/fall/FallRS.java
index 06bf97b..cdb1384 100644
--- a/src/com/android/wallpaper/fall/FallRS.java
+++ b/src/com/android/wallpaper/fall/FallRS.java
@@ -262,6 +262,7 @@
         public float skySpeedX;
         public float skySpeedY;
         public int rotate;
+        public int isPreview;
     }
 
     static class DropState {
@@ -283,6 +284,7 @@
         mWorldState.skySpeedX = random(-0.001f, 0.001f);
         mWorldState.skySpeedY = random(0.00008f, 0.0002f);
         mWorldState.rotate = mWidth > mHeight ? 1 : 0;
+        mWorldState.isPreview = isPreview() ? 1 : 0;
 
         mStateType = Type.createFromClass(mRS, WorldState.class, 1, "WorldState");
         mState = Allocation.createTyped(mRS, mStateType);
diff --git a/src/com/android/wallpaper/grass/GrassRS.java b/src/com/android/wallpaper/grass/GrassRS.java
index e824d7e..7ce391b 100644
--- a/src/com/android/wallpaper/grass/GrassRS.java
+++ b/src/com/android/wallpaper/grass/GrassRS.java
@@ -217,14 +217,21 @@
         public float morning;
         public float afternoon;
         public float dusk;
+        public int isPreview;
     }
 
     private void createState() {
+        final boolean isPreview = isPreview();
+
         mWorldState = new WorldState();
         mWorldState.width = mWidth;
         mWorldState.height = mHeight;
         mWorldState.bladesCount = BLADES_COUNT;
         mWorldState.trianglesCount = mTriangles;
+        mWorldState.isPreview = isPreview ? 1 : 0;
+        if (isPreview) {
+            mWorldState.xOffset = 0.5f;
+        }
 
         mStateType = Type.createFromClass(mRS, WorldState.class, 1, "WorldState");
         mState = Allocation.createTyped(mRS, mStateType);
diff --git a/src/com/android/wallpaper/polarclock/PolarClockWallpaper.java b/src/com/android/wallpaper/polarclock/PolarClockWallpaper.java
index ab0452a..507ba70 100644
--- a/src/com/android/wallpaper/polarclock/PolarClockWallpaper.java
+++ b/src/com/android/wallpaper/polarclock/PolarClockWallpaper.java
@@ -34,6 +34,7 @@
 import android.os.SystemClock;
 import android.text.format.Time;
 import android.util.MathUtils;
+import android.util.Log;
 
 import java.util.HashMap;
 import java.util.TimeZone;
@@ -45,13 +46,15 @@
 import com.android.wallpaper.R;
 
 public class PolarClockWallpaper extends WallpaperService {
-    public static final String SHARED_PREFS_NAME = "polar_clock_settings";
+    private static final String LOG_TAG = "PolarClock";
+    
+    static final String SHARED_PREFS_NAME = "polar_clock_settings";
 
-    public static final String PREF_SHOW_SECONDS = "show_seconds";
-    public static final String PREF_VARIABLE_LINE_WIDTH = "variable_line_width";
-    public static final String PREF_PALETTE = "palette";
+    static final String PREF_SHOW_SECONDS = "show_seconds";
+    static final String PREF_VARIABLE_LINE_WIDTH = "variable_line_width";
+    static final String PREF_PALETTE = "palette";
 
-    public static final int BACKGROUND_COLOR = 0xffffffff;
+    static final int BACKGROUND_COLOR = 0xffffffff;
 
     static abstract class ClockPalette {
         public static ClockPalette parseXmlPaletteTag(XmlResourceParser xrp) {
@@ -271,8 +274,7 @@
         return new ClockEngine();
     }
 
-    class ClockEngine extends Engine
-            implements SharedPreferences.OnSharedPreferenceChangeListener {
+    class ClockEngine extends Engine implements SharedPreferences.OnSharedPreferenceChangeListener {
         private static final float SMALL_RING_THICKNESS = 8.0f;
         private static final float MEDIUM_RING_THICKNESS = 16.0f;
         private static final float LARGE_RING_THICKNESS = 32.0f;
@@ -329,9 +331,9 @@
                     what = xrp.next();
                 }
             } catch (IOException e) {
-                // XXX: Log?
+                Log.e(LOG_TAG, "An error occured during wallpaper configuration:", e);
             } catch (XmlPullParserException e) {
-                // XXX: Log?
+                Log.e(LOG_TAG, "An error occured during wallpaper configuration:", e);
             } finally {
                 xrp.close();
             }
@@ -358,6 +360,10 @@
             paint.setStrokeWidth(DEFAULT_RING_THICKNESS);
             paint.setStrokeCap(Paint.Cap.ROUND);
             paint.setStyle(Paint.Style.STROKE);
+
+            if (isPreview()) {
+                mOffsetX = 0.5f;            
+            }
         }
 
         @Override
@@ -446,7 +452,7 @@
 
         void drawFrame() {
             if (mPalette == null) {
-                android.util.Log.w("PolarClockWallpaper", "no palette?!");
+                Log.w("PolarClockWallpaper", "no palette?!");
                 return;
             }