Home | History | Annotate | Download | only in overlay
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.settings.intelligence.overlay;
     18 
     19 import android.content.Context;
     20 import android.text.TextUtils;
     21 import android.util.Log;
     22 
     23 import com.android.settings.intelligence.R;
     24 import com.android.settings.intelligence.experiment.ExperimentFeatureProvider;
     25 import com.android.settings.intelligence.instrumentation.MetricsFeatureProvider;
     26 import com.android.settings.intelligence.search.SearchFeatureProvider;
     27 import com.android.settings.intelligence.suggestions.SuggestionFeatureProvider;
     28 
     29 public abstract class FeatureFactory {
     30 
     31     private static final String LOG_TAG = "FeatureFactory";
     32     private static final boolean DEBUG = false;
     33 
     34     protected static FeatureFactory sFactory;
     35 
     36     /**
     37      * Returns a factory for creating feature controllers. Creates the factory if it does not
     38      * already exist. Uses the value of {@link R.string#config_featureFactory} to instantiate
     39      * a factory implementation.
     40      */
     41     public static FeatureFactory get(Context context) {
     42         if (sFactory != null) {
     43             return sFactory;
     44         }
     45 
     46         if (DEBUG) {
     47             Log.d(LOG_TAG, "getFactory");
     48         }
     49         final String clsName = context.getString(R.string.config_featureFactory);
     50         if (TextUtils.isEmpty(clsName)) {
     51             throw new UnsupportedOperationException("No feature factory configured");
     52         }
     53         try {
     54             sFactory = (FeatureFactory) context.getClassLoader().loadClass(clsName).newInstance();
     55         } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
     56             throw new FactoryNotFoundException(e);
     57         }
     58 
     59         if (DEBUG) {
     60             Log.d(LOG_TAG, "started " + sFactory.getClass().getSimpleName());
     61         }
     62         return sFactory;
     63     }
     64 
     65     public abstract MetricsFeatureProvider metricsFeatureProvider(Context context);
     66 
     67     public abstract SuggestionFeatureProvider suggestionFeatureProvider();
     68 
     69     public abstract ExperimentFeatureProvider experimentFeatureProvider();
     70 
     71     public abstract SearchFeatureProvider searchFeatureProvider();
     72 
     73     public static final class FactoryNotFoundException extends RuntimeException {
     74         public FactoryNotFoundException(Throwable throwable) {
     75             super("Unable to create factory. Did you misconfigure Proguard?", throwable);
     76         }
     77     }
     78 
     79 }
     80