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.database.DataSetObserver;
     23 import android.util.Log;
     24 
     25 /**
     26  * A {@link SuggestionsAdapter} that doesn't expose the new suggestions
     27  * until there are some results to show.
     28  */
     29 public class DelayingSuggestionsAdapter extends SuggestionsAdapter {
     30 
     31     private static final boolean DBG = false;
     32     private static final String TAG = "QSB.DelayingSuggestionsAdapter";
     33 
     34     private DataSetObserver mPendingDataSetObserver;
     35 
     36     private Suggestions mPendingSuggestions;
     37 
     38     public DelayingSuggestionsAdapter(SuggestionViewFactory viewFactory) {
     39         super(viewFactory);
     40     }
     41 
     42     @Override
     43     public void close() {
     44         setPendingSuggestions(null);
     45         super.close();
     46     }
     47 
     48     @Override
     49     public void setSuggestions(Suggestions suggestions) {
     50         if (suggestions == null) {
     51             super.setSuggestions(null);
     52             setPendingSuggestions(null);
     53             return;
     54         }
     55         if (shouldPublish(suggestions)) {
     56             if (DBG) Log.d(TAG, "Publishing suggestions immediately: " + suggestions);
     57             super.setSuggestions(suggestions);
     58             // Clear any old pending suggestions.
     59             setPendingSuggestions(null);
     60         } else {
     61             if (DBG) Log.d(TAG, "Delaying suggestions publishing: " + suggestions);
     62             setPendingSuggestions(suggestions);
     63         }
     64     }
     65 
     66     /**
     67      * Gets whether the given suggestions are non-empty for the selected source.
     68      */
     69     private boolean shouldPublish(Suggestions suggestions) {
     70         if (suggestions.isDone()) return true;
     71         SuggestionCursor cursor = getCorpusCursor(suggestions, getCorpus());
     72         return cursor != null && cursor.getCount() > 0;
     73     }
     74 
     75     private void setPendingSuggestions(Suggestions suggestions) {
     76         if (mPendingSuggestions == suggestions) {
     77             return;
     78         }
     79         if (isClosed()) {
     80             if (suggestions != null) {
     81                 suggestions.close();
     82             }
     83             return;
     84         }
     85         if (mPendingDataSetObserver == null) {
     86             mPendingDataSetObserver = new PendingSuggestionsObserver();
     87         }
     88         if (mPendingSuggestions != null) {
     89             mPendingSuggestions.unregisterDataSetObserver(mPendingDataSetObserver);
     90             // Close old suggestions, but only if they are not also the current
     91             // suggestions.
     92             if (mPendingSuggestions != getSuggestions()) {
     93                 mPendingSuggestions.close();
     94             }
     95         }
     96         mPendingSuggestions = suggestions;
     97         if (mPendingSuggestions != null) {
     98             mPendingSuggestions.registerDataSetObserver(mPendingDataSetObserver);
     99         }
    100     }
    101 
    102     protected void onPendingSuggestionsChanged() {
    103         if (DBG) {
    104             Log.d(TAG, "onPendingSuggestionsChanged(), mPendingSuggestions="
    105                     + mPendingSuggestions);
    106         }
    107         if (shouldPublish(mPendingSuggestions)) {
    108             if (DBG) Log.d(TAG, "Suggestions now available, publishing: " + mPendingSuggestions);
    109             super.setSuggestions(mPendingSuggestions);
    110             // The suggestions are no longer pending.
    111             setPendingSuggestions(null);
    112         }
    113     }
    114 
    115     private class PendingSuggestionsObserver extends DataSetObserver {
    116         @Override
    117         public void onChanged() {
    118             onPendingSuggestionsChanged();
    119         }
    120     }
    121 
    122 }
    123