1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15 package com.android.launcher3.uioverrides.plugins; 16 17 import static android.content.pm.PackageManager.MATCH_DISABLED_COMPONENTS; 18 19 import static com.android.launcher3.util.Executors.MODEL_EXECUTOR; 20 21 import android.app.NotificationManager; 22 import android.content.ComponentName; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.pm.ResolveInfo; 26 27 import com.android.launcher3.Utilities; 28 import com.android.launcher3.util.MainThreadInitializedObject; 29 import com.android.systemui.plugins.Plugin; 30 import com.android.systemui.plugins.PluginListener; 31 import com.android.systemui.plugins.PluginManager; 32 import com.android.systemui.shared.plugins.PluginActionManager; 33 import com.android.systemui.shared.plugins.PluginInstance; 34 import com.android.systemui.shared.plugins.PluginManagerImpl; 35 import com.android.systemui.shared.plugins.PluginPrefs; 36 import com.android.systemui.shared.system.UncaughtExceptionPreHandlerManager; 37 38 import java.io.PrintWriter; 39 import java.util.ArrayList; 40 import java.util.Collections; 41 import java.util.List; 42 import java.util.Set; 43 44 public class PluginManagerWrapper { 45 46 public static final MainThreadInitializedObject<PluginManagerWrapper> INSTANCE = 47 new MainThreadInitializedObject<>(PluginManagerWrapper::new); 48 49 public static final String PLUGIN_CHANGED = PluginManager.PLUGIN_CHANGED; 50 51 private static final UncaughtExceptionPreHandlerManager UNCAUGHT_EXCEPTION_PRE_HANDLER_MANAGER = 52 new UncaughtExceptionPreHandlerManager(); 53 54 private final Context mContext; 55 private final PluginManager mPluginManager; 56 private final PluginEnablerImpl mPluginEnabler; 57 PluginManagerWrapper(Context c)58 private PluginManagerWrapper(Context c) { 59 mContext = c; 60 mPluginEnabler = new PluginEnablerImpl(c); 61 List<String> privilegedPlugins = Collections.emptyList(); 62 PluginInstance.Factory instanceFactory = new PluginInstance.Factory( 63 getClass().getClassLoader(), new PluginInstance.InstanceFactory<>(), 64 new PluginInstance.VersionCheckerImpl(), privilegedPlugins, 65 Utilities.IS_DEBUG_DEVICE); 66 PluginActionManager.Factory instanceManagerFactory = new PluginActionManager.Factory( 67 c, c.getPackageManager(), c.getMainExecutor(), MODEL_EXECUTOR, 68 c.getSystemService(NotificationManager.class), mPluginEnabler, 69 privilegedPlugins, instanceFactory); 70 71 mPluginManager = new PluginManagerImpl(c, instanceManagerFactory, 72 Utilities.IS_DEBUG_DEVICE, 73 UNCAUGHT_EXCEPTION_PRE_HANDLER_MANAGER, mPluginEnabler, 74 new PluginPrefs(c), privilegedPlugins); 75 } 76 getPluginEnabler()77 public PluginEnablerImpl getPluginEnabler() { 78 return mPluginEnabler; 79 } 80 81 /** */ addPluginListener( PluginListener<T> listener, Class<T> pluginClass)82 public <T extends Plugin> void addPluginListener( 83 PluginListener<T> listener, Class<T> pluginClass) { 84 addPluginListener(listener, pluginClass, false); 85 } 86 87 /** */ addPluginListener( PluginListener<T> listener, Class<T> pluginClass, boolean allowMultiple)88 public <T extends Plugin> void addPluginListener( 89 PluginListener<T> listener, Class<T> pluginClass, boolean allowMultiple) { 90 mPluginManager.addPluginListener(listener, pluginClass, allowMultiple); 91 } 92 removePluginListener(PluginListener<? extends Plugin> listener)93 public void removePluginListener(PluginListener<? extends Plugin> listener) { 94 mPluginManager.removePluginListener(listener); 95 } 96 getPluginActions()97 public Set<String> getPluginActions() { 98 return new PluginPrefs(mContext).getPluginList(); 99 } 100 101 /** 102 * Returns the string key used to store plugin enabled/disabled setting 103 */ pluginEnabledKey(ComponentName cn)104 public static String pluginEnabledKey(ComponentName cn) { 105 return PluginEnablerImpl.pluginEnabledKey(cn); 106 } 107 hasPlugins(Context context)108 public static boolean hasPlugins(Context context) { 109 return PluginPrefs.hasPlugins(context); 110 } 111 dump(PrintWriter pw)112 public void dump(PrintWriter pw) { 113 final List<ComponentName> enabledPlugins = new ArrayList<>(); 114 final List<ComponentName> disabledPlugins = new ArrayList<>(); 115 for (String action : getPluginActions()) { 116 for (ResolveInfo resolveInfo : mContext.getPackageManager().queryIntentServices( 117 new Intent(action), MATCH_DISABLED_COMPONENTS)) { 118 ComponentName installedPlugin = new ComponentName( 119 resolveInfo.serviceInfo.packageName, resolveInfo.serviceInfo.name); 120 if (mPluginEnabler.isEnabled(installedPlugin)) { 121 enabledPlugins.add(installedPlugin); 122 } else { 123 disabledPlugins.add(installedPlugin); 124 } 125 } 126 } 127 128 pw.println("PluginManager:"); 129 pw.println(" numEnabledPlugins=" + enabledPlugins.size()); 130 pw.println(" numDisabledPlugins=" + disabledPlugins.size()); 131 pw.println(" enabledPlugins=" + enabledPlugins); 132 pw.println(" disabledPlugins=" + disabledPlugins); 133 } 134 } 135