blob: d7822c9fc6dadf0cce553aa886ca660632014761 [file] [log] [blame]
Dave Mankoff2ea5a832019-07-03 13:26:55 -04001/*
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
Dave Mankofff4736812019-10-18 17:25:50 -040017package com.android.systemui.dagger;
Dave Mankoff2ea5a832019-07-03 13:26:55 -040018
Dave Mankoffeb593ae2019-09-04 11:31:55 -040019import android.app.Activity;
Dave Mankoffa4a71362019-08-27 14:40:05 -040020import android.app.Service;
21
Dave Mankofff4736812019-10-18 17:25:50 -040022import com.android.systemui.SystemUI;
23
Dave Mankoff2ea5a832019-07-03 13:26:55 -040024import java.util.Map;
25
26import javax.inject.Inject;
27import javax.inject.Provider;
Dave Mankoffaa8b7ae2019-09-04 18:03:21 -040028import javax.inject.Singleton;
Dave Mankoff2ea5a832019-07-03 13:26:55 -040029
30/**
31 * Used during Service and Activity instantiation to make them injectable.
32 */
Dave Mankoffaa8b7ae2019-09-04 18:03:21 -040033@Singleton
Dave Mankoff2ea5a832019-07-03 13:26:55 -040034public class ContextComponentResolver implements ContextComponentHelper {
Dave Mankoffeb593ae2019-09-04 11:31:55 -040035 private final Map<Class<?>, Provider<Activity>> mActivityCreators;
Dave Mankoffa4a71362019-08-27 14:40:05 -040036 private final Map<Class<?>, Provider<Service>> mServiceCreators;
Dave Mankoffaa8b7ae2019-09-04 18:03:21 -040037 private final Map<Class<?>, Provider<SystemUI>> mSystemUICreators;
Dave Mankoff2ea5a832019-07-03 13:26:55 -040038
39 @Inject
Dave Mankoffa4a71362019-08-27 14:40:05 -040040 ContextComponentResolver(
Dave Mankoffeb593ae2019-09-04 11:31:55 -040041 Map<Class<?>, Provider<Activity>> activityCreators,
Dave Mankoffaa8b7ae2019-09-04 18:03:21 -040042 Map<Class<?>, Provider<Service>> serviceCreators,
43 Map<Class<?>, Provider<SystemUI>> systemUICreators) {
Dave Mankoffeb593ae2019-09-04 11:31:55 -040044 mActivityCreators = activityCreators;
Dave Mankoffa4a71362019-08-27 14:40:05 -040045 mServiceCreators = serviceCreators;
Dave Mankoffaa8b7ae2019-09-04 18:03:21 -040046 mSystemUICreators = systemUICreators;
Dave Mankoff2ea5a832019-07-03 13:26:55 -040047 }
48
49 /**
Dave Mankoffeb593ae2019-09-04 11:31:55 -040050 * Looks up the Activity class name to see if Dagger has an instance of it.
51 */
52 @Override
53 public Activity resolveActivity(String className) {
54 return resolve(className, mActivityCreators);
55 }
56
57 /**
Dave Mankoffa4a71362019-08-27 14:40:05 -040058 * Looks up the Service class name to see if Dagger has an instance of it.
Dave Mankoff2ea5a832019-07-03 13:26:55 -040059 */
60 @Override
Dave Mankoffa4a71362019-08-27 14:40:05 -040061 public Service resolveService(String className) {
62 return resolve(className, mServiceCreators);
63 }
64
Dave Mankoffaa8b7ae2019-09-04 18:03:21 -040065 /**
66 * Looks up the SystemUI class name to see if Dagger has an instance of it.
67 */
68 @Override
69 public SystemUI resolveSystemUI(String className) {
70 return resolve(className, mSystemUICreators);
71 }
72
Dave Mankoffa4a71362019-08-27 14:40:05 -040073 private <T> T resolve(String className, Map<Class<?>, Provider<T>> creators) {
Dave Mankoffeb593ae2019-09-04 11:31:55 -040074 try {
75 Class<?> clazz = Class.forName(className);
76 Provider<T> provider = creators.get(clazz);
77 return provider == null ? null : provider.get();
78 } catch (ClassNotFoundException e) {
79 return null;
Dave Mankoff2ea5a832019-07-03 13:26:55 -040080 }
Dave Mankoff2ea5a832019-07-03 13:26:55 -040081 }
82}