2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.overlay;
19 import android.content.Context;
20 import android.text.TextUtils;
21 import android.util.Log;
23 import com.android.settings.R;
26 * Abstract class for creating feature controllers. Allows OEM implementations to define their own
27 * factories with their own controllers containing whatever code is needed to implement
28 * the features. To provide a factory implementation, implementors should override
29 * {@link R.string#config_featureFactory} in their override.
31 public abstract class FeatureFactory {
32 private static final String LOG_TAG = "FeatureFactory";
33 private static final boolean DEBUG = false;
35 private static FeatureFactory sFactory;
38 * Returns a factory for creating feature controllers. Creates the factory if it does not
39 * already exist. Uses the value of {@link R.string#config_featureFactory} to instantiate
40 * a factory implementation.
42 public static FeatureFactory getFactory(Context context) {
43 if (sFactory != null) {
47 if (DEBUG) Log.d(LOG_TAG, "getFactory");
48 final String clsName = context.getString(R.string.config_featureFactory);
49 if (TextUtils.isEmpty(clsName)) {
50 throw new UnsupportedOperationException("No feature factory configured");
53 sFactory = (FeatureFactory) context.getClassLoader().loadClass(clsName).newInstance();
54 } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
55 throw new FactoryNotFoundException(e);
58 if (DEBUG) Log.d(LOG_TAG, "started " + sFactory.getClass().getSimpleName());
63 * Creates stub controller that makes {@link android.widget.Toast}s.
64 * Will be removed before submitting.
66 public abstract ToastController createToastController();
68 public static class FactoryNotFoundException extends RuntimeException {
69 public FactoryNotFoundException(Throwable throwable) {
70 super("Unable to create factory. Did you misconfigure Proguard?", throwable);