2 * Copyright 2011 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package cz.cvut.fel.dce.barcodescanner.result.supplement;
19 import android.content.Context;
20 import android.widget.TextView;
22 import cz.cvut.fel.dce.barcodescanner.HttpHelper;
23 import cz.cvut.fel.dce.barcodescanner.LocaleManager;
24 import cz.cvut.fel.dce.barcodescanner.R;
25 import cz.cvut.fel.dce.barcodescanner.history.HistoryManager;
27 import org.json.JSONArray;
28 import org.json.JSONException;
29 import org.json.JSONObject;
30 import org.json.JSONTokener;
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.Collection;
37 * @author Kamil Kaczmarczyk
40 final class BookResultInfoRetriever extends SupplementalInfoRetriever {
42 private final String isbn;
43 private final String source;
44 private final Context context;
46 BookResultInfoRetriever(TextView textView, String isbn, HistoryManager historyManager, Context context) {
47 super(textView, historyManager);
49 this.source = context.getString(R.string.msg_google_books);
50 this.context = context;
54 void retrieveSupplementalInfo() throws IOException {
56 CharSequence contents = HttpHelper.downloadViaHttp("https://www.googleapis.com/books/v1/volumes?q=isbn:" + isbn,
57 HttpHelper.ContentType.JSON);
59 if (contents.length() == 0) {
65 Collection<String> authors = null;
69 JSONObject topLevel = (JSONObject) new JSONTokener(contents.toString()).nextValue();
70 JSONArray items = topLevel.optJSONArray("items");
71 if (items == null || items.isNull(0)) {
75 JSONObject volumeInfo = ((JSONObject) items.get(0)).getJSONObject("volumeInfo");
76 if (volumeInfo == null) {
80 title = volumeInfo.optString("title");
81 pages = volumeInfo.optString("pageCount");
83 JSONArray authorsArray = volumeInfo.optJSONArray("authors");
84 if (authorsArray != null && !authorsArray.isNull(0)) {
85 authors = new ArrayList<>(authorsArray.length());
86 for (int i = 0; i < authorsArray.length(); i++) {
87 authors.add(authorsArray.getString(i));
91 } catch (JSONException e) {
92 throw new IOException(e);
95 Collection<String> newTexts = new ArrayList<>();
96 maybeAddText(title, newTexts);
97 maybeAddTextSeries(authors, newTexts);
98 maybeAddText(pages == null || pages.isEmpty() ? null : pages + "pp.", newTexts);
100 String baseBookUri = "http://www.google." + LocaleManager.getBookSearchCountryTLD(context)
101 + "/search?tbm=bks&source=zxing&q=";
103 append(isbn, source, newTexts.toArray(new String[newTexts.size()]), baseBookUri + isbn);