Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7a5879cb authored by Benson Huang's avatar Benson Huang Committed by Android Git Automerger
Browse files

am 46104d8a: [FM] Only space character should not be allowed while renaming a...

am 46104d8a: [FM] Only space character should not be allowed while renaming a FM channel or a recorded audio file

* commit '46104d8a':
  [FM] Only space character should not be allowed while renaming a FM channel or a recorded audio file
parents 3dfb1097 46104d8a
Loading
Loading
Loading
Loading
+63 −8
Original line number Diff line number Diff line
@@ -24,7 +24,10 @@ import android.content.DialogInterface;
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 @@ public class FmFavoriteEditDialog extends DialogFragment {
    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 @@ public class FmFavoriteEditDialog extends DialogFragment {
        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 @@ public class FmFavoriteEditDialog extends DialogFragment {
                .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);
    }
}