Merge "Check voicemail capability when "1" is longpressed"
diff --git a/res/values/strings.xml b/res/values/strings.xml
index f4b94af..e33debb 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -1811,7 +1811,15 @@
     <!-- Button label to prompt the user to add another account (when there are already existing accounts on the device) [CHAR LIMIT=30] -->
     <string name="add_new_account">Add new account</string>
 
-    <!-- Dialog message which is shown when the user tries to make a phone call
-         to prohibited phone numbers [CHAR LIMIT=NONE] -->
-    <string name="phone_call_prohibited" msgid="4313552620858880999">Call not sent.</string>
+    <!-- Dialog title which is shown when the user tries to make a phone call
+         to prohibited phone numbers [CHAR LIMIT=40] -->
+    <string name="dialog_phone_call_prohibited_title" msgid="4313552620858880999">Call not sent</string>
+
+    <!-- Dialog title which is shown when the user tries to check voicemail
+         while the system isn't ready for the access. [CHAR LIMIT=40] -->
+    <string name="dialog_voicemail_not_ready_title">Voicemail number unavailable</string>
+
+    <!-- Dialog message which is shown when the user tries to check voicemail
+         while the system isn't ready for the access. [CHAR LIMIT=NONE] -->
+    <string name="dialog_voicemail_not_ready_message">To configure voicemail, go to Menu &gt; Settings.</string>
 </resources>
diff --git a/src/com/android/contacts/dialpad/DialpadFragment.java b/src/com/android/contacts/dialpad/DialpadFragment.java
index d1c5868..1790b9e 100644
--- a/src/com/android/contacts/dialpad/DialpadFragment.java
+++ b/src/com/android/contacts/dialpad/DialpadFragment.java
@@ -132,7 +132,6 @@
 
     private boolean mShowOptionsMenu;
 
-    private boolean mHasVoicemail = false;
 
     // Last number dialed, retrieved asynchronously from the call DB
     // in onCreate. This number is displayed when the user hits the
@@ -271,8 +270,6 @@
 
         mAdditionalButtonsRow = fragmentView.findViewById(R.id.dialpadAdditionalButtons);
 
-        initVoicemailButton();
-
         mSearchButton = mAdditionalButtonsRow.findViewById(R.id.searchButton);
         if (mSearchButton != null) {
             mSearchButton.setOnClickListener(this);
@@ -790,8 +787,15 @@
                 return true;
             }
             case R.id.one: {
-                if (mHasVoicemail && isDigitsEmpty()) {
-                    callVoicemail();
+                if (isDigitsEmpty()) {
+                    if (isVoicemailAvailable()) {
+                        callVoicemail();
+                    } else if (getActivity() != null) {
+                        DialogFragment dialogFragment = ErrorDialogFragment.newInstance(
+                                R.string.dialog_voicemail_not_ready_title,
+                                R.string.dialog_voicemail_not_ready_message);
+                        dialogFragment.show(getFragmentManager(), "voicemail_not_ready");
+                    }
                     return true;
                 }
                 return false;
@@ -810,23 +814,57 @@
         getActivity().finish();
     }
 
-    public static class CallProhibitedDialogFragment extends DialogFragment {
-        public static CallProhibitedDialogFragment newInstance() {
-            return new CallProhibitedDialogFragment();
+    public static class ErrorDialogFragment extends DialogFragment {
+        private int mTitleResId;
+        private Integer mMessageResId;  // can be null
+
+        private static final String ARG_TITLE_RES_ID = "argTitleResId";
+        private static final String ARG_MESSAGE_RES_ID = "argMessageResId";
+
+        public static ErrorDialogFragment newInstance(int titleResId) {
+            return newInstanceInter(titleResId, null);
+        }
+
+        public static ErrorDialogFragment newInstance(int titleResId, int messageResId) {
+            return newInstanceInter(titleResId, messageResId);
+        }
+
+        private static ErrorDialogFragment newInstanceInter(
+                int titleResId, Integer messageResId) {
+            final ErrorDialogFragment fragment = new ErrorDialogFragment();
+            final Bundle args = new Bundle();
+            args.putInt(ARG_TITLE_RES_ID, titleResId);
+            if (messageResId != null) {
+                args.putInt(ARG_MESSAGE_RES_ID, messageResId);
+            }
+            fragment.setArguments(args);
+            return fragment;
+        }
+
+        @Override
+        public void onCreate(Bundle savedInstanceState) {
+            super.onCreate(savedInstanceState);
+            mTitleResId = getArguments().getInt(ARG_TITLE_RES_ID);
+            if (getArguments().containsKey(ARG_MESSAGE_RES_ID)) {
+                mMessageResId = getArguments().getInt(ARG_MESSAGE_RES_ID);
+            }
         }
 
         @Override
         public Dialog onCreateDialog(Bundle savedInstanceState) {
-            return new AlertDialog.Builder(getActivity())
-                    .setTitle(R.string.phone_call_prohibited)
+            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
+            builder.setTitle(mTitleResId)
                     .setPositiveButton(android.R.string.ok,
                             new DialogInterface.OnClickListener() {
                                 @Override
                                 public void onClick(DialogInterface dialog, int which) {
                                     dismiss();
                                 }
-                            })
-                    .create();
+                            });
+            if (mMessageResId != null) {
+                builder.setMessage(mMessageResId);
+            }
+            return builder.create();
         }
     }
 
@@ -892,7 +930,8 @@
                     && (SystemProperties.getInt("persist.radio.otaspdial", 0) != 1)) {
                 Log.i(TAG, "The phone number is prohibited explicitly by a rule.");
                 if (getActivity() != null) {
-                    DialogFragment dialogFragment = CallProhibitedDialogFragment.newInstance();
+                    DialogFragment dialogFragment = ErrorDialogFragment.newInstance(
+                                    R.string.dialog_phone_call_prohibited_title);
                     dialogFragment.show(getFragmentManager(), "phone_prohibited_dialog");
                 }
 
@@ -1290,13 +1329,19 @@
 
     /**
      * Check if voicemail is enabled/accessible.
+     *
+     * @return true if voicemail is enabled and accessibly. Note that this can be false
+     * "temporarily" after the app boot.
+     * @see TelephonyManager#getVoiceMailNumber()
      */
-    private void initVoicemailButton() {
+    private boolean isVoicemailAvailable() {
         try {
-            mHasVoicemail = TelephonyManager.getDefault().getVoiceMailNumber() != null;
+            return (TelephonyManager.getDefault().getVoiceMailNumber() != null);
         } catch (SecurityException se) {
             // Possibly no READ_PHONE_STATE privilege.
+            Log.w(TAG, "SecurityException is thrown. Maybe privilege isn't sufficient.");
         }
+        return false;
     }
 
     /**