blob: 5d234947de2983b8b1be969e9597eefd94883a79 [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;
felkachang08579552018-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;
Daniel Sandler173bae22012-09-25 14:37:42 -040025import android.view.View;
Daniel Sandler08d05e32012-08-08 16:39:54 -040026import android.widget.FrameLayout;
27
Selim Cinek3d395c62015-06-16 19:37:37 -070028public abstract class PanelBar extends FrameLayout {
Daniel Sandlerbf4aa9d2012-08-15 10:49:28 -040029 public static final boolean DEBUG = false;
Daniel Sandler198a0302012-08-17 16:04:31 -040030 public static final String TAG = PanelBar.class.getSimpleName();
Chris Wren16895942015-06-23 11:22:20 -040031 private static final boolean SPEW = false;
felkachang08579552018-05-24 15:38:04 +080032 private static final String PANEL_BAR_SUPER_PARCELABLE = "panel_bar_super_parcelable";
33 private static final String STATE = "state";
Lucas Dupinbc9aac12018-03-04 20:18:15 -080034 private boolean mBouncerShowing;
Lucas Dupin3d565fa2018-03-20 15:40:14 -070035 private boolean mExpanded;
Lucas Dupin95951a32018-06-21 11:41:34 -070036 protected float mPanelFraction;
Chris Wren16895942015-06-23 11:22:20 -040037
Daniel Sandler08d05e32012-08-08 16:39:54 -040038 public static final void LOG(String fmt, Object... args) {
39 if (!DEBUG) return;
John Spurlockcd686b52013-06-05 10:13:46 -040040 Log.v(TAG, String.format(fmt, args));
Daniel Sandler08d05e32012-08-08 16:39:54 -040041 }
42
Daniel Sandler198a0302012-08-17 16:04:31 -040043 public static final int STATE_CLOSED = 0;
44 public static final int STATE_OPENING = 1;
45 public static final int STATE_OPEN = 2;
46
Xiaohui Chen9f967112016-01-07 14:14:06 -080047 PanelView mPanel;
Daniel Sandler978f8532012-08-15 15:48:16 -040048 private int mState = STATE_CLOSED;
49 private boolean mTracking;
50
Daniel Sandler198a0302012-08-17 16:04:31 -040051 public void go(int state) {
Daniel Sandler67eab792012-10-02 17:08:23 -040052 if (DEBUG) LOG("go state: %d -> %d", mState, state);
Daniel Sandler978f8532012-08-15 15:48:16 -040053 mState = state;
54 }
Daniel Sandler08d05e32012-08-08 16:39:54 -040055
felkachang08579552018-05-24 15:38:04 +080056 @Override
57 protected Parcelable onSaveInstanceState() {
58 Bundle bundle = new Bundle();
59 bundle.putParcelable(PANEL_BAR_SUPER_PARCELABLE, super.onSaveInstanceState());
60 bundle.putInt(STATE, mState);
61 return bundle;
62 }
63
64 @Override
65 protected void onRestoreInstanceState(Parcelable state) {
66 if (state == null || !(state instanceof Bundle)) {
67 super.onRestoreInstanceState(state);
68 return;
69 }
70
71 Bundle bundle = (Bundle) state;
72 super.onRestoreInstanceState(bundle.getParcelable(PANEL_BAR_SUPER_PARCELABLE));
73 if (((Bundle) state).containsKey(STATE)) {
74 go(bundle.getInt(STATE, STATE_CLOSED));
75 }
Jason Monk0a7d9f22017-03-15 11:03:06 -040076 }
77
Daniel Sandler08d05e32012-08-08 16:39:54 -040078 public PanelBar(Context context, AttributeSet attrs) {
79 super(context, attrs);
80 }
81
82 @Override
83 protected void onFinishInflate() {
84 super.onFinishInflate();
85 }
86
Xiaohui Chen9f967112016-01-07 14:14:06 -080087 public void setPanel(PanelView pv) {
88 mPanel = pv;
Daniel Sandler08d05e32012-08-08 16:39:54 -040089 pv.setBar(this);
90 }
91
Adrian Roosd0b2f7d2015-04-29 13:36:12 -070092 public void setBouncerShowing(boolean showing) {
Lucas Dupinbc9aac12018-03-04 20:18:15 -080093 mBouncerShowing = showing;
Adrian Roos3531a952015-06-10 13:19:17 -070094 int important = showing ? IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS
95 : IMPORTANT_FOR_ACCESSIBILITY_AUTO;
96
97 setImportantForAccessibility(important);
Lucas Dupin3d565fa2018-03-20 15:40:14 -070098 updateVisibility();
Adrian Roos3531a952015-06-10 13:19:17 -070099
Xiaohui Chen9f967112016-01-07 14:14:06 -0800100 if (mPanel != null) mPanel.setImportantForAccessibility(important);
Adrian Roosd0b2f7d2015-04-29 13:36:12 -0700101 }
102
Lucas Dupin95951a32018-06-21 11:41:34 -0700103 public float getExpansionFraction() {
104 return mPanelFraction;
105 }
106
107 public boolean isExpanded() {
108 return mExpanded;
109 }
110
Lucas Dupin3d565fa2018-03-20 15:40:14 -0700111 private void updateVisibility() {
112 mPanel.setVisibility(mExpanded || mBouncerShowing ? VISIBLE : INVISIBLE);
113 }
114
Xiaohui Chen9f967112016-01-07 14:14:06 -0800115 public boolean panelEnabled() {
Daniel Sandler1e8feef2012-08-16 11:37:41 -0400116 return true;
117 }
118
Daniel Sandler08d05e32012-08-08 16:39:54 -0400119 @Override
120 public boolean onTouchEvent(MotionEvent event) {
Daniel Sandler1e8feef2012-08-16 11:37:41 -0400121 // Allow subclasses to implement enable/disable semantics
Xiaohui Chen9f967112016-01-07 14:14:06 -0800122 if (!panelEnabled()) {
Daniel Sandler2b99a492013-02-05 13:05:44 -0500123 if (event.getAction() == MotionEvent.ACTION_DOWN) {
John Spurlockcd686b52013-06-05 10:13:46 -0400124 Log.v(TAG, String.format("onTouch: all panels disabled, ignoring touch at (%d,%d)",
Daniel Sandler2b99a492013-02-05 13:05:44 -0500125 (int) event.getX(), (int) event.getY()));
126 }
127 return false;
128 }
Daniel Sandler1e8feef2012-08-16 11:37:41 -0400129
Daniel Sandler08d05e32012-08-08 16:39:54 -0400130 if (event.getAction() == MotionEvent.ACTION_DOWN) {
Xiaohui Chen9f967112016-01-07 14:14:06 -0800131 final PanelView panel = mPanel;
Daniel Sandlerefb0faf2012-10-10 14:15:34 -0700132 if (panel == null) {
133 // panel is not there, so we'll eat the gesture
John Spurlockcd686b52013-06-05 10:13:46 -0400134 Log.v(TAG, String.format("onTouch: no panel for touch at (%d,%d)",
Daniel Sandler2b99a492013-02-05 13:05:44 -0500135 (int) event.getX(), (int) event.getY()));
Daniel Sandlerefb0faf2012-10-10 14:15:34 -0700136 return true;
137 }
John Spurlock919adac2012-10-02 16:41:12 -0400138 boolean enabled = panel.isEnabled();
Daniel Sandler67eab792012-10-02 17:08:23 -0400139 if (DEBUG) LOG("PanelBar.onTouch: state=%d ACTION_DOWN: panel %s %s", mState, panel,
John Spurlock919adac2012-10-02 16:41:12 -0400140 (enabled ? "" : " (disabled)"));
Daniel Sandlerefb0faf2012-10-10 14:15:34 -0700141 if (!enabled) {
142 // panel is disabled, so we'll eat the gesture
John Spurlockcd686b52013-06-05 10:13:46 -0400143 Log.v(TAG, String.format(
Daniel Sandler2b99a492013-02-05 13:05:44 -0500144 "onTouch: panel (%s) is disabled, ignoring touch at (%d,%d)",
145 panel, (int) event.getX(), (int) event.getY()));
Daniel Sandlerefb0faf2012-10-10 14:15:34 -0700146 return true;
147 }
Daniel Sandler08d05e32012-08-08 16:39:54 -0400148 }
Xiaohui Chen9f967112016-01-07 14:14:06 -0800149 return mPanel == null || mPanel.onTouchEvent(event);
Daniel Sandler198a0302012-08-17 16:04:31 -0400150 }
151
Selim Cinek3d395c62015-06-16 19:37:37 -0700152 public abstract void panelScrimMinFractionChanged(float minFraction);
153
Jorim Jaggib472b3472014-06-30 19:56:24 +0200154 /**
Jorim Jaggib472b3472014-06-30 19:56:24 +0200155 * @param frac the fraction from the expansion in [0, 1]
156 * @param expanded whether the panel is currently expanded; this is independent from the
157 * fraction as the panel also might be expanded if the fraction is 0
158 */
Xiaohui Chen9f967112016-01-07 14:14:06 -0800159 public void panelExpansionChanged(float frac, boolean expanded) {
Daniel Sandler08d05e32012-08-08 16:39:54 -0400160 boolean fullyClosed = true;
Xiaohui Chen9f967112016-01-07 14:14:06 -0800161 boolean fullyOpened = false;
162 if (SPEW) LOG("panelExpansionChanged: start state=%d", mState);
163 PanelView pv = mPanel;
Lucas Dupin3d565fa2018-03-20 15:40:14 -0700164 mExpanded = expanded;
Lucas Dupin95951a32018-06-21 11:41:34 -0700165 mPanelFraction = frac;
Lucas Dupin3d565fa2018-03-20 15:40:14 -0700166 updateVisibility();
Xiaohui Chen9f967112016-01-07 14:14:06 -0800167 // adjust any other panels that may be partially visible
168 if (expanded) {
169 if (mState == STATE_CLOSED) {
170 go(STATE_OPENING);
171 onPanelPeeked();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400172 }
Xiaohui Chen9f967112016-01-07 14:14:06 -0800173 fullyClosed = false;
174 final float thisFrac = pv.getExpandedFraction();
175 if (SPEW) LOG("panelExpansionChanged: -> %s: f=%.1f", pv.getName(), thisFrac);
176 fullyOpened = thisFrac >= 1f;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400177 }
Xiaohui Chen9f967112016-01-07 14:14:06 -0800178 if (fullyOpened && !mTracking) {
Daniel Sandler978f8532012-08-15 15:48:16 -0400179 go(STATE_OPEN);
Xiaohui Chen9f967112016-01-07 14:14:06 -0800180 onPanelFullyOpened();
Daniel Sandler198a0302012-08-17 16:04:31 -0400181 } else if (fullyClosed && !mTracking && mState != STATE_CLOSED) {
Daniel Sandler978f8532012-08-15 15:48:16 -0400182 go(STATE_CLOSED);
Xiaohui Chen9f967112016-01-07 14:14:06 -0800183 onPanelCollapsed();
Daniel Sandler978f8532012-08-15 15:48:16 -0400184 }
Daniel Sandler08d05e32012-08-08 16:39:54 -0400185
Chris Wren16895942015-06-23 11:22:20 -0400186 if (SPEW) LOG("panelExpansionChanged: end state=%d [%s%s ]", mState,
Xiaohui Chen9f967112016-01-07 14:14:06 -0800187 fullyOpened?" fullyOpened":"", fullyClosed?" fullyClosed":"");
Daniel Sandler08d05e32012-08-08 16:39:54 -0400188 }
189
Xiaohui Chen9f967112016-01-07 14:14:06 -0800190 public void collapsePanel(boolean animate, boolean delayed, float speedUpFactor) {
Daniel Sandler198a0302012-08-17 16:04:31 -0400191 boolean waiting = false;
Xiaohui Chen9f967112016-01-07 14:14:06 -0800192 PanelView pv = mPanel;
193 if (animate && !pv.isFullyCollapsed()) {
194 pv.collapse(delayed, speedUpFactor);
195 waiting = true;
196 } else {
197 pv.resetViews();
198 pv.setExpandedFraction(0); // just in case
199 pv.cancelPeek();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400200 }
Xiaohui Chen9f967112016-01-07 14:14:06 -0800201 if (DEBUG) LOG("collapsePanel: animate=%s waiting=%s", animate, waiting);
Daniel Sandlerc38bbc32012-10-05 12:21:38 -0400202 if (!waiting && mState != STATE_CLOSED) {
John Spurlock209bede2013-07-17 12:23:27 -0400203 // it's possible that nothing animated, so we replicate the termination
Daniel Sandler198a0302012-08-17 16:04:31 -0400204 // conditions of panelExpansionChanged here
205 go(STATE_CLOSED);
Xiaohui Chen9f967112016-01-07 14:14:06 -0800206 onPanelCollapsed();
Daniel Sandler198a0302012-08-17 16:04:31 -0400207 }
Daniel Sandler08d05e32012-08-08 16:39:54 -0400208 }
209
210 public void onPanelPeeked() {
Daniel Sandler67eab792012-10-02 17:08:23 -0400211 if (DEBUG) LOG("onPanelPeeked");
Daniel Sandler08d05e32012-08-08 16:39:54 -0400212 }
213
Jason Monkaa573e92017-01-27 17:00:29 -0500214 public boolean isClosed() {
215 return mState == STATE_CLOSED;
216 }
217
Xiaohui Chen9f967112016-01-07 14:14:06 -0800218 public void onPanelCollapsed() {
219 if (DEBUG) LOG("onPanelCollapsed");
Daniel Sandler08d05e32012-08-08 16:39:54 -0400220 }
221
Xiaohui Chen9f967112016-01-07 14:14:06 -0800222 public void onPanelFullyOpened() {
Daniel Sandler67eab792012-10-02 17:08:23 -0400223 if (DEBUG) LOG("onPanelFullyOpened");
Daniel Sandler08d05e32012-08-08 16:39:54 -0400224 }
Daniel Sandler978f8532012-08-15 15:48:16 -0400225
Xiaohui Chen9f967112016-01-07 14:14:06 -0800226 public void onTrackingStarted() {
Daniel Sandler978f8532012-08-15 15:48:16 -0400227 mTracking = true;
Daniel Sandler978f8532012-08-15 15:48:16 -0400228 }
229
Xiaohui Chen9f967112016-01-07 14:14:06 -0800230 public void onTrackingStopped(boolean expand) {
Daniel Sandler978f8532012-08-15 15:48:16 -0400231 mTracking = false;
Daniel Sandler978f8532012-08-15 15:48:16 -0400232 }
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200233
234 public void onExpandingFinished() {
Chris Wren16895942015-06-23 11:22:20 -0400235 if (DEBUG) LOG("onExpandingFinished");
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200236 }
Selim Cinekdbbcfbe2014-10-24 17:52:35 +0200237
238 public void onClosingFinished() {
239
240 }
Daniel Sandler08d05e32012-08-08 16:39:54 -0400241}