• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.plugins;
18 
19 import android.app.NotificationManager;
20 import android.content.Context;
21 import android.content.pm.PackageManager;
22 import android.os.Build;
23 
24 import com.android.systemui.dagger.PluginModule;
25 import com.android.systemui.dagger.qualifiers.Main;
26 import com.android.systemui.res.R;
27 import com.android.systemui.shared.plugins.PluginActionManager;
28 import com.android.systemui.shared.plugins.PluginEnabler;
29 import com.android.systemui.shared.plugins.PluginInstance;
30 import com.android.systemui.shared.plugins.PluginManagerImpl;
31 import com.android.systemui.shared.plugins.PluginPrefs;
32 import com.android.systemui.shared.system.UncaughtExceptionPreHandlerManager;
33 import com.android.systemui.util.concurrency.GlobalConcurrencyModule;
34 import com.android.systemui.util.concurrency.ThreadFactory;
35 
36 import dagger.Binds;
37 import dagger.Module;
38 import dagger.Provides;
39 
40 import java.util.Arrays;
41 import java.util.List;
42 import java.util.concurrent.Executor;
43 
44 import javax.inject.Named;
45 import javax.inject.Singleton;
46 
47 /**
48  * Dagger Module for code related to plugins.
49  *
50  * Covers code both in com.android.systemui.plugins and code in
51  * com.android.systemui.shared.plugins.
52  */
53 @Module(includes = {GlobalConcurrencyModule.class})
54 public abstract class PluginsModule {
55     public static final String PLUGIN_THREAD = "plugin_thread";
56     public static final String PLUGIN_DEBUG = "plugin_debug";
57     public static final String PLUGIN_PRIVILEGED = "plugin_privileged";
58 
59     @Provides
60     @Named(PLUGIN_DEBUG)
providesPluginDebug()61     static boolean providesPluginDebug() {
62         return Build.IS_DEBUGGABLE;
63     }
64 
65     @Binds
bindsPluginEnablerImpl(PluginEnablerImpl impl)66     abstract PluginEnabler bindsPluginEnablerImpl(PluginEnablerImpl impl);
67 
68     @Provides
69     @Singleton
providesPluginInstanceFactory( @amedPLUGIN_PRIVILEGED) List<String> privilegedPlugins, @Named(PLUGIN_DEBUG) boolean isDebug)70     static PluginInstance.Factory providesPluginInstanceFactory(
71             @Named(PLUGIN_PRIVILEGED) List<String> privilegedPlugins,
72             @Named(PLUGIN_DEBUG) boolean isDebug) {
73         return new PluginInstance.Factory(
74                 PluginModule.class.getClassLoader(),
75                 new PluginInstance.InstanceFactory<>(),
76                 new PluginInstance.VersionCheckerImpl(),
77                 privilegedPlugins,
78                 isDebug);
79     }
80 
81     @Provides
82     @Singleton
providePluginInstanceManagerFactory(Context context, PackageManager packageManager, @Main Executor mainExecutor, @Named(PLUGIN_THREAD) Executor pluginExecutor, NotificationManager notificationManager, PluginEnabler pluginEnabler, @Named(PLUGIN_PRIVILEGED) List<String> privilegedPlugins, PluginInstance.Factory pluginInstanceFactory)83     static PluginActionManager.Factory providePluginInstanceManagerFactory(Context context,
84             PackageManager packageManager, @Main Executor mainExecutor,
85             @Named(PLUGIN_THREAD) Executor pluginExecutor,
86             NotificationManager notificationManager, PluginEnabler pluginEnabler,
87             @Named(PLUGIN_PRIVILEGED) List<String> privilegedPlugins,
88             PluginInstance.Factory pluginInstanceFactory) {
89         return new PluginActionManager.Factory(
90                 context, packageManager, mainExecutor, pluginExecutor,
91                 notificationManager, pluginEnabler, privilegedPlugins, pluginInstanceFactory);
92     }
93 
94     @Provides
95     @Singleton
96     @Named(PLUGIN_THREAD)
providesPluginExecutor(ThreadFactory threadFactory)97     static Executor providesPluginExecutor(ThreadFactory threadFactory) {
98         return threadFactory.buildExecutorOnNewThread("plugin");
99     }
100 
101     @Provides
102     @Singleton
providesPluginManager( Context context, PluginActionManager.Factory instanceManagerFactory, @Named(PLUGIN_DEBUG) boolean debug, UncaughtExceptionPreHandlerManager preHandlerManager, PluginEnabler pluginEnabler, PluginPrefs pluginPrefs, @Named(PLUGIN_PRIVILEGED) List<String> privilegedPlugins)103     static PluginManager providesPluginManager(
104             Context context,
105             PluginActionManager.Factory instanceManagerFactory,
106             @Named(PLUGIN_DEBUG) boolean debug,
107             UncaughtExceptionPreHandlerManager preHandlerManager,
108             PluginEnabler pluginEnabler,
109             PluginPrefs pluginPrefs,
110             @Named(PLUGIN_PRIVILEGED) List<String> privilegedPlugins) {
111         return new PluginManagerImpl(context, instanceManagerFactory, debug,
112                 preHandlerManager, pluginEnabler, pluginPrefs,
113                 privilegedPlugins);
114     }
115 
116     @Provides
providesPluginPrefs(Context context)117     static PluginPrefs providesPluginPrefs(Context context) {
118         return new PluginPrefs(context);
119     }
120 
121     @Provides
122     @Named(PLUGIN_PRIVILEGED)
providesPrivilegedPlugins(Context context)123     static List<String> providesPrivilegedPlugins(Context context) {
124         return Arrays.asList(context.getResources().getStringArray(R.array.config_pluginAllowlist));
125     }
126 }
127