1 /* 2 * Copyright (C) 2019 The Dagger Authors. 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 dagger.hilt.android.internal.managers; 18 19 import android.app.Activity; 20 import android.content.Context; 21 import android.content.ContextWrapper; 22 import android.os.Bundle; 23 import androidx.fragment.app.Fragment; 24 import android.view.LayoutInflater; 25 import dagger.hilt.EntryPoint; 26 import dagger.hilt.EntryPoints; 27 import dagger.hilt.InstallIn; 28 import dagger.hilt.android.components.ActivityComponent; 29 import dagger.hilt.android.internal.builders.FragmentComponentBuilder; 30 import dagger.hilt.internal.GeneratedComponentManager; 31 import dagger.hilt.internal.Preconditions; 32 33 /** 34 * Do not use except in Hilt generated code! 35 * 36 * <p>A manager for the creation of components that live in the Fragment. 37 * 38 * <p>Note: This class is not typed since its type in generated code is always <?> or <Object>. This 39 * is mainly due to the fact that we don't know the components at the time of generation, and 40 * because even the injector interface type is not a valid type if we have a hilt base class. 41 * 42 */ 43 public class FragmentComponentManager implements GeneratedComponentManager<Object> { 44 /** Entrypoint for {@link FragmentComponentBuilder}. */ 45 @EntryPoint 46 @InstallIn(ActivityComponent.class) 47 public interface FragmentComponentBuilderEntryPoint { fragmentComponentBuilder()48 FragmentComponentBuilder fragmentComponentBuilder(); 49 } 50 51 private volatile Object component; 52 private final Object componentLock = new Object(); 53 private final Fragment fragment; 54 FragmentComponentManager(Fragment fragment)55 public FragmentComponentManager(Fragment fragment) { 56 this.fragment = fragment; 57 } 58 59 @Override generatedComponent()60 public Object generatedComponent() { 61 if (component == null) { 62 synchronized (componentLock) { 63 if (component == null) { 64 component = createComponent(); 65 } 66 } 67 } 68 return component; 69 } 70 createComponent()71 private Object createComponent() { 72 Preconditions.checkNotNull( 73 fragment.getHost(), "Hilt Fragments must be attached before creating the component."); 74 Preconditions.checkState( 75 fragment.getHost() instanceof GeneratedComponentManager, 76 "Hilt Fragments must be attached to an @AndroidEntryPoint Activity. Found: %s", 77 fragment.getHost().getClass()); 78 79 validate(fragment); 80 81 return EntryPoints.get(fragment.getHost(), FragmentComponentBuilderEntryPoint.class) 82 .fragmentComponentBuilder() 83 .fragment(fragment) 84 .build(); 85 } 86 87 /** Returns the fragments bundle, creating a new one if none exists. */ initializeArguments(Fragment fragment)88 public static final void initializeArguments(Fragment fragment) { 89 Preconditions.checkNotNull(fragment); 90 if (fragment.getArguments() == null) { 91 fragment.setArguments(new Bundle()); 92 } 93 } 94 findActivity(Context context)95 public static final Context findActivity(Context context) { 96 while (context instanceof ContextWrapper 97 && !(context instanceof Activity)) { 98 context = ((ContextWrapper) context).getBaseContext(); 99 } 100 return context; 101 } 102 createContextWrapper(Context base, Fragment fragment)103 public static ContextWrapper createContextWrapper(Context base, Fragment fragment) { 104 return new ViewComponentManager.FragmentContextWrapper(base, fragment); 105 } 106 createContextWrapper( LayoutInflater baseInflater, Fragment fragment)107 public static ContextWrapper createContextWrapper( 108 LayoutInflater baseInflater, Fragment fragment) { 109 return new ViewComponentManager.FragmentContextWrapper(baseInflater, fragment); 110 } 111 112 /** Called immediately before component creation to allow validation on the Fragment. */ validate(Fragment fragment)113 protected void validate(Fragment fragment) { 114 } 115 } 116