summaryrefslogtreecommitdiffstats
path: root/src/com/android/browser/NavTabScroller.java
blob: 312e2b8719042cb48097d6c132f10a10989daca8 (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
/*
 * Copyright (C) 2011 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.browser;

import android.content.Context;
import android.content.res.Configuration;
import android.database.DataSetObserver;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.android.browser.view.HorizontalScrollView;
import com.android.browser.view.ScrollView;

/**
 * custom view for displaying tabs in the nav screen
 */
public class NavTabScroller extends FrameLayout {

    private LinearLayout mContentView;
    private BaseAdapter mAdapter;
    private SelectableSroller mScroller;
    private int mOrientation;

    public NavTabScroller(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    public NavTabScroller(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public NavTabScroller(Context context) {
        super(context);
        init(context);
    }

    private void init(Context ctx) {
        mOrientation = ctx.getResources().getConfiguration().orientation;
        mScroller = (mOrientation == Configuration.ORIENTATION_LANDSCAPE) ?
                new HorizontalScroller(ctx) : new VerticalScroller(ctx);
        mContentView = mScroller.getContentView();
        View sview = (View) mScroller;
        sview.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
        addView(sview);
    }

    @Override
    protected void onMeasure(int wspec, int hspec) {
        super.onMeasure(wspec, hspec);
        calcPadding();
    }

    private void calcPadding() {
        if (mAdapter.getCount() > 0) {
            View v = mContentView.getChildAt(0);
            if (mOrientation == Configuration.ORIENTATION_PORTRAIT) {
                int pad = (getMeasuredHeight() - v.getMeasuredHeight()) / 2;
                mContentView.setPadding(0, pad, 0, pad);
            } else {
                int pad = (getMeasuredWidth() - v.getMeasuredWidth()) / 2;
                mContentView.setPadding(pad, 0, pad, 0);
            }
        }
    }

    protected void setAdapter(BaseAdapter adapter) {
        mAdapter = adapter;
        mAdapter.registerDataSetObserver(new DataSetObserver() {

            @Override
            public void onChanged() {
                super.onChanged();
                populateList();
            }

            @Override
            public void onInvalidated() {
                super.onInvalidated();
            }
        });
        populateList();
    }

    protected void setSelection(int ix) {
        mScroller.setSelection(ix);
    }

    protected int getSelectionIndex() {
        return mScroller.getSelection();
    }

    protected Tab getSelectedItem() {
        return (Tab) mAdapter.getItem(mScroller.getSelection());
    }

    protected ViewGroup getContentView() {
        return mContentView;
    }

    private void populateList() {
        mContentView.removeAllViewsInLayout();
        for (int i = 0; i < mAdapter.getCount(); i++) {
            NavTabView v = (NavTabView) mAdapter.getView(i, null, mContentView);
            mContentView.addView(v);
        }
    }

    View getSelectedTab() {
        int selected = mScroller.getSelection();
        if ((selected >= 0) && (selected < mContentView.getChildCount())) {
            return mContentView.getChildAt(selected);
        } else {
            return null;
        }
    }

    static interface SelectableSroller {
        void setSelection(int index);
        int getSelection();
        LinearLayout getContentView();

    }

    static class VerticalScroller extends ScrollView implements SelectableSroller  {

        private LinearLayout mContentView;
        private int mSelected;
        private boolean mSnapScroll;

        public VerticalScroller(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init(context);
        }

        public VerticalScroller(Context context, AttributeSet attrs) {
            super(context, attrs);
            init(context);
        }

        public VerticalScroller(Context context) {
            super(context);
            init(context);
        }

        private void init(Context ctx) {
            setHorizontalScrollBarEnabled(false);
            mContentView = new LinearLayout(ctx);
            mContentView.setOrientation(LinearLayout.VERTICAL);
            setVerticalScrollBarEnabled(false);
            setSmoothScrollingEnabled(true);
            mContentView.setLayoutParams(
                    new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            addView(mContentView);

        }

        public LinearLayout getContentView() {
            return mContentView;
        }

        public void setSelection(int ix) {
            mSelected = ix;
        }

        public int getSelection() {
            return mSelected;
        }

        protected void onScrollChanged(int sl, int st, int ol, int ot) {
            int midy = getScrollY() + getHeight() / 2;
            int sel = -1;
            for (int i = 0; i < mContentView.getChildCount(); i++) {
                NavTabView child = (NavTabView) mContentView.getChildAt(i);
                int top = child.getTop();
                int bottom = child.getBottom();
                if (top <= midy && bottom >= midy) {
                    sel = i;
                    break;
                }
            }
            if (sel != -1) {
                if (sel != mSelected) {
                    setSelection(sel);
                }
            }
        }

        @Override
        public boolean onTouchEvent(MotionEvent evt) {
            // save drag state before super call
            boolean dragged = mIsBeingDragged;
            boolean result = super.onTouchEvent(evt);
            if (MotionEvent.ACTION_UP == evt.getActionMasked()) {
                if (mScroller.isFinished() && dragged) {
                    snapToSelected();
                }
            } else if (MotionEvent.ACTION_MOVE == evt.getActionMasked()) {
                NavTabView ntv = (NavTabView) getSelectedView();
                if (mIsBeingDragged && ntv.isHighlighted()) {
                    ntv.setHighlighted(false);
                }
            }
            return result;
        }

        @Override
        public void computeScroll() {
            super.computeScroll();
            if (mScroller.isFinished() && !mIsBeingDragged) {
                if (!mSnapScroll) {
                    snapToSelected();
                } else {
                    // reset snap scrolling flag
                    mSnapScroll = false;
                    NavTabView ntv = (NavTabView) getSelectedView();
                    ntv.setHighlighted(true);
                }
            }
        }

        private void snapToSelected() {
            View v = mContentView.getChildAt(mSelected);
            int top = (v.getTop() + v.getBottom()) / 2;
            top -= getHeight() / 2;
            if (top != getScrollY()) {
                // snap to selected
                mSnapScroll = true;
                smoothScrollTo(0, top);
            } else {
                NavTabView ntv = (NavTabView) getSelectedView();
                ntv.setHighlighted(true);
            }
        }

        protected View getSelectedView() {
            return mContentView.getChildAt(mSelected);
        }

    }

    static class HorizontalScroller extends HorizontalScrollView implements SelectableSroller  {

        private LinearLayout mContentView;
        private int mSelected;
        private boolean mSnapScroll;

        public HorizontalScroller(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init(context);
        }

        public HorizontalScroller(Context context, AttributeSet attrs) {
            super(context, attrs);
            init(context);
        }

        public HorizontalScroller(Context context) {
            super(context);
            init(context);
        }

        private void init(Context ctx) {
            setHorizontalScrollBarEnabled(false);
            mContentView = new LinearLayout(ctx);
            mContentView.setOrientation(LinearLayout.HORIZONTAL);
            setVerticalScrollBarEnabled(false);
            setSmoothScrollingEnabled(true);
            mContentView.setLayoutParams(
                    new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
            addView(mContentView);
        }

        public LinearLayout getContentView() {
            return mContentView;
        }

        public void setSelection(int ix) {
            mSelected = ix;
        }

        public int getSelection() {
            return mSelected;
        }

        protected void onScrollChanged(int sl, int st, int ol, int ot) {
            int midx = getScrollX() + getWidth() / 2;
            int sel = -1;
            for (int i = 0; i < mContentView.getChildCount(); i++) {
                View child = mContentView.getChildAt(i);
                if (child.getLeft() <= midx && child.getRight() >= midx) {
                    sel = i;
                    break;
                }
            }
            if (sel != -1) {
                if (sel != mSelected) {
                    setSelection(sel);
                }
            }
        }

        @Override
        public boolean onTouchEvent(MotionEvent evt) {
            // save drag state before super call
            boolean dragged = mIsBeingDragged;
            boolean result = super.onTouchEvent(evt);
            if (MotionEvent.ACTION_UP == evt.getActionMasked()) {
                if (mScroller.isFinished() && dragged) {
                    snapToSelected();
                }
            } else if (MotionEvent.ACTION_MOVE == evt.getActionMasked()) {
                NavTabView ntv = (NavTabView) getSelectedView();
                if (mIsBeingDragged && ntv.isHighlighted()) {
                    ntv.setHighlighted(false);
                }
            }
            return result;
        }

        @Override
        public void computeScroll() {
            super.computeScroll();
            if (mScroller.isFinished() && !mIsBeingDragged) {
                if (!mSnapScroll) {
                    snapToSelected();
                } else {
                    // reset snap scrolling flag
                    mSnapScroll = false;
                    NavTabView ntv = (NavTabView) getSelectedView();
                    ntv.setHighlighted(true);
                }
            }
        }

        private void snapToSelected() {
            View v = mContentView.getChildAt(mSelected);
            int left = (v.getLeft() + v.getRight()) / 2;
            left -= getWidth() / 2;
            if (left != getScrollX()) {
                // snap to selected
                mSnapScroll = true;
                smoothScrollTo(left, 0);
            } else {
                NavTabView ntv = (NavTabView) getSelectedView();
                ntv.setHighlighted(true);
            }
        }

        protected View getSelectedView() {
            return mContentView.getChildAt(mSelected);
        }

    }

}