summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher2/AllAppsView.java
blob: 7bf0c7c6b68ab8a9cd33f7b3b74efca27b944d83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.launcher2;

import java.io.Writer;
import java.util.ArrayList;
import java.util.concurrent.Semaphore;
import java.lang.Float;

import android.renderscript.RSSurfaceView;
import android.renderscript.RenderScript;

import android.renderscript.RenderScript;
import android.renderscript.ProgramVertex;
import android.renderscript.Element;
import android.renderscript.Allocation;
import android.renderscript.Script;
import android.renderscript.ScriptC;
import android.renderscript.ProgramFragment;
import android.renderscript.ProgramStore;
import android.renderscript.Sampler;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.graphics.PixelFormat;


public class AllAppsView extends RSSurfaceView {
    public AllAppsView(Context context) {
        super(context);
        setFocusable(true);
        getHolder().setFormat(PixelFormat.TRANSLUCENT);
    }

    public AllAppsView(Context context, AttributeSet attrs) {
        this(context);
    }

    public AllAppsView(Context context, AttributeSet attrs, int defStyle) {
        this(context);
    }

    private RenderScript mRS;
    private RolloRS mRender;

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        super.surfaceChanged(holder, format, w, h);

        mRS = createRenderScript();
        mRender = new RolloRS();
        mRender.init(mRS, getResources(), w, h);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        // break point at here
        // this method doesn't work when 'extends View' include 'extends ScrollView'.
        return super.onKeyDown(keyCode, event);
    }

    boolean mControlMode = false;
    boolean mZoomMode = false;
    boolean mFlingMode = false;
    float mFlingX = 0;
    float mFlingY = 0;
    float mColumn = -1;
    float mOldColumn;
    float mZoom = 1;

    int mIconCount = 29;
    int mRows = 4;
    int mColumns = (mIconCount + mRows - 1) / mRows;

    float mMaxZoom = ((float)mColumns) / 3.f;


    void setColumn(boolean clamp)
    {
        //Log.e("rs", " col = " + Float.toString(mColumn));
        float c = mColumn;
        if(c > (mColumns -2)) {
            c = (mColumns -2);
        }
        if(c < 0) {
            c = 0;
        }
        mRender.setPosition(c);
        if(clamp) {
            mColumn = c;
        }
    }

    void computeSelection(float x, float y)
    {
        float col = mColumn + (x - 0.5f) * 4 + 1.25f;
        int iCol = (int)(col + 0.25f);

        float row = (y / 0.8f) * mRows;
        int iRow = (int)(row - 0.5f);

        mRender.setSelected(iCol * mRows + iRow);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev)
    {
        boolean ret = true;
        int act = ev.getAction();
        if (act == ev.ACTION_UP) {
            ret = false;
        }

        float nx = ev.getX() / getWidth();
        float ny = ev.getY() / getHeight();

        //Log.e("rs", "width=" + Float.toString(getWidth()));
        //Log.e("rs", "height=" + Float.toString(getHeight()));

        mRender.setTouch(ret);

        if((ny > 0.85f) || mControlMode) {
            mFlingMode = false;

            // Projector control
            if((nx > 0.2f) && (nx < 0.8f) || mControlMode) {
                if(act != ev.ACTION_UP) {
                    float zoom = mMaxZoom;
                    if(mControlMode) {
                        if(!mZoomMode) {
                            zoom = 1.f;
                        }
                        float dx = nx - mFlingX;

                        if((ny < 0.9) && mZoomMode) {
                            zoom = mMaxZoom - ((0.9f - ny) * 10.f);
                            if(zoom < 1) {
                                zoom = 1;
                                mZoomMode = false;
                            }
                            mOldColumn = mColumn;
                        }
                        mColumn += dx * 4;// * zoom;
                        if(zoom > 1.01f) {
                            mColumn += (mZoom - zoom) * (nx - 0.5f) * 4 * zoom;
                        }
                    } else {
                        mOldColumn = mColumn;
                        mColumn = ((float)mColumns) / 2;
                        mControlMode = true;
                        mZoomMode = true;
                    }
                    mZoom = zoom;
                    mFlingX = nx;
                    mRender.setZoom(zoom);
                    if(mZoom < 1.01f) {
                        computeSelection(nx, ny);
                    }
                } else {
                    mControlMode = false;
                    mColumn = mOldColumn;
                    mRender.setZoom(1.f);
                    mRender.setSelected(-1);
                }
            } else {
                // Do something with corners here....
            }
            setColumn(true);

        } else {
            // icon control
            if(act != ev.ACTION_UP) {
                if(mFlingMode) {
                    mColumn += (mFlingX - nx) * 4;
                    setColumn(true);
                }
                mFlingMode = true;
                mFlingX = nx;
                mFlingY = ny;
            } else {
                mFlingMode = false;
                mColumn = (float)(java.lang.Math.floor(mColumn * 0.25f + 0.3f) * 4.f) + 1.f;
                setColumn(true);
            }
        }


        return ret;
    }

    @Override
    public boolean onTrackballEvent(MotionEvent ev)
    {
        float x = ev.getX();
        float y = ev.getY();
        //Float tx = new Float(x);
        //Float ty = new Float(y);
        //Log.e("rs", "tbe " + tx.toString() + ", " + ty.toString());


        return true;
    }

    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;
        public static final int STATE_ZOOM = 3;
        //public static final int STATE_WARP = 4;
        public static final int STATE_ORIENTATION = 5;
        public static final int STATE_SELECTION = 6;
        public static final int STATE_FIRST_VISIBLE = 7;
        public static final int STATE_COUNT = 8;
        public static final int STATE_TOUCH = 9;

        public RolloRS() {
        }

        public void init(RenderScript rs, Resources res, int width, int height) {
            mRS = rs;
            mRes = res;
            mWidth = width;
            mHeight = height;
            initNamed();
            initIcons(29);
            initRS();
        }

        public void setPosition(float column) {
            mAllocStateBuf[STATE_FIRST_VISIBLE] = (int)(column * (-20));
            mAllocState.data(mAllocStateBuf);
        }

        public void setTouch(boolean touch) {
            mAllocStateBuf[STATE_TOUCH] = touch ? 1 : 0;
            mAllocState.data(mAllocStateBuf);
        }

        public void setZoom(float z) {
            //Log.e("rs", "zoom " + Float.toString(z));

            mAllocStateBuf[STATE_ZOOM] = (int)(z * 1000.f);
            mAllocState.data(mAllocStateBuf);
        }

        public void setSelected(int index) {
            //Log.e("rs",  "setSelected " + Integer.toString(index));

            mAllocStateBuf[STATE_SELECTION] = index;
            mAllocStateBuf[STATE_DONE] = 1;
            mAllocState.data(mAllocStateBuf);
        }

        private int mWidth;
        private int mHeight;

        private Resources mRes;
        private RenderScript mRS;
        private Script mScript;
        private Sampler mSampler;
        private Sampler mSamplerText;
        private ProgramStore mPSBackground;
        private ProgramStore mPSText;
        private ProgramFragment mPFImages;
        private ProgramFragment mPFText;
        private ProgramVertex mPV;
        private ProgramVertex.MatrixAllocation mPVAlloc;
        private ProgramVertex mPVOrtho;
        private ProgramVertex.MatrixAllocation mPVOrthoAlloc;

        private int[] mAllocStateBuf;
        private Allocation mAllocState;

        private Allocation[] mIcons;
        private int[] mAllocIconIDBuf;
        private Allocation mAllocIconID;

        private Allocation[] mLabels;
        private int[] mAllocLabelIDBuf;
        private Allocation mAllocLabelID;

        private int[] mAllocScratchBuf;
        private Allocation mAllocScratch;

        private void initNamed() {
            Sampler.Builder sb = new Sampler.Builder(mRS);
            sb.setMin(Sampler.Value.LINEAR);//_MIP_LINEAR);
            sb.setMag(Sampler.Value.LINEAR);
            sb.setWrapS(Sampler.Value.CLAMP);
            sb.setWrapT(Sampler.Value.CLAMP);
            mSampler = sb.create();

            sb.setMin(Sampler.Value.NEAREST);
            sb.setMag(Sampler.Value.NEAREST);
            mSamplerText = sb.create();


            ProgramFragment.Builder bf = new ProgramFragment.Builder(mRS, null, null);
            bf.setTexEnable(true, 0);
            bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
            mPFImages = bf.create();
            mPFImages.setName("PF");
            mPFImages.bindSampler(mSampler, 0);

            bf.setTexEnvMode(ProgramFragment.EnvMode.MODULATE, 0);
            mPFText = bf.create();
            mPFText.setName("PFText");
            mPFText.bindSampler(mSamplerText, 0);

            ProgramStore.Builder bs = new ProgramStore.Builder(mRS, null, null);
            bs.setDepthFunc(ProgramStore.DepthFunc.LESS);
            bs.setDitherEnable(false);
            bs.setDepthMask(true);
            bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
                            ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
            mPSBackground = bs.create();
            mPSBackground.setName("PFS");

            bs.setDepthFunc(ProgramStore.DepthFunc.ALWAYS);
            bs.setDepthMask(false);
            bs.setBlendFunc(ProgramStore.BlendSrcFunc.SRC_ALPHA,
                            ProgramStore.BlendDstFunc.ONE_MINUS_SRC_ALPHA);
            mPSText = bs.create();
            mPSText.setName("PFSText");

            mPVAlloc = new ProgramVertex.MatrixAllocation(mRS);
            mPVAlloc.setupProjectionNormalized(mWidth, mHeight);

            ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
            mPV = pvb.create();
            mPV.setName("PV");
            mPV.bindAllocation(mPVAlloc);

            mPVOrthoAlloc = new ProgramVertex.MatrixAllocation(mRS);
            mPVOrthoAlloc.setupOrthoWindow(mWidth, mHeight);

            pvb.setTextureMatrixEnable(true);
            mPVOrtho = pvb.create();
            mPVOrtho.setName("PVOrtho");
            mPVOrtho.bindAllocation(mPVOrthoAlloc);

            mRS.contextBindProgramVertex(mPV);

            mAllocScratchBuf = new int[32];
            mAllocScratch = Allocation.createSized(mRS,
                Element.USER_I32, mAllocScratchBuf.length);
            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;

            final Utilities.BubbleText bubble = new Utilities.BubbleText(getContext());

            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(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);
            //sb.setScript(mRes, R.raw.rollo2);
            sb.setRoot(true);
            mScript = sb.create();
            mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);

            mAllocStateBuf = new int[] {0, 0, 0, 8, 0, 0, -1, 0, mAllocIconIDBuf.length, 0, 0};
            mAllocState = Allocation.createSized(mRS,
                Element.USER_I32, mAllocStateBuf.length);
            mScript.bindAllocation(mAllocState, 0);
            mScript.bindAllocation(mAllocIconID, 1);
            mScript.bindAllocation(mAllocScratch, 2);
            mScript.bindAllocation(mAllocLabelID, 3);
            setPosition(0);
            setZoom(1);

            //RenderScript.File f = mRS.fileOpen("/sdcard/test.a3d");

            mRS.contextBindRootScript(mScript);
        }
    }

}