Android Open Source - GlassCounter Book Result Info Retriever






From Project

Back to project page GlassCounter.

License

The source code is released under:

Apache License

If you think the Android project GlassCounter listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2011 ZXing authors/*w  w w . jav  a  2s .  c  o m*/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.github.barcodeeye.scan.result.supplement;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.content.Context;
import android.widget.TextView;

import com.github.barcodeeye.R;
import com.github.barcodeeye.migrated.HttpHelper;
import com.github.barcodeeye.migrated.LocaleManager;

/**
 * @author Kamil Kaczmarczyk
 * @author Sean Owen
 */
final class BookResultInfoRetriever extends SupplementalInfoRetriever {

  private final String isbn;
  private final String source;
  private final Context context;

  BookResultInfoRetriever(TextView textView, String isbn, Context context) {
    super(textView);
    this.isbn = isbn;
    this.source = context.getString(R.string.msg_google_books);
    this.context = context;
  }

  @Override
  void retrieveSupplementalInfo() throws IOException {

    CharSequence contents = HttpHelper.downloadViaHttp("https://www.googleapis.com/books/v1/volumes?q=isbn:" + isbn,
                                                       HttpHelper.ContentType.JSON);

    if (contents.length() == 0) {
      return;
    }

    String title;
    String pages;
    Collection<String> authors = null;

    try {

      JSONObject topLevel = (JSONObject) new JSONTokener(contents.toString()).nextValue();
      JSONArray items = topLevel.optJSONArray("items");
      if (items == null || items.isNull(0)) {
        return;
      }

      JSONObject volumeInfo = ((JSONObject) items.get(0)).getJSONObject("volumeInfo");
      if (volumeInfo == null) {
        return;
      }

      title = volumeInfo.optString("title");
      pages = volumeInfo.optString("pageCount");

      JSONArray authorsArray = volumeInfo.optJSONArray("authors");
      if (authorsArray != null && !authorsArray.isNull(0)) {
        authors = new ArrayList<String>(authorsArray.length());
        for (int i = 0; i < authorsArray.length(); i++) {
          authors.add(authorsArray.getString(i));
        }
      }

    } catch (JSONException e) {
      throw new IOException(e);
    }

    Collection<String> newTexts = new ArrayList<String>();
    maybeAddText(title, newTexts);
    maybeAddTextSeries(authors, newTexts);
    maybeAddText(pages == null || pages.isEmpty() ? null : pages + "pp.", newTexts);

    String baseBookUri = "http://www.google." + LocaleManager.getBookSearchCountryTLD(context)
        + "/search?tbm=bks&source=zxing&q=";

    append(isbn, source, newTexts.toArray(new String[newTexts.size()]), baseBookUri + isbn);
  }

}




Java Source Code List

com.github.barcodeeye.BaseGlassActivity.java
com.github.barcodeeye.LaunchActivity.java
com.github.barcodeeye.image.ImageManager.java
com.github.barcodeeye.migrated.AmbientLightManager.java
com.github.barcodeeye.migrated.BeepManager.java
com.github.barcodeeye.migrated.DecodeFormatManager.java
com.github.barcodeeye.migrated.DecodeHintManager.java
com.github.barcodeeye.migrated.FinishListener.java
com.github.barcodeeye.migrated.HttpHelper.java
com.github.barcodeeye.migrated.InactivityTimer.java
com.github.barcodeeye.migrated.Intents.java
com.github.barcodeeye.migrated.LocaleManager.java
com.github.barcodeeye.scan.CaptureActivityHandler.java
com.github.barcodeeye.scan.CaptureActivity.java
com.github.barcodeeye.scan.DecodeHandler.java
com.github.barcodeeye.scan.DecodeThread.java
com.github.barcodeeye.scan.FinalActivity.java
com.github.barcodeeye.scan.FourthActivity.java
com.github.barcodeeye.scan.ResultsActivity.java
com.github.barcodeeye.scan.api.CardPresenter.java
com.github.barcodeeye.scan.result.ResultProcessorFactory.java
com.github.barcodeeye.scan.result.ResultProcessor.java
com.github.barcodeeye.scan.result.internal.IsbnResultProcessor.java
com.github.barcodeeye.scan.result.internal.ProductResultProcessor.java
com.github.barcodeeye.scan.result.internal.TextResultProcessor.java
com.github.barcodeeye.scan.result.internal.UriResultProcessor.java
com.github.barcodeeye.scan.result.supplement.AmazonInfoRetriever.java
com.github.barcodeeye.scan.result.supplement.BookResultInfoRetriever.java
com.github.barcodeeye.scan.result.supplement.ProductResultInfoRetriever.java
com.github.barcodeeye.scan.result.supplement.SupplementalInfoRetriever.java
com.github.barcodeeye.scan.result.supplement.TitleRetriever.java
com.github.barcodeeye.scan.result.supplement.URIResultInfoRetriever.java
com.github.barcodeeye.scan.ui.ViewfinderView.java
com.google.zxing.client.android.camera.AutoFocusManager.java
com.google.zxing.client.android.camera.CameraConfigurationManager.java
com.google.zxing.client.android.camera.CameraManager.java
com.google.zxing.client.android.camera.PreviewCallback.java
com.google.zxing.client.android.camera.open.OpenCameraInterface.java
com.google.zxing.client.android.clipboard.ClipboardInterface.java
com.google.zxing.client.android.encode.ContactEncoder.java
com.google.zxing.client.android.encode.Formatter.java
com.google.zxing.client.android.encode.MECARDContactEncoder.java
com.google.zxing.client.android.encode.VCardContactEncoder.java
com.google.zxing.client.android.history.DBHelper.java
com.google.zxing.client.android.history.HistoryItemAdapter.java
com.google.zxing.client.android.history.HistoryItem.java
com.google.zxing.client.android.share.AppInfo.java
com.google.zxing.client.android.share.AppPickerActivity.java
com.google.zxing.client.android.share.BookmarkAdapter.java
com.google.zxing.client.android.share.BookmarkPickerActivity.java
com.google.zxing.client.android.share.LoadPackagesAsyncTask.java
com.google.zxing.client.android.wifi.NetworkType.java
com.google.zxing.client.android.wifi.WifiConfigManager.java