Debian Codegraph @

libs/WindowManager/Shell/src/com/android/wm/shell/bubbles/BubblePositioner.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2020 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
17 package com.android.wm.shell.bubbles;
18
19 import static android.view.View.LAYOUT_DIRECTION_RTL;
20
21 import static java.lang.annotation.RetentionPolicy.SOURCE;
22
23 import android.annotation.IntDef;
24 import android.content.Context;
25 import android.content.res.Configuration;
26 import android.content.res.Resources;
27 import android.graphics.Insets;
28 import android.graphics.PointF;
29 import android.graphics.Rect;
30 import android.graphics.RectF;
31 import android.util.Log;
32 import android.view.Surface;
33 import android.view.WindowInsets;
34 import android.view.WindowManager;
35 import android.view.WindowMetrics;
36
37 import androidx.annotation.VisibleForTesting;
38
39 import com.android.launcher3.icons.IconNormalizer;
40 import com.android.wm.shell.R;
41
42 import java.lang.annotation.Retention;
43
44 /**
45 * Keeps track of display size, configuration, and specific bubble sizes. One place for all
46 * placement and positioning calculations to refer to.
47 */
48 public class BubblePositioner {
49 private static final String TAG = BubbleDebugConfig.TAG_WITH_CLASS_NAME
50 ? "BubblePositioner"
51 : BubbleDebugConfig.TAG_BUBBLES;
52
53 @Retention(SOURCE)
54 @IntDef({TASKBAR_POSITION_NONE, TASKBAR_POSITION_RIGHT, TASKBAR_POSITION_LEFT,
55 TASKBAR_POSITION_BOTTOM})
56 @interface TaskbarPosition {}
57 public static final int TASKBAR_POSITION_NONE = -1;
58 public static final int TASKBAR_POSITION_RIGHT = 0;
59 public static final int TASKBAR_POSITION_LEFT = 1;
60 public static final int TASKBAR_POSITION_BOTTOM = 2;
61
62 /** When the bubbles are collapsed in a stack only some of them are shown, this is how many. **/
63 public static final int NUM_VISIBLE_WHEN_RESTING = 2;
64 /** Indicates a bubble's height should be the maximum available space. **/
65 public static final int MAX_HEIGHT = -1;
66 /** The max percent of screen width to use for the flyout on large screens. */
67 public static final float FLYOUT_MAX_WIDTH_PERCENT_LARGE_SCREEN = 0.3f;
68 /** The max percent of screen width to use for the flyout on phone. */
69 public static final float FLYOUT_MAX_WIDTH_PERCENT = 0.6f;
70 /** The percent of screen width that should be used for the expanded view on a large screen. **/
71 private static final float EXPANDED_VIEW_LARGE_SCREEN_LANDSCAPE_WIDTH_PERCENT = 0.48f;
72 /** The percent of screen width that should be used for the expanded view on a large screen. **/
73 private static final float EXPANDED_VIEW_LARGE_SCREEN_PORTRAIT_WIDTH_PERCENT = 0.70f;
74 /** The percent of screen width that should be used for the expanded view on a small tablet. **/
75 private static final float EXPANDED_VIEW_SMALL_TABLET_WIDTH_PERCENT = 0.72f;
76
77 private Context mContext;
78 private WindowManager mWindowManager;
79 private Rect mScreenRect;
80 private @Surface.Rotation int mRotation = Surface.ROTATION_0;
81 private Insets mInsets;
82 private boolean mImeVisible;
83 private int mImeHeight;
84 private boolean mIsLargeScreen;
85 private boolean mIsSmallTablet;
86
87 private Rect mPositionRect;
88 private int mDefaultMaxBubbles;
89 private int mMaxBubbles;
90 private int mBubbleSize;
91 private int mSpacingBetweenBubbles;
92 private int mBubblePaddingTop;
93 private int mBubbleOffscreenAmount;
94 private int mStackOffset;
95
96 private int mExpandedViewMinHeight;
97 private int mExpandedViewLargeScreenWidth;
98 private int mExpandedViewLargeScreenInsetClosestEdge;
99 private int mExpandedViewLargeScreenInsetFurthestEdge;
100
101 private int mOverflowWidth;
102 private int mExpandedViewPadding;
103 private int mPointerMargin;
104 private int mPointerWidth;
105 private int mPointerHeight;
106 private int mPointerOverlap;
107 private int mManageButtonHeight;
108 private int mOverflowHeight;
109 private int mMinimumFlyoutWidthLargeScreen;
110
111 private PointF mPinLocation;
112 private PointF mRestingStackPosition;
113 private int[] mPaddings = new int[4];
114
115 private boolean mShowingInTaskbar;
116 private @TaskbarPosition int mTaskbarPosition = TASKBAR_POSITION_NONE;
117 private int mTaskbarIconSize;
118 private int mTaskbarSize;
119
120 public BubblePositioner(Context context, WindowManager windowManager) {
121 mContext = context;
122 mWindowManager = windowManager;
123 update();
124 }
125
126 /**
127 * Available space and inset information. Call this when config changes
128 * occur or when added to a window.
129 */
130 public void update() {
131 WindowMetrics windowMetrics = mWindowManager.getCurrentWindowMetrics();
132 if (windowMetrics == null) {
133 return;
134 }
135 WindowInsets metricInsets = windowMetrics.getWindowInsets();
136 Insets insets = metricInsets.getInsetsIgnoringVisibility(WindowInsets.Type.navigationBars()
137 | WindowInsets.Type.statusBars()
138 | WindowInsets.Type.displayCutout());
139
140 final Rect bounds = windowMetrics.getBounds();
141 Configuration config = mContext.getResources().getConfiguration();
142 mIsLargeScreen = config.smallestScreenWidthDp >= 600;
143 if (mIsLargeScreen) {
144 float largestEdgeDp = Math.max(config.screenWidthDp, config.screenHeightDp);
145 mIsSmallTablet = largestEdgeDp < 960;
146 } else {
147 mIsSmallTablet = false;
148 }
149
150 if (BubbleDebugConfig.DEBUG_POSITIONER) {
151 Log.w(TAG, "update positioner:"
152 + " rotation: " + mRotation
153 + " insets: " + insets
154 + " isLargeScreen: " + mIsLargeScreen
155 + " isSmallTablet: " + mIsSmallTablet
156 + " bounds: " + bounds
157 + " showingInTaskbar: " + mShowingInTaskbar);
158 }
159 updateInternal(mRotation, insets, bounds);
160 }
161
162 /**
163 * Updates position information to account for taskbar state.
164 *
165 * @param taskbarPosition which position the taskbar is displayed in.
166 * @param showingInTaskbar whether the taskbar is being shown.
167 */
168 public void updateForTaskbar(int iconSize,
169 @TaskbarPosition int taskbarPosition, boolean showingInTaskbar, int taskbarSize) {
170 mShowingInTaskbar = showingInTaskbar;
171 mTaskbarIconSize = iconSize;
172 mTaskbarPosition = taskbarPosition;
173 mTaskbarSize = taskbarSize;
174 update();
175 }
176
177 @VisibleForTesting
178 public void updateInternal(int rotation, Insets insets, Rect bounds) {
179 mRotation = rotation;
180 mInsets = insets;
181
182 mScreenRect = new Rect(bounds);
183 mPositionRect = new Rect(bounds);
184 mPositionRect.left += mInsets.left;
185 mPositionRect.top += mInsets.top;
186 mPositionRect.right -= mInsets.right;
187 mPositionRect.bottom -= mInsets.bottom;
188
189 Resources res = mContext.getResources();
190 mBubbleSize = res.getDimensionPixelSize(R.dimen.bubble_size);
191 mSpacingBetweenBubbles = res.getDimensionPixelSize(R.dimen.bubble_spacing);
192 mDefaultMaxBubbles = res.getInteger(R.integer.bubbles_max_rendered);
193 mExpandedViewPadding = res.getDimensionPixelSize(R.dimen.bubble_expanded_view_padding);
194 mBubblePaddingTop = res.getDimensionPixelSize(R.dimen.bubble_padding_top);
195 mBubbleOffscreenAmount = res.getDimensionPixelSize(R.dimen.bubble_stack_offscreen);
196 mStackOffset = res.getDimensionPixelSize(R.dimen.bubble_stack_offset);
197
198 if (mIsSmallTablet) {
199 mExpandedViewLargeScreenWidth = (int) (bounds.width()
200 * EXPANDED_VIEW_SMALL_TABLET_WIDTH_PERCENT);
201 } else {
202 mExpandedViewLargeScreenWidth = isLandscape()
203 ? (int) (bounds.width() * EXPANDED_VIEW_LARGE_SCREEN_LANDSCAPE_WIDTH_PERCENT)
204 : (int) (bounds.width() * EXPANDED_VIEW_LARGE_SCREEN_PORTRAIT_WIDTH_PERCENT);
205 }
206 if (mIsLargeScreen) {
207 if (isLandscape() && !mIsSmallTablet) {
208 mExpandedViewLargeScreenInsetClosestEdge = res.getDimensionPixelSize(
209 R.dimen.bubble_expanded_view_largescreen_landscape_padding);
210 mExpandedViewLargeScreenInsetFurthestEdge = bounds.width()
211 - mExpandedViewLargeScreenInsetClosestEdge
212 - mExpandedViewLargeScreenWidth;
213 } else {
214 final int centeredInset = (bounds.width() - mExpandedViewLargeScreenWidth) / 2;
215 mExpandedViewLargeScreenInsetClosestEdge = centeredInset;
216 mExpandedViewLargeScreenInsetFurthestEdge = centeredInset;
217 }
218 } else {
219 mExpandedViewLargeScreenInsetClosestEdge = mExpandedViewPadding;
220 mExpandedViewLargeScreenInsetFurthestEdge = mExpandedViewPadding;
221 }
222
223 mOverflowWidth = res.getDimensionPixelSize(R.dimen.bubble_expanded_view_overflow_width);
224 mPointerWidth = res.getDimensionPixelSize(R.dimen.bubble_pointer_width);
225 mPointerHeight = res.getDimensionPixelSize(R.dimen.bubble_pointer_height);
226 mPointerMargin = res.getDimensionPixelSize(R.dimen.bubble_pointer_margin);
227 mPointerOverlap = res.getDimensionPixelSize(R.dimen.bubble_pointer_overlap);
228 mManageButtonHeight = res.getDimensionPixelSize(R.dimen.bubble_manage_button_total_height);
229 mExpandedViewMinHeight = res.getDimensionPixelSize(R.dimen.bubble_expanded_default_height);
230 mOverflowHeight = res.getDimensionPixelSize(R.dimen.bubble_overflow_height);
231 mMinimumFlyoutWidthLargeScreen = res.getDimensionPixelSize(
232 R.dimen.bubbles_flyout_min_width_large_screen);
233
234 mMaxBubbles = calculateMaxBubbles();
235
236 if (mShowingInTaskbar) {
237 adjustForTaskbar();
238 }
239 }
240
241 /**
242 * @return the maximum number of bubbles that can fit on the screen when expanded. If the
243 * screen size / screen density is too small to support the default maximum number, then
244 * the number will be adjust to something lower to ensure everything is presented nicely.
245 */
246 private int calculateMaxBubbles() {
247 // Use the shortest edge.
248 // In portrait the bubbles should align with the expanded view so subtract its padding.
249 // We always show the overflow so subtract one bubble size.
250 int padding = showBubblesVertically() ? 0 : (mExpandedViewPadding * 2);
251 int availableSpace = Math.min(mPositionRect.width(), mPositionRect.height())
252 - padding
253 - mBubbleSize;
254 // Each of the bubbles have spacing because the overflow is at the end.
255 int howManyFit = availableSpace / (mBubbleSize + mSpacingBetweenBubbles);
256 if (howManyFit < mDefaultMaxBubbles) {
257 // Not enough space for the default.
258 return howManyFit;
259 }
260 return mDefaultMaxBubbles;
261 }
262
263 /**
264 * Taskbar insets appear as navigationBar insets, however, unlike navigationBar this should
265 * not inset bubbles UI as bubbles floats above the taskbar. This adjust the available space
266 * and insets to account for the taskbar.
267 */
268 // TODO(b/171559950): When the insets are reported correctly we can remove this logic
269 private void adjustForTaskbar() {
270 // When bar is showing on edges... subtract that inset because we appear on top
271 if (mShowingInTaskbar && mTaskbarPosition != TASKBAR_POSITION_BOTTOM) {
272 WindowInsets metricInsets = mWindowManager.getCurrentWindowMetrics().getWindowInsets();
273 Insets navBarInsets = metricInsets.getInsetsIgnoringVisibility(
274 WindowInsets.Type.navigationBars());
275 int newInsetLeft = mInsets.left;
276 int newInsetRight = mInsets.right;
277 if (mTaskbarPosition == TASKBAR_POSITION_LEFT) {
278 mPositionRect.left -= navBarInsets.left;
279 newInsetLeft -= navBarInsets.left;
280 } else if (mTaskbarPosition == TASKBAR_POSITION_RIGHT) {
281 mPositionRect.right += navBarInsets.right;
282 newInsetRight -= navBarInsets.right;
283 }
284 mInsets = Insets.of(newInsetLeft, mInsets.top, newInsetRight, mInsets.bottom);
285 }
286 }
287
288 /**
289 * @return a rect of available screen space accounting for orientation, system bars and cutouts.
290 * Does not account for IME.
291 */
292 public Rect getAvailableRect() {
293 return mPositionRect;
294 }
295
296 /**
297 * @return a rect of the screen size.
298 */
299 public Rect getScreenRect() {
300 return mScreenRect;
301 }
302
303 /**
304 * @return the relevant insets (status bar, nav bar, cutouts). If taskbar is showing, its
305 * inset is not included here.
306 */
307 public Insets getInsets() {
308 return mInsets;
309 }
310
311 /** @return whether the device is in landscape orientation. */
312 public boolean isLandscape() {
313 return mContext.getResources().getConfiguration().orientation
314 == Configuration.ORIENTATION_LANDSCAPE;
315 }
316
317 /** @return whether the screen is considered large. */
318 public boolean isLargeScreen() {
319 return mIsLargeScreen;
320 }
321
322 /**
323 * Indicates how bubbles appear when expanded.
324 *
325 * When false, bubbles display at the top of the screen with the expanded view
326 * below them. When true, bubbles display at the edges of the screen with the expanded view
327 * to the left or right side.
328 */
329 public boolean showBubblesVertically() {
330 return isLandscape() || mShowingInTaskbar || mIsLargeScreen;
331 }
332
333 /** Size of the bubble. */
334 public int getBubbleSize() {
335 return (mShowingInTaskbar && mTaskbarIconSize > 0)
336 ? mTaskbarIconSize
337 : mBubbleSize;
338 }
339
340 /** The amount of padding at the top of the screen that the bubbles avoid when being placed. */
341 public int getBubblePaddingTop() {
342 return mBubblePaddingTop;
343 }
344
345 /** The amount the stack hang off of the screen when collapsed. */
346 public int getStackOffScreenAmount() {
347 return mBubbleOffscreenAmount;
348 }
349
350 /** Offset of bubbles in the stack (i.e. how much they overlap). */
351 public int getStackOffset() {
352 return mStackOffset;
353 }
354
355 /** Size of the visible (non-overlapping) part of the pointer. */
356 public int getPointerSize() {
357 return mPointerHeight - mPointerOverlap;
358 }
359
360 /** The maximum number of bubbles that can be displayed comfortably on screen. */
361 public int getMaxBubbles() {
362 return mMaxBubbles;
363 }
364
365 /** The height for the IME if it's visible. **/
366 public int getImeHeight() {
367 return mImeVisible ? mImeHeight : 0;
368 }
369
370 /** Return top position of the IME if it's visible */
371 public int getImeTop() {
372 if (mImeVisible) {
373 return getScreenRect().bottom - getImeHeight() - getInsets().bottom;
374 }
375 return 0;
376 }
377
378 /** Sets whether the IME is visible. **/
379 public void setImeVisible(boolean visible, int height) {
380 mImeVisible = visible;
381 mImeHeight = height;
382 }
383
384 private int getExpandedViewLargeScreenInsetFurthestEdge(boolean isOverflow) {
385 if (isOverflow && mIsLargeScreen) {
386 return mScreenRect.width()
387 - mExpandedViewLargeScreenInsetClosestEdge
388 - mOverflowWidth;
389 }
390 return mExpandedViewLargeScreenInsetFurthestEdge;
391 }
392
393 /**
394 * Calculates the padding for the bubble expanded view.
395 *
396 * Some specifics:
397 * On large screens the width of the expanded view is restricted via this padding.
398 * On phone landscape the bubble overflow expanded view is also restricted via this padding.
399 * On large screens & landscape no top padding is set, the top position is set via translation.
400 * On phone portrait top padding is set as the space between the tip of the pointer and the
401 * bubble.
402 * When the overflow is shown it doesn't have the manage button to pad out the bottom so
403 * padding is added.
404 */
405 public int[] getExpandedViewContainerPadding(boolean onLeft, boolean isOverflow) {
406 final int pointerTotalHeight = getPointerSize();
407 final int expandedViewLargeScreenInsetFurthestEdge =
408 getExpandedViewLargeScreenInsetFurthestEdge(isOverflow);
409 if (mIsLargeScreen) {
410 // Note:
411 // If we're in portrait OR if we're a small tablet, then the two insets values will
412 // be equal. If we're landscape and a large tablet, the two values will be different.
413 // [left, top, right, bottom]
414 mPaddings[0] = onLeft
415 ? mExpandedViewLargeScreenInsetClosestEdge - pointerTotalHeight
416 : expandedViewLargeScreenInsetFurthestEdge;
417 mPaddings[1] = 0;
418 mPaddings[2] = onLeft
419 ? expandedViewLargeScreenInsetFurthestEdge
420 : mExpandedViewLargeScreenInsetClosestEdge - pointerTotalHeight;
421 // Overflow doesn't show manage button / get padding from it so add padding here
422 mPaddings[3] = isOverflow ? mExpandedViewPadding : 0;
423 return mPaddings;
424 } else {
425 int leftPadding = mInsets.left + mExpandedViewPadding;
426 int rightPadding = mInsets.right + mExpandedViewPadding;
427 final float expandedViewWidth = isOverflow
428 ? mOverflowWidth
429 : mExpandedViewLargeScreenWidth;
430 if (showBubblesVertically()) {
431 if (!onLeft) {
432 rightPadding += mBubbleSize - pointerTotalHeight;
433 leftPadding += isOverflow
434 ? (mPositionRect.width() - rightPadding - expandedViewWidth)
435 : 0;
436 } else {
437 leftPadding += mBubbleSize - pointerTotalHeight;
438 rightPadding += isOverflow
439 ? (mPositionRect.width() - leftPadding - expandedViewWidth)
440 : 0;
441 }
442 }
443 // [left, top, right, bottom]
444 mPaddings[0] = leftPadding;
445 mPaddings[1] = showBubblesVertically() ? 0 : mPointerMargin;
446 mPaddings[2] = rightPadding;
447 mPaddings[3] = 0;
448 return mPaddings;
449 }
450 }
451
452 /** Gets the y position of the expanded view if it was top-aligned. */
453 public float getExpandedViewYTopAligned() {
454 final int top = getAvailableRect().top;
455 if (showBubblesVertically()) {
456 return top - mPointerWidth + mExpandedViewPadding;
457 } else {
458 return top + mBubbleSize + mPointerMargin;
459 }
460 }
461
462 /**
463 * Calculate the maximum height the expanded view can be depending on where it's placed on
464 * the screen and the size of the elements around it (e.g. padding, pointer, manage button).
465 */
466 public int getMaxExpandedViewHeight(boolean isOverflow) {
467 // Subtract top insets because availableRect.height would account for that
468 int expandedContainerY = (int) getExpandedViewYTopAligned() - getInsets().top;
469 int paddingTop = showBubblesVertically()
470 ? 0
471 : mPointerHeight;
472 // Subtract pointer size because it's laid out in LinearLayout with the expanded view.
473 int pointerSize = showBubblesVertically()
474 ? mPointerWidth
475 : (mPointerHeight + mPointerMargin);
476 int bottomPadding = isOverflow ? mExpandedViewPadding : mManageButtonHeight;
477 return getAvailableRect().height()
478 - expandedContainerY
479 - paddingTop
480 - pointerSize
481 - bottomPadding;
482 }
483
484 /**
485 * Determines the height for the bubble, ensuring a minimum height. If the height should be as
486 * big as available, returns {@link #MAX_HEIGHT}.
487 */
488 public float getExpandedViewHeight(BubbleViewProvider bubble) {
489 boolean isOverflow = bubble == null || BubbleOverflow.KEY.equals(bubble.getKey());
490 if (isOverflow && showBubblesVertically() && !mIsLargeScreen) {
491 // overflow in landscape on phone is max
492 return MAX_HEIGHT;
493 }
494 float desiredHeight = isOverflow
495 ? mOverflowHeight
496 : ((Bubble) bubble).getDesiredHeight(mContext);
497 desiredHeight = Math.max(desiredHeight, mExpandedViewMinHeight);
498 if (desiredHeight > getMaxExpandedViewHeight(isOverflow)) {
499 return MAX_HEIGHT;
500 }
501 return desiredHeight;
502 }
503
504 /**
505 * Gets the y position for the expanded view. This is the position on screen of the top
506 * horizontal line of the expanded view.
507 *
508 * @param bubble the bubble being positioned.
509 * @param bubblePosition the x position of the bubble if showing on top, the y position of the
510 * bubble if showing vertically.
511 * @return the y position for the expanded view.
512 */
513 public float getExpandedViewY(BubbleViewProvider bubble, float bubblePosition) {
514 boolean isOverflow = bubble == null || BubbleOverflow.KEY.equals(bubble.getKey());
515 float expandedViewHeight = getExpandedViewHeight(bubble);
516 float topAlignment = getExpandedViewYTopAligned();
517 if (!showBubblesVertically() || expandedViewHeight == MAX_HEIGHT) {
518 // Top-align when bubbles are shown at the top or are max size.
519 return topAlignment;
520 }
521 // If we're here, we're showing vertically & developer has made height less than maximum.
522 int manageButtonHeight = isOverflow ? mExpandedViewPadding : mManageButtonHeight;
523 float pointerPosition = getPointerPosition(bubblePosition);
524 float bottomIfCentered = pointerPosition + (expandedViewHeight / 2) + manageButtonHeight;
525 float topIfCentered = pointerPosition - (expandedViewHeight / 2);
526 if (topIfCentered > mPositionRect.top && mPositionRect.bottom > bottomIfCentered) {
527 // Center it
528 return pointerPosition - mPointerWidth - (expandedViewHeight / 2f);
529 } else if (topIfCentered <= mPositionRect.top) {
530 // Top align
531 return topAlignment;
532 } else {
533 // Bottom align
534 return mPositionRect.bottom - manageButtonHeight - expandedViewHeight - mPointerWidth;
535 }
536 }
537
538 /**
539 * The position the pointer points to, the center of the bubble.
540 *
541 * @param bubblePosition the x position of the bubble if showing on top, the y position of the
542 * bubble if showing vertically.
543 * @return the position the tip of the pointer points to. The x position if showing on top, the
544 * y position if showing vertically.
545 */
546 public float getPointerPosition(float bubblePosition) {
547 // TODO: I don't understand why it works but it does - why normalized in portrait
548 // & not in landscape? Am I missing ~2dp in the portrait expandedViewY calculation?
549 final float normalizedSize = IconNormalizer.getNormalizedCircleSize(
550 getBubbleSize());
551 return showBubblesVertically()
552 ? bubblePosition + (getBubbleSize() / 2f)
553 : bubblePosition + (normalizedSize / 2f) - mPointerWidth;
554 }
555
556 private int getExpandedStackSize(int numberOfBubbles) {
557 return (numberOfBubbles * mBubbleSize)
558 + ((numberOfBubbles - 1) * mSpacingBetweenBubbles);
559 }
560
561 /**
562 * Returns the position of the bubble on-screen when the stack is expanded.
563 *
564 * @param index the index of the bubble in the stack.
565 * @param state state information about the stack to help with calculations.
566 * @return the position of the bubble on-screen when the stack is expanded.
567 */
568 public PointF getExpandedBubbleXY(int index, BubbleStackView.StackViewState state) {
569 boolean showBubblesVertically = showBubblesVertically();
570 boolean isRtl = mContext.getResources().getConfiguration().getLayoutDirection()
571 == LAYOUT_DIRECTION_RTL;
572
573 int onScreenIndex;
574 if (showBubblesVertically || !isRtl) {
575 onScreenIndex = index;
576 } else {
577 // If bubbles are shown horizontally, check if RTL language is used.
578 // If RTL is active, position first bubble on the right and last on the left.
579 // Last bubble has screen index 0 and first bubble has max screen index value.
580 onScreenIndex = state.numberOfBubbles - 1 - index;
581 }
582
583 final float positionInRow = onScreenIndex * (mBubbleSize + mSpacingBetweenBubbles);
584 final float expandedStackSize = getExpandedStackSize(state.numberOfBubbles);
585 final float centerPosition = showBubblesVertically
586 ? mPositionRect.centerY()
587 : mPositionRect.centerX();
588 // alignment - centered on the edge
589 final float rowStart = centerPosition - (expandedStackSize / 2f);
590 float x;
591 float y;
592 if (showBubblesVertically) {
593 int inset = mExpandedViewLargeScreenInsetClosestEdge;
594 y = rowStart + positionInRow;
595 int left = mIsLargeScreen
596 ? inset - mExpandedViewPadding - mBubbleSize
597 : mPositionRect.left;
598 int right = mIsLargeScreen
599 ? mPositionRect.right - inset + mExpandedViewPadding
600 : mPositionRect.right - mBubbleSize;
601 x = state.onLeft
602 ? left
603 : right;
604 } else {
605 y = mPositionRect.top + mExpandedViewPadding;
606 x = rowStart + positionInRow;
607 }
608
609 if (showBubblesVertically && mImeVisible) {
610 return new PointF(x, getExpandedBubbleYForIme(onScreenIndex, state));
611 }
612 return new PointF(x, y);
613 }
614
615 /**
616 * Returns the position of the bubble on-screen when the stack is expanded and the IME
617 * is showing.
618 *
619 * @param index the index of the bubble in the stack.
620 * @param state information about the stack state (# of bubbles, selected bubble).
621 * @return y position of the bubble on-screen when the stack is expanded.
622 */
623 private float getExpandedBubbleYForIme(int index, BubbleStackView.StackViewState state) {
624 final float top = getAvailableRect().top + mExpandedViewPadding;
625 if (!showBubblesVertically()) {
626 // Showing horizontally: align to top
627 return top;
628 }
629
630 // Showing vertically: might need to translate the bubbles above the IME.
631 // Add spacing here to provide a margin between top of IME and bottom of bubble row.
632 final float bottomHeight = getImeHeight() + mInsets.bottom + (mSpacingBetweenBubbles * 2);
633 final float bottomInset = mScreenRect.bottom - bottomHeight;
634 final float expandedStackSize = getExpandedStackSize(state.numberOfBubbles);
635 final float centerPosition = mPositionRect.centerY();
636 final float rowBottom = centerPosition + (expandedStackSize / 2f);
637 final float rowTop = centerPosition - (expandedStackSize / 2f);
638 float rowTopForIme = rowTop;
639 if (rowBottom > bottomInset) {
640 // We overlap with IME, must shift the bubbles
641 float translationY = rowBottom - bottomInset;
642 rowTopForIme = Math.max(rowTop - translationY, top);
643 if (rowTop - translationY < top) {
644 // Even if we shift the bubbles, they will still overlap with the IME.
645 // Hide the overflow for a lil more space:
646 final float expandedStackSizeNoO = getExpandedStackSize(state.numberOfBubbles - 1);
647 final float centerPositionNoO = showBubblesVertically()
648 ? mPositionRect.centerY()
649 : mPositionRect.centerX();
650 final float rowBottomNoO = centerPositionNoO + (expandedStackSizeNoO / 2f);
651 final float rowTopNoO = centerPositionNoO - (expandedStackSizeNoO / 2f);
652 translationY = rowBottomNoO - bottomInset;
653 rowTopForIme = rowTopNoO - translationY;
654 }
655 }
656 // Check if the selected bubble is within the appropriate space
657 final float selectedPosition = rowTopForIme
658 + (state.selectedIndex * (mBubbleSize + mSpacingBetweenBubbles));
659 if (selectedPosition < top) {
660 // We must always keep the selected bubble in view so we'll have to allow more overlap.
661 rowTopForIme = top;
662 }
663 return rowTopForIme + (index * (mBubbleSize + mSpacingBetweenBubbles));
664 }
665
666 /**
667 * @return the width of the bubble flyout (message originating from the bubble).
668 */
669 public float getMaxFlyoutSize() {
670 if (isLargeScreen()) {
671 return Math.max(mScreenRect.width() * FLYOUT_MAX_WIDTH_PERCENT_LARGE_SCREEN,
672 mMinimumFlyoutWidthLargeScreen);
673 }
674 return mScreenRect.width() * FLYOUT_MAX_WIDTH_PERCENT;
675 }
676
677 /**
678 * @return whether the stack is considered on the left side of the screen.
679 */
680 public boolean isStackOnLeft(PointF currentStackPosition) {
681 if (currentStackPosition == null) {
682 currentStackPosition = getRestingPosition();
683 }
684 final int stackCenter = (int) currentStackPosition.x + mBubbleSize / 2;
685 return stackCenter < mScreenRect.width() / 2;
686 }
687
688 /**
689 * Sets the stack's most recent position along the edge of the screen. This is saved when the
690 * last bubble is removed, so that the stack can be restored in its previous position.
691 */
692 public void setRestingPosition(PointF position) {
693 if (mRestingStackPosition == null) {
694 mRestingStackPosition = new PointF(position);
695 } else {
696 mRestingStackPosition.set(position);
697 }
698 }
699
700 /** The position the bubble stack should rest at when collapsed. */
701 public PointF getRestingPosition() {
702 if (mPinLocation != null) {
703 return mPinLocation;
704 }
705 if (mRestingStackPosition == null) {
706 return getDefaultStartPosition();
707 }
708 return mRestingStackPosition;
709 }
710
711 /**
712 * @return the stack position to use if we don't have a saved location or if user education
713 * is being shown.
714 */
715 public PointF getDefaultStartPosition() {
716 if (mPinLocation != null) {
717 return mPinLocation;
718 }
719 // Start on the left if we're in LTR, right otherwise.
720 final boolean startOnLeft =
721 mContext.getResources().getConfiguration().getLayoutDirection()
722 != LAYOUT_DIRECTION_RTL;
723 final float startingVerticalOffset = mContext.getResources().getDimensionPixelOffset(
724 R.dimen.bubble_stack_starting_offset_y);
725 // TODO: placement bug here because mPositionRect doesn't handle the overhanging edge
726 return new BubbleStackView.RelativeStackPosition(
727 startOnLeft,
728 startingVerticalOffset / mPositionRect.height())
729 .getAbsolutePositionInRegion(getAllowableStackPositionRegion(
730 1 /* default starts with 1 bubble */));
731 }
732
733
734 /**
735 * Returns the region that the stack position must stay within. This goes slightly off the left
736 * and right sides of the screen, below the status bar/cutout and above the navigation bar.
737 * While the stack position is not allowed to rest outside of these bounds, it can temporarily
738 * be animated or dragged beyond them.
739 */
740 public RectF getAllowableStackPositionRegion(int bubbleCount) {
741 final RectF allowableRegion = new RectF(getAvailableRect());
742 final int imeHeight = getImeHeight();
743 final float bottomPadding = bubbleCount > 1
744 ? mBubblePaddingTop + mStackOffset
745 : mBubblePaddingTop;
746 allowableRegion.left -= mBubbleOffscreenAmount;
747 allowableRegion.top += mBubblePaddingTop;
748 allowableRegion.right += mBubbleOffscreenAmount - mBubbleSize;
749 allowableRegion.bottom -= imeHeight + bottomPadding + mBubbleSize;
750 return allowableRegion;
751 }
752
753 /**
754 * @return whether the bubble stack is pinned to the taskbar.
755 */
756 public boolean showingInTaskbar() {
757 return mShowingInTaskbar;
758 }
759
760 /**
761 * @return the taskbar position if set.
762 */
763 public int getTaskbarPosition() {
764 return mTaskbarPosition;
765 }
766
767 public int getTaskbarSize() {
768 return mTaskbarSize;
769 }
770
771 /**
772 * In some situations bubbles will be pinned to a specific onscreen location. This sets whether
773 * bubbles should be pinned or not.
774 */
775 public void setUsePinnedLocation(boolean usePinnedLocation) {
776 if (usePinnedLocation) {
777 mShowingInTaskbar = true;
778 mPinLocation = new PointF(mPositionRect.right - mBubbleSize,
779 mPositionRect.bottom - mBubbleSize);
780 } else {
781 mPinLocation = null;
782 mShowingInTaskbar = false;
783 }
784 }
785
786 /**
787 * Navigation bar has an area where system gestures can be started from.
788 *
789 * @return {@link Rect} for system navigation bar gesture zone
790 */
791 public Rect getNavBarGestureZone() {
792 // Gesture zone height from the bottom
793 int gestureZoneHeight = mContext.getResources().getDimensionPixelSize(
794 com.android.internal.R.dimen.navigation_bar_gesture_height);
795 Rect screen = getScreenRect();
796 return new Rect(
797 screen.left,
798 screen.bottom - gestureZoneHeight,
799 screen.right,
800 screen.bottom);
801 }
802 }