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