1 /*
2  * Copyright (C) 2024 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.ambient.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.ambient.touch.BouncerSwipeTouchHandler;
25 import com.android.systemui.ambient.touch.TouchHandler;
26 import com.android.systemui.dagger.qualifiers.Main;
27 import com.android.systemui.res.R;
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     public static final String MIN_BOUNCER_ZONE_SCREEN_PERCENTAGE =
50             "min_bouncer_zone_screen_percentage";
51 
52     /**
53      * The {@link android.view.animation.AnimationUtils} for animating the bouncer closing.
54      */
55     public static final String SWIPE_TO_BOUNCER_FLING_ANIMATION_UTILS_CLOSING =
56             "swipe_to_bouncer_fling_animation_utils_closing";
57 
58     /**
59      * The {@link android.view.animation.AnimationUtils} for animating the bouncer opening.
60      */
61     public static final String SWIPE_TO_BOUNCER_FLING_ANIMATION_UTILS_OPENING =
62                     "swipe_to_bouncer_fling_animation_utils_opening";
63 
64     /**
65      * Provides {@link BouncerSwipeTouchHandler} for inclusion in touch handling over the dream.
66      */
67     @Provides
68     @IntoSet
providesBouncerSwipeTouchHandler( BouncerSwipeTouchHandler touchHandler)69     public static TouchHandler providesBouncerSwipeTouchHandler(
70             BouncerSwipeTouchHandler touchHandler) {
71         return touchHandler;
72     }
73 
74     /**
75      * Provides {@link android.view.animation.AnimationUtils} for closing.
76      */
77     @Provides
78     @Named(SWIPE_TO_BOUNCER_FLING_ANIMATION_UTILS_CLOSING)
providesSwipeToBouncerFlingAnimationUtilsClosing( Provider<FlingAnimationUtils.Builder> flingAnimationUtilsBuilderProvider)79     public static FlingAnimationUtils providesSwipeToBouncerFlingAnimationUtilsClosing(
80             Provider<FlingAnimationUtils.Builder> flingAnimationUtilsBuilderProvider) {
81         return flingAnimationUtilsBuilderProvider.get()
82                 .reset()
83                 .setMaxLengthSeconds(
84                         ShadeViewController.FLING_CLOSING_MAX_LENGTH_SECONDS)
85                 .setSpeedUpFactor(ShadeViewController.FLING_SPEED_UP_FACTOR)
86                 .build();
87     }
88 
89     /**
90      * Provides {@link android.view.animation.AnimationUtils} for opening.
91      */
92     @Provides
93     @Named(SWIPE_TO_BOUNCER_FLING_ANIMATION_UTILS_OPENING)
providesSwipeToBouncerFlingAnimationUtilsOpening( Provider<FlingAnimationUtils.Builder> flingAnimationUtilsBuilderProvider)94     public static FlingAnimationUtils providesSwipeToBouncerFlingAnimationUtilsOpening(
95             Provider<FlingAnimationUtils.Builder> flingAnimationUtilsBuilderProvider) {
96         return flingAnimationUtilsBuilderProvider.get()
97                 .reset()
98                 .setMaxLengthSeconds(ShadeViewController.FLING_MAX_LENGTH_SECONDS)
99                 .setSpeedUpFactor(ShadeViewController.FLING_SPEED_UP_FACTOR)
100                 .build();
101     }
102 
103     /**
104      * Provides the region to start swipe gestures from.
105      */
106     @Provides
107     @Named(SWIPE_TO_BOUNCER_START_REGION)
providesSwipeToBouncerStartRegion(@ain Resources resources)108     public static float providesSwipeToBouncerStartRegion(@Main Resources resources) {
109         TypedValue typedValue = new TypedValue();
110         resources.getValue(R.dimen.dream_overlay_bouncer_start_region_screen_percentage,
111                 typedValue, true);
112         return typedValue.getFloat();
113     }
114 
115     /**
116      * Provides the minimum region to start wipe gestures from.
117      */
118     @Provides
119     @Named(MIN_BOUNCER_ZONE_SCREEN_PERCENTAGE)
providesMinBouncerZoneScreenPercentage(@ain Resources resources)120     public static float providesMinBouncerZoneScreenPercentage(@Main Resources resources) {
121         TypedValue typedValue = new TypedValue();
122         resources.getValue(R.dimen.dream_overlay_bouncer_min_region_screen_percentage,
123                 typedValue, true);
124         return typedValue.getFloat();
125     }
126 
127     /**
128      * Provides the default {@link BouncerSwipeTouchHandler.ValueAnimatorCreator}, which is simply
129      * a wrapper around {@link ValueAnimator}.
130      */
131     @Provides
providesValueAnimatorCreator()132     public static BouncerSwipeTouchHandler.ValueAnimatorCreator providesValueAnimatorCreator() {
133         return (start, finish) -> ValueAnimator.ofFloat(start, finish);
134     }
135 
136     /**
137      * Provides the default {@link BouncerSwipeTouchHandler.VelocityTrackerFactory}. which is a
138      * passthrough to {@link android.view.VelocityTracker}.
139      */
140     @Provides
providesVelocityTrackerFactory()141     public static BouncerSwipeTouchHandler.VelocityTrackerFactory providesVelocityTrackerFactory() {
142         return () -> VelocityTracker.obtain();
143     }
144 }
145