[say-my-texts-commits] 02/10: fixes #1008 Send back an SMS to the sender
This is an automated email from the git hooks/post-receive script. unknown user pushed a commit to branch devel in repository Say My Texts. commit fa89e6c618b52eb1a47c6f1ba56e97f41706ea95 Author: kmorin <kmorin@2d65e43e-0f24-4770-8739-84cc4fd997b9> Date: Mon Apr 7 21:16:56 2014 +0000 fixes #1008 Send back an SMS to the sender --- res/values-fr/strings.xml | 3 +- res/values/strings.xml | 4 +- ...eiver.java => DictateSmsBroadcastReceiver.java} | 41 +-- .../saymytexts/NewTextBroadcastReceiver.java | 3 +- .../android/saymytexts/SayMyTextService.java | 319 +++++++++++++++------ .../saymytexts/SayNextActionBroadcastReceiver.java | 51 +++- 6 files changed, 290 insertions(+), 131 deletions(-) diff --git a/res/values-fr/strings.xml b/res/values-fr/strings.xml index 05dbca1..b652157 100644 --- a/res/values-fr/strings.xml +++ b/res/values-fr/strings.xml @@ -3,7 +3,7 @@ <string name="app_name">Say My Texts</string> <string name="app_description">Application Android qui lit à voix haute the SMS reçus quand un casque est branché.</string> - <string name="sms_received">Nouveau message de %1$s : %2$s</string> + <string name="sms_received">Nouveau message de %1$s : %2$s.</string> <!-- Preferences --> <string name="preferences_settings_label">Paramètres</string> @@ -29,6 +29,7 @@ <!-- actions --> <string name="ask_next_action">Que voulez-vous faire ? Appeler, répondre ou quitter ?</string> + <string name="send_sms_confirmation">Vous avez dicté : %s. Voulez-vous confirmer, corriger ou annuler ?</string> <string name="call_action">appeler</string> <string name="answer_action">répondre</string> diff --git a/res/values/strings.xml b/res/values/strings.xml index ee0365f..182790a 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -3,7 +3,7 @@ <string name="app_name">Say My Texts</string> <string name="app_description">Android application which reads out loud the new SMS when the headset is plugged.</string> - <string name="sms_received">New message from %1$s: %2$s</string> + <string name="sms_received">New message from %1$s: %2$s.</string> <!-- Preferences --> <string name="preferences_settings_label">Settings</string> @@ -30,6 +30,8 @@ <!-- actions --> <string name="ask_next_action">What would you like to do? Call, answer or quit?</string> + <string name="send_sms_confirmation">You dictated: %s. Would you like to confirm, modify ou cancel?</string> + <string name="call_action">call</string> <string name="answer_action">answer</string> <string name="quit_action">quit</string> diff --git a/src/org/chorem/android/saymytexts/SayNextActionBroadcastReceiver.java b/src/org/chorem/android/saymytexts/DictateSmsBroadcastReceiver.java similarity index 60% copy from src/org/chorem/android/saymytexts/SayNextActionBroadcastReceiver.java copy to src/org/chorem/android/saymytexts/DictateSmsBroadcastReceiver.java index 221ea1b..edad8ef 100644 --- a/src/org/chorem/android/saymytexts/SayNextActionBroadcastReceiver.java +++ b/src/org/chorem/android/saymytexts/DictateSmsBroadcastReceiver.java @@ -1,5 +1,6 @@ package org.chorem.android.saymytexts; +import android.app.PendingIntent; import android.content.ActivityNotFoundException; import android.content.BroadcastReceiver; import android.content.Context; @@ -9,6 +10,7 @@ import android.os.Bundle; import android.speech.RecognitionListener; import android.speech.RecognizerIntent; import android.speech.SpeechRecognizer; +import android.telephony.SmsManager; import android.util.Log; import java.util.List; @@ -17,23 +19,21 @@ import java.util.List; * @author Kevin Morin (Code Lutin) * @since x.x */ -public class SayNextActionBroadcastReceiver extends BroadcastReceiver { +public class DictateSmsBroadcastReceiver extends BroadcastReceiver { - private static final String TAG = "SayNextActionBroadcastReceiver"; + private static final String TAG = "DictateSmsBroadcastReceiver"; - public static final String ACTION_SAY_NEXT_ACTION = "org.chorem.android.saymytexts.SAY_NEXT_ACTION"; + public static final String ACTION_DICTATE_SMS = "org.chorem.android.saymytexts.DICTATE_SMS"; public static final String EXTRA_SMS = "sms"; - protected SpeechRecognizer speechRecognizer; - @Override public void onReceive(final Context context, Intent intent) { Log.d(TAG, "next action ?"); final SMS sms = (SMS) intent.getSerializableExtra(EXTRA_SMS); if (sms != null) { - speechRecognizer = SpeechRecognizer.createSpeechRecognizer(context); + SpeechRecognizer speechRecognizer = SpeechRecognizer.createSpeechRecognizer(context); speechRecognizer.setRecognitionListener(new RecognitionListener() { @Override public void onReadyForSpeech(Bundle params) { @@ -67,29 +67,12 @@ public class SayNextActionBroadcastReceiver extends BroadcastReceiver { Log.d(TAG, "results " + results); if (results != null) { - - if (results.contains(context.getString(R.string.call_action))) { - try { - Intent callIntent = new Intent(Intent.ACTION_CALL); - callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - callIntent.setData(Uri.parse("tel:" + sms.getSenderNumber())); - context.startActivity(callIntent); - - } catch (ActivityNotFoundException activityException) { - Log.e(TAG, "Calling a Phone Number failed", activityException); - } - - } else if (results.contains(context.getString(R.string.answer_action))) { - //TODO ask the user to dictate the message - Log.d(TAG, "Répondre"); - - } else if (results.contains(context.getString(R.string.quit_action))) { - // do nothing - - } else { - //TODO add a counter to ask only twice (or 3 times, or it should be configurable) - // recognizeVoice(sms); - } + String text = results.get(0); + Intent serviceIntent = new Intent(context, SayMyTextService.class); + serviceIntent.setAction(SayMyTextService.ACTION_CONFIRM_SMS_SENDING); + serviceIntent.putExtra(SayMyTextService.INTENT_EXTRA_SMS, sms); + serviceIntent.putExtra(SayMyTextService.INTENT_EXTRA_DICTATED_MESSAGE, text); + context.startService(serviceIntent); } } diff --git a/src/org/chorem/android/saymytexts/NewTextBroadcastReceiver.java b/src/org/chorem/android/saymytexts/NewTextBroadcastReceiver.java index f589aa9..d8db7f0 100644 --- a/src/org/chorem/android/saymytexts/NewTextBroadcastReceiver.java +++ b/src/org/chorem/android/saymytexts/NewTextBroadcastReceiver.java @@ -51,7 +51,6 @@ public class NewTextBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Intent serviceIntent = new Intent(context, SayMyTextService.class); -Log.d(TAG, "received"); String action = intent.getAction(); if ("android.provider.Telephony.SMS_RECEIVED".equals(action)) { @@ -77,11 +76,13 @@ Log.d(TAG, "received"); Log.d(TAG,messageReceived); // start the service to say it out loud serviceIntent.putExtra(SayMyTextService.INTENT_EXTRA_SMS, sms); + serviceIntent.setAction(SayMyTextService.ACTION_READ_SMS); context.startService(serviceIntent); } } else { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); + serviceIntent.setAction(SayMyTextService.ACTION_MANAGE_BT_DEVICE); if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) { int majorDeviceClass = device.getBluetoothClass().getMajorDeviceClass(); diff --git a/src/org/chorem/android/saymytexts/SayMyTextService.java b/src/org/chorem/android/saymytexts/SayMyTextService.java index d7c218e..c137d98 100644 --- a/src/org/chorem/android/saymytexts/SayMyTextService.java +++ b/src/org/chorem/android/saymytexts/SayMyTextService.java @@ -57,17 +57,26 @@ public class SayMyTextService extends Service implements TextToSpeech.OnInitList private static final String TAG = "SayMyTextService"; + public static final String ACTION_READ_SMS = "org.chorem.android.saymytexts.READ_SMS"; + public static final String ACTION_READ_NEXT_SMS = "org.chorem.android.saymytexts.READ_NEXT_SMS"; + public static final String ACTION_MANAGE_BT_DEVICE = "org.chorem.android.saymytexts.ADD_BT_DEVICE"; + public static final String ACTION_DICTATE_SMS = "org.chorem.android.saymytexts.DICTATE_SMS"; + public static final String ACTION_CONFIRM_SMS_SENDING = "org.chorem.android.saymytexts.CONFIRM_SMS_SENDING"; + /** SMS to read */ public static final String INTENT_EXTRA_SMS = "sms"; /** Bluetooth device which has just connected or disconnected */ public static final String INTENT_EXTRA_BT_DEVICE = "btDevice"; /** If true, the device has just connected, else disconnected */ public static final String INTENT_EXTRA_ADD_BT_DEVICE = "addBtDevice"; + /** Bluetooth device which has just connected or disconnected */ + public static final String INTENT_EXTRA_DICTATED_MESSAGE = "dictatedMessage"; /** utterance id when the bluetooth device is connected */ protected static final String BT_UTTERANCE_ID = "btUtteranceId"; protected static final String BT_ASK_NEXT_ACTION_UTTERANCE_ID = "btAskNextActionUtteranceId"; protected static final String ASK_NEXT_ACTION_UTTERANCE_ID = "askNextActionUtteranceId"; + protected static final String OTHER_UTTERANCE_ID = "oherUtteranceId"; protected AudioManager audioManager; @@ -83,7 +92,7 @@ public class SayMyTextService extends Service implements TextToSpeech.OnInitList protected Map<BluetoothDevice, Integer> bluetoothDevices = new HashMap<>(); /** - * Listener to clal state change + * Listener to call state change */ protected final PhoneStateListener callStateListener = new PhoneStateListener() { @Override @@ -108,6 +117,9 @@ public class SayMyTextService extends Service implements TextToSpeech.OnInitList IntentFilter intentFilter = new IntentFilter(SayNextActionBroadcastReceiver.ACTION_SAY_NEXT_ACTION); registerReceiver(new SayNextActionBroadcastReceiver(), intentFilter); + + intentFilter = new IntentFilter(DictateSmsBroadcastReceiver.ACTION_DICTATE_SMS); + registerReceiver(new DictateSmsBroadcastReceiver(), intentFilter); } @Override @@ -132,28 +144,81 @@ public class SayMyTextService extends Service implements TextToSpeech.OnInitList boolean readingEnabled = sharedPref.getBoolean(readingEnabledKey, true); if (intent != null) { - BluetoothDevice device = intent.getParcelableExtra(INTENT_EXTRA_BT_DEVICE); - // if a device is passed to the service - // add or remove the device from the connected devices - if (device != null) { - boolean addBtDevice = intent.getBooleanExtra(INTENT_EXTRA_ADD_BT_DEVICE, false); - if (addBtDevice) { - bluetoothDevices.put(device, device.getBluetoothClass().getDeviceClass()); - } else { - bluetoothDevices.remove(device); - } - - - // else if the user enabled the reading and - // if the headset is plugged, or if there is a bluetooth device connected - } else if (readingEnabled && (audioManager.isWiredHeadsetOn() || !bluetoothDevices.isEmpty())) { - - SMS sms = (SMS) intent.getSerializableExtra(INTENT_EXTRA_SMS); - if (canSpeak != null && canSpeak) { - requestReading(sms); - } else { - awaitingTexts.add(sms); - } + String action = intent.getAction(); + Log.d(TAG, "action " + action); + + final SMS sms = (SMS) intent.getSerializableExtra(INTENT_EXTRA_SMS); + switch (action) { + case ACTION_MANAGE_BT_DEVICE: + BluetoothDevice device = intent.getParcelableExtra(INTENT_EXTRA_BT_DEVICE); + // if a device is passed to the service + // add or remove the device from the connected devices + if (device != null) { + boolean addBtDevice = intent.getBooleanExtra(INTENT_EXTRA_ADD_BT_DEVICE, false); + if (addBtDevice) { + bluetoothDevices.put(device, device.getBluetoothClass().getDeviceClass()); + } else { + bluetoothDevices.remove(device); + } + } + break; + + case ACTION_DICTATE_SMS: + if (!bluetoothDevices.isEmpty()) { + registerReceiver(new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + int state = intent.getExtras().getInt(AudioManager.EXTRA_SCO_AUDIO_STATE); + if (state == AudioManager.SCO_AUDIO_STATE_CONNECTED) { + context.unregisterReceiver(this); + dictateSMS(sms); + } + } + }, new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED)); + audioManager.setMode(AudioManager.MODE_IN_CALL); + audioManager.startBluetoothSco(); + + } else { + dictateSMS(sms); + } + break; + + case ACTION_CONFIRM_SMS_SENDING: + // if a message has just been dictated + final String dictatedMessage = intent.getStringExtra(INTENT_EXTRA_DICTATED_MESSAGE); + if (!bluetoothDevices.isEmpty()) { + registerReceiver(new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + int state = intent.getExtras().getInt(AudioManager.EXTRA_SCO_AUDIO_STATE); + if (state == AudioManager.SCO_AUDIO_STATE_CONNECTED) { + context.unregisterReceiver(this); + askSendingConfirmation(dictatedMessage, sms); + } + } + }, new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED)); + audioManager.setMode(AudioManager.MODE_IN_CALL); + audioManager.startBluetoothSco(); + + } else { + askSendingConfirmation(dictatedMessage, sms); + } + break; + + case ACTION_READ_NEXT_SMS: + setCanSpeak(true); + break; + + default: + // if the user enabled the reading and + // if the headset is plugged, or if there is a bluetooth device connected + if (readingEnabled && (audioManager.isWiredHeadsetOn() || !bluetoothDevices.isEmpty())) { + if (Boolean.TRUE.equals(canSpeak)) { + requestReading(sms); + } else { + awaitingTexts.add(sms); + } + } } result = START_STICKY; @@ -183,9 +248,9 @@ public class SayMyTextService extends Service implements TextToSpeech.OnInitList */ protected void setCanSpeak(Boolean canSpeak) { this.canSpeak = canSpeak; - if (canSpeak != null && canSpeak) { - requestReading(awaitingTexts); - awaitingTexts.clear(); + if (Boolean.TRUE.equals(canSpeak) && !awaitingTexts.isEmpty()) { + SMS sms = awaitingTexts.remove(0); + requestReading(sms); } } @@ -194,33 +259,25 @@ public class SayMyTextService extends Service implements TextToSpeech.OnInitList * @param sms the text to read */ protected void requestReading(SMS sms) { - requestReading(Collections.singletonList(sms)); - } - - /** - * Requests the reading of a list of texts through the wired headset or bluetooth device - * @param smsList the texts to read - */ - protected void requestReading(List<SMS> smsList) { if (bluetoothDevices.isEmpty()) { - readText(smsList, false); + readText(sms, false); } else { - requestReadingOverBt(smsList); + requestReadingOverBt(sms); } } /** * Starts the connection with the bluetooth device and requests the reading - * @param smsList the texts to read + * @param sms the texts to read */ - protected void requestReadingOverBt(final List<SMS> smsList) { + protected void requestReadingOverBt(final SMS sms) { registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { int state = intent.getExtras().getInt(AudioManager.EXTRA_SCO_AUDIO_STATE); if (state == AudioManager.SCO_AUDIO_STATE_CONNECTED) { context.unregisterReceiver(this); - readText(smsList, true); + readText(sms, true); } } }, new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED)); @@ -230,79 +287,159 @@ public class SayMyTextService extends Service implements TextToSpeech.OnInitList /** * Reads the texts out loud - * @param smsList the texts to read + * @param sms the text to read * @param btConnected if true, adds the utterance id for the bluetooth device */ - protected void readText(List<SMS> smsList, boolean btConnected) { + protected void readText(final SMS sms, boolean btConnected) { + // disable the reading of the nexts sms while reading the current one + setCanSpeak(false); + SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); String heisendroidModeEnabledKey = getString(R.string.preference_enable_heisendroid_mode_key); boolean heisendroidModeEnabled = sharedPref.getBoolean(heisendroidModeEnabledKey, true); - for (final SMS sms : smsList) { // waiting = true; - // init texttospeech - textToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() { - @Override - public void onStart(String utteranceId) { + // init texttospeech + textToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() { + @Override + public void onStart(String utteranceId) { + } + + @Override + public void onError(String utteranceId) { + Log.e(TAG, "Error speaking: " + utteranceId); + } + + @Override + public void onDone(String utteranceId) { + if (BT_UTTERANCE_ID.equals(utteranceId) || + BT_ASK_NEXT_ACTION_UTTERANCE_ID.equals(utteranceId)) { + // when the text has been read by the bluetooth device, stop the connection + audioManager.stopBluetoothSco(); + audioManager.setMode(AudioManager.MODE_NORMAL); } - @Override - public void onError(String utteranceId) { - Log.e(TAG, "Error speaking: " + utteranceId); + if (ASK_NEXT_ACTION_UTTERANCE_ID.equals(utteranceId) || + BT_ASK_NEXT_ACTION_UTTERANCE_ID.equals(utteranceId)) { + Intent sayaction = new Intent(SayNextActionBroadcastReceiver.ACTION_SAY_NEXT_ACTION); + sayaction.putExtra(SayNextActionBroadcastReceiver.EXTRA_SMS, sms); + sendBroadcast(sayaction); } + } + }); - @Override - public void onDone(String utteranceId) { - if (BT_UTTERANCE_ID.equals(utteranceId) || - BT_ASK_NEXT_ACTION_UTTERANCE_ID.equals(utteranceId)) { - // when the text has been read by the bluetooth device, stop the connection - audioManager.stopBluetoothSco(); - audioManager.setMode(AudioManager.MODE_NORMAL); - } + HashMap<String, String> params = new HashMap<>(); + params.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_VOICE_CALL)); + if (btConnected) { + params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, BT_UTTERANCE_ID); + } - if (ASK_NEXT_ACTION_UTTERANCE_ID.equals(utteranceId) || - BT_ASK_NEXT_ACTION_UTTERANCE_ID.equals(utteranceId)) { - Intent sayaction = new Intent(SayNextActionBroadcastReceiver.ACTION_SAY_NEXT_ACTION); - sayaction.putExtra(SayNextActionBroadcastReceiver.EXTRA_SMS, sms); - sendBroadcast(sayaction); - } + if (heisendroidModeEnabled) { + textToSpeech.setLanguage(Locale.US); + textToSpeech.setSpeechRate(0.3f); + textToSpeech.setPitch(0.1f); + textToSpeech.speak("Say my text.", TextToSpeech.QUEUE_ADD, params); + } + + textToSpeech.setLanguage(Locale.getDefault()); + textToSpeech.setSpeechRate(1f); + textToSpeech.setPitch(1f); + String text = getString(R.string.sms_received, sms.getSenderName(), sms.getMessage()); + textToSpeech.speak(text, TextToSpeech.QUEUE_ADD, params); + + if (heisendroidModeEnabled) { + textToSpeech.setLanguage(Locale.US); + textToSpeech.setSpeechRate(0.3f); + textToSpeech.setPitch(0.1f); + textToSpeech.speak("You're goddamn right.", TextToSpeech.QUEUE_ADD, params); + } + + params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, + btConnected ? BT_ASK_NEXT_ACTION_UTTERANCE_ID : ASK_NEXT_ACTION_UTTERANCE_ID); + textToSpeech.setLanguage(Locale.getDefault()); + textToSpeech.setSpeechRate(1f); + textToSpeech.setPitch(1f); + textToSpeech.speak(getString(R.string.ask_next_action), TextToSpeech.QUEUE_ADD, params); + } + + protected void dictateSMS(final SMS sms) { + Log.d(TAG, "dictateSMS " ); + textToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() { + @Override + public void onStart(String utteranceId) { + } + + @Override + public void onError(String utteranceId) { + Log.e(TAG, "Error speaking: " + utteranceId); + } + + @Override + public void onDone(String utteranceId) { + if (BT_UTTERANCE_ID.equals(utteranceId)) { + // when the text has been read by the bluetooth device, stop the connection + audioManager.stopBluetoothSco(); + audioManager.setMode(AudioManager.MODE_NORMAL); } - }); - HashMap<String, String> params = new HashMap<>(); - params.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_VOICE_CALL)); - if (btConnected) { - params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, BT_UTTERANCE_ID); + Intent dictateAction = new Intent(DictateSmsBroadcastReceiver.ACTION_DICTATE_SMS); + dictateAction.putExtra(DictateSmsBroadcastReceiver.EXTRA_SMS, sms); + sendBroadcast(dictateAction); + } + }); + + HashMap<String, String> params = new HashMap<>(); + params.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_VOICE_CALL)); + params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, + bluetoothDevices.isEmpty() ? BT_UTTERANCE_ID : OTHER_UTTERANCE_ID); + + textToSpeech.setLanguage(Locale.getDefault()); + textToSpeech.setSpeechRate(1f); + textToSpeech.setPitch(1f); +// String text = getString(R.string.send_sms_confirmation); + textToSpeech.speak("dictez votre sms", TextToSpeech.QUEUE_ADD, params); + } - if (heisendroidModeEnabled) { - textToSpeech.setLanguage(Locale.US); - textToSpeech.setSpeechRate(0.3f); - textToSpeech.setPitch(0.1f); - textToSpeech.speak("Say my text.", TextToSpeech.QUEUE_ADD, params); + protected void askSendingConfirmation(final String message, final SMS originSms) { + Log.d(TAG, "askSendingConfirmation " + message); + textToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() { + @Override + public void onStart(String utteranceId) { } - textToSpeech.setLanguage(Locale.getDefault()); - textToSpeech.setSpeechRate(1f); - textToSpeech.setPitch(1f); - String text = getString(R.string.sms_received, sms.getSenderName(), sms.getMessage()); - textToSpeech.speak(text, TextToSpeech.QUEUE_ADD, params); - - if (heisendroidModeEnabled) { - textToSpeech.setLanguage(Locale.US); - textToSpeech.setSpeechRate(0.3f); - textToSpeech.setPitch(0.1f); - textToSpeech.speak("You're goddamn right.", TextToSpeech.QUEUE_ADD, params); + @Override + public void onError(String utteranceId) { + Log.e(TAG, "Error speaking: " + utteranceId); } - params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, - btConnected ? BT_ASK_NEXT_ACTION_UTTERANCE_ID : ASK_NEXT_ACTION_UTTERANCE_ID); - textToSpeech.setLanguage(Locale.getDefault()); - textToSpeech.setSpeechRate(1f); - textToSpeech.setPitch(1f); - textToSpeech.speak(getString(R.string.ask_next_action), TextToSpeech.QUEUE_ADD, params); - } + @Override + public void onDone(String utteranceId) { + if (BT_UTTERANCE_ID.equals(utteranceId)) { + // when the text has been read by the bluetooth device, stop the connection + audioManager.stopBluetoothSco(); + audioManager.setMode(AudioManager.MODE_NORMAL); + } + + Intent sayaction = new Intent(SayNextActionBroadcastReceiver.ACTION_SAY_NEXT_ACTION); + sayaction.putExtra(SayNextActionBroadcastReceiver.EXTRA_SMS, originSms); + sayaction.putExtra(SayNextActionBroadcastReceiver.EXTRA_MESSAGE, message); + sendBroadcast(sayaction); + + } + }); + + HashMap<String, String> params = new HashMap<>(); + params.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_VOICE_CALL)); + params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, + bluetoothDevices.isEmpty() ? BT_UTTERANCE_ID : OTHER_UTTERANCE_ID); + + textToSpeech.setLanguage(Locale.getDefault()); + textToSpeech.setSpeechRate(1f); + textToSpeech.setPitch(1f); + String text = getString(R.string.send_sms_confirmation, message); + textToSpeech.speak(text, TextToSpeech.QUEUE_ADD, params); } } diff --git a/src/org/chorem/android/saymytexts/SayNextActionBroadcastReceiver.java b/src/org/chorem/android/saymytexts/SayNextActionBroadcastReceiver.java index 221ea1b..5689405 100644 --- a/src/org/chorem/android/saymytexts/SayNextActionBroadcastReceiver.java +++ b/src/org/chorem/android/saymytexts/SayNextActionBroadcastReceiver.java @@ -4,11 +4,15 @@ import android.content.ActivityNotFoundException; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; +import android.media.AudioManager; +import android.media.ToneGenerator; import android.net.Uri; import android.os.Bundle; import android.speech.RecognitionListener; import android.speech.RecognizerIntent; import android.speech.SpeechRecognizer; +import android.telephony.SmsManager; import android.util.Log; import java.util.List; @@ -24,17 +28,19 @@ public class SayNextActionBroadcastReceiver extends BroadcastReceiver { public static final String ACTION_SAY_NEXT_ACTION = "org.chorem.android.saymytexts.SAY_NEXT_ACTION"; public static final String EXTRA_SMS = "sms"; - - protected SpeechRecognizer speechRecognizer; + public static final String EXTRA_MESSAGE = "message"; @Override - public void onReceive(final Context context, Intent intent) { + public void onReceive(final Context context, final Intent intent) { Log.d(TAG, "next action ?"); final SMS sms = (SMS) intent.getSerializableExtra(EXTRA_SMS); if (sms != null) { - speechRecognizer = SpeechRecognizer.createSpeechRecognizer(context); + SpeechRecognizer speechRecognizer = SpeechRecognizer.createSpeechRecognizer(context); speechRecognizer.setRecognitionListener(new RecognitionListener() { + + private ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_VOICE_CALL, ToneGenerator.MAX_VOLUME); + @Override public void onReadyForSpeech(Bundle params) { } @@ -59,6 +65,8 @@ public class SayNextActionBroadcastReceiver extends BroadcastReceiver { //TODO if error 6 ERROR_SPEECH_TIMEOUT ask to say it again //TODO if error 7 ERROR_NO_MATCH ask to say it again //TODO if other error say there is an error + tg.startTone(ToneGenerator.TONE_PROP_NACK); + readNext(context); } @Override @@ -77,14 +85,35 @@ public class SayNextActionBroadcastReceiver extends BroadcastReceiver { } catch (ActivityNotFoundException activityException) { Log.e(TAG, "Calling a Phone Number failed", activityException); + tg.startTone(ToneGenerator.TONE_PROP_NACK); + readNext(context); } - } else if (results.contains(context.getString(R.string.answer_action))) { - //TODO ask the user to dictate the message - Log.d(TAG, "Répondre"); + } else if (results.contains(context.getString(R.string.answer_action)) + || results.contains(context.getString(R.string.modifiy_action))) { + Log.d(TAG, "Répondre ou corriger"); + + Intent serviceIntent = new Intent(context, SayMyTextService.class); + serviceIntent.setAction(SayMyTextService.ACTION_DICTATE_SMS); + serviceIntent.putExtra(SayMyTextService.INTENT_EXTRA_SMS, sms); + context.startService(serviceIntent); + + } else if (results.contains(context.getString(R.string.confirm_action))) { + String message = intent.getStringExtra(EXTRA_MESSAGE); + SmsManager smsManager = SmsManager.getDefault(); + smsManager.sendTextMessage(sms.getSenderNumber(), null, message, null, null); - } else if (results.contains(context.getString(R.string.quit_action))) { + tg.startTone(ToneGenerator.TONE_PROP_ACK); + + readNext(context); + + } else if (results.contains(context.getString(R.string.quit_action)) + || results.contains(context.getString(R.string.cancel_action))) { // do nothing + Log.d(TAG, "Quitter"); + tg.startTone(ToneGenerator.TONE_PROP_ACK); + + readNext(context); } else { //TODO add a counter to ask only twice (or 3 times, or it should be configurable) @@ -108,4 +137,10 @@ public class SayNextActionBroadcastReceiver extends BroadcastReceiver { speechRecognizer.startListening(recognizeIntent); } } + + protected void readNext(Context context) { + Intent serviceIntent = new Intent(context, SayMyTextService.class); + serviceIntent.setAction(SayMyTextService.ACTION_READ_NEXT_SMS); + context.startService(serviceIntent); + } } -- To stop receiving notification emails like this one, please contact Chorem.org SCM administrator <admin+scm@chorem.org>.
participants (1)
-
Chorem.org scm