blob: cacbf969e5622da18e85a3ee0610ab6861524d4a [file] [log] [blame]
Dave Mankoff2ea5a832019-07-03 13:26:55 -04001/*
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
17package com.android.systemui;
18
Dave Mankoffeb593ae2019-09-04 11:31:55 -040019import android.app.Activity;
Dave Mankoff2ea5a832019-07-03 13:26:55 -040020import android.app.Application;
21import android.app.Service;
Dave Mankoffaefb3462019-11-01 14:21:45 -040022import android.content.BroadcastReceiver;
Dave Mankoffa4a71362019-08-27 14:40:05 -040023import android.content.ContentProvider;
24import android.content.Context;
Dave Mankoff2ea5a832019-07-03 13:26:55 -040025import android.content.Intent;
26
Dave Mankoffa4a71362019-08-27 14:40:05 -040027import androidx.annotation.NonNull;
Dave Mankoffeb593ae2019-09-04 11:31:55 -040028import androidx.annotation.Nullable;
Dave Mankoffa4a71362019-08-27 14:40:05 -040029import androidx.core.app.AppComponentFactory;
Dave Mankoff2ea5a832019-07-03 13:26:55 -040030
Dave Mankofff4736812019-10-18 17:25:50 -040031import com.android.systemui.dagger.ContextComponentHelper;
32
Dave Mankoff2ea5a832019-07-03 13:26:55 -040033import javax.inject.Inject;
34
35/**
36 * Implementation of AppComponentFactory that injects into constructors.
Dave Mankoffa4a71362019-08-27 14:40:05 -040037 *
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 Mankoff2ea5a832019-07-03 13:26:55 -040043 */
Dave Mankoffa4a71362019-08-27 14:40:05 -040044public class SystemUIAppComponentFactory extends AppComponentFactory {
Dave Mankoff2ea5a832019-07-03 13:26:55 -040045
Dave Mankoff61170782019-09-10 13:28:42 -040046 private static final String TAG = "AppComponentFactory";
Dave Mankoff2ea5a832019-07-03 13:26:55 -040047 @Inject
48 public ContextComponentHelper mComponentHelper;
49
50 public SystemUIAppComponentFactory() {
51 super();
52 }
53
Dave Mankoffa4a71362019-08-27 14:40:05 -040054 @NonNull
Dave Mankoff2ea5a832019-07-03 13:26:55 -040055 @Override
Dave Mankoffa4a71362019-08-27 14:40:05 -040056 public Application instantiateApplicationCompat(
57 @NonNull ClassLoader cl, @NonNull String className)
Dave Mankoff2ea5a832019-07-03 13:26:55 -040058 throws InstantiationException, IllegalAccessException, ClassNotFoundException {
Dave Mankoffa4a71362019-08-27 14:40:05 -040059 Application app = super.instantiateApplicationCompat(cl, className);
Dave Mankoffe2294692019-08-14 11:53:13 -040060 if (app instanceof ContextInitializer) {
61 ((ContextInitializer) app).setContextAvailableCallback(
Dave Mankoff2ea5a832019-07-03 13:26:55 -040062 context -> {
63 SystemUIFactory.createFromConfig(context);
64 SystemUIFactory.getInstance().getRootComponent().inject(
65 SystemUIAppComponentFactory.this);
66 }
67 );
68 }
69
70 return app;
71 }
72
Dave Mankoffa4a71362019-08-27 14:40:05 -040073 @NonNull
Dave Mankoff2ea5a832019-07-03 13:26:55 -040074 @Override
Dave Mankoffa4a71362019-08-27 14:40:05 -040075 public ContentProvider instantiateProviderCompat(
76 @NonNull ClassLoader cl, @NonNull String className)
Dave Mankoff2ea5a832019-07-03 13:26:55 -040077 throws InstantiationException, IllegalAccessException, ClassNotFoundException {
Dave Mankoffa4a71362019-08-27 14:40:05 -040078
79 ContentProvider contentProvider = super.instantiateProviderCompat(cl, className);
Dave Mankoffe2294692019-08-14 11:53:13 -040080 if (contentProvider instanceof ContextInitializer) {
81 ((ContextInitializer) contentProvider).setContextAvailableCallback(
Dave Mankoffa4a71362019-08-27 14:40:05 -040082 context -> {
83 SystemUIFactory.createFromConfig(context);
84 SystemUIFactory.getInstance().getRootComponent().inject(
85 contentProvider);
86 }
87 );
Dave Mankoff2ea5a832019-07-03 13:26:55 -040088 }
Dave Mankoffa4a71362019-08-27 14:40:05 -040089
90 return contentProvider;
Dave Mankoff2ea5a832019-07-03 13:26:55 -040091 }
92
Dave Mankoffa4a71362019-08-27 14:40:05 -040093 @NonNull
94 @Override
Dave Mankoffeb593ae2019-09-04 11:31:55 -040095 public Activity instantiateActivityCompat(@NonNull ClassLoader cl, @NonNull String className,
96 @Nullable Intent intent)
97 throws InstantiationException, IllegalAccessException, ClassNotFoundException {
Dave Mankoff730f01a62019-09-25 10:18:36 -040098 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 Mankoffeb593ae2019-09-04 11:31:55 -0400104 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 Mankoffa4a71362019-08-27 14:40:05 -0400113 public Service instantiateServiceCompat(
114 @NonNull ClassLoader cl, @NonNull String className, Intent intent)
115 throws InstantiationException, IllegalAccessException, ClassNotFoundException {
Dave Mankoff61170782019-09-10 13:28:42 -0400116 if (mComponentHelper == null) {
Dave Mankoff66c4c722019-09-13 14:35:15 -0400117 // 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 Mankoff61170782019-09-10 13:28:42 -0400121 }
Dave Mankoffa4a71362019-08-27 14:40:05 -0400122 Service service = mComponentHelper.resolveService(className);
123 if (service != null) {
124 return service;
Dave Mankoff2ea5a832019-07-03 13:26:55 -0400125 }
Dave Mankoffa4a71362019-08-27 14:40:05 -0400126 return super.instantiateServiceCompat(cl, className, intent);
127 }
Dave Mankoff2ea5a832019-07-03 13:26:55 -0400128
Dave Mankoffaefb3462019-11-01 14:21:45 -0400129 @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 Mankoffe2294692019-08-14 11:53:13 -0400148 /**
149 * A callback that receives a Context when one is ready.
150 */
151 public interface ContextAvailableCallback {
Dave Mankoffa4a71362019-08-27 14:40:05 -0400152 void onContextAvailable(Context context);
153 }
154
Dave Mankoffe2294692019-08-14 11:53:13 -0400155 /**
156 * Implemented in classes that get started by the system before a context is available.
157 */
158 public interface ContextInitializer {
Dave Mankoffa4a71362019-08-27 14:40:05 -0400159 void setContextAvailableCallback(ContextAvailableCallback callback);
Dave Mankoff2ea5a832019-07-03 13:26:55 -0400160 }
161}