From 3512b82f9bb1c31f9f4337e68c1283ad591970b1 Mon Sep 17 00:00:00 2001 From: Alexey Kulish Date: Sat, 1 Apr 2017 20:57:03 +0300 Subject: [PATCH] Fix #3558 --- OsmAnd/res/xml/general_settings.xml | 5 +- OsmAnd/res/xml/navigation_settings.xml | 5 + .../src/net/osmand/plus/AppInitializer.java | 16 ++- .../net/osmand/plus/OsmandApplication.java | 7 +- .../src/net/osmand/plus/OsmandSettings.java | 6 +- .../osmand/plus/activities/MapActivity.java | 5 +- .../plus/activities/MapActivityActions.java | 2 +- .../activities/SettingsGeneralActivity.java | 89 ----------------- .../SettingsNavigationActivity.java | 97 ++++++++++++++++++- .../net/osmand/plus/api/AudioFocusHelper.java | 6 +- .../osmand/plus/api/AudioFocusHelperImpl.java | 7 +- .../osmand/plus/base/FailSafeFuntions.java | 2 +- .../plus/development/TestVoiceActivity.java | 3 +- .../plus/download/DownloadIndexesThread.java | 8 +- .../other/MapRouteInfoMenu.java | 1 + .../other/RoutePreferencesMenu.java | 11 ++- .../net/osmand/plus/routing/VoiceRouter.java | 2 +- .../voice/AbstractPrologCommandPlayer.java | 19 ++-- .../plus/voice/MediaCommandPlayerImpl.java | 17 ++-- .../plus/voice/TTSCommandPlayerImpl.java | 10 +- 20 files changed, 174 insertions(+), 144 deletions(-) diff --git a/OsmAnd/res/xml/general_settings.xml b/OsmAnd/res/xml/general_settings.xml index 4cd0d2465b..b20160d42e 100644 --- a/OsmAnd/res/xml/general_settings.xml +++ b/OsmAnd/res/xml/general_settings.xml @@ -11,10 +11,7 @@ - - - - + diff --git a/OsmAnd/res/xml/navigation_settings.xml b/OsmAnd/res/xml/navigation_settings.xml index b3dc364cdd..5463abb74c 100644 --- a/OsmAnd/res/xml/navigation_settings.xml +++ b/OsmAnd/res/xml/navigation_settings.xml @@ -47,4 +47,9 @@ android:summary="@string/delay_to_start_navigation_descr" /> --> + + + + + diff --git a/OsmAnd/src/net/osmand/plus/AppInitializer.java b/OsmAnd/src/net/osmand/plus/AppInitializer.java index a01f069aba..d547f54ed1 100644 --- a/OsmAnd/src/net/osmand/plus/AppInitializer.java +++ b/OsmAnd/src/net/osmand/plus/AppInitializer.java @@ -439,12 +439,18 @@ public class AppInitializer implements IProgress { - public synchronized void initVoiceDataInDifferentThread(final Activity uiContext, final String voiceProvider, final Runnable run, boolean showDialog) { + public synchronized void initVoiceDataInDifferentThread(final Activity uiContext, + final ApplicationMode applicationMode, + final String voiceProvider, + final Runnable run, + boolean showDialog) { + final ProgressDialog dlg = showDialog ? ProgressDialog.show(uiContext, app.getString(R.string.loading_data), app.getString(R.string.voice_data_initializing)) : null; new Thread(new Runnable() { - public CommandPlayer createCommandPlayer(String voiceProvider, OsmandApplication osmandApplication, Activity ctx) + public CommandPlayer createCommandPlayer(String voiceProvider, ApplicationMode applicationMode, + OsmandApplication osmandApplication, Activity ctx) throws CommandPlayerException { if (voiceProvider != null) { File parent = osmandApplication.getAppPath(IndexConstants.VOICE_INDEX_DIR); @@ -454,9 +460,9 @@ public class AppInitializer implements IProgress { } if (MediaCommandPlayerImpl.isMyData(voiceDir)) { - return new MediaCommandPlayerImpl(osmandApplication, osmandApplication.getRoutingHelper().getVoiceRouter(), voiceProvider); + return new MediaCommandPlayerImpl(osmandApplication, applicationMode, osmandApplication.getRoutingHelper().getVoiceRouter(), voiceProvider); } else if (TTSCommandPlayerImpl.isMyData(voiceDir)) { - return new TTSCommandPlayerImpl(ctx, osmandApplication.getRoutingHelper().getVoiceRouter(), voiceProvider); + return new TTSCommandPlayerImpl(ctx, applicationMode, osmandApplication.getRoutingHelper().getVoiceRouter(), voiceProvider); } throw new CommandPlayerException(ctx.getString(R.string.voice_data_not_supported)); } @@ -468,7 +474,7 @@ public class AppInitializer implements IProgress { if (app.player != null) { app.player.clear(); } - app.player = createCommandPlayer(voiceProvider, app, uiContext); + app.player = createCommandPlayer(voiceProvider, applicationMode, app, uiContext); app.getRoutingHelper().getVoiceRouter().setPlayer(app.player); if(dlg != null) { dlg.dismiss(); diff --git a/OsmAnd/src/net/osmand/plus/OsmandApplication.java b/OsmAnd/src/net/osmand/plus/OsmandApplication.java index ee43428dba..18b1df685f 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandApplication.java +++ b/OsmAnd/src/net/osmand/plus/OsmandApplication.java @@ -386,8 +386,9 @@ public class OsmandApplication extends MultiDexApplication { return player; } - public void initVoiceCommandPlayer(final Activity uiContext, boolean warningNoneProvider, Runnable run, boolean showDialog, boolean force) { - String voiceProvider = osmandSettings.VOICE_PROVIDER.get(); + public void initVoiceCommandPlayer(final Activity uiContext, ApplicationMode applicationMode, + boolean warningNoneProvider, Runnable run, boolean showDialog, boolean force) { + String voiceProvider = osmandSettings.VOICE_PROVIDER.getModeValue(applicationMode); if (voiceProvider == null || OsmandSettings.VOICE_PROVIDER_NOT_USE.equals(voiceProvider)) { if (warningNoneProvider && voiceProvider == null) { final AlertDialog.Builder builder = new AlertDialog.Builder(uiContext); @@ -435,7 +436,7 @@ public class OsmandApplication extends MultiDexApplication { } else { if (player == null || !Algorithms.objectEquals(voiceProvider, player.getCurrentVoice()) || force) { - appInitializer.initVoiceDataInDifferentThread(uiContext, voiceProvider, run, showDialog); + appInitializer.initVoiceDataInDifferentThread(uiContext, applicationMode, voiceProvider, run, showDialog); } } diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index 060a7028bf..0f6ce146aa 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -990,7 +990,7 @@ public class OsmandSettings { SNAP_TO_ROAD.setModeDefaultValue(ApplicationMode.BICYCLE, true); } - public final CommonPreference INTERRUPT_MUSIC = new BooleanPreference("interrupt_music", false).makeGlobal(); + public final CommonPreference INTERRUPT_MUSIC = new BooleanPreference("interrupt_music", false).makeProfile(); public final CommonPreference PROXY_HOST = new StringPreference("proxy_host", "127.0.0.1").makeGlobal(); public final CommonPreference PROXY_PORT = new IntPreference("proxy_port", 8118).makeGlobal(); @@ -1230,7 +1230,7 @@ public class OsmandSettings { // this value string is synchronized with settings_pref.xml preference name public final OsmandPreference AUDIO_STREAM_GUIDANCE = new IntPreference("audio_stream", - 3/*AudioManager.STREAM_MUSIC*/).makeGlobal(); + 3/*AudioManager.STREAM_MUSIC*/).makeProfile(); // For now this can be changed only in TestVoiceActivity public final OsmandPreference BT_SCO_DELAY = new IntPreference("bt_sco_delay", 1500).makeGlobal().cache(); @@ -2675,7 +2675,7 @@ public class OsmandSettings { } ; - }.makeGlobal(); + }.makeProfile(); // this value string is synchronized with settings_pref.xml preference name diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index c07f34b741..4f95bc2c42 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -511,8 +511,9 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven app.getLocationProvider().checkIfLastKnownLocationIsValid(); // for voice navigation - if (settings.AUDIO_STREAM_GUIDANCE.get() != null) { - setVolumeControlStream(settings.AUDIO_STREAM_GUIDANCE.get()); + ApplicationMode routingAppMode = getRoutingHelper().getAppMode(); + if (routingAppMode != null && settings.AUDIO_STREAM_GUIDANCE.getModeValue(routingAppMode) != null) { + setVolumeControlStream(settings.AUDIO_STREAM_GUIDANCE.getModeValue(routingAppMode)); } else { setVolumeControlStream(AudioManager.STREAM_MUSIC); } diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java b/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java index c7bf42a978..27b1dd9ea2 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java @@ -441,7 +441,7 @@ public class MapActivityActions implements DialogProvider { ApplicationMode mode = getRouteMode(from); //app.getSettings().APPLICATION_MODE.set(mode); app.getRoutingHelper().setAppMode(mode); - app.initVoiceCommandPlayer(mapActivity, true, null, false, false); + app.initVoiceCommandPlayer(mapActivity, mode, true, null, false, false); // save application mode controls settings.FOLLOW_THE_ROUTE.set(false); app.getRoutingHelper().setFollowingMode(false); diff --git a/OsmAnd/src/net/osmand/plus/activities/SettingsGeneralActivity.java b/OsmAnd/src/net/osmand/plus/activities/SettingsGeneralActivity.java index 41a0cbb0ea..d9240e2249 100644 --- a/OsmAnd/src/net/osmand/plus/activities/SettingsGeneralActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/SettingsGeneralActivity.java @@ -4,11 +4,9 @@ package net.osmand.plus.activities; import android.app.Dialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; -import android.content.Intent; import android.content.pm.ActivityInfo; import android.content.pm.PackageManager; import android.content.res.Configuration; -import android.media.AudioManager; import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; @@ -49,23 +47,17 @@ import net.osmand.plus.dashboard.DashChooseAppDirFragment.ChooseAppDirFragment; import net.osmand.plus.dashboard.DashChooseAppDirFragment.MoveFilesToDifferentDirectory; import net.osmand.plus.dialogs.ConfigureMapMenu; import net.osmand.plus.download.DownloadActivity; -import net.osmand.plus.download.DownloadActivityType; -import net.osmand.plus.helpers.FileNameTranslationHelper; import net.osmand.plus.render.NativeOsmandLibrary; -import net.osmand.plus.voice.CommandPlayer; import net.osmand.render.RenderingRulesStorage; import java.io.File; import java.util.ArrayList; import java.util.Arrays; -import java.util.LinkedHashSet; import java.util.List; -import java.util.Set; public class SettingsGeneralActivity extends SettingsBaseActivity implements OnRequestPermissionsResultCallback { - public static final String MORE_VALUE = "MORE_VALUE"; private Preference applicationDir; private ListPreference applicationModePreference; private Preference drivingRegionPreference; @@ -104,7 +96,6 @@ public class SettingsGeneralActivity extends SettingsBaseActivity implements OnR drivingRegionPreference = screen.findPreference(settings.DRIVING_REGION.getId()); addLocalPrefs((PreferenceGroup) screen.findPreference("localization")); - addVoicePrefs((PreferenceGroup) screen.findPreference("voice")); addProxyPrefs((PreferenceGroup) screen.findPreference("proxy")); addMiscPreferences((PreferenceGroup) screen.findPreference("misc")); @@ -112,35 +103,6 @@ public class SettingsGeneralActivity extends SettingsBaseActivity implements OnR applicationModePreference.setOnPreferenceChangeListener(this); } - - private void addVoicePrefs(PreferenceGroup cat) { - if (!Version.isBlackberry((OsmandApplication) getApplication())) { - ListPreference lp = createListPreference( - settings.AUDIO_STREAM_GUIDANCE, - new String[]{getString(R.string.voice_stream_music), getString(R.string.voice_stream_notification), - getString(R.string.voice_stream_voice_call)}, new Integer[]{AudioManager.STREAM_MUSIC, - AudioManager.STREAM_NOTIFICATION, AudioManager.STREAM_VOICE_CALL}, R.string.choose_audio_stream, - R.string.choose_audio_stream_descr); - final OnPreferenceChangeListener prev = lp.getOnPreferenceChangeListener(); - lp.setOnPreferenceChangeListener(new OnPreferenceChangeListener() { - - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - prev.onPreferenceChange(preference, newValue); - CommandPlayer player = getMyApplication().getPlayer(); - if (player != null) { - player.updateAudioStream(settings.AUDIO_STREAM_GUIDANCE.get()); - } - return true; - } - }); - cat.addPreference(lp); - cat.addPreference(createCheckBoxPreference(settings.INTERRUPT_MUSIC, R.string.interrupt_music, - R.string.interrupt_music_descr)); - } - } - - private void addLocalPrefs(PreferenceGroup screen) { drivingRegionPreference.setTitle(R.string.driving_region); drivingRegionPreference.setSummary(R.string.driving_region_descr); @@ -524,7 +486,6 @@ public class SettingsGeneralActivity extends SettingsBaseActivity implements OnR } public void updateAllSettings() { - reloadVoiceListPreference(getPreferenceScreen()); super.updateAllSettings(); updateApplicationDirTextAndSummary(); applicationModePreference.setTitle(getString(R.string.settings_preset) + " [" @@ -536,19 +497,6 @@ public class SettingsGeneralActivity extends SettingsBaseActivity implements OnR @Override public boolean onPreferenceChange(Preference preference, Object newValue) { String id = preference.getKey(); - if (id.equals(settings.VOICE_PROVIDER.getId())) { - if (MORE_VALUE.equals(newValue)) { - // listPref.set(oldValue); // revert the change.. - final Intent intent = new Intent(this, DownloadActivity.class); - intent.putExtra(DownloadActivity.TAB_TO_OPEN, DownloadActivity.DOWNLOAD_TAB); - intent.putExtra(DownloadActivity.FILTER_CAT, DownloadActivityType.VOICE_FILE.getTag()); - startActivity(intent); - } else { - super.onPreferenceChange(preference, newValue); - getMyApplication().initVoiceCommandPlayer(this, false, null, true, false); - } - return true; - } super.onPreferenceChange(preference, newValue); if (id.equals(settings.SAFE_MODE.getId())) { if ((Boolean) newValue) { @@ -710,43 +658,6 @@ public class SettingsGeneralActivity extends SettingsBaseActivity implements OnR } } - - private void reloadVoiceListPreference(PreferenceScreen screen) { - String[] entries; - String[] entrieValues; - Set voiceFiles = getVoiceFiles(); - entries = new String[voiceFiles.size() + 2]; - entrieValues = new String[voiceFiles.size() + 2]; - int k = 0; - // entries[k++] = getString(R.string.shared_string_none); - entrieValues[k] = OsmandSettings.VOICE_PROVIDER_NOT_USE; - entries[k++] = getString(R.string.shared_string_do_not_use); - for (String s : voiceFiles) { - entries[k] = (s.contains("tts") ? getString(R.string.ttsvoice) + " " : "") + - FileNameTranslationHelper.getVoiceName(this, s); - entrieValues[k] = s; - k++; - } - entrieValues[k] = MORE_VALUE; - entries[k] = getString(R.string.install_more); - registerListPreference(settings.VOICE_PROVIDER, screen, entries, entrieValues); - } - - - private Set getVoiceFiles() { - // read available voice data - File extStorage = getMyApplication().getAppPath(IndexConstants.VOICE_INDEX_DIR); - Set setFiles = new LinkedHashSet(); - if (extStorage.exists()) { - for (File f : extStorage.listFiles()) { - if (f.isDirectory()) { - setFiles.add(f.getName()); - } - } - } - return setFiles; - } - @Override protected void onResume() { super.onResume(); diff --git a/OsmAnd/src/net/osmand/plus/activities/SettingsNavigationActivity.java b/OsmAnd/src/net/osmand/plus/activities/SettingsNavigationActivity.java index f3ca8bb4d8..3853ef7fee 100644 --- a/OsmAnd/src/net/osmand/plus/activities/SettingsNavigationActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/SettingsNavigationActivity.java @@ -7,11 +7,13 @@ import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnMultiChoiceClickListener; import android.content.Intent; +import android.media.AudioManager; import android.os.Bundle; import android.preference.CheckBoxPreference; import android.preference.ListPreference; import android.preference.Preference; import android.preference.PreferenceCategory; +import android.preference.PreferenceGroup; import android.preference.PreferenceScreen; import android.support.annotation.NonNull; import android.support.v7.app.AlertDialog; @@ -21,6 +23,7 @@ import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.TextView; +import net.osmand.IndexConstants; import net.osmand.plus.ApplicationMode; import net.osmand.plus.ContextMenuAdapter; import net.osmand.plus.ContextMenuItem; @@ -32,18 +35,27 @@ import net.osmand.plus.OsmandSettings.OsmandPreference; import net.osmand.plus.OsmandSettings.SpeedConstants; import net.osmand.plus.R; import net.osmand.plus.Version; +import net.osmand.plus.download.DownloadActivity; +import net.osmand.plus.download.DownloadActivityType; +import net.osmand.plus.helpers.FileNameTranslationHelper; import net.osmand.plus.routing.RouteProvider.RouteService; +import net.osmand.plus.voice.CommandPlayer; import net.osmand.router.GeneralRouter; import net.osmand.router.GeneralRouter.RoutingParameter; import net.osmand.router.GeneralRouter.RoutingParameterType; import net.osmand.util.Algorithms; +import java.io.File; import java.util.ArrayList; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; +import java.util.Set; public class SettingsNavigationActivity extends SettingsBaseActivity { + public static final String MORE_VALUE = "MORE_VALUE"; + private Preference avoidRouting; private Preference preferRouting; private Preference reliefFactorRouting; @@ -223,8 +235,72 @@ public class SettingsNavigationActivity extends SettingsBaseActivity { } else { profileDialog(); } + + addVoicePrefs((PreferenceGroup) screen.findPreference("voice")); + } + + private void reloadVoiceListPreference(PreferenceScreen screen) { + String[] entries; + String[] entrieValues; + Set voiceFiles = getVoiceFiles(); + entries = new String[voiceFiles.size() + 2]; + entrieValues = new String[voiceFiles.size() + 2]; + int k = 0; + // entries[k++] = getString(R.string.shared_string_none); + entrieValues[k] = OsmandSettings.VOICE_PROVIDER_NOT_USE; + entries[k++] = getString(R.string.shared_string_do_not_use); + for (String s : voiceFiles) { + entries[k] = (s.contains("tts") ? getString(R.string.ttsvoice) + " " : "") + + FileNameTranslationHelper.getVoiceName(this, s); + entrieValues[k] = s; + k++; + } + entrieValues[k] = MORE_VALUE; + entries[k] = getString(R.string.install_more); + registerListPreference(settings.VOICE_PROVIDER, screen, entries, entrieValues); + } + + + private Set getVoiceFiles() { + // read available voice data + File extStorage = getMyApplication().getAppPath(IndexConstants.VOICE_INDEX_DIR); + Set setFiles = new LinkedHashSet(); + if (extStorage.exists()) { + for (File f : extStorage.listFiles()) { + if (f.isDirectory()) { + setFiles.add(f.getName()); + } + } + } + return setFiles; + } + + private void addVoicePrefs(PreferenceGroup cat) { + if (!Version.isBlackberry((OsmandApplication) getApplication())) { + ListPreference lp = createListPreference( + settings.AUDIO_STREAM_GUIDANCE, + new String[]{getString(R.string.voice_stream_music), getString(R.string.voice_stream_notification), + getString(R.string.voice_stream_voice_call)}, new Integer[]{AudioManager.STREAM_MUSIC, + AudioManager.STREAM_NOTIFICATION, AudioManager.STREAM_VOICE_CALL}, R.string.choose_audio_stream, + R.string.choose_audio_stream_descr); + final Preference.OnPreferenceChangeListener prev = lp.getOnPreferenceChangeListener(); + lp.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + prev.onPreferenceChange(preference, newValue); + CommandPlayer player = getMyApplication().getPlayer(); + if (player != null) { + player.updateAudioStream(settings.AUDIO_STREAM_GUIDANCE.get()); + } + return true; + } + }); + cat.addPreference(lp); + cat.addPreference(createCheckBoxPreference(settings.INTERRUPT_MUSIC, R.string.interrupt_music, + R.string.interrupt_music_descr)); + } } - private void prepareRoutingPrefs(PreferenceScreen screen) { PreferenceCategory cat = (PreferenceCategory) screen.findPreference("routing_preferences"); @@ -350,12 +426,9 @@ public class SettingsNavigationActivity extends SettingsBaseActivity { return router; } - - - - public void updateAllSettings() { prepareRoutingPrefs(getPreferenceScreen()); + reloadVoiceListPreference(getPreferenceScreen()); super.updateAllSettings(); routerServicePreference.setSummary(getString(R.string.router_service_descr) + " [" + settings.ROUTER_SERVICE.get() + "]"); } @@ -363,6 +436,20 @@ public class SettingsNavigationActivity extends SettingsBaseActivity { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { String id = preference.getKey(); + if (id.equals(settings.VOICE_PROVIDER.getId())) { + if (MORE_VALUE.equals(newValue)) { + // listPref.set(oldValue); // revert the change.. + final Intent intent = new Intent(this, DownloadActivity.class); + intent.putExtra(DownloadActivity.TAB_TO_OPEN, DownloadActivity.DOWNLOAD_TAB); + intent.putExtra(DownloadActivity.FILTER_CAT, DownloadActivityType.VOICE_FILE.getTag()); + startActivity(intent); + } else { + super.onPreferenceChange(preference, newValue); + getMyApplication().initVoiceCommandPlayer( + this, settings.APPLICATION_MODE.get(), false, null, true, false); + } + return true; + } super.onPreferenceChange(preference, newValue); if (id.equals(settings.ROUTER_SERVICE.getId())) { routerServicePreference.setSummary(getString(R.string.router_service_descr) + " [" diff --git a/OsmAnd/src/net/osmand/plus/api/AudioFocusHelper.java b/OsmAnd/src/net/osmand/plus/api/AudioFocusHelper.java index 4bfc940b4a..8182f70e18 100644 --- a/OsmAnd/src/net/osmand/plus/api/AudioFocusHelper.java +++ b/OsmAnd/src/net/osmand/plus/api/AudioFocusHelper.java @@ -2,12 +2,14 @@ package net.osmand.plus.api; import android.content.Context; +import net.osmand.plus.ApplicationMode; + public interface AudioFocusHelper { - boolean requestFocus(Context context, int streamType); + boolean requestFocus(Context context, ApplicationMode applicationMode, int streamType); void onAudioFocusChange(int focusChange); - boolean abandonFocus(Context context, int streamType); + boolean abandonFocus(Context context, ApplicationMode applicationMode, int streamType); } diff --git a/OsmAnd/src/net/osmand/plus/api/AudioFocusHelperImpl.java b/OsmAnd/src/net/osmand/plus/api/AudioFocusHelperImpl.java index 3098c4d85e..e10bd83c3c 100644 --- a/OsmAnd/src/net/osmand/plus/api/AudioFocusHelperImpl.java +++ b/OsmAnd/src/net/osmand/plus/api/AudioFocusHelperImpl.java @@ -2,6 +2,7 @@ package net.osmand.plus.api; import net.osmand.PlatformUtil; +import net.osmand.plus.ApplicationMode; import net.osmand.plus.OsmandApplication; import org.apache.commons.logging.Log; @@ -19,16 +20,16 @@ public class AudioFocusHelperImpl implements AudioManager.OnAudioFocusChangeList private static final Log log = PlatformUtil.getLog(AudioFocusHelperImpl.class); @Override - public boolean requestFocus(Context context, int streamType) { + public boolean requestFocus(Context context, ApplicationMode applicationMode, int streamType) { AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); return AudioManager.AUDIOFOCUS_REQUEST_GRANTED == mAudioManager.requestAudioFocus(this, streamType, - ((OsmandApplication) context.getApplicationContext()).getSettings().INTERRUPT_MUSIC.get()? + ((OsmandApplication) context.getApplicationContext()).getSettings().INTERRUPT_MUSIC.getModeValue(applicationMode)? AudioManager.AUDIOFOCUS_GAIN_TRANSIENT: AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK ); } @Override - public boolean abandonFocus(Context context, int streamType) + public boolean abandonFocus(Context context, ApplicationMode applicationMode, int streamType) { AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); return AudioManager.AUDIOFOCUS_REQUEST_GRANTED == mAudioManager.abandonAudioFocus(this); diff --git a/OsmAnd/src/net/osmand/plus/base/FailSafeFuntions.java b/OsmAnd/src/net/osmand/plus/base/FailSafeFuntions.java index c4bdc33ecb..02c2c3ddec 100644 --- a/OsmAnd/src/net/osmand/plus/base/FailSafeFuntions.java +++ b/OsmAnd/src/net/osmand/plus/base/FailSafeFuntions.java @@ -173,7 +173,7 @@ public class FailSafeFuntions { app.getSettings().FOLLOW_THE_ROUTE.set(true); routingHelper.setFollowingMode(true); app.getTargetPointsHelper().updateRouteAndRefresh(true); - app.initVoiceCommandPlayer(ma, true, null, false, false); + app.initVoiceCommandPlayer(ma, routingHelper.getAppMode(), true, null, false, false); if(ma.getDashboard().isVisible()) { ma.getDashboard().hideDashboard(); } diff --git a/OsmAnd/src/net/osmand/plus/development/TestVoiceActivity.java b/OsmAnd/src/net/osmand/plus/development/TestVoiceActivity.java index 0ba0f20f16..b058a004c6 100644 --- a/OsmAnd/src/net/osmand/plus/development/TestVoiceActivity.java +++ b/OsmAnd/src/net/osmand/plus/development/TestVoiceActivity.java @@ -114,7 +114,8 @@ public class TestVoiceActivity extends OsmandActionBarActivity { final OsmandApplication app = (OsmandApplication) getApplication(); getSupportActionBar().setTitle(app.getString(R.string.test_voice_prompts) + " (" + entrieValues[which] + ")"); app.getSettings().VOICE_PROVIDER.set(entrieValues[which]); - app.initVoiceCommandPlayer(TestVoiceActivity.this, true, new Runnable() { + app.initVoiceCommandPlayer(TestVoiceActivity.this, + app.getSettings().APPLICATION_MODE.get(), true, new Runnable() { @Override public void run() { diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java index cb7168a77b..2d51016d89 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java @@ -25,6 +25,7 @@ import net.osmand.IndexConstants; import net.osmand.PlatformUtil; import net.osmand.map.WorldRegion; import net.osmand.map.WorldRegion.RegionParams; +import net.osmand.plus.ApplicationMode; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandSettings; import net.osmand.plus.OsmandSettings.OsmandPreference; @@ -155,7 +156,7 @@ public class DownloadIndexesThread { } public void initSettingsFirstMap(WorldRegion reg) { - if(app.getSettings().FIRST_MAP_IS_DOWNLOADED.get() || reg == null) { + if (app.getSettings().FIRST_MAP_IS_DOWNLOADED.get() || reg == null) { return; } app.getSettings().FIRST_MAP_IS_DOWNLOADED.set(true); @@ -176,7 +177,10 @@ public class DownloadIndexesThread { } } if (setTts != null) { - app.getSettings().VOICE_PROVIDER.set(setTts); + List modes = ApplicationMode.allPossibleValues(); + for (ApplicationMode mode : modes) { + app.getSettings().VOICE_PROVIDER.setModeValue(mode, setTts); + } } } } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/MapRouteInfoMenu.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/MapRouteInfoMenu.java index 7c968a3400..59e249cfa2 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/MapRouteInfoMenu.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/MapRouteInfoMenu.java @@ -251,6 +251,7 @@ public class MapRouteInfoMenu implements IRouteInformationListener { //updateMenu(); } routingHelper.setAppMode(next); + mapActivity.getMyApplication().initVoiceCommandPlayer(mapActivity, next, true, null, false, false); routingHelper.recalculateRouteDueToSettingsChange(); } } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/RoutePreferencesMenu.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/RoutePreferencesMenu.java index d662c38638..2d4a394597 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/RoutePreferencesMenu.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/RoutePreferencesMenu.java @@ -269,7 +269,8 @@ public class RoutePreferencesMenu { entrieValues = new String[voiceFiles.size() + 2]; int k = 0; int selected = -1; - String selectedValue = mapActivity.getMyApplication().getSettings().VOICE_PROVIDER.get(); + String selectedValue = mapActivity.getMyApplication().getSettings().VOICE_PROVIDER.getModeValue( + mapActivity.getMyApplication().getRoutingHelper().getAppMode()); entrieValues[k] = OsmandSettings.VOICE_PROVIDER_NOT_USE; entries[k] = mapActivity.getResources().getString(R.string.shared_string_do_not_use); ContextMenuItem.ItemBuilder itemBuilder = new ContextMenuItem.ItemBuilder(); @@ -324,8 +325,10 @@ public class RoutePreferencesMenu { } public static void applyVoiceProvider(MapActivity mapActivity, String provider) { - mapActivity.getMyApplication().getSettings().VOICE_PROVIDER.set(provider); - mapActivity.getMyApplication().initVoiceCommandPlayer(mapActivity, false, null, true, false); + OsmandApplication app = mapActivity.getMyApplication(); + ApplicationMode applicationMode = app.getRoutingHelper().getAppMode(); + app.getSettings().VOICE_PROVIDER.setModeValue(applicationMode, provider); + app.initVoiceCommandPlayer(mapActivity, applicationMode, false, null, true, false); } private static Set getVoiceFiles(MapActivity mapActivity) { @@ -502,7 +505,7 @@ public class RoutePreferencesMenu { v.findViewById(R.id.toggle_item).setVisibility(View.GONE); final TextView btn = (TextView) v.findViewById(R.id.select_button); btn.setTextColor(btn.getLinkTextColors()); - String voiceProvider = settings.VOICE_PROVIDER.get(); + String voiceProvider = settings.VOICE_PROVIDER.getModeValue(routingHelper.getAppMode()); String voiceProviderStr; if (voiceProvider != null) { if (OsmandSettings.VOICE_PROVIDER_NOT_USE.equals(voiceProvider)) { diff --git a/OsmAnd/src/net/osmand/plus/routing/VoiceRouter.java b/OsmAnd/src/net/osmand/plus/routing/VoiceRouter.java index 86db5e0f91..0c7b344260 100644 --- a/OsmAnd/src/net/osmand/plus/routing/VoiceRouter.java +++ b/OsmAnd/src/net/osmand/plus/routing/VoiceRouter.java @@ -175,7 +175,7 @@ public class VoiceRouter { } // Trigger close prompts earlier if delayed for BT SCO connection establishment - if ((settings.AUDIO_STREAM_GUIDANCE.get() == 0) && !AbstractPrologCommandPlayer.btScoStatus) { + if ((settings.AUDIO_STREAM_GUIDANCE.getModeValue(router.getAppMode()) == 0) && !AbstractPrologCommandPlayer.btScoStatus) { btScoDelayDistance = currentSpeed * (double) settings.BT_SCO_DELAY.get() / 1000; } diff --git a/OsmAnd/src/net/osmand/plus/voice/AbstractPrologCommandPlayer.java b/OsmAnd/src/net/osmand/plus/voice/AbstractPrologCommandPlayer.java index 0fdd9cb797..4ff9cd2998 100644 --- a/OsmAnd/src/net/osmand/plus/voice/AbstractPrologCommandPlayer.java +++ b/OsmAnd/src/net/osmand/plus/voice/AbstractPrologCommandPlayer.java @@ -65,12 +65,15 @@ public abstract class AbstractPrologCommandPlayer implements CommandPlayer, Stat protected String language = ""; protected int streamType; private static int currentVersion; + private ApplicationMode applicationMode; - protected AbstractPrologCommandPlayer(OsmandApplication ctx, String voiceProvider, String configFile, int[] sortedVoiceVersions) + protected AbstractPrologCommandPlayer(OsmandApplication ctx, ApplicationMode applicationMode, + String voiceProvider, String configFile, int[] sortedVoiceVersions) throws CommandPlayerException { this.ctx = ctx; this.sortedVoiceVersions = sortedVoiceVersions; + this.applicationMode = applicationMode; long time = System.currentTimeMillis(); try { this.ctx = ctx; @@ -82,7 +85,7 @@ public abstract class AbstractPrologCommandPlayer implements CommandPlayer, Stat if (log.isInfoEnabled()) { log.info("Initializing prolog system : " + (System.currentTimeMillis() - time)); //$NON-NLS-1$ } - this.streamType = ctx.getSettings().AUDIO_STREAM_GUIDANCE.get(); + this.streamType = ctx.getSettings().AUDIO_STREAM_GUIDANCE.getModeValue(applicationMode); init(voiceProvider, ctx.getSettings(), configFile); final Term langVal = solveSimplePredicate("language"); if (langVal instanceof Struct) { @@ -90,6 +93,10 @@ public abstract class AbstractPrologCommandPlayer implements CommandPlayer, Stat } } + public ApplicationMode getApplicationMode() { + return applicationMode; + } + public String getLanguage() { return language; } @@ -277,9 +284,9 @@ public abstract class AbstractPrologCommandPlayer implements CommandPlayer, Stat mAudioFocusHelper = getAudioFocus(); } if (mAudioFocusHelper != null && ctx != null) { - boolean audioFocusGranted = mAudioFocusHelper.requestFocus(ctx, streamType); + boolean audioFocusGranted = mAudioFocusHelper.requestFocus(ctx, applicationMode, streamType); // If AudioManager.STREAM_VOICE_CALL try using BT SCO: - if (audioFocusGranted && ctx.getSettings().AUDIO_STREAM_GUIDANCE.get() == 0) { + if (audioFocusGranted && ctx.getSettings().AUDIO_STREAM_GUIDANCE.getModeValue(applicationMode) == 0) { toggleBtSco(true); } } @@ -296,11 +303,11 @@ public abstract class AbstractPrologCommandPlayer implements CommandPlayer, Stat protected synchronized void abandonAudioFocus() { log.debug("abandonAudioFocus"); - if ((ctx != null && ctx.getSettings().AUDIO_STREAM_GUIDANCE.get() == 0) || (btScoStatus == true)) { + if ((ctx != null && ctx.getSettings().AUDIO_STREAM_GUIDANCE.getModeValue(applicationMode) == 0) || (btScoStatus == true)) { toggleBtSco(false); } if (ctx != null && mAudioFocusHelper != null) { - mAudioFocusHelper.abandonFocus(ctx, streamType); + mAudioFocusHelper.abandonFocus(ctx, applicationMode, streamType); } mAudioFocusHelper = null; } diff --git a/OsmAnd/src/net/osmand/plus/voice/MediaCommandPlayerImpl.java b/OsmAnd/src/net/osmand/plus/voice/MediaCommandPlayerImpl.java index 0a6e17105a..a48a904d98 100644 --- a/OsmAnd/src/net/osmand/plus/voice/MediaCommandPlayerImpl.java +++ b/OsmAnd/src/net/osmand/plus/voice/MediaCommandPlayerImpl.java @@ -1,18 +1,19 @@ package net.osmand.plus.voice; -import java.io.File; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; +import android.media.MediaPlayer; import net.osmand.PlatformUtil; +import net.osmand.plus.ApplicationMode; import net.osmand.plus.OsmandApplication; import net.osmand.plus.routing.VoiceRouter; import org.apache.commons.logging.Log; -import android.media.MediaPlayer; +import java.io.File; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; /** @@ -34,10 +35,10 @@ public class MediaCommandPlayerImpl extends AbstractPrologCommandPlayer implemen private VoiceRouter vrt; - public MediaCommandPlayerImpl(OsmandApplication ctx, VoiceRouter vrt, String voiceProvider) + public MediaCommandPlayerImpl(OsmandApplication ctx, ApplicationMode applicationMode, VoiceRouter vrt, String voiceProvider) throws CommandPlayerException { - super(ctx, voiceProvider, CONFIG_FILE, MEDIA_VOICE_VERSION); + super(ctx, applicationMode, voiceProvider, CONFIG_FILE, MEDIA_VOICE_VERSION); this.vrt = vrt; } @@ -83,7 +84,7 @@ public class MediaCommandPlayerImpl extends AbstractPrologCommandPlayer implemen if (mediaPlayer == null) { requestAudioFocus(); // Delay first prompt of each batch to allow BT SCO connection being established - if (ctx != null && ctx.getSettings().AUDIO_STREAM_GUIDANCE.get() == 0) { + if (ctx != null && ctx.getSettings().AUDIO_STREAM_GUIDANCE.getModeValue(getApplicationMode()) == 0) { try { log.debug("Delaying MediaCommandPlayer for BT SCO"); Thread.sleep(ctx.getSettings().BT_SCO_DELAY.get()); diff --git a/OsmAnd/src/net/osmand/plus/voice/TTSCommandPlayerImpl.java b/OsmAnd/src/net/osmand/plus/voice/TTSCommandPlayerImpl.java index 4c0c7fc3ff..33c1f04e19 100644 --- a/OsmAnd/src/net/osmand/plus/voice/TTSCommandPlayerImpl.java +++ b/OsmAnd/src/net/osmand/plus/voice/TTSCommandPlayerImpl.java @@ -11,6 +11,7 @@ import android.speech.tts.TextToSpeech.OnUtteranceCompletedListener; import android.support.v7.app.AlertDialog; import net.osmand.PlatformUtil; +import net.osmand.plus.ApplicationMode; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.activities.SettingsActivity; @@ -69,9 +70,9 @@ public class TTSCommandPlayerImpl extends AbstractPrologCommandPlayer { private HashMap params = new HashMap(); private VoiceRouter vrt; - public TTSCommandPlayerImpl(Activity ctx, VoiceRouter vrt, String voiceProvider) + public TTSCommandPlayerImpl(Activity ctx, ApplicationMode applicationMode, VoiceRouter vrt, String voiceProvider) throws CommandPlayerException { - super((OsmandApplication) ctx.getApplicationContext(), voiceProvider, CONFIG_FILE, TTS_VOICE_VERSION); + super((OsmandApplication) ctx.getApplicationContext(), applicationMode, voiceProvider, CONFIG_FILE, TTS_VOICE_VERSION); this.vrt = vrt; if (Algorithms.isEmpty(language)) { throw new CommandPlayerException( @@ -82,7 +83,8 @@ public class TTSCommandPlayerImpl extends AbstractPrologCommandPlayer { cSpeechRate = app.getSettings().SPEECH_RATE.get(); } initializeEngine(app, ctx); - params.put(TextToSpeech.Engine.KEY_PARAM_STREAM, app.getSettings().AUDIO_STREAM_GUIDANCE.get().toString()); + params.put(TextToSpeech.Engine.KEY_PARAM_STREAM, app.getSettings().AUDIO_STREAM_GUIDANCE + .getModeValue(getApplicationMode()).toString()); } @@ -111,7 +113,7 @@ public class TTSCommandPlayerImpl extends AbstractPrologCommandPlayer { if (ttsRequests++ == 0) { requestAudioFocus(); // Delay first prompt of each batch to allow BT SCO connection being established - if (ctx.getSettings().AUDIO_STREAM_GUIDANCE.get() == 0) { + if (ctx.getSettings().AUDIO_STREAM_GUIDANCE.getModeValue(getApplicationMode()) == 0) { ttsRequests++; if (android.os.Build.VERSION.SDK_INT < 21) { params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,""+System.currentTimeMillis());