blob: 063d00b806c2a09482146bc0fe8036fb1402afe0 [file] [log] [blame]
Daniel Sandler50a53132012-10-24 15:02:27 -04001/*
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
Daniel Sandler08d05e32012-08-08 16:39:54 -040017package com.android.systemui.statusbar.phone;
18
Daniel Sandler08d05e32012-08-08 16:39:54 -040019import android.content.Context;
felkachange6c03a02018-05-24 15:38:04 +080020import android.os.Bundle;
21import android.os.Parcelable;
Daniel Sandler08d05e32012-08-08 16:39:54 -040022import android.util.AttributeSet;
John Spurlockcd686b52013-06-05 10:13:46 -040023import android.util.Log;
Daniel Sandler08d05e32012-08-08 16:39:54 -040024import android.view.MotionEvent;
25import android.widget.FrameLayout;
26
Selim Cinek3d395c62015-06-16 19:37:37 -070027public abstract class PanelBar extends FrameLayout {
Daniel Sandlerbf4aa9d2012-08-15 10:49:28 -040028 public static final boolean DEBUG = false;
Daniel Sandler198a0302012-08-17 16:04:31 -040029 public static final String TAG = PanelBar.class.getSimpleName();
Chris Wren16895942015-06-23 11:22:20 -040030 private static final boolean SPEW = false;
felkachange6c03a02018-05-24 15:38:04 +080031 private static final String PANEL_BAR_SUPER_PARCELABLE = "panel_bar_super_parcelable";
32 private static final String STATE = "state";
Lucas Dupinbc9aac12018-03-04 20:18:15 -080033 private boolean mBouncerShowing;
Lucas Dupin3d565fa2018-03-20 15:40:14 -070034 private boolean mExpanded;
Lucas Dupin5220db72018-06-21 11:41:34 -070035 protected float mPanelFraction;
Chris Wren16895942015-06-23 11:22:20 -040036
Daniel Sandler08d05e32012-08-08 16:39:54 -040037 public static final void LOG(String fmt, Object... args) {
38 if (!DEBUG) return;
John Spurlockcd686b52013-06-05 10:13:46 -040039 Log.v(TAG, String.format(fmt, args));
Daniel Sandler08d05e32012-08-08 16:39:54 -040040 }
41
Daniel Sandler198a0302012-08-17 16:04:31 -040042 public static final int STATE_CLOSED = 0;
43 public static final int STATE_OPENING = 1;
44 public static final int STATE_OPEN = 2;
45
Xiaohui Chen9f967112016-01-07 14:14:06 -080046 PanelView mPanel;
Daniel Sandler978f8532012-08-15 15:48:16 -040047 private int mState = STATE_CLOSED;
48 private boolean mTracking;
49
Daniel Sandler198a0302012-08-17 16:04:31 -040050 public void go(int state) {
Daniel Sandler67eab792012-10-02 17:08:23 -040051 if (DEBUG) LOG("go state: %d -> %d", mState, state);
Daniel Sandler978f8532012-08-15 15:48:16 -040052 mState = state;
53 }
Daniel Sandler08d05e32012-08-08 16:39:54 -040054
felkachange6c03a02018-05-24 15:38:04 +080055 @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 }
Jason Monk0a7d9f22017-03-15 11:03:06 -040075 }
76
Daniel Sandler08d05e32012-08-08 16:39:54 -040077 public PanelBar(Context context, AttributeSet attrs) {
78 super(context, attrs);
79 }
80
81 @Override
82 protected void onFinishInflate() {
83 super.onFinishInflate();
84 }
85
Xiaohui Chen9f967112016-01-07 14:14:06 -080086 public void setPanel(PanelView pv) {
87 mPanel = pv;
Daniel Sandler08d05e32012-08-08 16:39:54 -040088 pv.setBar(this);
89 }
90
Adrian Roosd0b2f7d2015-04-29 13:36:12 -070091 public void setBouncerShowing(boolean showing) {
Lucas Dupinbc9aac12018-03-04 20:18:15 -080092 mBouncerShowing = showing;
Adrian Roos3531a952015-06-10 13:19:17 -070093 int important = showing ? IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS
94 : IMPORTANT_FOR_ACCESSIBILITY_AUTO;
95
96 setImportantForAccessibility(important);
Lucas Dupin3d565fa2018-03-20 15:40:14 -070097 updateVisibility();
Adrian Roos3531a952015-06-10 13:19:17 -070098
Xiaohui Chen9f967112016-01-07 14:14:06 -080099 if (mPanel != null) mPanel.setImportantForAccessibility(important);
Adrian Roosd0b2f7d2015-04-29 13:36:12 -0700100 }
101
Lucas Dupin5220db72018-06-21 11:41:34 -0700102 public float getExpansionFraction() {
103 return mPanelFraction;
104 }
105
106 public boolean isExpanded() {
107 return mExpanded;
108 }
109
Selim Cinekd21232e2019-06-20 14:15:59 -0700110 protected void updateVisibility() {
111 mPanel.setVisibility(shouldPanelBeVisible() ? VISIBLE : INVISIBLE);
112 }
113
114 protected boolean shouldPanelBeVisible() {
115 return mExpanded || mBouncerShowing;
Lucas Dupin3d565fa2018-03-20 15:40:14 -0700116 }
117
Xiaohui Chen9f967112016-01-07 14:14:06 -0800118 public boolean panelEnabled() {
Daniel Sandler1e8feef2012-08-16 11:37:41 -0400119 return true;
120 }
121
Daniel Sandler08d05e32012-08-08 16:39:54 -0400122 @Override
123 public boolean onTouchEvent(MotionEvent event) {
Daniel Sandler1e8feef2012-08-16 11:37:41 -0400124 // Allow subclasses to implement enable/disable semantics
Xiaohui Chen9f967112016-01-07 14:14:06 -0800125 if (!panelEnabled()) {
Daniel Sandler2b99a492013-02-05 13:05:44 -0500126 if (event.getAction() == MotionEvent.ACTION_DOWN) {
John Spurlockcd686b52013-06-05 10:13:46 -0400127 Log.v(TAG, String.format("onTouch: all panels disabled, ignoring touch at (%d,%d)",
Daniel Sandler2b99a492013-02-05 13:05:44 -0500128 (int) event.getX(), (int) event.getY()));
129 }
130 return false;
131 }
Daniel Sandler1e8feef2012-08-16 11:37:41 -0400132
Daniel Sandler08d05e32012-08-08 16:39:54 -0400133 if (event.getAction() == MotionEvent.ACTION_DOWN) {
Xiaohui Chen9f967112016-01-07 14:14:06 -0800134 final PanelView panel = mPanel;
Daniel Sandlerefb0faf2012-10-10 14:15:34 -0700135 if (panel == null) {
136 // panel is not there, so we'll eat the gesture
John Spurlockcd686b52013-06-05 10:13:46 -0400137 Log.v(TAG, String.format("onTouch: no panel for touch at (%d,%d)",
Daniel Sandler2b99a492013-02-05 13:05:44 -0500138 (int) event.getX(), (int) event.getY()));
Daniel Sandlerefb0faf2012-10-10 14:15:34 -0700139 return true;
140 }
John Spurlock919adac2012-10-02 16:41:12 -0400141 boolean enabled = panel.isEnabled();
Daniel Sandler67eab792012-10-02 17:08:23 -0400142 if (DEBUG) LOG("PanelBar.onTouch: state=%d ACTION_DOWN: panel %s %s", mState, panel,
John Spurlock919adac2012-10-02 16:41:12 -0400143 (enabled ? "" : " (disabled)"));
Daniel Sandlerefb0faf2012-10-10 14:15:34 -0700144 if (!enabled) {
145 // panel is disabled, so we'll eat the gesture
John Spurlockcd686b52013-06-05 10:13:46 -0400146 Log.v(TAG, String.format(
Daniel Sandler2b99a492013-02-05 13:05:44 -0500147 "onTouch: panel (%s) is disabled, ignoring touch at (%d,%d)",
148 panel, (int) event.getX(), (int) event.getY()));
Daniel Sandlerefb0faf2012-10-10 14:15:34 -0700149 return true;
150 }
Daniel Sandler08d05e32012-08-08 16:39:54 -0400151 }
Xiaohui Chen9f967112016-01-07 14:14:06 -0800152 return mPanel == null || mPanel.onTouchEvent(event);
Daniel Sandler198a0302012-08-17 16:04:31 -0400153 }
154
Selim Cinek3d395c62015-06-16 19:37:37 -0700155 public abstract void panelScrimMinFractionChanged(float minFraction);
156
Jorim Jaggib472b3472014-06-30 19:56:24 +0200157 /**
Jorim Jaggib472b3472014-06-30 19:56:24 +0200158 * @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 */
Xiaohui Chen9f967112016-01-07 14:14:06 -0800162 public void panelExpansionChanged(float frac, boolean expanded) {
Daniel Sandler08d05e32012-08-08 16:39:54 -0400163 boolean fullyClosed = true;
Xiaohui Chen9f967112016-01-07 14:14:06 -0800164 boolean fullyOpened = false;
165 if (SPEW) LOG("panelExpansionChanged: start state=%d", mState);
166 PanelView pv = mPanel;
Lucas Dupin3d565fa2018-03-20 15:40:14 -0700167 mExpanded = expanded;
Lucas Dupin5220db72018-06-21 11:41:34 -0700168 mPanelFraction = frac;
Lucas Dupin3d565fa2018-03-20 15:40:14 -0700169 updateVisibility();
Xiaohui Chen9f967112016-01-07 14:14:06 -0800170 // adjust any other panels that may be partially visible
171 if (expanded) {
172 if (mState == STATE_CLOSED) {
173 go(STATE_OPENING);
174 onPanelPeeked();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400175 }
Xiaohui Chen9f967112016-01-07 14:14:06 -0800176 fullyClosed = false;
177 final float thisFrac = pv.getExpandedFraction();
178 if (SPEW) LOG("panelExpansionChanged: -> %s: f=%.1f", pv.getName(), thisFrac);
179 fullyOpened = thisFrac >= 1f;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400180 }
Xiaohui Chen9f967112016-01-07 14:14:06 -0800181 if (fullyOpened && !mTracking) {
Daniel Sandler978f8532012-08-15 15:48:16 -0400182 go(STATE_OPEN);
Xiaohui Chen9f967112016-01-07 14:14:06 -0800183 onPanelFullyOpened();
Daniel Sandler198a0302012-08-17 16:04:31 -0400184 } else if (fullyClosed && !mTracking && mState != STATE_CLOSED) {
Daniel Sandler978f8532012-08-15 15:48:16 -0400185 go(STATE_CLOSED);
Xiaohui Chen9f967112016-01-07 14:14:06 -0800186 onPanelCollapsed();
Daniel Sandler978f8532012-08-15 15:48:16 -0400187 }
Daniel Sandler08d05e32012-08-08 16:39:54 -0400188
Chris Wren16895942015-06-23 11:22:20 -0400189 if (SPEW) LOG("panelExpansionChanged: end state=%d [%s%s ]", mState,
Xiaohui Chen9f967112016-01-07 14:14:06 -0800190 fullyOpened?" fullyOpened":"", fullyClosed?" fullyClosed":"");
Daniel Sandler08d05e32012-08-08 16:39:54 -0400191 }
192
Xiaohui Chen9f967112016-01-07 14:14:06 -0800193 public void collapsePanel(boolean animate, boolean delayed, float speedUpFactor) {
Daniel Sandler198a0302012-08-17 16:04:31 -0400194 boolean waiting = false;
Xiaohui Chen9f967112016-01-07 14:14:06 -0800195 PanelView pv = mPanel;
196 if (animate && !pv.isFullyCollapsed()) {
197 pv.collapse(delayed, speedUpFactor);
198 waiting = true;
199 } else {
Lucas Dupinee63c372018-08-03 11:58:24 -0700200 pv.resetViews(false /* animate */);
Xiaohui Chen9f967112016-01-07 14:14:06 -0800201 pv.setExpandedFraction(0); // just in case
202 pv.cancelPeek();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400203 }
Xiaohui Chen9f967112016-01-07 14:14:06 -0800204 if (DEBUG) LOG("collapsePanel: animate=%s waiting=%s", animate, waiting);
Daniel Sandlerc38bbc32012-10-05 12:21:38 -0400205 if (!waiting && mState != STATE_CLOSED) {
John Spurlock209bede2013-07-17 12:23:27 -0400206 // it's possible that nothing animated, so we replicate the termination
Daniel Sandler198a0302012-08-17 16:04:31 -0400207 // conditions of panelExpansionChanged here
208 go(STATE_CLOSED);
Xiaohui Chen9f967112016-01-07 14:14:06 -0800209 onPanelCollapsed();
Daniel Sandler198a0302012-08-17 16:04:31 -0400210 }
Daniel Sandler08d05e32012-08-08 16:39:54 -0400211 }
212
213 public void onPanelPeeked() {
Daniel Sandler67eab792012-10-02 17:08:23 -0400214 if (DEBUG) LOG("onPanelPeeked");
Daniel Sandler08d05e32012-08-08 16:39:54 -0400215 }
216
Jason Monkaa573e92017-01-27 17:00:29 -0500217 public boolean isClosed() {
218 return mState == STATE_CLOSED;
219 }
220
Xiaohui Chen9f967112016-01-07 14:14:06 -0800221 public void onPanelCollapsed() {
222 if (DEBUG) LOG("onPanelCollapsed");
Daniel Sandler08d05e32012-08-08 16:39:54 -0400223 }
224
Xiaohui Chen9f967112016-01-07 14:14:06 -0800225 public void onPanelFullyOpened() {
Daniel Sandler67eab792012-10-02 17:08:23 -0400226 if (DEBUG) LOG("onPanelFullyOpened");
Daniel Sandler08d05e32012-08-08 16:39:54 -0400227 }
Daniel Sandler978f8532012-08-15 15:48:16 -0400228
Xiaohui Chen9f967112016-01-07 14:14:06 -0800229 public void onTrackingStarted() {
Daniel Sandler978f8532012-08-15 15:48:16 -0400230 mTracking = true;
Daniel Sandler978f8532012-08-15 15:48:16 -0400231 }
232
Xiaohui Chen9f967112016-01-07 14:14:06 -0800233 public void onTrackingStopped(boolean expand) {
Daniel Sandler978f8532012-08-15 15:48:16 -0400234 mTracking = false;
Daniel Sandler978f8532012-08-15 15:48:16 -0400235 }
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200236
237 public void onExpandingFinished() {
Chris Wren16895942015-06-23 11:22:20 -0400238 if (DEBUG) LOG("onExpandingFinished");
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200239 }
Selim Cinekdbbcfbe2014-10-24 17:52:35 +0200240
241 public void onClosingFinished() {
242
243 }
Daniel Sandler08d05e32012-08-08 16:39:54 -0400244}