Initial checkin of AOSP Messaging app.

b/23110861

Change-Id: I9aa980d7569247d6b2ca78f5dcb4502e1eaadb8a
This commit is contained in:
Mike Dodd
2015-08-12 08:58:28 -07:00
parent 8b3e2b9c1b
commit 461a34b466
1645 changed files with 186271 additions and 0 deletions
@@ -0,0 +1,302 @@
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.messaging.ui.animation;
import android.animation.TypeEvaluator;
import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.PopupWindow;
import com.android.messaging.util.LogUtil;
import com.android.messaging.util.ThreadUtil;
import com.android.messaging.util.UiUtils;
/**
* Animates viewToAnimate from startRect to the place where it is in the layout, viewToAnimate
* should be in its final destination location before startAfterLayoutComplete is called.
* viewToAnimate will be drawn scaled and offset in a popupWindow.
* This class handles the case where the viewToAnimate moves during the animation
*/
public class PopupTransitionAnimation extends Animation {
/** The view we're animating */
private final View mViewToAnimate;
/** The rect to start the slide in animation from */
private final Rect mStartRect;
/** The rect of the currently animated view */
private Rect mCurrentRect;
/** The rect that we're animating to. This can change during the animation */
private final Rect mDestRect;
/** The bounds of the popup in window coordinates. Does not include notification bar */
private final Rect mPopupRect;
/** The bounds of the action bar in window coordinates. We clip the popup to below this */
private final Rect mActionBarRect;
/** Interpolates between the start and end rect for every animation tick */
private final TypeEvaluator<Rect> mRectEvaluator;
/** The popup window that holds contains the animating view */
private PopupWindow mPopupWindow;
/** The layout root for the popup which is where the animated view is rendered */
private View mPopupRoot;
/** The action bar's view */
private final View mActionBarView;
private Runnable mOnStartCallback;
private Runnable mOnStopCallback;
public PopupTransitionAnimation(final Rect startRect, final View viewToAnimate) {
mViewToAnimate = viewToAnimate;
mStartRect = startRect;
mCurrentRect = new Rect(mStartRect);
mDestRect = new Rect();
mPopupRect = new Rect();
mActionBarRect = new Rect();
final Activity activity = (Activity) viewToAnimate.getRootView().getContext();
mActionBarView = activity.getWindow().getDecorView().findViewById(
android.support.v7.appcompat.R.id.action_bar);
mRectEvaluator = RectEvaluatorCompat.create();
setDuration(UiUtils.MEDIAPICKER_TRANSITION_DURATION);
setInterpolator(UiUtils.DEFAULT_INTERPOLATOR);
setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(final Animation animation) {
if (mOnStartCallback != null) {
mOnStartCallback.run();
}
mEvents.append("oAS,");
}
@Override
public void onAnimationEnd(final Animation animation) {
if (mOnStopCallback != null) {
mOnStopCallback.run();
}
dismiss();
mEvents.append("oAE,");
}
@Override
public void onAnimationRepeat(final Animation animation) {
}
});
}
private final StringBuilder mEvents = new StringBuilder();
private final Runnable mCleanupRunnable = new Runnable() {
@Override
public void run() {
LogUtil.w(LogUtil.BUGLE_TAG, "PopupTransitionAnimation: " + mEvents);
}
};
/**
* Ensures the animation is ready before starting the animation.
* viewToAnimate must first be layed out so we know where we will animate to
*/
public void startAfterLayoutComplete() {
// We want layout to occur, and then we immediately animate it in, so hide it initially to
// reduce jank on the first frame
mViewToAnimate.setVisibility(View.INVISIBLE);
mViewToAnimate.setAlpha(0);
final Runnable startAnimation = new Runnable() {
boolean mRunComplete = false;
boolean mFirstTry = true;
@Override
public void run() {
if (mRunComplete) {
return;
}
mViewToAnimate.getGlobalVisibleRect(mDestRect);
// In Android views which are visible but haven't computed their size yet have a
// size of 1x1 because anything with a size of 0x0 is considered hidden. We can't
// start the animation until after the size is greater than 1x1
if (mDestRect.width() <= 1 || mDestRect.height() <= 1) {
// Layout hasn't occurred yet
if (!mFirstTry) {
// Give up if this is not the first try, since layout change still doesn't
// yield a size for the view. This is likely because the media picker is
// full screen so there's no space left for the animated view. We give up
// on animation, but need to make sure the view that was initially
// hidden is re-shown.
mViewToAnimate.setAlpha(1);
mViewToAnimate.setVisibility(View.VISIBLE);
} else {
mFirstTry = false;
UiUtils.doOnceAfterLayoutChange(mViewToAnimate, this);
}
return;
}
mRunComplete = true;
mViewToAnimate.startAnimation(PopupTransitionAnimation.this);
mViewToAnimate.invalidate();
// http://b/20856505: The PopupWindow sometimes does not get dismissed.
ThreadUtil.getMainThreadHandler().postDelayed(mCleanupRunnable, getDuration() * 2);
}
};
startAnimation.run();
}
public PopupTransitionAnimation setOnStartCallback(final Runnable onStart) {
mOnStartCallback = onStart;
return this;
}
public PopupTransitionAnimation setOnStopCallback(final Runnable onStop) {
mOnStopCallback = onStop;
return this;
}
@Override
protected void applyTransformation(final float interpolatedTime, final Transformation t) {
if (mPopupWindow == null) {
initPopupWindow();
}
// Update mDestRect as it may have moved during the animation
mPopupRect.set(UiUtils.getMeasuredBoundsOnScreen(mPopupRoot));
mActionBarRect.set(UiUtils.getMeasuredBoundsOnScreen(mActionBarView));
computeDestRect();
// Update currentRect to the new animated coordinates, and request mPopupRoot to redraw
// itself at the new coordinates
mCurrentRect = mRectEvaluator.evaluate(interpolatedTime, mStartRect, mDestRect);
mPopupRoot.invalidate();
if (interpolatedTime >= 0.98) {
mEvents.append("aT").append(interpolatedTime).append(',');
}
if (interpolatedTime == 1) {
dismiss();
}
}
private void dismiss() {
mEvents.append("d,");
mViewToAnimate.setAlpha(1);
mViewToAnimate.setVisibility(View.VISIBLE);
// Delay dismissing the popup window to let mViewToAnimate draw under it and reduce the
// flash
ThreadUtil.getMainThreadHandler().post(new Runnable() {
@Override
public void run() {
try {
mPopupWindow.dismiss();
} catch (IllegalArgumentException e) {
// PopupWindow.dismiss() will fire an IllegalArgumentException if the activity
// has already ended while we were animating
}
ThreadUtil.getMainThreadHandler().removeCallbacks(mCleanupRunnable);
}
});
}
@Override
public boolean willChangeBounds() {
return false;
}
/**
* Computes mDestRect (the position in window space of the placeholder view that we should
* animate to). Some frames during the animation fail to compute getGlobalVisibleRect, so use
* the last known values in that case
*/
private void computeDestRect() {
final int prevTop = mDestRect.top;
final int prevLeft = mDestRect.left;
final int prevRight = mDestRect.right;
final int prevBottom = mDestRect.bottom;
if (!getViewScreenMeasureRect(mViewToAnimate, mDestRect)) {
mDestRect.top = prevTop;
mDestRect.left = prevLeft;
mDestRect.bottom = prevBottom;
mDestRect.right = prevRight;
}
}
/**
* Sets up the PopupWindow that the view will animate in. Animating the size and position of a
* popup can be choppy, so instead we make the popup fill the entire space of the screen, and
* animate the position of viewToAnimate within the popup using a Transformation
*/
private void initPopupWindow() {
mPopupRoot = new View(mViewToAnimate.getContext()) {
@Override
protected void onDraw(final Canvas canvas) {
canvas.save();
canvas.clipRect(getLeft(), mActionBarRect.bottom - mPopupRect.top, getRight(),
getBottom());
canvas.drawColor(Color.TRANSPARENT);
final float previousAlpha = mViewToAnimate.getAlpha();
mViewToAnimate.setAlpha(1);
// The view's global position includes the notification bar height, but
// the popup window may or may not cover the notification bar (depending on screen
// rotation, IME status etc.), so we need to compensate for this difference by
// offseting vertically.
canvas.translate(mCurrentRect.left, mCurrentRect.top - mPopupRect.top);
final float viewWidth = mViewToAnimate.getWidth();
final float viewHeight = mViewToAnimate.getHeight();
if (viewWidth > 0 && viewHeight > 0) {
canvas.scale(mCurrentRect.width() / viewWidth,
mCurrentRect.height() / viewHeight);
}
canvas.clipRect(0, 0, mCurrentRect.width(), mCurrentRect.height());
if (!mPopupRect.isEmpty()) {
// HACK: Layout is unstable until mPopupRect is non-empty.
mViewToAnimate.draw(canvas);
}
mViewToAnimate.setAlpha(previousAlpha);
canvas.restore();
}
};
mPopupWindow = new PopupWindow(mViewToAnimate.getContext());
mPopupWindow.setBackgroundDrawable(null);
mPopupWindow.setContentView(mPopupRoot);
mPopupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
mPopupWindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
mPopupWindow.setTouchable(false);
// We must pass a non-zero value for the y offset, or else the system resets the status bar
// color to black (M only) during the animation. The actual position of the window (and
// the animated view inside it) are still correct, regardless of what we pass for the y
// parameter (e.g. 1 and 100 both work). Not entirely sure why this works.
mPopupWindow.showAtLocation(mViewToAnimate, Gravity.TOP, 0, 1);
}
private static boolean getViewScreenMeasureRect(final View view, final Rect outRect) {
outRect.set(UiUtils.getMeasuredBoundsOnScreen(view));
return !outRect.isEmpty();
}
}
@@ -0,0 +1,45 @@
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.messaging.ui.animation;
import android.animation.RectEvaluator;
import android.animation.TypeEvaluator;
import android.graphics.Rect;
import com.android.messaging.util.OsUtil;
/**
* This evaluator can be used to perform type interpolation between <code>Rect</code> values.
* It's backward compatible to Api Level 11.
*/
public class RectEvaluatorCompat implements TypeEvaluator<Rect> {
public static TypeEvaluator<Rect> create() {
if (OsUtil.isAtLeastJB_MR2()) {
return new RectEvaluator();
} else {
return new RectEvaluatorCompat();
}
}
@Override
public Rect evaluate(float fraction, Rect startValue, Rect endValue) {
int left = startValue.left + (int) ((endValue.left - startValue.left) * fraction);
int top = startValue.top + (int) ((endValue.top - startValue.top) * fraction);
int right = startValue.right + (int) ((endValue.right - startValue.right) * fraction);
int bottom = startValue.bottom + (int) ((endValue.bottom - startValue.bottom) * fraction);
return new Rect(left, top, right, bottom);
}
}
@@ -0,0 +1,208 @@
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.messaging.ui.animation;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.view.ViewCompat;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroupOverlay;
import android.view.ViewOverlay;
import android.widget.FrameLayout;
import com.android.messaging.R;
import com.android.messaging.util.ImageUtils;
import com.android.messaging.util.OsUtil;
import com.android.messaging.util.UiUtils;
/**
* <p>
* Shows a vertical "explode" animation for any view inside a view group (e.g. views inside a
* ListView). During the animation, a snapshot is taken for the view to the animated and
* presented in a popup window or view overlay on top of the original view group. The background
* of the view (a highlight) vertically expands (explodes) during the animation.
* </p>
* <p>
* The exact implementation of the animation depends on platform API level. For JB_MR2 and later,
* the implementation utilizes ViewOverlay to perform highly performant overlay animations; for
* older API levels, the implementation falls back to using a full screen popup window to stage
* the animation.
* </p>
* <p>
* To start this animation, call {@link #startAnimationForView(ViewGroup, View, View, boolean, int)}
* </p>
*/
public class ViewGroupItemVerticalExplodeAnimation {
/**
* Starts a vertical explode animation for a given view situated in a given container.
*
* @param container the container of the view which determines the explode animation's final
* size
* @param viewToAnimate the view to be animated. The view will be highlighted by the explode
* highlight, which expands from the size of the view to the size of the container.
* @param animationStagingView the view that stages the animation. Since viewToAnimate may be
* removed from the view tree during the animation, we need a view that'll be alive
* for the duration of the animation so that the animation won't get cancelled.
* @param snapshotView whether a snapshot of the view to animate is needed.
*/
public static void startAnimationForView(final ViewGroup container, final View viewToAnimate,
final View animationStagingView, final boolean snapshotView, final int duration) {
if (OsUtil.isAtLeastJB_MR2() && (viewToAnimate.getContext() instanceof Activity)) {
new ViewExplodeAnimationJellyBeanMR2(viewToAnimate, container, snapshotView, duration)
.startAnimation();
} else {
// Pre JB_MR2, this animation can cause rendering failures which causes the framework
// to fall back to software rendering where camera preview isn't supported (b/18264647)
// just skip the animation to avoid this case.
}
}
/**
* Implementation class for API level >= 18.
*/
@TargetApi(18)
private static class ViewExplodeAnimationJellyBeanMR2 {
private final View mViewToAnimate;
private final ViewGroup mContainer;
private final View mSnapshot;
private final Bitmap mViewBitmap;
private final int mDuration;
public ViewExplodeAnimationJellyBeanMR2(final View viewToAnimate, final ViewGroup container,
final boolean snapshotView, final int duration) {
mViewToAnimate = viewToAnimate;
mContainer = container;
mDuration = duration;
if (snapshotView) {
mViewBitmap = snapshotView(viewToAnimate);
mSnapshot = new View(viewToAnimate.getContext());
} else {
mSnapshot = null;
mViewBitmap = null;
}
}
public void startAnimation() {
final Context context = mViewToAnimate.getContext();
final Resources resources = context.getResources();
final View decorView = ((Activity) context).getWindow().getDecorView();
final ViewOverlay viewOverlay = decorView.getOverlay();
if (viewOverlay instanceof ViewGroupOverlay) {
final ViewGroupOverlay overlay = (ViewGroupOverlay) viewOverlay;
// Add a shadow layer to the overlay.
final FrameLayout shadowContainerLayer = new FrameLayout(context);
final Drawable oldBackground = mViewToAnimate.getBackground();
final Rect containerRect = UiUtils.getMeasuredBoundsOnScreen(mContainer);
final Rect decorRect = UiUtils.getMeasuredBoundsOnScreen(decorView);
// Position the container rect relative to the decor rect since the decor rect
// defines whether the view overlay will be positioned.
containerRect.offset(-decorRect.left, -decorRect.top);
shadowContainerLayer.setLeft(containerRect.left);
shadowContainerLayer.setTop(containerRect.top);
shadowContainerLayer.setBottom(containerRect.bottom);
shadowContainerLayer.setRight(containerRect.right);
shadowContainerLayer.setBackgroundColor(resources.getColor(
R.color.open_conversation_animation_background_shadow));
// Per design request, temporarily clear out the background of the item content
// to not show any ripple effects during animation.
if (!(oldBackground instanceof ColorDrawable)) {
mViewToAnimate.setBackground(null);
}
overlay.add(shadowContainerLayer);
// Add a expand layer and position it with in the shadow background, so it can
// be properly clipped to the container bounds during the animation.
final View expandLayer = new View(context);
final int elevation = resources.getDimensionPixelSize(
R.dimen.explode_animation_highlight_elevation);
final Rect viewRect = UiUtils.getMeasuredBoundsOnScreen(mViewToAnimate);
// Frame viewRect from screen space to containerRect space.
viewRect.offset(-containerRect.left - decorRect.left,
-containerRect.top - decorRect.top);
// Since the expand layer expands at the same rate above and below, we need to
// compute the expand scale using the bigger of the top/bottom distances.
final int expandLayerHalfHeight = viewRect.height() / 2;
final int topDist = viewRect.top;
final int bottomDist = containerRect.height() - viewRect.bottom;
final float scale = expandLayerHalfHeight == 0 ? 1 :
((float) Math.max(topDist, bottomDist) + expandLayerHalfHeight) /
expandLayerHalfHeight;
// Position the expand layer initially to exactly match the animated item.
shadowContainerLayer.addView(expandLayer);
expandLayer.setLeft(viewRect.left);
expandLayer.setTop(viewRect.top);
expandLayer.setBottom(viewRect.bottom);
expandLayer.setRight(viewRect.right);
expandLayer.setBackgroundColor(resources.getColor(
R.color.conversation_background));
ViewCompat.setElevation(expandLayer, elevation);
// Conditionally stage the snapshot in the overlay.
if (mSnapshot != null) {
shadowContainerLayer.addView(mSnapshot);
mSnapshot.setLeft(viewRect.left);
mSnapshot.setTop(viewRect.top);
mSnapshot.setBottom(viewRect.bottom);
mSnapshot.setRight(viewRect.right);
mSnapshot.setBackground(new BitmapDrawable(resources, mViewBitmap));
ViewCompat.setElevation(mSnapshot, elevation);
}
// Apply a scale animation to scale to full screen.
expandLayer.animate().scaleY(scale)
.setDuration(mDuration)
.setInterpolator(UiUtils.EASE_IN_INTERPOLATOR)
.withEndAction(new Runnable() {
@Override
public void run() {
// Clean up the views added to overlay on animation finish.
overlay.remove(shadowContainerLayer);
mViewToAnimate.setBackground(oldBackground);
if (mViewBitmap != null) {
mViewBitmap.recycle();
}
}
});
}
}
}
/**
* Take a snapshot of the given review, return a Bitmap object that's owned by the caller.
*/
static Bitmap snapshotView(final View view) {
// Save the content of the view into a bitmap.
final Bitmap viewBitmap = Bitmap.createBitmap(view.getWidth(),
view.getHeight(), Bitmap.Config.ARGB_8888);
// Strip the view of its background when taking a snapshot so that things like touch
// feedback don't get accidentally snapshotted.
final Drawable viewBackground = view.getBackground();
ImageUtils.setBackgroundDrawableOnView(view, null);
view.draw(new Canvas(viewBitmap));
ImageUtils.setBackgroundDrawableOnView(view, viewBackground);
return viewBitmap;
}
}