mirror of
https://github.com/zxing/zxing.git
synced 2025-03-05 20:48:51 -08:00
Committing Jeff's latest for ISSUE-238. May still be an issue with international properties?
git-svn-id: https://zxing.googlecode.com/svn/trunk@1091 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
7e9f5c95f9
commit
154172b6ab
|
@ -0,0 +1,54 @@
|
|||
/*
|
||||
* Copyright (C) 2009 ZXing authors
|
||||
*
|
||||
* 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.google.zxing.client.android.book;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.view.View;
|
||||
import android.widget.AdapterView;
|
||||
import com.google.zxing.client.android.LocaleManager;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
final class BrowseBookListener implements AdapterView.OnItemClickListener {
|
||||
|
||||
private final SearchBookContentsActivity activity;
|
||||
private final List<SearchBookContentsResult> items;
|
||||
|
||||
BrowseBookListener(SearchBookContentsActivity activity, List<SearchBookContentsResult> items) {
|
||||
this.activity = activity;
|
||||
this.items = items;
|
||||
}
|
||||
|
||||
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;
|
||||
activity.startActivity(new Intent(Intent.ACTION_VIEW,
|
||||
Uri.parse(readBookURI)));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -59,8 +59,14 @@ import com.google.zxing.client.android.AndroidHttpClient;
|
|||
* @author dswitkin@google.com (Daniel Switkin)
|
||||
*/
|
||||
public final class SearchBookContentsActivity extends Activity {
|
||||
|
||||
private static final String TAG = "SearchBookContents";
|
||||
private static final String USER_AGENT = "ZXing (Android)";
|
||||
private static final Pattern TAG_PATTERN = Pattern.compile("\\<.*?\\>");
|
||||
private static final Pattern LT_ENTITY_PATTERN = Pattern.compile("<");
|
||||
private static final Pattern GT_ENTITY_PATTERN = Pattern.compile(">");
|
||||
private static final Pattern QUOTE_ENTITY_PATTERN = Pattern.compile("'");
|
||||
private static final Pattern QUOT_ENTITY_PATTERN = Pattern.compile(""");
|
||||
|
||||
private NetworkThread networkThread;
|
||||
private String isbn;
|
||||
|
@ -100,11 +106,10 @@ public final class SearchBookContentsActivity extends Activity {
|
|||
return false;
|
||||
}
|
||||
};
|
||||
private static final Pattern TAG_PATTERN = Pattern.compile("\\<.*?\\>");
|
||||
private static final Pattern LT_ENTITY_PATTERN = Pattern.compile("<");
|
||||
private static final Pattern GT_ENTITY_PATTERN = Pattern.compile(">");
|
||||
private static final Pattern QUOTE_ENTITY_PATTERN = Pattern.compile("'");
|
||||
private static final Pattern QUOT_ENTITY_PATTERN = Pattern.compile(""");
|
||||
|
||||
String getISBN() {
|
||||
return isbn;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle icicle) {
|
||||
|
@ -121,7 +126,11 @@ public final class SearchBookContentsActivity extends Activity {
|
|||
}
|
||||
|
||||
isbn = intent.getStringExtra(Intents.SearchBookContents.ISBN);
|
||||
setTitle(getString(R.string.sbc_name) + ": ISBN " + isbn);
|
||||
if (isbn.startsWith("http://google.com/books?id=")) {
|
||||
setTitle(getString(R.string.sbc_name));
|
||||
} else {
|
||||
setTitle(getString(R.string.sbc_name) + ": ISBN " + isbn);
|
||||
}
|
||||
|
||||
setContentView(R.layout.search_book_contents);
|
||||
queryTextView = (EditText) findViewById(R.id.query_text_view);
|
||||
|
@ -189,6 +198,7 @@ public final class SearchBookContentsActivity extends Activity {
|
|||
for (int x = 0; x < count; x++) {
|
||||
items.add(parseResult(results.getJSONObject(x)));
|
||||
}
|
||||
resultListView.setOnItemClickListener(new BrowseBookListener(this, items));
|
||||
resultListView.setAdapter(new SearchBookContentsAdapter(this, items));
|
||||
} else {
|
||||
String searchable = json.optString("searchable");
|
||||
|
@ -204,9 +214,10 @@ public final class SearchBookContentsActivity extends Activity {
|
|||
}
|
||||
}
|
||||
|
||||
// Available fields: page_number, page_id, page_url, snippet_text
|
||||
// Available fields: page_id, page_number, page_url, snippet_text
|
||||
private SearchBookContentsResult parseResult(JSONObject json) {
|
||||
try {
|
||||
String pageId = json.getString("page_id");
|
||||
String pageNumber = json.getString("page_number");
|
||||
if (pageNumber.length() > 0) {
|
||||
pageNumber = getString(R.string.msg_sbc_page) + ' ' + pageNumber;
|
||||
|
@ -228,10 +239,10 @@ public final class SearchBookContentsActivity extends Activity {
|
|||
snippet = '(' + getString(R.string.msg_sbc_snippet_unavailable) + ')';
|
||||
valid = false;
|
||||
}
|
||||
return new SearchBookContentsResult(pageNumber, snippet, valid);
|
||||
return new SearchBookContentsResult(pageId, pageNumber, snippet, valid);
|
||||
} catch (JSONException e) {
|
||||
// Never seen in the wild, just being complete.
|
||||
return new SearchBookContentsResult(getString(R.string.msg_sbc_no_page_returned), "", false);
|
||||
return new SearchBookContentsResult(getString(R.string.msg_sbc_no_page_returned), "", "", false);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -253,8 +264,16 @@ public final class SearchBookContentsActivity extends Activity {
|
|||
// These return a JSON result which describes if and where the query was found. This API may
|
||||
// break or disappear at any time in the future. Since this is an API call rather than a
|
||||
// website, we don't use LocaleManager to change the TLD.
|
||||
URI uri = new URI("http", null, "www.google.com", -1, "/books", "vid=isbn" + isbn +
|
||||
"&jscmd=SearchWithinVolume2&q=" + query, null);
|
||||
URI uri;
|
||||
if (isbn.startsWith("http://google.com/books?id=")) {
|
||||
int equals = isbn.indexOf('=');
|
||||
String volumeId = isbn.substring(equals + 1);
|
||||
uri = new URI("http", null, "www.google.com", -1, "/books", "id=" + volumeId +
|
||||
"&jscmd=SearchWithinVolume2&q=" + query, null);
|
||||
} else {
|
||||
uri = new URI("http", null, "www.google.com", -1, "/books", "vid=isbn" + isbn +
|
||||
"&jscmd=SearchWithinVolume2&q=" + query, null);
|
||||
}
|
||||
HttpUriRequest get = new HttpGet(uri);
|
||||
get.setHeader("cookie", getCookie(uri.toString()));
|
||||
client = AndroidHttpClient.newInstance(USER_AGENT);
|
||||
|
|
|
@ -22,13 +22,17 @@ package com.google.zxing.client.android.book;
|
|||
* @author dswitkin@google.com (Daniel Switkin)
|
||||
*/
|
||||
final class SearchBookContentsResult {
|
||||
|
||||
private static String query;
|
||||
|
||||
private final String pageId;
|
||||
private final String pageNumber;
|
||||
private final String snippet;
|
||||
private final boolean validSnippet;
|
||||
|
||||
SearchBookContentsResult(String pageNumber, String snippet, boolean validSnippet) {
|
||||
SearchBookContentsResult(String pageId, String pageNumber, String snippet,
|
||||
boolean validSnippet) {
|
||||
this.pageId = pageId;
|
||||
this.pageNumber = pageNumber;
|
||||
this.snippet = snippet;
|
||||
this.validSnippet = validSnippet;
|
||||
|
@ -38,6 +42,10 @@ final class SearchBookContentsResult {
|
|||
SearchBookContentsResult.query = query;
|
||||
}
|
||||
|
||||
public String getPageId() {
|
||||
return pageId;
|
||||
}
|
||||
|
||||
public String getPageNumber() {
|
||||
return pageNumber;
|
||||
}
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
package com.google.zxing.client.android.result;
|
||||
|
||||
import com.google.zxing.client.android.R;
|
||||
import com.google.zxing.client.android.LocaleManager;
|
||||
import com.google.zxing.client.result.ParsedResult;
|
||||
import com.google.zxing.client.result.URIParsedResult;
|
||||
|
||||
|
@ -29,11 +28,12 @@ import android.app.Activity;
|
|||
* @author dswitkin@google.com (Daniel Switkin)
|
||||
*/
|
||||
public final class URIResultHandler extends ResultHandler {
|
||||
|
||||
private static final int[] buttons = {
|
||||
R.string.button_open_browser,
|
||||
R.string.button_share_by_email,
|
||||
R.string.button_share_by_sms,
|
||||
R.string.button_read_book,
|
||||
R.string.button_search_book_contents,
|
||||
};
|
||||
|
||||
public URIResultHandler(Activity activity, ParsedResult result) {
|
||||
|
@ -65,11 +65,7 @@ public final class URIResultHandler extends ResultHandler {
|
|||
shareBySMS(uri);
|
||||
break;
|
||||
case 3:
|
||||
int equals = uri.indexOf('=');
|
||||
String id = uri.substring(equals + 1);
|
||||
String readBookURI = "http://books.google." +
|
||||
LocaleManager.getBookSearchCountryTLD() + "/m#Read?id=" + id;
|
||||
openURL(readBookURI);
|
||||
searchBookContents(uri);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue