Request audio focus.

This commit is contained in:
Sandor Dornbush
2011-02-22 22:13:44 -08:00
parent 59344f540d
commit 35aa51d6a9
4 changed files with 89 additions and 3 deletions
@@ -0,0 +1,74 @@
/*
* Copyright 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.android.apps.mytracks.services;
import static com.google.android.apps.mytracks.MyTracksConstants.TAG;
import android.content.Context;
import android.media.AudioManager;
import android.media.AudioManager.OnAudioFocusChangeListener;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnUtteranceCompletedListener;
import android.util.Log;
import java.util.HashMap;
/**
* This class will periodically announce the user's trip statistics for Froyo and future handsets.
* This class will request and release audio focus.
*
* @author Sandor Dornbush
*/
public class FroyoStatusAnnouncerTask
extends StatusAnnouncerTask
implements OnUtteranceCompletedListener, OnAudioFocusChangeListener {
private AudioManager audioManager;
public FroyoStatusAnnouncerTask(Context context) {
super(context);
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
}
protected void onTtsInit(int status) {
super.onTtsInit(status);
if (status == TextToSpeech.SUCCESS) {
tts.setOnUtteranceCompletedListener(this);
}
}
protected void speakAnnouncment(String announcement) {
audioManager.requestAudioFocus(this,
TextToSpeech.Engine.DEFAULT_STREAM, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);
// We don't care about the id.
// It is supplied here to force onUtteranceCompleted to be called.
HashMap<String, String> params = new HashMap<String, String>();
params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "not_used");
tts.speak(announcement, TextToSpeech.QUEUE_FLUSH, params);
}
@Override
public void onUtteranceCompleted(String utteranceId) {
Log.d(TAG, "FroyoStatusAnnouncerTask: Abandoning audio focus.");
audioManager.abandonAudioFocus(this);
}
@Override
public void onAudioFocusChange(int focusChange) {
// We don't care.
}
}
@@ -41,7 +41,11 @@ public class StatusAnnouncerFactory {
*/
public PeriodicTask create(Context context) {
if (hasTts) {
return new StatusAnnouncerTask(context);
if (ApiFeatures.getInstance().supportsAudioFocus()) {
return new FroyoStatusAnnouncerTask(context);
} else {
return new StatusAnnouncerTask(context);
}
} else {
return null;
}
@@ -59,7 +59,7 @@ public class StatusAnnouncerTask implements PeriodicTask {
/**
* The interface to the text to speech engine.
*/
private TextToSpeech tts;
protected TextToSpeech tts;
/**
* The response received from the TTS engine after initialization.
@@ -98,7 +98,7 @@ public class StatusAnnouncerTask implements PeriodicTask {
/**
* Called when the TTS engine is initialized.
*/
private void onTtsInit(int status) {
protected void onTtsInit(int status) {
Log.i(MyTracksConstants.TAG, "TrackRecordingService.TTS init: " + status);
this.ready = status == TextToSpeech.SUCCESS;
@@ -146,6 +146,10 @@ public class StatusAnnouncerTask implements PeriodicTask {
String announcement = getAnnouncement(service.getTripStatistics());
Log.d(MyTracksConstants.TAG, "Announcement: " + announcement);
speakAnnouncment(announcement);
}
protected void speakAnnouncment(String announcement) {
tts.speak(announcement, TextToSpeech.QUEUE_FLUSH, null);
}
@@ -107,6 +107,10 @@ public class ApiFeatures {
return getApiLevel() >= 9;
}
public boolean supportsAudioFocus() {
return getApiLevel() >= 8;
}
/**
* There's a bug (#1587) in Cupcake and Donut which prevents you from
* using a SQLiteQueryBuilder twice. That is, if you call buildQuery