1 /*
2  * Copyright (C) 2021 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.dagger;
18 
19 import android.annotation.Nullable;
20 import android.content.res.Resources;
21 import android.view.LayoutInflater;
22 import android.view.ViewGroup;
23 
24 import androidx.lifecycle.Lifecycle;
25 import androidx.lifecycle.LifecycleOwner;
26 
27 import com.android.internal.util.Preconditions;
28 import com.android.systemui.R;
29 import com.android.systemui.dagger.qualifiers.Main;
30 import com.android.systemui.dreams.DreamOverlayContainerView;
31 import com.android.systemui.dreams.DreamOverlayStatusBarView;
32 import com.android.systemui.dreams.touch.DreamTouchHandler;
33 import com.android.systemui.touch.TouchInsetManager;
34 
35 import dagger.Module;
36 import dagger.Provides;
37 import dagger.multibindings.ElementsIntoSet;
38 
39 import java.util.HashSet;
40 import java.util.Set;
41 
42 import javax.inject.Named;
43 
44 /** Dagger module for {@link DreamOverlayComponent}. */
45 @Module
46 public abstract class DreamOverlayModule {
47     public static final String DREAM_TOUCH_HANDLERS = "dream_touch_handlers";
48     public static final String DREAM_OVERLAY_CONTENT_VIEW = "dream_overlay_content_view";
49     public static final String MAX_BURN_IN_OFFSET = "max_burn_in_offset";
50     public static final String BURN_IN_PROTECTION_UPDATE_INTERVAL =
51             "burn_in_protection_update_interval";
52     public static final String MILLIS_UNTIL_FULL_JITTER = "millis_until_full_jitter";
53     public static final String DREAM_BLUR_RADIUS = "DREAM_BLUR_RADIUS";
54     public static final String DREAM_IN_BLUR_ANIMATION_DURATION = "dream_in_blur_anim_duration";
55     public static final String DREAM_IN_COMPLICATIONS_ANIMATION_DURATION =
56             "dream_in_complications_anim_duration";
57     public static final String DREAM_IN_TRANSLATION_Y_DISTANCE =
58             "dream_in_complications_translation_y";
59     public static final String DREAM_IN_TRANSLATION_Y_DURATION =
60             "dream_in_complications_translation_y_duration";
61 
62     /** */
63     @Provides
64     @DreamOverlayComponent.DreamOverlayScope
providesDreamOverlayContainerView( LayoutInflater layoutInflater)65     public static DreamOverlayContainerView providesDreamOverlayContainerView(
66             LayoutInflater layoutInflater) {
67         return Preconditions.checkNotNull((DreamOverlayContainerView)
68                 layoutInflater.inflate(R.layout.dream_overlay_container, null),
69                 "R.layout.dream_layout_container could not be properly inflated");
70     }
71 
72     /** */
73     @Provides
74     @DreamOverlayComponent.DreamOverlayScope
75     @Named(DREAM_OVERLAY_CONTENT_VIEW)
providesDreamOverlayContentView(DreamOverlayContainerView view)76     public static ViewGroup providesDreamOverlayContentView(DreamOverlayContainerView view) {
77         return Preconditions.checkNotNull(view.findViewById(R.id.dream_overlay_content),
78                 "R.id.dream_overlay_content must not be null");
79     }
80 
81     /** */
82     @Provides
providesTouchInsetSession( TouchInsetManager manager)83     public static TouchInsetManager.TouchInsetSession providesTouchInsetSession(
84             TouchInsetManager manager) {
85         return manager.createSession();
86     }
87 
88     /** */
89     @Provides
90     @DreamOverlayComponent.DreamOverlayScope
providesDreamOverlayStatusBarView( DreamOverlayContainerView view)91     public static DreamOverlayStatusBarView providesDreamOverlayStatusBarView(
92             DreamOverlayContainerView view) {
93         return Preconditions.checkNotNull(view.findViewById(R.id.dream_overlay_status_bar),
94                 "R.id.status_bar must not be null");
95     }
96 
97     /** */
98     @Provides
99     @DreamOverlayComponent.DreamOverlayScope
100     @Named(MAX_BURN_IN_OFFSET)
providesMaxBurnInOffset(@ain Resources resources)101     static int providesMaxBurnInOffset(@Main Resources resources) {
102         return resources.getDimensionPixelSize(R.dimen.default_burn_in_prevention_offset);
103     }
104 
105     /** */
106     @Provides
107     @Named(BURN_IN_PROTECTION_UPDATE_INTERVAL)
providesBurnInProtectionUpdateInterval(@ain Resources resources)108     static long providesBurnInProtectionUpdateInterval(@Main Resources resources) {
109         return resources.getInteger(
110                 R.integer.config_dreamOverlayBurnInProtectionUpdateIntervalMillis);
111     }
112 
113     /** */
114     @Provides
115     @Named(MILLIS_UNTIL_FULL_JITTER)
providesMillisUntilFullJitter(@ain Resources resources)116     static long providesMillisUntilFullJitter(@Main Resources resources) {
117         return resources.getInteger(R.integer.config_dreamOverlayMillisUntilFullJitter);
118     }
119 
120     /**
121      * The blur radius applied to the dream overlay at dream entry and exit.
122      */
123     @Provides
124     @Named(DREAM_BLUR_RADIUS)
providesDreamBlurRadius(@ain Resources resources)125     static int providesDreamBlurRadius(@Main Resources resources) {
126         return resources.getDimensionPixelSize(R.dimen.dream_overlay_anim_blur_radius);
127     }
128 
129     /**
130      * Duration in milliseconds of the dream in un-blur animation.
131      */
132     @Provides
133     @Named(DREAM_IN_BLUR_ANIMATION_DURATION)
providesDreamInBlurAnimationDuration(@ain Resources resources)134     static long providesDreamInBlurAnimationDuration(@Main Resources resources) {
135         return (long) resources.getInteger(R.integer.config_dreamOverlayInBlurDurationMs);
136     }
137 
138     /**
139      * Duration in milliseconds of the dream in complications fade-in animation.
140      */
141     @Provides
142     @Named(DREAM_IN_COMPLICATIONS_ANIMATION_DURATION)
providesDreamInComplicationsAnimationDuration(@ain Resources resources)143     static long providesDreamInComplicationsAnimationDuration(@Main Resources resources) {
144         return (long) resources.getInteger(R.integer.config_dreamOverlayInComplicationsDurationMs);
145     }
146 
147     /**
148      * Provides the number of pixels to translate complications when entering a dream.
149      */
150     @Provides
151     @Named(DREAM_IN_TRANSLATION_Y_DISTANCE)
152     @DreamOverlayComponent.DreamOverlayScope
providesDreamInComplicationsTranslationY(@ain Resources resources)153     static int providesDreamInComplicationsTranslationY(@Main Resources resources) {
154         return resources.getDimensionPixelSize(R.dimen.dream_overlay_entry_y_offset);
155     }
156 
157     /**
158      * Provides the duration in ms of the y-translation when dream enters.
159      */
160     @Provides
161     @Named(DREAM_IN_TRANSLATION_Y_DURATION)
162     @DreamOverlayComponent.DreamOverlayScope
providesDreamInComplicationsTranslationYDuration(@ain Resources resources)163     static long providesDreamInComplicationsTranslationYDuration(@Main Resources resources) {
164         return (long) resources.getInteger(R.integer.config_dreamOverlayInTranslationYDurationMs);
165     }
166 
167     @Provides
168     @DreamOverlayComponent.DreamOverlayScope
providesLifecycle(LifecycleOwner lifecycleOwner)169     static Lifecycle providesLifecycle(LifecycleOwner lifecycleOwner) {
170         return lifecycleOwner.getLifecycle();
171     }
172 
173     @Provides
174     @ElementsIntoSet
providesDreamTouchHandlers( @amedDREAM_TOUCH_HANDLERS) @ullable Set<DreamTouchHandler> touchHandlers)175     static Set<DreamTouchHandler> providesDreamTouchHandlers(
176             @Named(DREAM_TOUCH_HANDLERS) @Nullable Set<DreamTouchHandler> touchHandlers) {
177         return touchHandlers != null ? touchHandlers : new HashSet<>();
178     }
179 }
180