Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 106a578e authored by Hawkwood Glazier's avatar Hawkwood Glazier
Browse files

More flexible logging from PluginInstance

I had a version of this where PluginCoreLib took a dependency on
SystemUILogLib, but that proved too difficult to get working. This
approach ended up being much simpler while accomplishing the same
objectives.

Flag: NONE
Bug: 279977555
Test: Existing tests
Change-Id: I74ee7fc7d11ac1326f3bf4ec1bb886c80d8e2d82
parent 566ca565
Loading
Loading
Loading
Loading
+4 −2
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import android.net.Uri
import android.os.UserHandle
import android.provider.Settings
import androidx.annotation.OpenForTesting
import com.android.systemui.log.LogBuffer
import com.android.systemui.log.core.LogLevel
import com.android.systemui.log.core.LogcatOnlyMessageBuffer
import com.android.systemui.log.core.Logger
@@ -120,8 +121,9 @@ open class ClockRegistry(
            override fun onPluginAttached(
                manager: PluginLifecycleManager<ClockProviderPlugin>
            ): Boolean {
                manager.isDebug = !keepAllLoaded

                manager.setLogFunc({ tag, msg ->
                    (clockBuffers?.infraMessageBuffer as LogBuffer?)?.log(tag, LogLevel.DEBUG, msg)
                })
                if (keepAllLoaded) {
                    // Always load new plugins if requested
                    return true
+4 −5
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@ package com.android.systemui.plugins;

import android.content.ComponentName;

import java.util.function.BiConsumer;

/**
 * Provides the ability for consumers to control plugin lifecycle.
 *
@@ -33,11 +35,8 @@ public interface PluginLifecycleManager<T extends Plugin> {
    /** Returns the currently loaded plugin instance (if plugin is loaded) */
    T getPlugin();

    /** Returns true if the lifecycle manager should log debug messages */
    boolean getIsDebug();

    /** Sets whether or not hte lifecycle manager should log debug messages */
    void setIsDebug(boolean debug);
    /** Log tag and messages will be sent to the provided Consumer */
    void setLogFunc(BiConsumer<String, String> logConsumer);

    /** returns true if the plugin is currently loaded */
    default boolean isLoaded() {
+15 −18
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ import dalvik.system.PathClassLoader;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Supplier;

/**
@@ -57,7 +58,7 @@ public class PluginInstance<T extends Plugin> implements PluginLifecycleManager
    private final PluginFactory<T> mPluginFactory;
    private final String mTag;

    private boolean mIsDebug = false;
    private BiConsumer<String, String> mLogConsumer = null;
    private Context mPluginContext;
    private T mPlugin;

@@ -86,17 +87,13 @@ public class PluginInstance<T extends Plugin> implements PluginLifecycleManager
        return mTag;
    }

    public boolean getIsDebug() {
        return mIsDebug;
    public void setLogFunc(BiConsumer logConsumer) {
        mLogConsumer = logConsumer;
    }

    public void setIsDebug(boolean debug) {
        mIsDebug = debug;
    }

    private void logDebug(String message) {
        if (mIsDebug) {
            Log.i(mTag, message);
    private void log(String message) {
        if (mLogConsumer != null) {
            mLogConsumer.accept(mTag, message);
        }
    }

@@ -105,19 +102,19 @@ public class PluginInstance<T extends Plugin> implements PluginLifecycleManager
        boolean loadPlugin = mListener.onPluginAttached(this);
        if (!loadPlugin) {
            if (mPlugin != null) {
                logDebug("onCreate: auto-unload");
                log("onCreate: auto-unload");
                unloadPlugin();
            }
            return;
        }

        if (mPlugin == null) {
            logDebug("onCreate auto-load");
            log("onCreate auto-load");
            loadPlugin();
            return;
        }

        logDebug("onCreate: load callbacks");
        log("onCreate: load callbacks");
        mPluginFactory.checkVersion(mPlugin);
        if (!(mPlugin instanceof PluginFragment)) {
            // Only call onCreate for plugins that aren't fragments, as fragments
@@ -129,7 +126,7 @@ public class PluginInstance<T extends Plugin> implements PluginLifecycleManager

    /** Alerts listener and plugin that the plugin is being shutdown. */
    public synchronized void onDestroy() {
        logDebug("onDestroy");
        log("onDestroy");
        unloadPlugin();
        mListener.onPluginDetached(this);
    }
@@ -145,7 +142,7 @@ public class PluginInstance<T extends Plugin> implements PluginLifecycleManager
     */
    public synchronized void loadPlugin() {
        if (mPlugin != null) {
            logDebug("Load request when already loaded");
            log("Load request when already loaded");
            return;
        }

@@ -157,7 +154,7 @@ public class PluginInstance<T extends Plugin> implements PluginLifecycleManager
            return;
        }

        logDebug("Loaded plugin; running callbacks");
        log("Loaded plugin; running callbacks");
        mPluginFactory.checkVersion(mPlugin);
        if (!(mPlugin instanceof PluginFragment)) {
            // Only call onCreate for plugins that aren't fragments, as fragments
@@ -174,11 +171,11 @@ public class PluginInstance<T extends Plugin> implements PluginLifecycleManager
     */
    public synchronized void unloadPlugin() {
        if (mPlugin == null) {
            logDebug("Unload request when already unloaded");
            log("Unload request when already unloaded");
            return;
        }

        logDebug("Unloading plugin, running callbacks");
        log("Unloading plugin, running callbacks");
        mListener.onPluginUnloaded(mPlugin, this);
        if (!(mPlugin instanceof PluginFragment)) {
            // Only call onDestroy for plugins that aren't fragments, as fragments
+2 −4
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ import com.android.systemui.plugins.PluginListener
import com.android.systemui.plugins.PluginManager
import com.android.systemui.util.mockito.argumentCaptor
import com.android.systemui.util.mockito.eq
import java.util.function.BiConsumer
import junit.framework.Assert.assertEquals
import junit.framework.Assert.fail
import kotlinx.coroutines.CoroutineDispatcher
@@ -100,10 +101,7 @@ class ClockRegistryTest : SysuiTestCase() {
        override fun toString() = "Manager[$tag]"
        override fun getPackage(): String = mComponentName.getPackageName()
        override fun getComponentName(): ComponentName = mComponentName

        private var isDebug: Boolean = false
        override fun getIsDebug(): Boolean = isDebug
        override fun setIsDebug(value: Boolean) { isDebug = value }
        override fun setLogFunc(func: BiConsumer<String, String>) { }

        override fun loadPlugin() {
            if (!mIsLoaded) {
+1 −1
Original line number Diff line number Diff line
@@ -112,7 +112,7 @@ public class PluginInstanceTest extends SysuiTestCase {
        mPluginInstance = mPluginInstanceFactory.create(
                mContext, mAppInfo, TEST_PLUGIN_COMPONENT_NAME,
                TestPlugin.class, mPluginListener);
        mPluginInstance.setIsDebug(true);
        mPluginInstance.setLogFunc((tag, msg) -> Log.d((String) tag, (String) msg));
        mPluginContext = new WeakReference<>(mPluginInstance.getPluginContext());
    }