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 androidx.lifecycle.ViewModelProvider; 22 import androidx.lifecycle.ViewModelStore; 23 24 import com.android.systemui.complication.Complication; 25 import com.android.systemui.complication.ComplicationCollectionViewModel; 26 import com.android.systemui.complication.ComplicationLayoutEngine; 27 import com.android.systemui.touch.TouchInsetManager; 28 29 import dagger.Module; 30 import dagger.Provides; 31 32 import java.lang.annotation.Documented; 33 import java.lang.annotation.Retention; 34 35 import javax.inject.Named; 36 import javax.inject.Scope; 37 /** 38 * Module for housing components related to rendering complications. 39 */ 40 @Module(includes = { 41 ComplicationHostViewModule.class, 42 }, subcomponents = { 43 ComplicationViewModelComponent.class, 44 }) 45 public interface ComplicationModule { 46 String SCOPED_COMPLICATIONS_MODEL = "scoped_complications_model"; 47 48 /** Scope annotation for singleton items within the {@link ComplicationModule}. */ 49 @Documented 50 @Retention(RUNTIME) 51 @Scope 52 @interface ComplicationScope {} 53 54 /** 55 * The complication collection is provided through this way to ensure that the instances are 56 * tied to the {@link ViewModelStore}. 57 */ 58 @Provides 59 @Named(SCOPED_COMPLICATIONS_MODEL) providesComplicationCollectionViewModel( ViewModelStore store, ComplicationCollectionViewModel viewModel)60 static ComplicationCollectionViewModel providesComplicationCollectionViewModel( 61 ViewModelStore store, ComplicationCollectionViewModel viewModel) { 62 final ViewModelProvider provider = new ViewModelProvider(store, 63 new DaggerViewModelProviderFactory(() -> viewModel)); 64 65 return provider.get(ComplicationCollectionViewModel.class); 66 } 67 68 /** 69 * Provides the visibility controller for display complications. 70 */ 71 @Provides providesVisibilityController( ComplicationLayoutEngine engine)72 static Complication.VisibilityController providesVisibilityController( 73 ComplicationLayoutEngine engine) { 74 return engine; 75 } 76 77 /** 78 * Provides a new touch inset session instance for complication logic. 79 */ 80 @Provides providesTouchInsetSession( TouchInsetManager manager)81 static TouchInsetManager.TouchInsetSession providesTouchInsetSession( 82 TouchInsetManager manager) { 83 return manager.createSession(); 84 } 85 } 86