blob: 6f4a3cd8214079449e001b65444d2adac073f1c5 [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;
20import android.util.AttributeSet;
John Spurlockcd686b52013-06-05 10:13:46 -040021import android.util.Log;
Daniel Sandler08d05e32012-08-08 16:39:54 -040022import android.view.MotionEvent;
Daniel Sandler173bae22012-09-25 14:37:42 -040023import android.view.View;
Daniel Sandler08d05e32012-08-08 16:39:54 -040024import android.widget.FrameLayout;
25
Selim Cinek3d395c62015-06-16 19:37:37 -070026public abstract class PanelBar extends FrameLayout {
Daniel Sandlerbf4aa9d2012-08-15 10:49:28 -040027 public static final boolean DEBUG = false;
Daniel Sandler198a0302012-08-17 16:04:31 -040028 public static final String TAG = PanelBar.class.getSimpleName();
Chris Wren16895942015-06-23 11:22:20 -040029 private static final boolean SPEW = false;
Lucas Dupinbc9aac12018-03-04 20:18:15 -080030 private boolean mBouncerShowing;
Lucas Dupin3d565fa2018-03-20 15:40:14 -070031 private boolean mExpanded;
Lucas Dupin95951a32018-06-21 11:41:34 -070032 protected float mPanelFraction;
Chris Wren16895942015-06-23 11:22:20 -040033
Daniel Sandler08d05e32012-08-08 16:39:54 -040034 public static final void LOG(String fmt, Object... args) {
35 if (!DEBUG) return;
John Spurlockcd686b52013-06-05 10:13:46 -040036 Log.v(TAG, String.format(fmt, args));
Daniel Sandler08d05e32012-08-08 16:39:54 -040037 }
38
Daniel Sandler198a0302012-08-17 16:04:31 -040039 public static final int STATE_CLOSED = 0;
40 public static final int STATE_OPENING = 1;
41 public static final int STATE_OPEN = 2;
42
Xiaohui Chen9f967112016-01-07 14:14:06 -080043 PanelView mPanel;
Daniel Sandler978f8532012-08-15 15:48:16 -040044 private int mState = STATE_CLOSED;
45 private boolean mTracking;
46
Daniel Sandler198a0302012-08-17 16:04:31 -040047 public void go(int state) {
Daniel Sandler67eab792012-10-02 17:08:23 -040048 if (DEBUG) LOG("go state: %d -> %d", mState, state);
Daniel Sandler978f8532012-08-15 15:48:16 -040049 mState = state;
50 }
Daniel Sandler08d05e32012-08-08 16:39:54 -040051
Jason Monk0a7d9f22017-03-15 11:03:06 -040052 public int getState() {
53 return mState;
54 }
55
Daniel Sandler08d05e32012-08-08 16:39:54 -040056 public PanelBar(Context context, AttributeSet attrs) {
57 super(context, attrs);
58 }
59
60 @Override
61 protected void onFinishInflate() {
62 super.onFinishInflate();
63 }
64
Xiaohui Chen9f967112016-01-07 14:14:06 -080065 public void setPanel(PanelView pv) {
66 mPanel = pv;
Daniel Sandler08d05e32012-08-08 16:39:54 -040067 pv.setBar(this);
68 }
69
Adrian Roosd0b2f7d2015-04-29 13:36:12 -070070 public void setBouncerShowing(boolean showing) {
Lucas Dupinbc9aac12018-03-04 20:18:15 -080071 mBouncerShowing = showing;
Adrian Roos3531a952015-06-10 13:19:17 -070072 int important = showing ? IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS
73 : IMPORTANT_FOR_ACCESSIBILITY_AUTO;
74
75 setImportantForAccessibility(important);
Lucas Dupin3d565fa2018-03-20 15:40:14 -070076 updateVisibility();
Adrian Roos3531a952015-06-10 13:19:17 -070077
Xiaohui Chen9f967112016-01-07 14:14:06 -080078 if (mPanel != null) mPanel.setImportantForAccessibility(important);
Adrian Roosd0b2f7d2015-04-29 13:36:12 -070079 }
80
Lucas Dupin95951a32018-06-21 11:41:34 -070081 public float getExpansionFraction() {
82 return mPanelFraction;
83 }
84
85 public boolean isExpanded() {
86 return mExpanded;
87 }
88
Lucas Dupin3d565fa2018-03-20 15:40:14 -070089 private void updateVisibility() {
90 mPanel.setVisibility(mExpanded || mBouncerShowing ? VISIBLE : INVISIBLE);
91 }
92
Xiaohui Chen9f967112016-01-07 14:14:06 -080093 public boolean panelEnabled() {
Daniel Sandler1e8feef2012-08-16 11:37:41 -040094 return true;
95 }
96
Daniel Sandler08d05e32012-08-08 16:39:54 -040097 @Override
98 public boolean onTouchEvent(MotionEvent event) {
Daniel Sandler1e8feef2012-08-16 11:37:41 -040099 // Allow subclasses to implement enable/disable semantics
Xiaohui Chen9f967112016-01-07 14:14:06 -0800100 if (!panelEnabled()) {
Daniel Sandler2b99a492013-02-05 13:05:44 -0500101 if (event.getAction() == MotionEvent.ACTION_DOWN) {
John Spurlockcd686b52013-06-05 10:13:46 -0400102 Log.v(TAG, String.format("onTouch: all panels disabled, ignoring touch at (%d,%d)",
Daniel Sandler2b99a492013-02-05 13:05:44 -0500103 (int) event.getX(), (int) event.getY()));
104 }
105 return false;
106 }
Daniel Sandler1e8feef2012-08-16 11:37:41 -0400107
Daniel Sandler08d05e32012-08-08 16:39:54 -0400108 if (event.getAction() == MotionEvent.ACTION_DOWN) {
Xiaohui Chen9f967112016-01-07 14:14:06 -0800109 final PanelView panel = mPanel;
Daniel Sandlerefb0faf2012-10-10 14:15:34 -0700110 if (panel == null) {
111 // panel is not there, so we'll eat the gesture
John Spurlockcd686b52013-06-05 10:13:46 -0400112 Log.v(TAG, String.format("onTouch: no panel for touch at (%d,%d)",
Daniel Sandler2b99a492013-02-05 13:05:44 -0500113 (int) event.getX(), (int) event.getY()));
Daniel Sandlerefb0faf2012-10-10 14:15:34 -0700114 return true;
115 }
John Spurlock919adac2012-10-02 16:41:12 -0400116 boolean enabled = panel.isEnabled();
Daniel Sandler67eab792012-10-02 17:08:23 -0400117 if (DEBUG) LOG("PanelBar.onTouch: state=%d ACTION_DOWN: panel %s %s", mState, panel,
John Spurlock919adac2012-10-02 16:41:12 -0400118 (enabled ? "" : " (disabled)"));
Daniel Sandlerefb0faf2012-10-10 14:15:34 -0700119 if (!enabled) {
120 // panel is disabled, so we'll eat the gesture
John Spurlockcd686b52013-06-05 10:13:46 -0400121 Log.v(TAG, String.format(
Daniel Sandler2b99a492013-02-05 13:05:44 -0500122 "onTouch: panel (%s) is disabled, ignoring touch at (%d,%d)",
123 panel, (int) event.getX(), (int) event.getY()));
Daniel Sandlerefb0faf2012-10-10 14:15:34 -0700124 return true;
125 }
Daniel Sandler08d05e32012-08-08 16:39:54 -0400126 }
Xiaohui Chen9f967112016-01-07 14:14:06 -0800127 return mPanel == null || mPanel.onTouchEvent(event);
Daniel Sandler198a0302012-08-17 16:04:31 -0400128 }
129
Selim Cinek3d395c62015-06-16 19:37:37 -0700130 public abstract void panelScrimMinFractionChanged(float minFraction);
131
Jorim Jaggib472b3472014-06-30 19:56:24 +0200132 /**
Jorim Jaggib472b3472014-06-30 19:56:24 +0200133 * @param frac the fraction from the expansion in [0, 1]
134 * @param expanded whether the panel is currently expanded; this is independent from the
135 * fraction as the panel also might be expanded if the fraction is 0
136 */
Xiaohui Chen9f967112016-01-07 14:14:06 -0800137 public void panelExpansionChanged(float frac, boolean expanded) {
Daniel Sandler08d05e32012-08-08 16:39:54 -0400138 boolean fullyClosed = true;
Xiaohui Chen9f967112016-01-07 14:14:06 -0800139 boolean fullyOpened = false;
140 if (SPEW) LOG("panelExpansionChanged: start state=%d", mState);
141 PanelView pv = mPanel;
Lucas Dupin3d565fa2018-03-20 15:40:14 -0700142 mExpanded = expanded;
Lucas Dupin95951a32018-06-21 11:41:34 -0700143 mPanelFraction = frac;
Lucas Dupin3d565fa2018-03-20 15:40:14 -0700144 updateVisibility();
Xiaohui Chen9f967112016-01-07 14:14:06 -0800145 // adjust any other panels that may be partially visible
146 if (expanded) {
147 if (mState == STATE_CLOSED) {
148 go(STATE_OPENING);
149 onPanelPeeked();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400150 }
Xiaohui Chen9f967112016-01-07 14:14:06 -0800151 fullyClosed = false;
152 final float thisFrac = pv.getExpandedFraction();
153 if (SPEW) LOG("panelExpansionChanged: -> %s: f=%.1f", pv.getName(), thisFrac);
154 fullyOpened = thisFrac >= 1f;
Daniel Sandler08d05e32012-08-08 16:39:54 -0400155 }
Xiaohui Chen9f967112016-01-07 14:14:06 -0800156 if (fullyOpened && !mTracking) {
Daniel Sandler978f8532012-08-15 15:48:16 -0400157 go(STATE_OPEN);
Xiaohui Chen9f967112016-01-07 14:14:06 -0800158 onPanelFullyOpened();
Daniel Sandler198a0302012-08-17 16:04:31 -0400159 } else if (fullyClosed && !mTracking && mState != STATE_CLOSED) {
Daniel Sandler978f8532012-08-15 15:48:16 -0400160 go(STATE_CLOSED);
Xiaohui Chen9f967112016-01-07 14:14:06 -0800161 onPanelCollapsed();
Daniel Sandler978f8532012-08-15 15:48:16 -0400162 }
Daniel Sandler08d05e32012-08-08 16:39:54 -0400163
Chris Wren16895942015-06-23 11:22:20 -0400164 if (SPEW) LOG("panelExpansionChanged: end state=%d [%s%s ]", mState,
Xiaohui Chen9f967112016-01-07 14:14:06 -0800165 fullyOpened?" fullyOpened":"", fullyClosed?" fullyClosed":"");
Daniel Sandler08d05e32012-08-08 16:39:54 -0400166 }
167
Xiaohui Chen9f967112016-01-07 14:14:06 -0800168 public void collapsePanel(boolean animate, boolean delayed, float speedUpFactor) {
Daniel Sandler198a0302012-08-17 16:04:31 -0400169 boolean waiting = false;
Xiaohui Chen9f967112016-01-07 14:14:06 -0800170 PanelView pv = mPanel;
171 if (animate && !pv.isFullyCollapsed()) {
172 pv.collapse(delayed, speedUpFactor);
173 waiting = true;
174 } else {
175 pv.resetViews();
176 pv.setExpandedFraction(0); // just in case
177 pv.cancelPeek();
Daniel Sandler08d05e32012-08-08 16:39:54 -0400178 }
Xiaohui Chen9f967112016-01-07 14:14:06 -0800179 if (DEBUG) LOG("collapsePanel: animate=%s waiting=%s", animate, waiting);
Daniel Sandlerc38bbc32012-10-05 12:21:38 -0400180 if (!waiting && mState != STATE_CLOSED) {
John Spurlock209bede2013-07-17 12:23:27 -0400181 // it's possible that nothing animated, so we replicate the termination
Daniel Sandler198a0302012-08-17 16:04:31 -0400182 // conditions of panelExpansionChanged here
183 go(STATE_CLOSED);
Xiaohui Chen9f967112016-01-07 14:14:06 -0800184 onPanelCollapsed();
Daniel Sandler198a0302012-08-17 16:04:31 -0400185 }
Daniel Sandler08d05e32012-08-08 16:39:54 -0400186 }
187
188 public void onPanelPeeked() {
Daniel Sandler67eab792012-10-02 17:08:23 -0400189 if (DEBUG) LOG("onPanelPeeked");
Daniel Sandler08d05e32012-08-08 16:39:54 -0400190 }
191
Jason Monkaa573e92017-01-27 17:00:29 -0500192 public boolean isClosed() {
193 return mState == STATE_CLOSED;
194 }
195
Xiaohui Chen9f967112016-01-07 14:14:06 -0800196 public void onPanelCollapsed() {
197 if (DEBUG) LOG("onPanelCollapsed");
Daniel Sandler08d05e32012-08-08 16:39:54 -0400198 }
199
Xiaohui Chen9f967112016-01-07 14:14:06 -0800200 public void onPanelFullyOpened() {
Daniel Sandler67eab792012-10-02 17:08:23 -0400201 if (DEBUG) LOG("onPanelFullyOpened");
Daniel Sandler08d05e32012-08-08 16:39:54 -0400202 }
Daniel Sandler978f8532012-08-15 15:48:16 -0400203
Xiaohui Chen9f967112016-01-07 14:14:06 -0800204 public void onTrackingStarted() {
Daniel Sandler978f8532012-08-15 15:48:16 -0400205 mTracking = true;
Daniel Sandler978f8532012-08-15 15:48:16 -0400206 }
207
Xiaohui Chen9f967112016-01-07 14:14:06 -0800208 public void onTrackingStopped(boolean expand) {
Daniel Sandler978f8532012-08-15 15:48:16 -0400209 mTracking = false;
Daniel Sandler978f8532012-08-15 15:48:16 -0400210 }
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200211
212 public void onExpandingFinished() {
Chris Wren16895942015-06-23 11:22:20 -0400213 if (DEBUG) LOG("onExpandingFinished");
Jorim Jaggi2fbad7b2014-05-26 22:38:00 +0200214 }
Selim Cinekdbbcfbe2014-10-24 17:52:35 +0200215
216 public void onClosingFinished() {
217
218 }
Daniel Sandler08d05e32012-08-08 16:39:54 -0400219}