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