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 package com.android.emergency.overlay; 17 18 import android.content.Context; 19 import android.text.TextUtils; 20 import android.util.Log; 21 22 import com.android.emergency.edit.EmergencyContactsFeatureProvider; 23 import com.android.emergency.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 the 28 * features. To provide a factory implementation, implementers 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 protected static FeatureFactory sFactory; 36 37 /** @return a singleton factory instance. */ getFactory(Context context)38 public static FeatureFactory getFactory(Context context) { 39 if (sFactory != null) { 40 return sFactory; 41 } 42 43 if (DEBUG) Log.d(LOG_TAG, "getFactory"); 44 final String clsName = context.getString(R.string.config_featureFactory); 45 if (TextUtils.isEmpty(clsName)) { 46 throw new UnsupportedOperationException("No feature factory configured"); 47 } 48 try { 49 sFactory = (FeatureFactory) context.getClassLoader().loadClass(clsName).newInstance(); 50 } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { 51 throw new FactoryNotFoundException(e); 52 } 53 54 if (DEBUG) Log.d(LOG_TAG, "started " + sFactory.getClass().getSimpleName()); 55 return sFactory; 56 } 57 getEmergencyContactsFeatureProvider()58 public abstract EmergencyContactsFeatureProvider getEmergencyContactsFeatureProvider(); 59 60 /** Exception thrown when a factory can't be created. */ 61 public static final class FactoryNotFoundException extends RuntimeException { 62 /** 63 * @param throwable The underlying cause for this exception. 64 */ FactoryNotFoundException(Throwable throwable)65 public FactoryNotFoundException(Throwable throwable) { 66 super("Unable to create factory. Did you misconfigure Proguard?", throwable); 67 } 68 } 69 } 70