Home | History | Annotate | Download | only in quicksearchbox
      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;
     18 
     19 import android.util.Log;
     20 
     21 import java.util.ArrayList;
     22 
     23 /**
     24  * A promoter that first promotes any shortcuts, and then delegates to another
     25  * promoter.
     26  *
     27  */
     28 public class ShortcutPromoter implements Promoter {
     29 
     30     private static final String TAG = "QSB.ShortcutPromoter";
     31     private static final boolean DBG = false;
     32 
     33     /** The promoter to use when there are no more shortcuts. */
     34     private final Promoter mNextPromoter;
     35 
     36     /**
     37      * Creates a new ShortcutPromoter.
     38      *
     39      * @param nextPromoter The promoter to use when there are no more shortcuts.
     40      *        May be {@code null}.
     41      */
     42     public ShortcutPromoter(Promoter nextPromoter) {
     43         mNextPromoter = nextPromoter;
     44     }
     45 
     46     public void pickPromoted(SuggestionCursor shortcuts,
     47             ArrayList<CorpusResult> suggestions, int maxPromoted,
     48             ListSuggestionCursor promoted) {
     49         int shortcutCount = shortcuts == null ? 0 : shortcuts.getCount();
     50         int promotedShortcutCount = Math.min(shortcutCount, maxPromoted);
     51         if (DBG) {
     52             Log.d(TAG, "pickPromoted(shortcutCount = " + shortcutCount +
     53                     ", maxPromoted = " + maxPromoted + ")");
     54         }
     55 
     56         for (int i = 0; i < promotedShortcutCount; i++) {
     57             promoted.add(new SuggestionPosition(shortcuts, i));
     58         }
     59 
     60         if (promoted.getCount() < maxPromoted && mNextPromoter != null) {
     61             mNextPromoter.pickPromoted(null, suggestions, maxPromoted, promoted);
     62         }
     63     }
     64 
     65 }
     66