summaryrefslogtreecommitdiffstats
path: root/src/com/android/camera/ui/GLListView.java
blob: c1bc11ab35e6630a4c1dbbabee24717f6de99002 (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
/*
 * Copyright (C) 2010 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.camera.ui;

import static android.view.View.MeasureSpec.makeMeasureSpec;

import android.content.Context;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View.MeasureSpec;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.Scroller;

import com.android.camera.Util;

import javax.microedition.khronos.opengles.GL11;

class GLListView extends GLView {
    @SuppressWarnings("unused")
    private static final String TAG = "GLListView";
    private static final int INDEX_NONE = -1;
    private static final int SCROLL_BAR_TIMEOUT = 2500;

    private static final int HIDE_SCROLL_BAR = 1;

    private Model mModel;
    private Handler mHandler;

    private int mHighlightIndex = INDEX_NONE;
    private GLView mHighlightView;

    private Texture mHighLight;
    private NinePatchTexture mScrollbar;

    private int mVisibleStart = 0; // inclusive
    private int mVisibleEnd = 0; // exclusive

    private boolean mHasMeasured = false;

    private boolean mScrollBarVisible = false;
    private Animation mScrollBarAnimation;
    private OnItemSelectedListener mOnItemSelectedListener;

    private GestureDetector mGestureDetector;
    private final Scroller mScroller;
    private boolean mScrollable;
    private boolean mIsPressed = false;

    static public interface Model {
        public int size();
        public GLView getView(int index);
        public boolean isSelectable(int index);
    }

    static public interface OnItemSelectedListener {
        public void onItemSelected(GLView view, int position);
    }

    public GLListView(Context context) {
        mScroller = new Scroller(context);
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                GLRootView root = getGLRootView();
                if (root != null) {
                    synchronized (root) {
                        handleMessageLocked(msg);
                    }
                } else {
                    handleMessageLocked(msg);
                }
            }

            private void handleMessageLocked(Message msg) {
                switch(msg.what) {
                    case HIDE_SCROLL_BAR:
                        setScrollBarVisible(false);
                        break;
                }
            }
        };
        mGestureDetector = new GestureDetector(
                context, new MyGestureListener(), mHandler);
    }

    @Override
    protected void onVisibilityChanged(int visibility) {
        super.onVisibilityChanged(visibility);
        if (visibility == GLView.VISIBLE && mScrollHeight > getHeight()) {
            setScrollBarVisible(true);
            mHandler.sendEmptyMessageDelayed(
                    HIDE_SCROLL_BAR, SCROLL_BAR_TIMEOUT);
        }
    }

    private void setScrollBarVisible(boolean visible) {
        if (mScrollBarVisible == visible || mScrollbar == null) return;
        mScrollBarVisible = visible;
        if (!visible) {
            mScrollBarAnimation = new AlphaAnimation(1, 0);
            mScrollBarAnimation.setDuration(300);
            mScrollBarAnimation.start();
        } else {
            mScrollBarAnimation = null;
        }
        invalidate();
    }

    public void setHighLight(Texture highLight) {
        mHighLight = highLight;
    }

    public void setDataModel(Model model) {
        mModel = model;
        mScrollY = 0;
        requestLayout();
    }

    public void setOnItemSelectedListener(OnItemSelectedListener l) {
        mOnItemSelectedListener = l;
    }

    private boolean drawWithAnimation(GLRootView root,
            Texture texture, int x, int y, int w, int h, Animation anim) {
        long now = root.currentAnimationTimeMillis();
        Transformation temp = root.obtainTransformation();
        boolean more = anim.getTransformation(now, temp);
        Transformation transformation = root.pushTransform();
        transformation.compose(temp);
        texture.draw(root, x, y, w, h);
        invalidate();
        root.popTransform();
        return more;
    }

    @Override
    protected void render(GLRootView root, GL11 gl) {
        root.clipRect(0, 0, getWidth(), getHeight());
        if (mHighlightIndex != INDEX_NONE) {
            GLView view = mModel.getView(mHighlightIndex);
            Rect bounds = view.bounds();
            if (mHighLight != null) {
                int width = bounds.width();
                int height = bounds.height();
                mHighLight.draw(root,
                        bounds.left - mScrollX, bounds.top - mScrollY,
                        width, height);
            }
        }
        super.render(root, gl);
        root.clearClip();

        if (mScrollBarAnimation != null || mScrollBarVisible) {
            int width = mScrollbar.getWidth();
            int height = getHeight() * getHeight() / mScrollHeight;
            int yoffset = mScrollY * getHeight() / mScrollHeight;
            if (mScrollBarAnimation != null) {
                if (!drawWithAnimation(
                        root, mScrollbar, getWidth() - width, yoffset,
                        width, height, mScrollBarAnimation)) {
                    mScrollBarAnimation = null;
                }
            } else {
                mScrollbar.draw(
                        root, getWidth() - width, yoffset, width, height);
            }
        }
        if (mScroller.computeScrollOffset()) {
            setScrollPosition(mScroller.getCurrY(), false);
        }
    }

    @Override
    protected void onMeasure(int widthSpec, int heightSpec) {
        // first get the total height
        int height = 0;
        int maxWidth = 0;
        for (int i = 0, n = mModel.size(); i < n; ++i) {
            GLView view = mModel.getView(i);
            view.measure(widthSpec, MeasureSpec.UNSPECIFIED);
            height += view.getMeasuredHeight();
            maxWidth = Math.max(maxWidth, view.getMeasuredWidth());
        }
        mScrollHeight = height;
        mHasMeasured = true;
        new MeasureHelper(this)
                .setPreferredContentSize(maxWidth, height)
                .measure(widthSpec, heightSpec);
    }

    @Override
    public int getComponentCount() {
        return mVisibleEnd - mVisibleStart;
    }

    @Override
    public GLView getComponent(int index) {
        if (index < 0 || index >= mVisibleEnd - mVisibleStart) {
            throw new ArrayIndexOutOfBoundsException(index);
        }
        return mModel.getView(mVisibleStart + index);
    }

    @Override
    public void requestLayout() {
        mHasMeasured = false;
        super.requestLayout();
    }

    @Override
    protected void onLayout(
            boolean change, int left, int top, int right, int bottom) {

        if (!mHasMeasured || mMeasuredWidth != (right - left)) {
            measure(makeMeasureSpec(right - left, MeasureSpec.EXACTLY),
                    makeMeasureSpec(bottom - top, MeasureSpec.EXACTLY));
        }

        mScrollable = mScrollHeight > (bottom - top);
        int width = right - left;
        int yoffset = 0;

        for (int i = 0, n = mModel.size(); i < n; ++i) {
            GLView item = mModel.getView(i);
            item.onAddToParent(this);
            int nextOffset = yoffset + item.getMeasuredHeight();
            item.layout(0, yoffset, width, nextOffset);
            yoffset = nextOffset;
        }
        setScrollPosition(mScrollY, true);
    }

    private void setScrollPosition(int position, boolean force) {
        int height = getHeight();

        position = Util.clamp(position, 0, mScrollHeight - height);

        if (!force && position == mScrollY) return;
        mScrollY = position;

        int n = mModel.size();

        int start = 0;
        int end = 0;
        for (start = 0; start < n; ++start) {
            if (position < mModel.getView(start).mBounds.bottom) break;
        }

        int bottom = position + height;
        for (end = start; end < n; ++ end) {
            if (bottom <= mModel.getView(end).mBounds.top) break;
        }
        setVisibleRange(start , end);
        invalidate();
    }

    private void setVisibleRange(int start, int end) {
        if (start == mVisibleStart && end == mVisibleEnd) return;
        mVisibleStart = start;
        mVisibleEnd = end;
    }

    @Override
    protected boolean dispatchTouchEvent(MotionEvent event) {
        return onTouch(event);
    }

    @Override @SuppressWarnings("fallthrough")
    protected boolean onTouch(MotionEvent event) {

        mGestureDetector.onTouchEvent(event);

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mIsPressed = true;
                mHandler.removeMessages(HIDE_SCROLL_BAR);
                setScrollBarVisible(mScrollHeight > getHeight());

                // fallthrough: we need to highlight the item which is pressed
            case MotionEvent.ACTION_MOVE:
                if (!mScrollable) {
                    findAndSetHighlightItem((int) event.getY());
                }
                break;
            case MotionEvent.ACTION_UP:
                mIsPressed = false;
                if (mScrollBarVisible) {
                    mHandler.removeMessages(HIDE_SCROLL_BAR);
                    mHandler.sendEmptyMessageDelayed(
                            HIDE_SCROLL_BAR, SCROLL_BAR_TIMEOUT);
                }
                if (!mScrollable && mOnItemSelectedListener != null
                        && mHighlightView != null) {
                    mOnItemSelectedListener
                            .onItemSelected(mHighlightView, mHighlightIndex);
                }
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_OUTSIDE:
                setHighlightItem(null, INDEX_NONE);
        }
        return true;
    }

    private void findAndSetHighlightItem(int y) {
        int position = y + mScrollY;
        for (int i = mVisibleStart, n = mVisibleEnd; i < n; ++i) {
            GLView child = mModel.getView(i);
            if (child.mBounds.bottom > position) {
                if (mModel.isSelectable(i)) {
                    setHighlightItem(child, i);
                    return;
                }
                break;
            }
        }
        setHighlightItem(null, INDEX_NONE);
    }

    private void setHighlightItem(GLView view, int index) {
        if (index == mHighlightIndex) return;
        mHighlightIndex = index;
        mHighlightView = view;
        if (mHighLight != null) invalidate();
    }

    public void setScroller(NinePatchTexture scrollbar) {
        this.mScrollbar = scrollbar;
        requestLayout();
    }

    private class MyGestureListener
            extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onFling(MotionEvent e1,
                MotionEvent e2, float velocityX, float velocityY) {
            if (!mScrollable) return false;
            mScroller.fling(0, mScrollY,
                    0, -(int) velocityY, 0, 0, 0, mScrollHeight - getHeight());
            invalidate();
            return true;
        }

        @Override
        public boolean onScroll(MotionEvent e1,
                MotionEvent e2, float distanceX, float distanceY) {
            if (!mScrollable) return false;
            setHighlightItem(null, INDEX_NONE);
            setScrollPosition(mScrollY + (int) distanceY, false);
            return true;
        }

        @Override
        public void onShowPress(MotionEvent e) {
            if (!mScrollable || !mIsPressed) return;
            findAndSetHighlightItem((int) e.getY());
        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            if (!mScrollable) return false;
            findAndSetHighlightItem((int) e.getY());
            if (mOnItemSelectedListener != null && mHighlightView != null) {
                mOnItemSelectedListener
                        .onItemSelected(mHighlightView, mHighlightIndex);
            }
            setHighlightItem(null, INDEX_NONE);
            return true;
        }
    }
}