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;
18 
19 import static com.android.systemui.complication.dagger.RegisteredComplicationsModule.DREAM_SMARTSPACE_LAYOUT_PARAMS;
20 
21 import android.content.Context;
22 import android.os.Parcelable;
23 import android.view.View;
24 import android.view.ViewGroup;
25 import android.widget.FrameLayout;
26 
27 import com.android.systemui.CoreStartable;
28 import com.android.systemui.dagger.qualifiers.SystemUser;
29 import com.android.systemui.dreams.DreamOverlayStateController;
30 import com.android.systemui.dreams.smartspace.DreamSmartspaceController;
31 import com.android.systemui.flags.FeatureFlags;
32 import com.android.systemui.flags.Flags;
33 import com.android.systemui.plugins.BcSmartspaceDataPlugin;
34 import com.android.systemui.shared.condition.Monitor;
35 import com.android.systemui.util.condition.ConditionalCoreStartable;
36 
37 import java.util.List;
38 
39 import javax.inject.Inject;
40 import javax.inject.Named;
41 import javax.inject.Provider;
42 
43 /**
44  * {@link SmartSpaceComplication} embodies the SmartSpace view found on the lockscreen as a
45  * {@link Complication}
46  */
47 public class SmartSpaceComplication implements Complication {
48     private final Provider<SmartSpaceComplicationViewHolder> mViewHolderProvider;
49 
50     @Inject
SmartSpaceComplication(Provider<SmartSpaceComplicationViewHolder> viewHolderProvider)51     public SmartSpaceComplication(Provider<SmartSpaceComplicationViewHolder> viewHolderProvider) {
52         mViewHolderProvider = viewHolderProvider;
53     }
54 
55     @Override
createView(ComplicationViewModel model)56     public ViewHolder createView(ComplicationViewModel model) {
57         return mViewHolderProvider.get();
58     }
59 
60     @Override
getRequiredTypeAvailability()61     public int getRequiredTypeAvailability() {
62         return COMPLICATION_TYPE_SMARTSPACE;
63     }
64 
65     /**
66      * {@link CoreStartable} responsbile for registering {@link SmartSpaceComplication} with
67      * SystemUI.
68      */
69     public static class Registrant extends ConditionalCoreStartable {
70         private final DreamSmartspaceController mSmartSpaceController;
71         private final DreamOverlayStateController mDreamOverlayStateController;
72         private final SmartSpaceComplication mComplication;
73         private final FeatureFlags mFeatureFlags;
74         private final DreamOverlayStateController.Callback mStateControllerCallback =
75                 new DreamOverlayStateController.Callback() {
76                     @Override
77                     public void onStateChanged() {
78                         if (mDreamOverlayStateController.isOverlayActive()) {
79                             mSmartSpaceController.addListener(mSmartspaceListener);
80                         } else {
81                             mSmartSpaceController.removeListener(mSmartspaceListener);
82                             mDreamOverlayStateController.removeComplication(mComplication);
83                         }
84                     }
85                 };
86 
87         private final BcSmartspaceDataPlugin.SmartspaceTargetListener mSmartspaceListener =
88                 new BcSmartspaceDataPlugin.SmartspaceTargetListener() {
89             @Override
90             public void onSmartspaceTargetsUpdated(List<? extends Parcelable> targets) {
91                 mDreamOverlayStateController.addComplication(mComplication);
92             }
93         };
94 
95         /**
96          * Default constructor for {@link SmartSpaceComplication}.
97          */
98         @Inject
Registrant( DreamOverlayStateController dreamOverlayStateController, SmartSpaceComplication smartSpaceComplication, DreamSmartspaceController smartSpaceController, @SystemUser Monitor monitor, FeatureFlags featureFlags)99         public Registrant(
100                 DreamOverlayStateController dreamOverlayStateController,
101                 SmartSpaceComplication smartSpaceComplication,
102                 DreamSmartspaceController smartSpaceController,
103                 @SystemUser Monitor monitor,
104                 FeatureFlags featureFlags) {
105             super(monitor);
106             mDreamOverlayStateController = dreamOverlayStateController;
107             mComplication = smartSpaceComplication;
108             mSmartSpaceController = smartSpaceController;
109             mFeatureFlags = featureFlags;
110         }
111 
112         @Override
onStart()113         public void onStart() {
114             if (mFeatureFlags.isEnabled(Flags.HIDE_SMARTSPACE_ON_DREAM_OVERLAY)) {
115                 return;
116             }
117 
118             mDreamOverlayStateController.addCallback(mStateControllerCallback);
119         }
120     }
121 
122     static class SmartSpaceComplicationViewHolder implements ViewHolder {
123         private View mView = null;
124         private final DreamSmartspaceController mSmartSpaceController;
125         private final Context mContext;
126         private final ComplicationLayoutParams mLayoutParams;
127 
128         @Inject
SmartSpaceComplicationViewHolder( Context context, DreamSmartspaceController smartSpaceController, @Named(DREAM_SMARTSPACE_LAYOUT_PARAMS) ComplicationLayoutParams layoutParams)129         protected SmartSpaceComplicationViewHolder(
130                 Context context,
131                 DreamSmartspaceController smartSpaceController,
132                 @Named(DREAM_SMARTSPACE_LAYOUT_PARAMS) ComplicationLayoutParams layoutParams) {
133             mSmartSpaceController = smartSpaceController;
134             mContext = context;
135             mLayoutParams = layoutParams;
136         }
137 
138         @Override
getView()139         public View getView() {
140             if (mView != null) {
141                 return mView;
142             }
143             final FrameLayout smartSpaceContainer = new FrameLayout(mContext);
144             smartSpaceContainer.addView(
145                     mSmartSpaceController.buildAndConnectView(smartSpaceContainer),
146                     new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
147                             ViewGroup.LayoutParams.WRAP_CONTENT));
148 
149             mView = smartSpaceContainer;
150             return smartSpaceContainer;
151         }
152 
153         @Override
getLayoutParams()154         public ComplicationLayoutParams getLayoutParams() {
155             return mLayoutParams;
156         }
157     }
158 }
159