Home | History | Annotate | Download | only in assist
      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 
     17 package com.android.test.assist;
     18 
     19 import android.animation.Animator;
     20 import android.animation.ValueAnimator;
     21 import android.app.VoiceInteractor;
     22 import android.content.Context;
     23 import android.graphics.Color;
     24 import android.os.Bundle;
     25 import android.os.Handler;
     26 import android.service.voice.VoiceInteractionSession;
     27 import android.util.Log;
     28 import android.view.View;
     29 import android.view.ViewAnimationUtils;
     30 import android.view.ViewTreeObserver;
     31 import android.view.animation.AnimationUtils;
     32 import android.view.animation.Interpolator;
     33 
     34 /**
     35  * Sample session to show test assist transition.
     36  */
     37 public 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         getWindow().getWindow().getDecorView().setSystemUiVisibility(
     82                 View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
     83     }
     84 
     85     @Override
     86     public View onCreateContentView() {
     87         View v = getLayoutInflater().inflate(R.layout.assist, null);
     88         mScrim = v.findViewById(R.id.scrim);
     89         mBackground = v.findViewById(R.id.background);
     90         mDensity = mScrim.getResources().getDisplayMetrics().density;
     91         mCard1 = v.findViewById(R.id.card1);
     92         mCard2 = v.findViewById(R.id.card2);
     93         mNavbarScrim = v.findViewById(R.id.navbar_scrim);
     94         return v;
     95     }
     96 
     97     @Override
     98     public void onShow(Bundle args, int showFlags) {
     99         super.onShow(args, showFlags);
    100         if ((showFlags & SHOW_SOURCE_ASSIST_GESTURE) != 0) {
    101             mBackground.getViewTreeObserver().addOnPreDrawListener(
    102                     new ViewTreeObserver.OnPreDrawListener() {
    103                         @Override
    104                         public boolean onPreDraw() {
    105                             mBackground.getViewTreeObserver().removeOnPreDrawListener(this);
    106                             playAssistAnimation();
    107                             return true;
    108                         }
    109                     });
    110         }
    111     }
    112 
    113     @Override
    114     public void onLockscreenShown() {
    115         super.onLockscreenShown();
    116         Log.i("Assistant", "Lockscreen was shown");
    117     }
    118 
    119     private void playAssistAnimation() {
    120         Interpolator linearOutSlowIn = AnimationUtils.loadInterpolator(mBackground.getContext(),
    121                 android.R.interpolator.linear_out_slow_in);
    122         Interpolator fastOutSlowIn = AnimationUtils.loadInterpolator(mBackground.getContext(),
    123                 android.R.interpolator.fast_out_slow_in);
    124         mScrim.setAlpha(0f);
    125         mScrim.animate()
    126                 .alpha(1f)
    127                 .setStartDelay(100)
    128                 .setDuration(500);
    129         mBackground.setTranslationY(50 * mDensity);
    130         mBackground.animate()
    131                 .translationY(0)
    132                 .setDuration(300)
    133                 .setInterpolator(linearOutSlowIn);
    134         int centerX = mBackground.getWidth()/2;
    135         int centerY = (int) (mBackground.getHeight()/5*3.8f);
    136         int radius = (int) Math.sqrt(centerX*centerX + centerY*centerY) + 1;
    137         Animator animator = ViewAnimationUtils.createCircularReveal(mBackground, centerX, centerY,
    138                 0, radius);
    139         animator.setDuration(300);
    140         animator.setInterpolator(fastOutSlowIn);
    141         animator.start();
    142 
    143         ValueAnimator colorAnim = ValueAnimator.ofArgb(Color.WHITE, 0xffe0e0e0);
    144         colorAnim.setDuration(300);
    145         colorAnim.setInterpolator(fastOutSlowIn);
    146         colorAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    147             @Override
    148             public void onAnimationUpdate(ValueAnimator animation) {
    149                 mBackground.setBackgroundColor((Integer) animation.getAnimatedValue());
    150             }
    151         });
    152         colorAnim.start();
    153 
    154 
    155         mCard1.setY(mBackground.getHeight());
    156         mCard2.setTranslationY(mCard1.getTranslationY());
    157         mCard1.animate()
    158                 .translationY(0)
    159                 .setDuration(500)
    160                 .setInterpolator(linearOutSlowIn)
    161                 .setStartDelay(100);
    162         mCard2.animate()
    163                 .translationY(0)
    164                 .setInterpolator(linearOutSlowIn)
    165                 .setStartDelay(150)
    166                 .setDuration(500);
    167 
    168         mNavbarScrim.setAlpha(0f);
    169         mNavbarScrim.animate()
    170                 .alpha(1f)
    171                 .setDuration(500)
    172                 .setStartDelay(100);
    173     }
    174 
    175     @Override
    176     public void onHide() {
    177         super.onHide();
    178     }
    179 }
    180