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.complication.dagger;
18 
19 import static java.lang.annotation.RetentionPolicy.RUNTIME;
20 
21 import android.content.Context;
22 import android.content.res.Resources;
23 import android.graphics.drawable.Drawable;
24 import android.view.LayoutInflater;
25 import android.widget.ImageView;
26 
27 import com.android.systemui.complication.DreamHomeControlsComplication;
28 import com.android.systemui.res.R;
29 import com.android.systemui.shared.shadow.DoubleShadowIconDrawable;
30 import com.android.systemui.shared.shadow.DoubleShadowTextHelper;
31 
32 import dagger.BindsInstance;
33 import dagger.Module;
34 import dagger.Provides;
35 import dagger.Subcomponent;
36 
37 import java.lang.annotation.Documented;
38 import java.lang.annotation.Retention;
39 
40 import javax.inject.Named;
41 import javax.inject.Scope;
42 
43 /**
44  * Responsible for generating dependencies for the {@link DreamHomeControlsComplication}.
45  */
46 @Subcomponent(modules = DreamHomeControlsComplicationComponent.DreamHomeControlsModule.class)
47 @DreamHomeControlsComplicationComponent.DreamHomeControlsComplicationScope
48 public interface DreamHomeControlsComplicationComponent {
49     /**
50      * Creates a view holder for the home controls complication.
51      */
getViewHolder()52     DreamHomeControlsComplication.DreamHomeControlsChipViewHolder getViewHolder();
53 
54     /**
55      * Scope of the home controls complication.
56      */
57     @Documented
58     @Retention(RUNTIME)
59     @Scope
60     @interface DreamHomeControlsComplicationScope {}
61 
62     /**
63      * Factory that generates a {@link DreamHomeControlsComplicationComponent}.
64      */
65     @Subcomponent.Factory
66     interface Factory {
create(@indsInstance Resources resources)67         DreamHomeControlsComplicationComponent create(@BindsInstance Resources resources);
68     }
69 
70     /**
71      * Scoped injected values for the {@link DreamHomeControlsComplicationComponent}.
72      */
73     @Module
74     interface DreamHomeControlsModule {
75         String DREAM_HOME_CONTROLS_CHIP_VIEW = "dream_home_controls_chip_view";
76         String DREAM_HOME_CONTROLS_BACKGROUND_DRAWABLE = "dream_home_controls_background_drawable";
77 
78         /**
79          * Provides the dream home controls chip view.
80          */
81         @Provides
82         @DreamHomeControlsComplicationScope
83         @Named(DREAM_HOME_CONTROLS_CHIP_VIEW)
provideHomeControlsChipView( LayoutInflater layoutInflater, @Named(DREAM_HOME_CONTROLS_BACKGROUND_DRAWABLE) Drawable backgroundDrawable)84         static ImageView provideHomeControlsChipView(
85                 LayoutInflater layoutInflater,
86                 @Named(DREAM_HOME_CONTROLS_BACKGROUND_DRAWABLE) Drawable backgroundDrawable) {
87             final ImageView chip =
88                     (ImageView) layoutInflater.inflate(R.layout.dream_overlay_home_controls_chip,
89                             null, false);
90             chip.setBackground(backgroundDrawable);
91 
92             return chip;
93         }
94 
95         @Provides
96         @DreamHomeControlsComplicationScope
97         @Named(DREAM_HOME_CONTROLS_BACKGROUND_DRAWABLE)
providesHomeControlsBackground(Context context, Resources resources)98         static Drawable providesHomeControlsBackground(Context context, Resources resources) {
99             return new DoubleShadowIconDrawable(createShadowInfo(
100                             resources,
101                             R.dimen.dream_overlay_bottom_affordance_key_text_shadow_radius,
102                             R.dimen.dream_overlay_bottom_affordance_key_text_shadow_dx,
103                             R.dimen.dream_overlay_bottom_affordance_key_text_shadow_dy,
104                             R.dimen.dream_overlay_bottom_affordance_key_shadow_alpha
105                     ),
106                     createShadowInfo(
107                             resources,
108                             R.dimen.dream_overlay_bottom_affordance_ambient_text_shadow_radius,
109                             R.dimen.dream_overlay_bottom_affordance_ambient_text_shadow_dx,
110                             R.dimen.dream_overlay_bottom_affordance_ambient_text_shadow_dy,
111                             R.dimen.dream_overlay_bottom_affordance_ambient_shadow_alpha
112                     ),
113                     resources.getDrawable(R.drawable.dream_overlay_bottom_affordance_bg),
114                     resources.getDimensionPixelOffset(
115                             R.dimen.dream_overlay_bottom_affordance_width),
116                     resources.getDimensionPixelSize(R.dimen.dream_overlay_bottom_affordance_inset)
117             );
118         }
119 
createShadowInfo(Resources resources, int blurId, int offsetXId, int offsetYId, int alphaId)120         private static DoubleShadowTextHelper.ShadowInfo createShadowInfo(Resources resources,
121                 int blurId, int offsetXId, int offsetYId, int alphaId) {
122 
123             return new DoubleShadowTextHelper.ShadowInfo(
124                     resources.getDimension(blurId),
125                     resources.getDimension(offsetXId),
126                     resources.getDimension(offsetYId),
127                     resources.getFloat(alphaId)
128             );
129         }
130     }
131 }
132