ExpandableView.java revision eb973565f3efc6417ca35363e4d6c642947775d8
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package com.android.systemui.statusbar;
18
19import android.content.Context;
20import android.util.AttributeSet;
21import android.view.View;
22import android.widget.FrameLayout;
23
24/**
25 * An abstract view for expandable views.
26 */
27public abstract class ExpandableView extends FrameLayout {
28
29    private OnHeightChangedListener mOnHeightChangedListener;
30    protected int mActualHeight;
31    protected int mClipTopAmount;
32    private boolean mActualHeightInitialized;
33
34    public ExpandableView(Context context, AttributeSet attrs) {
35        super(context, attrs);
36    }
37
38    @Override
39    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
40        super.onLayout(changed, left, top, right, bottom);
41        if (!mActualHeightInitialized && mActualHeight == 0) {
42            mActualHeight = getHeight();
43        }
44        mActualHeightInitialized = true;
45    }
46
47    /**
48     * Sets the actual height of this notification. This is different than the laid out
49     * {@link View#getHeight()}, as we want to avoid layouting during scrolling and expanding.
50     */
51    public void setActualHeight(int actualHeight) {
52        mActualHeight = actualHeight;
53        notifyHeightChanged();
54    }
55
56    /**
57     * See {@link #setActualHeight}.
58     *
59     * @return The current actual height of this notification.
60     */
61    public int getActualHeight() {
62        return mActualHeight;
63    }
64
65    /**
66     * @return The maximum height of this notification.
67     */
68    public int getMaxHeight() {
69        return getHeight();
70    }
71
72    /**
73     * @return The minimum height of this notification.
74     */
75    public int getMinHeight() {
76        return getHeight();
77    }
78
79    /**
80     * @return The desired notification height.
81     */
82    public int getIntrinsicHeight() {
83        return mActualHeight;
84    }
85
86    /**
87     * Sets the amount this view should be clipped from the top. This is used when an expanded
88     * notification is scrolling in the top or bottom stack.
89     *
90     * @param clipTopAmount The amount of pixels this view should be clipped from top.
91     */
92    public void setClipTopAmount(int clipTopAmount) {
93        mClipTopAmount = clipTopAmount;
94    }
95
96    public int getClipTopAmount() {
97        return mClipTopAmount;
98    }
99
100    public void setOnHeightChangedListener(OnHeightChangedListener listener) {
101        mOnHeightChangedListener = listener;
102    }
103
104    /**
105     * @return Whether we can expand this views content.
106     */
107    public boolean isContentExpandable() {
108        return false;
109    }
110
111    public void notifyHeightChanged() {
112        if (mOnHeightChangedListener != null) {
113            mOnHeightChangedListener.onHeightChanged(this);
114        }
115    }
116
117    /**
118     * A listener notifying when {@link #getActualHeight} changes.
119     */
120    public interface OnHeightChangedListener {
121        void onHeightChanged(ExpandableView view);
122    }
123}
124