mirror of
https://github.com/zxing/zxing.git
synced 2025-03-05 20:48:51 -08:00
Some refactoring to remove small disconnect between how Results are handled and whether the client thinks it can do anything meaningful with a Result
git-svn-id: https://zxing.googlecode.com/svn/trunk@288 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
b18107391b
commit
c3b3254cf6
|
@ -158,8 +158,8 @@ public final class BarcodeReaderCaptureActivity extends Activity {
|
||||||
|
|
||||||
Context context = getApplication();
|
Context context = getApplication();
|
||||||
ParsedReaderResult readerResult = parseReaderResult(rawResult);
|
ParsedReaderResult readerResult = parseReaderResult(rawResult);
|
||||||
Handler handler = new ResultHandler(this, readerResult);
|
ResultHandler handler = new ResultHandler(this, readerResult);
|
||||||
if (canBeHandled(readerResult.getType())) {
|
if (handler.getIntent() != null) {
|
||||||
// Can be handled by some external app; ask if the user wants to
|
// Can be handled by some external app; ask if the user wants to
|
||||||
// proceed first though
|
// proceed first though
|
||||||
Message yesMessage = handler.obtainMessage(R.string.button_yes);
|
Message yesMessage = handler.obtainMessage(R.string.button_yes);
|
||||||
|
@ -193,10 +193,6 @@ public final class BarcodeReaderCaptureActivity extends Activity {
|
||||||
return readerResult;
|
return readerResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean canBeHandled(ParsedReaderResultType type) {
|
|
||||||
return !type.equals(ParsedReaderResultType.TEXT);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int getDialogTitleID(ParsedReaderResultType type) {
|
private static int getDialogTitleID(ParsedReaderResultType type) {
|
||||||
if (type.equals(ParsedReaderResultType.ADDRESSBOOK)) {
|
if (type.equals(ParsedReaderResultType.ADDRESSBOOK)) {
|
||||||
return R.string.title_add_contact;
|
return R.string.title_add_contact;
|
||||||
|
|
|
@ -43,94 +43,89 @@ import java.net.URISyntaxException;
|
||||||
*/
|
*/
|
||||||
final class ResultHandler extends Handler {
|
final class ResultHandler extends Handler {
|
||||||
|
|
||||||
private final ParsedReaderResult result;
|
private final Intent intent;
|
||||||
private final BarcodeReaderCaptureActivity captureActivity;
|
private final BarcodeReaderCaptureActivity captureActivity;
|
||||||
|
|
||||||
ResultHandler(BarcodeReaderCaptureActivity captureActivity, ParsedReaderResult result) {
|
ResultHandler(BarcodeReaderCaptureActivity captureActivity, ParsedReaderResult result) {
|
||||||
this.captureActivity = captureActivity;
|
this.captureActivity = captureActivity;
|
||||||
this.result = result;
|
this.intent = resultToIntent(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Intent resultToIntent(ParsedReaderResult result) {
|
||||||
|
Intent intent = null;
|
||||||
|
ParsedReaderResultType type = result.getType();
|
||||||
|
if (type.equals(ParsedReaderResultType.ADDRESSBOOK)) {
|
||||||
|
AddressBookDoCoMoResult addressResult = (AddressBookDoCoMoResult) result;
|
||||||
|
intent = new Intent(Contacts.Intents.Insert.ACTION, Contacts.People.CONTENT_URI);
|
||||||
|
putExtra(intent, Contacts.Intents.Insert.NAME, addressResult.getName());
|
||||||
|
putExtra(intent, Contacts.Intents.Insert.PHONE, addressResult.getPhoneNumbers());
|
||||||
|
putExtra(intent, Contacts.Intents.Insert.EMAIL, addressResult.getEmail());
|
||||||
|
putExtra(intent, Contacts.Intents.Insert.NOTES, addressResult.getNote());
|
||||||
|
putExtra(intent, Contacts.Intents.Insert.POSTAL, addressResult.getAddress());
|
||||||
|
} else if (type.equals(ParsedReaderResultType.ADDRESSBOOK_AU)) {
|
||||||
|
AddressBookAUResult addressResult = (AddressBookAUResult) result;
|
||||||
|
intent = new Intent(Contacts.Intents.Insert.ACTION, Contacts.People.CONTENT_URI);
|
||||||
|
putExtra(intent, Contacts.Intents.Insert.NAME, addressResult.getNames());
|
||||||
|
putExtra(intent, Contacts.Intents.Insert.PHONE, addressResult.getPhoneNumbers());
|
||||||
|
putExtra(intent, Contacts.Intents.Insert.EMAIL, addressResult.getEmails());
|
||||||
|
putExtra(intent, Contacts.Intents.Insert.NOTES, addressResult.getNote());
|
||||||
|
putExtra(intent, Contacts.Intents.Insert.POSTAL, addressResult.getAddress());
|
||||||
|
} else if (type.equals(ParsedReaderResultType.BOOKMARK)) {
|
||||||
|
// For now, we can only open the browser, and not actually add a bookmark
|
||||||
|
try {
|
||||||
|
intent = new Intent(Intent.VIEW_ACTION, new ContentURI(((BookmarkDoCoMoResult) result).getURI()));
|
||||||
|
} catch (URISyntaxException e) {
|
||||||
|
}
|
||||||
|
} else if (type.equals(ParsedReaderResultType.URLTO)) {
|
||||||
|
try {
|
||||||
|
intent = new Intent(Intent.VIEW_ACTION, new ContentURI(((URLTOResult) result).getURI()));
|
||||||
|
} catch (URISyntaxException e) {
|
||||||
|
}
|
||||||
|
} else if (type.equals(ParsedReaderResultType.EMAIL)) {
|
||||||
|
EmailDoCoMoResult emailResult = (EmailDoCoMoResult) result;
|
||||||
|
try {
|
||||||
|
intent = new Intent(Intent.SENDTO_ACTION, new ContentURI(emailResult.getTo()));
|
||||||
|
} catch (URISyntaxException e) {
|
||||||
|
}
|
||||||
|
putExtra(intent, "subject", emailResult.getSubject());
|
||||||
|
putExtra(intent, "body", emailResult.getBody());
|
||||||
|
} else if (type.equals(ParsedReaderResultType.EMAIL_ADDRESS)) {
|
||||||
|
EmailAddressResult emailResult = (EmailAddressResult) result;
|
||||||
|
try {
|
||||||
|
intent = new Intent(Intent.SENDTO_ACTION, new ContentURI(emailResult.getEmailAddress()));
|
||||||
|
} catch (URISyntaxException e) {
|
||||||
|
}
|
||||||
|
//} else if (type.equals(ParsedReaderResultType.GEO)) {
|
||||||
|
// GeoParsedResult geoResult = (GeoParsedResult) result;
|
||||||
|
// try {
|
||||||
|
// intent = new Intent(Intent.VIEW_ACTION, new ContentURI(geoResult.getGoogleMapsURI()));
|
||||||
|
// // or can we send the raw geo: URI to Android? maybe it'll open Maps?
|
||||||
|
// // or just open a MapView
|
||||||
|
// } catch (URISyntaxException e) {
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
} else if (type.equals(ParsedReaderResultType.UPC)) {
|
||||||
|
UPCParsedResult upcResult = (UPCParsedResult) result;
|
||||||
|
try {
|
||||||
|
ContentURI uri = new ContentURI("http://www.upcdatabase.com/item.asp?upc=" + upcResult.getUPC());
|
||||||
|
intent = new Intent(Intent.VIEW_ACTION, uri);
|
||||||
|
} catch (URISyntaxException e) {
|
||||||
|
}
|
||||||
|
} else if (type.equals(ParsedReaderResultType.URI)) {
|
||||||
|
URIParsedResult uriResult = (URIParsedResult) result;
|
||||||
|
try {
|
||||||
|
intent = new Intent(Intent.VIEW_ACTION, new ContentURI(uriResult.getURI()));
|
||||||
|
} catch (URISyntaxException e) {
|
||||||
|
}
|
||||||
|
} else if (type.equals(ParsedReaderResultType.ANDROID_INTENT)) {
|
||||||
|
intent = ((AndroidIntentParsedResult) result).getIntent();
|
||||||
|
}
|
||||||
|
return intent;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleMessage(Message message) {
|
public void handleMessage(Message message) {
|
||||||
if (message.what == R.string.button_yes) {
|
if (message.what == R.string.button_yes) {
|
||||||
Intent intent = null;
|
|
||||||
ParsedReaderResultType type = result.getType();
|
|
||||||
if (type.equals(ParsedReaderResultType.ADDRESSBOOK)) {
|
|
||||||
AddressBookDoCoMoResult addressResult = (AddressBookDoCoMoResult) result;
|
|
||||||
intent = new Intent(Contacts.Intents.Insert.ACTION, Contacts.People.CONTENT_URI);
|
|
||||||
putExtra(intent, Contacts.Intents.Insert.NAME, addressResult.getName());
|
|
||||||
putExtra(intent, Contacts.Intents.Insert.PHONE, addressResult.getPhoneNumbers());
|
|
||||||
putExtra(intent, Contacts.Intents.Insert.EMAIL, addressResult.getEmail());
|
|
||||||
putExtra(intent, Contacts.Intents.Insert.NOTES, addressResult.getNote());
|
|
||||||
putExtra(intent, Contacts.Intents.Insert.POSTAL, addressResult.getAddress());
|
|
||||||
} else if (type.equals(ParsedReaderResultType.ADDRESSBOOK_AU)) {
|
|
||||||
AddressBookAUResult addressResult = (AddressBookAUResult) result;
|
|
||||||
intent = new Intent(Contacts.Intents.Insert.ACTION, Contacts.People.CONTENT_URI);
|
|
||||||
putExtra(intent, Contacts.Intents.Insert.NAME, addressResult.getNames());
|
|
||||||
putExtra(intent, Contacts.Intents.Insert.PHONE, addressResult.getPhoneNumbers());
|
|
||||||
putExtra(intent, Contacts.Intents.Insert.EMAIL, addressResult.getEmails());
|
|
||||||
putExtra(intent, Contacts.Intents.Insert.NOTES, addressResult.getNote());
|
|
||||||
putExtra(intent, Contacts.Intents.Insert.POSTAL, addressResult.getAddress());
|
|
||||||
} else if (type.equals(ParsedReaderResultType.BOOKMARK)) {
|
|
||||||
// For now, we can only open the browser, and not actually add a bookmark
|
|
||||||
try {
|
|
||||||
intent = new Intent(Intent.VIEW_ACTION,
|
|
||||||
new ContentURI(((BookmarkDoCoMoResult) result).getURI()));
|
|
||||||
} catch (URISyntaxException e) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} else if (type.equals(ParsedReaderResultType.URLTO)) {
|
|
||||||
try {
|
|
||||||
intent = new Intent(Intent.VIEW_ACTION,
|
|
||||||
new ContentURI(((URLTOResult) result).getURI()));
|
|
||||||
} catch (URISyntaxException e) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} else if (type.equals(ParsedReaderResultType.EMAIL)) {
|
|
||||||
EmailDoCoMoResult emailResult = (EmailDoCoMoResult) result;
|
|
||||||
try {
|
|
||||||
intent = new Intent(Intent.SENDTO_ACTION, new ContentURI(emailResult.getTo()));
|
|
||||||
} catch (URISyntaxException e) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
putExtra(intent, "subject", emailResult.getSubject());
|
|
||||||
putExtra(intent, "body", emailResult.getBody());
|
|
||||||
} else if (type.equals(ParsedReaderResultType.EMAIL_ADDRESS)) {
|
|
||||||
EmailAddressResult emailResult = (EmailAddressResult) result;
|
|
||||||
try {
|
|
||||||
intent = new Intent(Intent.SENDTO_ACTION, new ContentURI(emailResult.getEmailAddress()));
|
|
||||||
} catch (URISyntaxException e) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
//} else if (type.equals(ParsedReaderResultType.GEO)) {
|
|
||||||
// GeoParsedResult geoResult = (GeoParsedResult) result;
|
|
||||||
// try {
|
|
||||||
// intent = new Intent(Intent.VIEW_ACTION, new ContentURI(geoResult.getGoogleMapsURI()));
|
|
||||||
// // or can we send the raw geo: URI to Android? maybe it'll open Maps?
|
|
||||||
// // or just open a MapView
|
|
||||||
// } catch (URISyntaxException e) {
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
} else if (type.equals(ParsedReaderResultType.UPC)) {
|
|
||||||
UPCParsedResult upcResult = (UPCParsedResult) result;
|
|
||||||
try {
|
|
||||||
ContentURI uri = new ContentURI("http://www.upcdatabase.com/item.asp?upc=" +
|
|
||||||
upcResult.getUPC());
|
|
||||||
intent = new Intent(Intent.VIEW_ACTION, uri);
|
|
||||||
} catch (URISyntaxException e) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} else if (type.equals(ParsedReaderResultType.URI)) {
|
|
||||||
URIParsedResult uriResult = (URIParsedResult) result;
|
|
||||||
try {
|
|
||||||
intent = new Intent(Intent.VIEW_ACTION, new ContentURI(uriResult.getURI()));
|
|
||||||
} catch (URISyntaxException e) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} else if (type.equals(ParsedReaderResultType.ANDROID_INTENT)) {
|
|
||||||
intent = ((AndroidIntentParsedResult) result).getIntent();
|
|
||||||
}
|
|
||||||
if (intent != null) {
|
if (intent != null) {
|
||||||
captureActivity.startActivity(intent);
|
captureActivity.startActivity(intent);
|
||||||
}
|
}
|
||||||
|
@ -139,6 +134,10 @@ final class ResultHandler extends Handler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Intent getIntent() {
|
||||||
|
return intent;
|
||||||
|
}
|
||||||
|
|
||||||
private static void putExtra(Intent intent, String key, String value) {
|
private static void putExtra(Intent intent, String key, String value) {
|
||||||
if (value != null && value.length() > 0) {
|
if (value != null && value.length() > 0) {
|
||||||
intent.putExtra(key, value);
|
intent.putExtra(key, value);
|
||||||
|
|
Loading…
Reference in a new issue