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 
     46     private static Properties sProperties = new Properties();
     47     private static AllInOneMenuExtensionsInterface sAllInOneMenuExtensions = null;
     48 
     49     public static void init(AssetManager assetManager) {
     50         try {
     51             InputStream fileStream = assetManager.open(EXTENSIONS_PROPERTIES);
     52             sProperties.load(fileStream);
     53             fileStream.close();
     54         } catch (FileNotFoundException e) {
     55             // No custom extensions. Ignore.
     56             Log.d(TAG, "No custom extensions.");
     57         } catch (IOException e) {
     58             Log.d(TAG, e.toString());
     59         }
     60     }
     61 
     62     private static <T> T createInstance(String className) {
     63         try {
     64             Class<?> c = Class.forName(className);
     65             return (T) c.newInstance();
     66         } catch (ClassNotFoundException e) {
     67             Log.e(TAG, className + ": unable to create instance.", e);
     68         } catch (IllegalAccessException e) {
     69             Log.e(TAG, className + ": unable to create instance.", e);
     70         } catch (InstantiationException e) {
     71             Log.e(TAG, className + ": unable to create instance.", e);
     72         }
     73         return null;
     74     }
     75 
     76     public static AllInOneMenuExtensionsInterface getAllInOneMenuExtensions() {
     77         if (sAllInOneMenuExtensions == null) {
     78             String className = sProperties.getProperty(ALL_IN_ONE_MENU_KEY);
     79             if (className != null) {
     80                 sAllInOneMenuExtensions = createInstance(className);
     81             } else {
     82                 Log.d(TAG, ALL_IN_ONE_MENU_KEY + " not found in properties file.");
     83             }
     84 
     85             if (sAllInOneMenuExtensions == null) {
     86                 sAllInOneMenuExtensions = new AllInOneMenuExtensionsInterface() {
     87                     @Override
     88                     public Integer getExtensionMenuResource(Menu menu) {
     89                         return null;
     90                     }
     91 
     92                     @Override
     93                     public boolean handleItemSelected(MenuItem item, Context context) {
     94                         return false;
     95                     }
     96                 };
     97             }
     98         }
     99 
    100         return sAllInOneMenuExtensions;
    101     }
    102 
    103     public static CloudNotificationBackplane getCloudNotificationBackplane() {
    104         CloudNotificationBackplane cnb = null;
    105 
    106         String className = sProperties.getProperty(CLOUD_NOTIFICATION_KEY);
    107         if (className != null) {
    108             cnb = createInstance(className);
    109         } else {
    110             Log.d(TAG, CLOUD_NOTIFICATION_KEY + " not found in properties file.");
    111         }
    112 
    113         if (cnb == null) {
    114             cnb = new CloudNotificationBackplane() {
    115                 @Override
    116                 public boolean open(Context context) {
    117                     return true;
    118                 }
    119 
    120                 @Override
    121                 public boolean subscribeToGroup(String senderId, String account, String groupId)
    122                         throws IOException {
    123                     return true;}
    124 
    125                 @Override
    126                 public void send(String to, String msgId, Bundle data) {
    127                 }
    128 
    129                 @Override
    130                 public void close() {
    131                 }
    132             };
    133         }
    134 
    135         return cnb;
    136     }
    137 }
    138