AssistInteractionSession.java revision 2ee5c368f844bc0f6ce55ff6d5cf3d5604cad5d8
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 onConfirm(Caller caller,
56            Request request, CharSequence prompt, Bundle extras) {
57
58    }
59
60    @Override
61    public void onPickOption(Caller caller,
62            Request request, CharSequence prompt,
63            VoiceInteractor.PickOptionRequest.Option[] options, Bundle extras) {
64
65    }
66
67    @Override
68    public void onCommand(Caller caller,
69            Request request, String command, Bundle extras) {
70
71    }
72
73    @Override
74    public void onCreate(Bundle args) {
75        super.onCreate(args);
76
77        // Simulate slowness of Assist app
78        try {
79            Thread.sleep(1000);
80        } catch (InterruptedException e) {
81            e.printStackTrace();
82        }
83    }
84
85    @Override
86    public void onCancel(Request request) {
87
88    }
89
90    @Override
91    public View onCreateContentView() {
92        View v = getLayoutInflater().inflate(R.layout.assist, null);
93        mScrim = v.findViewById(R.id.scrim);
94        mBackground = v.findViewById(R.id.background);
95        mDensity = mScrim.getResources().getDisplayMetrics().density;
96        mCard1 = v.findViewById(R.id.card1);
97        mCard2 = v.findViewById(R.id.card2);
98        mNavbarScrim = v.findViewById(R.id.navbar_scrim);
99        return v;
100    }
101
102    @Override
103    public void onShow(Bundle args, int showFlags) {
104        super.onShow(args, showFlags);
105        if ((showFlags & SHOW_SOURCE_ASSIST_GESTURE) != 0) {
106            mBackground.getViewTreeObserver().addOnPreDrawListener(
107                    new ViewTreeObserver.OnPreDrawListener() {
108                        @Override
109                        public boolean onPreDraw() {
110                            mBackground.getViewTreeObserver().removeOnPreDrawListener(this);
111                            playAssistAnimation();
112                            return true;
113                        }
114                    });
115        }
116    }
117
118    private void playAssistAnimation() {
119        Interpolator linearOutSlowIn = AnimationUtils.loadInterpolator(mBackground.getContext(),
120                android.R.interpolator.linear_out_slow_in);
121        Interpolator fastOutSlowIn = AnimationUtils.loadInterpolator(mBackground.getContext(),
122                android.R.interpolator.fast_out_slow_in);
123        mScrim.setAlpha(0f);
124        mScrim.animate()
125                .alpha(1f)
126                .setStartDelay(100)
127                .setDuration(500);
128        mBackground.setTranslationY(50 * mDensity);
129        mBackground.animate()
130                .translationY(0)
131                .setDuration(300)
132                .setInterpolator(linearOutSlowIn);
133        int centerX = mBackground.getWidth()/2;
134        int centerY = (int) (mBackground.getHeight()/5*3.8f);
135        int radius = (int) Math.sqrt(centerX*centerX + centerY*centerY) + 1;
136        Animator animator = ViewAnimationUtils.createCircularReveal(mBackground, centerX, centerY,
137                0, radius);
138        animator.setDuration(300);
139        animator.setInterpolator(fastOutSlowIn);
140        animator.start();
141
142        ValueAnimator colorAnim = ValueAnimator.ofArgb(Color.WHITE, 0xffe0e0e0);
143        colorAnim.setDuration(300);
144        colorAnim.setInterpolator(fastOutSlowIn);
145        colorAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
146            @Override
147            public void onAnimationUpdate(ValueAnimator animation) {
148                mBackground.setBackgroundColor((Integer) animation.getAnimatedValue());
149            }
150        });
151        colorAnim.start();
152
153
154        mCard1.setY(mBackground.getHeight());
155        mCard2.setTranslationY(mCard1.getTranslationY());
156        mCard1.animate()
157                .translationY(0)
158                .setDuration(500)
159                .setInterpolator(linearOutSlowIn)
160                .setStartDelay(100);
161        mCard2.animate()
162                .translationY(0)
163                .setInterpolator(linearOutSlowIn)
164                .setStartDelay(150)
165                .setDuration(500);
166
167        mNavbarScrim.setAlpha(0f);
168        mNavbarScrim.animate()
169                .alpha(1f)
170                .setDuration(500)
171                .setStartDelay(100);
172    }
173
174    @Override
175    public void onHide() {
176        super.onHide();
177    }
178}
179