Minor changes from code inspection results
[zxing.git] / android / src / com / google / zxing / client / android / book / SearchBookContentsListItem.java
1 /*
2  * Copyright (C) 2008 ZXing authors
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.google.zxing.client.android.book;
18
19 import com.google.zxing.client.android.R;
20
21 import android.content.Context;
22 import android.graphics.Typeface;
23 import android.text.Spannable;
24 import android.text.SpannableString;
25 import android.text.style.StyleSpan;
26 import android.util.AttributeSet;
27 import android.widget.LinearLayout;
28 import android.widget.TextView;
29
30 /**
31  * A list item which displays the page number and snippet of this search result.
32  *
33  * @author dswitkin@google.com (Daniel Switkin)
34  */
35 public final class SearchBookContentsListItem extends LinearLayout {
36   private TextView pageNumberView;
37   private TextView snippetView;
38
39   SearchBookContentsListItem(Context context) {
40     super(context);
41   }
42
43   public SearchBookContentsListItem(Context context, AttributeSet attrs) {
44     super(context, attrs);
45   }
46
47   @Override
48   protected void onFinishInflate() {
49     super.onFinishInflate();
50     pageNumberView = (TextView) findViewById(R.id.page_number_view);
51     snippetView = (TextView) findViewById(R.id.snippet_view);
52   }
53
54   public void set(SearchBookContentsResult result) {
55     pageNumberView.setText(result.getPageNumber());
56     String snippet = result.getSnippet();
57     if (snippet.length() > 0) {
58       if (result.getValidSnippet()) {
59         String lowerQuery = SearchBookContentsResult.getQuery().toLowerCase();
60         String lowerSnippet = snippet.toLowerCase();
61         Spannable styledSnippet = new SpannableString(snippet);
62         StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
63         int queryLength = lowerQuery.length();
64         int offset = 0;
65         while (true) {
66           int pos = lowerSnippet.indexOf(lowerQuery, offset);
67           if (pos < 0) {
68             break;
69           }
70           styledSnippet.setSpan(boldSpan, pos, pos + queryLength, 0);
71           offset = pos + queryLength;
72         }
73         snippetView.setText(styledSnippet);
74       } else {
75         // This may be an error message, so don't try to bold the query terms within it
76         snippetView.setText(snippet);
77       }
78     } else {
79       snippetView.setText("");
80     }
81   }
82 }