From 446c0e1aaa7d11c864568e1b16ba8ffd788ff1bf Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Sat, 29 Sep 2012 15:51:31 +0200 Subject: [PATCH] Fix android 1.6 support --- .../voice/AbstractPrologCommandPlayer.java | 55 ++++++------------- .../plus/voice/AudioFocusHelperImpl.java | 39 +++++++++++++ 2 files changed, 56 insertions(+), 38 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/voice/AudioFocusHelperImpl.java diff --git a/OsmAnd/src/net/osmand/plus/voice/AbstractPrologCommandPlayer.java b/OsmAnd/src/net/osmand/plus/voice/AbstractPrologCommandPlayer.java index 8bb38cdbf9..e1e36e1aa2 100644 --- a/OsmAnd/src/net/osmand/plus/voice/AbstractPrologCommandPlayer.java +++ b/OsmAnd/src/net/osmand/plus/voice/AbstractPrologCommandPlayer.java @@ -28,9 +28,7 @@ import alice.tuprolog.Struct; import alice.tuprolog.Term; import alice.tuprolog.Theory; import alice.tuprolog.Var; -import android.annotation.SuppressLint; import android.content.Context; -import android.media.AudioManager; public abstract class AbstractPrologCommandPlayer implements CommandPlayer { @@ -203,51 +201,32 @@ public abstract class AbstractPrologCommandPlayer implements CommandPlayer { protected void requestAudioFocus() { log.debug("requestAudioFocus"); if (android.os.Build.VERSION.SDK_INT >= 8) { - mAudioFocusHelper = new AudioFocusHelper(ctx); + try { + mAudioFocusHelper = (AudioFocusHelper) Class.forName("net.osmand.plus.voice.AudioFocusHelperImpl").newInstance(); + } catch (Exception e) { + log.error(e.getMessage(), e); + } + } + if (mAudioFocusHelper != null) { + mAudioFocusHelper.requestFocus(ctx, streamType); } - if (mAudioFocusHelper != null) - mAudioFocusHelper.requestFocus(); } protected void abandonAudioFocus() { log.debug("abandonAudioFocus"); if (mAudioFocusHelper != null) { - mAudioFocusHelper.abandonFocus(); + mAudioFocusHelper.abandonFocus(ctx, streamType); mAudioFocusHelper = null; } } - - /** - * This helper class allows API level 8 calls to be isolated from the rest of the app. - * This class is only be instantiated on OS versions which support it. - * @author genly - * - */ - // We Use API level 8 calls here, suppress warnings. - @SuppressLint("NewApi") - public class AudioFocusHelper implements AudioManager.OnAudioFocusChangeListener { - private Context mContext; - private AudioManager mAudioManager; - - public AudioFocusHelper(Context context) { - mContext = context; - mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); - } + + + public interface AudioFocusHelper { - public boolean requestFocus() { - return AudioManager.AUDIOFOCUS_REQUEST_GRANTED == - mAudioManager.requestAudioFocus(this, streamType, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); - } - - public boolean abandonFocus() { - return AudioManager.AUDIOFOCUS_REQUEST_GRANTED == mAudioManager.abandonAudioFocus(this); - } - @Override - public void onAudioFocusChange(int focusChange) { - // Basically we ignore audio focus changes. There's not much we can do when we have interrupted audio - // for our speech, and we in turn get interrupted. Ignore it until a scenario comes up which gives us - // reason to change this strategy. - log.error("MediaCommandPlayerImpl.onAudioFocusChange(): Unexpected audio focus change: "+focusChange); - } + public boolean requestFocus(Context context, int streamType); + + public boolean abandonFocus(Context context, int streamType); } + + } diff --git a/OsmAnd/src/net/osmand/plus/voice/AudioFocusHelperImpl.java b/OsmAnd/src/net/osmand/plus/voice/AudioFocusHelperImpl.java new file mode 100644 index 0000000000..255333c4f5 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/voice/AudioFocusHelperImpl.java @@ -0,0 +1,39 @@ +package net.osmand.plus.voice; + +import org.apache.commons.logging.Log; + +import net.osmand.LogUtil; +import net.osmand.plus.voice.AbstractPrologCommandPlayer.AudioFocusHelper; +import android.content.Context; +import android.media.AudioManager; + +/** + * This helper class allows API level 8 calls to be isolated from the rest of the app. This class is only be instantiated on OS versions + * which support it. + * + * @author genly + */ +public class AudioFocusHelperImpl implements AudioManager.OnAudioFocusChangeListener, AudioFocusHelper { + private static final Log log = LogUtil.getLog(AudioFocusHelperImpl.class); + + @Override + public boolean requestFocus(Context context, int streamType) { + AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); + return AudioManager.AUDIOFOCUS_REQUEST_GRANTED == mAudioManager.requestAudioFocus(this, streamType, + AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); + } + + @Override + public boolean abandonFocus(Context context, int streamType) { + AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); + return AudioManager.AUDIOFOCUS_REQUEST_GRANTED == mAudioManager.abandonAudioFocus(this); + } + + @Override + public void onAudioFocusChange(int focusChange) { + // Basically we ignore audio focus changes. There's not much we can do when we have interrupted audio + // for our speech, and we in turn get interrupted. Ignore it until a scenario comes up which gives us + // reason to change this strategy. + log.error("MediaCommandPlayerImpl.onAudioFocusChange(): Unexpected audio focus change: " + focusChange); + } +} \ No newline at end of file