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(), 74 "Hilt Fragments must be attached before creating the component."); 75 Preconditions.checkState( 76 fragment.getHost() instanceof GeneratedComponentManager, 77 "Hilt Fragments must be attached to an @AndroidEntryPoint Activity. Found: %s", 78 fragment.getHost().getClass()); 79 80 validate(fragment); 81 82 return EntryPoints.get(fragment.getHost(), FragmentComponentBuilderEntryPoint.class) 83 .fragmentComponentBuilder() 84 .fragment(fragment) 85 .build(); 86 } 87 88 /** Returns the fragments bundle, creating a new one if none exists. */ initializeArguments(Fragment fragment)89 public static final void initializeArguments(Fragment fragment) { 90 Preconditions.checkNotNull(fragment); 91 if (fragment.getArguments() == null) { 92 fragment.setArguments(new Bundle()); 93 } 94 } 95 findActivity(Context context)96 public static final Context findActivity(Context context) { 97 while (context instanceof ContextWrapper 98 && !(context instanceof Activity)) { 99 context = ((ContextWrapper) context).getBaseContext(); 100 } 101 return context; 102 } 103 createContextWrapper(Context base, Fragment fragment)104 public static ContextWrapper createContextWrapper(Context base, Fragment fragment) { 105 return new ViewComponentManager.FragmentContextWrapper(base, fragment); 106 } 107 createContextWrapper( LayoutInflater baseInflater, Fragment fragment)108 public static ContextWrapper createContextWrapper( 109 LayoutInflater baseInflater, Fragment fragment) { 110 return new ViewComponentManager.FragmentContextWrapper(baseInflater, fragment); 111 } 112 113 /** Called immediately before component creation to allow validation on the Fragment. */ validate(Fragment fragment)114 protected void validate(Fragment fragment) { 115 } 116 } 117