X-Git-Url: http://git.ieval.ro/?a=blobdiff_plain;f=src%2Fro%2Fieval%2Ffonbot%2FHeavy.java;h=3eb17a875685c1fe75ac9881bdd6a29b7109883f;hb=e4e4926fe4f8580a24ff0a5e9c8d28736962d63a;hp=cac04f7a14f3130d300f274bf6f4bff862fdedd8;hpb=740a6fa59649fc6edced392dbbdcd74363f489c5;p=fonbot.git diff --git a/src/ro/ieval/fonbot/Heavy.java b/src/ro/ieval/fonbot/Heavy.java index cac04f7..3eb17a8 100644 --- a/src/ro/ieval/fonbot/Heavy.java +++ b/src/ro/ieval/fonbot/Heavy.java @@ -3,11 +3,13 @@ package ro.ieval.fonbot; import static ro.ieval.fonbot.R.string.*; import static ro.ieval.fonbot.Utils.toNonNull; +import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileNotFoundException; import java.io.InputStream; +import java.io.InputStreamReader; import java.io.IOException; import java.io.OutputStream; import java.lang.reflect.InvocationTargetException; @@ -58,6 +60,7 @@ import android.os.BatteryManager; import android.os.Bundle; import android.os.Handler; import android.os.PowerManager; +import android.os.SystemClock; import android.os.Vibrator; import android.preference.PreferenceManager; import android.provider.BaseColumns; @@ -334,7 +337,7 @@ final class Heavy { try{ sock = SocketChannel.open(new InetSocketAddress(hostname, port)); } catch (final IOException e){ - Utils.sendMessage(context, replyTo, toNonNull(context.getString(cannot_connect_to_host_on_port, hostname, Integer.valueOf(port)))); + Utils.sendMessage(context, replyTo, cannot_connect_to_host_on_port, hostname, Integer.valueOf(port)); try { in.close(); } catch (IOException ex) { @@ -346,7 +349,7 @@ final class Heavy { try{ in.transferTo(0, in.size(), sock); } catch (final IOException e){ - Utils.sendMessage(context, replyTo, toNonNull(context.getString(io_error, e.getMessage()))); + Utils.sendMessage(context, replyTo, io_error, e.getMessage()); } finally { try{ in.close(); @@ -407,7 +410,7 @@ final class Heavy { sock = new Socket(hostname, port); in = sock.getInputStream(); } catch (final IOException e){ - Utils.sendMessage(context, replyTo, toNonNull(context.getString(cannot_connect_to_host_on_port, hostname, Integer.valueOf(port)))); + Utils.sendMessage(context, replyTo, cannot_connect_to_host_on_port, hostname, Integer.valueOf(port)); try { out.close(); } catch (IOException ex) { @@ -422,7 +425,7 @@ final class Heavy { while((nread = in.read(buffer)) > 0) out.write(buffer, 0, nread); } catch (final IOException e){ - Utils.sendMessage(context, replyTo, toNonNull(context.getString(io_error, e.getMessage()))); + Utils.sendMessage(context, replyTo, io_error, e.getMessage()); } finally { try{ out.close(); @@ -631,6 +634,9 @@ final class Heavy { case SMS: Utils.sendMessage(context, replyTo, sms_help); break; + case SMSQ: + Utils.sendMessage(context, replyTo, smsq_help); + break; case SMSLOG: Utils.sendMessage(context, replyTo, smslog_help); break; @@ -664,6 +670,17 @@ final class Heavy { case TORCH: Utils.sendMessage(context, replyTo, torch_help); break; + case GETFILE: + Utils.sendMessage(context, replyTo, getfile_help); + break; + case SH: + Utils.sendMessage(context, replyTo, sh_help); + break; + case ROOTSH: + Utils.sendMessage(context, replyTo, rootsh_help); + break; + default: + Utils.sendMessage(context, replyTo, command_not_documented); } } @@ -988,10 +1005,10 @@ final class Heavy { CommonDataKinds.Phone.LABEL); while(inCursor.moveToNext()) - Utils.sendMessage(context, replyTo, toNonNull(context.getString(contact_info, + Utils.sendMessage(context, replyTo, contact_info, cursor.getString(0), inCursor.getString(0), - phoneNumberType(context, inCursor.getInt(1), inCursor.getString(2))))); + phoneNumberType(context, inCursor.getInt(1), inCursor.getString(2))); inCursor.close(); } @@ -1237,16 +1254,16 @@ final class Heavy { new Thread(new Runnable() { @Override public void run() { - try { - Thread.sleep(2000); - } catch (InterruptedException e) { - //ignored - } + SystemClock.sleep(2000); handler.post(new Runnable() { @Override public void run() { - camera.takePicture(null, null, new FonBotPictureCallback(context, replyTo, hostname, port)); + try { + camera.takePicture(null, null, new FonBotPictureCallback(context, replyTo, hostname, port)); + } catch(Exception e){ + Utils.sendMessage(context, replyTo, error_while_processing_command, e.getClass().getName(), e.getMessage()); + } } }); } @@ -1404,7 +1421,7 @@ final class Heavy { * @param destination destination phone number * @param text text message contents */ - public static void sms(final Context context, final Address replyTo, final String destination, final String text){ + public static void sms(final Context context, final Address replyTo, final String destination, final String text, final boolean quiet){ final SmsManager manager=SmsManager.getDefault(); final ArrayList messages=manager.divideMessage(text); if(messages.size() > 1) @@ -1426,7 +1443,7 @@ final class Heavy { sent.putExtra(SmsStatusReceiver.EXTRA_PART, i+1); sent.putExtra(SmsStatusReceiver.EXTRA_TOTAL, messages.size()); sent.putExtra(SmsStatusReceiver.EXTRA_REPLY_TO, replyTo.toString()); - sent.setAction(SmsStatusReceiver.SENT_ACTION+i);//actions must be unique + sent.setAction(SmsStatusReceiver.SENT_ACTION+i+System.currentTimeMillis());//actions must be unique sents.add(PendingIntent.getBroadcast(context, 0, sent, PendingIntent.FLAG_UPDATE_CURRENT)); final Intent delivered=new Intent(context, SmsStatusReceiver.class); @@ -1434,12 +1451,12 @@ final class Heavy { delivered.putExtra(SmsStatusReceiver.EXTRA_PART, i+1); delivered.putExtra(SmsStatusReceiver.EXTRA_TOTAL, messages.size()); delivered.putExtra(SmsStatusReceiver.EXTRA_REPLY_TO, replyTo.toString()); - delivered.setAction(SmsStatusReceiver.DELIVERED_ACTION+i);//actions must be unique + delivered.setAction(SmsStatusReceiver.DELIVERED_ACTION+i+System.currentTimeMillis());//actions must be unique delivereds.add(PendingIntent.getBroadcast(context, 0, delivered, PendingIntent.FLAG_UPDATE_CURRENT)); } Log.d(Heavy.class.getName(), "Sending sms to "+destination); - manager.sendMultipartTextMessage(destination, null, messages, sents, delivereds); + manager.sendMultipartTextMessage(destination, null, messages, sents, quiet ? null : delivereds); } /** @@ -1464,11 +1481,16 @@ final class Heavy { do { final String fromNumber=cursor.getString(1); final String from; - final String name=Utils.callerId(context, Utils.toNonNull(fromNumber)); - if(name==null) - from=fromNumber; - else - from=fromNumber+" ("+name+')'; + if(fromNumber == null) + from = null; + else { + final String name=Utils.callerId(context, Utils.toNonNull(fromNumber)); + if(name==null) + from=fromNumber; + else + from=fromNumber+" ("+name+')'; + } + final String message=cursor.getString(2).replace("\n", "\n "); final Date date=new Date(cursor.getLong(3)); @@ -1653,13 +1675,13 @@ final class Heavy { } /** - * Poll the server for pending commands. + * Start long polling if stopped * * @param context Context instance * @param replyTo reply Address */ public static void poll(final Context context, final Address replyTo) { - Utils.sendMessage(context, replyTo, polling_server); + Utils.sendMessage(context, replyTo, starting_long_polling_if_stopped); context.startService(new Intent(context, FonBotMainService.class)); } @@ -1837,8 +1859,22 @@ final class Heavy { */ public static void reboot(final Context context, final Address replyTo, final @Nullable String reason) { final PowerManager pm=(PowerManager) context.getSystemService(Context.POWER_SERVICE); - Utils.sendMessage(context, replyTo, rebooting); - pm.reboot(reason); + Utils.sendMessage(context, replyTo, rebooting); + try { + pm.reboot(reason); + } catch (final Exception e){ + e.printStackTrace(); + } + try { + Runtime.getRuntime().exec(new String[]{ + "su", + "-c", + "reboot" + }).waitFor(); + } catch (final Exception e){ + e.printStackTrace(); + } + Utils.sendMessage(toNonNull(context), toNonNull(replyTo), reboot_failed); } /** @@ -1907,4 +1943,30 @@ final class Heavy { public static void getfile(final Context context, final Address replyTo, final String filename, final String hostname, final int port){ new GetfileExecutableRunnable(context, replyTo, filename, hostname, port).execute(); } + + /** + * Execute a command using a given shell and reply with the output. + * + * @param context Context instance + * @param replyTo reply Address + * @param shell The shell to execute with. Usually sh or su. + * @param command The command to pass to the shell. + */ + public static void execute(final Context context, final Address replyTo, final String shell, final String command) { + try { + final Process proc = Runtime.getRuntime().exec(new String[]{ + shell, + "-c", + command + }); + final BufferedReader br = new BufferedReader (new InputStreamReader(proc.getInputStream())); + String line; + while((line = br.readLine()) != null) + Utils.sendMessage(context, replyTo, line); + proc.waitFor(); + } catch (final Exception e){ + Utils.sendMessage(context, replyTo, error_while_processing_command, e.getClass().getName(), e.getMessage()); + Log.w(Heavy.class.getName(), "Error while processing command", e); + } + } }