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 | |
| 17 | package com.android.systemui; |
| 18 | |
Dave Mankoff | eb593ae | 2019-09-04 11:31:55 -0400 | [diff] [blame] | 19 | import android.app.Activity; |
Dave Mankoff | 2ea5a83 | 2019-07-03 13:26:55 -0400 | [diff] [blame] | 20 | import android.app.Application; |
| 21 | import android.app.Service; |
Dave Mankoff | aefb346 | 2019-11-01 14:21:45 -0400 | [diff] [blame] | 22 | import android.content.BroadcastReceiver; |
Dave Mankoff | a4a7136 | 2019-08-27 14:40:05 -0400 | [diff] [blame] | 23 | import android.content.ContentProvider; |
| 24 | import android.content.Context; |
Dave Mankoff | 2ea5a83 | 2019-07-03 13:26:55 -0400 | [diff] [blame] | 25 | import android.content.Intent; |
| 26 | |
Dave Mankoff | a4a7136 | 2019-08-27 14:40:05 -0400 | [diff] [blame] | 27 | import androidx.annotation.NonNull; |
Dave Mankoff | eb593ae | 2019-09-04 11:31:55 -0400 | [diff] [blame] | 28 | import androidx.annotation.Nullable; |
Dave Mankoff | a4a7136 | 2019-08-27 14:40:05 -0400 | [diff] [blame] | 29 | import androidx.core.app.AppComponentFactory; |
Dave Mankoff | 2ea5a83 | 2019-07-03 13:26:55 -0400 | [diff] [blame] | 30 | |
Dave Mankoff | f473681 | 2019-10-18 17:25:50 -0400 | [diff] [blame] | 31 | import com.android.systemui.dagger.ContextComponentHelper; |
| 32 | |
Dave Mankoff | 2ea5a83 | 2019-07-03 13:26:55 -0400 | [diff] [blame] | 33 | import javax.inject.Inject; |
| 34 | |
| 35 | /** |
| 36 | * Implementation of AppComponentFactory that injects into constructors. |
Dave Mankoff | a4a7136 | 2019-08-27 14:40:05 -0400 | [diff] [blame] | 37 | * |
| 38 | * This class sets up dependency injection when creating our application. |
| 39 | * |
| 40 | * Services support dependency injection into their constructors. |
| 41 | * |
| 42 | * ContentProviders support injection into member variables - _not_ constructors. |
Dave Mankoff | 2ea5a83 | 2019-07-03 13:26:55 -0400 | [diff] [blame] | 43 | */ |
Dave Mankoff | a4a7136 | 2019-08-27 14:40:05 -0400 | [diff] [blame] | 44 | public class SystemUIAppComponentFactory extends AppComponentFactory { |
Dave Mankoff | 2ea5a83 | 2019-07-03 13:26:55 -0400 | [diff] [blame] | 45 | |
Dave Mankoff | 6117078 | 2019-09-10 13:28:42 -0400 | [diff] [blame] | 46 | private static final String TAG = "AppComponentFactory"; |
Dave Mankoff | 2ea5a83 | 2019-07-03 13:26:55 -0400 | [diff] [blame] | 47 | @Inject |
| 48 | public ContextComponentHelper mComponentHelper; |
| 49 | |
| 50 | public SystemUIAppComponentFactory() { |
| 51 | super(); |
| 52 | } |
| 53 | |
Dave Mankoff | a4a7136 | 2019-08-27 14:40:05 -0400 | [diff] [blame] | 54 | @NonNull |
Dave Mankoff | 2ea5a83 | 2019-07-03 13:26:55 -0400 | [diff] [blame] | 55 | @Override |
Dave Mankoff | a4a7136 | 2019-08-27 14:40:05 -0400 | [diff] [blame] | 56 | public Application instantiateApplicationCompat( |
| 57 | @NonNull ClassLoader cl, @NonNull String className) |
Dave Mankoff | 2ea5a83 | 2019-07-03 13:26:55 -0400 | [diff] [blame] | 58 | throws InstantiationException, IllegalAccessException, ClassNotFoundException { |
Dave Mankoff | a4a7136 | 2019-08-27 14:40:05 -0400 | [diff] [blame] | 59 | Application app = super.instantiateApplicationCompat(cl, className); |
Dave Mankoff | e229469 | 2019-08-14 11:53:13 -0400 | [diff] [blame] | 60 | if (app instanceof ContextInitializer) { |
| 61 | ((ContextInitializer) app).setContextAvailableCallback( |
Dave Mankoff | 2ea5a83 | 2019-07-03 13:26:55 -0400 | [diff] [blame] | 62 | context -> { |
| 63 | SystemUIFactory.createFromConfig(context); |
| 64 | SystemUIFactory.getInstance().getRootComponent().inject( |
| 65 | SystemUIAppComponentFactory.this); |
| 66 | } |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | return app; |
| 71 | } |
| 72 | |
Dave Mankoff | a4a7136 | 2019-08-27 14:40:05 -0400 | [diff] [blame] | 73 | @NonNull |
Dave Mankoff | 2ea5a83 | 2019-07-03 13:26:55 -0400 | [diff] [blame] | 74 | @Override |
Dave Mankoff | a4a7136 | 2019-08-27 14:40:05 -0400 | [diff] [blame] | 75 | public ContentProvider instantiateProviderCompat( |
| 76 | @NonNull ClassLoader cl, @NonNull String className) |
Dave Mankoff | 2ea5a83 | 2019-07-03 13:26:55 -0400 | [diff] [blame] | 77 | throws InstantiationException, IllegalAccessException, ClassNotFoundException { |
Dave Mankoff | a4a7136 | 2019-08-27 14:40:05 -0400 | [diff] [blame] | 78 | |
| 79 | ContentProvider contentProvider = super.instantiateProviderCompat(cl, className); |
Dave Mankoff | e229469 | 2019-08-14 11:53:13 -0400 | [diff] [blame] | 80 | if (contentProvider instanceof ContextInitializer) { |
| 81 | ((ContextInitializer) contentProvider).setContextAvailableCallback( |
Dave Mankoff | a4a7136 | 2019-08-27 14:40:05 -0400 | [diff] [blame] | 82 | context -> { |
| 83 | SystemUIFactory.createFromConfig(context); |
| 84 | SystemUIFactory.getInstance().getRootComponent().inject( |
| 85 | contentProvider); |
| 86 | } |
| 87 | ); |
Dave Mankoff | 2ea5a83 | 2019-07-03 13:26:55 -0400 | [diff] [blame] | 88 | } |
Dave Mankoff | a4a7136 | 2019-08-27 14:40:05 -0400 | [diff] [blame] | 89 | |
| 90 | return contentProvider; |
Dave Mankoff | 2ea5a83 | 2019-07-03 13:26:55 -0400 | [diff] [blame] | 91 | } |
| 92 | |
Dave Mankoff | a4a7136 | 2019-08-27 14:40:05 -0400 | [diff] [blame] | 93 | @NonNull |
| 94 | @Override |
Dave Mankoff | eb593ae | 2019-09-04 11:31:55 -0400 | [diff] [blame] | 95 | public Activity instantiateActivityCompat(@NonNull ClassLoader cl, @NonNull String className, |
| 96 | @Nullable Intent intent) |
| 97 | throws InstantiationException, IllegalAccessException, ClassNotFoundException { |
Dave Mankoff | 730f01a6 | 2019-09-25 10:18:36 -0400 | [diff] [blame] | 98 | if (mComponentHelper == null) { |
| 99 | // This shouldn't happen, but is seen on occasion. |
| 100 | // Bug filed against framework to take a look: http://b/141008541 |
| 101 | SystemUIFactory.getInstance().getRootComponent().inject( |
| 102 | SystemUIAppComponentFactory.this); |
| 103 | } |
Dave Mankoff | eb593ae | 2019-09-04 11:31:55 -0400 | [diff] [blame] | 104 | Activity activity = mComponentHelper.resolveActivity(className); |
| 105 | if (activity != null) { |
| 106 | return activity; |
| 107 | } |
| 108 | return super.instantiateActivityCompat(cl, className, intent); |
| 109 | } |
| 110 | |
| 111 | @NonNull |
| 112 | @Override |
Dave Mankoff | a4a7136 | 2019-08-27 14:40:05 -0400 | [diff] [blame] | 113 | public Service instantiateServiceCompat( |
| 114 | @NonNull ClassLoader cl, @NonNull String className, Intent intent) |
| 115 | throws InstantiationException, IllegalAccessException, ClassNotFoundException { |
Dave Mankoff | 6117078 | 2019-09-10 13:28:42 -0400 | [diff] [blame] | 116 | if (mComponentHelper == null) { |
Dave Mankoff | 66c4c72 | 2019-09-13 14:35:15 -0400 | [diff] [blame] | 117 | // This shouldn't happen, but does when a device is freshly formatted. |
| 118 | // Bug filed against framework to take a look: http://b/141008541 |
| 119 | SystemUIFactory.getInstance().getRootComponent().inject( |
| 120 | SystemUIAppComponentFactory.this); |
Dave Mankoff | 6117078 | 2019-09-10 13:28:42 -0400 | [diff] [blame] | 121 | } |
Dave Mankoff | a4a7136 | 2019-08-27 14:40:05 -0400 | [diff] [blame] | 122 | Service service = mComponentHelper.resolveService(className); |
| 123 | if (service != null) { |
| 124 | return service; |
Dave Mankoff | 2ea5a83 | 2019-07-03 13:26:55 -0400 | [diff] [blame] | 125 | } |
Dave Mankoff | a4a7136 | 2019-08-27 14:40:05 -0400 | [diff] [blame] | 126 | return super.instantiateServiceCompat(cl, className, intent); |
| 127 | } |
Dave Mankoff | 2ea5a83 | 2019-07-03 13:26:55 -0400 | [diff] [blame] | 128 | |
Dave Mankoff | aefb346 | 2019-11-01 14:21:45 -0400 | [diff] [blame] | 129 | @NonNull |
| 130 | @Override |
| 131 | public BroadcastReceiver instantiateReceiverCompat(@NonNull ClassLoader cl, |
| 132 | @NonNull String className, @Nullable Intent intent) |
| 133 | throws InstantiationException, IllegalAccessException, ClassNotFoundException { |
| 134 | if (mComponentHelper == null) { |
| 135 | // This shouldn't happen, but does when a device is freshly formatted. |
| 136 | // Bug filed against framework to take a look: http://b/141008541 |
| 137 | SystemUIFactory.getInstance().getRootComponent().inject( |
| 138 | SystemUIAppComponentFactory.this); |
| 139 | } |
| 140 | BroadcastReceiver receiver = mComponentHelper.resolveBroadcastReceiver(className); |
| 141 | if (receiver != null) { |
| 142 | return receiver; |
| 143 | } |
| 144 | |
| 145 | return super.instantiateReceiverCompat(cl, className, intent); |
| 146 | } |
| 147 | |
Dave Mankoff | e229469 | 2019-08-14 11:53:13 -0400 | [diff] [blame] | 148 | /** |
| 149 | * A callback that receives a Context when one is ready. |
| 150 | */ |
| 151 | public interface ContextAvailableCallback { |
Dave Mankoff | a4a7136 | 2019-08-27 14:40:05 -0400 | [diff] [blame] | 152 | void onContextAvailable(Context context); |
| 153 | } |
| 154 | |
Dave Mankoff | e229469 | 2019-08-14 11:53:13 -0400 | [diff] [blame] | 155 | /** |
| 156 | * Implemented in classes that get started by the system before a context is available. |
| 157 | */ |
| 158 | public interface ContextInitializer { |
Dave Mankoff | a4a7136 | 2019-08-27 14:40:05 -0400 | [diff] [blame] | 159 | void setContextAvailableCallback(ContextAvailableCallback callback); |
Dave Mankoff | 2ea5a83 | 2019-07-03 13:26:55 -0400 | [diff] [blame] | 160 | } |
| 161 | } |