1 /* 2 * Copyright (C) 2017 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.systemui.plugins; 16 17 import android.util.ArrayMap; 18 19 import com.android.systemui.Dependency; 20 import com.android.systemui.plugins.PluginDependency.DependencyProvider; 21 import com.android.systemui.shared.plugins.PluginManager; 22 23 import javax.inject.Inject; 24 import javax.inject.Singleton; 25 26 import dagger.Lazy; 27 28 /** 29 */ 30 @Singleton 31 public class PluginDependencyProvider extends DependencyProvider { 32 33 private final ArrayMap<Class<?>, Object> mDependencies = new ArrayMap<>(); 34 private final Lazy<PluginManager> mManagerLazy; 35 36 /** 37 */ 38 @Inject PluginDependencyProvider(Lazy<PluginManager> managerLazy)39 public PluginDependencyProvider(Lazy<PluginManager> managerLazy) { 40 mManagerLazy = managerLazy; 41 PluginDependency.sProvider = this; 42 } 43 allowPluginDependency(Class<T> cls)44 public <T> void allowPluginDependency(Class<T> cls) { 45 allowPluginDependency(cls, Dependency.get(cls)); 46 } 47 allowPluginDependency(Class<T> cls, T obj)48 public <T> void allowPluginDependency(Class<T> cls, T obj) { 49 synchronized (mDependencies) { 50 mDependencies.put(cls, obj); 51 } 52 } 53 54 @Override get(Plugin p, Class<T> cls)55 <T> T get(Plugin p, Class<T> cls) { 56 if (!mManagerLazy.get().dependsOn(p, cls)) { 57 throw new IllegalArgumentException(p.getClass() + " does not depend on " + cls); 58 } 59 synchronized (mDependencies) { 60 if (!mDependencies.containsKey(cls)) { 61 throw new IllegalArgumentException("Unknown dependency " + cls); 62 } 63 return (T) mDependencies.get(cls); 64 } 65 } 66 } 67