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.text.Spannable;
20 
21 /**
22  * Suggestion formatter interface. This is used to bold (or otherwise highlight) portions of a
23  * suggestion which were not a part of the query.
24  */
25 public abstract class SuggestionFormatter {
26 
27     private final TextAppearanceFactory mSpanFactory;
28 
SuggestionFormatter(TextAppearanceFactory spanFactory)29     protected SuggestionFormatter(TextAppearanceFactory spanFactory) {
30         mSpanFactory = spanFactory;
31     }
32 
33     /**
34      * Formats a suggestion for display in the UI.
35      *
36      * @param query the query as entered by the user
37      * @param suggestion the suggestion
38      * @return Formatted suggestion text.
39      */
formatSuggestion(String query, String suggestion)40     public abstract CharSequence formatSuggestion(String query, String suggestion);
41 
applyQueryTextStyle(Spannable text, int start, int end)42     protected void applyQueryTextStyle(Spannable text, int start, int end) {
43         if (start == end) return;
44         setSpans(text, start, end, mSpanFactory.createSuggestionQueryTextAppearance());
45     }
46 
applySuggestedTextStyle(Spannable text, int start, int end)47     protected void applySuggestedTextStyle(Spannable text, int start, int end) {
48         if (start == end) return;
49         setSpans(text, start, end, mSpanFactory.createSuggestionSuggestedTextAppearance());
50     }
51 
setSpans(Spannable text, int start, int end, Object[] spans)52     private void setSpans(Spannable text, int start, int end, Object[] spans) {
53         for (Object span : spans) {
54             text.setSpan(span, start, end, 0);
55         }
56     }
57 
58 }
59