blob: cd66b4ffd8b8e792019028a30193e1e6a8f78792 [file] [log] [blame]
Joe Onorato93839052009-08-06 20:34:32 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher2;
18
19import java.io.Writer;
20import java.util.ArrayList;
21import java.util.concurrent.Semaphore;
22import java.lang.Float;
23
24import android.renderscript.RSSurfaceView;
25import android.renderscript.RenderScript;
26
27import android.renderscript.RenderScript;
28import android.renderscript.ProgramVertex;
29import android.renderscript.Element;
30import android.renderscript.Allocation;
31import android.renderscript.Script;
32import android.renderscript.ScriptC;
33import android.renderscript.ProgramFragment;
34import android.renderscript.ProgramStore;
35import android.renderscript.Sampler;
36
37import android.content.Context;
38import android.content.res.Resources;
Joe Onorato7c312c12009-08-13 21:36:53 -070039import android.database.DataSetObserver;
Joe Onorato93839052009-08-06 20:34:32 -070040import android.graphics.Bitmap;
41import android.graphics.BitmapFactory;
42import android.graphics.Canvas;
43import android.graphics.Paint;
Joe Onorato93839052009-08-06 20:34:32 -070044import android.graphics.drawable.BitmapDrawable;
45import android.graphics.drawable.Drawable;
46import android.os.Handler;
47import android.os.Message;
Joe Onoratod769a632009-08-11 17:09:02 -070048import android.os.SystemClock;
Joe Onorato93839052009-08-06 20:34:32 -070049import android.util.AttributeSet;
50import android.util.Log;
Joe Onoratod769a632009-08-11 17:09:02 -070051import android.view.KeyEvent;
52import android.view.MotionEvent;
Joe Onorato93839052009-08-06 20:34:32 -070053import android.view.Surface;
54import android.view.SurfaceHolder;
55import android.view.SurfaceView;
Joe Onoratod769a632009-08-11 17:09:02 -070056import android.view.VelocityTracker;
57import android.view.ViewConfiguration;
Joe Onorato93839052009-08-06 20:34:32 -070058import android.graphics.PixelFormat;
59
60
61public class AllAppsView extends RSSurfaceView {
Joe Onorato9c1289c2009-08-17 11:03:03 -040062 private static final String TAG = "Launcher.AllAppsView";
63
Joe Onorato1feb3a82009-08-08 22:32:00 -070064 private RenderScript mRS;
65 private RolloRS mRollo;
Joe Onorato9c1289c2009-08-17 11:03:03 -040066 private ArrayList<ApplicationInfo> mAllAppsList;
Joe Onorato1feb3a82009-08-08 22:32:00 -070067
Joe Onoratod769a632009-08-11 17:09:02 -070068 private ViewConfiguration mConfig;
69 private VelocityTracker mVelocity;
70 private int mLastScrollX;
Joe Onorato1feb3a82009-08-08 22:32:00 -070071 private int mLastMotionX;
Joe Onorato7c312c12009-08-13 21:36:53 -070072 private ApplicationsAdapter mAdapter;
Joe Onorato1feb3a82009-08-08 22:32:00 -070073
Joe Onorato7c312c12009-08-13 21:36:53 -070074
75 public AllAppsView(Context context, AttributeSet attrs) {
76 super(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -070077 setFocusable(true);
78 getHolder().setFormat(PixelFormat.TRANSLUCENT);
Joe Onoratod769a632009-08-11 17:09:02 -070079 mConfig = ViewConfiguration.get(context);
Joe Onorato93839052009-08-06 20:34:32 -070080 }
81
Joe Onorato7c312c12009-08-13 21:36:53 -070082 public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
83 this(context, attrs);
Joe Onorato93839052009-08-06 20:34:32 -070084 }
85
Joe Onorato7c312c12009-08-13 21:36:53 -070086 void setAdapter(ApplicationsAdapter adapter) {
87 if (mAdapter != null) {
88 mAdapter.unregisterDataSetObserver(mIconObserver);
89 }
90 mAdapter = adapter;
91 if (adapter != null) {
92 adapter.registerDataSetObserver(mIconObserver);
93 }
Joe Onorato93839052009-08-06 20:34:32 -070094 }
95
Joe Onorato1feb3a82009-08-08 22:32:00 -070096 @Override
Joe Onorato93839052009-08-06 20:34:32 -070097 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
98 super.surfaceChanged(holder, format, w, h);
99
Jason Samsb58cbdc2009-08-21 16:56:58 -0700100 mRS = createRenderScript(true);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700101 mRollo = new RolloRS();
102 mRollo.init(getResources(), w, h);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400103 if (mAllAppsList != null) {
104 mRollo.setApps(mAllAppsList);
105 }
Joe Onorato93839052009-08-06 20:34:32 -0700106 }
107
108 @Override
109 public boolean onKeyDown(int keyCode, KeyEvent event)
110 {
Joe Onorato93839052009-08-06 20:34:32 -0700111 // this method doesn't work when 'extends View' include 'extends ScrollView'.
112 return super.onKeyDown(keyCode, event);
113 }
114
Joe Onorato93839052009-08-06 20:34:32 -0700115 @Override
116 public boolean onTouchEvent(MotionEvent ev)
117 {
Joe Onorato1feb3a82009-08-08 22:32:00 -0700118 int x = (int)ev.getX();
119 int deltaX;
120 switch (ev.getAction()) {
121 case MotionEvent.ACTION_DOWN:
122 mLastMotionX = x;
Joe Onoratod769a632009-08-11 17:09:02 -0700123 mRollo.mState.read();
124 mRollo.mState.scrollX = mLastScrollX = mRollo.mState.currentScrollX;
125 mRollo.mState.flingVelocityX = 0;
126 mRollo.mState.adjustedDeceleration = 0;
127 mRollo.mState.save();
128 mVelocity = VelocityTracker.obtain();
129 mVelocity.addMovement(ev);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700130 break;
131 case MotionEvent.ACTION_MOVE:
132 case MotionEvent.ACTION_OUTSIDE:
133 deltaX = x - mLastMotionX;
Joe Onoratod769a632009-08-11 17:09:02 -0700134 mVelocity.addMovement(ev);
135 mRollo.mState.currentScrollX = mLastScrollX;
136 mLastScrollX += deltaX;
137 mRollo.mState.scrollX = mLastScrollX;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700138 mRollo.mState.save();
139 mLastMotionX = x;
140 break;
141 case MotionEvent.ACTION_UP:
142 case MotionEvent.ACTION_CANCEL:
Joe Onoratod769a632009-08-11 17:09:02 -0700143 mVelocity.computeCurrentVelocity(1000 /* px/sec */,
144 mConfig.getScaledMaximumFlingVelocity());
145 mRollo.mState.flingTimeMs = (int)SystemClock.uptimeMillis(); // TODO: use long
146 mRollo.mState.flingVelocityX = (int)mVelocity.getXVelocity();
147 mRollo.mState.save();
Joe Onorato1feb3a82009-08-08 22:32:00 -0700148 mLastMotionX = -10000;
Joe Onoratod769a632009-08-11 17:09:02 -0700149 mVelocity.recycle();
150 mVelocity = null;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700151 break;
Joe Onorato93839052009-08-06 20:34:32 -0700152 }
Joe Onorato1feb3a82009-08-08 22:32:00 -0700153 return true;
Joe Onorato93839052009-08-06 20:34:32 -0700154 }
155
156 @Override
157 public boolean onTrackballEvent(MotionEvent ev)
158 {
159 float x = ev.getX();
160 float y = ev.getY();
161 //Float tx = new Float(x);
162 //Float ty = new Float(y);
163 //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());
164
165
166 return true;
167 }
168
Joe Onorato7c312c12009-08-13 21:36:53 -0700169 DataSetObserver mIconObserver = new DataSetObserver() {
170 public void onChanged() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400171 Log.d(TAG, "new icons arrived! now have " + mAdapter.getCount());
Joe Onorato7c312c12009-08-13 21:36:53 -0700172 }
173 };
174
Joe Onorato9c1289c2009-08-17 11:03:03 -0400175 public void setApps(ArrayList<ApplicationInfo> list) {
176 mAllAppsList = list;
177 if (mRollo != null) {
178 mRollo.setApps(list);
179 }
180 }
181
Joe Onorato93839052009-08-06 20:34:32 -0700182 public class RolloRS {
Joe Onoratobf15cb42009-08-07 14:33:40 -0700183
Joe Onorato1feb3a82009-08-08 22:32:00 -0700184 // Allocations ======
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700185
Joe Onorato93839052009-08-06 20:34:32 -0700186 private int mWidth;
187 private int mHeight;
188
189 private Resources mRes;
Joe Onorato93839052009-08-06 20:34:32 -0700190 private Script mScript;
191 private Sampler mSampler;
192 private Sampler mSamplerText;
193 private ProgramStore mPSBackground;
194 private ProgramStore mPSText;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700195 private ProgramFragment mPFDebug;
Joe Onorato93839052009-08-06 20:34:32 -0700196 private ProgramFragment mPFImages;
197 private ProgramFragment mPFText;
198 private ProgramVertex mPV;
199 private ProgramVertex.MatrixAllocation mPVAlloc;
200 private ProgramVertex mPVOrtho;
201 private ProgramVertex.MatrixAllocation mPVOrthoAlloc;
Joe Onorato93839052009-08-06 20:34:32 -0700202
Joe Onoratobf15cb42009-08-07 14:33:40 -0700203 private Allocation[] mIcons;
Joe Onorato93839052009-08-06 20:34:32 -0700204 private int[] mAllocIconIDBuf;
205 private Allocation mAllocIconID;
206
Joe Onoratobf15cb42009-08-07 14:33:40 -0700207 private Allocation[] mLabels;
Joe Onorato93839052009-08-06 20:34:32 -0700208 private int[] mAllocLabelIDBuf;
209 private Allocation mAllocLabelID;
210
211 private int[] mAllocScratchBuf;
212 private Allocation mAllocScratch;
213
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700214 Params mParams;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700215 State mState;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700216
Joe Onoratod769a632009-08-11 17:09:02 -0700217 class Defines {
218 public static final int ALLOC_PARAMS = 0;
219 public static final int ALLOC_STATE = 1;
220 public static final int ALLOC_SCRATCH = 2;
221 public static final int ALLOC_ICON_IDS = 3;
222 public static final int ALLOC_LABEL_IDS = 4;
223 }
224
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700225 class Params extends IntAllocation {
226 Params(RenderScript rs) {
227 super(rs);
228 }
229 @AllocationIndex(0) public int bubbleWidth;
230 @AllocationIndex(1) public int bubbleHeight;
231 @AllocationIndex(2) public int bubbleBitmapWidth;
232 @AllocationIndex(3) public int bubbleBitmapHeight;
233 }
234
Joe Onorato1feb3a82009-08-08 22:32:00 -0700235 class State extends IntAllocation {
236 State(RenderScript rs) {
237 super(rs);
238 }
239 @AllocationIndex(0) public int iconCount;
240 @AllocationIndex(1) public int scrollX;
Joe Onoratod769a632009-08-11 17:09:02 -0700241 @AllocationIndex(2) public int flingTimeMs;
242 @AllocationIndex(3) public int flingVelocityX;
243 @AllocationIndex(4) public int adjustedDeceleration;
244 @AllocationIndex(5) public int currentScrollX;
Joe Onoratoeb2c02e2009-08-12 21:40:52 -0700245 @AllocationIndex(6) public int flingDuration;
246 @AllocationIndex(7) public int flingEndPos;
Joe Onorato1feb3a82009-08-08 22:32:00 -0700247 }
248
249 public RolloRS() {
250 }
251
252 public void init(Resources res, int width, int height) {
253 mRes = res;
254 mWidth = width;
255 mHeight = height;
256 initGl();
257 initData();
258 initRs();
259 }
260
261 private void initGl() {
Joe Onorato93839052009-08-06 20:34:32 -0700262 Sampler.Builder sb = new Sampler.Builder(mRS);
263 sb.setMin(Sampler.Value.LINEAR);//_MIP_LINEAR);
264 sb.setMag(Sampler.Value.LINEAR);
265 sb.setWrapS(Sampler.Value.CLAMP);
266 sb.setWrapT(Sampler.Value.CLAMP);
267 mSampler = sb.create();
268
269 sb.setMin(Sampler.Value.NEAREST);
270 sb.setMag(Sampler.Value.NEAREST);
271 mSamplerText = sb.create();
272
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700273 ProgramFragment.Builder dbg = new ProgramFragment.Builder(mRS, null, null);
274 mPFDebug = dbg.create();
275 mPFDebug.setName("PFDebug");
Joe Onorato93839052009-08-06 20:34:32 -0700276
277 ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
278 bf.setTexEnable(true, 0);
279 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
280 mPFImages = bf.create();
281 mPFImages.setName("PF");
282 mPFImages.bindSampler(mSampler, 0);
283
284 bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
285 mPFText = bf.create();
286 mPFText.setName("PFText");
287 mPFText.bindSampler(mSamplerText, 0);
288
289 ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
Joe Onoratoefabe002009-08-28 09:38:18 -0700290 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
Joe Onorato93839052009-08-06 20:34:32 -0700291 bs.setDitherEnable(false);
292 bs.setDepthMask(true);
293 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
294 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
295 mPSBackground = bs.create();
296 mPSBackground.setName("PFS");
297
298 bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
299 bs.setDepthMask(false);
300 bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
301 ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
302 mPSText = bs.create();
303 mPSText.setName("PFSText");
304
305 mPVAlloc = new ProgramVertex.MatrixAllocation(mRS);
306 mPVAlloc.setupProjectionNormalized(mWidth, mHeight);
307
308 ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
309 mPV = pvb.create();
310 mPV.setName("PV");
311 mPV.bindAllocation(mPVAlloc);
312
313 mPVOrthoAlloc = new ProgramVertex.MatrixAllocation(mRS);
314 mPVOrthoAlloc.setupOrthoWindow(mWidth, mHeight);
315
316 pvb.setTextureMatrixEnable(true);
317 mPVOrtho = pvb.create();
318 mPVOrtho.setName("PVOrtho");
319 mPVOrtho.bindAllocation(mPVOrthoAlloc);
320
321 mRS.contextBindProgramVertex(mPV);
322
323 mAllocScratchBuf = new int[32];
Joe Onoratod769a632009-08-11 17:09:02 -0700324 mAllocScratch = Allocation.createSized(mRS, Element.USER_I32, mAllocScratchBuf.length);
Joe Onorato93839052009-08-06 20:34:32 -0700325 mAllocScratch.data(mAllocScratchBuf);
326
327 Log.e("rs", "Done loading named");
Joe Onoratobf15cb42009-08-07 14:33:40 -0700328 }
329
Joe Onorato1feb3a82009-08-08 22:32:00 -0700330 private void initData() {
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700331 mParams = new Params(mRS);
Joe Onorato1feb3a82009-08-08 22:32:00 -0700332 mState = new State(mRS);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700333
Joe Onoratobf15cb42009-08-07 14:33:40 -0700334 final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
Joe Onorato93839052009-08-06 20:34:32 -0700335
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700336 mParams.bubbleWidth = bubble.getBubbleWidth();
337 mParams.bubbleHeight = bubble.getMaxBubbleHeight();
338 mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
339 mParams.bubbleBitmapHeight = bubble.getBitmapHeight();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700340
Joe Onorato1feb3a82009-08-08 22:32:00 -0700341 mParams.save();
342 mState.save();
Joe Onorato9c1289c2009-08-17 11:03:03 -0400343
344 setApps(null);
Joe Onorato93839052009-08-06 20:34:32 -0700345 }
346
Joe Onoratobf15cb42009-08-07 14:33:40 -0700347 Allocation makeTextBitmap(Utilities.BubbleText bubble, String label) {
348 Bitmap b = bubble.createTextBitmap(label);
349 Allocation a = Allocation.createFromBitmap(mRS, b, Element.RGBA_8888, true);
350 b.recycle();
351 return a;
Joe Onorato93839052009-08-06 20:34:32 -0700352 }
353
Joe Onorato1feb3a82009-08-08 22:32:00 -0700354 private void initRs() {
Joe Onorato93839052009-08-06 20:34:32 -0700355 ScriptC.Builder sb = new ScriptC.Builder(mRS);
356 sb.setScript(mRes, R.raw.rollo);
Joe Onorato93839052009-08-06 20:34:32 -0700357 sb.setRoot(true);
Joe Onoratod769a632009-08-11 17:09:02 -0700358 sb.addDefines(Defines.class);
Joe Onorato93839052009-08-06 20:34:32 -0700359 mScript = sb.create();
360 mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
361
Joe Onoratod769a632009-08-11 17:09:02 -0700362 mScript.bindAllocation(mParams.getAllocation(), Defines.ALLOC_PARAMS);
363 mScript.bindAllocation(mState.getAllocation(), Defines.ALLOC_STATE);
364 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
365 mScript.bindAllocation(mAllocScratch, Defines.ALLOC_SCRATCH);
366 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
Joe Onorato93839052009-08-06 20:34:32 -0700367
368 mRS.contextBindRootScript(mScript);
369 }
Joe Onorato93839052009-08-06 20:34:32 -0700370
Joe Onorato9c1289c2009-08-17 11:03:03 -0400371 private void setApps(ArrayList<ApplicationInfo> list) {
372 final int count = list != null ? list.size() : 0;
373 mIcons = new Allocation[count];
374 mAllocIconIDBuf = new int[count];
375 mAllocIconID = Allocation.createSized(mRS, Element.USER_I32, count);
376
377 mLabels = new Allocation[count];
378 mAllocLabelIDBuf = new int[count];
379 mAllocLabelID = Allocation.createSized(mRS, Element.USER_I32, count);
380
381 Element ie8888 = Element.RGBA_8888;
382
383 Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());
384
385 for (int i=0; i<count; i++) {
386 final ApplicationInfo item = list.get(i);
387
388 mIcons[i] = Allocation.createFromBitmap(mRS, item.iconBitmap,
389 Element.RGBA_8888, true);
390 mLabels[i] = Allocation.createFromBitmap(mRS, item.titleBitmap,
391 Element.RGBA_8888, true);
392
393 mIcons[i].uploadToTexture(0);
394 mLabels[i].uploadToTexture(0);
395
396 mAllocIconIDBuf[i] = mIcons[i].getID();
397 mAllocLabelIDBuf[i] = mLabels[i].getID();
398 }
399
400 mAllocIconID.data(mAllocIconIDBuf);
401 mAllocLabelID.data(mAllocLabelIDBuf);
402
403 mState.iconCount = count;
404
405 Log.d("AllAppsView", "mScript=" + mScript + " mAllocIconID=" + mAllocIconID);
406
407 if (mScript != null) { // wtf
408 mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
409 mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);
410 }
411
412 mState.save();
413 }
414 }
Joe Onorato93839052009-08-06 20:34:32 -0700415}
416
417
418