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