1716ba89e7f459f49ea85070d4710c1d79d715298George Mount/*
2716ba89e7f459f49ea85070d4710c1d79d715298George Mount * Copyright (C) 2015 The Android Open Source Project
3716ba89e7f459f49ea85070d4710c1d79d715298George Mount *
4716ba89e7f459f49ea85070d4710c1d79d715298George Mount * Licensed under the Apache License, Version 2.0 (the "License");
5716ba89e7f459f49ea85070d4710c1d79d715298George Mount * you may not use this file except in compliance with the License.
6716ba89e7f459f49ea85070d4710c1d79d715298George Mount * You may obtain a copy of the License at
7716ba89e7f459f49ea85070d4710c1d79d715298George Mount *
8716ba89e7f459f49ea85070d4710c1d79d715298George Mount *      http://www.apache.org/licenses/LICENSE-2.0
9716ba89e7f459f49ea85070d4710c1d79d715298George Mount *
10716ba89e7f459f49ea85070d4710c1d79d715298George Mount * Unless required by applicable law or agreed to in writing, software
11716ba89e7f459f49ea85070d4710c1d79d715298George Mount * distributed under the License is distributed on an "AS IS" BASIS,
12716ba89e7f459f49ea85070d4710c1d79d715298George Mount * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13716ba89e7f459f49ea85070d4710c1d79d715298George Mount * See the License for the specific language governing permissions and
14716ba89e7f459f49ea85070d4710c1d79d715298George Mount * limitations under the License.
15716ba89e7f459f49ea85070d4710c1d79d715298George Mount */
16716ba89e7f459f49ea85070d4710c1d79d715298George Mountpackage android.databinding.adapters;
17716ba89e7f459f49ea85070d4710c1d79d715298George Mount
18716ba89e7f459f49ea85070d4710c1d79d715298George Mountimport android.databinding.BindingAdapter;
193b920788e90bb0abe615a5d5c899915f0014444bGeorge Mountimport android.databinding.InverseBindingListener;
203b920788e90bb0abe615a5d5c899915f0014444bGeorge Mountimport android.databinding.InverseBindingMethod;
213b920788e90bb0abe615a5d5c899915f0014444bGeorge Mountimport android.databinding.InverseBindingMethods;
22716ba89e7f459f49ea85070d4710c1d79d715298George Mountimport android.widget.SeekBar;
23716ba89e7f459f49ea85070d4710c1d79d715298George Mountimport android.widget.SeekBar.OnSeekBarChangeListener;
24716ba89e7f459f49ea85070d4710c1d79d715298George Mount
253b920788e90bb0abe615a5d5c899915f0014444bGeorge Mount@InverseBindingMethods({
263b920788e90bb0abe615a5d5c899915f0014444bGeorge Mount        @InverseBindingMethod(type = SeekBar.class, attribute = "android:progress"),
273b920788e90bb0abe615a5d5c899915f0014444bGeorge Mount})
28716ba89e7f459f49ea85070d4710c1d79d715298George Mountpublic class SeekBarBindingAdapter {
293b920788e90bb0abe615a5d5c899915f0014444bGeorge Mount
303b920788e90bb0abe615a5d5c899915f0014444bGeorge Mount    @BindingAdapter("android:progress")
313b920788e90bb0abe615a5d5c899915f0014444bGeorge Mount    public static void setProgress(SeekBar view, int progress) {
323b920788e90bb0abe615a5d5c899915f0014444bGeorge Mount        if (progress != view.getProgress()) {
333b920788e90bb0abe615a5d5c899915f0014444bGeorge Mount            view.setProgress(progress);
343b920788e90bb0abe615a5d5c899915f0014444bGeorge Mount        }
353b920788e90bb0abe615a5d5c899915f0014444bGeorge Mount    }
363b920788e90bb0abe615a5d5c899915f0014444bGeorge Mount
3796b22e7bbbf942aea1079dc8e8d0c4657663e5a7George Mount    @BindingAdapter(value = {"android:onStartTrackingTouch", "android:onStopTrackingTouch",
383b920788e90bb0abe615a5d5c899915f0014444bGeorge Mount            "android:onProgressChanged", "android:progressAttrChanged"}, requireAll = false)
3996b22e7bbbf942aea1079dc8e8d0c4657663e5a7George Mount    public static void setOnSeekBarChangeListener(SeekBar view, final OnStartTrackingTouch start,
403b920788e90bb0abe615a5d5c899915f0014444bGeorge Mount            final OnStopTrackingTouch stop, final OnProgressChanged progressChanged,
413b920788e90bb0abe615a5d5c899915f0014444bGeorge Mount            final InverseBindingListener attrChanged) {
423b920788e90bb0abe615a5d5c899915f0014444bGeorge Mount        if (start == null && stop == null && progressChanged == null && attrChanged == null) {
43716ba89e7f459f49ea85070d4710c1d79d715298George Mount            view.setOnSeekBarChangeListener(null);
44716ba89e7f459f49ea85070d4710c1d79d715298George Mount        } else {
45716ba89e7f459f49ea85070d4710c1d79d715298George Mount            view.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
46716ba89e7f459f49ea85070d4710c1d79d715298George Mount                @Override
47716ba89e7f459f49ea85070d4710c1d79d715298George Mount                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
48716ba89e7f459f49ea85070d4710c1d79d715298George Mount                    if (progressChanged != null) {
49716ba89e7f459f49ea85070d4710c1d79d715298George Mount                        progressChanged.onProgressChanged(seekBar, progress, fromUser);
50716ba89e7f459f49ea85070d4710c1d79d715298George Mount                    }
513b920788e90bb0abe615a5d5c899915f0014444bGeorge Mount                    if (attrChanged != null) {
523b920788e90bb0abe615a5d5c899915f0014444bGeorge Mount                        attrChanged.onChange();
533b920788e90bb0abe615a5d5c899915f0014444bGeorge Mount                    }
54716ba89e7f459f49ea85070d4710c1d79d715298George Mount                }
55716ba89e7f459f49ea85070d4710c1d79d715298George Mount
56716ba89e7f459f49ea85070d4710c1d79d715298George Mount                @Override
57716ba89e7f459f49ea85070d4710c1d79d715298George Mount                public void onStartTrackingTouch(SeekBar seekBar) {
58716ba89e7f459f49ea85070d4710c1d79d715298George Mount                    if (start != null) {
59716ba89e7f459f49ea85070d4710c1d79d715298George Mount                        start.onStartTrackingTouch(seekBar);
60716ba89e7f459f49ea85070d4710c1d79d715298George Mount                    }
61716ba89e7f459f49ea85070d4710c1d79d715298George Mount                }
62716ba89e7f459f49ea85070d4710c1d79d715298George Mount
63716ba89e7f459f49ea85070d4710c1d79d715298George Mount                @Override
64716ba89e7f459f49ea85070d4710c1d79d715298George Mount                public void onStopTrackingTouch(SeekBar seekBar) {
65716ba89e7f459f49ea85070d4710c1d79d715298George Mount                    if (stop != null) {
66716ba89e7f459f49ea85070d4710c1d79d715298George Mount                        stop.onStopTrackingTouch(seekBar);
67716ba89e7f459f49ea85070d4710c1d79d715298George Mount                    }
68716ba89e7f459f49ea85070d4710c1d79d715298George Mount                }
69716ba89e7f459f49ea85070d4710c1d79d715298George Mount            });
70716ba89e7f459f49ea85070d4710c1d79d715298George Mount        }
71716ba89e7f459f49ea85070d4710c1d79d715298George Mount    }
72716ba89e7f459f49ea85070d4710c1d79d715298George Mount
73716ba89e7f459f49ea85070d4710c1d79d715298George Mount    public interface OnStartTrackingTouch {
74716ba89e7f459f49ea85070d4710c1d79d715298George Mount        void onStartTrackingTouch(SeekBar seekBar);
75716ba89e7f459f49ea85070d4710c1d79d715298George Mount    }
76716ba89e7f459f49ea85070d4710c1d79d715298George Mount
77716ba89e7f459f49ea85070d4710c1d79d715298George Mount    public interface OnStopTrackingTouch {
78716ba89e7f459f49ea85070d4710c1d79d715298George Mount        void onStopTrackingTouch(SeekBar seekBar);
79716ba89e7f459f49ea85070d4710c1d79d715298George Mount    }
80716ba89e7f459f49ea85070d4710c1d79d715298George Mount
81716ba89e7f459f49ea85070d4710c1d79d715298George Mount    public interface OnProgressChanged {
82716ba89e7f459f49ea85070d4710c1d79d715298George Mount        void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser);
83716ba89e7f459f49ea85070d4710c1d79d715298George Mount    }
84716ba89e7f459f49ea85070d4710c1d79d715298George Mount}
85