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

Commit ec34da8f authored by Jason Monk's avatar Jason Monk
Browse files

Add method for plugins to reference global dependencies

The new annotation versioning system allows plugins to share common
global interfaces that can be versioned on their own. To facilitate
this add a static method for plugins to get a hold of dependencies.
To get any dependency a plugin must @Requires it so it can be version
checked.

Test: Plugin that uses the new call.
Change-Id: I8e01260f8fbc14465502d506a7aa08212795ad9b
parent 56fee637
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -12,17 +12,21 @@
 * permissions and limitations under the License.
 */

package com.android.systemui;
package com.android.systemui.plugins;

import android.app.PendingIntent;
import android.content.Intent;

import com.android.systemui.plugins.annotations.ProvidesInterface;

/**
 * An interface to start activities. This is used as a callback from the views to
 * {@link PhoneStatusBar} to allow custom handling for starting the activity, i.e. dismissing the
 * Keyguard.
 */
@ProvidesInterface(version = ActivityStarter.VERSION)
public interface ActivityStarter {
    int VERSION = 1;

    void startPendingIntentDismissingKeyguard(PendingIntent intent);
    void startActivity(Intent intent, boolean dismissShade);
+31 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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 com.android.systemui.plugins.annotations.ProvidesInterface;

@ProvidesInterface(version = PluginDependency.VERSION)
public class PluginDependency {
    public static final int VERSION = 1;
    static DependencyProvider sProvider;

    public static <T> T get(Plugin p, Class<T> cls) {
        return sProvider.get(p, cls);
    }

    static abstract class DependencyProvider {
        abstract <T> T get(Plugin p, Class<T> cls);
    }
}
+2 −0
Original line number Diff line number Diff line
@@ -17,6 +17,8 @@ package com.android.systemui;
import android.app.PendingIntent;
import android.content.Intent;

import com.android.systemui.plugins.ActivityStarter;

/**
 * Single common instance of ActivityStarter that can be gotten and referenced from anywhere, but
 * delegates to an actual implementation such as StatusBar, assuming it exists.
+5 −1
Original line number Diff line number Diff line
@@ -25,8 +25,9 @@ import android.util.ArrayMap;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.Preconditions;
import com.android.systemui.assist.AssistManager;
import com.android.systemui.fragments.FragmentHostManager;
import com.android.systemui.fragments.FragmentService;
import com.android.systemui.plugins.ActivityStarter;
import com.android.systemui.plugins.PluginDependencyProvider;
import com.android.systemui.plugins.PluginManager;
import com.android.systemui.statusbar.phone.ConfigurationControllerImpl;
import com.android.systemui.statusbar.phone.DarkIconDispatcherImpl;
@@ -238,6 +239,9 @@ public class Dependency extends SystemUI {
        mProviders.put(ExtensionController.class, () ->
                new ExtensionControllerImpl());

        mProviders.put(PluginDependencyProvider.class, () ->
                new PluginDependencyProvider(get(PluginManager.class)));

        // Put all dependencies above here so the factory can override them if it wants.
        SystemUIFactory.getInstance().injectDependencies(mProviders, mContext);
    }
+50 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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.util.ArrayMap;

import com.android.systemui.Dependency;
import com.android.systemui.plugins.PluginDependency.DependencyProvider;

public class PluginDependencyProvider extends DependencyProvider {

    private final ArrayMap<Class<?>, Object> mDependencies = new ArrayMap<>();
    private final PluginManager mManager;

    public PluginDependencyProvider(PluginManager manager) {
        mManager = manager;
        PluginDependency.sProvider = this;
    }

    public <T> void allowPluginDependency(Class<T> cls) {
        allowPluginDependency(cls, Dependency.get(cls));
    }

    public <T> void allowPluginDependency(Class<T> cls, T obj) {
        mDependencies.put(cls, obj);
    }

    @Override
    <T> T get(Plugin p, Class<T> cls) {
        if (!mManager.dependsOn(p, cls)) {
            throw new IllegalArgumentException(p.getClass() + " does not depend on " + cls);
        }
        if (!mDependencies.containsKey(cls)) {
            throw new IllegalArgumentException("Unknown dependency " + cls);
        }
        return (T) mDependencies.get(cls);
    }
}
Loading