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/common/src/com/android/services/telephony/common/Call.java b/common/src/com/android/services/telephony/common/Call.java
index 36c6315..8e7a595 100644
--- a/common/src/com/android/services/telephony/common/Call.java
+++ b/common/src/com/android/services/telephony/common/Call.java
@@ -208,6 +208,7 @@
         mCallId = call.mCallId;
         mIdentification = new CallIdentification(call.mIdentification);
         mState = call.mState;
+        mIsWifiCall = call.mIsWifiCall;
         mDisconnectCause = call.mDisconnectCause;
         mCapabilities = call.mCapabilities;
         mConnectTime = call.mConnectTime;
@@ -240,6 +241,10 @@
         mState = state;
     }
 
+    public void setIsWifiCall(boolean isWifiCall) {
+        mIsWifiCall = isWifiCall;
+    }
+
     public boolean isWifiCall() {
         return mIsWifiCall;
     }
@@ -348,6 +353,7 @@
     public void writeToParcel(Parcel dest, int flags) {
         dest.writeInt(mCallId);
         dest.writeInt(mState);
+        dest.writeByte((byte) (mIsWifiCall ? 1 : 0));
         dest.writeString(getDisconnectCause().toString());
         dest.writeInt(getCapabilities());
         dest.writeLong(getConnectTime());
@@ -363,6 +369,7 @@
     private Call(Parcel in) {
         mCallId = in.readInt();
         mState = in.readInt();
+        mIsWifiCall = in.readByte() != 0;
         mDisconnectCause = DisconnectCause.valueOf(in.readString());
         mCapabilities = in.readInt();
         mConnectTime = in.readLong();
@@ -399,6 +406,7 @@
         return Objects.toStringHelper(this)
                 .add("mCallId", mCallId)
                 .add("mState", STATE_MAP.get(mState))
+                .add("mIsWifiCall", mIsWifiCall)
                 .add("mDisconnectCause", mDisconnectCause)
                 .add("mCapabilities", mCapabilities)
                 .add("mConnectTime", mConnectTime)
diff --git a/res/values/array.xml b/res/values/array.xml
index db62307..c8c2e61 100644
--- a/res/values/array.xml
+++ b/res/values/array.xml
@@ -46,7 +46,7 @@
     </string-array>
 
     <!-- android.telephony.TelephonyManager.WifiCallingChoices -->
-    <string-array name="wifi_calling_choice_values">
+    <string-array translatable="false" name="wifi_calling_choice_values">
         <item>@string/wifi_calling_choices_always_use</item>
         <item>@string/wifi_calling_choices_ask_every_time</item>
         <item>@string/wifi_calling_choices_never_use</item>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 04bd7fe..4b9ac2f 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -1162,12 +1162,8 @@
     <!-- Item of Wi-Fi settings: For asking each time whether to use Wi-Fi. [CHAR LIMIT=NONE] -->
     <string name="wifi_calling_choice_entry_ask_every_time">Always ask</string>
 
-    <!-- Keep values in sync with android.telephony.TelephonyManager.WifiCallingChoices -->
-    <!-- android.telephony.TelephonyManager.WifiCallingChoices.ALWAYS_USE -->
     <string translatable="false" name="wifi_calling_choices_always_use">ALWAYS_USE</string>
-    <!-- android.telephony.TelephonyManager.WifiCallingChoices.NEVER_USE -->
     <string translatable="false" name="wifi_calling_choices_never_use">NEVER_USE</string>
-    <!-- android.telephony.TelephonyManager.WifiCallingChoices.ASK_EVERY_TIME -->
     <string translatable="false" name="wifi_calling_choices_ask_every_time">ASK_EVERY_TIME</string>
 
     <!-- Title of the dialog to select the phone type for the outgoing call. [CHAR LIMIT=NONE] -->
@@ -1541,4 +1537,13 @@
     <!-- String resource for the font-family to use for the call setting activity's title
          Do not translate. -->
     <string name="call_settings_title_font_family">sans-serif-light</string>
+
+    <!-- Title of the dialog to choose WiFi calling. -->
+    <string name ="choose_wifi_for_call_msg">Use Wi-Fi to place call?</string>
+
+    <!-- Title for button to use WiFi calling. -->
+    <string name ="choose_wifi_for_call_yes">Yes</string>
+
+    <!-- Title for button to not use WiFi calling. -->
+    <string name ="choose_wifi_for_call_no">No</string>
 </resources>
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;
                     }
                 }