PanelBar.java revision d0b2f7ddcac251acfe88092ecef4abb63b3e248b
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 17package com.android.systemui.statusbar.phone; 18 19import android.content.Context; 20import android.util.AttributeSet; 21import android.util.Log; 22import android.view.MotionEvent; 23import android.view.View; 24import android.widget.FrameLayout; 25 26import java.util.ArrayList; 27 28public class PanelBar extends FrameLayout { 29 public static final boolean DEBUG = false; 30 public static final String TAG = PanelBar.class.getSimpleName(); 31 public static final void LOG(String fmt, Object... args) { 32 if (!DEBUG) return; 33 Log.v(TAG, String.format(fmt, args)); 34 } 35 36 public static final int STATE_CLOSED = 0; 37 public static final int STATE_OPENING = 1; 38 public static final int STATE_OPEN = 2; 39 40 PanelHolder mPanelHolder; 41 ArrayList<PanelView> mPanels = new ArrayList<PanelView>(); 42 PanelView mTouchingPanel; 43 private int mState = STATE_CLOSED; 44 private boolean mTracking; 45 46 float mPanelExpandedFractionSum; 47 48 public void go(int state) { 49 if (DEBUG) LOG("go state: %d -> %d", mState, state); 50 mState = state; 51 } 52 53 public PanelBar(Context context, AttributeSet attrs) { 54 super(context, attrs); 55 } 56 57 @Override 58 protected void onFinishInflate() { 59 super.onFinishInflate(); 60 } 61 62 public void addPanel(PanelView pv) { 63 mPanels.add(pv); 64 pv.setBar(this); 65 } 66 67 public void setPanelHolder(PanelHolder ph) { 68 if (ph == null) { 69 Log.e(TAG, "setPanelHolder: null PanelHolder", new Throwable()); 70 return; 71 } 72 ph.setBar(this); 73 mPanelHolder = ph; 74 final int N = ph.getChildCount(); 75 for (int i=0; i<N; i++) { 76 final View v = ph.getChildAt(i); 77 if (v != null && v instanceof PanelView) { 78 addPanel((PanelView) v); 79 } 80 } 81 } 82 83 public void setBouncerShowing(boolean showing) { 84 if (mPanelHolder != null) { 85 mPanelHolder.setImportantForAccessibility( 86 showing ? IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS 87 : IMPORTANT_FOR_ACCESSIBILITY_AUTO); 88 } 89 } 90 91 public float getBarHeight() { 92 return getMeasuredHeight(); 93 } 94 95 public PanelView selectPanelForTouch(MotionEvent touch) { 96 final int N = mPanels.size(); 97 return mPanels.get((int)(N * touch.getX() / getMeasuredWidth())); 98 } 99 100 public boolean panelsEnabled() { 101 return true; 102 } 103 104 @Override 105 public boolean onTouchEvent(MotionEvent event) { 106 // Allow subclasses to implement enable/disable semantics 107 if (!panelsEnabled()) { 108 if (event.getAction() == MotionEvent.ACTION_DOWN) { 109 Log.v(TAG, String.format("onTouch: all panels disabled, ignoring touch at (%d,%d)", 110 (int) event.getX(), (int) event.getY())); 111 } 112 return false; 113 } 114 115 // figure out which panel needs to be talked to here 116 if (event.getAction() == MotionEvent.ACTION_DOWN) { 117 final PanelView panel = selectPanelForTouch(event); 118 if (panel == null) { 119 // panel is not there, so we'll eat the gesture 120 Log.v(TAG, String.format("onTouch: no panel for touch at (%d,%d)", 121 (int) event.getX(), (int) event.getY())); 122 mTouchingPanel = null; 123 return true; 124 } 125 boolean enabled = panel.isEnabled(); 126 if (DEBUG) LOG("PanelBar.onTouch: state=%d ACTION_DOWN: panel %s %s", mState, panel, 127 (enabled ? "" : " (disabled)")); 128 if (!enabled) { 129 // panel is disabled, so we'll eat the gesture 130 Log.v(TAG, String.format( 131 "onTouch: panel (%s) is disabled, ignoring touch at (%d,%d)", 132 panel, (int) event.getX(), (int) event.getY())); 133 mTouchingPanel = null; 134 return true; 135 } 136 startOpeningPanel(panel); 137 } 138 final boolean result = mTouchingPanel != null 139 ? mTouchingPanel.onTouchEvent(event) 140 : true; 141 return result; 142 } 143 144 // called from PanelView when self-expanding, too 145 public void startOpeningPanel(PanelView panel) { 146 if (DEBUG) LOG("startOpeningPanel: " + panel); 147 mTouchingPanel = panel; 148 mPanelHolder.setSelectedPanel(mTouchingPanel); 149 for (PanelView pv : mPanels) { 150 if (pv != panel) { 151 pv.collapse(false /* delayed */, 1.0f /* speedUpFactor */); 152 } 153 } 154 } 155 156 /** 157 * @param panel the panel which changed its expansion state 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(PanelView panel, float frac, boolean expanded) { 163 boolean fullyClosed = true; 164 PanelView fullyOpenedPanel = null; 165 if (DEBUG) LOG("panelExpansionChanged: start state=%d panel=%s", mState, panel.getName()); 166 mPanelExpandedFractionSum = 0f; 167 for (PanelView pv : mPanels) { 168 pv.setVisibility(expanded ? View.VISIBLE : View.INVISIBLE); 169 // adjust any other panels that may be partially visible 170 if (expanded) { 171 if (mState == STATE_CLOSED) { 172 go(STATE_OPENING); 173 onPanelPeeked(); 174 } 175 fullyClosed = false; 176 final float thisFrac = pv.getExpandedFraction(); 177 mPanelExpandedFractionSum += thisFrac; 178 if (DEBUG) LOG("panelExpansionChanged: -> %s: f=%.1f", pv.getName(), thisFrac); 179 if (panel == pv) { 180 if (thisFrac == 1f) fullyOpenedPanel = panel; 181 } 182 } 183 } 184 mPanelExpandedFractionSum /= mPanels.size(); 185 if (fullyOpenedPanel != null && !mTracking) { 186 go(STATE_OPEN); 187 onPanelFullyOpened(fullyOpenedPanel); 188 } else if (fullyClosed && !mTracking && mState != STATE_CLOSED) { 189 go(STATE_CLOSED); 190 onAllPanelsCollapsed(); 191 } 192 193 if (DEBUG) LOG("panelExpansionChanged: end state=%d [%s%s ]", mState, 194 (fullyOpenedPanel!=null)?" fullyOpened":"", fullyClosed?" fullyClosed":""); 195 } 196 197 public void collapseAllPanels(boolean animate, boolean delayed, float speedUpFactor) { 198 boolean waiting = false; 199 for (PanelView pv : mPanels) { 200 if (animate && !pv.isFullyCollapsed()) { 201 pv.collapse(delayed, speedUpFactor); 202 waiting = true; 203 } else { 204 pv.resetViews(); 205 pv.setExpandedFraction(0); // just in case 206 pv.cancelPeek(); 207 } 208 } 209 if (DEBUG) LOG("collapseAllPanels: animate=%s waiting=%s", animate, waiting); 210 if (!waiting && mState != STATE_CLOSED) { 211 // it's possible that nothing animated, so we replicate the termination 212 // conditions of panelExpansionChanged here 213 go(STATE_CLOSED); 214 onAllPanelsCollapsed(); 215 } 216 } 217 218 public void onPanelPeeked() { 219 if (DEBUG) LOG("onPanelPeeked"); 220 } 221 222 public void onAllPanelsCollapsed() { 223 if (DEBUG) LOG("onAllPanelsCollapsed"); 224 } 225 226 public void onPanelFullyOpened(PanelView openPanel) { 227 if (DEBUG) LOG("onPanelFullyOpened"); 228 } 229 230 public void onTrackingStarted(PanelView panel) { 231 mTracking = true; 232 if (DEBUG && panel != mTouchingPanel) { 233 LOG("shouldn't happen: onTrackingStarted(%s) != mTouchingPanel(%s)", 234 panel, mTouchingPanel); 235 } 236 } 237 238 public void onTrackingStopped(PanelView panel, boolean expand) { 239 mTracking = false; 240 } 241 242 public void onExpandingFinished() { 243 244 } 245 246 public void onClosingFinished() { 247 248 } 249} 250