1/*
2 * Copyright (C) 2015 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
17package android.support.v14.preference;
18
19import android.os.Bundle;
20import android.support.v7.preference.EditTextPreference;
21import android.view.View;
22import android.view.ViewGroup;
23import android.view.ViewParent;
24import android.widget.EditText;
25
26public class EditTextPreferenceDialogFragment extends PreferenceDialogFragment {
27
28    private EditText mEditText;
29
30    public static EditTextPreferenceDialogFragment newInstance(String key) {
31        final EditTextPreferenceDialogFragment
32                fragment = new EditTextPreferenceDialogFragment();
33        final Bundle b = new Bundle(1);
34        b.putString(ARG_KEY, key);
35        fragment.setArguments(b);
36        return fragment;
37    }
38
39    @Override
40    protected void onBindDialogView(View view) {
41        super.onBindDialogView(view);
42
43        mEditText = new EditText(view.getContext());
44        // Give it an ID so it can be saved/restored
45        mEditText.setId(android.R.id.edit);
46
47        mEditText.setText(getEditTextPreference().getText());
48
49        ViewParent oldParent = mEditText.getParent();
50        if (oldParent != view) {
51            if (oldParent != null) {
52                ((ViewGroup) oldParent).removeView(mEditText);
53            }
54            onAddEditTextToDialogView(view, mEditText);
55        }
56    }
57
58    private EditTextPreference getEditTextPreference() {
59        return (EditTextPreference) getPreference();
60    }
61
62    /** @hide */
63    @Override
64    protected boolean needInputMethod() {
65        // We want the input method to show, if possible, when dialog is displayed
66        return true;
67    }
68
69    /**
70     * Adds the EditText widget of this preference to the dialog's view.
71     *
72     * @param dialogView The dialog view.
73     */
74    protected void onAddEditTextToDialogView(View dialogView, EditText editText) {
75        ViewGroup container = (ViewGroup) dialogView
76                .findViewById(R.id.edittext_container);
77        if (container != null) {
78            container.addView(editText, ViewGroup.LayoutParams.FILL_PARENT,
79                    ViewGroup.LayoutParams.WRAP_CONTENT);
80        }
81    }
82
83    @Override
84    public void onDialogClosed(boolean positiveResult) {
85
86        if (positiveResult) {
87            String value = mEditText.getText().toString();
88            if (getEditTextPreference().callChangeListener(value)) {
89                getEditTextPreference().setText(value);
90            }
91        }
92    }
93
94}
95