summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher2/AllAppsView.java
blob: 755ba65f4d84e1fbf6b67560333d7f61bc4f107c (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
/*
 * 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.database.DataSetObserver;
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.os.SystemClock;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.VelocityTracker;
import android.view.ViewConfiguration;
import android.graphics.PixelFormat;


public class AllAppsView extends RSSurfaceView {
    private RenderScript mRS;
    private RolloRS mRollo;

    private ViewConfiguration mConfig;
    private VelocityTracker mVelocity;
    private int mLastScrollX;
    private int mLastMotionX;
    private ApplicationsAdapter mAdapter;


    public AllAppsView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setFocusable(true);
        getHolder().setFormat(PixelFormat.TRANSLUCENT);
        mConfig = ViewConfiguration.get(context);
    }

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

    void setAdapter(ApplicationsAdapter adapter) {
        if (mAdapter != null) {
            mAdapter.unregisterDataSetObserver(mIconObserver);
        }
        mAdapter = adapter;
        if (adapter != null) {
            adapter.registerDataSetObserver(mIconObserver);
        }
    }

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

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

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

    @Override
    public boolean onTouchEvent(MotionEvent ev)
    {
        Log.d(Launcher.LOG_TAG, "onTouchEvent " + ev);
        int x = (int)ev.getX();
        int deltaX;
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mLastMotionX = x;
                mRollo.mState.read();
                mRollo.mState.scrollX = mLastScrollX = mRollo.mState.currentScrollX;
                mRollo.mState.flingVelocityX = 0;
                mRollo.mState.adjustedDeceleration = 0;
                mRollo.mState.save();
                mVelocity = VelocityTracker.obtain();
                mVelocity.addMovement(ev);
                break;
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_OUTSIDE:
                deltaX = x - mLastMotionX;
                mVelocity.addMovement(ev);
                mRollo.mState.currentScrollX = mLastScrollX;
                mLastScrollX += deltaX;
                mRollo.mState.scrollX = mLastScrollX;
                mRollo.mState.save();
                mLastMotionX = x;
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                mVelocity.computeCurrentVelocity(1000 /* px/sec */,
                        mConfig.getScaledMaximumFlingVelocity());
                mRollo.mState.flingTimeMs = (int)SystemClock.uptimeMillis(); // TODO: use long
                mRollo.mState.flingVelocityX = (int)mVelocity.getXVelocity();
                mRollo.mState.save();
                mLastMotionX = -10000;
                mVelocity.recycle();
                mVelocity = null;
                break;
        }
        return true;
    }

    @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;
    }

    DataSetObserver mIconObserver = new DataSetObserver() {
        public void onChanged() {
            Log.d(Launcher.LOG_TAG, "new icons arrived! now have " + mAdapter.getCount());
        }
    };

    public class RolloRS {

        // Allocations ======

        private int mWidth;
        private int mHeight;

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

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

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

        private int[] mAllocScratchBuf;
        private Allocation mAllocScratch;

        Params mParams;
        State mState;

        class Defines {
            public static final int ALLOC_PARAMS = 0;
            public static final int ALLOC_STATE = 1;
            public static final int ALLOC_SCRATCH = 2;
            public static final int ALLOC_ICON_IDS = 3;
            public static final int ALLOC_LABEL_IDS = 4;
        }

        class Params extends IntAllocation {
            Params(RenderScript rs) {
                super(rs);
            }
            @AllocationIndex(0) public int bubbleWidth;
            @AllocationIndex(1) public int bubbleHeight;
            @AllocationIndex(2) public int bubbleBitmapWidth;
            @AllocationIndex(3) public int bubbleBitmapHeight;
        }

        class State extends IntAllocation {
            State(RenderScript rs) {
                super(rs);
            }
            @AllocationIndex(0) public int iconCount;
            @AllocationIndex(1) public int scrollX;
            @AllocationIndex(2) public int flingTimeMs;
            @AllocationIndex(3) public int flingVelocityX;
            @AllocationIndex(4) public int adjustedDeceleration;
            @AllocationIndex(5) public int currentScrollX;
            @AllocationIndex(6) public int flingDuration;
            @AllocationIndex(7) public int flingEndPos;
        }

        public RolloRS() {
        }

        public void init(Resources res, int width, int height) {
            mRes = res;
            mWidth = width;
            mHeight = height;
            initGl();
            initData();
            initRs();
        }

        private void initGl() {
            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 dbg = new ProgramFragment.Builder(mRS, null, null);
            mPFDebug = dbg.create();
            mPFDebug.setName("PFDebug");

            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 initData() {
            final int count = 100;
            mParams = new Params(mRS);
            mState = new State(mRS);

            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());

            mParams.bubbleWidth = bubble.getBubbleWidth();
            mParams.bubbleHeight = bubble.getMaxBubbleHeight();
            mParams.bubbleBitmapWidth = bubble.getBitmapWidth();
            mParams.bubbleBitmapHeight = bubble.getBitmapHeight();

            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 i=0; i<count; i++) {
                mIcons[i].uploadToTexture(0);
                mLabels[i].uploadToTexture(0);
                mAllocIconIDBuf[i] = mIcons[i].getID();
                mAllocLabelIDBuf[i] = mLabels[i].getID();
            }
            mAllocIconID.data(mAllocIconIDBuf);
            mAllocLabelID.data(mAllocLabelIDBuf);

            mState.iconCount = count;

            mParams.save();
            mState.save();
        }

        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.setRoot(true);
            sb.addDefines(Defines.class);
            mScript = sb.create();
            mScript.setClearColor(0.0f, 0.0f, 0.0f, 0.0f);

            mScript.bindAllocation(mParams.getAllocation(), Defines.ALLOC_PARAMS);
            mScript.bindAllocation(mState.getAllocation(), Defines.ALLOC_STATE);
            mScript.bindAllocation(mAllocIconID, Defines.ALLOC_ICON_IDS);
            mScript.bindAllocation(mAllocScratch, Defines.ALLOC_SCRATCH);
            mScript.bindAllocation(mAllocLabelID, Defines.ALLOC_LABEL_IDS);

            mRS.contextBindRootScript(mScript);
        }
    }

}