1 /* 2 * Copyright (C) 2016 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.overlay; 18 19 import android.content.Context; 20 import android.text.TextUtils; 21 import android.util.Log; 22 23 import androidx.annotation.Nullable; 24 25 import com.android.settings.R; 26 import com.android.settings.accounts.AccountFeatureProvider; 27 import com.android.settings.applications.ApplicationFeatureProvider; 28 import com.android.settings.aware.AwareFeatureProvider; 29 import com.android.settings.bluetooth.BluetoothFeatureProvider; 30 import com.android.settings.dashboard.DashboardFeatureProvider; 31 import com.android.settings.dashboard.suggestions.SuggestionFeatureProvider; 32 import com.android.settings.enterprise.EnterprisePrivacyFeatureProvider; 33 import com.android.settings.fuelgauge.PowerUsageFeatureProvider; 34 import com.android.settings.gestures.AssistGestureFeatureProvider; 35 import com.android.settings.homepage.contextualcards.ContextualCardFeatureProvider; 36 import com.android.settings.localepicker.LocaleFeatureProvider; 37 import com.android.settings.panel.PanelFeatureProvider; 38 import com.android.settings.search.SearchFeatureProvider; 39 import com.android.settings.security.SecurityFeatureProvider; 40 import com.android.settings.slices.SlicesFeatureProvider; 41 import com.android.settings.users.UserFeatureProvider; 42 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; 43 44 /** 45 * Abstract class for creating feature controllers. Allows OEM implementations to define their own 46 * factories with their own controllers containing whatever code is needed to implement 47 * the features. To provide a factory implementation, implementors should override 48 * {@link R.string#config_featureFactory} in their override. 49 */ 50 public abstract class FeatureFactory { 51 private static final String LOG_TAG = "FeatureFactory"; 52 private static final boolean DEBUG = false; 53 54 protected static FeatureFactory sFactory; 55 protected static Context sAppContext; 56 57 /** 58 * Returns a factory for creating feature controllers. Creates the factory if it does not 59 * already exist. Uses the value of {@link R.string#config_featureFactory} to instantiate 60 * a factory implementation. 61 */ getFactory(Context context)62 public static FeatureFactory getFactory(Context context) { 63 if (sFactory != null) { 64 return sFactory; 65 } 66 if (sAppContext == null) { 67 sAppContext = context.getApplicationContext(); 68 } 69 70 if (DEBUG) Log.d(LOG_TAG, "getFactory"); 71 final String clsName = context.getString(R.string.config_featureFactory); 72 if (TextUtils.isEmpty(clsName)) { 73 throw new UnsupportedOperationException("No feature factory configured"); 74 } 75 try { 76 sFactory = (FeatureFactory) context.getClassLoader().loadClass(clsName).newInstance(); 77 } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { 78 throw new FactoryNotFoundException(e); 79 } 80 81 if (DEBUG) Log.d(LOG_TAG, "started " + sFactory.getClass().getSimpleName()); 82 return sFactory; 83 } 84 85 /** 86 * Returns an application {@link Context} used to create this {@link FeatureFactory}. If the 87 * factory has not been properly created yet (aka {@link #getFactory} has not been called), this 88 * will return null. 89 */ 90 @Nullable getAppContext()91 public static Context getAppContext() { 92 return sAppContext; 93 } 94 getAssistGestureFeatureProvider()95 public abstract AssistGestureFeatureProvider getAssistGestureFeatureProvider(); 96 getSuggestionFeatureProvider(Context context)97 public abstract SuggestionFeatureProvider getSuggestionFeatureProvider(Context context); 98 getSupportFeatureProvider(Context context)99 public abstract SupportFeatureProvider getSupportFeatureProvider(Context context); 100 getMetricsFeatureProvider()101 public abstract MetricsFeatureProvider getMetricsFeatureProvider(); 102 getPowerUsageFeatureProvider(Context context)103 public abstract PowerUsageFeatureProvider getPowerUsageFeatureProvider(Context context); 104 getDashboardFeatureProvider(Context context)105 public abstract DashboardFeatureProvider getDashboardFeatureProvider(Context context); 106 getDockUpdaterFeatureProvider()107 public abstract DockUpdaterFeatureProvider getDockUpdaterFeatureProvider(); 108 getApplicationFeatureProvider(Context context)109 public abstract ApplicationFeatureProvider getApplicationFeatureProvider(Context context); 110 getLocaleFeatureProvider()111 public abstract LocaleFeatureProvider getLocaleFeatureProvider(); 112 getEnterprisePrivacyFeatureProvider( Context context)113 public abstract EnterprisePrivacyFeatureProvider getEnterprisePrivacyFeatureProvider( 114 Context context); 115 getSearchFeatureProvider()116 public abstract SearchFeatureProvider getSearchFeatureProvider(); 117 getSurveyFeatureProvider(Context context)118 public abstract SurveyFeatureProvider getSurveyFeatureProvider(Context context); 119 getSecurityFeatureProvider()120 public abstract SecurityFeatureProvider getSecurityFeatureProvider(); 121 getUserFeatureProvider(Context context)122 public abstract UserFeatureProvider getUserFeatureProvider(Context context); 123 getSlicesFeatureProvider()124 public abstract SlicesFeatureProvider getSlicesFeatureProvider(); 125 getAccountFeatureProvider()126 public abstract AccountFeatureProvider getAccountFeatureProvider(); 127 getPanelFeatureProvider()128 public abstract PanelFeatureProvider getPanelFeatureProvider(); 129 getContextualCardFeatureProvider(Context context)130 public abstract ContextualCardFeatureProvider getContextualCardFeatureProvider(Context context); 131 getBluetoothFeatureProvider(Context context)132 public abstract BluetoothFeatureProvider getBluetoothFeatureProvider(Context context); 133 getAwareFeatureProvider()134 public abstract AwareFeatureProvider getAwareFeatureProvider(); 135 136 public static final class FactoryNotFoundException extends RuntimeException { FactoryNotFoundException(Throwable throwable)137 public FactoryNotFoundException(Throwable throwable) { 138 super("Unable to create factory. Did you misconfigure Proguard?", throwable); 139 } 140 } 141 } 142