Dave Mankoff | 2ea5a83 | 2019-07-03 13:26:55 -0400 | [diff] [blame] | 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 | |
Dave Mankoff | f473681 | 2019-10-18 17:25:50 -0400 | [diff] [blame^] | 17 | package com.android.systemui.dagger; |
Dave Mankoff | 2ea5a83 | 2019-07-03 13:26:55 -0400 | [diff] [blame] | 18 | |
Dave Mankoff | eb593ae | 2019-09-04 11:31:55 -0400 | [diff] [blame] | 19 | import android.app.Activity; |
Dave Mankoff | a4a7136 | 2019-08-27 14:40:05 -0400 | [diff] [blame] | 20 | import android.app.Service; |
| 21 | |
Dave Mankoff | f473681 | 2019-10-18 17:25:50 -0400 | [diff] [blame^] | 22 | import com.android.systemui.SystemUI; |
| 23 | |
Dave Mankoff | 2ea5a83 | 2019-07-03 13:26:55 -0400 | [diff] [blame] | 24 | import java.util.Map; |
| 25 | |
| 26 | import javax.inject.Inject; |
| 27 | import javax.inject.Provider; |
Dave Mankoff | aa8b7ae | 2019-09-04 18:03:21 -0400 | [diff] [blame] | 28 | import javax.inject.Singleton; |
Dave Mankoff | 2ea5a83 | 2019-07-03 13:26:55 -0400 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Used during Service and Activity instantiation to make them injectable. |
| 32 | */ |
Dave Mankoff | aa8b7ae | 2019-09-04 18:03:21 -0400 | [diff] [blame] | 33 | @Singleton |
Dave Mankoff | 2ea5a83 | 2019-07-03 13:26:55 -0400 | [diff] [blame] | 34 | public class ContextComponentResolver implements ContextComponentHelper { |
Dave Mankoff | eb593ae | 2019-09-04 11:31:55 -0400 | [diff] [blame] | 35 | private final Map<Class<?>, Provider<Activity>> mActivityCreators; |
Dave Mankoff | a4a7136 | 2019-08-27 14:40:05 -0400 | [diff] [blame] | 36 | private final Map<Class<?>, Provider<Service>> mServiceCreators; |
Dave Mankoff | aa8b7ae | 2019-09-04 18:03:21 -0400 | [diff] [blame] | 37 | private final Map<Class<?>, Provider<SystemUI>> mSystemUICreators; |
Dave Mankoff | 2ea5a83 | 2019-07-03 13:26:55 -0400 | [diff] [blame] | 38 | |
| 39 | @Inject |
Dave Mankoff | a4a7136 | 2019-08-27 14:40:05 -0400 | [diff] [blame] | 40 | ContextComponentResolver( |
Dave Mankoff | eb593ae | 2019-09-04 11:31:55 -0400 | [diff] [blame] | 41 | Map<Class<?>, Provider<Activity>> activityCreators, |
Dave Mankoff | aa8b7ae | 2019-09-04 18:03:21 -0400 | [diff] [blame] | 42 | Map<Class<?>, Provider<Service>> serviceCreators, |
| 43 | Map<Class<?>, Provider<SystemUI>> systemUICreators) { |
Dave Mankoff | eb593ae | 2019-09-04 11:31:55 -0400 | [diff] [blame] | 44 | mActivityCreators = activityCreators; |
Dave Mankoff | a4a7136 | 2019-08-27 14:40:05 -0400 | [diff] [blame] | 45 | mServiceCreators = serviceCreators; |
Dave Mankoff | aa8b7ae | 2019-09-04 18:03:21 -0400 | [diff] [blame] | 46 | mSystemUICreators = systemUICreators; |
Dave Mankoff | 2ea5a83 | 2019-07-03 13:26:55 -0400 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | /** |
Dave Mankoff | eb593ae | 2019-09-04 11:31:55 -0400 | [diff] [blame] | 50 | * 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 Mankoff | a4a7136 | 2019-08-27 14:40:05 -0400 | [diff] [blame] | 58 | * Looks up the Service class name to see if Dagger has an instance of it. |
Dave Mankoff | 2ea5a83 | 2019-07-03 13:26:55 -0400 | [diff] [blame] | 59 | */ |
| 60 | @Override |
Dave Mankoff | a4a7136 | 2019-08-27 14:40:05 -0400 | [diff] [blame] | 61 | public Service resolveService(String className) { |
| 62 | return resolve(className, mServiceCreators); |
| 63 | } |
| 64 | |
Dave Mankoff | aa8b7ae | 2019-09-04 18:03:21 -0400 | [diff] [blame] | 65 | /** |
| 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 Mankoff | a4a7136 | 2019-08-27 14:40:05 -0400 | [diff] [blame] | 73 | private <T> T resolve(String className, Map<Class<?>, Provider<T>> creators) { |
Dave Mankoff | eb593ae | 2019-09-04 11:31:55 -0400 | [diff] [blame] | 74 | 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 Mankoff | 2ea5a83 | 2019-07-03 13:26:55 -0400 | [diff] [blame] | 80 | } |
Dave Mankoff | 2ea5a83 | 2019-07-03 13:26:55 -0400 | [diff] [blame] | 81 | } |
| 82 | } |