1 /* 2 * Copyright (C) 2020 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.migration; 18 19 import android.app.Service; 20 import android.content.BroadcastReceiver; 21 import androidx.annotation.NonNull; 22 import androidx.fragment.app.Fragment; 23 import android.view.View; 24 import androidx.activity.ComponentActivity; 25 import dagger.hilt.android.internal.migration.InjectedByHilt; 26 import dagger.hilt.internal.Preconditions; 27 28 /** 29 * Utility methods for validating if an {@link dagger.hilt.android.AndroidEntryPoint}-annotated 30 * class that is also annotated with {@link OptionalInject} was injected by Hilt. 31 * 32 * @see OptionalInject 33 */ 34 public final class OptionalInjectCheck { 35 36 /** 37 * Returns true if the Activity was injected by Hilt. 38 * 39 * @throws IllegalArgumentException if the given instance is not an AndroidEntryPoint nor is 40 * annotated with {@link OptionalInject}. 41 */ wasInjectedByHilt(@onNull ComponentActivity activity)42 public static boolean wasInjectedByHilt(@NonNull ComponentActivity activity) { 43 return check(activity); 44 } 45 46 /** 47 * Returns true if the BroadcastReceiver was injected by Hilt. 48 * 49 * @throws IllegalArgumentException if the given instance is not an AndroidEntryPoint nor is 50 * annotated with {@link OptionalInject}. 51 */ wasInjectedByHilt(@onNull BroadcastReceiver broadcastReceiver)52 public static boolean wasInjectedByHilt(@NonNull BroadcastReceiver broadcastReceiver) { 53 return check(broadcastReceiver); 54 } 55 56 /** 57 * Returns true if the Fragment was injected by Hilt. 58 * 59 * @throws IllegalArgumentException if the given instance is not an AndroidEntryPoint nor is 60 * annotated with {@link OptionalInject}. 61 */ wasInjectedByHilt(@onNull Fragment fragment)62 public static boolean wasInjectedByHilt(@NonNull Fragment fragment) { 63 return check(fragment); 64 } 65 66 /** 67 * Returns true if the Service was injected by Hilt. 68 * 69 * @throws IllegalArgumentException if the given instance is not an AndroidEntryPoint nor is 70 * annotated with {@link OptionalInject}. 71 */ wasInjectedByHilt(@onNull Service service)72 public static boolean wasInjectedByHilt(@NonNull Service service) { 73 return check(service); 74 } 75 76 /** 77 * Returns true if the View was injected by Hilt. 78 * 79 * @throws IllegalArgumentException if the given instance is not an AndroidEntryPoint nor is 80 * annotated with {@link OptionalInject}. 81 */ wasInjectedByHilt(@onNull View view)82 public static boolean wasInjectedByHilt(@NonNull View view) { 83 return check(view); 84 } 85 check(@onNull Object obj)86 private static boolean check(@NonNull Object obj) { 87 Preconditions.checkNotNull(obj); 88 Preconditions.checkArgument( 89 obj instanceof InjectedByHilt, 90 "'%s' is not an optionally injected android entry point. Check that you have annotated" 91 + " the class with both @AndroidEntryPoint and @OptionalInject.", 92 obj.getClass()); 93 return ((InjectedByHilt) obj).wasInjectedByHilt(); 94 } 95 OptionalInjectCheck()96 private OptionalInjectCheck() {} 97 } 98