OSDN Git Service

Merge "docs: Add documentation for equals() method" into qt-dev am: 732a127636
[android-x86/frameworks-base.git] / packages / SystemUI / src / com / android / systemui / statusbar / phone / PanelBar.java
1 /*
2  * Copyright (C) 2012 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
17 package com.android.systemui.statusbar.phone;
18
19 import android.content.Context;
20 import android.os.Bundle;
21 import android.os.Parcelable;
22 import android.util.AttributeSet;
23 import android.util.Log;
24 import android.view.MotionEvent;
25 import android.widget.FrameLayout;
26
27 public abstract class PanelBar extends FrameLayout {
28     public static final boolean DEBUG = false;
29     public static final String TAG = PanelBar.class.getSimpleName();
30     private static final boolean SPEW = false;
31     private static final String PANEL_BAR_SUPER_PARCELABLE = "panel_bar_super_parcelable";
32     private static final String STATE = "state";
33     private boolean mBouncerShowing;
34     private boolean mExpanded;
35     protected float mPanelFraction;
36
37     public static final void LOG(String fmt, Object... args) {
38         if (!DEBUG) return;
39         Log.v(TAG, String.format(fmt, args));
40     }
41
42     public static final int STATE_CLOSED = 0;
43     public static final int STATE_OPENING = 1;
44     public static final int STATE_OPEN = 2;
45
46     PanelView mPanel;
47     private int mState = STATE_CLOSED;
48     private boolean mTracking;
49
50     public void go(int state) {
51         if (DEBUG) LOG("go state: %d -> %d", mState, state);
52         mState = state;
53     }
54
55     @Override
56     protected Parcelable onSaveInstanceState() {
57         Bundle bundle = new Bundle();
58         bundle.putParcelable(PANEL_BAR_SUPER_PARCELABLE, super.onSaveInstanceState());
59         bundle.putInt(STATE, mState);
60         return bundle;
61     }
62
63     @Override
64     protected void onRestoreInstanceState(Parcelable state) {
65         if (state == null || !(state instanceof Bundle)) {
66             super.onRestoreInstanceState(state);
67             return;
68         }
69
70         Bundle bundle = (Bundle) state;
71         super.onRestoreInstanceState(bundle.getParcelable(PANEL_BAR_SUPER_PARCELABLE));
72         if (((Bundle) state).containsKey(STATE)) {
73             go(bundle.getInt(STATE, STATE_CLOSED));
74         }
75     }
76
77     public PanelBar(Context context, AttributeSet attrs) {
78         super(context, attrs);
79     }
80
81     @Override
82     protected void onFinishInflate() {
83         super.onFinishInflate();
84     }
85
86     public void setPanel(PanelView pv) {
87         mPanel = pv;
88         pv.setBar(this);
89     }
90
91     public void setBouncerShowing(boolean showing) {
92         mBouncerShowing = showing;
93         int important = showing ? IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS
94                 : IMPORTANT_FOR_ACCESSIBILITY_AUTO;
95
96         setImportantForAccessibility(important);
97         updateVisibility();
98
99         if (mPanel != null) mPanel.setImportantForAccessibility(important);
100     }
101
102     public float getExpansionFraction() {
103         return mPanelFraction;
104     }
105
106     public boolean isExpanded() {
107         return mExpanded;
108     }
109
110     protected void updateVisibility() {
111         mPanel.setVisibility(shouldPanelBeVisible() ? VISIBLE : INVISIBLE);
112     }
113
114     protected boolean shouldPanelBeVisible() {
115         return mExpanded || mBouncerShowing;
116     }
117
118     public boolean panelEnabled() {
119         return true;
120     }
121
122     @Override
123     public boolean onTouchEvent(MotionEvent event) {
124         // Allow subclasses to implement enable/disable semantics
125         if (!panelEnabled()) {
126             if (event.getAction() == MotionEvent.ACTION_DOWN) {
127                 Log.v(TAG, String.format("onTouch: all panels disabled, ignoring touch at (%d,%d)",
128                         (int) event.getX(), (int) event.getY()));
129             }
130             return false;
131         }
132
133         if (event.getAction() == MotionEvent.ACTION_DOWN) {
134             final PanelView panel = mPanel;
135             if (panel == null) {
136                 // panel is not there, so we'll eat the gesture
137                 Log.v(TAG, String.format("onTouch: no panel for touch at (%d,%d)",
138                         (int) event.getX(), (int) event.getY()));
139                 return true;
140             }
141             boolean enabled = panel.isEnabled();
142             if (DEBUG) LOG("PanelBar.onTouch: state=%d ACTION_DOWN: panel %s %s", mState, panel,
143                     (enabled ? "" : " (disabled)"));
144             if (!enabled) {
145                 // panel is disabled, so we'll eat the gesture
146                 Log.v(TAG, String.format(
147                         "onTouch: panel (%s) is disabled, ignoring touch at (%d,%d)",
148                         panel, (int) event.getX(), (int) event.getY()));
149                 return true;
150             }
151         }
152         return mPanel == null || mPanel.onTouchEvent(event);
153     }
154
155     public abstract void panelScrimMinFractionChanged(float minFraction);
156
157     /**
158      * @param frac the fraction from the expansion in [0, 1]
159      * @param expanded whether the panel is currently expanded; this is independent from the
160      *                 fraction as the panel also might be expanded if the fraction is 0
161      */
162     public void panelExpansionChanged(float frac, boolean expanded) {
163         boolean fullyClosed = true;
164         boolean fullyOpened = false;
165         if (SPEW) LOG("panelExpansionChanged: start state=%d", mState);
166         PanelView pv = mPanel;
167         mExpanded = expanded;
168         mPanelFraction = frac;
169         updateVisibility();
170         // adjust any other panels that may be partially visible
171         if (expanded) {
172             if (mState == STATE_CLOSED) {
173                 go(STATE_OPENING);
174                 onPanelPeeked();
175             }
176             fullyClosed = false;
177             final float thisFrac = pv.getExpandedFraction();
178             if (SPEW) LOG("panelExpansionChanged:  -> %s: f=%.1f", pv.getName(), thisFrac);
179             fullyOpened = thisFrac >= 1f;
180         }
181         if (fullyOpened && !mTracking) {
182             go(STATE_OPEN);
183             onPanelFullyOpened();
184         } else if (fullyClosed && !mTracking && mState != STATE_CLOSED) {
185             go(STATE_CLOSED);
186             onPanelCollapsed();
187         }
188
189         if (SPEW) LOG("panelExpansionChanged: end state=%d [%s%s ]", mState,
190                 fullyOpened?" fullyOpened":"", fullyClosed?" fullyClosed":"");
191     }
192
193     public void collapsePanel(boolean animate, boolean delayed, float speedUpFactor) {
194         boolean waiting = false;
195         PanelView pv = mPanel;
196         if (animate && !pv.isFullyCollapsed()) {
197             pv.collapse(delayed, speedUpFactor);
198             waiting = true;
199         } else {
200             pv.resetViews(false /* animate */);
201             pv.setExpandedFraction(0); // just in case
202             pv.cancelPeek();
203         }
204         if (DEBUG) LOG("collapsePanel: animate=%s waiting=%s", animate, waiting);
205         if (!waiting && mState != STATE_CLOSED) {
206             // it's possible that nothing animated, so we replicate the termination
207             // conditions of panelExpansionChanged here
208             go(STATE_CLOSED);
209             onPanelCollapsed();
210         }
211     }
212
213     public void onPanelPeeked() {
214         if (DEBUG) LOG("onPanelPeeked");
215     }
216
217     public boolean isClosed() {
218         return mState == STATE_CLOSED;
219     }
220
221     public void onPanelCollapsed() {
222         if (DEBUG) LOG("onPanelCollapsed");
223     }
224
225     public void onPanelFullyOpened() {
226         if (DEBUG) LOG("onPanelFullyOpened");
227     }
228
229     public void onTrackingStarted() {
230         mTracking = true;
231     }
232
233     public void onTrackingStopped(boolean expand) {
234         mTracking = false;
235     }
236
237     public void onExpandingFinished() {
238         if (DEBUG) LOG("onExpandingFinished");
239     }
240
241     public void onClosingFinished() {
242
243     }
244 }