12fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood/*
22fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood * Copyright (C) 2010 The Android Open Source Project
32fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood *
42fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood * Licensed under the Apache License, Version 2.0 (the "License");
52fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood * you may not use this file except in compliance with the License.
62fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood * You may obtain a copy of the License at
72fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood *
82fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood *      http://www.apache.org/licenses/LICENSE-2.0
92fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood *
102fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood * Unless required by applicable law or agreed to in writing, software
112fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood * distributed under the License is distributed on an "AS IS" BASIS,
122fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood * See the License for the specific language governing permissions and
142fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood * limitations under the License.
152fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood */
162fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood
172fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwoodpackage com.android.quicksearchbox;
182fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood
192fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwoodimport android.text.Spannable;
202fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood
212fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood/**
222fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood * Suggestion formatter interface. This is used to bold (or otherwise highlight) portions of a
232fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood * suggestion which were not a part of the query.
242fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood */
252fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwoodpublic abstract class SuggestionFormatter {
262fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood
27132a8afcb4970d1b783a6dba7944dc0dd5063101Mathew Inwood    private final TextAppearanceFactory mSpanFactory;
282fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood
29132a8afcb4970d1b783a6dba7944dc0dd5063101Mathew Inwood    protected SuggestionFormatter(TextAppearanceFactory spanFactory) {
30132a8afcb4970d1b783a6dba7944dc0dd5063101Mathew Inwood        mSpanFactory = spanFactory;
312fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood    }
322fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood
332fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood    /**
342fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood     * Formats a suggestion for display in the UI.
352fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood     *
362fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood     * @param query the query as entered by the user
372fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood     * @param suggestion the suggestion
382fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood     * @return Formatted suggestion text.
392fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood     */
40f16bea9765ee9838d316655d38cb51b72a3b4acbBjorn Bringert    public abstract CharSequence formatSuggestion(String query, String suggestion);
412fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood
422fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood    protected void applyQueryTextStyle(Spannable text, int start, int end) {
432fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood        if (start == end) return;
44132a8afcb4970d1b783a6dba7944dc0dd5063101Mathew Inwood        setSpans(text, start, end, mSpanFactory.createSuggestionQueryTextAppearance());
452fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood    }
462fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood
472fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood    protected void applySuggestedTextStyle(Spannable text, int start, int end) {
482fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood        if (start == end) return;
49132a8afcb4970d1b783a6dba7944dc0dd5063101Mathew Inwood        setSpans(text, start, end, mSpanFactory.createSuggestionSuggestedTextAppearance());
50132a8afcb4970d1b783a6dba7944dc0dd5063101Mathew Inwood    }
51132a8afcb4970d1b783a6dba7944dc0dd5063101Mathew Inwood
52132a8afcb4970d1b783a6dba7944dc0dd5063101Mathew Inwood    private void setSpans(Spannable text, int start, int end, Object[] spans) {
53132a8afcb4970d1b783a6dba7944dc0dd5063101Mathew Inwood        for (Object span : spans) {
54132a8afcb4970d1b783a6dba7944dc0dd5063101Mathew Inwood            text.setSpan(span, start, end, 0);
55132a8afcb4970d1b783a6dba7944dc0dd5063101Mathew Inwood        }
562fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood    }
572fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood
582fb3a129925a42e72944b836e85a1a2d55a0d950Mathew Inwood}
59