Home | History | Annotate | Download | only in ui
      1 /*
      2  * Copyright (C) 2009 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.quicksearchbox.ui;
     18 
     19 import com.android.quicksearchbox.SuggestionCursor;
     20 import com.android.quicksearchbox.Suggestions;
     21 
     22 import android.content.Context;
     23 import android.database.DataSetObserver;
     24 import android.view.ViewGroup;
     25 
     26 /**
     27  * View shown at the bottom of the suggestions list.
     28  */
     29 public abstract class SuggestionsFooter {
     30 
     31     private final DataSetObserver mDataSetObserver = new AdapterObserver();
     32 
     33     private final Context mContext;
     34 
     35     private SuggestionsAdapter mAdapter;
     36 
     37     public SuggestionsFooter(Context context) {
     38         mContext = context;
     39     }
     40 
     41     public abstract void addToContainer(ViewGroup parent);
     42 
     43     protected abstract void onSuggestionsChanged();
     44 
     45     protected Context getContext() {
     46         return mContext;
     47     }
     48 
     49     public void setAdapter(SuggestionsAdapter adapter) {
     50         if (mAdapter == adapter) {
     51             return;
     52         }
     53         if (mAdapter != null) {
     54             mAdapter.unregisterDataSetObserver(mDataSetObserver);
     55         }
     56         mAdapter = adapter;
     57         if (mAdapter != null) {
     58             mAdapter.registerDataSetObserver(mDataSetObserver);
     59         }
     60         onSuggestionsChanged();
     61     }
     62 
     63     protected Suggestions getSuggestions() {
     64         return mAdapter == null ? null : mAdapter.getSuggestions();
     65     }
     66 
     67     protected SuggestionCursor getCurrentSuggestions() {
     68         return mAdapter == null ? null : mAdapter.getCurrentSuggestions();
     69     }
     70 
     71     private class AdapterObserver extends DataSetObserver {
     72         @Override
     73         public void onChanged() {
     74             onSuggestionsChanged();
     75         }
     76 
     77         @Override
     78         public void onInvalidated() {
     79             onSuggestionsChanged();
     80         }
     81     }
     82 
     83 }
     84