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 public class PluginDependencyProvider extends DependencyProvider {
23 
24     private final ArrayMap<Class<?>, Object> mDependencies = new ArrayMap<>();
25     private final PluginManager mManager;
26 
PluginDependencyProvider(PluginManager manager)27     public PluginDependencyProvider(PluginManager manager) {
28         mManager = manager;
29         PluginDependency.sProvider = this;
30     }
31 
allowPluginDependency(Class<T> cls)32     public <T> void allowPluginDependency(Class<T> cls) {
33         allowPluginDependency(cls, Dependency.get(cls));
34     }
35 
allowPluginDependency(Class<T> cls, T obj)36     public <T> void allowPluginDependency(Class<T> cls, T obj) {
37         synchronized (mDependencies) {
38             mDependencies.put(cls, obj);
39         }
40     }
41 
42     @Override
get(Plugin p, Class<T> cls)43     <T> T get(Plugin p, Class<T> cls) {
44         if (!mManager.dependsOn(p, cls)) {
45             throw new IllegalArgumentException(p.getClass() + " does not depend on " + cls);
46         }
47         synchronized (mDependencies) {
48             if (!mDependencies.containsKey(cls)) {
49                 throw new IllegalArgumentException("Unknown dependency " + cls);
50             }
51             return (T) mDependencies.get(cls);
52         }
53     }
54 }
55