2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.systemui.statusbar.phone;
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;
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;
37 public static final void LOG(String fmt, Object... args) {
39 Log.v(TAG, String.format(fmt, args));
42 public static final int STATE_CLOSED = 0;
43 public static final int STATE_OPENING = 1;
44 public static final int STATE_OPEN = 2;
47 private int mState = STATE_CLOSED;
48 private boolean mTracking;
50 public void go(int state) {
51 if (DEBUG) LOG("go state: %d -> %d", mState, state);
56 protected Parcelable onSaveInstanceState() {
57 Bundle bundle = new Bundle();
58 bundle.putParcelable(PANEL_BAR_SUPER_PARCELABLE, super.onSaveInstanceState());
59 bundle.putInt(STATE, mState);
64 protected void onRestoreInstanceState(Parcelable state) {
65 if (state == null || !(state instanceof Bundle)) {
66 super.onRestoreInstanceState(state);
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));
77 public PanelBar(Context context, AttributeSet attrs) {
78 super(context, attrs);
82 protected void onFinishInflate() {
83 super.onFinishInflate();
86 public void setPanel(PanelView pv) {
91 public void setBouncerShowing(boolean showing) {
92 mBouncerShowing = showing;
93 int important = showing ? IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS
94 : IMPORTANT_FOR_ACCESSIBILITY_AUTO;
96 setImportantForAccessibility(important);
99 if (mPanel != null) mPanel.setImportantForAccessibility(important);
102 public float getExpansionFraction() {
103 return mPanelFraction;
106 public boolean isExpanded() {
110 protected void updateVisibility() {
111 mPanel.setVisibility(shouldPanelBeVisible() ? VISIBLE : INVISIBLE);
114 protected boolean shouldPanelBeVisible() {
115 return mExpanded || mBouncerShowing;
118 public boolean panelEnabled() {
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()));
133 if (event.getAction() == MotionEvent.ACTION_DOWN) {
134 final PanelView panel = mPanel;
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()));
141 boolean enabled = panel.isEnabled();
142 if (DEBUG) LOG("PanelBar.onTouch: state=%d ACTION_DOWN: panel %s %s", mState, panel,
143 (enabled ? "" : " (disabled)"));
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()));
152 return mPanel == null || mPanel.onTouchEvent(event);
155 public abstract void panelScrimMinFractionChanged(float minFraction);
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
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;
170 // adjust any other panels that may be partially visible
172 if (mState == STATE_CLOSED) {
177 final float thisFrac = pv.getExpandedFraction();
178 if (SPEW) LOG("panelExpansionChanged: -> %s: f=%.1f", pv.getName(), thisFrac);
179 fullyOpened = thisFrac >= 1f;
181 if (fullyOpened && !mTracking) {
183 onPanelFullyOpened();
184 } else if (fullyClosed && !mTracking && mState != STATE_CLOSED) {
189 if (SPEW) LOG("panelExpansionChanged: end state=%d [%s%s ]", mState,
190 fullyOpened?" fullyOpened":"", fullyClosed?" fullyClosed":"");
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);
200 pv.resetViews(false /* animate */);
201 pv.setExpandedFraction(0); // just in case
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
213 public void onPanelPeeked() {
214 if (DEBUG) LOG("onPanelPeeked");
217 public boolean isClosed() {
218 return mState == STATE_CLOSED;
221 public void onPanelCollapsed() {
222 if (DEBUG) LOG("onPanelCollapsed");
225 public void onPanelFullyOpened() {
226 if (DEBUG) LOG("onPanelFullyOpened");
229 public void onTrackingStarted() {
233 public void onTrackingStopped(boolean expand) {
237 public void onExpandingFinished() {
238 if (DEBUG) LOG("onExpandingFinished");
241 public void onClosingFinished() {