Add screencap command
authorMarius Gavrilescu <marius@ieval.ro>
Fri, 26 Jul 2013 10:03:41 +0000 (13:03 +0300)
committerMarius Gavrilescu <marius@ieval.ro>
Fri, 26 Jul 2013 10:03:41 +0000 (13:03 +0300)
res/values/strings.xml
src/ro/ieval/fonbot/Heavy.java
src/ro/ieval/fonbot/Utils.java

index 15fb4d9962f99c62f728be865c2300dbf7d1a567..6340fb062413ed1944ded531a3175143ed2693e8 100644 (file)
@@ -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 &lt;title&gt; and text &lt;text&gt; and id &lt;id&gt;. This replaces any previous notiifcation with the same id.\n
         With one argument, removes the notification with id &lt;id&gt;\n
         Example: notify 10 Hello \"Hello, world!\"
-        </string>
-    <string name="user_or_password_not_set">User or password not set</string>
-</resources>
\ No newline at end of file
+    </string>
+       <string name="user_or_password_not_set">User or password not set</string>
+       <string name="screencap_successful">Screencap successful</string>
+       <string name="screencap_failed">Screencap failed. Remember: the screencap command requires root access</string>
+       <string name="screencap_help">
+         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
+       </string>
+</resources>
index 3af6bc463e6da2b1e236048c7a67ba4360213ff3..c5c6de7462450b04f5529aaf38dc6621216e7991 100644 (file)
@@ -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();
+       }
 }
index 38c34644c8962bfca1f72f8de4644b307314d8aa..b29d2454a8e29fc4d0bdefd8d432fd97e867c774 100644 (file)
@@ -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;
                }
 
        }
This page took 0.015971 seconds and 4 git commands to generate.