Adding a CallList class to manage Calls from CallHandlerService.

Change-Id: Ic6a7c88721a881654fb8afa174291415539b586e
diff --git a/InCallUI/Android.mk b/InCallUI/Android.mk
index b716642..86f787e 100644
--- a/InCallUI/Android.mk
+++ b/InCallUI/Android.mk
@@ -1,7 +1,8 @@
 LOCAL_PATH:= $(call my-dir)
 include $(CLEAR_VARS)
 
-LOCAL_STATIC_JAVA_LIBRARIES := com.android.services.telephony.common
+LOCAL_STATIC_JAVA_LIBRARIES := com.android.services.telephony.common \
+        guava \
 
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
 
diff --git a/InCallUI/src/com/android/incallui/CallHandlerService.java b/InCallUI/src/com/android/incallui/CallHandlerService.java
index 31d278b..127e575 100644
--- a/InCallUI/src/com/android/incallui/CallHandlerService.java
+++ b/InCallUI/src/com/android/incallui/CallHandlerService.java
@@ -35,9 +35,13 @@
     private static final String TAG = CallHandlerService.class.getSimpleName();
     private static final boolean DBG = false; // TODO: Have a shared location for this.
 
+    private CallList mCallList;
+
     @Override
     public void onCreate() {
         super.onCreate();
+
+        mCallList = new CallList();
     }
 
     @Override
@@ -59,6 +63,8 @@
 
         @Override
         public void onIncomingCall(Call call) {
+            mCallList.onUpdate(call);
+
             final Intent intent = new Intent(getApplication(), InCallActivity.class);
             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
             startActivity(intent);
@@ -66,10 +72,12 @@
 
         @Override
         public void onDisconnect(Call call) {
+            mCallList.onUpdate(call);
         }
 
         @Override
-        public void onUpdate(List<Call> call) {
+        public void onUpdate(List<Call> calls) {
+            mCallList.onUpdate(calls);
         }
     };
 
diff --git a/InCallUI/src/com/android/incallui/CallList.java b/InCallUI/src/com/android/incallui/CallList.java
new file mode 100644
index 0000000..5fe2619
--- /dev/null
+++ b/InCallUI/src/com/android/incallui/CallList.java
@@ -0,0 +1,64 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.android.incallui;
+
+import com.google.android.collect.Maps;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+
+import com.android.services.telephony.common.Call;
+
+import java.util.HashMap;
+import java.util.List;
+
+/**
+ * Maintains the list of active calls received from CallHandlerService.
+ * TODO(klp): This class should be used by InCallUI to read about
+ * changes to calls.
+ */
+public class CallList {
+
+    final HashMap<Integer, Call> callMap = Maps.newHashMap();
+
+    public void onUpdate(Call call) {
+        updateCallInMap(call);
+    }
+
+    public void onUpdate(List<Call> callsToUpdate) {
+        Preconditions.checkNotNull(callsToUpdate);
+        for (Call call : callsToUpdate) {
+            updateCallInMap(call);
+        }
+    }
+
+    private void updateCallInMap(Call call) {
+        Preconditions.checkNotNull(call);
+
+        final Integer id = new Integer(call.getCallId());
+
+        if (isCallActive(call)) {
+            callMap.put(id, call);
+        } else if (callMap.containsKey(id)) {
+            callMap.remove(id);
+        }
+    }
+
+    private boolean isCallActive(Call call) {
+        final int state = call.getState();
+        return Call.State.IDLE != state && Call.State.INVALID != state;
+    }
+}