Home | History | Annotate | Download | only in calendar
      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;
     21 import android.os.Bundle;
     22 import android.util.Log;
     23 import android.view.Menu;
     24 import android.view.MenuItem;
     25 
     26 import java.io.FileNotFoundException;
     27 import java.io.IOException;
     28 import java.io.InputStream;
     29 import java.util.Properties;
     30 
     31 
     32 /*
     33  * Skeleton for additional options in the AllInOne menu.
     34  */
     35 public class ExtensionsFactory {
     36 
     37     private static String TAG = "ExtensionsFactory";
     38 
     39     // Config filename for mappings of various class names to their custom
     40     // implementations.
     41     private static String EXTENSIONS_PROPERTIES = "calendar_extensions.properties";
     42 
     43     private static String ALL_IN_ONE_MENU_KEY = "AllInOneMenuExtensions";
     44     private static String CLOUD_NOTIFICATION_KEY = "CloudNotificationChannel";
     45     private static String ANALYTICS_LOGGER_KEY = "AnalyticsLogger";
     46 
     47     private static Properties sProperties = new Properties();
     48     private static AllInOneMenuExtensionsInterface sAllInOneMenuExtensions = null;
     49     private static AnalyticsLogger sAnalyticsLogger = null;
     50 
     51     public static void init(AssetManager assetManager) {
     52         try {
     53             InputStream fileStream = assetManager.open(EXTENSIONS_PROPERTIES);
     54             sProperties.load(fileStream);
     55             fileStream.close();
     56         } catch (FileNotFoundException e) {
     57             // No custom extensions. Ignore.
     58             Log.d(TAG, "No custom extensions.");
     59         } catch (IOException e) {
     60             Log.d(TAG, e.toString());
     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() {
     79         if ((sAllInOneMenuExtensions != null)) {
     80             return sAllInOneMenuExtensions;
     81         }
     82 
     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         }
    103         return sAllInOneMenuExtensions;
    104     }
    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
    129                 public void send(String to, String msgId, Bundle data) {
    130                 }
    131 
    132                 @Override
    133                 public void close() {
    134                 }
    135             };
    136         }
    137 
    138         return cnb;
    139     }
    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     }
    169 }
    170