Merge "[FM] There is no sound output after resuming playing when FM is over BT" into lmp-mr1-dev
diff --git a/src/com/android/fmradio/dialogs/FmFavoriteEditDialog.java b/src/com/android/fmradio/dialogs/FmFavoriteEditDialog.java
index c2bfc24..ca07c6f 100644
--- a/src/com/android/fmradio/dialogs/FmFavoriteEditDialog.java
+++ b/src/com/android/fmradio/dialogs/FmFavoriteEditDialog.java
@@ -24,7 +24,10 @@
 import android.os.Bundle;
 import android.text.Editable;
 import android.text.Selection;
+import android.text.TextUtils;
+import android.text.TextWatcher;
 import android.view.View;
+import android.widget.Button;
 import android.widget.EditText;
 
 import com.android.fmradio.R;
@@ -37,7 +40,7 @@
     private static final String STATION_NAME = "station_name";
     private static final String STATION_FREQ = "station_freq";
     private EditFavoriteListener mListener = null;
-    private EditText mEditTextFrequency = null;
+    private EditText mStationNameEditor = null;
 
     /**
      * Create edit favorite dialog instance, caller should implement edit
@@ -81,18 +84,18 @@
         String stationName = getArguments().getString(STATION_NAME);
         final int stationFreq = getArguments().getInt(STATION_FREQ);
         View v = View.inflate(getActivity(), R.layout.editstation, null);
-        final EditText editTextStationName = (EditText) v.findViewById(
+        mStationNameEditor = (EditText) v.findViewById(
                 R.id.dlg_edit_station_name_text);
 
-        if (null == stationName || "".equals(stationName)) {
+        if (null == stationName || "".equals(stationName.trim())) {
             stationName = "";
         }
 
-        editTextStationName.requestFocus();
-        editTextStationName.requestFocusFromTouch();
+        mStationNameEditor.requestFocus();
+        mStationNameEditor.requestFocusFromTouch();
         // Edit
-        editTextStationName.setText(stationName);
-        Editable text = editTextStationName.getText();
+        mStationNameEditor.setText(stationName);
+        Editable text = mStationNameEditor.getText();
         Selection.setSelection(text, text.length());
         return new AlertDialog.Builder(getActivity())
                 // Must call setTitle here or the title will not be displayed.
@@ -100,11 +103,63 @@
                 .setPositiveButton(R.string.save,
                         new DialogInterface.OnClickListener() {
                             public void onClick(DialogInterface dialog, int which) {
-                                String newName = editTextStationName.getText().toString();
+                                String newName = mStationNameEditor.getText().toString().trim();
                                 mListener.editFavorite(stationFreq, newName);
                             }
                         })
                 .setNegativeButton(android.R.string.cancel, null)
                 .create();
     }
+
+    /**
+     * Set the dialog edit text and other attribute.
+     */
+    @Override
+    public void onResume() {
+        super.onResume();
+        setTextChangedCallback();
+        String toName = mStationNameEditor.getText().toString();
+        // empty or blank or white space only name is not allowed
+        toggleSaveButton(toName != null && TextUtils.getTrimmedLength(toName) > 0);
+    }
+
+    /**
+     * This method register callback and set filter to Edit, in order to make
+     * sure that user input is legal. The input can't be empty/blank/all-spaces filename
+     */
+    private void setTextChangedCallback() {
+        mStationNameEditor.addTextChangedListener(new TextWatcher() {
+            // not use, so don't need to implement it
+            @Override
+            public void afterTextChanged(Editable arg0) {
+            }
+
+            // not use, so don't need to implement it
+            @Override
+            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
+            }
+
+            /**
+             * check user input whether is null or all white space.
+             */
+            @Override
+            public void onTextChanged(CharSequence s, int start, int before, int count) {
+                // empty or blank or white space only name is not allowed
+                toggleSaveButton(TextUtils.getTrimmedLength(s) > 0);
+            }
+        });
+    }
+
+    /**
+     * This method enables or disables save button to forbid renaming station name to null.
+     * @param isEnabled true to enable save button, false to disable save button
+     */
+    private void toggleSaveButton(boolean isEnabled) {
+        final AlertDialog dialog = (AlertDialog) getDialog();
+        if (dialog == null) {
+            return;
+        }
+        final Button button = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
+        button.setEnabled(isEnabled);
+    }
 }