Home | History | Annotate | Download | only in widget
      1 /*
      2  * Copyright (C) 2010 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 
     17 package com.android.contacts.widget;
     18 
     19 import android.content.Context;
     20 import android.graphics.drawable.Drawable;
     21 import android.text.Editable;
     22 import android.text.TextUtils;
     23 import android.text.TextWatcher;
     24 import android.util.AttributeSet;
     25 import android.view.KeyEvent;
     26 import android.view.inputmethod.EditorInfo;
     27 import android.view.inputmethod.InputMethodManager;
     28 import android.widget.EditText;
     29 import android.widget.TextView;
     30 import android.widget.TextView.OnEditorActionListener;
     31 
     32 /**
     33  * A custom text editor that helps automatically dismiss the activity along with the soft
     34  * keyboard.
     35  */
     36 public class SearchEditText extends EditText implements OnEditorActionListener, TextWatcher {
     37 
     38     private boolean mMaginfyingGlassEnabled = true;
     39     private Drawable mMagnifyingGlass;
     40     private OnFilterTextListener mListener;
     41 
     42     private boolean mMagnifyingGlassShown;
     43 
     44     public interface OnFilterTextListener {
     45         void onFilterChange(String queryString);
     46         void onCancelSearch();
     47     }
     48 
     49     public SearchEditText(Context context, AttributeSet attrs) {
     50         super(context, attrs);
     51         addTextChangedListener(this);
     52         setOnEditorActionListener(this);
     53         mMagnifyingGlass = getCompoundDrawables()[2];
     54         setCompoundDrawables(null, null, null, null);
     55     }
     56 
     57     public boolean isMaginfyingGlassEnabled() {
     58         return mMaginfyingGlassEnabled;
     59     }
     60 
     61     public void setMaginfyingGlassEnabled(boolean flag) {
     62         this.mMaginfyingGlassEnabled = flag;
     63     }
     64 
     65     public void setOnFilterTextListener(OnFilterTextListener listener) {
     66         this.mListener = listener;
     67     }
     68 
     69     /**
     70      * Conditionally shows a magnifying glass icon on the right side of the text field
     71      * when the text it empty.
     72      */
     73     @Override
     74     public boolean onPreDraw() {
     75         boolean emptyText = TextUtils.isEmpty(getText());
     76         if (mMagnifyingGlassShown != emptyText) {
     77             mMagnifyingGlassShown = emptyText;
     78             if (mMagnifyingGlassShown && mMaginfyingGlassEnabled) {
     79                 setCompoundDrawables(null, null, mMagnifyingGlass, null);
     80             } else {
     81                 setCompoundDrawables(null, null, null, null);
     82             }
     83             return false;
     84         }
     85         return super.onPreDraw();
     86     }
     87 
     88     /**
     89      * Dismisses the search UI along with the keyboard if the filter text is empty.
     90      */
     91     @Override
     92     public boolean onKeyPreIme(int keyCode, KeyEvent event) {
     93         if (keyCode == KeyEvent.KEYCODE_BACK && TextUtils.isEmpty(getText()) && mListener != null) {
     94             mListener.onCancelSearch();
     95             return true;
     96         }
     97         return false;
     98     }
     99 
    100     public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    101     }
    102 
    103     @Override
    104     public void onTextChanged(CharSequence s, int start, int before, int count) {
    105     }
    106 
    107     /**
    108      * Event handler for search UI.
    109      */
    110     public void afterTextChanged(Editable s) {
    111         if (mListener != null) {
    112             mListener.onFilterChange(trim(s));
    113         }
    114     }
    115 
    116     private String trim(Editable s) {
    117         return s.toString().trim();
    118     }
    119 
    120     /**
    121      * Event handler for search UI.
    122      */
    123     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    124         if (actionId == EditorInfo.IME_ACTION_DONE) {
    125             hideSoftKeyboard();
    126             if (TextUtils.isEmpty(trim(getText())) && mListener != null) {
    127                 mListener.onCancelSearch();
    128             }
    129             return true;
    130         }
    131         return false;
    132     }
    133 
    134     private void hideSoftKeyboard() {
    135         // Hide soft keyboard, if visible
    136         InputMethodManager inputMethodManager = (InputMethodManager)
    137                 getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    138         inputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
    139     }
    140 
    141 }
    142