Remove Guava references from Telecom.

Prepping telecom to run in system-server process by removing references
to guava.

Bug: 18112269
Change-Id: I994be480284462240da46c9cf0e47aa83f5f1fe9
diff --git a/Android.mk b/Android.mk
index 395aacc..0917793 100644
--- a/Android.mk
+++ b/Android.mk
@@ -4,8 +4,6 @@
 include $(CLEAR_VARS)
 
 LOCAL_JAVA_LIBRARIES := telephony-common
-LOCAL_STATIC_JAVA_LIBRARIES := \
-        guava \
 
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
 
diff --git a/src/com/android/server/telecom/CallIdMapper.java b/src/com/android/server/telecom/CallIdMapper.java
index 40a50a5..729db0a 100644
--- a/src/com/android/server/telecom/CallIdMapper.java
+++ b/src/com/android/server/telecom/CallIdMapper.java
@@ -16,11 +16,65 @@
 
 package com.android.server.telecom;
 
-import com.google.common.collect.HashBiMap;
+import android.util.ArrayMap;
+
+import java.util.Map;
 
 /** Utility to map {@link Call} objects to unique IDs. IDs are generated when a call is added. */
 class CallIdMapper {
-    private final HashBiMap<String, Call> mCalls = HashBiMap.create();
+    /**
+     * A very basic bidirectional map.
+     */
+    static class BiMap<K, V> {
+        private Map<K, V> mPrimaryMap = new ArrayMap<>();
+        private Map<V, K> mSecondaryMap = new ArrayMap<>();
+
+        public boolean put(K key, V value) {
+            if (key == null || value == null || mPrimaryMap.containsKey(key) ||
+                    mSecondaryMap.containsKey(value)) {
+                return false;
+            }
+
+            mPrimaryMap.put(key, value);
+            mSecondaryMap.put(value, key);
+            return true;
+        }
+
+        public boolean remove(K key) {
+            if (key == null) {
+                return false;
+            }
+            if (mPrimaryMap.containsKey(key)) {
+                V value = getValue(key);
+                mPrimaryMap.remove(key);
+                mSecondaryMap.remove(value);
+                return true;
+            }
+            return false;
+        }
+
+        public boolean removeValue(V value) {
+            if (value == null) {
+                return false;
+            }
+            return remove(getKey(value));
+        }
+
+        public V getValue(K key) {
+            return mPrimaryMap.get(key);
+        }
+
+        public K getKey(V value) {
+            return mSecondaryMap.get(value);
+        }
+
+        public void clear() {
+            mPrimaryMap.clear();
+            mSecondaryMap.clear();
+        }
+    }
+
+    private final BiMap<String, Call> mCalls = new BiMap<>();
     private final String mCallIdPrefix;
     private static int sIdCount;
 
@@ -55,7 +109,7 @@
             return;
         }
         ThreadUtil.checkOnMainThread();
-        mCalls.inverse().remove(call);
+        mCalls.removeValue(call);
     }
 
     void removeCall(String callId) {
@@ -68,7 +122,7 @@
             return null;
         }
         ThreadUtil.checkOnMainThread();
-        return mCalls.inverse().get(call);
+        return mCalls.getKey(call);
     }
 
     Call getCall(Object objId) {
@@ -82,7 +136,7 @@
             return null;
         }
 
-        return mCalls.get(callId);
+        return mCalls.getValue(callId);
     }
 
     void clear() {
diff --git a/src/com/android/server/telecom/CallsManager.java b/src/com/android/server/telecom/CallsManager.java
index 5c8f24f..eb5b7ab 100644
--- a/src/com/android/server/telecom/CallsManager.java
+++ b/src/com/android/server/telecom/CallsManager.java
@@ -40,9 +40,7 @@
 
 import com.android.internal.util.IndentingPrintWriter;
 
-import com.google.common.collect.ImmutableCollection;
-import com.google.common.collect.ImmutableList;
-
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.List;
@@ -314,8 +312,8 @@
         return true;
     }
 
-    ImmutableCollection<Call> getCalls() {
-        return ImmutableList.copyOf(mCalls);
+    Collection<Call> getCalls() {
+        return Collections.unmodifiableCollection(mCalls);
     }
 
     Call getForegroundCall() {
diff --git a/src/com/android/server/telecom/DtmfLocalTonePlayer.java b/src/com/android/server/telecom/DtmfLocalTonePlayer.java
index 6b4b74d..562f8d3 100644
--- a/src/com/android/server/telecom/DtmfLocalTonePlayer.java
+++ b/src/com/android/server/telecom/DtmfLocalTonePlayer.java
@@ -22,9 +22,6 @@
 import android.provider.Settings;
 
 // TODO: Needed for move to system service: import com.android.internal.R;
-import com.google.common.collect.ImmutableMap;
-
-import java.util.Map;
 
 /**
  * Plays DTMF tones locally for the caller to hear. In order to reduce (1) the amount of times we
@@ -33,22 +30,6 @@
  * changes.
  */
 class DtmfLocalTonePlayer extends CallsManagerListenerBase {
-    private static final Map<Character, Integer> TONE_MAP =
-            ImmutableMap.<Character, Integer>builder()
-                    .put('1', ToneGenerator.TONE_DTMF_1)
-                    .put('2', ToneGenerator.TONE_DTMF_2)
-                    .put('3', ToneGenerator.TONE_DTMF_3)
-                    .put('4', ToneGenerator.TONE_DTMF_4)
-                    .put('5', ToneGenerator.TONE_DTMF_5)
-                    .put('6', ToneGenerator.TONE_DTMF_6)
-                    .put('7', ToneGenerator.TONE_DTMF_7)
-                    .put('8', ToneGenerator.TONE_DTMF_8)
-                    .put('9', ToneGenerator.TONE_DTMF_9)
-                    .put('0', ToneGenerator.TONE_DTMF_0)
-                    .put('#', ToneGenerator.TONE_DTMF_P)
-                    .put('*', ToneGenerator.TONE_DTMF_S)
-                    .build();
-
     /** Generator used to actually play the tone. */
     private ToneGenerator mToneGenerator;
 
@@ -85,8 +66,9 @@
             Log.d(this, "playTone: mToneGenerator == null, %c.", c);
         } else {
             Log.d(this, "starting local tone: %c.", c);
-            if (TONE_MAP.containsKey(c)) {
-                mToneGenerator.startTone(TONE_MAP.get(c), -1 /* toneDuration */);
+            int tone = getMappedTone(c);
+            if (tone != ToneGenerator.TONE_UNKNOWN) {
+                mToneGenerator.startTone(tone, -1 /* toneDuration */);
             }
         }
     }
@@ -160,4 +142,15 @@
             }
         }
     }
+
+    private static final int getMappedTone(char digit) {
+        if (digit >= '0' && digit <= '9') {
+            return ToneGenerator.TONE_DTMF_0 + digit - '0';
+        } else if (digit == '#') {
+            return ToneGenerator.TONE_DTMF_P;
+        } else if (digit == '*') {
+            return ToneGenerator.TONE_DTMF_S;
+        }
+        return ToneGenerator.TONE_UNKNOWN;
+    }
 }
diff --git a/src/com/android/server/telecom/InCallController.java b/src/com/android/server/telecom/InCallController.java
index 9498d4c..9bb2826 100644
--- a/src/com/android/server/telecom/InCallController.java
+++ b/src/com/android/server/telecom/InCallController.java
@@ -40,13 +40,13 @@
 import android.util.ArrayMap;
 
 
+
 // TODO: Needed for move to system service: import com.android.internal.R;
 import com.android.internal.telecom.IInCallService;
 import com.android.internal.util.IndentingPrintWriter;
 
-import com.google.common.collect.ImmutableCollection;
-
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -355,7 +355,7 @@
         }
 
         // Upon successful connection, send the state of the world to the service.
-        ImmutableCollection<Call> calls = CallsManager.getInstance().getCalls();
+        Collection<Call> calls = CallsManager.getInstance().getCalls();
         if (!calls.isEmpty()) {
             Log.i(this, "Adding %s calls to InCallService after onConnected: %s", calls.size(),
                     componentName);
diff --git a/src/com/android/server/telecom/ServiceBinder.java b/src/com/android/server/telecom/ServiceBinder.java
index fb747f2..4b30990 100644
--- a/src/com/android/server/telecom/ServiceBinder.java
+++ b/src/com/android/server/telecom/ServiceBinder.java
@@ -22,11 +22,10 @@
 import android.content.ServiceConnection;
 import android.os.IBinder;
 import android.os.IInterface;
+import android.text.TextUtils;
+import android.util.ArraySet;
 
 import com.android.internal.util.Preconditions;
-import com.google.common.base.Strings;
-
-import com.google.common.collect.Sets;
 
 import java.util.Collections;
 import java.util.Set;
@@ -136,7 +135,7 @@
     private final ComponentName mComponentName;
 
     /** The set of callbacks waiting for notification of the binding's success or failure. */
-    private final Set<BindCallback> mCallbacks = Sets.newHashSet();
+    private final Set<BindCallback> mCallbacks = new ArraySet<>();
 
     /** Used to bind and unbind from the service. */
     private ServiceConnection mServiceConnection;
@@ -169,7 +168,7 @@
      * @param context The context.
      */
     protected ServiceBinder(String serviceAction, ComponentName componentName, Context context) {
-        Preconditions.checkState(!Strings.isNullOrEmpty(serviceAction));
+        Preconditions.checkState(!TextUtils.isEmpty(serviceAction));
         Preconditions.checkNotNull(componentName);
 
         mContext = context;