r42 - in trunk: res/values res/values-fr src/org/chorem/android/saymytexts
Author: kmorin Date: 2014-04-07 23:16:56 +0200 (Mon, 07 Apr 2014) New Revision: 42 Url: http://forge.chorem.org/projects/say-my-texts/repository/revisions/42 Log: fixes #1008 Send back an SMS to the sender Added: trunk/src/org/chorem/android/saymytexts/DictateSmsBroadcastReceiver.java Modified: trunk/res/values-fr/strings.xml trunk/res/values/strings.xml trunk/src/org/chorem/android/saymytexts/NewTextBroadcastReceiver.java trunk/src/org/chorem/android/saymytexts/SayMyTextService.java trunk/src/org/chorem/android/saymytexts/SayNextActionBroadcastReceiver.java Modified: trunk/res/values/strings.xml =================================================================== --- trunk/res/values/strings.xml 2014-04-03 12:20:29 UTC (rev 41) +++ trunk/res/values/strings.xml 2014-04-07 21:16:56 UTC (rev 42) @@ -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> Modified: trunk/res/values-fr/strings.xml =================================================================== --- trunk/res/values-fr/strings.xml 2014-04-03 12:20:29 UTC (rev 41) +++ trunk/res/values-fr/strings.xml 2014-04-07 21:16:56 UTC (rev 42) @@ -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> Copied: trunk/src/org/chorem/android/saymytexts/DictateSmsBroadcastReceiver.java (from rev 41, trunk/src/org/chorem/android/saymytexts/SayNextActionBroadcastReceiver.java) =================================================================== --- trunk/src/org/chorem/android/saymytexts/DictateSmsBroadcastReceiver.java (rev 0) +++ trunk/src/org/chorem/android/saymytexts/DictateSmsBroadcastReceiver.java 2014-04-07 21:16:56 UTC (rev 42) @@ -0,0 +1,94 @@ +package org.chorem.android.saymytexts; + +import android.app.PendingIntent; +import android.content.ActivityNotFoundException; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +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; + +/** + * @author Kevin Morin (Code Lutin) + * @since x.x + */ +public class DictateSmsBroadcastReceiver extends BroadcastReceiver { + + private static final String TAG = "DictateSmsBroadcastReceiver"; + + public static final String ACTION_DICTATE_SMS = "org.chorem.android.saymytexts.DICTATE_SMS"; + + public static final String EXTRA_SMS = "sms"; + + @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 = SpeechRecognizer.createSpeechRecognizer(context); + speechRecognizer.setRecognitionListener(new RecognitionListener() { + @Override + public void onReadyForSpeech(Bundle params) { + } + + @Override + public void onBeginningOfSpeech() { + } + + @Override + public void onRmsChanged(float rmsdB) { + } + + @Override + public void onBufferReceived(byte[] buffer) {} + + @Override + public void onEndOfSpeech() {} + + @Override + public void onError(int error) { + Log.d(TAG, "onError " + error); + //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 + } + + @Override + public void onResults(Bundle data) { + List<String> results = data.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); + + Log.d(TAG, "results " + results); + if (results != null) { + 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); + } + } + + @Override + public void onPartialResults(Bundle partialResults) {} + + @Override + public void onEvent(int eventType, Bundle params) {} + }); + + Intent recognizeIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); + // Specify free form input + recognizeIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, + RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); + + speechRecognizer.startListening(recognizeIntent); + } + } +} Modified: trunk/src/org/chorem/android/saymytexts/NewTextBroadcastReceiver.java =================================================================== --- trunk/src/org/chorem/android/saymytexts/NewTextBroadcastReceiver.java 2014-04-03 12:20:29 UTC (rev 41) +++ trunk/src/org/chorem/android/saymytexts/NewTextBroadcastReceiver.java 2014-04-07 21:16:56 UTC (rev 42) @@ -51,7 +51,6 @@ @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,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(); Modified: trunk/src/org/chorem/android/saymytexts/SayMyTextService.java =================================================================== --- trunk/src/org/chorem/android/saymytexts/SayMyTextService.java 2014-04-03 12:20:29 UTC (rev 41) +++ trunk/src/org/chorem/android/saymytexts/SayMyTextService.java 2014-04-07 21:16:56 UTC (rev 42) @@ -57,17 +57,26 @@ 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 @@ 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 @@ 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 @@ 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); - } + 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; - // 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())) { + 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(); - SMS sms = (SMS) intent.getSerializableExtra(INTENT_EXTRA_SMS); - if (canSpeak != null && canSpeak) { - requestReading(sms); - } else { - awaitingTexts.add(sms); - } + } 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 @@ */ 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 @@ * @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 @@ /** * 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); + } + }); - if (heisendroidModeEnabled) { - textToSpeech.setLanguage(Locale.US); - textToSpeech.setSpeechRate(0.3f); - textToSpeech.setPitch(0.1f); - textToSpeech.speak("Say my text.", TextToSpeech.QUEUE_ADD, params); + 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); + } + + 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); + @Override + public void onError(String utteranceId) { + Log.e(TAG, "Error speaking: " + utteranceId); + } - 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 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); + } + }); - 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); - } + 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); } } Modified: trunk/src/org/chorem/android/saymytexts/SayNextActionBroadcastReceiver.java =================================================================== --- trunk/src/org/chorem/android/saymytexts/SayNextActionBroadcastReceiver.java 2014-04-03 12:20:29 UTC (rev 41) +++ trunk/src/org/chorem/android/saymytexts/SayNextActionBroadcastReceiver.java 2014-04-07 21:16:56 UTC (rev 42) @@ -4,11 +4,15 @@ 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 static final String ACTION_SAY_NEXT_ACTION = "org.chorem.android.saymytexts.SAY_NEXT_ACTION"; public static final String EXTRA_SMS = "sms"; + public static final String EXTRA_MESSAGE = "message"; - protected SpeechRecognizer speechRecognizer; - @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 @@ //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,15 +85,36 @@ } 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"); - } else if (results.contains(context.getString(R.string.quit_action))) { + 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); + + 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) // recognizeVoice(sms); @@ -108,4 +137,10 @@ 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); + } }
participants (1)
-
kmorin@users.chorem.org