blob: 88ca8a90571411526206cc21e4f3c1f5fad1e217 [file] [log] [blame]
Sara Tingf84b1a92012-12-17 10:16:03 -08001/*
2 * Copyright (C) 2012 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
17package com.android.calendar;
18
19import android.content.Context;
20import android.content.res.AssetManager;
21import android.util.Log;
22import android.view.Menu;
23import android.view.MenuItem;
24
25import java.io.IOException;
26import java.io.InputStream;
27import java.util.Properties;
28
29/*
30 * Skeleton for additional options in the AllInOne menu.
31 */
32public class ExtensionsFactory {
33 private static String TAG = "ExtensionsFactory";
34
35 // Config filename for mappings of various class names to their custom implementations.
36 private static String EXTENSIONS_PROPERTIES = "calendar_extensions.properties";
37
38 private static String ALL_IN_ONE_MENU_KEY = "AllInOneMenuExtensions";
39
40 private static Properties sProperties = new Properties();
41 private static AllInOneMenuExtensionsInterface sAllInOneMenuExtensions = null;
42
43 public static void init(AssetManager assetManager) {
44 try {
45 InputStream fileStream = assetManager.open(EXTENSIONS_PROPERTIES);
46 sProperties.load(fileStream);
47 fileStream.close();
48 } catch (IOException e) {
49 // No custom extensions. Ignore.
50 Log.d(TAG, "No custom extensions.", e);
51 }
52 }
53
54 private static <T> T createInstance(String className) {
55 try {
56 Class<?> c = Class.forName(className);
57 return (T) c.newInstance();
58 } catch (ClassNotFoundException e) {
59 Log.e(TAG, className + ": unable to create instance.", e);
60 } catch (IllegalAccessException e) {
61 Log.e(TAG, className + ": unable to create instance.", e);
62 } catch (InstantiationException e) {
63 Log.e(TAG, className + ": unable to create instance.", e);
64 }
65 return null;
66 }
67
68 public static AllInOneMenuExtensionsInterface getAllInOneMenuExtensions() {
69 if (sAllInOneMenuExtensions == null) {
70 String className = sProperties.getProperty(ALL_IN_ONE_MENU_KEY);
71 if (className != null) {
72 sAllInOneMenuExtensions = createInstance(className);
73 } else {
74 Log.d(TAG, ALL_IN_ONE_MENU_KEY + " not found in properties file.");
75 }
76
77 if (sAllInOneMenuExtensions == null) {
78 sAllInOneMenuExtensions = new AllInOneMenuExtensionsInterface() {
79 @Override
80 public Integer getExtensionMenuResource(Menu menu) {
81 return null;
82 }
83
84 @Override
85 public boolean handleItemSelected(MenuItem item, Context context) {
86 return false;
87 }
88 };
89 }
90 }
91
92 return sAllInOneMenuExtensions;
93 }
94}