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