]> rtime.felk.cvut.cz Git - hornmich/skoda-qr-demo.git/blob - BarCodeScanner/mobile/src/main/java/cz/cvut/fel/dce/barcodescanner/result/supplement/BookResultInfoRetriever.java
Initial commit
[hornmich/skoda-qr-demo.git] / BarCodeScanner / mobile / src / main / java / cz / cvut / fel / dce / barcodescanner / result / supplement / BookResultInfoRetriever.java
1 /*
2  * Copyright 2011 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 cz.cvut.fel.dce.barcodescanner.result.supplement;
18
19 import android.content.Context;
20 import android.widget.TextView;
21
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;
26
27 import org.json.JSONArray;
28 import org.json.JSONException;
29 import org.json.JSONObject;
30 import org.json.JSONTokener;
31
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.Collection;
35
36 /**
37  * @author Kamil Kaczmarczyk
38  * @author Sean Owen
39  */
40 final class BookResultInfoRetriever extends SupplementalInfoRetriever {
41
42   private final String isbn;
43   private final String source;
44   private final Context context;
45   
46   BookResultInfoRetriever(TextView textView, String isbn, HistoryManager historyManager, Context context) {
47     super(textView, historyManager);
48     this.isbn = isbn;
49     this.source = context.getString(R.string.msg_google_books);
50     this.context = context;
51   }
52
53   @Override
54   void retrieveSupplementalInfo() throws IOException {
55
56     CharSequence contents = HttpHelper.downloadViaHttp("https://www.googleapis.com/books/v1/volumes?q=isbn:" + isbn,
57                                                        HttpHelper.ContentType.JSON);
58
59     if (contents.length() == 0) {
60       return;
61     }
62
63     String title;
64     String pages;
65     Collection<String> authors = null;
66
67     try {
68
69       JSONObject topLevel = (JSONObject) new JSONTokener(contents.toString()).nextValue();
70       JSONArray items = topLevel.optJSONArray("items");
71       if (items == null || items.isNull(0)) {
72         return;
73       }
74
75       JSONObject volumeInfo = ((JSONObject) items.get(0)).getJSONObject("volumeInfo");
76       if (volumeInfo == null) {
77         return;
78       }
79
80       title = volumeInfo.optString("title");
81       pages = volumeInfo.optString("pageCount");
82
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));
88         }
89       }
90
91     } catch (JSONException e) {
92       throw new IOException(e);
93     }
94
95     Collection<String> newTexts = new ArrayList<>();
96     maybeAddText(title, newTexts);
97     maybeAddTextSeries(authors, newTexts);
98     maybeAddText(pages == null || pages.isEmpty() ? null : pages + "pp.", newTexts);
99     
100     String baseBookUri = "http://www.google." + LocaleManager.getBookSearchCountryTLD(context)
101         + "/search?tbm=bks&source=zxing&q=";
102
103     append(isbn, source, newTexts.toArray(new String[newTexts.size()]), baseBookUri + isbn);
104   }
105
106 }