From: Marius Gavrilescu Date: Fri, 26 Jul 2013 10:03:41 +0000 (+0300) Subject: Add screencap command X-Git-Tag: 0.000_001~30 X-Git-Url: http://git.ieval.ro/?a=commitdiff_plain;h=0f74646f9a4c1e195e2ea3600ae3c03d51fd11d5;p=fonbot.git Add screencap command --- diff --git a/res/values/strings.xml b/res/values/strings.xml index 15fb4d9..6340fb0 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -448,6 +448,13 @@ The help command can be used to get a list of commands and help for them. Exampl With three arguments, shows a notification with title <title> and text <text> and id <id>. This replaces any previous notiifcation with the same id.\n With one argument, removes the notification with id <id>\n Example: notify 10 Hello \"Hello, world!\" - - User or password not set - \ No newline at end of file + + User or password not set + Screencap successful + Screencap failed. Remember: the screencap command requires root access + + Usage: screencap filename + Takes a screen capture and saves it to the given filename as a PNG image file. + Example: screencap /storage/sdcard0/screencap.png + + diff --git a/src/ro/ieval/fonbot/Heavy.java b/src/ro/ieval/fonbot/Heavy.java index 3af6bc4..c5c6de7 100644 --- a/src/ro/ieval/fonbot/Heavy.java +++ b/src/ro/ieval/fonbot/Heavy.java @@ -323,6 +323,42 @@ final class Heavy { } } + /** + * Runnable that takes a screen capture and stores it in a file. + */ + private static final class ScreencapRunnable implements Runnable{ + private final Context context; + private final Address replyTo; + private final String filename; + + ScreencapRunnable(final Context context, final Address replyTo, final String filename){ + this.context=context; + this.replyTo=replyTo; + this.filename=filename; + } + + @Override + public void run(){ + final int exitCode; + try { + exitCode=Runtime.getRuntime().exec(new String[]{ + "su", + "-c", + "screencap -p \"" + filename + "\"" + }).waitFor(); + } catch (final Exception e){ + e.printStackTrace(); + Utils.sendMessage(toNonNull(context), toNonNull(replyTo), screencap_failed); + return; + } + + if(exitCode == 0 && new File(filename).exists()) + Utils.sendMessage(toNonNull(context), toNonNull(replyTo), screencap_successful); + else + Utils.sendMessage(toNonNull(context), toNonNull(replyTo), screencap_failed); + } + } + /** * Get help for a particular command * @@ -463,6 +499,10 @@ final class Heavy { break; case NOTIFY: Utils.sendMessage(context, replyTo, notify_help); + break; + case SCREENCAP: + Utils.sendMessage(context, replyTo, screencap_help); + break; } } @@ -1740,4 +1780,15 @@ final class Heavy { build()); Utils.sendMessage(context, replyTo, notification_shown); } + + /** + * Take a screen capture. Uses the screencap utility and requires root. + * + * @param context Context instance + * @param replyTo reply Address + * @param filename capture file location + */ + public static void screencap(final Context context, final Address replyTo, final String filename){ + new Thread(new ScreencapRunnable(context, replyTo, filename)).start(); + } } diff --git a/src/ro/ieval/fonbot/Utils.java b/src/ro/ieval/fonbot/Utils.java index 38c3464..b29d245 100644 --- a/src/ro/ieval/fonbot/Utils.java +++ b/src/ro/ieval/fonbot/Utils.java @@ -86,7 +86,7 @@ public final class Utils { NEXT, PREV, BATT, CALLLOG, SMSLOG, LS, RM, CONTACTS, DISABLE, ENABLE, POLL, HANGUP, ANSWER, LAUNCH, DATA, - GPS, GLOCATION, REBOOT, NOTIFY + GPS, GLOCATION, REBOOT, NOTIFY, SCREENCAP } /** @@ -1022,6 +1022,15 @@ public final class Utils { else Heavy.notify(context, replyTo, id, toNonNull(args[1]), toNonNull(args[2])); break; + + case SCREENCAP: + if(args.length != 1){ + Heavy.help(context, replyTo, toNonNull(Command.SCREENCAP)); + return; + } + + Heavy.screencap(context, replyTo, args[0]); + break; } }