Try adding current javadoc to SVN
[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 public final class SearchBookContentsListItem extends LinearLayout {
29
30   private TextView mPageNumberView;
31   private TextView mSnippetView;
32
33   SearchBookContentsListItem(Context context) {
34     super(context);
35   }
36
37   public SearchBookContentsListItem(Context context, AttributeSet attrs) {
38     super(context, attrs);
39   }
40
41   @Override
42   protected void onFinishInflate() {
43     super.onFinishInflate();
44     mPageNumberView = (TextView) findViewById(R.id.page_number_view);
45     mSnippetView = (TextView) findViewById(R.id.snippet_view);
46   }
47
48   public void set(SearchBookContentsResult result) {
49     mPageNumberView.setText(result.getPageNumber());
50     String snippet = result.getSnippet();
51     if (snippet.length() > 0) {
52       if (result.getValidSnippet()) {
53         String lowerQuery = SearchBookContentsResult.getQuery().toLowerCase();
54         String lowerSnippet = snippet.toLowerCase();
55         Spannable styledSnippet = new SpannableString(snippet);
56         StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
57         int queryLength = lowerQuery.length();
58         int offset = 0;
59         while (true) {
60           int pos = lowerSnippet.indexOf(lowerQuery, offset);
61           if (pos < 0) {
62             break;
63           }
64           styledSnippet.setSpan(boldSpan, pos, pos + queryLength, 0);
65           offset = pos + queryLength;
66         }
67         mSnippetView.setText(styledSnippet);
68       } else {
69         // This may be an error message, so don't try to bold the query terms within it
70         mSnippetView.setText(snippet);
71       }
72     } else {
73       mSnippetView.setText("");
74     }
75   }
76
77 }