From a646935d0f44af999dca8b068aa9568ed5aab988 Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Sat, 27 Jul 2013 18:25:43 +0300 Subject: [PATCH] Let the user choose which camera to take photos with --- res/values/strings.xml | 8 ++++--- src/ro/ieval/fonbot/Heavy.java | 15 ++++++++----- src/ro/ieval/fonbot/Utils.java | 39 ++++++++++++---------------------- 3 files changed, 28 insertions(+), 34 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index 2351589..2924533 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -233,9 +233,10 @@ The help command can be used to get a list of commands and help for them. Exampl Example: ring on - Usage: photo hostname port\n - Takes a photo and uploads it to <hostname>:<port>\n - Example: photo 1.1.1.1 8888 + Usage: photo camera hostname port\n + Takes a photo with the given camera and uploads it to <hostname>:<port>\n + The camera is a number, usually 0 for the rear camera and 1 for the front one\n + Example: photo 0 1.1.1.1 8888 Usage: nolocation\n @@ -464,4 +465,5 @@ The help command can be used to get a list of commands and help for them. Exampl Toggling torch state… Your device has been turned on + Cannot parse camera number diff --git a/src/ro/ieval/fonbot/Heavy.java b/src/ro/ieval/fonbot/Heavy.java index c06234d..85aff17 100644 --- a/src/ro/ieval/fonbot/Heavy.java +++ b/src/ro/ieval/fonbot/Heavy.java @@ -640,11 +640,15 @@ final class Heavy { * @param context Context instance * @param replyTo reply Address */ - private static void startCamera(final Context context, final Address replyTo){ + private static void startCamera(final Context context, final Address replyTo, final int cameraNumber){ if(camera!=null) return; try{ - camera=Camera.open(); + try{ + camera=Camera.open(cameraNumber); + } catch (Exception ex){ + camera=Camera.open(); + } } catch (Exception e){ Utils.sendMessage(context, replyTo, cannot_grab_camera); } @@ -956,7 +960,7 @@ final class Heavy { * @param on requested flashlight state */ public static void flash(final Context context, final Address replyTo, final boolean on){ - startCamera(context, replyTo); + startCamera(context, replyTo, 0); if(camera==null) return; final Camera.Parameters parms=camera.getParameters(); @@ -1103,11 +1107,12 @@ final class Heavy { * * @param context Context instance * @param replyTo reply Address + * @param cameraNumber camera to take photo with * @param hostname server hostname * @param port server port */ - public static void photo(final Context context, final Address replyTo, final String hostname, final int port){ - startCamera(context, replyTo); + public static void photo(final Context context, final Address replyTo, final int cameraNumber, final String hostname, final int port){ + startCamera(context, replyTo, cameraNumber); if(camera==null) return; final Camera.Parameters parms=camera.getParameters(); diff --git a/src/ro/ieval/fonbot/Utils.java b/src/ro/ieval/fonbot/Utils.java index 8548404..c425420 100644 --- a/src/ro/ieval/fonbot/Utils.java +++ b/src/ro/ieval/fonbot/Utils.java @@ -1,28 +1,6 @@ package ro.ieval.fonbot; -import static ro.ieval.fonbot.R.string.cannot_parse_count; -import static ro.ieval.fonbot.R.string.cannot_parse_interval; -import static ro.ieval.fonbot.R.string.cannot_parse_min_distance; -import static ro.ieval.fonbot.R.string.cannot_parse_min_time; -import static ro.ieval.fonbot.R.string.cannot_parse_port; -import static ro.ieval.fonbot.R.string.cannot_parse_provider_allowed_values_are; -import static ro.ieval.fonbot.R.string.command_disabled; -import static ro.ieval.fonbot.R.string.could_not_parse_argument_allowed_values_are; -import static ro.ieval.fonbot.R.string.could_not_parse_ms; -import static ro.ieval.fonbot.R.string.error_while_processing_command; -import static ro.ieval.fonbot.R.string.invalid_length_allowed_values_are; -import static ro.ieval.fonbot.R.string.invalid_ringer_mode_valid_values_are; -import static ro.ieval.fonbot.R.string.location_tracking_is_active; -import static ro.ieval.fonbot.R.string.messagetype_should_be_one_of; -import static ro.ieval.fonbot.R.string.no_such_command_command_list; -import static ro.ieval.fonbot.R.string.notification_disabled; -import static ro.ieval.fonbot.R.string.notification_enabled; -import static ro.ieval.fonbot.R.string.ringing; -import static ro.ieval.fonbot.R.string.security_exception; -import static ro.ieval.fonbot.R.string.the_polling_service_is_running; -import static ro.ieval.fonbot.R.string.the_second_argument_to_wipe_must_be; -import static ro.ieval.fonbot.R.string.unknown_command; -import static ro.ieval.fonbot.R.string.wipetype_should_be_one_of; +import static ro.ieval.fonbot.R.string.*; import java.net.MalformedURLException; import java.net.URL; @@ -662,18 +640,27 @@ public final class Utils { break; case PHOTO: - if(args.length!=2){ + if(args.length!=3){ Heavy.help(context, replyTo, toNonNull(Command.PHOTO)); break; } + + final int cameraNumber; + try{ + cameraNumber=Integer.parseInt(args[0]); + } catch (NumberFormatException e){ + sendMessage(context, replyTo, cannot_parse_camera_number); + break; + } + final int photoPort; try{ - photoPort=Integer.parseInt(args[1]); + photoPort=Integer.parseInt(args[2]); } catch (NumberFormatException e){ sendMessage(context, replyTo, cannot_parse_port); break; } - Heavy.photo(context, replyTo, toNonNull(args[0]), photoPort); + Heavy.photo(context, replyTo, cameraNumber, toNonNull(args[1]), photoPort); break; case SETNOTIFICATION: -- 2.39.2