1 package com.android.deskclock; 2 3 import android.content.Context; 4 import android.content.res.AssetManager; 5 import android.util.Log; 6 7 import com.android.deskclock.provider.Alarm; 8 9 import java.io.FileNotFoundException; 10 import java.io.IOException; 11 import java.io.InputStream; 12 import java.util.Properties; 13 14 public class ExtensionsFactory { 15 16 private static String TAG = "ExtensionsFactory"; 17 // Config filename for mappings of various class names to their custom 18 // implementations. 19 private static String EXTENSIONS_PROPERTIES = "deskclock_extensions.properties"; 20 private static String DESKCLOCKEXTENSIONS_KEY = "DeskclockExtensions"; 21 private static Properties sProperties = new Properties(); 22 private static DeskClockExtensions sDeskClockExtensions = null; 23 24 public static void init(AssetManager assetManager) { 25 try { 26 InputStream fileStream = assetManager.open(EXTENSIONS_PROPERTIES); 27 sProperties.load(fileStream); 28 fileStream.close(); 29 } catch (FileNotFoundException e) { 30 // No custom extensions. Ignore. 31 if (Log.isLoggable(TAG, Log.DEBUG)) { 32 Log.d(TAG, "No custom extensions."); 33 } 34 } catch (IOException e) { 35 if (Log.isLoggable(TAG, Log.DEBUG)) { 36 Log.d(TAG, e.toString()); 37 } 38 } 39 } 40 41 private static <T> T createInstance(String className) { 42 try { 43 Class<?> c = Class.forName(className); 44 return (T) c.newInstance(); 45 } catch (ClassNotFoundException e) { 46 if (Log.isLoggable(TAG, Log.ERROR)) { 47 Log.e(TAG, className + ": unable to create instance.", e); 48 } 49 } catch (IllegalAccessException e) { 50 if (Log.isLoggable(TAG, Log.ERROR)) { 51 Log.e(TAG, className + ": unable to create instance.", e); 52 } 53 } catch (InstantiationException e) { 54 if (Log.isLoggable(TAG, Log.ERROR)) { 55 Log.e(TAG, className + ": unable to create instance.", e); 56 } 57 } 58 return null; 59 } 60 61 public static DeskClockExtensions getDeskClockExtensions() { 62 if ((sDeskClockExtensions != null)) { 63 return sDeskClockExtensions; 64 } 65 66 String className = sProperties.getProperty(DESKCLOCKEXTENSIONS_KEY); 67 if (className != null) { 68 sDeskClockExtensions = createInstance(className); 69 } else { 70 if (Log.isLoggable(TAG, Log.DEBUG)) { 71 Log.d(TAG, DESKCLOCKEXTENSIONS_KEY + " not found in properties file."); 72 } 73 } 74 75 if (sDeskClockExtensions == null) { 76 sDeskClockExtensions = new DeskClockExtensions() { 77 @Override 78 public void addAlarm(Context context, Alarm newAlarm) { 79 if (Log.isLoggable(TAG, Log.DEBUG)) { 80 Log.d(TAG, "Add alarm: Empty inline implementation called."); 81 } 82 } 83 84 @Override 85 public void deleteAlarm(Context context, long alarmId) { 86 if (Log.isLoggable(TAG, Log.DEBUG)) { 87 Log.d(TAG, "Delete alarm: Empty inline implementation called."); 88 } 89 } 90 }; 91 } 92 return sDeskClockExtensions; 93 } 94 } 95