Home | History | Annotate | Download | only in interactions
      1 /*
      2  * Copyright (C) 2010 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 package com.android.contacts.interactions;
     17 
     18 import com.android.contacts.R;
     19 
     20 import android.app.AlertDialog;
     21 import android.app.Dialog;
     22 import android.app.DialogFragment;
     23 import android.content.DialogInterface;
     24 import android.content.DialogInterface.OnShowListener;
     25 import android.os.Bundle;
     26 import android.text.Editable;
     27 import android.text.TextUtils;
     28 import android.text.TextWatcher;
     29 import android.view.LayoutInflater;
     30 import android.view.View;
     31 import android.widget.Button;
     32 import android.widget.EditText;
     33 
     34 /**
     35  * A common superclass for creating and renaming groups.
     36  */
     37 public abstract class GroupNameDialogFragment extends DialogFragment
     38         implements TextWatcher, OnShowListener {
     39     private EditText mEdit;
     40 
     41     protected abstract int getTitleResourceId();
     42     protected abstract void initializeGroupLabelEditText(EditText editText);
     43     protected abstract void onCompleted(String groupLabel);
     44 
     45     @Override
     46     public Dialog onCreateDialog(Bundle savedInstanceState) {
     47         View view = LayoutInflater.from(getActivity()).inflate(R.layout.group_name_dialog, null);
     48         mEdit = (EditText) view.findViewById(R.id.group_label);
     49         initializeGroupLabelEditText(mEdit);
     50 
     51         mEdit.addTextChangedListener(this);
     52 
     53         AlertDialog dialog = new AlertDialog.Builder(getActivity())
     54                 .setIconAttribute(android.R.attr.alertDialogIcon)
     55                 .setTitle(getTitleResourceId())
     56                 .setView(view)
     57                 .setPositiveButton(android.R.string.ok,
     58                     new DialogInterface.OnClickListener() {
     59                         @Override
     60                         public void onClick(DialogInterface dialog, int whichButton) {
     61                             onCompleted(mEdit.getText().toString().trim());
     62                         }
     63                     }
     64                 )
     65                 .setNegativeButton(android.R.string.cancel, null)
     66                 .create();
     67 
     68         dialog.setOnShowListener(this);
     69         return dialog;
     70     }
     71 
     72     public void onShow(DialogInterface dialog) {
     73         updateOkButtonState((AlertDialog) dialog);
     74     }
     75 
     76     @Override
     77     public void afterTextChanged(Editable s) {
     78         AlertDialog dialog = (AlertDialog) getDialog();
     79         // Make sure the dialog has not already been dismissed or destroyed.
     80         if (dialog != null) {
     81             updateOkButtonState(dialog);
     82         }
     83     }
     84 
     85     private void updateOkButtonState(AlertDialog dialog) {
     86         Button okButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
     87         okButton.setEnabled(!TextUtils.isEmpty(mEdit.getText().toString().trim()));
     88     }
     89 
     90     @Override
     91     public void beforeTextChanged(CharSequence s, int start, int count, int after) {
     92     }
     93 
     94     @Override
     95     public void onTextChanged(CharSequence s, int start, int before, int count) {
     96     }
     97 }
     98