Home | History | Annotate | Download | only in extensions
      1 /*
      2  * Copyright (C) 2013 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.contacts.common.extensions;
     18 
     19 import android.content.Context;
     20 import android.util.Log;
     21 
     22 import java.io.FileNotFoundException;
     23 import java.io.IOException;
     24 import java.io.InputStream;
     25 import java.util.Properties;
     26 
     27 
     28 /*
     29  * A framework for adding extensions to Dialer. This class reads a property file from
     30  * assets/contacts_extensions.properties and loads extension classes that an app has defined. If
     31  * an extension class was not defined, null is returned.
     32  */
     33 public class ExtensionsFactory {
     34 
     35     private static String TAG = "ExtensionsFactory";
     36 
     37     // Config filename for mappings of various class names to their custom
     38     // implementations.
     39     private static final String EXTENSIONS_PROPERTIES = "contacts_extensions.properties";
     40 
     41     private static final String EXTENDED_PHONE_DIRECTORIES_KEY = "extendedPhoneDirectories";
     42 
     43     private static Properties sProperties = null;
     44     private static ExtendedPhoneDirectoriesManager mExtendedPhoneDirectoriesManager = null;
     45 
     46     public static void init(Context context) {
     47         if (sProperties != null) {
     48             return;
     49         }
     50         try {
     51             final InputStream fileStream = context.getAssets().open(EXTENSIONS_PROPERTIES);
     52             sProperties = new Properties();
     53             sProperties.load(fileStream);
     54             fileStream.close();
     55 
     56             final String className = sProperties.getProperty(EXTENDED_PHONE_DIRECTORIES_KEY);
     57             if (className != null) {
     58                 mExtendedPhoneDirectoriesManager = createInstance(className);
     59             } else {
     60                 Log.d(TAG, EXTENDED_PHONE_DIRECTORIES_KEY + " not found in properties file.");
     61             }
     62 
     63         } catch (FileNotFoundException e) {
     64             // No custom extensions. Ignore.
     65             Log.d(TAG, "No custom extensions.");
     66         } catch (IOException e) {
     67             Log.d(TAG, e.toString());
     68         }
     69     }
     70 
     71     private static <T> T createInstance(String className) {
     72         try {
     73             Class<?> c = Class.forName(className);
     74             //noinspection unchecked
     75             return (T) c.newInstance();
     76         } catch (ClassNotFoundException e) {
     77             Log.e(TAG, className + ": unable to create instance.", e);
     78         } catch (IllegalAccessException e) {
     79             Log.e(TAG, className + ": unable to create instance.", e);
     80         } catch (InstantiationException e) {
     81             Log.e(TAG, className + ": unable to create instance.", e);
     82         }
     83         return null;
     84     }
     85 
     86     public static ExtendedPhoneDirectoriesManager getExtendedPhoneDirectoriesManager() {
     87         return mExtendedPhoneDirectoriesManager;
     88     }
     89 }
     90