blob: 697e2b4ee558b668f9802a35779777b031a20344 [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;
Chris Wrenfec5bff2013-03-27 17:32:38 -070021import android.os.Bundle;
Sara Tingf84b1a92012-12-17 10:16:03 -080022import android.util.Log;
23import android.view.Menu;
24import android.view.MenuItem;
25
Michael Chan33171c72013-04-01 18:23:23 -070026import java.io.FileNotFoundException;
Sara Tingf84b1a92012-12-17 10:16:03 -080027import java.io.IOException;
28import java.io.InputStream;
29import java.util.Properties;
30
Chris Wrenfec5bff2013-03-27 17:32:38 -070031
Sara Tingf84b1a92012-12-17 10:16:03 -080032/*
33 * Skeleton for additional options in the AllInOne menu.
34 */
35public class ExtensionsFactory {
Chris Wrenfec5bff2013-03-27 17:32:38 -070036
Sara Tingf84b1a92012-12-17 10:16:03 -080037 private static String TAG = "ExtensionsFactory";
38
Michael Chan33171c72013-04-01 18:23:23 -070039 // Config filename for mappings of various class names to their custom
40 // implementations.
Sara Tingf84b1a92012-12-17 10:16:03 -080041 private static String EXTENSIONS_PROPERTIES = "calendar_extensions.properties";
42
43 private static String ALL_IN_ONE_MENU_KEY = "AllInOneMenuExtensions";
Chris Wrenfec5bff2013-03-27 17:32:38 -070044 private static String CLOUD_NOTIFICATION_KEY = "CloudNotificationChannel";
Paul Sliwowski373e9da2013-06-26 18:59:18 -070045 private static String ANALYTICS_LOGGER_KEY = "AnalyticsLogger";
Sara Tingf84b1a92012-12-17 10:16:03 -080046
47 private static Properties sProperties = new Properties();
48 private static AllInOneMenuExtensionsInterface sAllInOneMenuExtensions = null;
Paul Sliwowski373e9da2013-06-26 18:59:18 -070049 private static AnalyticsLogger sAnalyticsLogger = null;
Sara Tingf84b1a92012-12-17 10:16:03 -080050
51 public static void init(AssetManager assetManager) {
52 try {
53 InputStream fileStream = assetManager.open(EXTENSIONS_PROPERTIES);
54 sProperties.load(fileStream);
55 fileStream.close();
Michael Chan33171c72013-04-01 18:23:23 -070056 } catch (FileNotFoundException e) {
57 // No custom extensions. Ignore.
58 Log.d(TAG, "No custom extensions.");
Sara Tingf84b1a92012-12-17 10:16:03 -080059 } catch (IOException e) {
Michael Chan33171c72013-04-01 18:23:23 -070060 Log.d(TAG, e.toString());
Sara Tingf84b1a92012-12-17 10:16:03 -080061 }
62 }
63
64 private static <T> T createInstance(String className) {
65 try {
66 Class<?> c = Class.forName(className);
67 return (T) c.newInstance();
68 } catch (ClassNotFoundException e) {
69 Log.e(TAG, className + ": unable to create instance.", e);
70 } catch (IllegalAccessException e) {
71 Log.e(TAG, className + ": unable to create instance.", e);
72 } catch (InstantiationException e) {
73 Log.e(TAG, className + ": unable to create instance.", e);
74 }
75 return null;
76 }
77
78 public static AllInOneMenuExtensionsInterface getAllInOneMenuExtensions() {
Paul Sliwowski373e9da2013-06-26 18:59:18 -070079 if ((sAllInOneMenuExtensions != null)) {
80 return sAllInOneMenuExtensions;
Sara Tingf84b1a92012-12-17 10:16:03 -080081 }
82
Paul Sliwowski373e9da2013-06-26 18:59:18 -070083 String className = sProperties.getProperty(ALL_IN_ONE_MENU_KEY);
84 if (className != null) {
85 sAllInOneMenuExtensions = createInstance(className);
86 } else {
87 Log.d(TAG, ALL_IN_ONE_MENU_KEY + " not found in properties file.");
88 }
89
90 if (sAllInOneMenuExtensions == null) {
91 sAllInOneMenuExtensions = new AllInOneMenuExtensionsInterface() {
92 @Override
93 public Integer getExtensionMenuResource(Menu menu) {
94 return null;
95 }
96
97 @Override
98 public boolean handleItemSelected(MenuItem item, Context context) {
99 return false;
100 }
101 };
102 }
Sara Tingf84b1a92012-12-17 10:16:03 -0800103 return sAllInOneMenuExtensions;
104 }
Chris Wrenfec5bff2013-03-27 17:32:38 -0700105
106 public static CloudNotificationBackplane getCloudNotificationBackplane() {
107 CloudNotificationBackplane cnb = null;
108
109 String className = sProperties.getProperty(CLOUD_NOTIFICATION_KEY);
110 if (className != null) {
111 cnb = createInstance(className);
112 } else {
113 Log.d(TAG, CLOUD_NOTIFICATION_KEY + " not found in properties file.");
114 }
115
116 if (cnb == null) {
117 cnb = new CloudNotificationBackplane() {
118 @Override
119 public boolean open(Context context) {
120 return true;
121 }
122
123 @Override
124 public boolean subscribeToGroup(String senderId, String account, String groupId)
125 throws IOException {
126 return true;}
127
128 @Override
Chris Wrenc9a20a32013-04-26 17:58:57 -0400129 public void send(String to, String msgId, Bundle data) {
Chris Wrenfec5bff2013-03-27 17:32:38 -0700130 }
131
132 @Override
133 public void close() {
134 }
135 };
136 }
137
138 return cnb;
139 }
Paul Sliwowski373e9da2013-06-26 18:59:18 -0700140
141 public static AnalyticsLogger getAnalyticsLogger(Context context) {
142 if (sAnalyticsLogger != null) {
143 return sAnalyticsLogger;
144 }
145
146 String className = sProperties.getProperty(ANALYTICS_LOGGER_KEY);
147 if (className != null) {
148 sAnalyticsLogger = createInstance(className);
149 } else {
150 Log.d(TAG, ANALYTICS_LOGGER_KEY + " not found in properties file.");
151 }
152
153 if (sAnalyticsLogger == null) {
154 sAnalyticsLogger = new AnalyticsLogger() {
155 @Override
156 public boolean initialize(Context context) {
157 return true;
158 }
159
160 @Override
161 public void trackView(String name) {
162 }
163 };
164 }
165
166 sAnalyticsLogger.initialize(context);
167 return sAnalyticsLogger;
168 }
Sara Tingf84b1a92012-12-17 10:16:03 -0800169}