Merge pull request #604 from krabaey/feature-audiofocus

add option to keep playing music
This commit is contained in:
vshcherb 2014-04-27 23:49:27 +02:00
commit 59f5e1c0a7
5 changed files with 50 additions and 22 deletions

View file

@ -497,6 +497,8 @@
<string name="auto_zoom_map">Auto zoom map</string>
<string name="snap_to_road_descr">Snap position to roads during navigation</string>
<string name="snap_to_road">Snap to road</string>
<string name="interrupt_music_descr">Interrupt music when making announcement</string>
<string name="interrupt_music">Interrupt Music</string>
<string name="tip_recent_changes_0_8_2_t">Changes in 0.8.2:
\n\t* Improved Routing
\n\t* Dynamic map widgets

View file

@ -14,6 +14,7 @@
<ListPreference android:key="auto_zoom_map_new" android:title="@string/auto_zoom_map"
android:summary="@string/auto_zoom_map_descr"></ListPreference>
<CheckBoxPreference android:title="@string/snap_to_road" android:summary="@string/snap_to_road_descr" android:key="snap_to_road"></CheckBoxPreference>
<CheckBoxPreference android:title="@string/interrupt_music" android:summary="@string/interrupt_music_descr" android:key="interrupt_music"></CheckBoxPreference>
<Preference android:title="@string/show_warnings_title" android:summary="@string/show_warnings_descr" android:key="show_routing_alarms"/>
<Preference android:title="@string/speak_title" android:summary="@string/speak_descr" android:key="speak_routing_alarms"/>
<CheckBoxPreference android:summary="@string/use_compass_navigation_descr" android:title="@string/use_compass_navigation"

View file

@ -753,6 +753,12 @@ public class OsmandSettings {
SNAP_TO_ROAD.setModeDefaultValue(ApplicationMode.BICYCLE, true);
}
public final CommonPreference<Boolean> INTERRUPT_MUSIC = new BooleanPreference("interrupt_music", false).makeProfile().cache();
{
INTERRUPT_MUSIC.setModeDefaultValue(ApplicationMode.CAR, true);
INTERRUPT_MUSIC.setModeDefaultValue(ApplicationMode.PEDESTRIAN, true);
}
// this value string is synchronized with settings_pref.xml preference name
public static final String SAVE_CURRENT_TRACK = "save_current_track"; //$NON-NLS-1$

View file

@ -85,6 +85,7 @@ public class SettingsNavigationActivity extends SettingsBaseActivity {
registerBooleanPreference(settings.SNAP_TO_ROAD, screen);
registerBooleanPreference(settings.INTERRUPT_MUSIC, screen);
registerBooleanPreference(settings.USE_COMPASS_IN_NAVIGATION, screen);
Integer[] intValues = new Integer[] { 0, 5, 10, 15, 20, 25, 30, 45, 60, 90};

View file

@ -2,6 +2,7 @@ package net.osmand.plus.api;
import net.osmand.PlatformUtil;
import net.osmand.plus.OsmandApplication;
import org.apache.commons.logging.Log;
import android.content.Context;
@ -10,30 +11,47 @@ 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 = PlatformUtil.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);
}
public class AudioFocusHelperImpl implements AudioManager.OnAudioFocusChangeListener, AudioFocusHelper
{
private static final Log log = PlatformUtil.getLog(AudioFocusHelperImpl.class);
@Override
public boolean abandonFocus(Context context, int streamType) {
AudioManager mAudioManager = (AudioManager) ((Context) context).getSystemService(Context.AUDIO_SERVICE);
return AudioManager.AUDIOFOCUS_REQUEST_GRANTED == mAudioManager.abandonAudioFocus(this);
}
@Override
public boolean requestFocus(Context context, int streamType)
{
AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
if (((OsmandApplication) context.getApplicationContext()).getSettings().INTERRUPT_MUSIC.get())
{
return AudioManager.AUDIOFOCUS_REQUEST_GRANTED == mAudioManager.requestAudioFocus(this, streamType, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);
}
else
{
return true;
}
}
@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);
}
@Override
public boolean abandonFocus(Context context, int streamType)
{
AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
if (((OsmandApplication) context.getApplicationContext()).getSettings().INTERRUPT_MUSIC.get())
{
return AudioManager.AUDIOFOCUS_REQUEST_GRANTED == mAudioManager.abandonAudioFocus(this);
}
else
{
return true;
}
}
@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);
}
}