blob: aaf7b01a944a7f9da771b711608fb77d7d4a0c1d [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
Michael Chan33171c72013-04-01 18:23:23 -070025import java.io.FileNotFoundException;
Sara Tingf84b1a92012-12-17 10:16:03 -080026import java.io.IOException;
27import java.io.InputStream;
28import java.util.Properties;
29
30/*
31 * Skeleton for additional options in the AllInOne menu.
32 */
33public class ExtensionsFactory {
34 private static String TAG = "ExtensionsFactory";
35
Michael Chan33171c72013-04-01 18:23:23 -070036 // Config filename for mappings of various class names to their custom
37 // implementations.
Sara Tingf84b1a92012-12-17 10:16:03 -080038 private static String EXTENSIONS_PROPERTIES = "calendar_extensions.properties";
39
40 private static String ALL_IN_ONE_MENU_KEY = "AllInOneMenuExtensions";
41
42 private static Properties sProperties = new Properties();
43 private static AllInOneMenuExtensionsInterface sAllInOneMenuExtensions = null;
44
45 public static void init(AssetManager assetManager) {
46 try {
47 InputStream fileStream = assetManager.open(EXTENSIONS_PROPERTIES);
48 sProperties.load(fileStream);
49 fileStream.close();
Michael Chan33171c72013-04-01 18:23:23 -070050 } catch (FileNotFoundException e) {
51 // No custom extensions. Ignore.
52 Log.d(TAG, "No custom extensions.");
Sara Tingf84b1a92012-12-17 10:16:03 -080053 } catch (IOException e) {
Michael Chan33171c72013-04-01 18:23:23 -070054 Log.d(TAG, e.toString());
Sara Tingf84b1a92012-12-17 10:16:03 -080055 }
56 }
57
58 private static <T> T createInstance(String className) {
59 try {
60 Class<?> c = Class.forName(className);
61 return (T) c.newInstance();
62 } catch (ClassNotFoundException e) {
63 Log.e(TAG, className + ": unable to create instance.", e);
64 } catch (IllegalAccessException e) {
65 Log.e(TAG, className + ": unable to create instance.", e);
66 } catch (InstantiationException e) {
67 Log.e(TAG, className + ": unable to create instance.", e);
68 }
69 return null;
70 }
71
72 public static AllInOneMenuExtensionsInterface getAllInOneMenuExtensions() {
73 if (sAllInOneMenuExtensions == null) {
74 String className = sProperties.getProperty(ALL_IN_ONE_MENU_KEY);
75 if (className != null) {
76 sAllInOneMenuExtensions = createInstance(className);
77 } else {
78 Log.d(TAG, ALL_IN_ONE_MENU_KEY + " not found in properties file.");
79 }
80
81 if (sAllInOneMenuExtensions == null) {
82 sAllInOneMenuExtensions = new AllInOneMenuExtensionsInterface() {
83 @Override
84 public Integer getExtensionMenuResource(Menu menu) {
85 return null;
86 }
87
88 @Override
89 public boolean handleItemSelected(MenuItem item, Context context) {
90 return false;
91 }
92 };
93 }
94 }
95
96 return sAllInOneMenuExtensions;
97 }
98}