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