NotificationPanelView.java revision b6d85ebfe4f9f5d3b7d7ab7b6123af02a0deb516
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 float childOffset = getRelativeTop(mNotificationStackScroller) 143 - mNotificationParent.getTranslationY(); 144 int newStackHeight = (int) (expandedHeight - childOffset); 145 int itemHeight = mNotificationStackScroller.getItemHeight(); 146 int bottomStackPeekSize = mNotificationStackScroller.getBottomStackPeekSize(); 147 int minStackHeight = itemHeight + bottomStackPeekSize; 148 if (newStackHeight >= minStackHeight) { 149 mNotificationParent.setTranslationY(0); 150 mNotificationStackScroller.setCurrentStackHeight(newStackHeight); 151 } else { 152 153 // We did not reach the position yet where we actually start growing, 154 // so we translate the stack upwards. 155 int translationY = (newStackHeight - minStackHeight); 156 // A slight parallax effect is introduced in order for the stack to catch up with 157 // the top card. 158 float partiallyThere = (float) newStackHeight / minStackHeight; 159 partiallyThere = Math.max(0, partiallyThere); 160 translationY += (1 - partiallyThere) * bottomStackPeekSize; 161 mNotificationParent.setTranslationY(translationY); 162 mNotificationStackScroller.setCurrentStackHeight( 163 (int) (expandedHeight - (childOffset + translationY))); 164 } 165 } 166 167 @Override 168 protected int getDesiredMeasureHeight() { 169 return mMaxPanelHeight; 170 } 171} 172