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