Author: kmorin Date: 2014-04-26 21:43:46 +0200 (Sat, 26 Apr 2014) New Revision: 44 Url: http://forge.chorem.org/projects/say-my-texts/repository/revisions/44 Log: refs #999 Answer to a sms : reask dictating the message if the user says nothing understandable Modified: trunk/res/values-fr/strings.xml trunk/res/values/strings.xml trunk/src/org/chorem/android/saymytexts/DictateSmsBroadcastReceiver.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-16 07:41:48 UTC (rev 43) +++ trunk/res/values/strings.xml 2014-04-26 19:43:46 UTC (rev 44) @@ -30,6 +30,7 @@ <!-- actions --> <string name="ask_next_action">What would you like to do? Call, answer or quit?</string> + <string name="dictate_sms">Dictate your answer</string> <string name="send_sms_confirmation">You dictated: %s. Would you like to confirm, modify ou cancel?</string> <string name="call_action">call</string> Modified: trunk/res/values-fr/strings.xml =================================================================== --- trunk/res/values-fr/strings.xml 2014-04-16 07:41:48 UTC (rev 43) +++ trunk/res/values-fr/strings.xml 2014-04-26 19:43:46 UTC (rev 44) @@ -29,6 +29,7 @@ <!-- actions --> <string name="ask_next_action">Que voulez-vous faire ? Appeler, répondre ou quitter ?</string> + <string name="dictate_sms">Dictez votre réponse</string> <string name="send_sms_confirmation">Vous avez dicté : %s. Voulez-vous confirmer, corriger ou annuler ?</string> <string name="call_action">appeler</string> Modified: trunk/src/org/chorem/android/saymytexts/DictateSmsBroadcastReceiver.java =================================================================== --- trunk/src/org/chorem/android/saymytexts/DictateSmsBroadcastReceiver.java 2014-04-16 07:41:48 UTC (rev 43) +++ trunk/src/org/chorem/android/saymytexts/DictateSmsBroadcastReceiver.java 2014-04-26 19:43:46 UTC (rev 44) @@ -5,6 +5,8 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.media.AudioManager; +import android.media.ToneGenerator; import android.net.Uri; import android.os.Bundle; import android.speech.RecognitionListener; @@ -25,16 +27,20 @@ public static final String ACTION_DICTATE_SMS = "org.chorem.android.saymytexts.DICTATE_SMS"; - public static final String EXTRA_SMS = "sms"; + public static final String INTENT_EXTRA_SMS = "sms"; + public static final String INTENT_EXTRA_ATTEMPT_NUMBER = "attemptNumber"; @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); + final SMS sms = (SMS) intent.getSerializableExtra(INTENT_EXTRA_SMS); if (sms != null) { 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) { } @@ -56,9 +62,8 @@ @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 + tg.startTone(ToneGenerator.TONE_PROP_NACK); + reaskToDictate(context, intent, sms); } @Override @@ -73,6 +78,10 @@ serviceIntent.putExtra(SayMyTextService.INTENT_EXTRA_SMS, sms); serviceIntent.putExtra(SayMyTextService.INTENT_EXTRA_DICTATED_MESSAGE, text); context.startService(serviceIntent); + + } else { + tg.startTone(ToneGenerator.TONE_PROP_NACK); + reaskToDictate(context, intent, sms); } } @@ -91,4 +100,14 @@ speechRecognizer.startListening(recognizeIntent); } } + + protected void reaskToDictate(Context context, Intent intent, SMS sms) { + int attemptNumber = intent.getIntExtra(INTENT_EXTRA_ATTEMPT_NUMBER, 1); + + Intent serviceIntent = new Intent(context, SayMyTextService.class); + serviceIntent.setAction(SayMyTextService.ACTION_DICTATE_SMS); + serviceIntent.putExtra(SayMyTextService.INTENT_EXTRA_SMS, sms); + serviceIntent.putExtra(SayMyTextService.INTENT_EXTRA_ATTEMPT_NUMBER, attemptNumber); + context.startService(serviceIntent); + } } Modified: trunk/src/org/chorem/android/saymytexts/SayMyTextService.java =================================================================== --- trunk/src/org/chorem/android/saymytexts/SayMyTextService.java 2014-04-16 07:41:48 UTC (rev 43) +++ trunk/src/org/chorem/android/saymytexts/SayMyTextService.java 2014-04-26 19:43:46 UTC (rev 44) @@ -87,6 +87,9 @@ /** null if the texttospeech is not initialized */ protected Boolean canSpeak = null; + // true if music was playing when the firts message in queue arrived + protected boolean musicWasActive; + protected TextToSpeech textToSpeech; /** texts to read, received before the textospeech is ready or while a call is in progress */ @@ -204,9 +207,6 @@ break; case ACTION_DICTATE_SMS: - if (attemptNumber > 0) { - - } // if (bluetoothDevices.isEmpty()) { dictateSMS(sms, attemptNumber + 1); // } else { @@ -250,6 +250,10 @@ // 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())) { + musicWasActive = audioManager.isMusicActive(); + if (musicWasActive) { + audioManager.setStreamMute(AudioManager.STREAM_MUSIC, true); + } if (Boolean.TRUE.equals(canSpeak)) { requestReading(sms); } else { @@ -290,9 +294,14 @@ SMS sms = awaitingTexts.remove(0); requestReading(sms); - } else if (!bluetoothDevices.isEmpty()) { - audioManager.stopBluetoothSco(); - audioManager.setMode(AudioManager.MODE_NORMAL); + } else { + if (!bluetoothDevices.isEmpty()) { + audioManager.stopBluetoothSco(); + audioManager.setMode(AudioManager.MODE_NORMAL); + } + if (musicWasActive) { + audioManager.setStreamMute(AudioManager.STREAM_MUSIC, false); + } } } } @@ -420,7 +429,6 @@ textToSpeech.speak(getString(R.string.voice_not_recognized), TextToSpeech.QUEUE_ADD, params); } - //TODO add a counter to ask only twice (or 3 times, or it should be configurable) if (attemptNumber <= 3) { params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, btConnected ? BT_ASK_NEXT_ACTION_UTTERANCE_ID : ASK_NEXT_ACTION_UTTERANCE_ID); @@ -446,8 +454,8 @@ @Override public void onDone(String utteranceId) { Intent dictateAction = new Intent(DictateSmsBroadcastReceiver.ACTION_DICTATE_SMS); - dictateAction.putExtra(DictateSmsBroadcastReceiver.EXTRA_SMS, sms); -// dictateAction.putExtra(DictateSmsBroadcastReceiver.INTENT_EXTRA_SMS, attemptNumber); + dictateAction.putExtra(DictateSmsBroadcastReceiver.INTENT_EXTRA_SMS, sms); + dictateAction.putExtra(DictateSmsBroadcastReceiver.INTENT_EXTRA_ATTEMPT_NUMBER, attemptNumber); sendBroadcast(dictateAction); } @@ -455,13 +463,22 @@ 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, 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 (attemptNumber > 1) { + textToSpeech.speak(getString(R.string.voice_not_recognized), TextToSpeech.QUEUE_ADD, params); + } + + if (attemptNumber <= 3) { + params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, OTHER_UTTERANCE_ID); + textToSpeech.setLanguage(Locale.getDefault()); + textToSpeech.setSpeechRate(1f); + textToSpeech.setPitch(1f); + textToSpeech.speak(getString(R.string.dictate_sms), TextToSpeech.QUEUE_ADD, params); + + } else { + setCanSpeak(true); + } + } protected void askSendingConfirmation(final String message, final SMS originSms, final int attemptNumber) { @@ -499,7 +516,6 @@ textToSpeech.speak(getString(R.string.voice_not_recognized), TextToSpeech.QUEUE_ADD, params); } - //TODO add a counter to ask only twice (or 3 times, or it should be configurable) if (attemptNumber <= 3) { params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, OTHER_UTTERANCE_ID); String text = getString(R.string.send_sms_confirmation, message); Modified: trunk/src/org/chorem/android/saymytexts/SayNextActionBroadcastReceiver.java =================================================================== --- trunk/src/org/chorem/android/saymytexts/SayNextActionBroadcastReceiver.java 2014-04-16 07:41:48 UTC (rev 43) +++ trunk/src/org/chorem/android/saymytexts/SayNextActionBroadcastReceiver.java 2014-04-26 19:43:46 UTC (rev 44) @@ -63,9 +63,6 @@ @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 tg.startTone(ToneGenerator.TONE_PROP_NACK); reaskAction(context, intent, sms); } @@ -120,6 +117,10 @@ tg.startTone(ToneGenerator.TONE_PROP_NACK); reaskAction(context, intent, sms); } + + } else { + tg.startTone(ToneGenerator.TONE_PROP_NACK); + reaskAction(context, intent, sms); } }