Home | History | Annotate | Download | only in quicksearchbox
      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.quicksearchbox;
     18 
     19 import android.util.Log;
     20 
     21 
     22 public class WebPromoter implements Promoter {
     23 
     24     private static final String TAG = "QSB.WebPromoter";
     25     private static final boolean DBG = false;
     26 
     27     private final int mMaxShortcuts;
     28 
     29     public WebPromoter(int maxShortcuts) {
     30         mMaxShortcuts = maxShortcuts;
     31     }
     32 
     33     public void pickPromoted(Suggestions suggestions, int maxPromoted,
     34             ListSuggestionCursor promoted) {
     35         // Add web shortcuts
     36         SuggestionCursor shortcuts = suggestions.getShortcuts();
     37         int shortcutCount = shortcuts == null ? 0 : shortcuts.getCount();
     38         if (DBG) Log.d(TAG, "Shortcut count: " + shortcutCount);
     39         int maxShortcutCount = Math.min(mMaxShortcuts, maxPromoted);
     40         for (int i = 0; i < shortcutCount && promoted.getCount() < maxShortcutCount; i++) {
     41             shortcuts.moveTo(i);
     42             if (shortcuts.isWebSearchSuggestion()) {
     43                 if (DBG) Log.d(TAG, "Including shortcut " + i);
     44                 promoted.add(new SuggestionPosition(shortcuts, i));
     45             } else {
     46                 if (DBG) Log.d(TAG, "Skipping shortcut " + i);
     47             }
     48         }
     49 
     50         // Add web suggestion
     51         CorpusResult webResult = suggestions.getWebResult();
     52         int webCount = webResult == null ? 0 : webResult.getCount();
     53         if (DBG) Log.d(TAG, "Web suggestion count: " + webCount);
     54         for (int i = 0; i < webCount && promoted.getCount() < maxPromoted; i++) {
     55             webResult.moveTo(i);
     56             if (webResult.isWebSearchSuggestion()) {
     57                 if (DBG) Log.d(TAG, "Including suggestion " + i);
     58                 promoted.add(new SuggestionPosition(webResult, i));
     59             } else {
     60                 if (DBG) Log.d(TAG, "Skipping suggestion " + i);
     61             }
     62         }
     63     }
     64 
     65 }