AssistInteractionSession.java revision 593334ab70a8341c7d24d71a377ab5617e3f4ab7
1/* 2 * Copyright (C) 2015 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.test.assist; 18 19import android.animation.Animator; 20import android.animation.ValueAnimator; 21import android.app.VoiceInteractor; 22import android.content.Context; 23import android.graphics.Color; 24import android.os.Bundle; 25import android.os.Handler; 26import android.service.voice.VoiceInteractionSession; 27import android.view.View; 28import android.view.ViewAnimationUtils; 29import android.view.ViewTreeObserver; 30import android.view.animation.AnimationUtils; 31import android.view.animation.Interpolator; 32 33/** 34 * Sample session to show test assist transition. 35 */ 36public class AssistInteractionSession extends VoiceInteractionSession { 37 38 private View mScrim; 39 private View mBackground; 40 private View mNavbarScrim; 41 private View mCard1; 42 private View mCard2; 43 44 private float mDensity; 45 46 public AssistInteractionSession(Context context) { 47 super(context); 48 } 49 50 public AssistInteractionSession(Context context, Handler handler) { 51 super(context, handler); 52 } 53 54 @Override 55 public void onRequestConfirmation(ConfirmationRequest request) { 56 } 57 58 @Override 59 public void onRequestPickOption(PickOptionRequest request) { 60 } 61 62 @Override 63 public void onRequestCommand(CommandRequest request) { 64 } 65 66 @Override 67 public void onCancelRequest(Request request) { 68 } 69 70 @Override 71 public void onCreate() { 72 // Simulate slowness of Assist app 73 try { 74 Thread.sleep(1000); 75 } catch (InterruptedException e) { 76 e.printStackTrace(); 77 } 78 } 79 80 @Override 81 public View onCreateContentView() { 82 View v = getLayoutInflater().inflate(R.layout.assist, null); 83 mScrim = v.findViewById(R.id.scrim); 84 mBackground = v.findViewById(R.id.background); 85 mDensity = mScrim.getResources().getDisplayMetrics().density; 86 mCard1 = v.findViewById(R.id.card1); 87 mCard2 = v.findViewById(R.id.card2); 88 mNavbarScrim = v.findViewById(R.id.navbar_scrim); 89 return v; 90 } 91 92 @Override 93 public void onShow(Bundle args, int showFlags) { 94 super.onShow(args, showFlags); 95 if ((showFlags & SHOW_SOURCE_ASSIST_GESTURE) != 0) { 96 mBackground.getViewTreeObserver().addOnPreDrawListener( 97 new ViewTreeObserver.OnPreDrawListener() { 98 @Override 99 public boolean onPreDraw() { 100 mBackground.getViewTreeObserver().removeOnPreDrawListener(this); 101 playAssistAnimation(); 102 return true; 103 } 104 }); 105 } 106 } 107 108 private void playAssistAnimation() { 109 Interpolator linearOutSlowIn = AnimationUtils.loadInterpolator(mBackground.getContext(), 110 android.R.interpolator.linear_out_slow_in); 111 Interpolator fastOutSlowIn = AnimationUtils.loadInterpolator(mBackground.getContext(), 112 android.R.interpolator.fast_out_slow_in); 113 mScrim.setAlpha(0f); 114 mScrim.animate() 115 .alpha(1f) 116 .setStartDelay(100) 117 .setDuration(500); 118 mBackground.setTranslationY(50 * mDensity); 119 mBackground.animate() 120 .translationY(0) 121 .setDuration(300) 122 .setInterpolator(linearOutSlowIn); 123 int centerX = mBackground.getWidth()/2; 124 int centerY = (int) (mBackground.getHeight()/5*3.8f); 125 int radius = (int) Math.sqrt(centerX*centerX + centerY*centerY) + 1; 126 Animator animator = ViewAnimationUtils.createCircularReveal(mBackground, centerX, centerY, 127 0, radius); 128 animator.setDuration(300); 129 animator.setInterpolator(fastOutSlowIn); 130 animator.start(); 131 132 ValueAnimator colorAnim = ValueAnimator.ofArgb(Color.WHITE, 0xffe0e0e0); 133 colorAnim.setDuration(300); 134 colorAnim.setInterpolator(fastOutSlowIn); 135 colorAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 136 @Override 137 public void onAnimationUpdate(ValueAnimator animation) { 138 mBackground.setBackgroundColor((Integer) animation.getAnimatedValue()); 139 } 140 }); 141 colorAnim.start(); 142 143 144 mCard1.setY(mBackground.getHeight()); 145 mCard2.setTranslationY(mCard1.getTranslationY()); 146 mCard1.animate() 147 .translationY(0) 148 .setDuration(500) 149 .setInterpolator(linearOutSlowIn) 150 .setStartDelay(100); 151 mCard2.animate() 152 .translationY(0) 153 .setInterpolator(linearOutSlowIn) 154 .setStartDelay(150) 155 .setDuration(500); 156 157 mNavbarScrim.setAlpha(0f); 158 mNavbarScrim.animate() 159 .alpha(1f) 160 .setDuration(500) 161 .setStartDelay(100); 162 } 163 164 @Override 165 public void onHide() { 166 super.onHide(); 167 } 168} 169