Check WiFi call setting

This CL does the following:
  - checks the WiFi call setting when placing calls
  - if the WiFi call setting is ASK_EVERY_TIME then displays a dialog
    asking the user to choose
  - changes the default WiFi call setting to always use WiFi calling
  - set Call.isWifiCall when using Wifi calling

Change-Id: I9dc6086c41d4fe1b2a693ff44b5266697eae0f89
diff --git a/src/com/android/phone/CallFeaturesSetting.java b/src/com/android/phone/CallFeaturesSetting.java
index 0d21f49..01719b7 100644
--- a/src/com/android/phone/CallFeaturesSetting.java
+++ b/src/com/android/phone/CallFeaturesSetting.java
@@ -53,6 +53,7 @@
 import android.provider.MediaStore;
 import android.provider.Settings;
 import android.telephony.PhoneNumberUtils;
+import android.telephony.TelephonyManager;
 import android.text.TextUtils;
 import android.util.Log;
 import android.view.MenuItem;
@@ -1753,14 +1754,40 @@
      * @see android.telephony.TelephonyManager.WifiCallingChoices
      */
     private String getWhenToMakeWifiCalls() {
-        return PhoneInterfaceManager.getWhenToMakeWifiCallsStr(mPhone.getContext());
+        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(
+                Context.TELEPHONY_SERVICE);
+        int intValue = telephonyManager.getWhenToMakeWifiCalls();
+        switch (intValue) {
+            case TelephonyManager.WifiCallingChoices.ALWAYS_USE:
+                return getString(R.string.wifi_calling_choices_always_use);
+            case TelephonyManager.WifiCallingChoices.ASK_EVERY_TIME:
+                return getString(R.string.wifi_calling_choices_ask_every_time);
+            case TelephonyManager.WifiCallingChoices.NEVER_USE:
+                return getString(R.string.wifi_calling_choices_never_use);
+            default:
+                Log.wtf(LOG_TAG, "unknown wifi call int value: " + intValue);
+                return getString(R.string.wifi_calling_choices_always_use);
+        }
     }
 
     /**
      * @see android.telephony.TelephonyManager.WifiCallingChoices
      */
-    public void setWhenToMakeWifiCalls(String value) {
-        PhoneInterfaceManager.setWhenToMakeWifiCallsStr(mPhone.getContext(), value);
+    public void setWhenToMakeWifiCalls(String stringValue) {
+        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(
+                Context.TELEPHONY_SERVICE);
+        int intValue;
+        if (stringValue.equals(getString(R.string.wifi_calling_choices_always_use))) {
+            intValue = TelephonyManager.WifiCallingChoices.ALWAYS_USE;
+        } else if (stringValue.equals(getString(R.string.wifi_calling_choices_ask_every_time))) {
+            intValue = TelephonyManager.WifiCallingChoices.ASK_EVERY_TIME;
+        } else if (stringValue.equals(getString(R.string.wifi_calling_choices_never_use))) {
+            intValue = TelephonyManager.WifiCallingChoices.NEVER_USE;
+        } else {
+            Log.wtf(LOG_TAG, "unknown wifi call string value: " + stringValue);
+            return;
+        }
+        telephonyManager.setWhenToMakeWifiCalls(intValue);
     }
 
     // Gets the call options for SIP depending on whether SIP is allowed only
diff --git a/src/com/android/phone/CallModeler.java b/src/com/android/phone/CallModeler.java
index e4ed147..3e37f29 100644
--- a/src/com/android/phone/CallModeler.java
+++ b/src/com/android/phone/CallModeler.java
@@ -555,6 +555,13 @@
             changed = true;
         }
 
+        final boolean isWifiCall = connection.getCall().getPhone().getPhoneType() ==
+                PhoneConstants.PHONE_TYPE_THIRD_PARTY;
+        if (call.isWifiCall() != isWifiCall) {
+            call.setIsWifiCall(isWifiCall);
+            changed = true;
+        }
+
         final Call.DisconnectCause newDisconnectCause =
                 translateDisconnectCauseFromTelephony(connection.getDisconnectCause());
         if (call.getDisconnectCause() != newDisconnectCause) {
@@ -650,7 +657,7 @@
         final boolean supportHold = PhoneUtils.okToSupportHold(mCallManager);
         final boolean canHold = (supportHold ? PhoneUtils.okToHoldCall(mCallManager) : false);
         final boolean genericConf = isForConference &&
-                (connection.getCall().getPhone().getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA);
+                (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA);
 
         // only applies to active calls
         if (callIsActive) {
diff --git a/src/com/android/phone/PhoneInterfaceManager.java b/src/com/android/phone/PhoneInterfaceManager.java
index 145f9a7..8074152 100644
--- a/src/com/android/phone/PhoneInterfaceManager.java
+++ b/src/com/android/phone/PhoneInterfaceManager.java
@@ -951,70 +951,21 @@
      * @see android.telephony.TelephonyManager.WifiCallingChoices
      */
     public int getWhenToMakeWifiCalls() {
-        return getWhenToMakeWifiCalls(mPhone.getContext());
+        return Settings.System.getInt(mPhone.getContext().getContentResolver(),
+                Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
     }
 
     /**
      * @see android.telephony.TelephonyManager.WifiCallingChoices
      */
     public void setWhenToMakeWifiCalls(int preference) {
-        setWhenToMakeWifiCalls(mPhone.getContext(), preference);
+        if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
+        Settings.System.putInt(mPhone.getContext().getContentResolver(),
+                Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
     }
 
-    private static int getWhenToMakeWifiCalls(Context context) {
-        String setting = getWhenToMakeWifiCallsStr(context);
-        if (setting == null) {
-            return TelephonyManager.WifiCallingChoices.NEVER_USE;
-        } else if (setting.equals(context.getString(
-                R.string.wifi_calling_choices_always_use))) {
-            return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
-        } else if (setting.equals(context.getString(
-                R.string.wifi_calling_choices_ask_every_time))) {
-            return TelephonyManager.WifiCallingChoices.ASK_EVERY_TIME;
-        } else if (setting.equals(context.getString(
-                R.string.wifi_calling_choices_never_use))) {
-            return TelephonyManager.WifiCallingChoices.NEVER_USE;
-        } else {
-            // Should be unreachable
-            return TelephonyManager.WifiCallingChoices.NEVER_USE;
-        }
-    }
-
-    private static void setWhenToMakeWifiCalls(Context context, int preference) {
-        int setting;
-        switch (preference) {
-            case TelephonyManager.WifiCallingChoices.ALWAYS_USE:
-                setting = R.string.wifi_calling_choices_always_use;
-                break;
-            case TelephonyManager.WifiCallingChoices.ASK_EVERY_TIME:
-                setting = R.string.wifi_calling_choices_ask_every_time;
-                break;
-            case TelephonyManager.WifiCallingChoices.NEVER_USE:
-                setting = R.string.wifi_calling_choices_never_use;
-                break;
-            default:
-                // Should be unreachable
-                setting = R.string.wifi_calling_choices_never_use;
-                break;
-        }
-        setWhenToMakeWifiCallsStr(context, context.getString(setting));
-    }
-
-    /** Public for utility use by other classes */
-    public static String getWhenToMakeWifiCallsStr(Context context) {
-        String str = Settings.System.getString(context.getContentResolver(),
-                Settings.System.WHEN_TO_MAKE_WIFI_CALLS);
-        if (str == null) {
-            str = context.getString(R.string.wifi_calling_choices_never_use);
-        }
-        if (DBG) log("getWhenToMakeWifiCallsStr, retrieving setting = " + str);
-        return str;
-    }
-
-    /** Public for utility use by other classes */
-    public static void setWhenToMakeWifiCallsStr(Context context, String setting) {
-        Settings.System.putString(context.getContentResolver(),
-                Settings.System.WHEN_TO_MAKE_WIFI_CALLS, setting);
-        if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + setting);
+    private static int getWhenToMakeWifiCallsDefaultPreference() {
+        // TODO(sail): Use a build property to choose this value.
+        return TelephonyManager.WifiCallingChoices.NEVER_USE;
     }
 }
diff --git a/src/com/android/phone/SipCallOptionHandler.java b/src/com/android/phone/SipCallOptionHandler.java
index 84fb7e3..a071755 100644
--- a/src/com/android/phone/SipCallOptionHandler.java
+++ b/src/com/android/phone/SipCallOptionHandler.java
@@ -45,6 +45,7 @@
 import android.os.SystemProperties;
 import android.provider.Settings;
 import android.telephony.PhoneNumberUtils;
+import android.telephony.TelephonyManager;
 import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
@@ -77,7 +78,8 @@
     static final int DIALOG_START_SIP_SETTINGS = 2;
     static final int DIALOG_NO_INTERNET_ERROR = 3;
     static final int DIALOG_NO_VOIP = 4;
-    static final int DIALOG_SIZE = 5;
+    static final int DIALOG_SELECT_WIFI_CALL = 5;
+    static final int DIALOG_SIZE = 6;
 
     private Intent mIntent;
     private List<SipProfile> mProfileList;
@@ -90,6 +92,7 @@
     private TextView mUnsetPriamryHint;
     private boolean mUseSipPhone = false;
     private boolean mMakePrimary = false;
+    private ComponentName mThirdPartyCallComponent;
 
     private static final int EVENT_DELAYED_FINISH = 1;
 
@@ -301,6 +304,14 @@
                     .setOnCancelListener(this)
                     .create();
             break;
+        case DIALOG_SELECT_WIFI_CALL:
+            dialog = new AlertDialog.Builder(this)
+                    .setMessage(R.string.choose_wifi_for_call_msg)
+                    .setPositiveButton(R.string.choose_wifi_for_call_yes, this)
+                    .setNegativeButton(R.string.choose_wifi_for_call_no, this)
+                    .setCancelable(false)
+                    .create();
+            break;
         default:
             dialog = null;
         }
@@ -336,7 +347,14 @@
     }
 
     public void onClick(DialogInterface dialog, int id) {
-        if (id == DialogInterface.BUTTON_NEGATIVE) {
+        if(dialog == mDialogs[DIALOG_SELECT_WIFI_CALL]) {
+            if (id == DialogInterface.BUTTON_NEGATIVE) {
+                setResultAndFinish();
+            } else {
+                useThirdPartyPhone();
+            }
+            return;
+        } else if (id == DialogInterface.BUTTON_NEGATIVE) {
             // button negative is cancel
             finish();
             return;
@@ -486,24 +504,36 @@
         return false;
     }
 
+    private void useThirdPartyPhone() {
+        mIntent.putExtra(OutgoingCallBroadcaster.EXTRA_THIRD_PARTY_CALL_COMPONENT,
+                mThirdPartyCallComponent);
+        PhoneGlobals.getInstance().callController.placeCall(mIntent);
+        startDelayedFinish();
+    }
+
     private void checkThirdPartyPhone() {
         runOnUiThread(new Runnable() {
             public void run() {
                 // TODO(sail): Move this out of this file or rename this file.
                 // TODO(sail): Move this logic to the switchboard.
-                if (isConnectedToWifi()) {
+                TelephonyManager telephonyManager = (TelephonyManager) getSystemService(
+                        Context.TELEPHONY_SERVICE);
+                int setting = telephonyManager.getWhenToMakeWifiCalls();
+                if (setting != TelephonyManager.WifiCallingChoices.NEVER_USE &&
+                        isConnectedToWifi()) {
                     Intent intent = new Intent(ThirdPartyPhone.ACTION_THIRD_PARTY_CALL_SERVICE);
                     PackageManager pm = getPackageManager();
                     // TODO(sail): Need to handle case where there are multiple services.
                     // TODO(sail): Need to enforce permissions.
                     ResolveInfo info = pm.resolveService(intent, 0);
                     if (info != null && info.serviceInfo != null) {
-                        ComponentName component = new ComponentName(info.serviceInfo.packageName,
+                        mThirdPartyCallComponent = new ComponentName(info.serviceInfo.packageName,
                                 info.serviceInfo.name);
-                        mIntent.putExtra(OutgoingCallBroadcaster.EXTRA_THIRD_PARTY_CALL_COMPONENT,
-                                component);
-                        PhoneGlobals.getInstance().callController.placeCall(mIntent);
-                        startDelayedFinish();
+                        if (setting == TelephonyManager.WifiCallingChoices.ASK_EVERY_TIME) {
+                            showDialog(DIALOG_SELECT_WIFI_CALL);
+                        } else {
+                            useThirdPartyPhone();
+                        }
                         return;
                     }
                 }