1 /*
2  * Copyright (C) 2022 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.systemui.dreams.touch.dagger;
18 
19 import android.animation.ValueAnimator;
20 import android.content.res.Resources;
21 import android.util.TypedValue;
22 import android.view.VelocityTracker;
23 
24 import com.android.systemui.R;
25 import com.android.systemui.dagger.qualifiers.Main;
26 import com.android.systemui.dreams.touch.BouncerSwipeTouchHandler;
27 import com.android.systemui.dreams.touch.DreamTouchHandler;
28 import com.android.systemui.shade.ShadeViewController;
29 import com.android.wm.shell.animation.FlingAnimationUtils;
30 
31 import dagger.Module;
32 import dagger.Provides;
33 import dagger.multibindings.IntoSet;
34 
35 import javax.inject.Named;
36 import javax.inject.Provider;
37 
38 /**
39  * This module captures the components associated with {@link BouncerSwipeTouchHandler}.
40  */
41 @Module
42 public class BouncerSwipeModule {
43     /**
44      * The region, defined as the percentage of the screen, from which a touch gesture to start
45      * swiping up to the bouncer can occur.
46      */
47     public static final String SWIPE_TO_BOUNCER_START_REGION = "swipe_to_bouncer_start_region";
48 
49     /**
50      * The {@link android.view.animation.AnimationUtils} for animating the bouncer closing.
51      */
52     public static final String SWIPE_TO_BOUNCER_FLING_ANIMATION_UTILS_CLOSING =
53             "swipe_to_bouncer_fling_animation_utils_closing";
54 
55     /**
56      * The {@link android.view.animation.AnimationUtils} for animating the bouncer opening.
57      */
58     public static final String SWIPE_TO_BOUNCER_FLING_ANIMATION_UTILS_OPENING =
59                     "swipe_to_bouncer_fling_animation_utils_opening";
60 
61     /**
62      * Provides {@link BouncerSwipeTouchHandler} for inclusion in touch handling over the dream.
63      */
64     @Provides
65     @IntoSet
providesBouncerSwipeTouchHandler( BouncerSwipeTouchHandler touchHandler)66     public static DreamTouchHandler providesBouncerSwipeTouchHandler(
67             BouncerSwipeTouchHandler touchHandler) {
68         return touchHandler;
69     }
70 
71     /**
72      * Provides {@link android.view.animation.AnimationUtils} for closing.
73      */
74     @Provides
75     @Named(SWIPE_TO_BOUNCER_FLING_ANIMATION_UTILS_CLOSING)
providesSwipeToBouncerFlingAnimationUtilsClosing( Provider<FlingAnimationUtils.Builder> flingAnimationUtilsBuilderProvider)76     public static FlingAnimationUtils providesSwipeToBouncerFlingAnimationUtilsClosing(
77             Provider<FlingAnimationUtils.Builder> flingAnimationUtilsBuilderProvider) {
78         return flingAnimationUtilsBuilderProvider.get()
79                 .reset()
80                 .setMaxLengthSeconds(
81                         ShadeViewController.FLING_CLOSING_MAX_LENGTH_SECONDS)
82                 .setSpeedUpFactor(ShadeViewController.FLING_SPEED_UP_FACTOR)
83                 .build();
84     }
85 
86     /**
87      * Provides {@link android.view.animation.AnimationUtils} for opening.
88      */
89     @Provides
90     @Named(SWIPE_TO_BOUNCER_FLING_ANIMATION_UTILS_OPENING)
providesSwipeToBouncerFlingAnimationUtilsOpening( Provider<FlingAnimationUtils.Builder> flingAnimationUtilsBuilderProvider)91     public static FlingAnimationUtils providesSwipeToBouncerFlingAnimationUtilsOpening(
92             Provider<FlingAnimationUtils.Builder> flingAnimationUtilsBuilderProvider) {
93         return flingAnimationUtilsBuilderProvider.get()
94                 .reset()
95                 .setMaxLengthSeconds(ShadeViewController.FLING_MAX_LENGTH_SECONDS)
96                 .setSpeedUpFactor(ShadeViewController.FLING_SPEED_UP_FACTOR)
97                 .build();
98     }
99 
100     /**
101      * Provides the region to start swipe gestures from.
102      */
103     @Provides
104     @Named(SWIPE_TO_BOUNCER_START_REGION)
providesSwipeToBouncerStartRegion(@ain Resources resources)105     public static float providesSwipeToBouncerStartRegion(@Main Resources resources) {
106         TypedValue typedValue = new TypedValue();
107         resources.getValue(R.dimen.dream_overlay_bouncer_start_region_screen_percentage,
108                 typedValue, true);
109         return typedValue.getFloat();
110     }
111 
112     /**
113      * Provides the default {@link BouncerSwipeTouchHandler.ValueAnimatorCreator}, which is simply
114      * a wrapper around {@link ValueAnimator}.
115      */
116     @Provides
providesValueAnimatorCreator()117     public static BouncerSwipeTouchHandler.ValueAnimatorCreator providesValueAnimatorCreator() {
118         return (start, finish) -> ValueAnimator.ofFloat(start, finish);
119     }
120 
121     /**
122      * Provides the default {@link BouncerSwipeTouchHandler.VelocityTrackerFactory}. which is a
123      * passthrough to {@link android.view.VelocityTracker}.
124      */
125     @Provides
providesVelocityTrackerFactory()126     public static BouncerSwipeTouchHandler.VelocityTrackerFactory providesVelocityTrackerFactory() {
127         return () -> VelocityTracker.obtain();
128     }
129 }
130