1612997fe2e41366573855f56898b27d4c8787244George Mount/*
2612997fe2e41366573855f56898b27d4c8787244George Mount * Copyright (C) 2015 The Android Open Source Project
3612997fe2e41366573855f56898b27d4c8787244George Mount *
4612997fe2e41366573855f56898b27d4c8787244George Mount * Licensed under the Apache License, Version 2.0 (the "License");
5612997fe2e41366573855f56898b27d4c8787244George Mount * you may not use this file except in compliance with the License.
6612997fe2e41366573855f56898b27d4c8787244George Mount * You may obtain a copy of the License at
7612997fe2e41366573855f56898b27d4c8787244George Mount *
8612997fe2e41366573855f56898b27d4c8787244George Mount *      http://www.apache.org/licenses/LICENSE-2.0
9612997fe2e41366573855f56898b27d4c8787244George Mount *
10612997fe2e41366573855f56898b27d4c8787244George Mount * Unless required by applicable law or agreed to in writing, software
11612997fe2e41366573855f56898b27d4c8787244George Mount * distributed under the License is distributed on an "AS IS" BASIS,
12612997fe2e41366573855f56898b27d4c8787244George Mount * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13612997fe2e41366573855f56898b27d4c8787244George Mount * See the License for the specific language governing permissions and
14612997fe2e41366573855f56898b27d4c8787244George Mount * limitations under the License.
15612997fe2e41366573855f56898b27d4c8787244George Mount */
16fead9ca09b117136b35bc5bf137340a754f9edddGeorge Mountpackage android.databinding.adapters;
17612997fe2e41366573855f56898b27d4c8787244George Mount
18716ba89e7f459f49ea85070d4710c1d79d715298George Mountimport android.databinding.BindingAdapter;
19fead9ca09b117136b35bc5bf137340a754f9edddGeorge Mountimport android.databinding.BindingMethod;
20fead9ca09b117136b35bc5bf137340a754f9edddGeorge Mountimport android.databinding.BindingMethods;
21716ba89e7f459f49ea85070d4710c1d79d715298George Mountimport android.databinding.adapters.AdapterViewBindingAdapter.OnItemSelected;
22716ba89e7f459f49ea85070d4710c1d79d715298George Mountimport android.databinding.adapters.AdapterViewBindingAdapter.OnItemSelectedComponentListener;
23716ba89e7f459f49ea85070d4710c1d79d715298George Mountimport android.databinding.adapters.AdapterViewBindingAdapter.OnNothingSelected;
24716ba89e7f459f49ea85070d4710c1d79d715298George Mountimport android.widget.AutoCompleteTextView;
25716ba89e7f459f49ea85070d4710c1d79d715298George Mountimport android.widget.AutoCompleteTextView.Validator;
263561e3e665698843b1c664385a842e779198960bGeorge Mount
273561e3e665698843b1c664385a842e779198960bGeorge Mount@BindingMethods({
28716ba89e7f459f49ea85070d4710c1d79d715298George Mount        @BindingMethod(type = AutoCompleteTextView.class, attribute = "android:completionThreshold", method = "setThreshold"),
29716ba89e7f459f49ea85070d4710c1d79d715298George Mount        @BindingMethod(type = AutoCompleteTextView.class, attribute = "android:popupBackground", method = "setDropDownBackgroundDrawable"),
30716ba89e7f459f49ea85070d4710c1d79d715298George Mount        @BindingMethod(type = AutoCompleteTextView.class, attribute = "android:onDismiss", method = "setOnDismissListener"),
31716ba89e7f459f49ea85070d4710c1d79d715298George Mount        @BindingMethod(type = AutoCompleteTextView.class, attribute = "android:onItemClick", method = "setOnItemClickListener"),
323561e3e665698843b1c664385a842e779198960bGeorge Mount})
333561e3e665698843b1c664385a842e779198960bGeorge Mountpublic class AutoCompleteTextViewBindingAdapter {
34612997fe2e41366573855f56898b27d4c8787244George Mount
3596b22e7bbbf942aea1079dc8e8d0c4657663e5a7George Mount    @BindingAdapter(value = {"android:fixText", "android:isValid"}, requireAll = false)
3696b22e7bbbf942aea1079dc8e8d0c4657663e5a7George Mount    public static void setValidator(AutoCompleteTextView view, final FixText fixText,
37716ba89e7f459f49ea85070d4710c1d79d715298George Mount            final IsValid isValid) {
38716ba89e7f459f49ea85070d4710c1d79d715298George Mount        if (fixText == null && isValid == null) {
39716ba89e7f459f49ea85070d4710c1d79d715298George Mount            view.setValidator(null);
40716ba89e7f459f49ea85070d4710c1d79d715298George Mount        } else {
41716ba89e7f459f49ea85070d4710c1d79d715298George Mount            view.setValidator(new Validator() {
42716ba89e7f459f49ea85070d4710c1d79d715298George Mount                @Override
43716ba89e7f459f49ea85070d4710c1d79d715298George Mount                public boolean isValid(CharSequence text) {
44716ba89e7f459f49ea85070d4710c1d79d715298George Mount                    if (isValid != null) {
45716ba89e7f459f49ea85070d4710c1d79d715298George Mount                        return isValid.isValid(text);
46716ba89e7f459f49ea85070d4710c1d79d715298George Mount                    } else {
47716ba89e7f459f49ea85070d4710c1d79d715298George Mount                        return true;
48716ba89e7f459f49ea85070d4710c1d79d715298George Mount                    }
49716ba89e7f459f49ea85070d4710c1d79d715298George Mount                }
50716ba89e7f459f49ea85070d4710c1d79d715298George Mount
51716ba89e7f459f49ea85070d4710c1d79d715298George Mount                @Override
52716ba89e7f459f49ea85070d4710c1d79d715298George Mount                public CharSequence fixText(CharSequence invalidText) {
53716ba89e7f459f49ea85070d4710c1d79d715298George Mount                    if (fixText != null) {
54716ba89e7f459f49ea85070d4710c1d79d715298George Mount                        return fixText.fixText(invalidText);
55716ba89e7f459f49ea85070d4710c1d79d715298George Mount                    } else {
56716ba89e7f459f49ea85070d4710c1d79d715298George Mount                        return invalidText;
57716ba89e7f459f49ea85070d4710c1d79d715298George Mount                    }
58716ba89e7f459f49ea85070d4710c1d79d715298George Mount                }
59716ba89e7f459f49ea85070d4710c1d79d715298George Mount            });
60716ba89e7f459f49ea85070d4710c1d79d715298George Mount        }
61716ba89e7f459f49ea85070d4710c1d79d715298George Mount    }
62716ba89e7f459f49ea85070d4710c1d79d715298George Mount
6396b22e7bbbf942aea1079dc8e8d0c4657663e5a7George Mount    @BindingAdapter(value = {"android:onItemSelected", "android:onNothingSelected"},
6496b22e7bbbf942aea1079dc8e8d0c4657663e5a7George Mount            requireAll = false)
6596b22e7bbbf942aea1079dc8e8d0c4657663e5a7George Mount    public static void setOnItemSelectedListener(AutoCompleteTextView view,
6696b22e7bbbf942aea1079dc8e8d0c4657663e5a7George Mount            final OnItemSelected selected, final OnNothingSelected nothingSelected) {
67716ba89e7f459f49ea85070d4710c1d79d715298George Mount        if (selected == null && nothingSelected == null) {
68716ba89e7f459f49ea85070d4710c1d79d715298George Mount            view.setOnItemSelectedListener(null);
69716ba89e7f459f49ea85070d4710c1d79d715298George Mount        } else {
70716ba89e7f459f49ea85070d4710c1d79d715298George Mount            view.setOnItemSelectedListener(
713b920788e90bb0abe615a5d5c899915f0014444bGeorge Mount                    new OnItemSelectedComponentListener(selected, nothingSelected, null));
72716ba89e7f459f49ea85070d4710c1d79d715298George Mount        }
73716ba89e7f459f49ea85070d4710c1d79d715298George Mount    }
74716ba89e7f459f49ea85070d4710c1d79d715298George Mount
75716ba89e7f459f49ea85070d4710c1d79d715298George Mount    public interface IsValid {
76716ba89e7f459f49ea85070d4710c1d79d715298George Mount        boolean isValid(CharSequence text);
77716ba89e7f459f49ea85070d4710c1d79d715298George Mount    }
78716ba89e7f459f49ea85070d4710c1d79d715298George Mount
79716ba89e7f459f49ea85070d4710c1d79d715298George Mount    public interface FixText {
80716ba89e7f459f49ea85070d4710c1d79d715298George Mount        CharSequence fixText(CharSequence invalidText);
81716ba89e7f459f49ea85070d4710c1d79d715298George Mount    }
82612997fe2e41366573855f56898b27d4c8787244George Mount}
83