clean up rollo / launcher2
diff --git a/src/com/android/launcher2/AllAppsView.java b/src/com/android/launcher2/AllAppsView.java
index dc24220..7bf0c7c 100644
--- a/src/com/android/launcher2/AllAppsView.java
+++ b/src/com/android/launcher2/AllAppsView.java
@@ -40,7 +40,6 @@
 import android.graphics.BitmapFactory;
 import android.graphics.Canvas;
 import android.graphics.Paint;
-import android.graphics.Typeface;
 import android.graphics.drawable.BitmapDrawable;
 import android.graphics.drawable.Drawable;
 import android.os.Handler;
@@ -232,6 +231,7 @@
     }
 
     public class RolloRS {
+
         //public static final int STATE_SELECTED_ID = 0;
         public static final int STATE_DONE = 1;
         //public static final int STATE_PRESSURE = 2;
@@ -243,7 +243,6 @@
         public static final int STATE_COUNT = 8;
         public static final int STATE_TOUCH = 9;
 
-
         public RolloRS() {
         }
 
@@ -253,6 +252,7 @@
             mWidth = width;
             mHeight = height;
             initNamed();
+            initIcons(29);
             initRS();
         }
 
@@ -297,15 +297,15 @@
         private ProgramVertex.MatrixAllocation mPVAlloc;
         private ProgramVertex mPVOrtho;
         private ProgramVertex.MatrixAllocation mPVOrthoAlloc;
-        private Allocation[] mIcons;
-        private Allocation[] mLabels;
 
         private int[] mAllocStateBuf;
         private Allocation mAllocState;
 
+        private Allocation[] mIcons;
         private int[] mAllocIconIDBuf;
         private Allocation mAllocIconID;
 
+        private Allocation[] mLabels;
         private int[] mAllocLabelIDBuf;
         private Allocation mAllocLabelID;
 
@@ -377,52 +377,48 @@
             mAllocScratch.data(mAllocScratchBuf);
 
             Log.e("rs", "Done loading named");
+        }
+        
+        private void initIcons(int count) {
+            mIcons = new Allocation[count];
+            mAllocIconIDBuf = new int[count];
+            mAllocIconID = Allocation.createSized(mRS,
+                Element.USER_I32, mAllocIconIDBuf.length);
 
+            mLabels = new Allocation[count];
+            mAllocLabelIDBuf = new int[mLabels.length];
+            mAllocLabelID = Allocation.createSized(mRS,
+                Element.USER_I32, mLabels.length);
 
+            Element ie8888 = Element.RGBA_8888;
 
-            {
-                mIcons = new Allocation[29];
-                mAllocIconIDBuf = new int[mIcons.length];
-                mAllocIconID = Allocation.createSized(mRS,
-                    Element.USER_I32, mAllocIconIDBuf.length);
+            final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
 
-                mLabels = new Allocation[29];
-                mAllocLabelIDBuf = new int[mLabels.length];
-                mAllocLabelID = Allocation.createSized(mRS,
-                    Element.USER_I32, mLabels.length);
-
-                Element ie8888 = Element.RGBA_8888;
-
-                for (int i=0; i<mIcons.length; i++) {
-                    mIcons[i] = Allocation.createFromBitmapResource(
-                            mRS, mRes, R.raw.maps, ie8888, true);
-                    mLabels[i] = makeTextBitmap("Maps");
-                }
-
-                for(int ct=0; ct < mIcons.length; ct++) {
-                    mIcons[ct].uploadToTexture(0);
-                    mLabels[ct].uploadToTexture(0);
-                    mAllocIconIDBuf[ct] = mIcons[ct].getID();
-                    mAllocLabelIDBuf[ct] = mLabels[ct].getID();
-                }
-                mAllocIconID.data(mAllocIconIDBuf);
-                mAllocLabelID.data(mAllocLabelIDBuf);
+            for (int i=0; i<count; i++) {
+                mIcons[i] = Allocation.createFromBitmapResource(
+                        mRS, mRes, R.raw.maps, ie8888, true);
+                mLabels[i] = makeTextBitmap(bubble, i%9==0 ? "Google Maps" : "Maps");
             }
 
+            for(int ct=0; ct < count; ct++) {
+                mIcons[ct].uploadToTexture(0);
+                mLabels[ct].uploadToTexture(0);
+                mAllocIconIDBuf[ct] = mIcons[ct].getID();
+                mAllocLabelIDBuf[ct] = mLabels[ct].getID();
+            }
+            mAllocIconID.data(mAllocIconIDBuf);
+            mAllocLabelID.data(mAllocLabelIDBuf);
+
+            Log.e("rs", "Done loading icons");
         }
 
-        Allocation makeTextBitmap(String t) {
-            Bitmap b = Bitmap.createBitmap(128, 32, Bitmap.Config.ARGB_8888);
-            Canvas c = new Canvas(b);
-            Paint p = new Paint();
-            p.setTypeface(Typeface.DEFAULT_BOLD);
-            p.setTextSize(20);
-            p.setColor(0xffffffff);
-            c.drawText(t, 2, 26, p);
-            return Allocation.createFromBitmap(mRS, b, Element.RGBA_8888, true);
+        Allocation makeTextBitmap(Utilities.BubbleText bubble, String label) {
+            Bitmap b = bubble.createTextBitmap(label);
+            Allocation a = Allocation.createFromBitmap(mRS, b, Element.RGBA_8888, true);
+            b.recycle();
+            return a;
         }
 
-
         private void initRS() {
             ScriptC.Builder sb = new ScriptC.Builder(mRS);
             sb.setScript(mRes, R.raw.rollo);
diff --git a/src/com/android/launcher2/BubbleTextView.java b/src/com/android/launcher2/BubbleTextView.java
index c357cb2..de375b6 100644
--- a/src/com/android/launcher2/BubbleTextView.java
+++ b/src/com/android/launcher2/BubbleTextView.java
@@ -31,9 +31,9 @@
  * too aggressive.
  */
 public class BubbleTextView extends TextView {
-    private static final float CORNER_RADIUS = 8.0f;
-    private static final float PADDING_H = 5.0f;
-    private static final float PADDING_V = 1.0f;
+    static final float CORNER_RADIUS = 8.0f;
+    static final float PADDING_H = 5.0f;
+    static final float PADDING_V = 1.0f;
 
     private final RectF mRect = new RectF();
     private Paint mPaint;
diff --git a/src/com/android/launcher2/Utilities.java b/src/com/android/launcher2/Utilities.java
index dc61be3..c710615 100644
--- a/src/com/android/launcher2/Utilities.java
+++ b/src/com/android/launcher2/Utilities.java
@@ -20,12 +20,18 @@
 import android.graphics.drawable.Drawable;
 import android.graphics.drawable.PaintDrawable;
 import android.graphics.Bitmap;
-import android.graphics.PixelFormat;
 import android.graphics.Canvas;
-import android.graphics.PaintFlagsDrawFilter;
 import android.graphics.Paint;
+import android.graphics.PaintFlagsDrawFilter;
+import android.graphics.PixelFormat;
 import android.graphics.Rect;
+import android.graphics.RectF;
+import android.graphics.Typeface;
+import android.text.Layout.Alignment;
+import android.text.StaticLayout;
+import android.text.TextPaint;
 import android.util.DisplayMetrics;
+import android.util.Log;
 import android.content.res.Resources;
 import android.content.Context;
 
@@ -201,4 +207,111 @@
 
         return bitmap;
     }
+
+    static class BubbleText {
+        private static final int MAX_LINES = 2;
+        private TextPaint mTextPaint;
+        private Paint mRectPaint;
+
+        private float mBubblePadding;
+        private float mCornerRadius;
+        private RectF mBubbleRect = new RectF();
+
+        private float mTextWidth;
+        private int mLeading;
+        private int mFirstLineY;
+        private int mLineHeight;
+
+        private int mBitmapWidth;
+        private int mBitmapHeight;
+
+        BubbleText(Context context) {
+            final Resources resources = context.getResources();
+
+            final float scale = resources.getDisplayMetrics().density;
+
+            final float paddingLeft = 5.0f * scale;
+            final float paddingRight = 5.0f * scale;
+            final float cellWidth = resources.getDimension(R.dimen.workspace_cell_width);
+            final float bubbleWidth = cellWidth - paddingLeft - paddingRight;
+            mBubblePadding = 5.0f * scale;
+
+            RectF bubbleRect = mBubbleRect;
+            bubbleRect.left = 0;
+            bubbleRect.top = 0;
+            bubbleRect.right = (int)(bubbleWidth+0.5f);
+
+            mCornerRadius = BubbleTextView.CORNER_RADIUS * scale;
+            mTextWidth = bubbleWidth - mBubblePadding - mBubblePadding;
+
+            Paint rectPaint = mRectPaint = new Paint();
+            rectPaint.setColor(0xff000000);
+
+            TextPaint textPaint = mTextPaint = new TextPaint();
+            textPaint.setTypeface(Typeface.DEFAULT_BOLD);
+            textPaint.setTextSize(20);
+            textPaint.setColor(0xffffffff);
+
+            float ascent = -textPaint.ascent();
+            float descent = textPaint.descent();
+            float leading = (ascent+descent) * 0.1f;
+            mLeading = (int)(leading + 0.5f);
+            mFirstLineY = (int)(leading + ascent + 0.5f);
+            mLineHeight = (int)(leading + ascent + descent + 0.5f);
+
+            roundToPow2(64);
+            mBitmapWidth = roundToPow2((int)(mBubbleRect.width() + 0.5f));
+            mBitmapHeight = roundToPow2((int)((MAX_LINES * mLineHeight) + mLeading + 0.5f));
+
+            Log.d(Launcher.LOG_TAG, "mBitmapWidth=" + mBitmapWidth + " mBitmapHeight="
+                    + mBitmapHeight + " w=" + ((int)(mBubbleRect.width() + 0.5f))
+                    + " h=" + ((int)((MAX_LINES * mLineHeight) + mLeading + 0.5f)));
+        }
+
+        /** You own the bitmap after this and you must call recycle on it. */
+        Bitmap createTextBitmap(String text) {
+            Bitmap b = Bitmap.createBitmap(mBitmapWidth, mBitmapHeight, Bitmap.Config.ARGB_8888);
+            Canvas c = new Canvas(b);
+
+            StaticLayout layout = new StaticLayout(text, mTextPaint, (int)mTextWidth,
+                    Alignment.ALIGN_CENTER, 1, 0, true);
+            int lineCount = layout.getLineCount();
+            if (lineCount > MAX_LINES) {
+                lineCount = MAX_LINES;
+            }
+            if (lineCount > 0) {
+                RectF bubbleRect = mBubbleRect;
+                bubbleRect.bottom = (int)((lineCount * mLineHeight) + mLeading + mLeading + 0.0f);
+                c.drawRoundRect(bubbleRect, mCornerRadius, mCornerRadius, mRectPaint);
+                Log.d(Launcher.LOG_TAG, "bubbleRect=" + bubbleRect);
+            }
+            for (int i=0; i<lineCount; i++) {
+                int x = (int)((mBubbleRect.width() - layout.getLineMax(i)) / 2.0f);
+                int y = mFirstLineY + (i * mLineHeight);
+                c.drawText(text.substring(layout.getLineStart(i), layout.getLineEnd(i)),
+                        x, y, mTextPaint);
+            }
+
+            return b;
+        }
+    }
+
+    /** Only works for positive numbers. */
+    static int roundToPow2(int n) {
+        int orig = n;
+        n >>= 1;
+        int mask = 0x8000000;
+        while (mask != 0 && (n & mask) == 0) {
+            mask >>= 1;
+        }
+        while (mask != 0) {
+            n |= mask;
+            mask >>= 1;
+        }
+        n += 1;
+        if (n != orig) {
+            n <<= 1;
+        }
+        return n;
+    }
 }