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 */
16package com.android.contacts.interactions;
17
18import com.android.contacts.R;
19
20import android.app.AlertDialog;
21import android.app.Dialog;
22import android.app.DialogFragment;
23import android.content.DialogInterface;
24import android.content.DialogInterface.OnShowListener;
25import android.os.Bundle;
26import android.text.Editable;
27import android.text.TextUtils;
28import android.text.TextWatcher;
29import android.view.LayoutInflater;
30import android.view.View;
31import android.widget.Button;
32import android.widget.EditText;
33
34/**
35 * A common superclass for creating and renaming groups.
36 */
37public 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