From a5259b32f390d9adf8bca1b7a4bbb620bde11479 Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Tue, 2 Sep 2014 00:49:14 +0300 Subject: [PATCH] Let users disable command confirmations over SMS Most FonBot commands confirm their execution with a short message, but this can be wasteful is SMS messages are used. This commit adds a new preference that disables confirmation messages if the command was sent over SMS. Thanks to Valerio Bozzolan for the suggestion in bug 14. --- res/xml/prefs.xml | 1 + src/ro/ieval/fonbot/Heavy.java | 80 +++++++++++----------- src/ro/ieval/fonbot/SmsStatusReceiver.java | 2 +- src/ro/ieval/fonbot/Utils.java | 22 +++++- 4 files changed, 61 insertions(+), 44 deletions(-) diff --git a/res/xml/prefs.xml b/res/xml/prefs.xml index 6d90c6a..3ed8b4f 100644 --- a/res/xml/prefs.xml +++ b/res/xml/prefs.xml @@ -4,6 +4,7 @@ + diff --git a/src/ro/ieval/fonbot/Heavy.java b/src/ro/ieval/fonbot/Heavy.java index 3eb17a8..f87f3f5 100644 --- a/src/ro/ieval/fonbot/Heavy.java +++ b/src/ro/ieval/fonbot/Heavy.java @@ -290,7 +290,7 @@ final class Heavy { Utils.sendMessage(toNonNull(context), toNonNull(replyTo), cannot_connect_to_host_on_port, hostname, Integer.valueOf(port)); return null; } - Utils.sendMessage(toNonNull(context), toNonNull(replyTo), photo_sent); + Utils.sendConfirmMessage(toNonNull(context), toNonNull(replyTo), photo_sent); return null; } } @@ -362,7 +362,7 @@ final class Heavy { //ignored } } - Utils.sendMessage(context, replyTo, file_sent); + Utils.sendConfirmMessage(context, replyTo, file_sent); } } @@ -438,7 +438,7 @@ final class Heavy { //ignored } } - Utils.sendMessage(context, replyTo, file_received); + Utils.sendConfirmMessage(context, replyTo, file_received); } } @@ -479,7 +479,7 @@ final class Heavy { return; camera.stopPreview(); stopCamera(); - Utils.sendMessage(toNonNull(context), toNonNull(replyTo), sending_photo); + Utils.sendConfirmMessage(toNonNull(context), toNonNull(replyTo), sending_photo); new SendDataAsyncTask(toNonNull(context), toNonNull(replyTo), toNonNull(hostname), port, data).execute(); } } @@ -514,7 +514,7 @@ final class Heavy { } if(exitCode == 0 && new File(filename).exists()) - Utils.sendMessage(toNonNull(context), toNonNull(replyTo), screencap_successful); + Utils.sendConfirmMessage(toNonNull(context), toNonNull(replyTo), screencap_successful); else Utils.sendMessage(toNonNull(context), toNonNull(replyTo), screencap_failed); } @@ -799,7 +799,7 @@ final class Heavy { man.setRingerMode(AudioManager.RINGER_MODE_NORMAL); savedRingVolume=man.getStreamVolume(AudioManager.STREAM_RING); man.setStreamVolume(AudioManager.STREAM_RING, man.getStreamMaxVolume(AudioManager.STREAM_RING), 0); - Utils.sendMessage(context, replyTo, ringing); + Utils.sendConfirmMessage(context, replyTo, ringing); ringtone.play(); } @@ -832,7 +832,7 @@ final class Heavy { private static void stopAlarm(final Context context, final Address replyTo){ Utils.unregisterOngoing(context, toNonNull(OngoingEvent.RING)); final AudioManager man=(AudioManager) context.getSystemService(Context.AUDIO_SERVICE); - Utils.sendMessage(context, replyTo, no_longer_ringing); + Utils.sendConfirmMessage(context, replyTo, no_longer_ringing); ringtone.stop(); man.setStreamVolume(AudioManager.STREAM_RING, savedRingVolume, 0); man.setRingerMode(savedRingerMode); @@ -897,11 +897,11 @@ final class Heavy { if(on) { adapter.enable(); - Utils.sendMessage(context, replyTo, enabling_bluetooth); + Utils.sendConfirmMessage(context, replyTo, enabling_bluetooth); } else { adapter.disable(); - Utils.sendMessage(context, replyTo, disabling_bluetooth); + Utils.sendConfirmMessage(context, replyTo, disabling_bluetooth); } } @@ -1093,9 +1093,9 @@ final class Heavy { intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); final String name=Utils.callerId(context, nr); if(name==null) - Utils.sendMessage(context, replyTo, dialing, nr); + Utils.sendConfirmMessage(context, replyTo, dialing, nr); else - Utils.sendMessage(context, replyTo, dialing, nr+" ("+name+")"); + Utils.sendConfirmMessage(context, replyTo, dialing, nr+" ("+name+")"); context.startActivity(intent); } @@ -1117,7 +1117,7 @@ final class Heavy { Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_NO_USER_ACTION| Intent.FLAG_FROM_BACKGROUND); - Utils.sendMessage(context, toNonNull(replyTo), showing_dialog); + Utils.sendConfirmMessage(context, toNonNull(replyTo), showing_dialog); context.startActivity(intent); } @@ -1166,7 +1166,7 @@ final class Heavy { Utils.sendMessage(context, replyTo, last_known_location); locationListener.onLocationChanged(lastKnownLocation); } - Utils.sendMessage(context, replyTo, listening_for_location_updates); + Utils.sendConfirmMessage(context, replyTo, listening_for_location_updates); man.requestLocationUpdates(provider, minTime, minDistance, locationListener); } @@ -1179,7 +1179,7 @@ final class Heavy { public static void lock(final Context context, final Address replyTo) { final DevicePolicyManager dpm=(DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE); dpm.lockNow(); - Utils.sendMessage(context, replyTo, device_locked); + Utils.sendConfirmMessage(context, replyTo, device_locked); } /** @@ -1193,7 +1193,7 @@ final class Heavy { final Intent intent=new Intent("com.android.music.musicservicecommand"); intent.putExtra("command", command); context.sendBroadcast(intent); - Utils.sendMessage(context, replyTo, command_sent); + Utils.sendConfirmMessage(context, replyTo, command_sent); } /** @@ -1220,7 +1220,7 @@ final class Heavy { 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); + Utils.sendConfirmMessage(context, replyTo, no_longer_listening_for_location_updates); } /** @@ -1379,7 +1379,7 @@ final class Heavy { */ public static void rm(final Context context, final Address replyTo, final String filename){ if(new File(filename).delete()) - Utils.sendMessage(context, replyTo, file_deleted); + Utils.sendConfirmMessage(context, replyTo, file_deleted); else Utils.sendMessage(context, replyTo, error_while_deleting_file); } @@ -1395,7 +1395,7 @@ final class Heavy { final DevicePolicyManager dpm=(DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE); dpm.resetPassword("", 0); - Utils.sendMessage(context, replyTo, password_cleared); + Utils.sendConfirmMessage(context, replyTo, password_cleared); } /** @@ -1410,7 +1410,7 @@ final class Heavy { final DevicePolicyManager dpm=(DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE); dpm.resetPassword(password, 0); - Utils.sendMessage(context, replyTo, password_set); + Utils.sendConfirmMessage(context, replyTo, password_set); } /** @@ -1425,7 +1425,7 @@ final class Heavy { final SmsManager manager=SmsManager.getDefault(); final ArrayList messages=manager.divideMessage(text); if(messages.size() > 1) - Utils.sendMessage(context, replyTo, message_was_split_into_parts, Integer.valueOf(messages.size())); + Utils.sendConfirmMessage(context, replyTo, message_was_split_into_parts, Integer.valueOf(messages.size())); final ArrayList sents=new ArrayList(messages.size()); final ArrayList delivereds=new ArrayList(messages.size()); @@ -1519,7 +1519,7 @@ final class Heavy { @Override public void onInit(final int status) { if(status==TextToSpeech.SUCCESS){ - Utils.sendMessage(context, replyTo, speaking); + Utils.sendConfirmMessage(context, replyTo, speaking); tts.speak(text, TextToSpeech.QUEUE_ADD, null); } else Utils.sendMessage(context, replyTo, tts_engine_not_available); @@ -1548,7 +1548,7 @@ final class Heavy { */ public static void toast(final Context context, final Address replyTo, final String text, final int duration){ Toast.makeText(context,text,duration).show(); - Utils.sendMessage(context, replyTo, toast_shown); + Utils.sendConfirmMessage(context, replyTo, toast_shown); } /** @@ -1560,7 +1560,7 @@ final class Heavy { */ public static void vibrate(final Context context, final Address replyTo, final long ms){ final Vibrator v=(Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); - Utils.sendMessage(context, replyTo, vibrating); + Utils.sendConfirmMessage(context, replyTo, vibrating); v.vibrate(ms); } @@ -1577,7 +1577,7 @@ final class Heavy { intent.setData(uri); intent.setFlags(Intent.FLAG_FROM_BACKGROUND|Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); - Utils.sendMessage(context, replyTo, url_opened); + Utils.sendConfirmMessage(context, replyTo, url_opened); } catch(ActivityNotFoundException e){ Utils.sendMessage(context, replyTo, no_activity_found_for_this_url); } catch(Exception e){ @@ -1610,9 +1610,9 @@ final class Heavy { final WifiManager man=(WifiManager) context.getSystemService(Context.WIFI_SERVICE); man.setWifiEnabled(on); if(on) - Utils.sendMessage(context, replyTo, enabling_wifi); + Utils.sendConfirmMessage(context, replyTo, enabling_wifi); else - Utils.sendMessage(context, replyTo, disabling_wifi); + Utils.sendConfirmMessage(context, replyTo, disabling_wifi); } /** @@ -1646,7 +1646,7 @@ final class Heavy { public static void disable(final Context context, final Address replyTo, final Command command){ PreferenceManager.getDefaultSharedPreferences(context).edit() .putBoolean(command+"disabled", true).commit(); - Utils.sendMessage(context, replyTo, command_disabled, command); + Utils.sendConfirmMessage(context, replyTo, command_disabled, command); } /** @@ -1659,7 +1659,7 @@ final class Heavy { public static void enable(final Context context, final Address replyTo, final Command command){ PreferenceManager.getDefaultSharedPreferences(context).edit() .remove(command+"disabled").commit(); - Utils.sendMessage(context, replyTo, command_enabled, command); + Utils.sendConfirmMessage(context, replyTo, command_enabled, command); } @@ -1681,7 +1681,7 @@ final class Heavy { * @param replyTo reply Address */ public static void poll(final Context context, final Address replyTo) { - Utils.sendMessage(context, replyTo, starting_long_polling_if_stopped); + Utils.sendConfirmMessage(context, replyTo, starting_long_polling_if_stopped); context.startService(new Intent(context, FonBotMainService.class)); } @@ -1746,7 +1746,7 @@ final class Heavy { return; } context.startActivity(intent); - Utils.sendMessage(context, replyTo, app_launched); + Utils.sendConfirmMessage(context, replyTo, app_launched); } /** @@ -1781,10 +1781,10 @@ final class Heavy { try{ if(enable){ getITelephony(context).enableDataConnectivity(); - Utils.sendMessage(context, replyTo, enabling_data); + Utils.sendConfirmMessage(context, replyTo, enabling_data); } else { getITelephony(context).disableDataConnectivity(); - Utils.sendMessage(context, replyTo, disabling_data); + Utils.sendConfirmMessage(context, replyTo, disabling_data); } } catch(Exception e){ Utils.sendMessage(context, replyTo, exception_while_getting_itelephony, @@ -1815,9 +1815,9 @@ final class Heavy { public static void gps(final Context context, final Address replyTo, final boolean enabled) { Secure.setLocationProviderEnabled(context.getContentResolver(), LocationManager.GPS_PROVIDER, enabled); if(enabled) - Utils.sendMessage(context, replyTo, enabling_gps); + Utils.sendConfirmMessage(context, replyTo, enabling_gps); else - Utils.sendMessage(context, replyTo, disabling_gps); + Utils.sendConfirmMessage(context, replyTo, disabling_gps); } /** @@ -1843,9 +1843,9 @@ final class Heavy { public static void glocation(final Context context, final Address replyTo, final boolean enabled) { Secure.setLocationProviderEnabled(context.getContentResolver(), LocationManager.NETWORK_PROVIDER, enabled); if(enabled) - Utils.sendMessage(context, replyTo, enabling_network_location); + Utils.sendConfirmMessage(context, replyTo, enabling_network_location); else - Utils.sendMessage(context, replyTo, disabling_network_location); + Utils.sendConfirmMessage(context, replyTo, disabling_network_location); } /** @@ -1859,7 +1859,7 @@ 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); + Utils.sendConfirmMessage(context, replyTo, rebooting); try { pm.reboot(reason); } catch (final Exception e){ @@ -1887,7 +1887,7 @@ final class Heavy { public static void notify(final Context context, final Address replyTo, final int id) { final NotificationManager man=(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); man.cancel(id); - Utils.sendMessage(context, replyTo, notification_canceled); + Utils.sendConfirmMessage(context, replyTo, notification_canceled); } /** @@ -1906,7 +1906,7 @@ final class Heavy { setContentText(text). setSmallIcon(android.R.drawable.stat_notify_sync_noanim). build()); - Utils.sendMessage(context, replyTo, notification_shown); + Utils.sendConfirmMessage(context, replyTo, notification_shown); } /** @@ -1928,7 +1928,7 @@ final class Heavy { */ 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); + Utils.sendConfirmMessage(context, replyTo, toggling_torch_state); } /** diff --git a/src/ro/ieval/fonbot/SmsStatusReceiver.java b/src/ro/ieval/fonbot/SmsStatusReceiver.java index 59a96a5..8ae9643 100644 --- a/src/ro/ieval/fonbot/SmsStatusReceiver.java +++ b/src/ro/ieval/fonbot/SmsStatusReceiver.java @@ -78,7 +78,7 @@ 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.sendConfirmMessage(context, replyTo, sms_sent, describeSMS(intent)); break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: Utils.sendMessage(context, replyTo, sms_not_sent_generic_failure, describeSMS(intent)); diff --git a/src/ro/ieval/fonbot/Utils.java b/src/ro/ieval/fonbot/Utils.java index 37049c5..40a5e57 100644 --- a/src/ro/ieval/fonbot/Utils.java +++ b/src/ro/ieval/fonbot/Utils.java @@ -271,6 +271,22 @@ public final class Utils { sendMessage(context, address, toNonNull(context.getString(resource, args))); } + /** + * Send a confirmation message to a certain Address. A confirm message is a message that: + * 1) Confers no information (except for confirming that the command was executed). + * 2) May be dropped if the user asked so. + * + * @param context Context instance + * @param address destination Address + * @param resource String resource for the message text + * @param args format parameters for the resource + */ + public static void sendConfirmMessage(final Context context, final Address address, final int resource, final Object... args){ + final SharedPreferences sp=PreferenceManager.getDefaultSharedPreferences(context); + if(address.protocol != Protocol.SMS || !sp.getBoolean("expensive_sms", false)) + sendMessage(context, address, resource, args); + } + /** * Send a notification to the user. * @@ -688,7 +704,7 @@ public final class Utils { PreferenceManager.getDefaultSharedPreferences(context).edit() .putString(MessageType.valueOf(args[0].toUpperCase(Locale.ENGLISH)).toString(), replyTo.toString()) .commit(); - sendMessage(context, replyTo, notification_enabled); + sendConfirmMessage(context, replyTo, notification_enabled); } catch (IllegalArgumentException e){ sendMessage(context, replyTo, messagetype_should_be_one_of, join(", ",toNonNull(MessageType.values()))); break; @@ -706,7 +722,7 @@ public final class Utils { PreferenceManager.getDefaultSharedPreferences(context).edit() .remove(MessageType.valueOf(args[0].toUpperCase(Locale.ENGLISH)).toString()) .commit(); - sendMessage(context, replyTo, notification_disabled); + sendConfirmMessage(context, replyTo, notification_disabled); } catch (IllegalArgumentException e){ sendMessage(context, replyTo, messagetype_should_be_one_of, join(", ",toNonNull(MessageType.values()))); break; @@ -726,7 +742,7 @@ public final class Utils { else Heavy.setPassword(context, replyTo, toNonNull(args[0])); } catch (SecurityException e){ - sendMessage(context, replyTo, security_exception+e.getMessage()); + sendMessage(context, replyTo, security_exception, e.getMessage()); } break; -- 2.39.2