• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.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 
init(Context context)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                 if (Log.isLoggable(TAG, Log.DEBUG)) {
61                     Log.d(TAG, EXTENDED_PHONE_DIRECTORIES_KEY + " not found in properties file.");
62                 }
63             }
64 
65         } catch (FileNotFoundException e) {
66             // No custom extensions. Ignore.
67             if (Log.isLoggable(TAG, Log.DEBUG)) {
68                 Log.d(TAG, "No custom extensions.");
69             }
70         } catch (IOException e) {
71             if (Log.isLoggable(TAG, Log.DEBUG)) {
72                 Log.d(TAG, e.toString());
73             }
74         }
75     }
76 
createInstance(String className)77     private static <T> T createInstance(String className) {
78         try {
79             Class<?> c = Class.forName(className);
80             //noinspection unchecked
81             return (T) c.newInstance();
82         } catch (ClassNotFoundException e) {
83             Log.e(TAG, className + ": unable to create instance.", e);
84         } catch (IllegalAccessException e) {
85             Log.e(TAG, className + ": unable to create instance.", e);
86         } catch (InstantiationException e) {
87             Log.e(TAG, className + ": unable to create instance.", e);
88         }
89         return null;
90     }
91 
getExtendedPhoneDirectoriesManager()92     public static ExtendedPhoneDirectoriesManager getExtendedPhoneDirectoriesManager() {
93         return mExtendedPhoneDirectoriesManager;
94     }
95 }
96