@Test public void forSelectPhoto_whenResultCancelled_doesNothing() { QRCodeActivityResultDelegate delegate = new QRCodeActivityResultDelegate(context, settingsImporter, fakeQRDecoder, analytics); delegate.onActivityResult(SELECT_PHOTO, Activity.RESULT_CANCELED, new Intent()); }
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == SELECT_PHOTO && resultCode == RESULT_OK && data != null) { final Uri imageUri = data.getData(); if (imageUri != null) { final InputStream imageStream; try { imageStream = activity.getContentResolver().openInputStream(imageUri); } catch (FileNotFoundException e) { return; } try { String response = qrCodeDecoder.decode(imageStream); String responseHash = FileUtils.getMd5Hash(new ByteArrayInputStream(response.getBytes())); if (response != null) { if (settingsImporter.fromJSON(response)) { showToast(R.string.successfully_imported_settings); analytics.logEvent(AnalyticsEvents.SETTINGS_IMPORT_QR_IMAGE, "Success", responseHash); startActivityAndCloseAllOthers(activity, MainMenuActivity.class); } else { showToast(R.string.invalid_qrcode); analytics.logEvent(AnalyticsEvents.SETTINGS_IMPORT_QR_IMAGE, "No valid settings", responseHash); } } } catch (QRCodeDecoder.InvalidException e) { showToast(R.string.invalid_qrcode); analytics.logEvent(AnalyticsEvents.SETTINGS_IMPORT_QR_IMAGE, "Invalid exception", "none"); } catch (QRCodeDecoder.NotFoundException e) { showToast(R.string.qr_code_not_found); analytics.logEvent(AnalyticsEvents.SETTINGS_IMPORT_QR_IMAGE, "No QR code", "none"); } } } }
QRCodeActivityResultDelegate implements ActivityResultDelegate { @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == SELECT_PHOTO && resultCode == RESULT_OK && data != null) { final Uri imageUri = data.getData(); if (imageUri != null) { final InputStream imageStream; try { imageStream = activity.getContentResolver().openInputStream(imageUri); } catch (FileNotFoundException e) { return; } try { String response = qrCodeDecoder.decode(imageStream); String responseHash = FileUtils.getMd5Hash(new ByteArrayInputStream(response.getBytes())); if (response != null) { if (settingsImporter.fromJSON(response)) { showToast(R.string.successfully_imported_settings); analytics.logEvent(AnalyticsEvents.SETTINGS_IMPORT_QR_IMAGE, "Success", responseHash); startActivityAndCloseAllOthers(activity, MainMenuActivity.class); } else { showToast(R.string.invalid_qrcode); analytics.logEvent(AnalyticsEvents.SETTINGS_IMPORT_QR_IMAGE, "No valid settings", responseHash); } } } catch (QRCodeDecoder.InvalidException e) { showToast(R.string.invalid_qrcode); analytics.logEvent(AnalyticsEvents.SETTINGS_IMPORT_QR_IMAGE, "Invalid exception", "none"); } catch (QRCodeDecoder.NotFoundException e) { showToast(R.string.qr_code_not_found); analytics.logEvent(AnalyticsEvents.SETTINGS_IMPORT_QR_IMAGE, "No QR code", "none"); } } } } }
QRCodeActivityResultDelegate implements ActivityResultDelegate { @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == SELECT_PHOTO && resultCode == RESULT_OK && data != null) { final Uri imageUri = data.getData(); if (imageUri != null) { final InputStream imageStream; try { imageStream = activity.getContentResolver().openInputStream(imageUri); } catch (FileNotFoundException e) { return; } try { String response = qrCodeDecoder.decode(imageStream); String responseHash = FileUtils.getMd5Hash(new ByteArrayInputStream(response.getBytes())); if (response != null) { if (settingsImporter.fromJSON(response)) { showToast(R.string.successfully_imported_settings); analytics.logEvent(AnalyticsEvents.SETTINGS_IMPORT_QR_IMAGE, "Success", responseHash); startActivityAndCloseAllOthers(activity, MainMenuActivity.class); } else { showToast(R.string.invalid_qrcode); analytics.logEvent(AnalyticsEvents.SETTINGS_IMPORT_QR_IMAGE, "No valid settings", responseHash); } } } catch (QRCodeDecoder.InvalidException e) { showToast(R.string.invalid_qrcode); analytics.logEvent(AnalyticsEvents.SETTINGS_IMPORT_QR_IMAGE, "Invalid exception", "none"); } catch (QRCodeDecoder.NotFoundException e) { showToast(R.string.qr_code_not_found); analytics.logEvent(AnalyticsEvents.SETTINGS_IMPORT_QR_IMAGE, "No QR code", "none"); } } } } QRCodeActivityResultDelegate(Activity activity, SettingsImporter settingsImporter, QRCodeDecoder qrCodeDecoder, Analytics analytics); }
QRCodeActivityResultDelegate implements ActivityResultDelegate { @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == SELECT_PHOTO && resultCode == RESULT_OK && data != null) { final Uri imageUri = data.getData(); if (imageUri != null) { final InputStream imageStream; try { imageStream = activity.getContentResolver().openInputStream(imageUri); } catch (FileNotFoundException e) { return; } try { String response = qrCodeDecoder.decode(imageStream); String responseHash = FileUtils.getMd5Hash(new ByteArrayInputStream(response.getBytes())); if (response != null) { if (settingsImporter.fromJSON(response)) { showToast(R.string.successfully_imported_settings); analytics.logEvent(AnalyticsEvents.SETTINGS_IMPORT_QR_IMAGE, "Success", responseHash); startActivityAndCloseAllOthers(activity, MainMenuActivity.class); } else { showToast(R.string.invalid_qrcode); analytics.logEvent(AnalyticsEvents.SETTINGS_IMPORT_QR_IMAGE, "No valid settings", responseHash); } } } catch (QRCodeDecoder.InvalidException e) { showToast(R.string.invalid_qrcode); analytics.logEvent(AnalyticsEvents.SETTINGS_IMPORT_QR_IMAGE, "Invalid exception", "none"); } catch (QRCodeDecoder.NotFoundException e) { showToast(R.string.qr_code_not_found); analytics.logEvent(AnalyticsEvents.SETTINGS_IMPORT_QR_IMAGE, "No QR code", "none"); } } } } QRCodeActivityResultDelegate(Activity activity, SettingsImporter settingsImporter, QRCodeDecoder qrCodeDecoder, Analytics analytics); @Override void onActivityResult(int requestCode, int resultCode, Intent data); }
QRCodeActivityResultDelegate implements ActivityResultDelegate { @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == SELECT_PHOTO && resultCode == RESULT_OK && data != null) { final Uri imageUri = data.getData(); if (imageUri != null) { final InputStream imageStream; try { imageStream = activity.getContentResolver().openInputStream(imageUri); } catch (FileNotFoundException e) { return; } try { String response = qrCodeDecoder.decode(imageStream); String responseHash = FileUtils.getMd5Hash(new ByteArrayInputStream(response.getBytes())); if (response != null) { if (settingsImporter.fromJSON(response)) { showToast(R.string.successfully_imported_settings); analytics.logEvent(AnalyticsEvents.SETTINGS_IMPORT_QR_IMAGE, "Success", responseHash); startActivityAndCloseAllOthers(activity, MainMenuActivity.class); } else { showToast(R.string.invalid_qrcode); analytics.logEvent(AnalyticsEvents.SETTINGS_IMPORT_QR_IMAGE, "No valid settings", responseHash); } } } catch (QRCodeDecoder.InvalidException e) { showToast(R.string.invalid_qrcode); analytics.logEvent(AnalyticsEvents.SETTINGS_IMPORT_QR_IMAGE, "Invalid exception", "none"); } catch (QRCodeDecoder.NotFoundException e) { showToast(R.string.qr_code_not_found); analytics.logEvent(AnalyticsEvents.SETTINGS_IMPORT_QR_IMAGE, "No QR code", "none"); } } } } QRCodeActivityResultDelegate(Activity activity, SettingsImporter settingsImporter, QRCodeDecoder qrCodeDecoder, Analytics analytics); @Override void onActivityResult(int requestCode, int resultCode, Intent data);  }