From e4e4926fe4f8580a24ff0a5e9c8d28736962d63a Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Tue, 2 Sep 2014 00:28:50 +0300 Subject: [PATCH] Clean up sendMessage calls --- res/values/strings.xml | 10 +++++++++- src/ro/ieval/fonbot/DialogActivity.java | 6 +++--- src/ro/ieval/fonbot/FonBotAdminReceiver.java | 16 ++++++---------- .../fonbot/FonBotPhoneStateListener.java | 11 ++++------- src/ro/ieval/fonbot/Heavy.java | 12 ++++++------ .../fonbot/ProtectedBroadcastReceiver.java | 9 +++------ src/ro/ieval/fonbot/SmsStatusReceiver.java | 19 ++++++++++--------- 7 files changed, 41 insertions(+), 42 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index 2416822..602040c 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -26,6 +26,7 @@ The help command can be used to get a list of commands and help for them. Exampl User tapped \'%s\' User navigated away from dialog User canceled dialog + Admin disabled Admin disable requested Admin enabled Device password changed @@ -33,7 +34,7 @@ The help command can be used to get a list of commands and help for them. Exampl Device login succeeded Logging in… Phone is now idle - "Phone is ringing. Incoming number: " + "Phone is ringing. Incoming number: %s" Phone is offhook Location Latitude @@ -487,4 +488,11 @@ The help command can be used to get a list of commands and help for them. Exampl Example: rootsh stop;sleep 3;start This command is not documented (yet). + SMS sent (%s) + SMS not sent: Generic failure (%s) + SMS not sent: No service (%s) + SMS not sent: Null PDU (%s) + SMS not sent: Radio off (%s) + SMS delivered (%s) + SMS not delivered (%s) diff --git a/src/ro/ieval/fonbot/DialogActivity.java b/src/ro/ieval/fonbot/DialogActivity.java index 89ae59c..b24c25d 100644 --- a/src/ro/ieval/fonbot/DialogActivity.java +++ b/src/ro/ieval/fonbot/DialogActivity.java @@ -72,7 +72,7 @@ public final class DialogActivity extends Activity { return; Utils.sendMessage(DialogActivity.this, toNonNull(address), - toNonNull(getString(user_tapped_fmt, ((Button)v).getText()))); + user_tapped_fmt, ((Button)v).getText()); gotAnswer=true; finish(); } @@ -91,7 +91,7 @@ public final class DialogActivity extends Activity { super.onStop(); if(!gotAnswer){ Utils.sendMessage(this, toNonNull(address), - toNonNull(getString(user_navigated_away_from_dialog))); + user_navigated_away_from_dialog); gotAnswer=true; } finish(); @@ -101,7 +101,7 @@ public final class DialogActivity extends Activity { public void onBackPressed() { if(!gotAnswer){ Utils.sendMessage(this, toNonNull(address), - toNonNull(getString(user_canceled_dialog))); + user_canceled_dialog); gotAnswer=true; } finish(); diff --git a/src/ro/ieval/fonbot/FonBotAdminReceiver.java b/src/ro/ieval/fonbot/FonBotAdminReceiver.java index 108b1da..9f54239 100644 --- a/src/ro/ieval/fonbot/FonBotAdminReceiver.java +++ b/src/ro/ieval/fonbot/FonBotAdminReceiver.java @@ -42,7 +42,7 @@ public final class FonBotAdminReceiver extends DeviceAdminReceiver { public void onDisabled(@Nullable final Context context, @Nullable final Intent intent) { if(context==null) return; - Utils.sendMessage(context, toNonNull(MessageType.ADMIN), "Admin disabled"); + Utils.sendMessage(context, toNonNull(MessageType.ADMIN), admin_disabled); } @Override @@ -51,8 +51,7 @@ public final class FonBotAdminReceiver extends DeviceAdminReceiver { Log.wtf(getClass().getName(), "context is null in onDisableRequested"); throw new AssertionError("Log.wtf did not terminate the process"); } - Utils.sendMessage(context, toNonNull(MessageType.ADMIN), - toNonNull(context.getString(admin_disable_requested))); + Utils.sendMessage(context, toNonNull(MessageType.ADMIN), admin_disable_requested); return null;//TODO: write this } @@ -60,16 +59,14 @@ public final class FonBotAdminReceiver extends DeviceAdminReceiver { public void onEnabled(@Nullable final Context context, @Nullable final Intent intent) { if(context==null) return; - Utils.sendMessage(context, toNonNull(MessageType.ADMIN), - toNonNull(context.getString(admin_enabled))); + Utils.sendMessage(context, toNonNull(MessageType.ADMIN), admin_enabled); } @Override public void onPasswordChanged(@Nullable final Context context, @Nullable final Intent intent) { if(context==null) return; - Utils.sendMessage(context, toNonNull(MessageType.ADMIN), - toNonNull(context.getString(device_password_changed))); + Utils.sendMessage(context, toNonNull(MessageType.ADMIN), device_password_changed); } @SuppressWarnings("boxing") @@ -78,14 +75,13 @@ public final class FonBotAdminReceiver extends DeviceAdminReceiver { if(context==null) return; final DevicePolicyManager dpm=(DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE); - Utils.sendMessage(context, toNonNull(MessageType.WATCH_LOGIN), - toNonNull(context.getString(device_login_failed_fmt, dpm.getCurrentFailedPasswordAttempts()))); + Utils.sendMessage(context, toNonNull(MessageType.WATCH_LOGIN), device_login_failed_fmt, dpm.getCurrentFailedPasswordAttempts()); } @Override public void onPasswordSucceeded(@Nullable final Context context, @Nullable final Intent intent) { if(context==null) return; - Utils.sendMessage(context, toNonNull(MessageType.WATCH_LOGIN), toNonNull(context.getString(device_login_succeeded))); + Utils.sendMessage(context, toNonNull(MessageType.WATCH_LOGIN), device_login_succeeded); } } diff --git a/src/ro/ieval/fonbot/FonBotPhoneStateListener.java b/src/ro/ieval/fonbot/FonBotPhoneStateListener.java index 88bde87..9dde5bf 100644 --- a/src/ro/ieval/fonbot/FonBotPhoneStateListener.java +++ b/src/ro/ieval/fonbot/FonBotPhoneStateListener.java @@ -61,23 +61,20 @@ final class FonBotPhoneStateListener extends PhoneStateListener { switch(state){ case TelephonyManager.CALL_STATE_IDLE: - Utils.sendMessage(context, toNonNull(MessageType.PHONE_STATE), - toNonNull(context.getString(phone_status_idle))); + Utils.sendMessage(context, toNonNull(MessageType.PHONE_STATE), phone_status_idle); break; case TelephonyManager.CALL_STATE_RINGING: if(incomingNumber==null) return; final String name=Utils.callerId(context, incomingNumber); if(name==null) - Utils.sendMessage(context, toNonNull(MessageType.PHONE_STATE), - context.getString(phone_status_ringing)+incomingNumber); + Utils.sendMessage(context, toNonNull(MessageType.PHONE_STATE), phone_status_ringing, incomingNumber); else Utils.sendMessage(context, toNonNull(MessageType.PHONE_STATE), - context.getString(phone_status_ringing)+incomingNumber+" ("+name+")"); + phone_status_ringing, incomingNumber + " ("+name+")"); break; case TelephonyManager.CALL_STATE_OFFHOOK: - Utils.sendMessage(context, toNonNull(MessageType.PHONE_STATE), - toNonNull(context.getString(phone_status_offhook))); + Utils.sendMessage(context, toNonNull(MessageType.PHONE_STATE), phone_status_offhook); break; } } diff --git a/src/ro/ieval/fonbot/Heavy.java b/src/ro/ieval/fonbot/Heavy.java index fcc5e4d..3eb17a8 100644 --- a/src/ro/ieval/fonbot/Heavy.java +++ b/src/ro/ieval/fonbot/Heavy.java @@ -337,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) { @@ -349,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(); @@ -410,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) { @@ -425,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(); @@ -1005,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(); } diff --git a/src/ro/ieval/fonbot/ProtectedBroadcastReceiver.java b/src/ro/ieval/fonbot/ProtectedBroadcastReceiver.java index 7ad1537..6625689 100644 --- a/src/ro/ieval/fonbot/ProtectedBroadcastReceiver.java +++ b/src/ro/ieval/fonbot/ProtectedBroadcastReceiver.java @@ -45,16 +45,13 @@ public final class ProtectedBroadcastReceiver extends BroadcastReceiver { final String action=intent.getAction(); if(action.equals(Intent.ACTION_BATTERY_LOW)){ - Utils.sendMessage(context, toNonNull(MessageType.BATTERY), - toNonNull(context.getString(battery_low))); + Utils.sendMessage(context, toNonNull(MessageType.BATTERY), battery_low); Heavy.describeBatteryLevel(context, null, toNonNull(MessageType.BATTERY)); } else if(action.equals(Intent.ACTION_BATTERY_OKAY)){ - Utils.sendMessage(context, toNonNull(MessageType.BATTERY), - toNonNull(context.getString(battery_okay))); + Utils.sendMessage(context, toNonNull(MessageType.BATTERY),battery_okay); Heavy.describeBatteryLevel(context, null, toNonNull(MessageType.BATTERY)); } else if(action.equals(Intent.ACTION_BOOT_COMPLETED)){ - Utils.sendMessage(context, toNonNull(MessageType.BOOT), - toNonNull(context.getString(device_booted))); + Utils.sendMessage(context, toNonNull(MessageType.BOOT), device_booted); } else if(action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) context.startService(new Intent(context, FonBotMainService.class)); } diff --git a/src/ro/ieval/fonbot/SmsStatusReceiver.java b/src/ro/ieval/fonbot/SmsStatusReceiver.java index 30809ae..59a96a5 100644 --- a/src/ro/ieval/fonbot/SmsStatusReceiver.java +++ b/src/ro/ieval/fonbot/SmsStatusReceiver.java @@ -1,5 +1,6 @@ package ro.ieval.fonbot; +import static ro.ieval.fonbot.R.string.*; import org.eclipse.jdt.annotation.Nullable; import android.app.Activity; @@ -40,8 +41,8 @@ public final class SmsStatusReceiver extends BroadcastReceiver { * @return the string representation of the SMS */ private static String describeSMS(final Intent intent){ - return " ("+"To: "+intent.getStringExtra(EXTRA_DESTINATION)+ - ", Part: "+intent.getIntExtra(EXTRA_PART, 1)+" of "+intent.getIntExtra(EXTRA_TOTAL, 1)+")"; + return "To: "+intent.getStringExtra(EXTRA_DESTINATION)+ + ", Part: "+intent.getIntExtra(EXTRA_PART, 1)+" of "+intent.getIntExtra(EXTRA_TOTAL, 1); } /** @@ -77,28 +78,28 @@ public final class SmsStatusReceiver extends BroadcastReceiver { if(intent.getAction().startsWith(SENT_ACTION)) switch(getResultCode()){ case Activity.RESULT_OK: - Utils.sendMessage(context, replyTo, "SMS sent"+describeSMS(intent)); + Utils.sendMessage(context, replyTo, sms_sent, describeSMS(intent)); break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: - Utils.sendMessage(context, replyTo, "SMS not sent: Generic failure"+describeSMS(intent)); + Utils.sendMessage(context, replyTo, sms_not_sent_generic_failure, describeSMS(intent)); break; case SmsManager.RESULT_ERROR_NO_SERVICE: - Utils.sendMessage(context, replyTo, "SMS not sent: No service"+describeSMS(intent)); + Utils.sendMessage(context, replyTo, sms_not_sent_no_service, describeSMS(intent)); break; case SmsManager.RESULT_ERROR_NULL_PDU: - Utils.sendMessage(context, replyTo, "SMS not sent: Null PDU"+describeSMS(intent)); + Utils.sendMessage(context, replyTo, sms_not_sent_null_pdu, describeSMS(intent)); break; case SmsManager.RESULT_ERROR_RADIO_OFF: - Utils.sendMessage(context, replyTo, "SMS not sent: Radio off"+describeSMS(intent)); + Utils.sendMessage(context, replyTo, sms_not_sent_radio_off, describeSMS(intent)); break; } else if(intent.getAction().startsWith(DELIVERED_ACTION)) switch(getResultCode()){ case Activity.RESULT_OK: - Utils.sendMessage(context, replyTo, "SMS delivered"+describeSMS(intent)); + Utils.sendMessage(context, replyTo, sms_delivered, describeSMS(intent)); break; case Activity.RESULT_CANCELED: - Utils.sendMessage(context, replyTo, "SMS not delivered"+describeSMS(intent)); + Utils.sendMessage(context, replyTo, sms_not_delivered, describeSMS(intent)); break; } } -- 2.39.2