Minor changes from code inspection results
[zxing.git] / android / src / com / google / zxing / client / android / book / BrowseBookListener.java
1 /*
2  * Copyright (C) 2009 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 android.content.Intent;
20 import android.net.Uri;
21 import android.view.View;
22 import android.widget.AdapterView;
23 import com.google.zxing.client.android.LocaleManager;
24
25 import java.util.List;
26
27 final class BrowseBookListener implements AdapterView.OnItemClickListener {
28
29   private final SearchBookContentsActivity activity;
30   private final List<SearchBookContentsResult> items;
31
32   BrowseBookListener(SearchBookContentsActivity activity, List<SearchBookContentsResult> items) {
33     this.activity = activity;
34     this.items = items;
35   }
36
37   public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
38     // HACK(jbreiden) I have no idea where the heck our pageId off by one
39     // error is coming from. I should not have to put in this position - 1
40     // kludge.
41     String pageId = items.get(position - 1).getPageId();
42     String query = SearchBookContentsResult.getQuery();
43     if (activity.getISBN().startsWith("http://google.com/books?id=") && (pageId.length() > 0)) {
44       String uri = activity.getISBN();
45       int equals = uri.indexOf('=');
46       String volumeId = uri.substring(equals + 1);
47       String readBookURI = "http://books.google." +
48           LocaleManager.getBookSearchCountryTLD() +
49           "/books?id=" + volumeId + "&pg=" + pageId + "&vq=" + query;
50       Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(readBookURI));
51       intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);                    
52       activity.startActivity(intent);
53     }
54   }
55 }