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