Merge "Add intent broadcast when a PhoneAccount is enabled or disabled. (3/3)" into lmp-dev
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index dd9b06d..9bc9a58 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -499,6 +499,8 @@
                 <action android:name="com.android.phone.SIP_INCOMING_CALL" />
                 <action android:name="com.android.phone.SIP_REMOVE_PHONE" />
                 <action android:name="com.android.phone.SIP_CALL_OPTION_CHANGED" />
+                <action android:name="android.telecom.action.PHONE_ACCOUNT_ENABLED" />
+                <action android:name="android.telecom.action.PHONE_ACCOUNT_DISABLED" />
             </intent-filter>
         </receiver>
 
diff --git a/sip/src/com/android/services/telephony/sip/SipAccountRegistry.java b/sip/src/com/android/services/telephony/sip/SipAccountRegistry.java
index b00acba..09708b7 100644
--- a/sip/src/com/android/services/telephony/sip/SipAccountRegistry.java
+++ b/sip/src/com/android/services/telephony/sip/SipAccountRegistry.java
@@ -23,6 +23,7 @@
 import android.telecomm.PhoneAccount;
 import android.telecomm.PhoneAccountHandle;
 import android.telecomm.TelecommManager;
+import android.text.TextUtils;
 import android.util.Log;
 
 import java.util.List;
@@ -176,6 +177,38 @@
     }
 
     /**
+     * Handles a {@link PhoneAccount} becoming enabled by starting the SIP service for the
+     * {@link SipProfile} associated with that {@linke PhoneAccount}.
+     *
+     * @param context The context.
+     * @param phoneAccountHandle The handle for the {@link PhoneAccount} which is enabled.
+     */
+    void setPhoneAccountEnabled(Context context, PhoneAccountHandle phoneAccountHandle) {
+        String sipUri = SipUtil.getSipUriFromPhoneAccount(phoneAccountHandle);
+        if (sipUri == null) {
+            return;
+        }
+
+        startSipService(context, sipUri);
+    }
+
+    /**
+     * Handles a {@link PhoneAccount} becoming disabled by stopping the SIP service for the
+     * {@link SipProfile} associated with that {@linke PhoneAccount}.
+     *
+     * @param context The context.
+     * @param phoneAccountHandle The handle for the {@link PhoneAccount} which is disabled.
+     */
+    void setPhoneAccountDisabled(Context context, PhoneAccountHandle phoneAccountHandle) {
+        String sipUri = SipUtil.getSipUriFromPhoneAccount(phoneAccountHandle);
+        if (sipUri == null) {
+            return;
+        }
+
+        stopSipService(context, sipUri);
+    }
+
+    /**
      * Performs an asynchronous call to
      * {@link SipAccountRegistry#startSipProfiles(android.content.Context, String)}, starting the
      * specified SIP profile and registering its {@link android.telecomm.PhoneAccount}.
diff --git a/sip/src/com/android/services/telephony/sip/SipBroadcastReceiver.java b/sip/src/com/android/services/telephony/sip/SipBroadcastReceiver.java
index 35c07a1..37ab30c 100644
--- a/sip/src/com/android/services/telephony/sip/SipBroadcastReceiver.java
+++ b/sip/src/com/android/services/telephony/sip/SipBroadcastReceiver.java
@@ -51,6 +51,20 @@
             if (VERBOSE) log("SIP_REMOVE_PHONE " +
                             intent.getStringExtra(SipManager.EXTRA_LOCAL_URI));
             sipAccountRegistry.removeSipProfile(intent.getStringExtra(SipManager.EXTRA_LOCAL_URI));
+        } else if (action.equals(TelecommManager.ACTION_PHONE_ACCOUNT_ENABLED)) {
+            PhoneAccountHandle phoneAccountHandle = intent.getParcelableExtra(
+                    TelecommManager.EXTRA_PHONE_ACCOUNT_HANDLE);
+            if (phoneAccountHandle == null) {
+                return;
+            }
+            sipAccountRegistry.setPhoneAccountEnabled(context, phoneAccountHandle);
+        } else if (action.equals(TelecommManager.ACTION_PHONE_ACCOUNT_DISABLED)) {
+            PhoneAccountHandle phoneAccountHandle = intent.getParcelableExtra(
+                    TelecommManager.EXTRA_PHONE_ACCOUNT_HANDLE);
+            if (phoneAccountHandle == null) {
+                return;
+            }
+            sipAccountRegistry.setPhoneAccountDisabled(context, phoneAccountHandle);
         } else {
             if (VERBOSE) log("onReceive, action not processed: " + action);
         }
diff --git a/sip/src/com/android/services/telephony/sip/SipSettings.java b/sip/src/com/android/services/telephony/sip/SipSettings.java
index 1dca310..7747637 100644
--- a/sip/src/com/android/services/telephony/sip/SipSettings.java
+++ b/sip/src/com/android/services/telephony/sip/SipSettings.java
@@ -99,7 +99,8 @@
         void setProfile(SipProfile p) {
             mProfile = p;
             setTitle(getProfileName(p));
-            updateSummary(mSipSharedPreferences.isReceivingCallsEnabled()
+            boolean isEnabled = SipUtil.isPhoneAccountEnabled(getContext(), p);
+            updateSummary(isEnabled && mSipSharedPreferences.isReceivingCallsEnabled()
                     ? getString(R.string.registration_status_checking_status)
                     : getString(R.string.registration_status_not_receiving));
         }
@@ -299,7 +300,8 @@
 
         if (!mSipSharedPreferences.isReceivingCallsEnabled()) return;
         for (SipProfile p : mSipProfileList) {
-            if (mUid == p.getCallingUid()) {
+            if (mUid == p.getCallingUid() &&
+                    SipUtil.isPhoneAccountEnabled(getApplicationContext(), p)) {
                 try {
                     mSipManager.setRegistrationListener(
                             p.getUriString(), createRegistrationListener());
diff --git a/sip/src/com/android/services/telephony/sip/SipUtil.java b/sip/src/com/android/services/telephony/sip/SipUtil.java
index 7186c6c..6194902 100644
--- a/sip/src/com/android/services/telephony/sip/SipUtil.java
+++ b/sip/src/com/android/services/telephony/sip/SipUtil.java
@@ -29,6 +29,7 @@
 import android.telecomm.PhoneAccount;
 import android.telecomm.PhoneAccountHandle;
 import android.telecomm.TelecommManager;
+import android.text.TextUtils;
 
 import java.util.ArrayList;
 
@@ -76,6 +77,24 @@
     }
 
     /**
+     * Determines the SIP Uri for a specified {@link PhoneAccountHandle}.
+     *
+     * @param phoneAccountHandle The {@link PhoneAccountHandle}.
+     * @return The SIP Uri.
+     */
+    static String getSipUriFromPhoneAccount(PhoneAccountHandle phoneAccountHandle) {
+        if (phoneAccountHandle == null) {
+            return null;
+        }
+
+        String sipUri = phoneAccountHandle.getId();
+        if (TextUtils.isEmpty(sipUri)) {
+            return null;
+        }
+        return sipUri;
+    }
+
+    /**
      * Determines if the {@link android.telecomm.PhoneAccount} associated with a {@link SipProfile}
      * is enabled.
      *