mirror of
https://github.com/zxing/zxing.git
synced 2024-11-09 20:44:03 -08:00
Issue 1425 add more placeholders to fill in in the URL specified in the scan-from-webpage mechanism
git-svn-id: https://zxing.googlecode.com/svn/trunk@2507 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
90a47a4029
commit
eb65f3b1ea
|
@ -63,8 +63,6 @@ import android.widget.TextView;
|
|||
import android.widget.Toast;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.text.DateFormat;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
|
@ -91,9 +89,6 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
|
|||
private static final String PRODUCT_SEARCH_URL_PREFIX = "http://www.google";
|
||||
private static final String PRODUCT_SEARCH_URL_SUFFIX = "/m/products/scan";
|
||||
private static final String[] ZXING_URLS = { "http://zxing.appspot.com/scan", "zxing://scan/" };
|
||||
private static final String RETURN_CODE_PLACEHOLDER = "{CODE}";
|
||||
private static final String RETURN_URL_PARAM = "ret";
|
||||
private static final String RAW_PARAM = "raw";
|
||||
|
||||
public static final int HISTORY_REQUEST_CODE = 0x0000bacc;
|
||||
|
||||
|
@ -114,8 +109,7 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
|
|||
private boolean copyToClipboard;
|
||||
private IntentSource source;
|
||||
private String sourceUrl;
|
||||
private String returnUrlTemplate;
|
||||
private boolean returnRaw;
|
||||
private ScanFromWebPageManager scanFromWebPageManager;
|
||||
private Collection<BarcodeFormat> decodeFormats;
|
||||
private String characterSet;
|
||||
private HistoryManager historyManager;
|
||||
|
@ -242,9 +236,8 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
|
|||
// If a return URL is specified, send the results there. Otherwise, handle it ourselves.
|
||||
source = IntentSource.ZXING_LINK;
|
||||
sourceUrl = dataString;
|
||||
Uri inputUri = Uri.parse(sourceUrl);
|
||||
returnUrlTemplate = inputUri.getQueryParameter(RETURN_URL_PARAM);
|
||||
returnRaw = inputUri.getQueryParameter(RAW_PARAM) != null;
|
||||
Uri inputUri = Uri.parse(dataString);
|
||||
scanFromWebPageManager = new ScanFromWebPageManager(inputUri);
|
||||
decodeFormats = DecodeFormatManager.parseDecodeFormats(inputUri);
|
||||
|
||||
}
|
||||
|
@ -427,7 +420,7 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
|
|||
handleDecodeExternally(rawResult, resultHandler, barcode);
|
||||
break;
|
||||
case ZXING_LINK:
|
||||
if (returnUrlTemplate == null){
|
||||
if (scanFromWebPageManager == null || !scanFromWebPageManager.isScanFromWebPage()) {
|
||||
handleDecodeInternally(rawResult, resultHandler, barcode);
|
||||
} else {
|
||||
handleDecodeExternally(rawResult, resultHandler, barcode);
|
||||
|
@ -644,17 +637,9 @@ public final class CaptureActivity extends Activity implements SurfaceHolder.Cal
|
|||
sendReplyMessage(R.id.launch_product_query, replyURL, resultDurationMS);
|
||||
|
||||
} else if (source == IntentSource.ZXING_LINK) {
|
||||
|
||||
// Replace each occurrence of RETURN_CODE_PLACEHOLDER in the returnUrlTemplate
|
||||
// with the scanned code. This allows both queries and REST-style URLs to work.
|
||||
if (returnUrlTemplate != null) {
|
||||
CharSequence codeReplacement = returnRaw ? rawResult.getText() : resultHandler.getDisplayContents();
|
||||
try {
|
||||
codeReplacement = URLEncoder.encode(codeReplacement.toString(), "UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
// can't happen; UTF-8 is always supported. Continue, I guess, without encoding
|
||||
}
|
||||
String replyURL = returnUrlTemplate.replace(RETURN_CODE_PLACEHOLDER, codeReplacement);
|
||||
|
||||
if (scanFromWebPageManager != null && scanFromWebPageManager.isScanFromWebPage()) {
|
||||
String replyURL = scanFromWebPageManager.buildReplyURL(rawResult, resultHandler);
|
||||
sendReplyMessage(R.id.launch_product_query, replyURL, resultDurationMS);
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,76 @@
|
|||
/*
|
||||
* Copyright (C) 2012 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;
|
||||
|
||||
import android.net.Uri;
|
||||
import com.google.zxing.Result;
|
||||
import com.google.zxing.client.android.result.ResultHandler;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
|
||||
/**
|
||||
* Manages functionality related to responding to requests to scan from an HTTP link in a web page.
|
||||
* See <a href="http://code.google.com/p/zxing/wiki/ScanningFromWebPages">ScanningFromWebPages</a>.
|
||||
*
|
||||
* @author Sean Owen
|
||||
*/
|
||||
final class ScanFromWebPageManager {
|
||||
|
||||
private static final String CODE_PLACEHOLDER = "{CODE}";
|
||||
private static final String RAW_CODE_PLACEHOLDER = "{RAWCODE}";
|
||||
private static final String META_PLACEHOLDER = "{META}";
|
||||
private static final String FORMAT_PLACEHOLDER = "{FORMAT}";
|
||||
private static final String TYPE_PLACEHOLDER = "{TYPE}";
|
||||
|
||||
private static final String RETURN_URL_PARAM = "ret";
|
||||
private static final String RAW_PARAM = "raw";
|
||||
|
||||
private final String returnUrlTemplate;
|
||||
private final boolean returnRaw;
|
||||
|
||||
ScanFromWebPageManager(Uri inputUri) {
|
||||
returnUrlTemplate = inputUri.getQueryParameter(RETURN_URL_PARAM);
|
||||
returnRaw = inputUri.getQueryParameter(RAW_PARAM) != null;
|
||||
}
|
||||
|
||||
boolean isScanFromWebPage() {
|
||||
return returnUrlTemplate != null;
|
||||
}
|
||||
|
||||
String buildReplyURL(Result rawResult, ResultHandler resultHandler) {
|
||||
String result = returnUrlTemplate;
|
||||
result = replace(CODE_PLACEHOLDER,
|
||||
returnRaw ? rawResult.getText() : resultHandler.getDisplayContents(), result);
|
||||
result = replace(RAW_CODE_PLACEHOLDER, rawResult.getText(), result);
|
||||
result = replace(FORMAT_PLACEHOLDER, rawResult.getBarcodeFormat().toString(), result);
|
||||
result = replace(TYPE_PLACEHOLDER, resultHandler.getType().toString(), result);
|
||||
result = replace(META_PLACEHOLDER, String.valueOf(rawResult.getResultMetadata()), result);
|
||||
return result;
|
||||
}
|
||||
|
||||
private static String replace(CharSequence placeholder, CharSequence with, String pattern) {
|
||||
CharSequence escapedWith = with == null ? "" : with;
|
||||
try {
|
||||
escapedWith = URLEncoder.encode(escapedWith.toString(), "UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
// can't happen; UTF-8 is always supported. Continue, I guess, without encoding
|
||||
}
|
||||
return pattern.replace(placeholder, escapedWith);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue