summaryrefslogtreecommitdiffstats
path: root/tests/Assist/src/com/android/test/assist/AssistInteractionSession.java
blob: 851bda92a5a5bec96ffe51545b437866869b39e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * 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.test.assist;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.app.VoiceInteractor;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.service.voice.VoiceInteractionSession;
import android.util.Log;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.ViewTreeObserver;
import android.view.animation.AnimationUtils;
import android.view.animation.Interpolator;

/**
 * Sample session to show test assist transition.
 */
public class AssistInteractionSession extends VoiceInteractionSession {

    private View mScrim;
    private View mBackground;
    private View mNavbarScrim;
    private View mCard1;
    private View mCard2;

    private float mDensity;

    public AssistInteractionSession(Context context) {
        super(context);
    }

    public AssistInteractionSession(Context context, Handler handler) {
        super(context, handler);
    }

    @Override
    public void onRequestConfirmation(ConfirmationRequest request) {
    }

    @Override
    public void onRequestPickOption(PickOptionRequest request) {
    }

    @Override
    public void onRequestCommand(CommandRequest request) {
    }

    @Override
    public void onCancelRequest(Request request) {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        // Simulate slowness of Assist app
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public View onCreateContentView() {
        View v = getLayoutInflater().inflate(R.layout.assist, null);
        mScrim = v.findViewById(R.id.scrim);
        mBackground = v.findViewById(R.id.background);
        mDensity = mScrim.getResources().getDisplayMetrics().density;
        mCard1 = v.findViewById(R.id.card1);
        mCard2 = v.findViewById(R.id.card2);
        mNavbarScrim = v.findViewById(R.id.navbar_scrim);
        return v;
    }

    @Override
    public void onShow(Bundle args, int showFlags) {
        super.onShow(args, showFlags);
        if ((showFlags & SHOW_SOURCE_ASSIST_GESTURE) != 0) {
            mBackground.getViewTreeObserver().addOnPreDrawListener(
                    new ViewTreeObserver.OnPreDrawListener() {
                        @Override
                        public boolean onPreDraw() {
                            mBackground.getViewTreeObserver().removeOnPreDrawListener(this);
                            playAssistAnimation();
                            return true;
                        }
                    });
        }
    }

    @Override
    public void onLockscreenShown() {
        super.onLockscreenShown();
        Log.i("Assistant", "Lockscreen was shown");
    }

    private void playAssistAnimation() {
        Interpolator linearOutSlowIn = AnimationUtils.loadInterpolator(mBackground.getContext(),
                android.R.interpolator.linear_out_slow_in);
        Interpolator fastOutSlowIn = AnimationUtils.loadInterpolator(mBackground.getContext(),
                android.R.interpolator.fast_out_slow_in);
        mScrim.setAlpha(0f);
        mScrim.animate()
                .alpha(1f)
                .setStartDelay(100)
                .setDuration(500);
        mBackground.setTranslationY(50 * mDensity);
        mBackground.animate()
                .translationY(0)
                .setDuration(300)
                .setInterpolator(linearOutSlowIn);
        int centerX = mBackground.getWidth()/2;
        int centerY = (int) (mBackground.getHeight()/5*3.8f);
        int radius = (int) Math.sqrt(centerX*centerX + centerY*centerY) + 1;
        Animator animator = ViewAnimationUtils.createCircularReveal(mBackground, centerX, centerY,
                0, radius);
        animator.setDuration(300);
        animator.setInterpolator(fastOutSlowIn);
        animator.start();

        ValueAnimator colorAnim = ValueAnimator.ofArgb(Color.WHITE, 0xffe0e0e0);
        colorAnim.setDuration(300);
        colorAnim.setInterpolator(fastOutSlowIn);
        colorAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mBackground.setBackgroundColor((Integer) animation.getAnimatedValue());
            }
        });
        colorAnim.start();


        mCard1.setY(mBackground.getHeight());
        mCard2.setTranslationY(mCard1.getTranslationY());
        mCard1.animate()
                .translationY(0)
                .setDuration(500)
                .setInterpolator(linearOutSlowIn)
                .setStartDelay(100);
        mCard2.animate()
                .translationY(0)
                .setInterpolator(linearOutSlowIn)
                .setStartDelay(150)
                .setDuration(500);

        mNavbarScrim.setAlpha(0f);
        mNavbarScrim.animate()
                .alpha(1f)
                .setDuration(500)
                .setStartDelay(100);
    }

    @Override
    public void onHide() {
        super.onHide();
    }
}