Jason Monk | ea54e8a | 2018-12-20 10:01:48 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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.util; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.util.ArrayMap; |
| 21 | import android.util.AttributeSet; |
| 22 | import android.view.InflateException; |
| 23 | import android.view.LayoutInflater; |
| 24 | import android.view.View; |
| 25 | |
| 26 | import com.android.systemui.SystemUIFactory; |
Fabian Kozynski | a48d2d0 | 2019-02-27 11:36:02 -0500 | [diff] [blame] | 27 | import com.android.systemui.qs.QSCarrierGroup; |
Jason Monk | 7a56b83 | 2018-12-27 13:45:51 -0500 | [diff] [blame] | 28 | import com.android.systemui.qs.QSFooterImpl; |
Jason Monk | ea54e8a | 2018-12-20 10:01:48 -0500 | [diff] [blame] | 29 | import com.android.systemui.qs.QuickStatusBarHeader; |
Gus Prevas | 59ec2ff | 2018-12-28 16:20:28 -0500 | [diff] [blame] | 30 | import com.android.systemui.statusbar.notification.stack.NotificationStackScrollLayout; |
Selim Cinek | d5921a8 | 2019-01-29 19:04:08 -0800 | [diff] [blame] | 31 | import com.android.systemui.statusbar.phone.NotificationPanelView; |
Jason Monk | ea54e8a | 2018-12-20 10:01:48 -0500 | [diff] [blame] | 32 | |
| 33 | import java.lang.reflect.InvocationTargetException; |
| 34 | import java.lang.reflect.Method; |
| 35 | import java.lang.reflect.Modifier; |
| 36 | |
| 37 | import javax.inject.Inject; |
| 38 | import javax.inject.Named; |
| 39 | import javax.inject.Singleton; |
| 40 | |
| 41 | import dagger.Module; |
| 42 | import dagger.Provides; |
| 43 | import dagger.Subcomponent; |
| 44 | |
| 45 | /** |
| 46 | * Manages inflation that requires dagger injection. |
| 47 | * See docs/dagger.md for details. |
| 48 | */ |
| 49 | @Singleton |
| 50 | public class InjectionInflationController { |
| 51 | |
| 52 | public static final String VIEW_CONTEXT = "view_context"; |
| 53 | private final ViewCreator mViewCreator; |
| 54 | private final ArrayMap<String, Method> mInjectionMap = new ArrayMap<>(); |
| 55 | private final LayoutInflater.Factory2 mFactory = new InjectionFactory(); |
| 56 | |
| 57 | @Inject |
| 58 | public InjectionInflationController(SystemUIFactory.SystemUIRootComponent rootComponent) { |
| 59 | mViewCreator = rootComponent.createViewCreator(); |
| 60 | initInjectionMap(); |
| 61 | } |
| 62 | |
| 63 | ArrayMap<String, Method> getInjectionMap() { |
| 64 | return mInjectionMap; |
| 65 | } |
| 66 | |
| 67 | ViewCreator getFragmentCreator() { |
| 68 | return mViewCreator; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Wraps a {@link LayoutInflater} to support creating dagger injected views. |
| 73 | * See docs/dagger.md for details. |
| 74 | */ |
| 75 | public LayoutInflater injectable(LayoutInflater inflater) { |
| 76 | LayoutInflater ret = inflater.cloneInContext(inflater.getContext()); |
| 77 | ret.setPrivateFactory(mFactory); |
| 78 | return ret; |
| 79 | } |
| 80 | |
| 81 | private void initInjectionMap() { |
| 82 | for (Method method : ViewInstanceCreator.class.getDeclaredMethods()) { |
| 83 | if (View.class.isAssignableFrom(method.getReturnType()) |
| 84 | && (method.getModifiers() & Modifier.PUBLIC) != 0) { |
| 85 | mInjectionMap.put(method.getReturnType().getName(), method); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * The subcomponent of dagger that holds all views that need injection. |
| 92 | */ |
| 93 | @Subcomponent |
| 94 | public interface ViewCreator { |
| 95 | /** |
| 96 | * Creates another subcomponent to actually generate the view. |
| 97 | */ |
| 98 | ViewInstanceCreator createInstanceCreator(ViewAttributeProvider attributeProvider); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Secondary sub-component that actually creates the views. |
| 103 | * |
| 104 | * Having two subcomponents lets us hide the complexity of providing the named context |
| 105 | * and AttributeSet from the SystemUIRootComponent, instead we have one subcomponent that |
| 106 | * creates a new ViewInstanceCreator any time we need to inflate a view. |
| 107 | */ |
| 108 | @Subcomponent(modules = ViewAttributeProvider.class) |
| 109 | public interface ViewInstanceCreator { |
| 110 | /** |
| 111 | * Creates the QuickStatusBarHeader. |
| 112 | */ |
| 113 | QuickStatusBarHeader createQsHeader(); |
Jason Monk | 7a56b83 | 2018-12-27 13:45:51 -0500 | [diff] [blame] | 114 | /** |
| 115 | * Creates the QSFooterImpl. |
| 116 | */ |
| 117 | QSFooterImpl createQsFooter(); |
Gus Prevas | 59ec2ff | 2018-12-28 16:20:28 -0500 | [diff] [blame] | 118 | |
| 119 | /** |
| 120 | * Creates the NotificationStackScrollLayout. |
| 121 | */ |
| 122 | NotificationStackScrollLayout createNotificationStackScrollLayout(); |
Selim Cinek | d5921a8 | 2019-01-29 19:04:08 -0800 | [diff] [blame] | 123 | |
| 124 | /** |
| 125 | * Creates the NotificationPanelView. |
| 126 | */ |
| 127 | NotificationPanelView createPanelView(); |
Fabian Kozynski | a48d2d0 | 2019-02-27 11:36:02 -0500 | [diff] [blame] | 128 | |
| 129 | /** |
| 130 | * Creates the QSCarrierGroup |
| 131 | */ |
| 132 | QSCarrierGroup createQSCarrierGroup(); |
Jason Monk | ea54e8a | 2018-12-20 10:01:48 -0500 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Module for providing view-specific constructor objects. |
| 137 | */ |
| 138 | @Module |
| 139 | public class ViewAttributeProvider { |
| 140 | private final Context mContext; |
| 141 | private final AttributeSet mAttrs; |
| 142 | |
| 143 | private ViewAttributeProvider(Context context, AttributeSet attrs) { |
| 144 | mContext = context; |
| 145 | mAttrs = attrs; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Provides the view-themed context (as opposed to the global sysui application context). |
| 150 | */ |
| 151 | @Provides |
| 152 | @Named(VIEW_CONTEXT) |
| 153 | public Context provideContext() { |
| 154 | return mContext; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Provides the AttributeSet for the current view being inflated. |
| 159 | */ |
| 160 | @Provides |
| 161 | public AttributeSet provideAttributeSet() { |
| 162 | return mAttrs; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | private class InjectionFactory implements LayoutInflater.Factory2 { |
| 167 | |
| 168 | @Override |
| 169 | public View onCreateView(String name, Context context, AttributeSet attrs) { |
| 170 | Method creationMethod = mInjectionMap.get(name); |
| 171 | if (creationMethod != null) { |
| 172 | ViewAttributeProvider provider = new ViewAttributeProvider(context, attrs); |
| 173 | try { |
| 174 | return (View) creationMethod.invoke( |
| 175 | mViewCreator.createInstanceCreator(provider)); |
| 176 | } catch (IllegalAccessException e) { |
| 177 | throw new InflateException("Could not inflate " + name, e); |
| 178 | } catch (InvocationTargetException e) { |
| 179 | throw new InflateException("Could not inflate " + name, e); |
| 180 | } |
| 181 | } |
| 182 | return null; |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public View onCreateView(View parent, String name, Context context, AttributeSet attrs) { |
| 187 | return onCreateView(name, context, attrs); |
| 188 | } |
| 189 | } |
| 190 | } |