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