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

Commit 32c55437 authored by Tony Wickham's avatar Tony Wickham
Browse files

Refactor code to PluginEnabler

SystemUI uses PackageManager to enable/disable plugin components.
Launcher cannot do this, so we abstract this logic into an interface;
Launcher will have a different implementation.

Test: "atest com.android.systemui.shared.plugins"
  build succeeds and all tests pass

Bug: 115877296
Change-Id: Ie6f11572026dafa369c99722d63d4daa0ea44fb5
parent 01b46f8f
Loading
Loading
Loading
Loading
+25 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package com.android.systemui.shared.plugins;

import android.content.ComponentName;

/**
 * Enables and disables plugins.
 */
public interface PluginEnabler {
    void setEnabled(ComponentName component, boolean enabled);
    boolean isEnabled(ComponentName component);
}
+4 −2
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@

package com.android.systemui.shared.plugins;

import android.annotation.Nullable;
import android.content.Context;
import android.os.Looper;

@@ -26,9 +27,10 @@ public interface PluginInitializer {

    /**
     * This Runnable is run on the bg looper during initialization of {@link PluginManagerImpl}.
     * It can be null.
     */
    Runnable getBgInitCallback();
    @Nullable Runnable getBgInitCallback();

    String[] getWhitelistedPlugins(Context context);

    PluginEnabler getPluginEnabler(Context context);
}
+7 −6
Original line number Diff line number Diff line
@@ -159,10 +159,8 @@ public class PluginInstanceManager<T extends Plugin> {
        // plugin, if the plugin causing a crash cannot be identified, they are all disabled
        // assuming one of them must be bad.
        Log.w(TAG, "Disabling plugin " + info.mPackage + "/" + info.mClass);
        mPm.setComponentEnabledSetting(
                new ComponentName(info.mPackage, info.mClass),
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);
        mManager.getPluginEnabler().setEnabled(new ComponentName(info.mPackage, info.mClass),
                false);
    }

    public <T> boolean dependsOn(Plugin p, Class<T> cls) {
@@ -280,8 +278,7 @@ public class PluginInstanceManager<T extends Plugin> {
            if (pkgName != null) {
                intent.setPackage(pkgName);
            }
            List<ResolveInfo> result =
                    mPm.queryIntentServices(intent, 0);
            List<ResolveInfo> result = mPm.queryIntentServices(intent, 0);
            if (DEBUG) Log.d(TAG, "Found " + result.size() + " plugins");
            if (result.size() > 1 && !mAllowMultiple) {
                // TODO: Show warning.
@@ -306,6 +303,10 @@ public class PluginInstanceManager<T extends Plugin> {
                Log.w(TAG, "Plugin cannot be loaded on production build: " + component);
                return null;
            }
            if (!mManager.getPluginEnabler().isEnabled(component)) {
                if (DEBUG) Log.d(TAG, "Plugin is not enabled, aborting load: " + component);
                return null;
            }
            String pkg = component.getPackageName();
            String cls = component.getClassName();
            try {
+8 −5
Original line number Diff line number Diff line
@@ -41,12 +41,11 @@ import android.widget.Toast;

import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;

import com.android.systemui.plugins.Plugin;
import com.android.systemui.plugins.PluginListener;
import com.android.systemui.plugins.annotations.ProvidesInterface;
import com.android.systemui.shared.plugins.PluginInstanceManager.PluginContextWrapper;
import com.android.systemui.shared.plugins.PluginInstanceManager.PluginInfo;
import com.android.systemui.plugins.annotations.ProvidesInterface;

import dalvik.system.PathClassLoader;

@@ -74,6 +73,7 @@ public class PluginManagerImpl extends BroadcastReceiver implements PluginManage
    private final PluginInstanceManagerFactory mFactory;
    private final boolean isDebuggable;
    private final PluginPrefs mPluginPrefs;
    private final PluginEnabler mPluginEnabler;
    private ClassLoaderFilter mParentClassLoader;
    private boolean mListening;
    private boolean mHasOneShot;
@@ -94,6 +94,7 @@ public class PluginManagerImpl extends BroadcastReceiver implements PluginManage
        isDebuggable = debuggable;
        mWhitelistedPlugins.addAll(Arrays.asList(initializer.getWhitelistedPlugins(mContext)));
        mPluginPrefs = new PluginPrefs(mContext);
        mPluginEnabler = initializer.getPluginEnabler(mContext);

        PluginExceptionHandler uncaughtExceptionHandler = new PluginExceptionHandler(
                defaultHandler);
@@ -109,6 +110,10 @@ public class PluginManagerImpl extends BroadcastReceiver implements PluginManage
        return mWhitelistedPlugins.toArray(new String[0]);
    }

    public PluginEnabler getPluginEnabler() {
        return mPluginEnabler;
    }

    public <T extends Plugin> T getOneShotPlugin(Class<T> cls) {
        ProvidesInterface info = cls.getDeclaredAnnotation(ProvidesInterface.class);
        if (info == null) {
@@ -202,9 +207,7 @@ public class PluginManagerImpl extends BroadcastReceiver implements PluginManage
            Uri uri = intent.getData();
            ComponentName component = ComponentName.unflattenFromString(
                    uri.toString().substring(10));
            mContext.getPackageManager().setComponentEnabledSetting(component,
                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                    PackageManager.DONT_KILL_APP);
            getPluginEnabler().setEnabled(component, false);
            mContext.getSystemService(NotificationManager.class).cancel(component.getClassName(),
                    SystemMessage.NOTE_PLUGIN);
        } else {
+48 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package com.android.systemui.plugins;

import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageManager;

import com.android.internal.annotations.VisibleForTesting;
import com.android.systemui.shared.plugins.PluginEnabler;

public class PluginEnablerImpl implements PluginEnabler {

    final private PackageManager mPm;

    public PluginEnablerImpl(Context context) {
        this(context.getPackageManager());
    }

    @VisibleForTesting public PluginEnablerImpl(PackageManager pm) {
        mPm = pm;
    }

    @Override
    public void setEnabled(ComponentName component, boolean enabled) {
        final int desiredState = enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
                : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
        mPm.setComponentEnabledSetting(component, desiredState, PackageManager.DONT_KILL_APP);
    }

    @Override
    public boolean isEnabled(ComponentName component) {
        return mPm.getComponentEnabledSetting(component)
                != PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
    }
}
Loading