Merge "Adds a cached phone factory." into master-nova
diff --git a/src/com/android/services/telephony/CachedPhoneFactory.java b/src/com/android/services/telephony/CachedPhoneFactory.java
new file mode 100644
index 0000000..34a1fc0
--- /dev/null
+++ b/src/com/android/services/telephony/CachedPhoneFactory.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2014 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.services.telephony;
+
+import com.android.internal.telephony.Phone;
+import com.android.internal.telephony.PhoneFactory;
+
+/**
+ * Creates and caches phone objects for use with call services. Incoming call listening and the call
+ * service itself exist independently (and across threads) but need to share their references to the
+ * {@link Phone} objects that they use. This class is used to provide those cached references.
+ * TODO(santoscordon): Investigate if this functionality can be folded into PhoneFactory once
+ * PhoneFactory is no longer being used by the old system.
+ */
+class CachedPhoneFactory {
+    private static Phone sCdmaPhone;
+    private static Phone sGsmPhone;
+
+    /**
+     * @return The GSM Phone instance.
+     */
+    public static synchronized Phone getGsmPhone() {
+        if (sGsmPhone == null) {
+            sGsmPhone = PhoneFactory.getGsmPhone();
+        }
+        return sGsmPhone;
+    }
+
+    /**
+     * @return The CDMA Phone instance.
+     */
+    public static synchronized Phone getCdmaPhone() {
+        if (sCdmaPhone == null) {
+            sCdmaPhone = PhoneFactory.getCdmaPhone();
+        }
+        return sCdmaPhone;
+    }
+}
diff --git a/src/com/android/services/telephony/CdmaCallService.java b/src/com/android/services/telephony/CdmaCallService.java
index 0971ac1..03fc2b7 100644
--- a/src/com/android/services/telephony/CdmaCallService.java
+++ b/src/com/android/services/telephony/CdmaCallService.java
@@ -30,7 +30,6 @@
  */
 public class CdmaCallService extends BaseTelephonyCallService {
     private static final String TAG = CdmaCallService.class.getSimpleName();
-    private static Phone sCdmaPhone;
 
     static boolean shouldSelect(Context context, CallInfo callInfo) {
         TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(
@@ -51,9 +50,6 @@
     /** {@inheritDoc} */
     @Override
     public void call(CallInfo callInfo) {
-        if (sCdmaPhone == null) {
-            sCdmaPhone = PhoneFactory.getCdmaPhone();
-        }
-        startCallWithPhone(sCdmaPhone, callInfo);
+        startCallWithPhone(CachedPhoneFactory.getCdmaPhone(), callInfo);
     }
 }
diff --git a/src/com/android/services/telephony/GsmCallService.java b/src/com/android/services/telephony/GsmCallService.java
index 5d7440a..5c31b52 100644
--- a/src/com/android/services/telephony/GsmCallService.java
+++ b/src/com/android/services/telephony/GsmCallService.java
@@ -30,7 +30,6 @@
  */
 public class GsmCallService extends BaseTelephonyCallService {
     private static final String TAG = GsmCallService.class.getSimpleName();
-    private static Phone sGsmPhone;
 
     static boolean shouldSelect(Context context, CallInfo callInfo) {
         TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(
@@ -51,9 +50,6 @@
     /** {@inheritDoc} */
     @Override
     public void call(CallInfo callInfo) {
-        if (sGsmPhone == null) {
-            sGsmPhone = PhoneFactory.getGsmPhone();
-        }
-        startCallWithPhone(sGsmPhone, callInfo);
+        startCallWithPhone(CachedPhoneFactory.getGsmPhone(), callInfo);
     }
 }