1 /* 2 * Copyright (C) 2019 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.dagger; 18 19 import android.app.Activity; 20 import android.app.Service; 21 import android.content.BroadcastReceiver; 22 23 import com.android.systemui.SystemUI; 24 import com.android.systemui.recents.RecentsImplementation; 25 26 import java.util.Map; 27 28 import javax.inject.Inject; 29 import javax.inject.Provider; 30 31 /** 32 * Used during Service and Activity instantiation to make them injectable. 33 */ 34 @SysUISingleton 35 public class ContextComponentResolver implements ContextComponentHelper { 36 private final Map<Class<?>, Provider<Activity>> mActivityCreators; 37 private final Map<Class<?>, Provider<Service>> mServiceCreators; 38 private final Map<Class<?>, Provider<SystemUI>> mSystemUICreators; 39 private final Map<Class<?>, Provider<RecentsImplementation>> mRecentsCreators; 40 private final Map<Class<?>, Provider<BroadcastReceiver>> mBroadcastReceiverCreators; 41 42 @Inject ContextComponentResolver(Map<Class<?>, Provider<Activity>> activityCreators, Map<Class<?>, Provider<Service>> serviceCreators, Map<Class<?>, Provider<SystemUI>> systemUICreators, Map<Class<?>, Provider<RecentsImplementation>> recentsCreators, Map<Class<?>, Provider<BroadcastReceiver>> broadcastReceiverCreators)43 ContextComponentResolver(Map<Class<?>, Provider<Activity>> activityCreators, 44 Map<Class<?>, Provider<Service>> serviceCreators, 45 Map<Class<?>, Provider<SystemUI>> systemUICreators, 46 Map<Class<?>, Provider<RecentsImplementation>> recentsCreators, 47 Map<Class<?>, Provider<BroadcastReceiver>> broadcastReceiverCreators) { 48 mActivityCreators = activityCreators; 49 mServiceCreators = serviceCreators; 50 mSystemUICreators = systemUICreators; 51 mRecentsCreators = recentsCreators; 52 mBroadcastReceiverCreators = broadcastReceiverCreators; 53 } 54 55 /** 56 * Looks up the Activity class name to see if Dagger has an instance of it. 57 */ 58 @Override resolveActivity(String className)59 public Activity resolveActivity(String className) { 60 return resolve(className, mActivityCreators); 61 } 62 63 /** 64 * Looks up the BroadcastReceiver class name to see if Dagger has an instance of it. 65 */ 66 @Override resolveBroadcastReceiver(String className)67 public BroadcastReceiver resolveBroadcastReceiver(String className) { 68 return resolve(className, mBroadcastReceiverCreators); 69 } 70 71 /** 72 * Looks up the RecentsImplementation class name to see if Dagger has an instance of it. 73 */ 74 @Override resolveRecents(String className)75 public RecentsImplementation resolveRecents(String className) { 76 return resolve(className, mRecentsCreators); 77 } 78 79 /** 80 * Looks up the Service class name to see if Dagger has an instance of it. 81 */ 82 @Override resolveService(String className)83 public Service resolveService(String className) { 84 return resolve(className, mServiceCreators); 85 } 86 87 /** 88 * Looks up the SystemUI class name to see if Dagger has an instance of it. 89 */ 90 @Override resolveSystemUI(String className)91 public SystemUI resolveSystemUI(String className) { 92 return resolve(className, mSystemUICreators); 93 } 94 resolve(String className, Map<Class<?>, Provider<T>> creators)95 private <T> T resolve(String className, Map<Class<?>, Provider<T>> creators) { 96 try { 97 Class<?> clazz = Class.forName(className); 98 Provider<T> provider = creators.get(clazz); 99 return provider == null ? null : provider.get(); 100 } catch (ClassNotFoundException e) { 101 return null; 102 } 103 } 104 } 105