]> iEval git - fonbot.git/blobdiff - src/ro/ieval/fonbot/Heavy.java
Let the user choose which camera to take photos with
[fonbot.git] / src / ro / ieval / fonbot / Heavy.java
index 3249db8208a37e841c75a35380ac5cd03e5ea066..85aff17016325459fb3f1d748f1b169e76ded185 100644 (file)
@@ -168,6 +168,12 @@ final class Heavy {
                        sb.append(toNonNull(context.getString(at)));
                        sb.append(" ");
                        sb.append(locationDate.toString());
+                       sb.append(". ");
+
+                       sb.append("http://openstreetmap.org/?zoom=15&mlat=");
+                       sb.append(loc.getLatitude());
+                       sb.append("&mlon=");
+                       sb.append(loc.getLongitude());
                        Utils.sendMessage(toNonNull(context), toNonNull(replyTo), toNonNull(sb.toString()));
                }
 
@@ -323,6 +329,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
         *
@@ -461,11 +503,15 @@ final class Heavy {
                case REBOOT:
                        Utils.sendMessage(context, replyTo, reboot_help);
                        break;
-               case SHUTDOWN:
-                       Utils.sendMessage(context, replyTo, shutdown_help);
-                       break;
                case NOTIFY:
                        Utils.sendMessage(context, replyTo, notify_help);
+                       break;
+               case SCREENCAP:
+                       Utils.sendMessage(context, replyTo, screencap_help);
+                       break;
+               case TORCH:
+                       Utils.sendMessage(context, replyTo, torch_help);
+                       break;
                }
        }
 
@@ -594,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);
                }
@@ -697,9 +747,6 @@ final class Heavy {
                case LOCATION:
                        nolocation(context, toNonNull(Address.BLACKHOLE));
                        break;
-               case POLL:
-                       poll(context, toNonNull(Address.BLACKHOLE), 0);
-                       break;
                case RING:
                        ring(context, toNonNull(Address.BLACKHOLE), false);
                        break;
@@ -913,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();
@@ -939,7 +986,7 @@ final class Heavy {
         * @see LocationManager#requestLocationUpdates(String, long, float, LocationListener)
         */
        public static void location(final Context context, final Address replyTo, final String provider,final long minTime,final float minDistance){
-               final LocationManager man=(LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
+               final LocationManager man=(LocationManager) context.getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
                if(locationListener!=null)
                        nolocation(context, toNonNull(Address.BLACKHOLE));
                Utils.registerOngoing(context, toNonNull(OngoingEvent.LOCATION));
@@ -1049,7 +1096,7 @@ final class Heavy {
         */
        public static void nolocation(final Context context, final Address replyTo){
                Utils.unregisterOngoing(context, toNonNull(OngoingEvent.LOCATION));
-               final LocationManager man=(LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
+               final LocationManager man=(LocationManager) context.getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
                man.removeUpdates(locationListener);
                locationListener=null;
                Utils.sendMessage(context, replyTo, no_longer_listening_for_location_updates);
@@ -1060,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();
@@ -1508,30 +1556,7 @@ final class Heavy {
         */
        public static void poll(final Context context, final Address replyTo) {
                Utils.sendMessage(context, replyTo, polling_server);
-               Utils.pollServer(context);
-       }
-
-       /**
-        * Change the server poll interval.
-        *
-        * @param context Context instance
-        * @param replyTo reply Address
-        * @param ms server poll interval in milliseconds. If 0, server poll is disabled
-        */
-       public static void poll(final Context context, final Address replyTo, final long ms){
-               final AlarmManager man=(AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
-               final Intent pollAlarm=new Intent(context, FonBotMainService.class);
-               pollAlarm.setAction(FonBotMainService.ACTION_TRIGGER_POLL);
-               final PendingIntent intent=PendingIntent.getService(context, 0, pollAlarm, 0);
-               if(ms==0){
-                       Utils.unregisterOngoing(context, toNonNull(OngoingEvent.POLL));
-                       man.cancel(intent);
-                       Utils.sendMessage(context, replyTo, polling_stopped);
-               } else {
-                       Utils.registerOngoing(context, toNonNull(OngoingEvent.POLL));
-                       man.setRepeating(AlarmManager.RTC_WAKEUP, 0, ms, intent);
-                       Utils.sendMessage(context, replyTo, polling_every_milliseconds, Long.valueOf(ms));
-               }
+               context.startService(new Intent(context, FonBotMainService.class));
        }
 
        /**
@@ -1743,4 +1768,26 @@ 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();
+       }
+
+       /**
+        * Toggle the torch state using the Torch (net.cactii.torch2) app.
+        *
+        * @param context Context instance
+        * @param replyTo reply Address
+        */
+       public static void torch(final Context context, final Address replyTo){
+               context.sendBroadcast(new Intent("net.cactii.flash2.TOGGLE_FLASHLIGHT"));
+               Utils.sendMessage(context, replyTo, toggling_torch_state);
+       }
 }
This page took 0.032179 seconds and 4 git commands to generate.