2 * Copyright (C) 2008 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.client.android.book;
19 import android.content.Context;
20 import android.graphics.Typeface;
21 import android.text.SpannableString;
22 import android.text.Spannable;
23 import android.text.style.StyleSpan;
24 import android.util.AttributeSet;
25 import android.widget.LinearLayout;
26 import android.widget.TextView;
27 import com.google.zxing.client.android.R;
30 * A list item which displays the page number and snippet of this search result.
32 * @author dswitkin@google.com (Daniel Switkin)
34 final class SearchBookContentsListItem extends LinearLayout {
35 private TextView pageNumberView;
36 private TextView snippetView;
38 SearchBookContentsListItem(Context context) {
42 public SearchBookContentsListItem(Context context, AttributeSet attrs) {
43 super(context, attrs);
47 protected void onFinishInflate() {
48 super.onFinishInflate();
49 pageNumberView = (TextView) findViewById(R.id.page_number_view);
50 snippetView = (TextView) findViewById(R.id.snippet_view);
53 public void set(SearchBookContentsResult result) {
54 pageNumberView.setText(result.getPageNumber());
55 String snippet = result.getSnippet();
56 if (snippet.length() > 0) {
57 if (result.getValidSnippet()) {
58 String lowerQuery = SearchBookContentsResult.getQuery().toLowerCase();
59 String lowerSnippet = snippet.toLowerCase();
60 Spannable styledSnippet = new SpannableString(snippet);
61 StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
62 int queryLength = lowerQuery.length();
65 int pos = lowerSnippet.indexOf(lowerQuery, offset);
69 styledSnippet.setSpan(boldSpan, pos, pos + queryLength, 0);
70 offset = pos + queryLength;
72 snippetView.setText(styledSnippet);
74 // This may be an error message, so don't try to bold the query terms within it
75 snippetView.setText(snippet);
78 snippetView.setText("");