NotificationPanelView.java revision 1685e634fb0b14033bd436af8d7174436699ffec
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.view.MotionEvent; 22import android.view.View; 23import android.view.accessibility.AccessibilityEvent; 24 25import com.android.systemui.R; 26import com.android.systemui.statusbar.GestureRecorder; 27import com.android.systemui.statusbar.stack.NotificationStackScrollLayout; 28 29public class NotificationPanelView extends PanelView { 30 public static final boolean DEBUG_GESTURES = true; 31 32 PhoneStatusBar mStatusBar; 33 private NotificationStackScrollLayout mNotificationStackScroller; 34 private int[] mTempLocation = new int[2]; 35 private int[] mTempChildLocation = new int[2]; 36 private View mNotificationParent; 37 38 39 public NotificationPanelView(Context context, AttributeSet attrs) { 40 super(context, attrs); 41 } 42 43 public void setStatusBar(PhoneStatusBar bar) { 44 if (mStatusBar != null) { 45 mStatusBar.setOnFlipRunnable(null); 46 } 47 mStatusBar = bar; 48 if (bar != null) { 49 mStatusBar.setOnFlipRunnable(new Runnable() { 50 @Override 51 public void run() { 52 requestPanelHeightUpdate(); 53 } 54 }); 55 } 56 } 57 58 @Override 59 protected void onFinishInflate() { 60 super.onFinishInflate(); 61 62 mNotificationStackScroller = (NotificationStackScrollLayout) 63 findViewById(R.id.notification_stack_scroller); 64 mNotificationParent = findViewById(R.id.notification_container_parent); 65 } 66 67 @Override 68 public void fling(float vel, boolean always) { 69 GestureRecorder gr = ((PhoneStatusBarView) mBar).mBar.getGestureRecorder(); 70 if (gr != null) { 71 gr.tag( 72 "fling " + ((vel > 0) ? "open" : "closed"), 73 "notifications,v=" + vel); 74 } 75 super.fling(vel, always); 76 } 77 78 @Override 79 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { 80 if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { 81 event.getText() 82 .add(getContext().getString(R.string.accessibility_desc_notification_shade)); 83 return true; 84 } 85 86 return super.dispatchPopulateAccessibilityEvent(event); 87 } 88 89 /** 90 * Gets the relative position of a view on the screen in regard to this view. 91 * 92 * @param requestedView the view we want to find the relative position for 93 * @return 94 */ 95 private int getRelativeTop(View requestedView) { 96 getLocationOnScreen(mTempLocation); 97 requestedView.getLocationOnScreen(mTempChildLocation); 98 return mTempChildLocation[1] - mTempLocation[1]; 99 } 100 101 @Override 102 public boolean onTouchEvent(MotionEvent event) { 103 // TODO: Handle doublefinger swipe to notifications again. Look at history for a reference 104 // implementation. 105 return super.onTouchEvent(event); 106 } 107 108 @Override 109 protected boolean isScrolledToBottom() { 110 if (!isInSettings()) { 111 return mNotificationStackScroller.isScrolledToBottom(); 112 } 113 return super.isScrolledToBottom(); 114 } 115 116 @Override 117 protected int getMaxPanelHeight() { 118 if (!isInSettings()) { 119 int maxPanelHeight = super.getMaxPanelHeight(); 120 int emptyBottomMargin = mNotificationStackScroller.getEmptyBottomMargin(); 121 return maxPanelHeight - emptyBottomMargin; 122 } 123 return super.getMaxPanelHeight(); 124 } 125 126 private boolean isInSettings() { 127 return mStatusBar != null && mStatusBar.isFlippedToSettings(); 128 } 129 130 @Override 131 protected void onHeightUpdated(float expandedHeight) { 132 updateNotificationStackHeight(expandedHeight); 133 } 134 135 /** 136 * Update the height of the {@link #mNotificationStackScroller} to the new expanded height. 137 * This is much more efficient than doing it over the layout pass. 138 * 139 * @param expandedHeight the new expanded height 140 */ 141 private void updateNotificationStackHeight(float expandedHeight) { 142 mNotificationStackScroller.setIsExpanded(expandedHeight > 0.0f); 143 float childOffset = getRelativeTop(mNotificationStackScroller) 144 - mNotificationParent.getTranslationY(); 145 int newStackHeight = (int) (expandedHeight - childOffset); 146 int itemHeight = mNotificationStackScroller.getItemHeight(); 147 int bottomStackPeekSize = mNotificationStackScroller.getBottomStackPeekSize(); 148 int minStackHeight = itemHeight + bottomStackPeekSize; 149 if (newStackHeight >= minStackHeight) { 150 mNotificationParent.setTranslationY(0); 151 mNotificationStackScroller.setCurrentStackHeight(newStackHeight); 152 } else { 153 154 // We did not reach the position yet where we actually start growing, 155 // so we translate the stack upwards. 156 int translationY = (newStackHeight - minStackHeight); 157 // A slight parallax effect is introduced in order for the stack to catch up with 158 // the top card. 159 float partiallyThere = (float) newStackHeight / minStackHeight; 160 partiallyThere = Math.max(0, partiallyThere); 161 translationY += (1 - partiallyThere) * bottomStackPeekSize; 162 mNotificationParent.setTranslationY(translationY); 163 mNotificationStackScroller.setCurrentStackHeight( 164 (int) (expandedHeight - (childOffset + translationY))); 165 } 166 } 167 168 @Override 169 protected int getDesiredMeasureHeight() { 170 return mMaxPanelHeight; 171 } 172 173 @Override 174 protected void onExpandingStarted() { 175 super.onExpandingStarted(); 176 mNotificationStackScroller.onExpansionStarted(); 177 } 178 179 @Override 180 protected void onExpandingFinished() { 181 super.onExpandingFinished(); 182 mNotificationStackScroller.onExpansionStopped(); 183 } 184} 185