Merge "Neuter Telephony app" into master-nova
diff --git a/src/com/android/phone/PhoneApp.java b/src/com/android/phone/PhoneApp.java
index c5f69bf..bf30d58 100644
--- a/src/com/android/phone/PhoneApp.java
+++ b/src/com/android/phone/PhoneApp.java
@@ -40,7 +40,7 @@
             mPhoneGlobals = new PhoneGlobals(this);
             mPhoneGlobals.onCreate();
 
-            mTelephonyGlobals = new TelephonyGlobals();
+            mTelephonyGlobals = new TelephonyGlobals(this);
             mTelephonyGlobals.onCreate();
         }
     }
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);
     }
 }
diff --git a/src/com/android/services/telephony/IncomingCallNotifier.java b/src/com/android/services/telephony/IncomingCallNotifier.java
new file mode 100644
index 0000000..5121dcf
--- /dev/null
+++ b/src/com/android/services/telephony/IncomingCallNotifier.java
@@ -0,0 +1,125 @@
+/*
+ * 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.google.common.base.Preconditions;
+
+import android.content.Context;
+import android.content.Intent;
+import android.os.AsyncResult;
+import android.os.Handler;
+import android.os.Message;
+import android.telecomm.CallService;
+import android.telecomm.CallServiceDescriptor;
+import android.telecomm.TelecommConstants;
+import android.util.Log;
+
+import com.android.internal.telephony.Call;
+import com.android.internal.telephony.Connection;
+import com.android.internal.telephony.Phone;
+
+/**
+ * Listens to incoming-call events from the associated phone object and notifies Telecomm upon each
+ * occurence. One instance of these exists for each of the telephony-based call services.
+ */
+final class IncomingCallNotifier {
+
+    private static final String TAG = IncomingCallNotifier.class.getSimpleName();
+    private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG);
+
+    /** New ringing connection event code. */
+    private static final int EVENT_NEW_RINGING_CONNECTION = 100;
+
+    /** The phone object to listen to. */
+    private final Phone mPhone;
+
+    /** The class for the associated call service. */
+    private final Class<? extends CallService> mCallServiceClass;
+
+    /**
+     * Used to listen to events from {@link #mPhone}.
+     */
+    private final Handler mHandler = new Handler() {
+        @Override
+        public void handleMessage(Message msg) {
+            switch(msg.what) {
+                case EVENT_NEW_RINGING_CONNECTION:
+                    handleNewRingingConnection((AsyncResult) msg.obj);
+                    break;
+                default:
+                    break;
+            }
+        }
+    };
+
+    /**
+     * Persists the specified parameters and starts listening to phone events.
+     *
+     * @param callServiceClass The call service class.
+     * @param phone The phone object for listening to incoming calls.
+     */
+    IncomingCallNotifier(Class<? extends CallService> callServiceClass, Phone phone) {
+        Preconditions.checkNotNull(callServiceClass);
+        Preconditions.checkNotNull(phone);
+
+        mCallServiceClass = callServiceClass;
+        mPhone = phone;
+
+        mPhone.registerForNewRingingConnection(mHandler, EVENT_NEW_RINGING_CONNECTION, null);
+    }
+
+    /**
+     * Verifies the incoming call and triggers sending the incoming-call intent to Telecomm.
+     *
+     * @param asyncResult The result object from the new ringing event.
+     */
+    private void handleNewRingingConnection(AsyncResult asyncResult) {
+        if (DBG) {
+            Log.d(TAG, "handleNewRingingConnection");
+        }
+
+        Connection connection = (Connection) asyncResult.result;
+        if (connection != null) {
+            Call call = connection.getCall();
+
+            // Final verification of the ringing state before sending the intent to Telecomm.
+            if (call != null && call.getState().isRinging()) {
+                sendIncomingCallIntent();
+            }
+        }
+    }
+
+    /**
+     * Sends the incoming call intent to telecomm.
+     */
+    private void sendIncomingCallIntent() {
+        Context context = mPhone.getContext();
+
+        CallServiceDescriptor.Builder builder = CallServiceDescriptor.newBuilder(context);
+        builder.setCallService(mCallServiceClass);
+        builder.setNetworkType(CallServiceDescriptor.FLAG_PSTN);
+
+        Intent intent = new Intent(TelecommConstants.ACTION_INCOMING_CALL);
+        intent.putExtra(TelecommConstants.EXTRA_CALL_SERVICE_DESCRIPTOR, builder.build());
+
+        if (DBG) {
+            Log.d(TAG, "Sending incoming call intent: " + intent);
+        }
+
+        context.startActivity(intent);
+    }
+}
diff --git a/src/com/android/services/telephony/TelephonyGlobals.java b/src/com/android/services/telephony/TelephonyGlobals.java
index e86a40e..722a880 100644
--- a/src/com/android/services/telephony/TelephonyGlobals.java
+++ b/src/com/android/services/telephony/TelephonyGlobals.java
@@ -16,6 +16,11 @@
 
 package com.android.services.telephony;
 
+import android.content.Context;
+import android.telephony.TelephonyManager;
+
+import com.android.internal.telephony.PhoneFactory;
+
 /**
  * Singleton entry point for the telephony-services app. Initializes ongoing systems relating to
  * PSTN and SIP calls. This is started when the device starts and will be restarted automatically
@@ -27,7 +32,54 @@
  */
 public class TelephonyGlobals {
 
+    /** The application context. */
+    private final Context mContext;
+
+    /** Handles incoming calls for GSM. */
+    private IncomingCallNotifier mGsmIncomingCallNotifier;
+
+    /** Handles incoming calls for CDMA. */
+    private IncomingCallNotifier mCdmaIncomingCallNotifier;
+
+    /**
+     * Persists the specified parameters.
+     *
+     * @param context The application context.
+     */
+    public TelephonyGlobals(Context context) {
+        mContext = context.getApplicationContext();
+    }
+
     public void onCreate() {
-        // TODO(santoscordon): Start incoming-call listeners.
+        setupIncomingCallNotifiers();
+    }
+
+    /**
+     * Sets up incoming call notifiers for all the call services.
+     */
+    private void setupIncomingCallNotifiers() {
+        TelephonyManager telephonyManager = (TelephonyManager) mContext.getSystemService(
+                Context.TELEPHONY_SERVICE);
+
+        // TODO(santoscordon): getPhoneType() eventually executes
+        // PhoneInterfaceManager.getActivePhoneType() which references the PhoneProxy that is
+        // created in PhoneGlobals.onCreate(). IOW, this only works right now because PhoneApp calls
+        // PhoneGlobals.onCreate() *before* calling TelephonyGlobals.onCreate(). We want to
+        // eventually move that setup code into this method.
+        int phoneType = telephonyManager.getPhoneType();
+
+        if (TelephonyManager.PHONE_TYPE_GSM == phoneType) {
+            mGsmIncomingCallNotifier = new IncomingCallNotifier(
+                    GsmCallService.class, CachedPhoneFactory.getGsmPhone());
+
+        } else if (TelephonyManager.PHONE_TYPE_CDMA == phoneType) {
+            mCdmaIncomingCallNotifier = new IncomingCallNotifier(
+                    CdmaCallService.class, CachedPhoneFactory.getCdmaPhone());
+        }
+
+        // TODO(santoscordon): Do SIP.  SIP will require a slightly different solution since it
+        // doesn't have a phone object in the same way as PSTN calls. Additionally, the user can
+        // set up SIP to do outgoing calls, but not listen for incoming calls (uses extra battery).
+        // We need to make sure we take all of that into account.
     }
 }