Merge "Update config loader to use CONFIG_SERVICE_INTERFACE" into mnc-dev
diff --git a/sip/src/com/android/services/telephony/sip/SipAccountRegistry.java b/sip/src/com/android/services/telephony/sip/SipAccountRegistry.java
index b9ee69d..0a08c61 100644
--- a/sip/src/com/android/services/telephony/sip/SipAccountRegistry.java
+++ b/sip/src/com/android/services/telephony/sip/SipAccountRegistry.java
@@ -113,7 +113,7 @@
     }
 
     void setup(Context context) {
-        startSipProfilesAsync(context, (String) null);
+        startSipProfilesAsync(context, (String) null, false);
 
         // TODO: remove orphaned SIP Accounts
     }
@@ -124,9 +124,10 @@
      *
      * @param context The context.
      * @param sipProfileName The name of the {@link SipProfile} to start, or {@code null} for all.
+     * @param enableProfile Sip account should be enabled
      */
-    void startSipService(Context context, String sipProfileName) {
-        startSipProfilesAsync(context, sipProfileName);
+    void startSipService(Context context, String sipProfileName, boolean enableProfile) {
+        startSipProfilesAsync(context, sipProfileName, enableProfile);
     }
 
     /**
@@ -175,7 +176,7 @@
      * @param context The context.
      */
     public void restartSipService(Context context) {
-        startSipProfiles(context, null);
+        startSipProfiles(context, null, false);
     }
 
     /**
@@ -185,14 +186,16 @@
      *
      * @param context The context.
      * @param sipProfileName Name of the SIP profile.
+     * @param enableProfile Sip account should be enabled.
      */
-    private void startSipProfilesAsync(final Context context, final String sipProfileName) {
+    private void startSipProfilesAsync(
+            final Context context, final String sipProfileName, final boolean enableProfile) {
         if (VERBOSE) log("startSipProfiles, start auto registration");
 
         new Thread(new Runnable() {
             @Override
             public void run() {
-                startSipProfiles(context, sipProfileName);
+                startSipProfiles(context, sipProfileName, enableProfile);
             }}
         ).start();
     }
@@ -204,8 +207,9 @@
      *
      * @param context The context.
      * @param sipProfileName A specific SIP profile Name to start, or {@code null} to start all.
+     * @param enableProfile Sip account should be enabled.
      */
-    private void startSipProfiles(Context context, String sipProfileName) {
+    private void startSipProfiles(Context context, String sipProfileName, boolean enableProfile) {
         final SipSharedPreferences sipSharedPreferences = new SipSharedPreferences(context);
         boolean isReceivingCalls = sipSharedPreferences.isReceivingCallsEnabled();
         String primaryProfile = sipSharedPreferences.getPrimaryAccount();
@@ -220,6 +224,9 @@
             if (sipProfileName == null || sipProfileName.equals(profile.getProfileName())) {
                 PhoneAccount phoneAccount = SipUtil.createPhoneAccount(context, profile);
                 telecomManager.registerPhoneAccount(phoneAccount);
+                if (enableProfile) {
+                    telecomManager.enablePhoneAccount(phoneAccount.getAccountHandle(), true);
+                }
                 startSipServiceForProfile(profile, sipManager, context, isReceivingCalls);
             }
         }
diff --git a/sip/src/com/android/services/telephony/sip/SipEditor.java b/sip/src/com/android/services/telephony/sip/SipEditor.java
index d6f862a..6304220 100644
--- a/sip/src/com/android/services/telephony/sip/SipEditor.java
+++ b/sip/src/com/android/services/telephony/sip/SipEditor.java
@@ -244,12 +244,13 @@
      * {@link android.telecom.PhoneAccount}.
      *
      * @param p The {@link SipProfile} to register.
+     * @param enableProfile {@code true} if profile should be enabled, too.
      * @throws IOException Exception resulting from profile save.
      */
-    private void saveAndRegisterProfile(SipProfile p) throws IOException {
+    private void saveAndRegisterProfile(SipProfile p, boolean enableProfile) throws IOException {
         if (p == null) return;
         mProfileDb.saveProfile(p);
-        mSipAccountRegistry.startSipService(this, p.getProfileName());
+        mSipAccountRegistry.startSipService(this, p.getProfileName(), enableProfile);
     }
 
     /**
@@ -372,7 +373,8 @@
             public void run() {
                 try {
                     deleteAndUnregisterProfile(oldProfile);
-                    saveAndRegisterProfile(newProfile);
+                    boolean autoEnableNewProfile = oldProfile == null;
+                    saveAndRegisterProfile(newProfile, autoEnableNewProfile);
                     finish();
                 } catch (Exception e) {
                     log("replaceProfile, can not save/register new SipProfile, exception: " + e);