CustomEditTextPreference.java revision 449f97204ad571d46589a5668051a34e7aa10f3f
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 */
16package com.android.settings;
17
18import android.app.AlertDialog;
19import android.app.Dialog;
20import android.content.Context;
21import android.content.DialogInterface;
22import android.os.Bundle;
23import android.support.v14.preference.EditTextPreferenceDialogFragment;
24import android.support.v7.preference.EditTextPreference;
25import android.util.AttributeSet;
26import android.view.View;
27import android.widget.EditText;
28
29public class CustomEditTextPreference extends EditTextPreference {
30
31    private CustomPreferenceDialogFragment mFragment;
32
33    public CustomEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
34        super(context, attrs, defStyleAttr, defStyleRes);
35    }
36
37    public CustomEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr) {
38        super(context, attrs, defStyleAttr);
39    }
40
41    public CustomEditTextPreference(Context context, AttributeSet attrs) {
42        super(context, attrs);
43    }
44
45    public CustomEditTextPreference(Context context) {
46        super(context);
47    }
48
49    public EditText getEditText() {
50        return mFragment != null ? (EditText) mFragment.getDialog().findViewById(android.R.id.edit)
51                : null;
52    }
53
54    public boolean isDialogOpen() {
55        return getDialog() != null && getDialog().isShowing();
56    }
57
58    public Dialog getDialog() {
59        return mFragment != null ? mFragment.getDialog() : null;
60    }
61
62    protected void onPrepareDialogBuilder(AlertDialog.Builder builder,
63            DialogInterface.OnClickListener listener) {
64    }
65
66    protected void onDialogClosed(boolean positiveResult) {
67    }
68
69    protected void onClick(DialogInterface dialog, int which) {
70    }
71
72    protected void onBindDialogView(View view) {
73    }
74
75    private void setFragment(CustomPreferenceDialogFragment fragment) {
76        mFragment = fragment;
77    }
78
79    public static class CustomPreferenceDialogFragment extends EditTextPreferenceDialogFragment {
80
81        public static CustomPreferenceDialogFragment newInstance(String key) {
82            final CustomPreferenceDialogFragment fragment = new CustomPreferenceDialogFragment();
83            final Bundle b = new Bundle(1);
84            b.putString(ARG_KEY, key);
85            fragment.setArguments(b);
86            return fragment;
87        }
88
89        private CustomEditTextPreference getCustomizablePreference() {
90            return (CustomEditTextPreference) getPreference();
91        }
92
93        @Override
94        protected void onBindDialogView(View view) {
95            super.onBindDialogView(view);
96            getCustomizablePreference().onBindDialogView(view);
97        }
98
99        @Override
100        protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
101            super.onPrepareDialogBuilder(builder);
102            getCustomizablePreference().setFragment(this);
103            getCustomizablePreference().onPrepareDialogBuilder(builder, this);
104        }
105
106        @Override
107        public void onDialogClosed(boolean positiveResult) {
108            super.onDialogClosed(positiveResult);
109            getCustomizablePreference().onDialogClosed(positiveResult);
110        }
111
112        @Override
113        public void onClick(DialogInterface dialog, int which) {
114            super.onClick(dialog, which);
115            getCustomizablePreference().onClick(dialog, which);
116        }
117    }
118}
119