Add custom message dialog for Respond via Message.

We don't want to require SMS applications to implement activities that show
over lock just to handle the "respond via custom message" scenario for
rejecting phone calls. This change adds an entry dialog into the phone call
UI. A separate change removes the code in the Telephony service that would
have asked the default SMS app to show UI.

Bug: 11084719 Provide UI for "quick response" messages built into Phone app
Change-Id: I7fcf20280fd3b741aa941cc6a24f5c262db1899b
diff --git a/InCallUI/res/values/strings.xml b/InCallUI/res/values/strings.xml
index e7049e5..7c588b3 100755
--- a/InCallUI/res/values/strings.xml
+++ b/InCallUI/res/values/strings.xml
@@ -1280,6 +1280,10 @@
     <string name="respond_via_sms_canned_response_4">Can\'t talk now. Call me later?</string>
     <!-- "Respond via SMS" option that lets you compose a custom response. [CHAR LIMIT=30] -->
     <string name="respond_via_sms_custom_message">Write your own...</string>
+    <!-- "Custom Message" Cancel alert dialog button -->
+    <string name="custom_message_cancel">Cancel</string>
+    <!-- "Custom Message" Send alert dialog button -->
+    <string name="custom_message_send">Send</string>
 
     <!-- Title of settings screen for managing the "Respond via SMS" feature. [CHAR LIMIT=30] -->
     <string name="respond_via_sms_setting_title">Quick responses</string>
diff --git a/InCallUI/src/com/android/incallui/AnswerFragment.java b/InCallUI/src/com/android/incallui/AnswerFragment.java
index 324d522..29a4586 100644
--- a/InCallUI/src/com/android/incallui/AnswerFragment.java
+++ b/InCallUI/src/com/android/incallui/AnswerFragment.java
@@ -18,12 +18,18 @@
 
 import android.app.AlertDialog;
 import android.app.Dialog;
+import android.content.DialogInterface;
 import android.os.Bundle;
+import android.text.Editable;
+import android.text.TextWatcher;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
+import android.view.WindowManager;
 import android.widget.AdapterView;
 import android.widget.ArrayAdapter;
+import android.widget.Button;
+import android.widget.EditText;
 import android.widget.ListView;
 
 import com.google.common.base.Preconditions;
@@ -106,12 +112,7 @@
     }
 
     @Override
-    public boolean isMessageDialogueShowing() {
-        return mCannedResponsePopup != null && mCannedResponsePopup.isShowing();
-    }
-
-    @Override
-    public void showMessageDialogue() {
+    public void showMessageDialog() {
         final ListView lv = new ListView(getActivity());
 
         Preconditions.checkNotNull(mTextResponsesAdapter);
@@ -130,16 +131,65 @@
      * This is safe to call even if the popup is already dismissed, and even if you never called
      * showRespondViaSmsPopup() in the first place.
      */
-    @Override
-    public void dismissPopup() {
+    private void dismissPopup() {
         if (mCannedResponsePopup != null) {
             mCannedResponsePopup.dismiss();  // safe even if already dismissed
             mCannedResponsePopup = null;
         }
     }
 
+    /**
+     * Shows the custom message entry dialog.
+     */
+    public void showCustomMessageDialog() {
+        // Create an alert dialog containing an EditText
+        final EditText et = new EditText(getActivity());
+        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()).setCancelable(
+                true).setView(et)
+                .setPositiveButton(R.string.custom_message_send,
+                        new DialogInterface.OnClickListener() {
+                    @Override
+                    public void onClick(DialogInterface dialog, int which) {
+                        getPresenter().rejectCallWithMessage(et.getText().toString().trim());
+                    }
+                })
+                .setNegativeButton(R.string.custom_message_cancel,
+                        new DialogInterface.OnClickListener() {
+                    @Override
+                    public void onClick(DialogInterface dialog, int which) {
+                    }
+                })
+                .setTitle(R.string.respond_via_sms_custom_message);
+        final AlertDialog customResponseDialog = builder.create();
+
+        // Enable/disable the send button based on whether there is a message in the EditText
+        et.addTextChangedListener(new TextWatcher() {
+            @Override
+            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
+            }
+            @Override
+            public void onTextChanged(CharSequence s, int start, int before, int count) {
+            }
+            @Override
+            public void afterTextChanged(Editable s) {
+                final Button sendButton = customResponseDialog.getButton(
+                        DialogInterface.BUTTON_POSITIVE);
+                sendButton.setEnabled(s != null && s.toString().trim().length() != 0);
+            }
+        });
+
+        // Keyboard up, show the dialog
+        customResponseDialog.getWindow().setSoftInputMode(
+                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
+        customResponseDialog.show();
+
+        // Send button starts out disabled
+        final Button sendButton = customResponseDialog.getButton(DialogInterface.BUTTON_POSITIVE);
+        sendButton.setEnabled(false);
+    }
+
     @Override
-    public void configureMessageDialogue(ArrayList<String> textResponses) {
+    public void configureMessageDialog(ArrayList<String> textResponses) {
         final ArrayList<String> textResponsesForDisplay = new ArrayList<String>(textResponses);
 
         textResponsesForDisplay.add(getResources().getString(
@@ -178,12 +228,13 @@
             Log.d(this, "RespondViaSmsItemClickListener.onItemClick(" + position + ")...");
             final String message = (String) parent.getItemAtPosition(position);
             Log.v(this, "- message: '" + message + "'");
+            dismissPopup();
 
             // The "Custom" choice is a special case.
             // (For now, it's guaranteed to be the last item.)
             if (position == (parent.getCount() - 1)) {
-                // Take the user to the standard SMS compose UI.
-                getPresenter().rejectCallWithMessage(null);
+                // Show the custom message dialog
+                showCustomMessageDialog();
             } else {
                 getPresenter().rejectCallWithMessage(message);
             }
diff --git a/InCallUI/src/com/android/incallui/AnswerPresenter.java b/InCallUI/src/com/android/incallui/AnswerPresenter.java
index b3deb64..6d70f81 100644
--- a/InCallUI/src/com/android/incallui/AnswerPresenter.java
+++ b/InCallUI/src/com/android/incallui/AnswerPresenter.java
@@ -93,7 +93,7 @@
 
         if (call.can(Call.Capabilities.RESPOND_VIA_TEXT) && textMsgs != null) {
             getUi().showTextButton(true);
-            getUi().configureMessageDialogue(textMsgs);
+            getUi().configureMessageDialog(textMsgs);
         } else {
             getUi().showTextButton(false);
         }
@@ -134,24 +134,19 @@
 
     public void onText() {
         if (getUi() != null) {
-            getUi().showMessageDialogue();
+            getUi().showMessageDialog();
         }
     }
 
     public void rejectCallWithMessage(String message) {
         Log.d(this, "sendTextToDefaultActivity()...");
-        if (getUi() != null) {
-            getUi().dismissPopup();
-        }
         CallCommandClient.getInstance().rejectCall(mCallId, true, message);
     }
 
     interface AnswerUi extends Ui {
         public void showAnswerUi(boolean show);
         public void showTextButton(boolean show);
-        public boolean isMessageDialogueShowing();
-        public void showMessageDialogue();
-        public void dismissPopup();
-        public void configureMessageDialogue(ArrayList<String> textResponses);
+        public void showMessageDialog();
+        public void configureMessageDialog(ArrayList<String> textResponses);
     }
 }