Sara Ting | f84b1a9 | 2012-12-17 10:16:03 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.calendar; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.res.AssetManager; |
Chris Wren | fec5bff | 2013-03-27 17:32:38 -0700 | [diff] [blame] | 21 | import android.os.Bundle; |
Sara Ting | f84b1a9 | 2012-12-17 10:16:03 -0800 | [diff] [blame] | 22 | import android.util.Log; |
| 23 | import android.view.Menu; |
| 24 | import android.view.MenuItem; |
| 25 | |
Michael Chan | 33171c7 | 2013-04-01 18:23:23 -0700 | [diff] [blame] | 26 | import java.io.FileNotFoundException; |
Sara Ting | f84b1a9 | 2012-12-17 10:16:03 -0800 | [diff] [blame] | 27 | import java.io.IOException; |
| 28 | import java.io.InputStream; |
| 29 | import java.util.Properties; |
| 30 | |
Chris Wren | fec5bff | 2013-03-27 17:32:38 -0700 | [diff] [blame] | 31 | |
Sara Ting | f84b1a9 | 2012-12-17 10:16:03 -0800 | [diff] [blame] | 32 | /* |
| 33 | * Skeleton for additional options in the AllInOne menu. |
| 34 | */ |
| 35 | public class ExtensionsFactory { |
Chris Wren | fec5bff | 2013-03-27 17:32:38 -0700 | [diff] [blame] | 36 | |
Sara Ting | f84b1a9 | 2012-12-17 10:16:03 -0800 | [diff] [blame] | 37 | private static String TAG = "ExtensionsFactory"; |
| 38 | |
Michael Chan | 33171c7 | 2013-04-01 18:23:23 -0700 | [diff] [blame] | 39 | // Config filename for mappings of various class names to their custom |
| 40 | // implementations. |
Sara Ting | f84b1a9 | 2012-12-17 10:16:03 -0800 | [diff] [blame] | 41 | private static String EXTENSIONS_PROPERTIES = "calendar_extensions.properties"; |
| 42 | |
| 43 | private static String ALL_IN_ONE_MENU_KEY = "AllInOneMenuExtensions"; |
Chris Wren | fec5bff | 2013-03-27 17:32:38 -0700 | [diff] [blame] | 44 | private static String CLOUD_NOTIFICATION_KEY = "CloudNotificationChannel"; |
Paul Sliwowski | 373e9da | 2013-06-26 18:59:18 -0700 | [diff] [blame] | 45 | private static String ANALYTICS_LOGGER_KEY = "AnalyticsLogger"; |
Sara Ting | f84b1a9 | 2012-12-17 10:16:03 -0800 | [diff] [blame] | 46 | |
| 47 | private static Properties sProperties = new Properties(); |
| 48 | private static AllInOneMenuExtensionsInterface sAllInOneMenuExtensions = null; |
Paul Sliwowski | 373e9da | 2013-06-26 18:59:18 -0700 | [diff] [blame] | 49 | private static AnalyticsLogger sAnalyticsLogger = null; |
Sara Ting | f84b1a9 | 2012-12-17 10:16:03 -0800 | [diff] [blame] | 50 | |
| 51 | public static void init(AssetManager assetManager) { |
| 52 | try { |
| 53 | InputStream fileStream = assetManager.open(EXTENSIONS_PROPERTIES); |
| 54 | sProperties.load(fileStream); |
| 55 | fileStream.close(); |
Michael Chan | 33171c7 | 2013-04-01 18:23:23 -0700 | [diff] [blame] | 56 | } catch (FileNotFoundException e) { |
| 57 | // No custom extensions. Ignore. |
| 58 | Log.d(TAG, "No custom extensions."); |
Sara Ting | f84b1a9 | 2012-12-17 10:16:03 -0800 | [diff] [blame] | 59 | } catch (IOException e) { |
Michael Chan | 33171c7 | 2013-04-01 18:23:23 -0700 | [diff] [blame] | 60 | Log.d(TAG, e.toString()); |
Sara Ting | f84b1a9 | 2012-12-17 10:16:03 -0800 | [diff] [blame] | 61 | } |
| 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 Sliwowski | 373e9da | 2013-06-26 18:59:18 -0700 | [diff] [blame] | 79 | if ((sAllInOneMenuExtensions != null)) { |
| 80 | return sAllInOneMenuExtensions; |
Sara Ting | f84b1a9 | 2012-12-17 10:16:03 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Paul Sliwowski | 373e9da | 2013-06-26 18:59:18 -0700 | [diff] [blame] | 83 | 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 Ting | f84b1a9 | 2012-12-17 10:16:03 -0800 | [diff] [blame] | 103 | return sAllInOneMenuExtensions; |
| 104 | } |
Chris Wren | fec5bff | 2013-03-27 17:32:38 -0700 | [diff] [blame] | 105 | |
| 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 Wren | c9a20a3 | 2013-04-26 17:58:57 -0400 | [diff] [blame] | 129 | public void send(String to, String msgId, Bundle data) { |
Chris Wren | fec5bff | 2013-03-27 17:32:38 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public void close() { |
| 134 | } |
| 135 | }; |
| 136 | } |
| 137 | |
| 138 | return cnb; |
| 139 | } |
Paul Sliwowski | 373e9da | 2013-06-26 18:59:18 -0700 | [diff] [blame] | 140 | |
| 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 Ting | f84b1a9 | 2012-12-17 10:16:03 -0800 | [diff] [blame] | 169 | } |