OSDN Git Service

a5e779d354d9ce20cef2238a7193509466b7b0a6
[android-x86/packages-apps-Settings.git] / src / com / android / settings / overlay / FeatureFactory.java
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 com.android.settings.R;
24
25 /**
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.
30  */
31 public abstract class FeatureFactory {
32     private static final String LOG_TAG = "FeatureFactory";
33     private static final boolean DEBUG = false;
34
35     private static FeatureFactory sFactory;
36
37     /**
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.
41      */
42     public static FeatureFactory getFactory(Context context) {
43         if (sFactory != null) {
44             return sFactory;
45         }
46
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");
51         }
52         try {
53             sFactory = (FeatureFactory) context.getClassLoader().loadClass(clsName).newInstance();
54         } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
55             throw new FactoryNotFoundException(e);
56         }
57
58         if (DEBUG) Log.d(LOG_TAG, "started " + sFactory.getClass().getSimpleName());
59         return sFactory;
60     }
61
62     /**
63      * Creates stub controller that makes {@link android.widget.Toast}s.
64      * Will be removed before submitting.
65      */
66     public abstract ToastController createToastController();
67
68     public static class FactoryNotFoundException extends RuntimeException {
69         public FactoryNotFoundException(Throwable throwable) {
70             super("Unable to create factory. Did you misconfigure Proguard?", throwable);
71         }
72     }
73 }