Home | History | Annotate | Download | only in policy
      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.statusbar.policy;
     16 
     17 import android.content.Context;
     18 
     19 import java.util.Map;
     20 import java.util.function.Consumer;
     21 import java.util.function.Supplier;
     22 
     23 /**
     24  * Utility class used to select between a plugin, tuner settings, and a default implementation
     25  * of an interface.
     26  */
     27 public interface ExtensionController {
     28 
     29     <T> ExtensionBuilder<T> newExtension(Class<T> cls);
     30 
     31     interface Extension<T> {
     32         T get();
     33         Context getContext();
     34         void destroy();
     35         void addCallback(Consumer<T> callback);
     36         /**
     37          * Triggers the extension to cycle through each of the sources again because something
     38          * (like configuration) may have changed.
     39          */
     40         T reload();
     41 
     42         /**
     43          * Null out the cached item for the purpose of memory saving, should only be done
     44          * when any other references are already gotten.
     45          * @param isDestroyed
     46          */
     47         void clearItem(boolean isDestroyed);
     48     }
     49 
     50     interface ExtensionBuilder<T> {
     51         ExtensionBuilder<T> withTunerFactory(TunerFactory<T> factory);
     52         <P extends T> ExtensionBuilder<T> withPlugin(Class<P> cls);
     53         <P extends T> ExtensionBuilder<T> withPlugin(Class<P> cls, String action);
     54         <P> ExtensionBuilder<T> withPlugin(Class<P> cls, String action,
     55                 PluginConverter<T, P> converter);
     56         ExtensionBuilder<T> withDefault(Supplier<T> def);
     57         ExtensionBuilder<T> withCallback(Consumer<T> callback);
     58         ExtensionBuilder<T> withUiMode(int mode, Supplier<T> def);
     59         ExtensionBuilder<T> withFeature(String feature, Supplier<T> def);
     60         Extension build();
     61     }
     62 
     63     public interface PluginConverter<T, P> {
     64         T getInterfaceFromPlugin(P plugin);
     65     }
     66 
     67     public interface TunerFactory<T> {
     68         String[] keys();
     69         T create(Map<String, String> settings);
     70     }
     71 }
     72