• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.view.WindowManager;
32 import android.widget.Button;
33 import android.widget.EditText;
34 
35 /**
36  * A common superclass for creating and renaming groups.
37  */
38 public abstract class GroupNameDialogFragment extends DialogFragment {
getTitleResourceId()39     protected abstract int getTitleResourceId();
initializeGroupLabelEditText(EditText editText)40     protected abstract void initializeGroupLabelEditText(EditText editText);
onCompleted(String groupLabel)41     protected abstract void onCompleted(String groupLabel);
42 
43     @Override
onCreateDialog(Bundle savedInstanceState)44     public Dialog onCreateDialog(Bundle savedInstanceState) {
45         final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
46         final LayoutInflater layoutInflater = LayoutInflater.from(builder.getContext());
47         final View view = layoutInflater.inflate(R.layout.group_name_dialog, null);
48         final EditText editText = (EditText) view.findViewById(R.id.group_label);
49         initializeGroupLabelEditText(editText);
50 
51         builder.setTitle(getTitleResourceId());
52         builder.setView(view);
53         editText.requestFocus();
54         builder.setPositiveButton(android.R.string.ok,
55                 new DialogInterface.OnClickListener() {
56                     @Override
57                     public void onClick(DialogInterface dialogInterface, int whichButton) {
58                         onCompleted(editText.getText().toString().trim());
59                     }
60                 }
61             );
62 
63         builder.setNegativeButton(android.R.string.cancel, null);
64         final AlertDialog dialog = builder.create();
65 
66         dialog.setOnShowListener(new OnShowListener() {
67             @Override
68             public void onShow(DialogInterface dialogInterface) {
69                 updateOkButtonState(dialog, editText);
70             }
71         });
72         editText.addTextChangedListener(new TextWatcher() {
73             @Override
74             public void onTextChanged(CharSequence s, int start, int before, int count) {
75             }
76 
77             @Override
78             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
79             }
80 
81             @Override
82             public void afterTextChanged(Editable s) {
83                 updateOkButtonState(dialog, editText);
84             }
85         });
86         dialog.getWindow().setSoftInputMode(
87                 WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
88         return dialog;
89     }
90 
updateOkButtonState(AlertDialog dialog, EditText editText)91     /* package */ void updateOkButtonState(AlertDialog dialog, EditText editText) {
92         final Button okButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
93         okButton.setEnabled(!TextUtils.isEmpty(editText.getText().toString().trim()));
94     }
95 }
96