Added getInstance method for CalendarController
This is unique in that it creates a singleton for each Activity/Context.
Fixes NPE b/2835494 b/2859217 b/2859195

Change-Id: I986fb72cc1f1cd876694f142e9f136b065c7d0e0
diff --git a/src/com/android/calendar/CalendarController.java b/src/com/android/calendar/CalendarController.java
index 45c38da..a270a8a 100644
--- a/src/com/android/calendar/CalendarController.java
+++ b/src/com/android/calendar/CalendarController.java
@@ -51,6 +51,9 @@
     private LinkedList<EventHandler> mToBeRemovedEventHandlers = new LinkedList<EventHandler>();
     private boolean mDispatchInProgress;
 
+    private static WeakHashMap<Context, CalendarController> instances =
+        new WeakHashMap<Context, CalendarController>();
+
     private WeakHashMap<Object, Long> filters = new WeakHashMap<Object, Long>(1);
 
     private int mViewType = -1;
@@ -134,7 +137,24 @@
 
     }
 
-    public CalendarController(Context context) {
+    /**
+     * Creates and/or returns an instance of CalendarController associated with
+     * the supplied context. It is best to pass in the current Activity.
+     *
+     * @param context The activity if at all possible.
+     */
+    public static CalendarController getInstance(Context context) {
+        synchronized (instances) {
+            CalendarController controller = instances.get(context);
+            if (controller == null) {
+                controller = new CalendarController(context);
+                instances.put(context, controller);
+            }
+            return controller;
+        }
+    }
+
+    private CalendarController(Context context) {
         mContext = context;
         mTime.setToNow();
         mService = new AsyncQueryService(context) {