Home | History | Annotate | Download | only in dialogs
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.fmradio.dialogs;
     18 
     19 import android.app.Activity;
     20 import android.app.AlertDialog;
     21 import android.app.Dialog;
     22 import android.app.DialogFragment;
     23 import android.content.DialogInterface;
     24 import android.os.Bundle;
     25 import android.text.Editable;
     26 import android.text.Selection;
     27 import android.text.TextUtils;
     28 import android.text.TextWatcher;
     29 import android.view.View;
     30 import android.widget.Button;
     31 import android.widget.EditText;
     32 
     33 import com.android.fmradio.R;
     34 
     35 /**
     36  * Edit favorite station name and frequency, caller should implement
     37  * EditFavoriteListener
     38  */
     39 public class FmFavoriteEditDialog extends DialogFragment {
     40     private static final String STATION_NAME = "station_name";
     41     private static final String STATION_FREQ = "station_freq";
     42     private EditFavoriteListener mListener = null;
     43     private EditText mStationNameEditor = null;
     44 
     45     /**
     46      * Create edit favorite dialog instance, caller should implement edit
     47      * favorite listener
     48      *
     49      * @param stationName The station name
     50      * @param stationFreq The station frequency
     51      * @return edit favorite dialog
     52      */
     53     public static FmFavoriteEditDialog newInstance(String stationName, int stationFreq) {
     54         FmFavoriteEditDialog fragment = new FmFavoriteEditDialog();
     55         Bundle args = new Bundle(2);
     56         args.putString(STATION_NAME, stationName);
     57         args.putInt(STATION_FREQ, stationFreq);
     58         fragment.setArguments(args);
     59         return fragment;
     60     }
     61 
     62     /**
     63      * Edit favorite listener
     64      */
     65     public interface EditFavoriteListener {
     66         /**
     67          * Edit favorite station name and station frequency
     68          */
     69         void editFavorite(int stationFreq, String name);
     70     }
     71 
     72     @Override
     73     public void onAttach(Activity activity) {
     74         super.onAttach(activity);
     75         try {
     76             mListener = (EditFavoriteListener) activity;
     77         } catch (ClassCastException e) {
     78             e.printStackTrace();
     79         }
     80     }
     81 
     82     @Override
     83     public Dialog onCreateDialog(Bundle savedInstanceState) {
     84         String stationName = getArguments().getString(STATION_NAME);
     85         final int stationFreq = getArguments().getInt(STATION_FREQ);
     86         View v = View.inflate(getActivity(), R.layout.editstation, null);
     87         mStationNameEditor = (EditText) v.findViewById(
     88                 R.id.dlg_edit_station_name_text);
     89 
     90         if (null == stationName || "".equals(stationName.trim())) {
     91             stationName = "";
     92         }
     93 
     94         mStationNameEditor.requestFocus();
     95         mStationNameEditor.requestFocusFromTouch();
     96         // Edit
     97         mStationNameEditor.setText(stationName);
     98         Editable text = mStationNameEditor.getText();
     99         Selection.setSelection(text, text.length());
    100         return new AlertDialog.Builder(getActivity())
    101                 // Must call setTitle here or the title will not be displayed.
    102                 .setTitle(getString(R.string.rename)).setView(v)
    103                 .setPositiveButton(R.string.save,
    104                         new DialogInterface.OnClickListener() {
    105                             public void onClick(DialogInterface dialog, int which) {
    106                                 String newName = mStationNameEditor.getText().toString().trim();
    107                                 mListener.editFavorite(stationFreq, newName);
    108                             }
    109                         })
    110                 .setNegativeButton(android.R.string.cancel, null)
    111                 .create();
    112     }
    113 
    114     /**
    115      * Set the dialog edit text and other attribute.
    116      */
    117     @Override
    118     public void onResume() {
    119         super.onResume();
    120         setTextChangedCallback();
    121         String toName = mStationNameEditor.getText().toString();
    122         // empty or blank or white space only name is not allowed
    123         toggleSaveButton(toName != null && TextUtils.getTrimmedLength(toName) > 0);
    124     }
    125 
    126     /**
    127      * This method register callback and set filter to Edit, in order to make
    128      * sure that user input is legal. The input can't be empty/blank/all-spaces filename
    129      */
    130     private void setTextChangedCallback() {
    131         mStationNameEditor.addTextChangedListener(new TextWatcher() {
    132             // not use, so don't need to implement it
    133             @Override
    134             public void afterTextChanged(Editable arg0) {
    135             }
    136 
    137             // not use, so don't need to implement it
    138             @Override
    139             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    140             }
    141 
    142             /**
    143              * check user input whether is null or all white space.
    144              */
    145             @Override
    146             public void onTextChanged(CharSequence s, int start, int before, int count) {
    147                 // empty or blank or white space only name is not allowed
    148                 toggleSaveButton(TextUtils.getTrimmedLength(s) > 0);
    149             }
    150         });
    151     }
    152 
    153     /**
    154      * This method enables or disables save button to forbid renaming station name to null.
    155      * @param isEnabled true to enable save button, false to disable save button
    156      */
    157     private void toggleSaveButton(boolean isEnabled) {
    158         final AlertDialog dialog = (AlertDialog) getDialog();
    159         if (dialog == null) {
    160             return;
    161         }
    162         final Button button = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
    163         button.setEnabled(isEnabled);
    164     }
    165 }
    166