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

Commit 46104d8a authored by Benson Huang's avatar Benson Huang Committed by Nicholas Sauer
Browse files

[FM] Only space character should not be allowed while renaming a FM channel or...

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

Launch FM app and plug in a wired headset -> Star mark a channel
to add it favorite -> Go to favorite, tap on the menu (3 dots) and
tap on rename to rename the channel -> Enter only spaces and save
the channel -> Record an audio and on the dialogue, rename it with
only spaces and tap on save, the channel names and audio files are
saved without any error message. While playing this channel, FM
notification shows no name because of the spaces

The fix is to add some condition check for showing/hiding save button.

Bug 19087268
from: https://partner-android-review.googlesource.com/#/c/198815/



Change-Id: I2ac0b81d2311eace3d5e28baca31d59d3095c8d1
Signed-off-by: default avatarBenson Huang <benson.huang@mediatek.com>
parent 31fe68d9
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);
    }
}