Move SIP settings to phone account settings.

+ Moved "SIP accounts" and "Receive incoming calls" settings from
the SIP settings creen to the Phone account settings screen.
+ To facilitate this, I moved some helper functions which used to
be in SipSettings to SipUtil. Their logical innards are for the
most part unchanged.
- Removed "SIP settings" preference screen added in call settings.
- Removed "Receive incoming calls" from SipSettings activity.
- Deleted unused SIP settings layout.
+ Changed back button behavior in SIP settings activity so that
back takes it to phone account settings, rather than call feature
settings.
+ Made some necessary classes/functions in sip/ public. Ihab said
that these were this way because of some refactoring at some point,
and it should be fine to make them public to achieve the necessary
class accessibility to facilitate this CL.

Bug: 17487262
Change-Id: I8e24abf624cadf9afc7aeff28781802732fef07d
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 02eb9c8..72cd63e 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -149,6 +149,8 @@
     <string name="wifi_calling_do_not_use">Do not use Wi-Fi calling</string>
     <!-- Indication for option to not use a call assistant. -->
     <string name="wifi_calling_do_not_use_call_assistant">Do not use call assistant</string>
+    <!-- Indication for option to not use a call assistant. -->
+    <string name="wifi_calling_call_assistant_none">None</string>
     <!-- Label for selected Wi-Fi call manager -->
     <string name="wifi_calling_call_assistant">Call assistant</string>
     <!-- Label for switch setting to enable/disable the use of SIM call managers. -->
diff --git a/res/xml/phone_account_settings.xml b/res/xml/phone_account_settings.xml
index 7c34b93..f812885 100644
--- a/res/xml/phone_account_settings.xml
+++ b/res/xml/phone_account_settings.xml
@@ -32,7 +32,7 @@
         <com.android.phone.settings.AccountSelectionPreference
             android:key="wifi_calling_call_assistant_preference"
             android:title="@string/wifi_calling_call_assistant"
-            android:summary="@string/wifi_calling_call_assistant_configure_no_selection" />
+            android:summary="@string/wifi_calling_call_assistant_none" />
 
         <Preference
             android:key="wifi_calling_configure_call_assistant_preference"
@@ -46,6 +46,16 @@
         android:title="@string/sip_settings"
         android:persistent="false">
 
+        <PreferenceScreen
+            android:title="@string/sip_accounts"
+            android:persistent="false">
+
+            <intent android:action="android.intent.action.MAIN"
+                android:targetPackage="com.android.phone"
+                android:targetClass="com.android.services.telephony.sip.SipSettings" />
+
+        </PreferenceScreen>
+
         <ListPreference
             android:key="use_sip_calling_options_key"
             android:title="@string/sip_call_options_title"
@@ -53,6 +63,12 @@
             android:entries="@array/sip_call_options_entries"
             android:entryValues="@array/sip_call_options_values"/>
 
+        <CheckBoxPreference
+            android:key="sip_receive_calls_key"
+            android:title="@string/sip_receive_calls"
+            android:summary="@string/sip_receive_calls_summary"
+            android:persistent="true"/>
+
     </PreferenceCategory>
 
 </PreferenceScreen>
diff --git a/sip/res/xml/sip_setting.xml b/sip/res/xml/sip_setting.xml
index 91b8311..fa31e7c 100644
--- a/sip/res/xml/sip_setting.xml
+++ b/sip/res/xml/sip_setting.xml
@@ -16,12 +16,6 @@
 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
     android:title="@string/sip_accounts">
 
-    <CheckBoxPreference
-        android:key="sip_receive_calls_key"
-        android:title="@string/sip_receive_calls"
-        android:summary="@string/sip_receive_calls_summary"
-        android:persistent="true"/>
-
     <PreferenceCategory android:key="sip_account_list" android:title="@string/sip_account_list"/>
 
 </PreferenceScreen>
diff --git a/sip/res/xml/sip_settings_category.xml b/sip/res/xml/sip_settings_category.xml
deleted file mode 100644
index 035a539..0000000
--- a/sip/res/xml/sip_settings_category.xml
+++ /dev/null
@@ -1,37 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2010 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.
--->
-<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
-    xmlns:phone="http://schemas.android.com/apk/res/com.android.phone">
-
-    <PreferenceScreen
-        android:key="sip_settings_preference_screen_key"
-        android:title="@string/sip_settings"
-        android:persistent="false" >
-
-        <PreferenceScreen
-                android:key="sip_account_settings_key"
-                android:title="@string/sip_accounts_title"
-                android:persistent="false">
-
-            <intent android:action="android.intent.action.MAIN"
-                    android:targetPackage="com.android.phone"
-                    android:targetClass="com.android.services.telephony.sip.SipSettings" />
-
-        </PreferenceScreen>
-
-    </PreferenceScreen>
-
-</PreferenceScreen>
diff --git a/sip/src/com/android/services/telephony/sip/SipAccountRegistry.java b/sip/src/com/android/services/telephony/sip/SipAccountRegistry.java
index c808133..b54df59 100644
--- a/sip/src/com/android/services/telephony/sip/SipAccountRegistry.java
+++ b/sip/src/com/android/services/telephony/sip/SipAccountRegistry.java
@@ -33,7 +33,7 @@
 /**
  * Manages the {@link PhoneAccount} entries for SIP calling.
  */
-final class SipAccountRegistry {
+public final class SipAccountRegistry {
     private final class AccountEntry {
         private final SipProfile mProfile;
 
@@ -108,7 +108,7 @@
 
     private SipAccountRegistry() {}
 
-    static SipAccountRegistry getInstance() {
+    public static SipAccountRegistry getInstance() {
         return INSTANCE;
     }
 
@@ -172,7 +172,7 @@
      *
      * @param context The context.
      */
-    void restartSipService(Context context) {
+    public void restartSipService(Context context) {
         startSipProfiles(context, null);
     }
 
diff --git a/sip/src/com/android/services/telephony/sip/SipSettings.java b/sip/src/com/android/services/telephony/sip/SipSettings.java
index 525fd0f..7da0131 100644
--- a/sip/src/com/android/services/telephony/sip/SipSettings.java
+++ b/sip/src/com/android/services/telephony/sip/SipSettings.java
@@ -66,8 +66,6 @@
 
     static final String KEY_SIP_PROFILE = "sip_profile";
 
-    private static final String BUTTON_SIP_RECEIVE_CALLS =
-            "sip_receive_calls_key";
     private static final String PREF_SIP_LIST = "sip_account_list";
 
     private static final int REQUEST_ADD_OR_EDIT_SIP_PROFILE = 1;
@@ -78,7 +76,6 @@
 
     private SipProfile mProfile; // profile that's being edited
 
-    private CheckBoxPreference mButtonSipReceiveCalls;
     private PreferenceCategory mSipListContainer;
     private Map<String, SipPreference> mSipPreferenceMap;
     private List<SipProfile> mSipProfileList;
@@ -146,7 +143,6 @@
         setContentView(R.layout.sip_settings_ui);
         addPreferencesFromResource(R.xml.sip_setting);
         mSipListContainer = (PreferenceCategory) findPreference(PREF_SIP_LIST);
-        registerForReceiveCallsCheckBox();
 
         updateProfilesStatus();
 
@@ -159,8 +155,6 @@
     @Override
     public void onResume() {
         super.onResume();
-
-        mButtonSipReceiveCalls.setEnabled(SipUtil.isPhoneIdle(this));
     }
 
     @Override
@@ -195,62 +189,6 @@
         }.start();
     }
 
-    private void registerForReceiveCallsCheckBox() {
-        mButtonSipReceiveCalls = (CheckBoxPreference) findPreference
-                (BUTTON_SIP_RECEIVE_CALLS);
-        mButtonSipReceiveCalls.setChecked(
-                mSipSharedPreferences.isReceivingCallsEnabled());
-        mButtonSipReceiveCalls.setOnPreferenceClickListener(
-                new OnPreferenceClickListener() {
-                    public boolean onPreferenceClick(Preference preference) {
-                        final boolean enabled = ((CheckBoxPreference) preference).isChecked();
-                        new Thread(new Runnable() {
-                                public void run() {
-                                    handleSipReceiveCallsOption(enabled);
-                                }
-                        }).start();
-                        return true;
-                    }
-                });
-    }
-
-    /**
-     * Handles changes to the "receive calls" option.
-     *
-     * @param isReceivingCalls {@code True} if receiving incoming SIP calls.
-     */
-    private synchronized void handleSipReceiveCallsOption(boolean isReceivingCalls) {
-        mSipSharedPreferences.setReceivingCallsEnabled(isReceivingCalls);
-
-        // Mark all profiles as auto-register if we are now receiving calls.
-        List<SipProfile> sipProfileList = mProfileDb.retrieveSipProfileList();
-        for (SipProfile p : sipProfileList) {
-            p = updateAutoRegistrationFlag(p, isReceivingCalls);
-        }
-
-        // Restart all Sip services to ensure we reflect whether we are receiving calls.
-        SipAccountRegistry sipAccountRegistry = SipAccountRegistry.getInstance();
-        sipAccountRegistry.restartSipService(this);
-
-        updateProfilesStatus();
-    }
-
-    private SipProfile updateAutoRegistrationFlag(SipProfile p, boolean enabled) {
-        SipProfile newProfile = new SipProfile.Builder(p)
-                .setAutoRegistration(enabled)
-                .build();
-        try {
-            // Note: The profile is updated, but the associated PhoneAccount is left alone since
-            // the only thing that changed is the auto-registration flag, which is not part of the
-            // PhoneAccount.
-            mProfileDb.deleteProfile(p);
-            mProfileDb.saveProfile(newProfile);
-        } catch (Exception e) {
-            log("updateAutoRegistrationFlag, exception: " + e);
-        }
-        return newProfile;
-    }
-
     private void updateProfilesStatus() {
         new Thread(new Runnable() {
             @Override
@@ -494,6 +432,10 @@
                 startSipEditor(null);
                 return true;
             }
+            case android.R.id.home: {
+                onBackPressed();
+                return true;
+            }
         }
         return super.onOptionsItemSelected(item);
     }
diff --git a/sip/src/com/android/services/telephony/sip/SipUtil.java b/sip/src/com/android/services/telephony/sip/SipUtil.java
index e4d5d4c..f93f02d 100644
--- a/sip/src/com/android/services/telephony/sip/SipUtil.java
+++ b/sip/src/com/android/services/telephony/sip/SipUtil.java
@@ -30,8 +30,10 @@
 import android.telecom.PhoneAccountHandle;
 import android.telecom.TelecomManager;
 import android.text.TextUtils;
+import android.util.Log;
 
 import java.util.ArrayList;
+import java.util.List;
 
 public class SipUtil {
     static final String LOG_TAG = "SIP";
@@ -59,7 +61,7 @@
         return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
     }
 
-    static boolean isPhoneIdle(Context context) {
+    public static boolean isPhoneIdle(Context context) {
         TelecomManager manager = (TelecomManager) context.getSystemService(
                 Context.TELECOM_SERVICE);
         if (manager != null) {
@@ -131,4 +133,34 @@
         final SipSharedPreferences sipSharedPreferences = new SipSharedPreferences(context);
         return sipSharedPreferences.getSipCallOption().equals(Settings.System.SIP_ALWAYS);
     }
+
+    /**
+     * Updates SIP accounts to indicate whether they are enabled to receive incoming SIP calls.
+     *
+     * @param isEnabled {@code True} if receiving incoming SIP calls.
+     */
+    public static void useSipToReceiveIncomingCalls(Context context, boolean isEnabled) {
+        SipProfileDb profileDb = new SipProfileDb(context);
+
+        // Mark all profiles as auto-register if we are now receiving calls.
+        List<SipProfile> sipProfileList = profileDb.retrieveSipProfileList();
+        for (SipProfile p : sipProfileList) {
+            updateAutoRegistrationFlag(p, profileDb, isEnabled);
+        }
+    }
+
+    private static void updateAutoRegistrationFlag(
+            SipProfile p, SipProfileDb db, boolean isEnabled) {
+        SipProfile newProfile = new SipProfile.Builder(p).setAutoRegistration(isEnabled).build();
+
+        try {
+            // Note: The profile is updated, but the associated PhoneAccount is left alone since
+            // the only thing that changed is the auto-registration flag, which is not part of the
+            // PhoneAccount.
+            db.deleteProfile(p);
+            db.saveProfile(newProfile);
+        } catch (Exception e) {
+            Log.d(LOG_TAG, "updateAutoRegistrationFlag, exception: " + e);
+        }
+    }
 }
diff --git a/src/com/android/phone/CallFeaturesSetting.java b/src/com/android/phone/CallFeaturesSetting.java
index d1bb25a..a4ec5c8 100644
--- a/src/com/android/phone/CallFeaturesSetting.java
+++ b/src/com/android/phone/CallFeaturesSetting.java
@@ -1647,12 +1647,6 @@
         updateVoiceNumberField();
         mVMProviderSettingsForced = false;
 
-        if (SipUtil.isVoipSupported(this)) {
-            addPreferencesFromResource(
-                    com.android.services.telephony.sip.R.xml.sip_settings_category);
-        }
-
-
         if (mButtonDTMF != null) {
             int dtmf = Settings.System.getInt(getContentResolver(),
                     Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, Constants.DTMF_TONE_TYPE_NORMAL);
diff --git a/src/com/android/phone/settings/PhoneAccountSettingsFragment.java b/src/com/android/phone/settings/PhoneAccountSettingsFragment.java
index fb6365e..5222ae4 100644
--- a/src/com/android/phone/settings/PhoneAccountSettingsFragment.java
+++ b/src/com/android/phone/settings/PhoneAccountSettingsFragment.java
@@ -2,22 +2,18 @@
 
 import android.content.ActivityNotFoundException;
 import android.content.Intent;
-import android.content.pm.PackageManager;
-import android.content.pm.ResolveInfo;
 import android.net.sip.SipManager;
-import android.net.sip.SipProfile;
 import android.os.Bundle;
 import android.preference.CheckBoxPreference;
 import android.preference.ListPreference;
 import android.preference.Preference;
 import android.preference.PreferenceFragment;
-import android.preference.PreferenceScreen;
-import android.preference.SwitchPreference;
 import android.telecom.PhoneAccountHandle;
 import android.telecom.TelecomManager;
 import android.util.Log;
 
 import com.android.phone.R;
+import com.android.services.telephony.sip.SipAccountRegistry;
 import com.android.services.telephony.sip.SipSharedPreferences;
 import com.android.services.telephony.sip.SipUtil;
 
@@ -43,6 +39,7 @@
 
     private static final String SIP_SETTINGS_CATEGORY_PREF_KEY = "phone_accounts_sip_settings_key";
     private static final String USE_SIP_PREF_KEY = "use_sip_calling_options_key";
+    private static final String SIP_RECEIVE_CALLS_PREF_KEY = "sip_receive_calls_key";
 
     private String LOG_TAG = PhoneAccountSettingsFragment.class.getSimpleName();
 
@@ -53,6 +50,7 @@
     private Preference mConfigureCallAssistant;
 
     private ListPreference mUseSipCalling;
+    private CheckBoxPreference mSipReceiveCallsPreference;
     private SipSharedPreferences mSipSharedPreferences;
 
     @Override
@@ -109,6 +107,13 @@
             mUseSipCalling.setValueIndex(
                     mUseSipCalling.findIndexOfValue(mSipSharedPreferences.getSipCallOption()));
             mUseSipCalling.setSummary(mUseSipCalling.getEntry());
+
+            mSipReceiveCallsPreference = (CheckBoxPreference)
+                    getPreferenceScreen().findPreference(SIP_RECEIVE_CALLS_PREF_KEY);
+            mSipReceiveCallsPreference.setEnabled(SipUtil.isPhoneIdle(getActivity()));
+            mSipReceiveCallsPreference.setChecked(
+                    mSipSharedPreferences.isReceivingCallsEnabled());
+            mSipReceiveCallsPreference.setOnPreferenceChangeListener(this);
         } else {
             getPreferenceScreen().removePreference(
                     getPreferenceScreen().findPreference(SIP_SETTINGS_CATEGORY_PREF_KEY));
@@ -116,8 +121,7 @@
     }
 
     /**
-     * Handles changes to the preferences, namely the switch which controls whether to use the call
-     * assistant or not.
+     * Handles changes to the preferences.
      *
      * @param pref The preference changed.
      * @param objValue The changed value.
@@ -131,6 +135,14 @@
             mUseSipCalling.setValueIndex(mUseSipCalling.findIndexOfValue(option));
             mUseSipCalling.setSummary(mUseSipCalling.getEntry());
             return true;
+        } else if (pref == mSipReceiveCallsPreference) {
+            final boolean isEnabled = !mSipReceiveCallsPreference.isChecked();
+            new Thread(new Runnable() {
+                public void run() {
+                    handleSipReceiveCallsOption(isEnabled);
+                }
+            }).start();
+            return true;
         }
         return false;
     }
@@ -191,6 +203,16 @@
         }
     }
 
+    private synchronized void handleSipReceiveCallsOption(boolean isEnabled) {
+        mSipSharedPreferences.setReceivingCallsEnabled(isEnabled);
+
+        SipUtil.useSipToReceiveIncomingCalls(getActivity(), isEnabled);
+
+        // Restart all Sip services to ensure we reflect whether we are receiving calls.
+        SipAccountRegistry sipAccountRegistry = SipAccountRegistry.getInstance();
+        sipAccountRegistry.restartSipService(getActivity());
+    }
+
     /**
      * Queries the telcomm manager to update the default outgoing account selection preference
      * with the list of outgoing accounts and the current default outgoing account.
@@ -213,7 +235,7 @@
                 mTelecomManager,
                 simCallManagers,
                 mTelecomManager.getSimCallManager(),
-                getString(R.string.wifi_calling_do_not_use_call_assistant));
+                getString(R.string.wifi_calling_call_assistant_none));
 
         updateConfigureCallAssistantSummary();
     }