Better exception handling

This commit is contained in:
Victor Shcherb 2012-11-15 19:03:31 +01:00
parent 6800e11bbc
commit 0d5f9730f9

View file

@ -362,77 +362,99 @@ public class MapActivityActions implements DialogProvider {
final double latitude = args.getDouble(KEY_LATITUDE); final double latitude = args.getDouble(KEY_LATITUDE);
final double longitude = args.getDouble(KEY_LONGITUDE); final double longitude = args.getDouble(KEY_LONGITUDE);
final int zoom = args.getInt(KEY_ZOOM); final int zoom = args.getInt(KEY_ZOOM);
try {
final String shortOsmUrl = MapUtils.buildShortOsmUrl(latitude, longitude, zoom); final String shortOsmUrl = MapUtils.buildShortOsmUrl(latitude, longitude, zoom);
final String appLink = "http://download.osmand.net/go?lat="+((float) latitude)+"&lon="+((float)longitude) +"&z="+zoom; final String appLink = "http://download.osmand.net/go?lat=" + ((float) latitude) + "&lon=" + ((float) longitude) + "&z=" + zoom;
if(which == 0){
String email = mapActivity.getString(R.string.send_location_email_pattern, shortOsmUrl, appLink);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("vnd.android.cursor.dir/email"); //$NON-NLS-1$
intent.putExtra(Intent.EXTRA_SUBJECT, "Location"); //$NON-NLS-1$
intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(email));
intent.setType("text/html");
mapActivity.startActivity(Intent.createChooser(intent, getString(R.string.send_location)));
} else {
String sms = mapActivity.getString(R.string.send_location_sms_pattern, shortOsmUrl, appLink); String sms = mapActivity.getString(R.string.send_location_sms_pattern, shortOsmUrl, appLink);
if(which == 1){ if (which == 0) {
Intent sendIntent = new Intent(Intent.ACTION_VIEW); sendEmail(shortOsmUrl, appLink);
sendIntent.putExtra("sms_body", sms); } else if (which == 1) {
sendIntent.setType("vnd.android-dir/mms-sms"); sendSms(sms);
mapActivity.startActivity(sendIntent); } else if (which == 2) {
} else if (which == 2){ sendToClipboard(sms);
ClipboardManager clipboard = (ClipboardManager) mapActivity.getSystemService(Activity.CLIPBOARD_SERVICE); } else if (which == 3) {
clipboard.setText(sms); sendGeoActivity(latitude, longitude, zoom);
} else if(which == 3){ } else if (which == 4) {
final String simpleGeo = "geo:"+((float) latitude)+","+((float)longitude) +"?z="+zoom; sendQRCode(latitude, longitude);
Uri location = Uri.parse(simpleGeo);
Intent mapIntent = new Intent(Intent.ACTION_VIEW, location);
mapActivity.startActivity(mapIntent);
} else if(which == 4){
Bundle bundle = new Bundle();
bundle.putFloat("LAT", (float) latitude);
bundle.putFloat("LONG", (float)longitude);
Intent intent = new Intent();
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setAction(ZXING_BARCODE_SCANNER_ACTIVITY);
ResolveInfo resolved = mapActivity.getPackageManager().resolveActivity(intent,
PackageManager.MATCH_DEFAULT_ONLY);
if (resolved != null) {
intent.putExtra("ENCODE_TYPE", "LOCATION_TYPE");
intent.putExtra("ENCODE_DATA", bundle);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
mapActivity.startActivity(intent);
} else {
if (Version.isGooglePlayEnabled(mapActivity)) {
AlertDialog.Builder builder = new AccessibleAlertBuilder(mapActivity);
builder.setMessage(getString(R.string.zxing_barcode_scanner_not_found));
builder.setPositiveButton(getString(R.string.default_buttons_yes), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=pname:" + ZXING_BARCODE_SCANNER_COMPONENT));
try {
mapActivity.startActivity(intent);
} catch (ActivityNotFoundException e) {
}
}
});
builder.setNegativeButton(getString(R.string.default_buttons_no), null);
builder.show();
} else {
Toast.makeText(mapActivity, R.string.zxing_barcode_scanner_not_found, Toast.LENGTH_LONG).show();
}
}
} }
} catch (RuntimeException e) {
Toast.makeText(mapActivity, R.string.input_output_error, Toast.LENGTH_SHORT).show();
} }
} }
}); });
return builder.create(); return builder.create();
} }
private void sendEmail(final String shortOsmUrl, final String appLink) {
String email = mapActivity.getString(R.string.send_location_email_pattern, shortOsmUrl, appLink);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("vnd.android.cursor.dir/email"); //$NON-NLS-1$
intent.putExtra(Intent.EXTRA_SUBJECT, "Location"); //$NON-NLS-1$
intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(email));
intent.setType("text/html");
mapActivity.startActivity(Intent.createChooser(intent, getString(R.string.send_location)));
}
private void sendSms(String sms) {
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", sms);
sendIntent.setType("vnd.android-dir/mms-sms");
mapActivity.startActivity(sendIntent);
}
private void sendToClipboard(String sms) {
ClipboardManager clipboard = (ClipboardManager) mapActivity.getSystemService(Activity.CLIPBOARD_SERVICE);
clipboard.setText(sms);
}
private void sendGeoActivity(final double latitude, final double longitude, final int zoom) {
final String simpleGeo = "geo:"+((float) latitude)+","+((float)longitude) +"?z="+zoom;
Uri location = Uri.parse(simpleGeo);
Intent mapIntent = new Intent(Intent.ACTION_VIEW, location);
mapActivity.startActivity(mapIntent);
}
private void sendQRCode(final double latitude, final double longitude) {
Bundle bundle = new Bundle();
bundle.putFloat("LAT", (float) latitude);
bundle.putFloat("LONG", (float) longitude);
Intent intent = new Intent();
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setAction(ZXING_BARCODE_SCANNER_ACTIVITY);
ResolveInfo resolved = mapActivity.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
if (resolved != null) {
intent.putExtra("ENCODE_TYPE", "LOCATION_TYPE");
intent.putExtra("ENCODE_DATA", bundle);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
mapActivity.startActivity(intent);
} else {
if (Version.isGooglePlayEnabled(mapActivity)) {
AlertDialog.Builder builder = new AccessibleAlertBuilder(mapActivity);
builder.setMessage(getString(R.string.zxing_barcode_scanner_not_found));
builder.setPositiveButton(getString(R.string.default_buttons_yes), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=pname:"
+ ZXING_BARCODE_SCANNER_COMPONENT));
try {
mapActivity.startActivity(intent);
} catch (ActivityNotFoundException e) {
}
}
});
builder.setNegativeButton(getString(R.string.default_buttons_no), null);
builder.show();
} else {
Toast.makeText(mapActivity, R.string.zxing_barcode_scanner_not_found, Toast.LENGTH_LONG).show();
}
}
}
protected void aboutRoute() { protected void aboutRoute() {
Intent intent = new Intent(mapActivity, ShowRouteInfoActivity.class); Intent intent = new Intent(mapActivity, ShowRouteInfoActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);