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