Home | History | Annotate | Download | only in adapters
      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 package android.databinding.adapters;
     17 
     18 import android.annotation.TargetApi;
     19 import android.databinding.BindingAdapter;
     20 import android.databinding.BindingMethod;
     21 import android.databinding.BindingMethods;
     22 import android.os.Build;
     23 import android.os.Build.VERSION;
     24 import android.os.Build.VERSION_CODES;
     25 import android.widget.RatingBar;
     26 import android.widget.SearchView;
     27 import android.widget.SearchView.OnCloseListener;
     28 import android.widget.SearchView.OnQueryTextListener;
     29 import android.widget.SearchView.OnSuggestionListener;
     30 
     31 @BindingMethods({
     32         @BindingMethod(type = SearchView.class, attribute = "android:onQueryTextFocusChange", method = "setOnQueryTextFocusChangeListener"),
     33         @BindingMethod(type = SearchView.class, attribute = "android:onSearchClick", method = "setOnSearchClickListener"),
     34         @BindingMethod(type = SearchView.class, attribute = "android:onClose", method = "setOnCloseListener"),
     35 })
     36 public class SearchViewBindingAdapter {
     37     @BindingAdapter("android:onQueryTextChange")
     38     public static void setListener(SearchView view, OnQueryTextChange listener) {
     39         setListener(view, null, listener);
     40     }
     41 
     42     @BindingAdapter("android:onQueryTextSubmit")
     43     public static void setListener(SearchView view, OnQueryTextSubmit listener) {
     44         setListener(view, listener, null);
     45     }
     46 
     47     @BindingAdapter({"android:onQueryTextSubmit", "android:onQueryTextChange"})
     48     public static void setListener(SearchView view, final OnQueryTextSubmit submit,
     49             final OnQueryTextChange change) {
     50         if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) {
     51             if (submit == null && change == null){
     52                 view.setOnQueryTextListener(null);
     53             } else {
     54                 view.setOnQueryTextListener(new OnQueryTextListener() {
     55                     @Override
     56                     public boolean onQueryTextSubmit(String query) {
     57                         if (submit != null) {
     58                             return submit.onQueryTextSubmit(query);
     59                         } else {
     60                             return false;
     61                         }
     62                     }
     63 
     64                     @Override
     65                     public boolean onQueryTextChange(String newText) {
     66                         if (change != null) {
     67                             return change.onQueryTextChange(newText);
     68                         } else {
     69                             return false;
     70                         }
     71                     }
     72                 });
     73             }
     74         }
     75     }
     76 
     77     @BindingAdapter("android:onSuggestionClick")
     78     public static void setListener(SearchView view, OnSuggestionClick listener) {
     79         setListener(view, null, listener);
     80     }
     81 
     82     @BindingAdapter("android:onSuggestionSelect")
     83     public static void setListener(SearchView view, OnSuggestionSelect listener) {
     84         setListener(view, listener, null);
     85     }
     86 
     87     @BindingAdapter({"android:onSuggestionSelect", "android:onSuggestionClick"})
     88     public static void setListener(SearchView view, final OnSuggestionSelect submit,
     89             final OnSuggestionClick change) {
     90         if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) {
     91             if (submit == null && change == null) {
     92                 view.setOnSuggestionListener(null);
     93             } else {
     94                 view.setOnSuggestionListener(new OnSuggestionListener() {
     95                     @Override
     96                     public boolean onSuggestionSelect(int position) {
     97                         if (submit != null) {
     98                             return submit.onSuggestionSelect(position);
     99                         } else {
    100                             return false;
    101                         }
    102                     }
    103 
    104                     @Override
    105                     public boolean onSuggestionClick(int position) {
    106                         if (change != null) {
    107                             return change.onSuggestionClick(position);
    108                         } else {
    109                             return false;
    110                         }
    111                     }
    112                 });
    113             }
    114         }
    115     }
    116 
    117     @TargetApi(VERSION_CODES.HONEYCOMB)
    118     public interface OnQueryTextSubmit {
    119         boolean onQueryTextSubmit(String query);
    120     }
    121 
    122     @TargetApi(VERSION_CODES.HONEYCOMB)
    123     public interface OnQueryTextChange {
    124         boolean onQueryTextChange(String newText);
    125     }
    126 
    127     @TargetApi(VERSION_CODES.HONEYCOMB)
    128     public interface OnSuggestionSelect {
    129         boolean onSuggestionSelect(int position);
    130     }
    131 
    132     @TargetApi(VERSION_CODES.HONEYCOMB)
    133     public interface OnSuggestionClick {
    134         boolean onSuggestionClick(int position);
    135     }
    136 }
    137