Add internet connectivity check.

If the device is not internet-connected, we will not make a SIP call
and will pop up an alert dialog instead.

+enable internet call settings if device is in airplanemode.

Change-Id: I95d5166c171510afff627b3505a8910a895ff985
diff --git a/res/values/strings.xml b/res/values/strings.xml
index c4a273c..7c7c52e 100755
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -1341,6 +1341,9 @@
     <!-- Showing that some preference field can not be empty in an alert dialog. [CHAR LIMIT=NONE] -->
     <string name="empty_alert">%s cannot be empty</string>
 
+    <!-- Title of the alert dialog to notify user that there is no Internet connectivity. [CHAR LIMIT=NONE] -->
+    <string name="no_internet_available">To place an Internet call, check Internet connectiviy first</string>
+
     <!-- Do not translate the following strings. Used for the Internet call options. Need to be in-sync with Settings.System.SIP_ strings. -->
     <string translatable="false" name="sip_always">SIP_ALWAYS</string>
     <string translatable="false" name="sip_address_only">SIP_ADDRESS_ONLY</string>
diff --git a/res/xml/sip_settings_category.xml b/res/xml/sip_settings_category.xml
index f645ef8..4cead43 100644
--- a/res/xml/sip_settings_category.xml
+++ b/res/xml/sip_settings_category.xml
@@ -14,8 +14,7 @@
      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"
-        android:title="@string/sip_settings">
+    xmlns:phone="http://schemas.android.com/apk/res/com.android.phone">
     <PreferenceCategory
             android:key="sip_settings_category_key"
             android:title="@string/sip_settings"
diff --git a/src/com/android/phone/CallFeaturesSetting.java b/src/com/android/phone/CallFeaturesSetting.java
index 685ee5b..5a1193e 100644
--- a/src/com/android/phone/CallFeaturesSetting.java
+++ b/src/com/android/phone/CallFeaturesSetting.java
@@ -137,6 +137,8 @@
             "sip_receive_calls_key";
     private static final String BUTTON_SIP_CALL_OPTIONS =
             "sip_call_options_key";
+    private static final String SIP_SETTINGS_CATEGORY_KEY =
+            "sip_settings_category_key";
 
     private Intent mContactListIntent;
 
@@ -1474,7 +1476,10 @@
         }
         updateVoiceNumberField();
         mVMProviderSettingsForced = false;
+        createSipCallSettings();
+    }
 
+    private void createSipCallSettings() {
         // Add Internet call settings.
         if (SipManager.isVoipSupported(this)) {
             mSipManager = SipManager.getInstance(this);
@@ -1501,6 +1506,17 @@
         super.onResume();
         mForeground = true;
 
+        if (isAirplaneModeOn()) {
+            Preference sipSettings = findPreference(SIP_SETTINGS_CATEGORY_KEY);
+            PreferenceScreen screen = getPreferenceScreen();
+            int count = screen.getPreferenceCount();
+            for (int i = 0 ; i < count ; ++i) {
+                Preference pref = screen.getPreference(i);
+                if (pref != sipSettings) pref.setEnabled(false);
+            }
+            return;
+        }
+
         if (mButtonDTMF != null) {
             int dtmf = Settings.System.getInt(getContentResolver(),
                     Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, DTMF_TONE_TYPE_NORMAL);
@@ -1527,6 +1543,11 @@
         }
     }
 
+    private boolean isAirplaneModeOn() {
+        return Settings.System.getInt(getContentResolver(),
+                Settings.System.AIRPLANE_MODE_ON, 0) != 0;
+    }
+
     private void handleTTYChange(Preference preference, Object objValue) {
         int buttonTtyMode;
         buttonTtyMode = Integer.valueOf((String) objValue).intValue();
diff --git a/src/com/android/phone/SipCallOptionHandler.java b/src/com/android/phone/SipCallOptionHandler.java
index c20ad99..49c636d 100644
--- a/src/com/android/phone/SipCallOptionHandler.java
+++ b/src/com/android/phone/SipCallOptionHandler.java
@@ -22,6 +22,8 @@
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
+import android.net.ConnectivityManager;
+import android.net.NetworkInfo;
 import android.net.sip.SipManager;
 import android.net.sip.SipProfile;
 import android.net.Uri;
@@ -55,7 +57,8 @@
     static final int DIALOG_SELECT_PHONE_TYPE = 0;
     static final int DIALOG_SELECT_OUTGOING_SIP_PHONE = 1;
     static final int DIALOG_START_SIP_SETTINGS = 2;
-    static final int DIALOG_SIZE = 3;
+    static final int DIALOG_NO_INTERNET_ERROR = 3;
+    static final int DIALOG_SIZE = 4;
 
     private Intent mIntent;
     private List<SipProfile> mProfileList;
@@ -166,6 +169,13 @@
                     .setOnCancelListener(this)
                     .create();
             break;
+        case DIALOG_NO_INTERNET_ERROR:
+            dialog = new AlertDialog.Builder(this)
+                    .setTitle(R.string.no_internet_available)
+                    .setPositiveButton(android.R.string.ok, this)
+                    .setOnCancelListener(this)
+                    .create();
+            break;
         default:
             dialog = null;
         }
@@ -212,6 +222,9 @@
             }
         } else if (dialog == mDialogs[DIALOG_SELECT_OUTGOING_SIP_PHONE]) {
             mOutgoingSipProfile = mProfileList.get(id);
+        } else if (dialog == mDialogs[DIALOG_NO_INTERNET_ERROR]) {
+            finish();
+            return;
         } else {
             if (id == DialogInterface.BUTTON_POSITIVE) {
                 // Redirect to sip settings and drop the call.
@@ -260,6 +273,10 @@
         runOnUiThread(new Runnable() {
             public void run() {
                 if (mOutgoingSipProfile != null) {
+                    if (!isNetworkConnected()) {
+                        showDialog(DIALOG_NO_INTERNET_ERROR);
+                        return;
+                    }
                     Log.v(TAG, "primary SIP URI is " +
                             mOutgoingSipProfile.getUriString());
                     createSipPhoneIfNeeded(mOutgoingSipProfile);
@@ -281,6 +298,16 @@
         });
     }
 
+    private boolean isNetworkConnected() {
+        ConnectivityManager cm = (ConnectivityManager) getSystemService(
+                Context.CONNECTIVITY_SERVICE);
+        if (cm != null) {
+            NetworkInfo ni = cm.getActiveNetworkInfo();
+            return (ni != null && ni.isConnected());
+        }
+        return false;
+    }
+
     private void startGetPrimarySipPhoneThread() {
         new Thread(new Runnable() {
             public void run() {