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