Ejemplo n.º 1
0
 public void set(SearchBookContentsResult result) {
   pageNumberView.setText(result.getPageNumber());
   String snippet = result.getSnippet();
   if (snippet.length() > 0) {
     if (result.getValidSnippet()) {
       String lowerQuery = SearchBookContentsResult.getQuery().toLowerCase(Locale.getDefault());
       String lowerSnippet = snippet.toLowerCase(Locale.getDefault());
       Spannable styledSnippet = new SpannableString(snippet);
       StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
       int queryLength = lowerQuery.length();
       int offset = 0;
       while (true) {
         int pos = lowerSnippet.indexOf(lowerQuery, offset);
         if (pos < 0) {
           break;
         }
         styledSnippet.setSpan(boldSpan, pos, pos + queryLength, 0);
         offset = pos + queryLength;
       }
       snippetView.setText(styledSnippet);
     } else {
       // This may be an error message, so don't try to bold the query terms within it
       snippetView.setText(snippet);
     }
   } else {
     snippetView.setText("");
   }
 }
 // Currently there is no way to distinguish between a query which had no results and a book
 // which is not searchable - both return zero results.
 private void handleSearchResults(JSONObject json) {
   try {
     int count = json.getInt("number_of_results");
     headerView.setText(getString(R.string.msg_sbc_results) + " : " + count);
     if (count > 0) {
       JSONArray results = json.getJSONArray("search_results");
       SearchBookContentsResult.setQuery(queryTextView.getText().toString());
       List<SearchBookContentsResult> items = new ArrayList<SearchBookContentsResult>(count);
       for (int x = 0; x < count; x++) {
         items.add(parseResult(results.getJSONObject(x)));
       }
       resultListView.setOnItemClickListener(
           new BrowseBookListener(SearchBookContentsActivity.this, items));
       resultListView.setAdapter(
           new SearchBookContentsAdapter(SearchBookContentsActivity.this, items));
     } else {
       String searchable = json.optString("searchable");
       if ("false".equals(searchable)) {
         headerView.setText(R.string.msg_sbc_book_not_searchable);
       }
       resultListView.setAdapter(null);
     }
   } catch (JSONException e) {
     Log.w(TAG, "Bad JSON from book search", e);
     resultListView.setAdapter(null);
     headerView.setText(R.string.msg_sbc_failed);
   }
 }
 public void onItemClick(AdapterView adapterview, View view, int i, long l) {
   String s = ((SearchBookContentsResult) items.get(i - 1)).getPageId();
   String s1 = SearchBookContentsResult.getQuery();
   if (activity.getISBN().startsWith("http://google.com/books?id=") && s.length() > 0) {
     String s2 = activity.getISBN();
     String s3 = s2.substring(1 + s2.indexOf('='));
     Intent intent =
         new Intent(
             "android.intent.action.VIEW",
             Uri.parse(
                 (new StringBuilder("http://books.google."))
                     .append(LocaleManager.getBookSearchCountryTLD())
                     .append("/books?id=")
                     .append(s3)
                     .append("&pg=")
                     .append(s)
                     .append("&vq=")
                     .append(s1)
                     .toString()));
     intent.addFlags(0x80000);
     activity.startActivity(intent);
   }
 }
 public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
   // HACK(jbreiden) I have no idea where the heck our pageId off by one
   // error is coming from. I should not have to put in this position - 1
   // kludge.
   String pageId = items.get(position - 1).getPageId();
   String query = SearchBookContentsResult.getQuery();
   if (activity.getISBN().startsWith("http://google.com/books?id=") && (pageId.length() > 0)) {
     String uri = activity.getISBN();
     int equals = uri.indexOf('=');
     String volumeId = uri.substring(equals + 1);
     String readBookURI =
         "http://books.google."
             + LocaleManager.getBookSearchCountryTLD()
             + "/books?id="
             + volumeId
             + "&pg="
             + pageId
             + "&vq="
             + query;
     Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(readBookURI));
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
     activity.startActivity(intent);
   }
 }