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 package com.android.systemui.smartspace.dagger
17 
18 import android.app.ActivityOptions
19 import android.app.PendingIntent
20 import android.content.Intent
21 import android.view.View
22 import android.view.ViewGroup
23 import com.android.systemui.plugins.ActivityStarter
24 import com.android.systemui.plugins.BcSmartspaceDataPlugin
25 import com.android.systemui.plugins.BcSmartspaceDataPlugin.UI_SURFACE_DREAM
26 import com.android.systemui.plugins.FalsingManager
27 import com.android.systemui.smartspace.dagger.SmartspaceViewComponent.SmartspaceViewModule.PLUGIN
28 import dagger.BindsInstance
29 import dagger.Module
30 import dagger.Provides
31 import dagger.Subcomponent
32 import javax.inject.Named
33 
34 @Subcomponent(modules = [SmartspaceViewComponent.SmartspaceViewModule::class])
35 interface SmartspaceViewComponent {
36     @Subcomponent.Factory
37     interface Factory {
38         fun create(
39             @BindsInstance parent: ViewGroup,
40             @BindsInstance @Named(PLUGIN) plugin: BcSmartspaceDataPlugin,
41             @BindsInstance onAttachListener: View.OnAttachStateChangeListener,
42             @BindsInstance viewWithCustomLayout: View? = null
43         ): SmartspaceViewComponent
44     }
45 
46     fun getView(): BcSmartspaceDataPlugin.SmartspaceView
47 
48     @Module
49     object SmartspaceViewModule {
50         const val PLUGIN = "plugin"
51 
52         @Provides
53         fun providesSmartspaceView(
54             activityStarter: ActivityStarter,
55             falsingManager: FalsingManager,
56             parent: ViewGroup,
57             @Named(PLUGIN) plugin: BcSmartspaceDataPlugin,
58             viewWithCustomLayout: View?,
59             onAttachListener: View.OnAttachStateChangeListener
60         ):
61                 BcSmartspaceDataPlugin.SmartspaceView {
62             val ssView = viewWithCustomLayout
63                     as? BcSmartspaceDataPlugin.SmartspaceView
64                     ?: plugin.getView(parent)
65             // Currently, this is only used to provide SmartspaceView on Dream surface.
66             ssView.setUiSurface(UI_SURFACE_DREAM)
67             ssView.registerDataProvider(plugin)
68 
69             ssView.setIntentStarter(object : BcSmartspaceDataPlugin.IntentStarter {
70                 override fun startIntent(view: View, intent: Intent, showOnLockscreen: Boolean) {
71                     activityStarter.startActivity(
72                             intent,
73                             true, /* dismissShade */
74                             null, /* launch animator */
75                             showOnLockscreen
76                     )
77                 }
78 
79                 override fun startPendingIntent(
80                         view: View,
81                         pi: PendingIntent,
82                         showOnLockscreen: Boolean
83                 ) {
84                     if (showOnLockscreen) {
85                         val options = ActivityOptions.makeBasic()
86                                 .setPendingIntentBackgroundActivityStartMode(
87                                         ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOWED)
88                                 .toBundle()
89                         pi.send(options)
90                     } else {
91                         activityStarter.startPendingIntentDismissingKeyguard(pi)
92                     }
93                 }
94             })
95             (ssView as View).addOnAttachStateChangeListener(onAttachListener)
96             ssView.setFalsingManager(falsingManager)
97             return ssView
98         }
99     }
100 }
101