blob: d13952ba5c7b18a7069eb5ba91f05e271a253c5e [file] [log] [blame]
Eric Erfanian938468d2017-10-24 14:05:52 -07001/*
2 * Copyright (C) 2017 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.newbubble;
18
19import android.animation.Animator;
yueg87111362017-12-08 12:45:50 -080020import android.animation.AnimatorListenerAdapter;
Eric Erfanian938468d2017-10-24 14:05:52 -070021import android.animation.AnimatorSet;
22import android.animation.ObjectAnimator;
yueg81a77ff2017-12-05 10:29:03 -080023import android.animation.ValueAnimator;
Eric Erfanian938468d2017-10-24 14:05:52 -070024import android.annotation.SuppressLint;
25import android.app.PendingIntent.CanceledException;
26import android.content.Context;
27import android.content.Intent;
28import android.graphics.PixelFormat;
yueg0b4755c2017-12-18 10:01:03 -080029import android.graphics.Rect;
Eric Erfanian938468d2017-10-24 14:05:52 -070030import android.graphics.drawable.Animatable;
31import android.graphics.drawable.Drawable;
Eric Erfanian938468d2017-10-24 14:05:52 -070032import android.net.Uri;
33import android.os.Handler;
34import android.provider.Settings;
Eric Erfanian938468d2017-10-24 14:05:52 -070035import android.support.annotation.IntDef;
36import android.support.annotation.NonNull;
37import android.support.annotation.Nullable;
38import android.support.annotation.VisibleForTesting;
39import android.support.v4.graphics.ColorUtils;
Eric Erfanian938468d2017-10-24 14:05:52 -070040import android.support.v4.os.BuildCompat;
Eric Erfanian938468d2017-10-24 14:05:52 -070041import android.support.v4.view.animation.LinearOutSlowInInterpolator;
42import android.transition.TransitionManager;
43import android.transition.TransitionValues;
44import android.view.ContextThemeWrapper;
45import android.view.Gravity;
46import android.view.LayoutInflater;
47import android.view.MotionEvent;
48import android.view.View;
yuega235e132017-12-13 14:13:57 -080049import android.view.View.AccessibilityDelegate;
Eric Erfanian938468d2017-10-24 14:05:52 -070050import android.view.ViewGroup;
51import android.view.ViewPropertyAnimator;
52import android.view.ViewTreeObserver.OnPreDrawListener;
53import android.view.WindowManager;
54import android.view.WindowManager.LayoutParams;
yuega235e132017-12-13 14:13:57 -080055import android.view.accessibility.AccessibilityNodeInfo;
56import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction;
yueg0b4755c2017-12-18 10:01:03 -080057import android.view.animation.AccelerateDecelerateInterpolator;
Eric Erfanian938468d2017-10-24 14:05:52 -070058import android.view.animation.AnticipateInterpolator;
59import android.view.animation.OvershootInterpolator;
60import android.widget.ImageView;
61import android.widget.TextView;
62import android.widget.ViewAnimator;
yueg81a77ff2017-12-05 10:29:03 -080063import com.android.dialer.common.LogUtil;
64import com.android.dialer.logging.DialerImpression;
65import com.android.dialer.logging.Logger;
yuega5a08d82017-10-31 14:11:53 -070066import com.android.dialer.util.DrawableConverter;
yueg81a77ff2017-12-05 10:29:03 -080067import com.android.incallui.call.CallList;
68import com.android.incallui.call.DialerCall;
Eric Erfanian938468d2017-10-24 14:05:52 -070069import com.android.newbubble.NewBubbleInfo.Action;
70import java.lang.annotation.Retention;
71import java.lang.annotation.RetentionPolicy;
72import java.util.List;
73
74/**
75 * Creates and manages a bubble window from information in a {@link NewBubbleInfo}. Before creating,
76 * be sure to check whether bubbles may be shown using {@link #canShowBubbles(Context)} and request
77 * permission if necessary ({@link #getRequestPermissionIntent(Context)} is provided for
78 * convenience)
79 */
80public class NewBubble {
81 // This class has some odd behavior that is not immediately obvious in order to avoid jank when
82 // resizing. See http://go/bubble-resize for details.
83
84 // How long text should show after showText(CharSequence) is called
85 private static final int SHOW_TEXT_DURATION_MILLIS = 3000;
86 // How long the new window should show before destroying the old one during resize operations.
87 // This ensures the new window has had time to draw first.
88 private static final int WINDOW_REDRAW_DELAY_MILLIS = 50;
89
yueg0b4755c2017-12-18 10:01:03 -080090 private static final int EXPAND_AND_COLLAPSE_ANIMATION_DURATION = 200;
91
Eric Erfanian938468d2017-10-24 14:05:52 -070092 private static Boolean canShowBubblesForTesting = null;
93
yueg0b4755c2017-12-18 10:01:03 -080094 private final AccelerateDecelerateInterpolator accelerateDecelerateInterpolator =
95 new AccelerateDecelerateInterpolator();
96
Eric Erfanian938468d2017-10-24 14:05:52 -070097 private final Context context;
98 private final WindowManager windowManager;
99
100 private final Handler handler;
101 private LayoutParams windowParams;
102
103 // Initialized in factory method
104 @SuppressWarnings("NullableProblems")
105 @NonNull
106 private NewBubbleInfo currentInfo;
107
108 @Visibility private int visibility;
109 private boolean expanded;
110 private boolean textShowing;
111 private boolean hideAfterText;
112 private CharSequence textAfterShow;
113 private int collapseEndAction;
114
yueg81a77ff2017-12-05 10:29:03 -0800115 ViewHolder viewHolder;
Eric Erfanian938468d2017-10-24 14:05:52 -0700116 private ViewPropertyAnimator collapseAnimation;
117 private Integer overrideGravity;
yueg87111362017-12-08 12:45:50 -0800118 @VisibleForTesting AnimatorSet exitAnimatorSet;
Eric Erfanian938468d2017-10-24 14:05:52 -0700119
yueg87111362017-12-08 12:45:50 -0800120 private final int primaryIconMoveDistance;
121 private final int leftBoundary;
yueg81a77ff2017-12-05 10:29:03 -0800122 private int savedYPosition = -1;
123
Eric Erfanian938468d2017-10-24 14:05:52 -0700124 private final Runnable collapseRunnable =
125 new Runnable() {
126 @Override
127 public void run() {
128 textShowing = false;
129 if (hideAfterText) {
130 // Always reset here since text shouldn't keep showing.
131 hideAndReset();
132 } else {
yueg81a77ff2017-12-05 10:29:03 -0800133 viewHolder.getPrimaryButton().setDisplayedChild(ViewHolder.CHILD_INDEX_AVATAR_AND_ICON);
Eric Erfanian938468d2017-10-24 14:05:52 -0700134 }
135 }
136 };
137
Eric Erfanian938468d2017-10-24 14:05:52 -0700138 /** Type of action after bubble collapse */
139 @Retention(RetentionPolicy.SOURCE)
140 @IntDef({CollapseEnd.NOTHING, CollapseEnd.HIDE})
141 @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
142 public @interface CollapseEnd {
143 int NOTHING = 0;
144 int HIDE = 1;
145 }
146
147 @Retention(RetentionPolicy.SOURCE)
148 @IntDef({Visibility.ENTERING, Visibility.SHOWING, Visibility.EXITING, Visibility.HIDDEN})
149 private @interface Visibility {
150 int HIDDEN = 0;
151 int ENTERING = 1;
152 int SHOWING = 2;
153 int EXITING = 3;
154 }
155
156 /** Indicate bubble expansion state. */
157 @Retention(RetentionPolicy.SOURCE)
158 @IntDef({ExpansionState.START_EXPANDING, ExpansionState.START_COLLAPSING})
159 public @interface ExpansionState {
160 // TODO(yueg): add more states when needed
161 int START_EXPANDING = 0;
162 int START_COLLAPSING = 1;
163 }
164
165 /**
166 * Determines whether bubbles can be shown based on permissions obtained. This should be checked
167 * before attempting to create a Bubble.
168 *
169 * @return true iff bubbles are able to be shown.
170 * @see Settings#canDrawOverlays(Context)
171 */
172 public static boolean canShowBubbles(@NonNull Context context) {
173 return canShowBubblesForTesting != null
174 ? canShowBubblesForTesting
175 : Settings.canDrawOverlays(context);
176 }
177
178 @VisibleForTesting(otherwise = VisibleForTesting.NONE)
179 public static void setCanShowBubblesForTesting(boolean canShowBubbles) {
180 canShowBubblesForTesting = canShowBubbles;
181 }
182
183 /** Returns an Intent to request permission to show overlays */
184 @NonNull
185 public static Intent getRequestPermissionIntent(@NonNull Context context) {
186 return new Intent(
187 Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
188 Uri.fromParts("package", context.getPackageName(), null));
189 }
190
191 /** Creates instances of Bubble. The default implementation just calls the constructor. */
192 @VisibleForTesting
193 public interface BubbleFactory {
194 NewBubble createBubble(@NonNull Context context, @NonNull Handler handler);
195 }
196
197 private static BubbleFactory bubbleFactory = NewBubble::new;
198
199 public static NewBubble createBubble(@NonNull Context context, @NonNull NewBubbleInfo info) {
200 NewBubble bubble = bubbleFactory.createBubble(context, new Handler());
201 bubble.setBubbleInfo(info);
202 return bubble;
203 }
204
205 @VisibleForTesting
206 public static void setBubbleFactory(@NonNull BubbleFactory bubbleFactory) {
207 NewBubble.bubbleFactory = bubbleFactory;
208 }
209
210 @VisibleForTesting
211 public static void resetBubbleFactory() {
212 NewBubble.bubbleFactory = NewBubble::new;
213 }
214
215 @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
216 NewBubble(@NonNull Context context, @NonNull Handler handler) {
217 context = new ContextThemeWrapper(context, R.style.Theme_AppCompat);
218 this.context = context;
219 this.handler = handler;
220 windowManager = context.getSystemService(WindowManager.class);
221
222 viewHolder = new ViewHolder(context);
yueg81a77ff2017-12-05 10:29:03 -0800223
224 leftBoundary =
225 context.getResources().getDimensionPixelOffset(R.dimen.bubble_off_screen_size_horizontal)
226 - context
227 .getResources()
228 .getDimensionPixelSize(R.dimen.bubble_shadow_padding_size_horizontal);
yueg87111362017-12-08 12:45:50 -0800229 primaryIconMoveDistance =
230 context.getResources().getDimensionPixelSize(R.dimen.bubble_size)
231 - context.getResources().getDimensionPixelSize(R.dimen.bubble_small_icon_size);
Eric Erfanian938468d2017-10-24 14:05:52 -0700232 }
233
234 /** Expands the main bubble menu. */
235 public void expand(boolean isUserAction) {
yueg81a77ff2017-12-05 10:29:03 -0800236 if (isUserAction) {
237 logBasicOrCallImpression(DialerImpression.Type.BUBBLE_PRIMARY_BUTTON_EXPAND);
Eric Erfanian938468d2017-10-24 14:05:52 -0700238 }
yuega235e132017-12-13 14:13:57 -0800239 setPrimaryButtonAccessibilityAction(
240 context.getString(R.string.a11y_bubble_primary_button_collapse_action));
yueg0b4755c2017-12-18 10:01:03 -0800241
yueg81a77ff2017-12-05 10:29:03 -0800242 viewHolder.setDrawerVisibility(View.INVISIBLE);
yueg0b4755c2017-12-18 10:01:03 -0800243 viewHolder.getArrow().setVisibility(View.INVISIBLE);
244 // No click during animation to avoid jank.
245 viewHolder.setPrimaryButtonClickable(false);
246
Eric Erfanian938468d2017-10-24 14:05:52 -0700247 View expandedView = viewHolder.getExpandedView();
248 expandedView
249 .getViewTreeObserver()
250 .addOnPreDrawListener(
251 new OnPreDrawListener() {
252 @Override
253 public boolean onPreDraw() {
yueg81a77ff2017-12-05 10:29:03 -0800254 // Move the whole bubble up so that expanded view is still in screen
255 int moveUpDistance = viewHolder.getMoveUpDistance();
256 if (moveUpDistance != 0) {
257 savedYPosition = windowParams.y;
258 }
259
yueg0b4755c2017-12-18 10:01:03 -0800260 // Animation 1: animate x-move and y-move (if needed) together
yueg81a77ff2017-12-05 10:29:03 -0800261 int deltaX =
262 (int) viewHolder.getRoot().findViewById(R.id.bubble_primary_container).getX();
yueg0b4755c2017-12-18 10:01:03 -0800263 float k = -(float) moveUpDistance / deltaX;
yueg81a77ff2017-12-05 10:29:03 -0800264 if (isDrawingFromRight()) {
265 deltaX = -deltaX;
266 }
yueg0b4755c2017-12-18 10:01:03 -0800267 ValueAnimator xValueAnimator =
268 createBubbleMoveAnimator(
269 windowParams.x - deltaX, windowParams.x, windowParams.y, k);
yueg81a77ff2017-12-05 10:29:03 -0800270
yueg0b4755c2017-12-18 10:01:03 -0800271 // Show expanded view
272 expandedView.setVisibility(View.VISIBLE);
yueg81a77ff2017-12-05 10:29:03 -0800273
yueg0b4755c2017-12-18 10:01:03 -0800274 // Animator 2: reveal expanded view from top left or top right
275 View expandedMenu = viewHolder.getRoot().findViewById(R.id.bubble_expanded_menu);
276 ValueAnimator revealAnim =
277 createOpenCloseOutlineProvider(expandedMenu)
278 .createRevealAnimator(expandedMenu, false);
279 revealAnim.setInterpolator(accelerateDecelerateInterpolator);
280
281 // Animator 3: expanded view fade in
282 Animator fadeIn = ObjectAnimator.ofFloat(expandedView, "alpha", 0, 1);
283 fadeIn.setInterpolator(accelerateDecelerateInterpolator);
284
285 // Play all animation together
286 AnimatorSet expandAnimatorSet = new AnimatorSet();
287 expandAnimatorSet.playTogether(revealAnim, fadeIn, xValueAnimator);
288 expandAnimatorSet.setDuration(EXPAND_AND_COLLAPSE_ANIMATION_DURATION);
289 expandAnimatorSet.addListener(
yueg87111362017-12-08 12:45:50 -0800290 new AnimatorListenerAdapter() {
yueg81a77ff2017-12-05 10:29:03 -0800291 @Override
292 public void onAnimationEnd(Animator animation) {
yueg0b4755c2017-12-18 10:01:03 -0800293 // Show arrow after animation
294 viewHolder.getArrow().setVisibility(View.VISIBLE);
295 // Safe to click primary button now
296 viewHolder.setPrimaryButtonClickable(true);
yueg81a77ff2017-12-05 10:29:03 -0800297 }
yueg81a77ff2017-12-05 10:29:03 -0800298 });
yueg0b4755c2017-12-18 10:01:03 -0800299 expandAnimatorSet.start();
yueg81a77ff2017-12-05 10:29:03 -0800300
Eric Erfanian938468d2017-10-24 14:05:52 -0700301 expandedView.getViewTreeObserver().removeOnPreDrawListener(this);
Eric Erfanian938468d2017-10-24 14:05:52 -0700302 return false;
303 }
304 });
305 setFocused(true);
306 expanded = true;
307 }
308
yueg81a77ff2017-12-05 10:29:03 -0800309 @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
310 public void startCollapse(
311 @CollapseEnd int endAction, boolean isUserAction, boolean shouldRecoverYPosition) {
312 View expandedView = viewHolder.getExpandedView();
313 if (expandedView.getVisibility() != View.VISIBLE || collapseAnimation != null) {
314 // Drawer is already collapsed or animation is running.
315 return;
316 }
317
318 overrideGravity = isDrawingFromRight() ? Gravity.RIGHT : Gravity.LEFT;
319 setFocused(false);
320
321 if (collapseEndAction == CollapseEnd.NOTHING) {
322 collapseEndAction = endAction;
323 }
324 if (isUserAction && collapseEndAction == CollapseEnd.NOTHING) {
325 logBasicOrCallImpression(DialerImpression.Type.BUBBLE_COLLAPSE_BY_USER);
326 }
yueg0b4755c2017-12-18 10:01:03 -0800327
yuega235e132017-12-13 14:13:57 -0800328 setPrimaryButtonAccessibilityAction(
329 context.getString(R.string.a11y_bubble_primary_button_expand_action));
yueg0b4755c2017-12-18 10:01:03 -0800330
331 // Hide arrow before animation
332 viewHolder.getArrow().setVisibility(View.INVISIBLE);
333
334 // No click during animation to avoid jank.
335 viewHolder.setPrimaryButtonClickable(false);
336
337 // Calculate animation values
338 int deltaX = (int) viewHolder.getRoot().findViewById(R.id.bubble_primary_container).getX();
339 float k =
340 (savedYPosition != -1 && shouldRecoverYPosition)
341 ? (savedYPosition - windowParams.y) / (float) deltaX
342 : 0;
343 // The position is not useful after collapse
344 savedYPosition = -1;
345
346 // Animation 1: animate x-move and y-move (if needed) together
347 ValueAnimator xValueAnimator =
348 createBubbleMoveAnimator(windowParams.x, windowParams.x - deltaX, windowParams.y, k);
349
350 // Animator 2: hide expanded view to top left or top right
351 View expandedMenu = viewHolder.getRoot().findViewById(R.id.bubble_expanded_menu);
352 ValueAnimator revealAnim =
353 createOpenCloseOutlineProvider(expandedMenu).createRevealAnimator(expandedMenu, true);
354 revealAnim.setInterpolator(accelerateDecelerateInterpolator);
355
356 // Animator 3: expanded view fade out
357 Animator fadeOut = ObjectAnimator.ofFloat(expandedView, "alpha", 1, 0);
358 fadeOut.setInterpolator(accelerateDecelerateInterpolator);
359
360 // Play all animation together
361 AnimatorSet collapseAnimatorSet = new AnimatorSet();
362 collapseAnimatorSet.setDuration(EXPAND_AND_COLLAPSE_ANIMATION_DURATION);
363 collapseAnimatorSet.playTogether(revealAnim, fadeOut, xValueAnimator);
364 collapseAnimatorSet.addListener(
365 new AnimatorListenerAdapter() {
366 @Override
367 public void onAnimationEnd(Animator animation) {
368 collapseAnimation = null;
369 expanded = false;
370
371 if (textShowing) {
372 // Will do resize once the text is done.
373 return;
374 }
375
376 // If this collapse was to come before a hide, do it now.
377 if (collapseEndAction == CollapseEnd.HIDE) {
378 hide();
379 }
380 collapseEndAction = CollapseEnd.NOTHING;
381
382 // If collapse on the right side, the primary button move left a bit after drawer
383 // visibility becoming GONE. To avoid it, we create a new ViewHolder.
384 // It also set primary button clickable back to true, so no need to reset manually.
385 replaceViewHolder();
386
387 // Resume normal gravity after any resizing is done.
388 handler.postDelayed(
yueg81a77ff2017-12-05 10:29:03 -0800389 () -> {
yueg0b4755c2017-12-18 10:01:03 -0800390 overrideGravity = null;
391 if (!viewHolder.isMoving()) {
392 viewHolder.undoGravityOverride();
yueg81a77ff2017-12-05 10:29:03 -0800393 }
yueg0b4755c2017-12-18 10:01:03 -0800394 },
395 // Need to wait twice as long for resize and layout
396 WINDOW_REDRAW_DELAY_MILLIS * 2);
397 }
398 });
399 collapseAnimatorSet.start();
yueg81a77ff2017-12-05 10:29:03 -0800400 }
401
Eric Erfanian938468d2017-10-24 14:05:52 -0700402 /**
403 * Make the bubble visible. Will show a short entrance animation as it enters. If the bubble is
404 * already showing this method does nothing.
405 */
406 public void show() {
407 if (collapseEndAction == CollapseEnd.HIDE) {
408 // If show() was called while collapsing, make sure we don't hide after.
409 collapseEndAction = CollapseEnd.NOTHING;
410 }
411 if (visibility == Visibility.SHOWING || visibility == Visibility.ENTERING) {
412 return;
413 }
414
415 hideAfterText = false;
416
417 if (windowParams == null) {
418 // Apps targeting O+ must use TYPE_APPLICATION_OVERLAY, which is not available prior to O.
419 @SuppressWarnings("deprecation")
420 @SuppressLint("InlinedApi")
421 int type =
422 BuildCompat.isAtLeastO()
423 ? LayoutParams.TYPE_APPLICATION_OVERLAY
424 : LayoutParams.TYPE_PHONE;
425
426 windowParams =
427 new LayoutParams(
428 type,
429 LayoutParams.FLAG_NOT_TOUCH_MODAL
430 | LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
431 | LayoutParams.FLAG_NOT_FOCUSABLE
432 | LayoutParams.FLAG_LAYOUT_NO_LIMITS,
433 PixelFormat.TRANSLUCENT);
434 windowParams.gravity = Gravity.TOP | Gravity.LEFT;
yueg81a77ff2017-12-05 10:29:03 -0800435 windowParams.x = leftBoundary;
Eric Erfanian938468d2017-10-24 14:05:52 -0700436 windowParams.y = currentInfo.getStartingYPosition();
437 windowParams.height = LayoutParams.WRAP_CONTENT;
438 windowParams.width = LayoutParams.WRAP_CONTENT;
439 }
440
yueg87111362017-12-08 12:45:50 -0800441 if (exitAnimatorSet != null) {
442 exitAnimatorSet.removeAllListeners();
443 exitAnimatorSet.cancel();
444 exitAnimatorSet = null;
Eric Erfanian938468d2017-10-24 14:05:52 -0700445 } else {
446 windowManager.addView(viewHolder.getRoot(), windowParams);
yueg87111362017-12-08 12:45:50 -0800447 viewHolder.getPrimaryButton().setVisibility(View.VISIBLE);
Eric Erfanian938468d2017-10-24 14:05:52 -0700448 viewHolder.getPrimaryButton().setScaleX(0);
449 viewHolder.getPrimaryButton().setScaleY(0);
yueg87111362017-12-08 12:45:50 -0800450 viewHolder.getPrimaryAvatar().setAlpha(0f);
451 viewHolder.getPrimaryIcon().setAlpha(0f);
Eric Erfanian938468d2017-10-24 14:05:52 -0700452 }
453
454 viewHolder.setChildClickable(true);
455 visibility = Visibility.ENTERING;
yueg87111362017-12-08 12:45:50 -0800456
yuega235e132017-12-13 14:13:57 -0800457 setPrimaryButtonAccessibilityAction(
458 context.getString(R.string.a11y_bubble_primary_button_expand_action));
459
yueg87111362017-12-08 12:45:50 -0800460 // Show bubble animation: scale the whole bubble to 1, and change avatar+icon's alpha to 1
461 ObjectAnimator scaleXAnimator =
462 ObjectAnimator.ofFloat(viewHolder.getPrimaryButton(), "scaleX", 1);
463 ObjectAnimator scaleYAnimator =
464 ObjectAnimator.ofFloat(viewHolder.getPrimaryButton(), "scaleY", 1);
465 ObjectAnimator avatarAlphaAnimator =
466 ObjectAnimator.ofFloat(viewHolder.getPrimaryAvatar(), "alpha", 1);
467 ObjectAnimator iconAlphaAnimator =
468 ObjectAnimator.ofFloat(viewHolder.getPrimaryIcon(), "alpha", 1);
469 AnimatorSet enterAnimatorSet = new AnimatorSet();
470 enterAnimatorSet.playTogether(
471 scaleXAnimator, scaleYAnimator, avatarAlphaAnimator, iconAlphaAnimator);
472 enterAnimatorSet.setInterpolator(new OvershootInterpolator());
473 enterAnimatorSet.addListener(
474 new AnimatorListenerAdapter() {
475 @Override
476 public void onAnimationEnd(Animator animation) {
477 visibility = Visibility.SHOWING;
478 // Show the queued up text, if available.
479 if (textAfterShow != null) {
480 showText(textAfterShow);
481 textAfterShow = null;
482 }
483 }
484 });
485 enterAnimatorSet.start();
Eric Erfanian938468d2017-10-24 14:05:52 -0700486
487 updatePrimaryIconAnimation();
488 }
489
490 /** Hide the bubble. */
491 public void hide() {
492 if (hideAfterText) {
493 // hideAndReset() will be called after showing text, do nothing here.
494 return;
495 }
496 hideHelper(this::defaultAfterHidingAnimation);
497 }
498
499 /** Hide the bubble and reset {@viewHolder} to initial state */
500 public void hideAndReset() {
501 hideHelper(
502 () -> {
503 defaultAfterHidingAnimation();
504 reset();
505 });
506 }
507
508 /** Returns whether the bubble is currently visible */
509 public boolean isVisible() {
510 return visibility == Visibility.SHOWING
511 || visibility == Visibility.ENTERING
512 || visibility == Visibility.EXITING;
513 }
514
515 /**
516 * Set the info for this Bubble to display
517 *
518 * @param bubbleInfo the BubbleInfo to display in this Bubble.
519 */
520 public void setBubbleInfo(@NonNull NewBubbleInfo bubbleInfo) {
521 currentInfo = bubbleInfo;
522 update();
523 }
524
525 /**
526 * Update the state and behavior of actions.
527 *
528 * @param actions the new state of the bubble's actions
529 */
530 public void updateActions(@NonNull List<Action> actions) {
531 currentInfo = NewBubbleInfo.from(currentInfo).setActions(actions).build();
532 updateButtonStates();
533 }
534
yuega5a08d82017-10-31 14:11:53 -0700535 /**
536 * Update the avatar from photo.
537 *
538 * @param avatar the new photo avatar in the bubble's primary button
539 */
540 public void updatePhotoAvatar(@NonNull Drawable avatar) {
541 // Make it round
542 int bubbleSize = context.getResources().getDimensionPixelSize(R.dimen.bubble_size);
543 Drawable roundAvatar =
544 DrawableConverter.getRoundedDrawable(context, avatar, bubbleSize, bubbleSize);
545
546 updateAvatar(roundAvatar);
547 }
548
549 /**
550 * Update the avatar.
551 *
552 * @param avatar the new avatar in the bubble's primary button
553 */
554 public void updateAvatar(@NonNull Drawable avatar) {
555 if (!avatar.equals(currentInfo.getAvatar())) {
556 currentInfo = NewBubbleInfo.from(currentInfo).setAvatar(avatar).build();
557 viewHolder.getPrimaryAvatar().setImageDrawable(currentInfo.getAvatar());
558 }
559 }
560
Eric Erfanian938468d2017-10-24 14:05:52 -0700561 /** Returns the currently displayed NewBubbleInfo */
562 public NewBubbleInfo getBubbleInfo() {
563 return currentInfo;
564 }
565
566 /**
567 * Display text in the main bubble. The bubble's drawer is not expandable while text is showing,
568 * and the drawer will be closed if already open.
569 *
570 * @param text the text to display to the user
571 */
572 public void showText(@NonNull CharSequence text) {
573 textShowing = true;
574 if (expanded) {
yueg81a77ff2017-12-05 10:29:03 -0800575 startCollapse(
576 CollapseEnd.NOTHING, false /* isUserAction */, false /* shouldRecoverYPosition */);
Eric Erfanian938468d2017-10-24 14:05:52 -0700577 doShowText(text);
578 } else {
579 // Need to transition from old bounds to new bounds manually
580 NewChangeOnScreenBounds transition = new NewChangeOnScreenBounds();
581 // Prepare and capture start values
582 TransitionValues startValues = new TransitionValues();
583 startValues.view = viewHolder.getPrimaryButton();
584 transition.addTarget(startValues.view);
585 transition.captureStartValues(startValues);
586
587 // If our view is not laid out yet, postpone showing the text.
588 if (startValues.values.isEmpty()) {
589 textAfterShow = text;
590 return;
591 }
592
yueg81a77ff2017-12-05 10:29:03 -0800593 doShowText(text);
594 // Hide the text so we can animate it in
595 viewHolder.getPrimaryText().setAlpha(0);
Eric Erfanian938468d2017-10-24 14:05:52 -0700596
yueg81a77ff2017-12-05 10:29:03 -0800597 ViewAnimator primaryButton = viewHolder.getPrimaryButton();
598 // Cancel the automatic transition scheduled in doShowText
599 TransitionManager.endTransitions((ViewGroup) primaryButton.getParent());
600 primaryButton
601 .getViewTreeObserver()
602 .addOnPreDrawListener(
603 new OnPreDrawListener() {
604 @Override
605 public boolean onPreDraw() {
606 primaryButton.getViewTreeObserver().removeOnPreDrawListener(this);
Eric Erfanian938468d2017-10-24 14:05:52 -0700607
yueg81a77ff2017-12-05 10:29:03 -0800608 // Prepare and capture end values, always use the size of primaryText since
609 // its invisibility makes primaryButton smaller than expected
610 TransitionValues endValues = new TransitionValues();
611 endValues.values.put(
612 NewChangeOnScreenBounds.PROPNAME_WIDTH,
613 viewHolder.getPrimaryText().getWidth());
614 endValues.values.put(
615 NewChangeOnScreenBounds.PROPNAME_HEIGHT,
616 viewHolder.getPrimaryText().getHeight());
617 endValues.view = primaryButton;
618 transition.addTarget(endValues.view);
619 transition.captureEndValues(endValues);
Eric Erfanian938468d2017-10-24 14:05:52 -0700620
yueg81a77ff2017-12-05 10:29:03 -0800621 // animate the primary button bounds change
622 Animator bounds =
623 transition.createAnimator(primaryButton, startValues, endValues);
Eric Erfanian938468d2017-10-24 14:05:52 -0700624
yueg81a77ff2017-12-05 10:29:03 -0800625 // Animate the text in
626 Animator alpha =
627 ObjectAnimator.ofFloat(viewHolder.getPrimaryText(), View.ALPHA, 1f);
Eric Erfanian938468d2017-10-24 14:05:52 -0700628
yueg81a77ff2017-12-05 10:29:03 -0800629 AnimatorSet set = new AnimatorSet();
630 set.play(bounds).before(alpha);
631 set.start();
632 return false;
633 }
634 });
Eric Erfanian938468d2017-10-24 14:05:52 -0700635 }
636 handler.removeCallbacks(collapseRunnable);
637 handler.postDelayed(collapseRunnable, SHOW_TEXT_DURATION_MILLIS);
638 }
639
Eric Erfanian938468d2017-10-24 14:05:52 -0700640 @Nullable
641 Integer getGravityOverride() {
642 return overrideGravity;
643 }
644
645 void onMoveStart() {
yueg81a77ff2017-12-05 10:29:03 -0800646 if (viewHolder.getExpandedView().getVisibility() == View.VISIBLE) {
647 viewHolder.setDrawerVisibility(View.INVISIBLE);
648 }
649 expanded = false;
650 savedYPosition = -1;
651
Eric Erfanian938468d2017-10-24 14:05:52 -0700652 viewHolder
653 .getPrimaryButton()
654 .animate()
655 .translationZ(
yuegc6deafc2017-11-06 16:42:13 -0800656 context
657 .getResources()
658 .getDimensionPixelOffset(R.dimen.bubble_dragging_elevation_change));
Eric Erfanian938468d2017-10-24 14:05:52 -0700659 }
660
661 void onMoveFinish() {
662 viewHolder.getPrimaryButton().animate().translationZ(0);
Eric Erfanian938468d2017-10-24 14:05:52 -0700663 }
664
665 void primaryButtonClick() {
666 if (textShowing || currentInfo.getActions().isEmpty()) {
667 return;
668 }
669 if (expanded) {
yueg81a77ff2017-12-05 10:29:03 -0800670 startCollapse(
671 CollapseEnd.NOTHING, true /* isUserAction */, true /* shouldRecoverYPosition */);
Eric Erfanian938468d2017-10-24 14:05:52 -0700672 } else {
673 expand(true);
674 }
675 }
676
yueg81a77ff2017-12-05 10:29:03 -0800677 void onLeftRightSwitch(boolean onRight) {
yueg87111362017-12-08 12:45:50 -0800678 // Move primary icon to the other side so it's not partially hiden
yueg81a77ff2017-12-05 10:29:03 -0800679 View primaryIcon = viewHolder.getPrimaryIcon();
yueg87111362017-12-08 12:45:50 -0800680 primaryIcon.animate().translationX(onRight ? -primaryIconMoveDistance : 0).start();
yueg81a77ff2017-12-05 10:29:03 -0800681 }
682
Eric Erfanian938468d2017-10-24 14:05:52 -0700683 LayoutParams getWindowParams() {
684 return windowParams;
685 }
686
687 View getRootView() {
688 return viewHolder.getRoot();
689 }
690
691 /**
692 * Hide the bubble if visible. Will run a short exit animation and before hiding, and {@code
693 * afterHiding} after hiding. If the bubble is currently showing text, will hide after the text is
694 * done displaying. If the bubble is not visible this method does nothing.
695 */
yueg87111362017-12-08 12:45:50 -0800696 @VisibleForTesting
697 void hideHelper(Runnable afterHiding) {
Eric Erfanian938468d2017-10-24 14:05:52 -0700698 if (visibility == Visibility.HIDDEN || visibility == Visibility.EXITING) {
699 return;
700 }
701
702 // Make bubble non clickable to prevent further buggy actions
703 viewHolder.setChildClickable(false);
704
705 if (textShowing) {
706 hideAfterText = true;
707 return;
708 }
709
710 if (collapseAnimation != null) {
711 collapseEndAction = CollapseEnd.HIDE;
712 return;
713 }
714
715 if (expanded) {
yueg81a77ff2017-12-05 10:29:03 -0800716 startCollapse(CollapseEnd.HIDE, false /* isUserAction */, false /* shouldRecoverYPosition */);
Eric Erfanian938468d2017-10-24 14:05:52 -0700717 return;
718 }
719
720 visibility = Visibility.EXITING;
yueg87111362017-12-08 12:45:50 -0800721
722 // Hide bubble animation: scale the whole bubble to 0, and change avatar+icon's alpha to 0
723 ObjectAnimator scaleXAnimator =
724 ObjectAnimator.ofFloat(viewHolder.getPrimaryButton(), "scaleX", 0);
725 ObjectAnimator scaleYAnimator =
726 ObjectAnimator.ofFloat(viewHolder.getPrimaryButton(), "scaleY", 0);
727 ObjectAnimator avatarAlphaAnimator =
728 ObjectAnimator.ofFloat(viewHolder.getPrimaryAvatar(), "alpha", 0);
729 ObjectAnimator iconAlphaAnimator =
730 ObjectAnimator.ofFloat(viewHolder.getPrimaryIcon(), "alpha", 0);
731 exitAnimatorSet = new AnimatorSet();
732 exitAnimatorSet.playTogether(
733 scaleXAnimator, scaleYAnimator, avatarAlphaAnimator, iconAlphaAnimator);
734 exitAnimatorSet.setInterpolator(new AnticipateInterpolator());
735 exitAnimatorSet.addListener(
736 new AnimatorListenerAdapter() {
737 @Override
yuega235e132017-12-13 14:13:57 -0800738 public void onAnimationStart(Animator animation) {
739 viewHolder.getPrimaryButton().setAccessibilityDelegate(null);
740 }
741
742 @Override
yueg87111362017-12-08 12:45:50 -0800743 public void onAnimationEnd(Animator animation) {
744 afterHiding.run();
745 }
746 });
747 exitAnimatorSet.start();
Eric Erfanian938468d2017-10-24 14:05:52 -0700748 }
749
750 private void reset() {
751 viewHolder = new ViewHolder(viewHolder.getRoot().getContext());
752 update();
753 }
754
755 private void update() {
yuega5a08d82017-10-31 14:11:53 -0700756 // Whole primary button background
yueg84ac49b2017-11-01 16:22:28 -0700757 Drawable backgroundCirle =
758 context.getResources().getDrawable(R.drawable.bubble_shape_circle, context.getTheme());
Eric Erfanian938468d2017-10-24 14:05:52 -0700759 int primaryTint =
760 ColorUtils.compositeColors(
761 context.getColor(R.color.bubble_primary_background_darken),
762 currentInfo.getPrimaryColor());
yueg84ac49b2017-11-01 16:22:28 -0700763 backgroundCirle.mutate().setTint(primaryTint);
764 viewHolder.getPrimaryButton().setBackground(backgroundCirle);
Eric Erfanian938468d2017-10-24 14:05:52 -0700765
yuega5a08d82017-10-31 14:11:53 -0700766 // Small icon
yueg84ac49b2017-11-01 16:22:28 -0700767 Drawable smallIconBackgroundCircle =
768 context
769 .getResources()
770 .getDrawable(R.drawable.bubble_shape_circle_small, context.getTheme());
771 smallIconBackgroundCircle.setTint(context.getColor(R.color.bubble_button_color_blue));
772 viewHolder.getPrimaryIcon().setBackground(smallIconBackgroundCircle);
Eric Erfanian938468d2017-10-24 14:05:52 -0700773 viewHolder.getPrimaryIcon().setImageIcon(currentInfo.getPrimaryIcon());
yuega5a08d82017-10-31 14:11:53 -0700774 viewHolder.getPrimaryAvatar().setImageDrawable(currentInfo.getAvatar());
Eric Erfanian938468d2017-10-24 14:05:52 -0700775
yuega5a08d82017-10-31 14:11:53 -0700776 updatePrimaryIconAnimation();
Eric Erfanian938468d2017-10-24 14:05:52 -0700777 updateButtonStates();
778 }
779
780 private void updatePrimaryIconAnimation() {
781 Drawable drawable = viewHolder.getPrimaryIcon().getDrawable();
782 if (drawable instanceof Animatable) {
783 if (isVisible()) {
784 ((Animatable) drawable).start();
785 } else {
786 ((Animatable) drawable).stop();
787 }
788 }
789 }
790
791 private void updateButtonStates() {
yueg84ac49b2017-11-01 16:22:28 -0700792 configureButton(currentInfo.getActions().get(0), viewHolder.getFullScreenButton());
793 configureButton(currentInfo.getActions().get(1), viewHolder.getMuteButton());
794 configureButton(currentInfo.getActions().get(2), viewHolder.getAudioRouteButton());
795 configureButton(currentInfo.getActions().get(3), viewHolder.getEndCallButton());
Eric Erfanian938468d2017-10-24 14:05:52 -0700796 }
797
yueg87111362017-12-08 12:45:50 -0800798 @VisibleForTesting
799 void doShowText(@NonNull CharSequence text) {
Eric Erfanian938468d2017-10-24 14:05:52 -0700800 TransitionManager.beginDelayedTransition((ViewGroup) viewHolder.getPrimaryButton().getParent());
801 viewHolder.getPrimaryText().setText(text);
802 viewHolder.getPrimaryButton().setDisplayedChild(ViewHolder.CHILD_INDEX_TEXT);
803 }
804
yueg84ac49b2017-11-01 16:22:28 -0700805 private void configureButton(Action action, NewCheckableButton button) {
806 button.setCompoundDrawablesWithIntrinsicBounds(action.getIconDrawable(), null, null, null);
Eric Erfanian938468d2017-10-24 14:05:52 -0700807 button.setChecked(action.isChecked());
808 button.setEnabled(action.isEnabled());
yueg84ac49b2017-11-01 16:22:28 -0700809 button.setText(action.getName());
yuega235e132017-12-13 14:13:57 -0800810 button.setContentDescription(action.getName());
Eric Erfanian938468d2017-10-24 14:05:52 -0700811 button.setOnClickListener(v -> doAction(action));
812 }
813
814 private void doAction(Action action) {
815 try {
816 action.getIntent().send();
817 } catch (CanceledException e) {
818 throw new RuntimeException(e);
819 }
820 }
821
yueg81a77ff2017-12-05 10:29:03 -0800822 /**
823 * Create a new ViewHolder object to replace the old one.It only happens when not moving and
824 * collapsed.
825 */
826 void replaceViewHolder() {
827 LogUtil.enterBlock("NewBubble.replaceViewHolder");
Eric Erfanian938468d2017-10-24 14:05:52 -0700828 ViewHolder oldViewHolder = viewHolder;
Eric Erfanian938468d2017-10-24 14:05:52 -0700829
yueg81a77ff2017-12-05 10:29:03 -0800830 // Create a new ViewHolder and copy needed info.
831 viewHolder = new ViewHolder(oldViewHolder.getRoot().getContext());
832 viewHolder
833 .getPrimaryButton()
834 .setDisplayedChild(oldViewHolder.getPrimaryButton().getDisplayedChild());
835 viewHolder.getPrimaryText().setText(oldViewHolder.getPrimaryText().getText());
yueg87111362017-12-08 12:45:50 -0800836 viewHolder.getPrimaryIcon().setX(isDrawingFromRight() ? 0 : primaryIconMoveDistance);
yueg81a77ff2017-12-05 10:29:03 -0800837 viewHolder
838 .getPrimaryIcon()
yueg87111362017-12-08 12:45:50 -0800839 .setTranslationX(isDrawingFromRight() ? -primaryIconMoveDistance : 0);
yuega235e132017-12-13 14:13:57 -0800840 setPrimaryButtonAccessibilityAction(
841 context.getString(R.string.a11y_bubble_primary_button_expand_action));
Eric Erfanian938468d2017-10-24 14:05:52 -0700842
yueg81a77ff2017-12-05 10:29:03 -0800843 update();
844
845 // Add new view at its horizontal boundary
Eric Erfanian938468d2017-10-24 14:05:52 -0700846 ViewGroup root = viewHolder.getRoot();
yueg81a77ff2017-12-05 10:29:03 -0800847 windowParams.x = leftBoundary;
848 windowParams.gravity = Gravity.TOP | (isDrawingFromRight() ? Gravity.RIGHT : Gravity.LEFT);
Eric Erfanian938468d2017-10-24 14:05:52 -0700849 windowManager.addView(root, windowParams);
yueg81a77ff2017-12-05 10:29:03 -0800850
851 // Remove the old view after delay
Eric Erfanian938468d2017-10-24 14:05:52 -0700852 root.getViewTreeObserver()
853 .addOnPreDrawListener(
854 new OnPreDrawListener() {
855 @Override
856 public boolean onPreDraw() {
857 root.getViewTreeObserver().removeOnPreDrawListener(this);
858 // Wait a bit before removing the old view; make sure the new one has drawn over it.
859 handler.postDelayed(
860 () -> windowManager.removeView(oldViewHolder.getRoot()),
861 WINDOW_REDRAW_DELAY_MILLIS);
862 return true;
863 }
864 });
865 }
866
yueg81a77ff2017-12-05 10:29:03 -0800867 int getDrawerVisibility() {
868 return viewHolder.getExpandedView().getVisibility();
Eric Erfanian938468d2017-10-24 14:05:52 -0700869 }
870
871 private boolean isDrawingFromRight() {
872 return (windowParams.gravity & Gravity.RIGHT) == Gravity.RIGHT;
873 }
874
875 private void setFocused(boolean focused) {
876 if (focused) {
877 windowParams.flags &= ~LayoutParams.FLAG_NOT_FOCUSABLE;
878 } else {
879 windowParams.flags |= LayoutParams.FLAG_NOT_FOCUSABLE;
880 }
881 windowManager.updateViewLayout(getRootView(), windowParams);
882 }
883
884 private void defaultAfterHidingAnimation() {
yueg87111362017-12-08 12:45:50 -0800885 exitAnimatorSet = null;
886 viewHolder.getPrimaryButton().setVisibility(View.INVISIBLE);
Eric Erfanian938468d2017-10-24 14:05:52 -0700887 windowManager.removeView(viewHolder.getRoot());
888 visibility = Visibility.HIDDEN;
889
890 updatePrimaryIconAnimation();
891 }
892
yueg81a77ff2017-12-05 10:29:03 -0800893 private void logBasicOrCallImpression(DialerImpression.Type impressionType) {
894 DialerCall call = CallList.getInstance().getActiveOrBackgroundCall();
895 if (call != null) {
896 Logger.get(context)
897 .logCallImpression(impressionType, call.getUniqueCallId(), call.getTimeAddedMs());
898 } else {
899 Logger.get(context).logImpression(impressionType);
900 }
901 }
902
yuega235e132017-12-13 14:13:57 -0800903 private void setPrimaryButtonAccessibilityAction(String description) {
904 viewHolder
905 .getPrimaryButton()
906 .setAccessibilityDelegate(
907 new AccessibilityDelegate() {
908 @Override
909 public void onInitializeAccessibilityNodeInfo(View v, AccessibilityNodeInfo info) {
910 super.onInitializeAccessibilityNodeInfo(v, info);
911
912 AccessibilityAction clickAction =
913 new AccessibilityAction(AccessibilityNodeInfo.ACTION_CLICK, description);
914 info.addAction(clickAction);
915 }
916 });
917 }
918
yueg0b4755c2017-12-18 10:01:03 -0800919 private RoundedRectRevealOutlineProvider createOpenCloseOutlineProvider(View view) {
920 int startRectX = isDrawingFromRight() ? view.getMeasuredWidth() : 0;
921 Rect startRect = new Rect(startRectX, 0, startRectX, 0);
922 Rect endRect = new Rect(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
923
924 float bubbleRadius = context.getResources().getDimension(R.dimen.bubble_radius);
925 return new RoundedRectRevealOutlineProvider(bubbleRadius, bubbleRadius, startRect, endRect);
926 }
927
928 private ValueAnimator createBubbleMoveAnimator(int startX, int endX, int startY, float k) {
929 ValueAnimator xValueAnimator = ValueAnimator.ofFloat(startX, endX);
930 xValueAnimator.setInterpolator(new LinearOutSlowInInterpolator());
931 xValueAnimator.addUpdateListener(
932 (valueAnimator) -> {
933 // Update windowParams and the root layout.
934 // We can't do ViewPropertyAnimation since it clips children.
935 float newX = (float) valueAnimator.getAnimatedValue();
936 if (k != 0) {
937 windowParams.y = startY + (int) (Math.abs(newX - (float) startX) * k);
938 }
939 windowParams.x = (int) newX;
940 windowManager.updateViewLayout(viewHolder.getRoot(), windowParams);
941 });
942 return xValueAnimator;
943 }
944
Eric Erfanian938468d2017-10-24 14:05:52 -0700945 @VisibleForTesting
946 class ViewHolder {
947
yuega5a08d82017-10-31 14:11:53 -0700948 public static final int CHILD_INDEX_AVATAR_AND_ICON = 0;
Eric Erfanian938468d2017-10-24 14:05:52 -0700949 public static final int CHILD_INDEX_TEXT = 1;
950
yueg0b4755c2017-12-18 10:01:03 -0800951 private NewMoveHandler moveHandler;
Eric Erfanian938468d2017-10-24 14:05:52 -0700952 private final NewWindowRoot root;
953 private final ViewAnimator primaryButton;
954 private final ImageView primaryIcon;
yuega5a08d82017-10-31 14:11:53 -0700955 private final ImageView primaryAvatar;
Eric Erfanian938468d2017-10-24 14:05:52 -0700956 private final TextView primaryText;
yueg0b4755c2017-12-18 10:01:03 -0800957 private final View arrow;
Eric Erfanian938468d2017-10-24 14:05:52 -0700958
959 private final NewCheckableButton fullScreenButton;
960 private final NewCheckableButton muteButton;
961 private final NewCheckableButton audioRouteButton;
962 private final NewCheckableButton endCallButton;
963 private final View expandedView;
964
965 public ViewHolder(Context context) {
966 // Window root is not in the layout file so that the inflater has a view to inflate into
967 this.root = new NewWindowRoot(context);
968 LayoutInflater inflater = LayoutInflater.from(root.getContext());
969 View contentView = inflater.inflate(R.layout.new_bubble_base, root, true);
970 expandedView = contentView.findViewById(R.id.bubble_expanded_layout);
971 primaryButton = contentView.findViewById(R.id.bubble_button_primary);
yuega5a08d82017-10-31 14:11:53 -0700972 primaryAvatar = contentView.findViewById(R.id.bubble_icon_avatar);
Eric Erfanian938468d2017-10-24 14:05:52 -0700973 primaryIcon = contentView.findViewById(R.id.bubble_icon_primary);
974 primaryText = contentView.findViewById(R.id.bubble_text);
yueg0b4755c2017-12-18 10:01:03 -0800975 arrow = contentView.findViewById(R.id.bubble_triangle);
Eric Erfanian938468d2017-10-24 14:05:52 -0700976
977 fullScreenButton = contentView.findViewById(R.id.bubble_button_full_screen);
978 muteButton = contentView.findViewById(R.id.bubble_button_mute);
979 audioRouteButton = contentView.findViewById(R.id.bubble_button_audio_route);
980 endCallButton = contentView.findViewById(R.id.bubble_button_end_call);
981
982 root.setOnBackPressedListener(
983 () -> {
984 if (visibility == Visibility.SHOWING && expanded) {
yueg81a77ff2017-12-05 10:29:03 -0800985 startCollapse(
986 CollapseEnd.NOTHING, true /* isUserAction */, true /* shouldRecoverYPosition */);
Eric Erfanian938468d2017-10-24 14:05:52 -0700987 return true;
988 }
989 return false;
990 });
991 root.setOnConfigurationChangedListener(
992 (configuration) -> {
993 // The values in the current MoveHandler may be stale, so replace it. Then ensure the
994 // Window is in bounds
995 moveHandler = new NewMoveHandler(primaryButton, NewBubble.this);
996 moveHandler.snapToBounds();
997 });
998 root.setOnTouchListener(
999 (v, event) -> {
1000 if (expanded && event.getActionMasked() == MotionEvent.ACTION_OUTSIDE) {
yueg81a77ff2017-12-05 10:29:03 -08001001 startCollapse(
1002 CollapseEnd.NOTHING, true /* isUserAction */, true /* shouldRecoverYPosition */);
Eric Erfanian938468d2017-10-24 14:05:52 -07001003 return true;
1004 }
1005 return false;
1006 });
1007 moveHandler = new NewMoveHandler(primaryButton, NewBubble.this);
1008 }
1009
1010 private void setChildClickable(boolean clickable) {
1011 fullScreenButton.setClickable(clickable);
1012 muteButton.setClickable(clickable);
1013 audioRouteButton.setClickable(clickable);
1014 endCallButton.setClickable(clickable);
yueg0b4755c2017-12-18 10:01:03 -08001015 setPrimaryButtonClickable(clickable);
1016 }
Eric Erfanian938468d2017-10-24 14:05:52 -07001017
yueg0b4755c2017-12-18 10:01:03 -08001018 private void setPrimaryButtonClickable(boolean clickable) {
Eric Erfanian938468d2017-10-24 14:05:52 -07001019 moveHandler.setClickable(clickable);
1020 }
1021
yueg81a77ff2017-12-05 10:29:03 -08001022 public int getMoveUpDistance() {
1023 int deltaAllowed =
1024 expandedView.getHeight()
1025 - context
1026 .getResources()
1027 .getDimensionPixelOffset(R.dimen.bubble_button_padding_vertical)
1028 * 2;
1029 return moveHandler.getMoveUpDistance(deltaAllowed);
1030 }
1031
Eric Erfanian938468d2017-10-24 14:05:52 -07001032 public ViewGroup getRoot() {
1033 return root;
1034 }
1035
1036 public ViewAnimator getPrimaryButton() {
1037 return primaryButton;
1038 }
1039
1040 public ImageView getPrimaryIcon() {
1041 return primaryIcon;
1042 }
1043
yuega5a08d82017-10-31 14:11:53 -07001044 public ImageView getPrimaryAvatar() {
1045 return primaryAvatar;
1046 }
1047
Eric Erfanian938468d2017-10-24 14:05:52 -07001048 public TextView getPrimaryText() {
1049 return primaryText;
1050 }
1051
1052 public View getExpandedView() {
1053 return expandedView;
1054 }
1055
yueg0b4755c2017-12-18 10:01:03 -08001056 public View getArrow() {
1057 return arrow;
1058 }
1059
Eric Erfanian938468d2017-10-24 14:05:52 -07001060 public NewCheckableButton getFullScreenButton() {
1061 return fullScreenButton;
1062 }
1063
1064 public NewCheckableButton getMuteButton() {
1065 return muteButton;
1066 }
1067
1068 public NewCheckableButton getAudioRouteButton() {
1069 return audioRouteButton;
1070 }
1071
1072 public NewCheckableButton getEndCallButton() {
1073 return endCallButton;
1074 }
1075
1076 public void setDrawerVisibility(int visibility) {
1077 expandedView.setVisibility(visibility);
1078 }
1079
1080 public boolean isMoving() {
1081 return moveHandler.isMoving();
1082 }
1083
1084 public void undoGravityOverride() {
1085 moveHandler.undoGravityOverride();
1086 }
1087 }
Eric Erfanian938468d2017-10-24 14:05:52 -07001088}