1/*
2**
3** Copyright 2012, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9**     http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18package com.android.packageinstaller;
19
20import android.content.Context;
21import android.graphics.Canvas;
22import android.util.AttributeSet;
23import android.widget.ScrollView;
24
25/**
26 * It's a ScrollView that knows how to stay awake.
27 */
28class CaffeinatedScrollView extends ScrollView {
29    private Runnable mFullScrollAction;
30    private int mBottomSlop;
31
32    public CaffeinatedScrollView(Context context) {
33        super(context);
34    }
35
36    public CaffeinatedScrollView(Context context, AttributeSet attrs) {
37        super(context, attrs);
38    }
39
40    /**
41     * Make this visible so we can call it
42     */
43    @Override
44    public boolean awakenScrollBars() {
45        return super.awakenScrollBars();
46    }
47
48    public void setFullScrollAction(Runnable action) {
49        mFullScrollAction = action;
50        mBottomSlop = (int)(4 * getResources().getDisplayMetrics().density);
51    }
52
53    @Override
54    protected void onDraw(Canvas canvas) {
55        super.onDraw(canvas);
56        checkFullScrollAction();
57    }
58
59    @Override
60    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
61        super.onScrollChanged(l, t, oldl, oldt);
62        checkFullScrollAction();
63    }
64
65    private void checkFullScrollAction() {
66        if (mFullScrollAction != null) {
67            int daBottom = getChildAt(0).getBottom();
68            int screenBottom = getScrollY() + getHeight() - getPaddingBottom();
69            if ((daBottom - screenBottom) < mBottomSlop) {
70                mFullScrollAction.run();
71                mFullScrollAction = null;
72            }
73        }
74    }
75}
76