Tweak the galaxy and add better support for preview mode.

Change-Id: I3339d8066ec0f20ff12adcd9eedf2186d2ea4b01
diff --git a/res/raw/galaxy.rs b/res/raw/galaxy.rs
index 2b081e2..6fd4470 100644
--- a/res/raw/galaxy.rs
+++ b/res/raw/galaxy.rs
@@ -23,8 +23,8 @@
 float distance;
 
 void init() {
-    angle = 0.0f;
-    distance = 0.0f;
+    angle = 37.0f;
+    distance = 0.55f;        
 }
 
 void drawSpace(float xOffset, int width, int height) {
@@ -61,7 +61,7 @@
 
     float matrix[16];
     matrixLoadTranslate(matrix, 0.0f, 0.0f, 10.0f - 6.0f * distance);
-    matrixScale(matrix, 6.15f, 6.0f, 1.0f);
+    matrixScale(matrix, 6.6f, 6.0f, 1.0f);
     matrixRotate(matrix, angle, 1.0f, 0.5f, 0.0f);
     vpLoadModelMatrix(matrix);
 
@@ -103,9 +103,17 @@
     drawParticles(x, width, height);
     drawLights(x, width, height);
 
-    if (angle < 68.0f) {
-        angle += 0.4f;
-        distance = angle / 68.0f;
+    if (State->isPreview == 0) {
+        if (angle > 0.0f) {
+            angle -= 0.4f;
+            distance = angle / 68.0f;
+        }
+    } else {
+        // Unfortunately this cannot happen in init()
+        // since the State structure instance does not
+        // exist at this point
+        angle = 0.0f;
+        distance = 0.0f;
     }
 
     return 1;
diff --git a/src/com/android/wallpaper/RenderScriptScene.java b/src/com/android/wallpaper/RenderScriptScene.java
index 01c551c..0272651 100644
--- a/src/com/android/wallpaper/RenderScriptScene.java
+++ b/src/com/android/wallpaper/RenderScriptScene.java
@@ -24,6 +24,7 @@
 public abstract class RenderScriptScene {
     protected int mWidth;
     protected int mHeight;
+    protected boolean mPreview;
     protected Resources mResources;
     protected RenderScript mRS;
     protected ScriptC mScript;
@@ -33,11 +34,16 @@
         mHeight = height;
     }
 
-    public void init(RenderScript rs, Resources res) {
+    public void init(RenderScript rs, Resources res, boolean isPreview) {
         mRS = rs;
         mResources = res;
+        mPreview = isPreview;
         mScript = createScript();
     }
+    
+    public boolean isPreview() {
+        return mPreview;
+    }
 
     public int getWidth() {
         return mWidth;
diff --git a/src/com/android/wallpaper/RenderScriptWallpaper.java b/src/com/android/wallpaper/RenderScriptWallpaper.java
index f40bf41..1102b0f 100644
--- a/src/com/android/wallpaper/RenderScriptWallpaper.java
+++ b/src/com/android/wallpaper/RenderScriptWallpaper.java
@@ -72,7 +72,7 @@
             super.onSurfaceChanged(holder, format, width, height);
             if (mRenderer == null) {
                 mRenderer = createScene(width, height);
-                mRenderer.init(mRs, getResources());
+                mRenderer.init(mRs, getResources(), isPreview());
                 mRenderer.start();
             } else {
                 mRenderer.resize(width, height);
diff --git a/src/com/android/wallpaper/fall/FallView.java b/src/com/android/wallpaper/fall/FallView.java
index 2e4c0d2..871ca16 100644
--- a/src/com/android/wallpaper/fall/FallView.java
+++ b/src/com/android/wallpaper/fall/FallView.java
@@ -37,7 +37,7 @@
 
         RenderScript RS = createRenderScript(false);
         mRender = new FallRS(w, h);
-        mRender.init(RS, getResources());
+        mRender.init(RS, getResources(), false);
         mRender.start();
     }
 
diff --git a/src/com/android/wallpaper/galaxy/GalaxyRS.java b/src/com/android/wallpaper/galaxy/GalaxyRS.java
index 6a964f4..0233b74 100644
--- a/src/com/android/wallpaper/galaxy/GalaxyRS.java
+++ b/src/com/android/wallpaper/galaxy/GalaxyRS.java
@@ -173,6 +173,7 @@
         public int particlesCount;
         public int galaxyRadius;
         public float xOffset;
+        public int isPreview;
     }
 
     static class GalaxyParticle {
@@ -184,11 +185,17 @@
     }
 
     private void createState() {
+        boolean isPreview = isPreview();
+
         mGalaxyState = new GalaxyState();
         mGalaxyState.width = mWidth;
         mGalaxyState.height = mHeight;
         mGalaxyState.particlesCount = PARTICLES_COUNT;
         mGalaxyState.galaxyRadius = GALAXY_RADIUS;
+        mGalaxyState.isPreview = isPreview ? 1 : 0;
+        if (isPreview) {
+            mGalaxyState.xOffset = 0.5f;
+        }
 
         mStateType = Type.createFromClass(mRS, GalaxyState.class, 1, "GalaxyState");
         mState = Allocation.createTyped(mRS, mStateType);
@@ -209,23 +216,29 @@
 
     @SuppressWarnings({"PointlessArithmeticExpression"})
     private void createParticle(GalaxyParticle gp, int index, float scale) {
-        float d = abs(randomGauss()) * GALAXY_RADIUS / 2.0f + random(-4.0f, 4.0f);
-        float z = randomGauss() * 0.5f * 0.8f * ((GALAXY_RADIUS - d) / (float) GALAXY_RADIUS);
+        float d = abs(randomGauss()) * GALAXY_RADIUS * 0.5f + random(64.0f);
+        float id = d / (float) GALAXY_RADIUS;
+        float z = randomGauss() * 0.4f * (1.0f - id);
         float p = -d * ELLIPSE_TWIST;
 
-        final float nd = d / (float) GALAXY_RADIUS;
-
         int red, green, blue, alpha;
-        if (d < GALAXY_RADIUS / 3.0f) {
-            red = (int) (220 + nd * 35);
+        if (d < GALAXY_RADIUS * 0.33f) {
+            red = (int) (220 + id * 35);
             green = 220;
             blue = 220;
         } else {
             red = 180;
             green = 180;
-            blue = (int) constrain(140 + nd * 115, 140, 255);
+            blue = (int) constrain(140 + id * 115, 140, 255);
         }
-        alpha = (int) (40 + nd * 215);
+
+        if (d > GALAXY_RADIUS * 0.15f) {
+            z *= 0.6f * (1.0f - id);
+        } else {
+            z *= 0.72f;
+        }
+
+        alpha = (int) (140 + (1.0f - id) * 115);
         int color = red | green << 8 | blue << 16 | alpha << 24;
 
         // Map to the projection coordinates (viewport.x = -1.0 -> 1.0)
diff --git a/src/com/android/wallpaper/galaxy/GalaxyView.java b/src/com/android/wallpaper/galaxy/GalaxyView.java
index 11c46ee..e065fc7 100644
--- a/src/com/android/wallpaper/galaxy/GalaxyView.java
+++ b/src/com/android/wallpaper/galaxy/GalaxyView.java
@@ -35,7 +35,7 @@
 
         RenderScript RS = createRenderScript(false);
         GalaxyRS render = new GalaxyRS(w, h);
-        render.init(RS, getResources());
+        render.init(RS, getResources(), false);
         render.setOffset(0.5f, 0.0f, 0, 0);
         render.start();
     }
diff --git a/src/com/android/wallpaper/grass/GrassView.java b/src/com/android/wallpaper/grass/GrassView.java
index b8c2d8a..3c1b167 100644
--- a/src/com/android/wallpaper/grass/GrassView.java
+++ b/src/com/android/wallpaper/grass/GrassView.java
@@ -35,7 +35,7 @@
 
         RenderScript RS = createRenderScript(false);
         GrassRS render = new GrassRS(w, h);
-        render.init(RS, getResources());
+        render.init(RS, getResources(), false);
         render.setOffset(0.5f, 0.0f, 0, 0);        
         render.start();
     }