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.VERSION;
     23 import android.os.Build.VERSION_CODES;
     24 import android.widget.SearchView;
     25 import android.widget.SearchView.OnQueryTextListener;
     26 import android.widget.SearchView.OnSuggestionListener;
     27 
     28 @BindingMethods({
     29         @BindingMethod(type = SearchView.class, attribute = "android:onQueryTextFocusChange", method = "setOnQueryTextFocusChangeListener"),
     30         @BindingMethod(type = SearchView.class, attribute = "android:onSearchClick", method = "setOnSearchClickListener"),
     31         @BindingMethod(type = SearchView.class, attribute = "android:onClose", method = "setOnCloseListener"),
     32 })
     33 public class SearchViewBindingAdapter {
     34     @BindingAdapter(value = {"android:onQueryTextSubmit", "android:onQueryTextChange"},
     35             requireAll = false)
     36     public static void setOnQueryTextListener(SearchView view, final OnQueryTextSubmit submit,
     37             final OnQueryTextChange change) {
     38         if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) {
     39             if (submit == null && change == null){
     40                 view.setOnQueryTextListener(null);
     41             } else {
     42                 view.setOnQueryTextListener(new OnQueryTextListener() {
     43                     @Override
     44                     public boolean onQueryTextSubmit(String query) {
     45                         if (submit != null) {
     46                             return submit.onQueryTextSubmit(query);
     47                         } else {
     48                             return false;
     49                         }
     50                     }
     51 
     52                     @Override
     53                     public boolean onQueryTextChange(String newText) {
     54                         if (change != null) {
     55                             return change.onQueryTextChange(newText);
     56                         } else {
     57                             return false;
     58                         }
     59                     }
     60                 });
     61             }
     62         }
     63     }
     64 
     65     @BindingAdapter(value = {"android:onSuggestionSelect", "android:onSuggestionClick"},
     66             requireAll = false)
     67     public static void setOnSuggestListener(SearchView view, final OnSuggestionSelect submit,
     68             final OnSuggestionClick change) {
     69         if (VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB) {
     70             if (submit == null && change == null) {
     71                 view.setOnSuggestionListener(null);
     72             } else {
     73                 view.setOnSuggestionListener(new OnSuggestionListener() {
     74                     @Override
     75                     public boolean onSuggestionSelect(int position) {
     76                         if (submit != null) {
     77                             return submit.onSuggestionSelect(position);
     78                         } else {
     79                             return false;
     80                         }
     81                     }
     82 
     83                     @Override
     84                     public boolean onSuggestionClick(int position) {
     85                         if (change != null) {
     86                             return change.onSuggestionClick(position);
     87                         } else {
     88                             return false;
     89                         }
     90                     }
     91                 });
     92             }
     93         }
     94     }
     95 
     96     @TargetApi(VERSION_CODES.HONEYCOMB)
     97     public interface OnQueryTextSubmit {
     98         boolean onQueryTextSubmit(String query);
     99     }
    100 
    101     @TargetApi(VERSION_CODES.HONEYCOMB)
    102     public interface OnQueryTextChange {
    103         boolean onQueryTextChange(String newText);
    104     }
    105 
    106     @TargetApi(VERSION_CODES.HONEYCOMB)
    107     public interface OnSuggestionSelect {
    108         boolean onSuggestionSelect(int position);
    109     }
    110 
    111     @TargetApi(VERSION_CODES.HONEYCOMB)
    112     public interface OnSuggestionClick {
    113         boolean onSuggestionClick(int position);
    114     }
    115 }
    116