18b2936607176720172aee068abc5631bdf77e843Bjorn Bringert/* 28b2936607176720172aee068abc5631bdf77e843Bjorn Bringert * Copyright (C) 2010 The Android Open Source Project 38b2936607176720172aee068abc5631bdf77e843Bjorn Bringert * 48b2936607176720172aee068abc5631bdf77e843Bjorn Bringert * Licensed under the Apache License, Version 2.0 (the "License"); 58b2936607176720172aee068abc5631bdf77e843Bjorn Bringert * you may not use this file except in compliance with the License. 68b2936607176720172aee068abc5631bdf77e843Bjorn Bringert * You may obtain a copy of the License at 78b2936607176720172aee068abc5631bdf77e843Bjorn Bringert * 88b2936607176720172aee068abc5631bdf77e843Bjorn Bringert * http://www.apache.org/licenses/LICENSE-2.0 98b2936607176720172aee068abc5631bdf77e843Bjorn Bringert * 108b2936607176720172aee068abc5631bdf77e843Bjorn Bringert * Unless required by applicable law or agreed to in writing, software 118b2936607176720172aee068abc5631bdf77e843Bjorn Bringert * distributed under the License is distributed on an "AS IS" BASIS, 128b2936607176720172aee068abc5631bdf77e843Bjorn Bringert * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 138b2936607176720172aee068abc5631bdf77e843Bjorn Bringert * See the License for the specific language governing permissions and 148b2936607176720172aee068abc5631bdf77e843Bjorn Bringert * limitations under the License. 158b2936607176720172aee068abc5631bdf77e843Bjorn Bringert */ 168b2936607176720172aee068abc5631bdf77e843Bjorn Bringertpackage com.android.quicksearchbox; 178b2936607176720172aee068abc5631bdf77e843Bjorn Bringert 188b2936607176720172aee068abc5631bdf77e843Bjorn Bringertimport java.util.HashSet; 198b2936607176720172aee068abc5631bdf77e843Bjorn Bringertimport java.util.Set; 208b2936607176720172aee068abc5631bdf77e843Bjorn Bringert 218b2936607176720172aee068abc5631bdf77e843Bjorn Bringert/** 228b2936607176720172aee068abc5631bdf77e843Bjorn Bringert * Promotes shortcuts and suggestions from a single corpus. 238b2936607176720172aee068abc5631bdf77e843Bjorn Bringert */ 248b2936607176720172aee068abc5631bdf77e843Bjorn Bringertpublic class SingleCorpusPromoter implements Promoter { 258b2936607176720172aee068abc5631bdf77e843Bjorn Bringert 268b2936607176720172aee068abc5631bdf77e843Bjorn Bringert private final Corpus mCorpus; 278b2936607176720172aee068abc5631bdf77e843Bjorn Bringert 288b2936607176720172aee068abc5631bdf77e843Bjorn Bringert private final int mMaxShortcuts; 298b2936607176720172aee068abc5631bdf77e843Bjorn Bringert 308b2936607176720172aee068abc5631bdf77e843Bjorn Bringert private final Set<String> mAllowedSources; 318b2936607176720172aee068abc5631bdf77e843Bjorn Bringert 328b2936607176720172aee068abc5631bdf77e843Bjorn Bringert public SingleCorpusPromoter(Corpus corpus, int maxShortcuts) { 338b2936607176720172aee068abc5631bdf77e843Bjorn Bringert mCorpus = corpus; 348b2936607176720172aee068abc5631bdf77e843Bjorn Bringert mMaxShortcuts = maxShortcuts; 358b2936607176720172aee068abc5631bdf77e843Bjorn Bringert mAllowedSources = new HashSet<String>(); 368b2936607176720172aee068abc5631bdf77e843Bjorn Bringert for (Source source : corpus.getSources()) { 378b2936607176720172aee068abc5631bdf77e843Bjorn Bringert mAllowedSources.add(source.getName()); 388b2936607176720172aee068abc5631bdf77e843Bjorn Bringert } 398b2936607176720172aee068abc5631bdf77e843Bjorn Bringert } 408b2936607176720172aee068abc5631bdf77e843Bjorn Bringert 418b2936607176720172aee068abc5631bdf77e843Bjorn Bringert public void pickPromoted(Suggestions suggestions, int maxPromoted, 428b2936607176720172aee068abc5631bdf77e843Bjorn Bringert ListSuggestionCursor promoted) { 438b2936607176720172aee068abc5631bdf77e843Bjorn Bringert // Add shortcuts 448b2936607176720172aee068abc5631bdf77e843Bjorn Bringert SuggestionCursor shortcuts = suggestions.getShortcuts(); 458b2936607176720172aee068abc5631bdf77e843Bjorn Bringert promoteUntilFull(shortcuts, mMaxShortcuts, promoted); 468b2936607176720172aee068abc5631bdf77e843Bjorn Bringert // Add suggestions 478b2936607176720172aee068abc5631bdf77e843Bjorn Bringert CorpusResult corpusResult = suggestions.getCorpusResult(mCorpus); 488b2936607176720172aee068abc5631bdf77e843Bjorn Bringert promoteUntilFull(corpusResult, maxPromoted, promoted); 498b2936607176720172aee068abc5631bdf77e843Bjorn Bringert } 508b2936607176720172aee068abc5631bdf77e843Bjorn Bringert 518b2936607176720172aee068abc5631bdf77e843Bjorn Bringert private void promoteUntilFull(SuggestionCursor c, int maxSize, ListSuggestionCursor promoted) { 528b2936607176720172aee068abc5631bdf77e843Bjorn Bringert if (c == null) return; 538b2936607176720172aee068abc5631bdf77e843Bjorn Bringert int count = c.getCount(); 548b2936607176720172aee068abc5631bdf77e843Bjorn Bringert for (int i = 0; i < count && promoted.getCount() < maxSize; i++) { 558b2936607176720172aee068abc5631bdf77e843Bjorn Bringert c.moveTo(i); 568b2936607176720172aee068abc5631bdf77e843Bjorn Bringert if (accept(c)) { 578b2936607176720172aee068abc5631bdf77e843Bjorn Bringert promoted.add(new SuggestionPosition(c, i)); 588b2936607176720172aee068abc5631bdf77e843Bjorn Bringert } 598b2936607176720172aee068abc5631bdf77e843Bjorn Bringert } 608b2936607176720172aee068abc5631bdf77e843Bjorn Bringert } 618b2936607176720172aee068abc5631bdf77e843Bjorn Bringert 628b2936607176720172aee068abc5631bdf77e843Bjorn Bringert protected boolean accept(Suggestion s) { 638b2936607176720172aee068abc5631bdf77e843Bjorn Bringert return mAllowedSources.contains(s.getSuggestionSource().getName()); 648b2936607176720172aee068abc5631bdf77e843Bjorn Bringert } 658b2936607176720172aee068abc5631bdf77e843Bjorn Bringert 668b2936607176720172aee068abc5631bdf77e843Bjorn Bringert} 67