Corrections after colleagues comments.

This commit is contained in:
Vladimir Byko-Ianko 2015-08-07 14:51:17 +03:00 committed by Alex Zolotarev
parent 63d373f16d
commit 36bb3499cd
4 changed files with 54 additions and 56 deletions

View file

@ -11,27 +11,26 @@ namespace
extern "C"
{
JNIEXPORT void JNICALL
Java_com_mapswithme_maps_sound_TTSPlayer_nativeEnableTurnNotifications(JNIEnv * env, jclass thiz, jboolean enable)
Java_com_mapswithme_maps_sound_TtsPlayer_nativeEnableTurnNotifications(JNIEnv * env, jclass thiz, jboolean enable)
{
return frm()->EnableTurnNotifications(enable == JNI_TRUE ? true : false);
}
JNIEXPORT jboolean JNICALL
Java_com_mapswithme_maps_sound_TTSPlayer_nativeAreTurnNotificationsEnabled(JNIEnv * env, jclass clazz)
Java_com_mapswithme_maps_sound_TtsPlayer_nativeAreTurnNotificationsEnabled(JNIEnv * env, jclass clazz)
{
return frm()->AreTurnNotificationsEnabled() ? JNI_TRUE : JNI_FALSE;
}
JNIEXPORT void JNICALL
Java_com_mapswithme_maps_sound_TTSPlayer_nativeSetTurnNotificationsLocale(JNIEnv * env, jclass thiz, jstring jLocale)
Java_com_mapswithme_maps_sound_TtsPlayer_nativeSetTurnNotificationsLocale(JNIEnv * env, jclass thiz, jstring jLocale)
{
if (frm())
frm()->SetTurnNotificationsLocale(jni::ToNativeString(env, jLocale));
frm()->SetTurnNotificationsLocale(jni::ToNativeString(env, jLocale));
}
JNIEXPORT jstring JNICALL
Java_com_mapswithme_maps_sound_TTSPlayer_nativeGetTurnNotificationsLocale(JNIEnv * env, jclass thiz)
Java_com_mapswithme_maps_sound_TtsPlayer_nativeGetTurnNotificationsLocale(JNIEnv * env, jclass thiz)
{
return jni::ToJavaString(env, frm()->GetTurnNotificationsLocale().c_str());
}
} // extern "C"
} // extern "C"

View file

@ -63,7 +63,7 @@ import com.mapswithme.maps.settings.SettingsActivity;
import com.mapswithme.maps.settings.StoragePathManager;
import com.mapswithme.maps.settings.StoragePathManager.MoveFilesListener;
import com.mapswithme.maps.settings.UnitLocale;
import com.mapswithme.maps.sound.TTSPlayer;
import com.mapswithme.maps.sound.TtsPlayer;
import com.mapswithme.maps.widget.BottomButtonsLayout;
import com.mapswithme.maps.widget.FadeView;
import com.mapswithme.maps.widget.placepage.BasePlacePageAnimationController;
@ -445,8 +445,8 @@ public class MWMActivity extends BaseMwmFragmentActivity
setContentView(R.layout.activity_map);
initViews();
// Initializing TTS player instance.
TTSPlayer.get();
// Initializing TTS player.
TtsPlayer.INSTANCE.init();
// Do not turn off the screen while benchmarking
if (MWMApplication.get().nativeIsBenchmarking())
@ -797,11 +797,7 @@ public class MWMActivity extends BaseMwmFragmentActivity
mTvTurnDistance.setText(builder);
// Turn sound notifications.
if (info.mTurnNotifications != null)
{
for (String textToSpeak : info.mTurnNotifications)
TTSPlayer.get().speak(textToSpeak);
}
TtsPlayer.INSTANCE.speakNotifications(info.mTurnNotifications);
}
}
@ -942,7 +938,7 @@ public class MWMActivity extends BaseMwmFragmentActivity
pauseLocation();
stopWatchingExternalStorage();
stopWatchingCompassStatusUpdate();
TTSPlayer.get().stop();
TtsPlayer.INSTANCE.stop();
super.onPause();
mLikesManager.cancelLikeDialog();
}

View file

@ -25,7 +25,7 @@ import com.mapswithme.maps.R;
import com.mapswithme.maps.base.BaseMwmRecyclerFragment;
import com.mapswithme.maps.base.OnBackPressListener;
import com.mapswithme.maps.location.LocationHelper;
import com.mapswithme.maps.sound.TTSPlayer;
import com.mapswithme.maps.sound.TtsPlayer;
import com.mapswithme.util.InputUtils;
import com.mapswithme.util.Language;
import com.mapswithme.util.StringUtils;
@ -281,7 +281,7 @@ public class SearchFragment extends BaseMwmRecyclerFragment implements View.OnCl
setSearchQuery(query);
}
private void hideSearchPanel()
private void hideSearch()
{
mEtSearchQuery.setText(null);
InputUtils.hideKeyboard(mEtSearchQuery);
@ -299,7 +299,7 @@ public class SearchFragment extends BaseMwmRecyclerFragment implements View.OnCl
return false;
// close Search panel
hideSearchPanel();
hideSearch();
// change map style for the Map activity
final int mapStyle = isDark ? Framework.MAP_STYLE_DARK : Framework.MAP_STYLE_LIGHT;
@ -316,7 +316,7 @@ public class SearchFragment extends BaseMwmRecyclerFragment implements View.OnCl
if (!pedestrian && !vehicle)
return false;
hideSearchPanel();
hideSearch();
final int routerType = pedestrian ? Framework.ROUTER_TYPE_PEDESTRIAN : Framework.ROUTER_TYPE_VEHICLE;
Framework.setRouter(routerType);
@ -326,14 +326,14 @@ public class SearchFragment extends BaseMwmRecyclerFragment implements View.OnCl
private boolean trySwitchOnTurnSound(String query)
{
final boolean sound = query.equals("?sound");
final boolean nosound = query.equals("?nosound");
final boolean sound = "?sound".equals(query);
final boolean nosound = "?nosound".equals(query);
if (!sound && !nosound)
return false;
hideSearchPanel();
TTSPlayer.get().enable(sound);
hideSearch();
TtsPlayer.INSTANCE.enable(sound);
return sound;
}

View file

@ -10,51 +10,45 @@ import com.mapswithme.maps.MWMApplication;
import java.util.Locale;
public class TTSPlayer
public enum TtsPlayer
{
private static TTSPlayer ourInstance = null;
INSTANCE;
private Context mContext = null;
private TextToSpeech mTts = null;
private Locale mTtsLocale = null;
private Context mContext;
private TextToSpeech mTts;
private Locale mTtsLocale;
private final static String TAG = "TTSPlayer";
private final static String TAG = "TtsPlayer";
private TTSPlayer()
TtsPlayer()
{
mContext = MWMApplication.get().getApplicationContext();
setLocaleIfAvailable(Locale.getDefault());
}
@Override
protected void finalize() throws Throwable
public void init()
{
if(mTts != null)
mTts.shutdown();
super.finalize();
Locale systemLanguage = Locale.getDefault();
if (INSTANCE.mTtsLocale == null || !INSTANCE.isLocaleEqual(systemLanguage))
INSTANCE.setLocaleIfAvailable(systemLanguage);
}
public static TTSPlayer get()
{
if (ourInstance == null || !ourInstance.isLocaleEquals(Locale.getDefault()))
ourInstance = new TTSPlayer();
return ourInstance;
}
private boolean isLocaleEquals(Locale locale)
private boolean isLocaleEqual(Locale locale)
{
return locale.getLanguage().equals(mTtsLocale.getLanguage()) &&
locale.getCountry().equals(mTtsLocale.getCountry());
locale.getCountry().equals(mTtsLocale.getCountry());
}
private void setLocaleIfAvailable(final Locale locale)
{
if (mTts != null && mTts.getLanguage().equals(locale))
if (mTts != null && mTtsLocale.equals(locale))
return;
// @TODO Consider move TextToSpeech to a service:
// http://stackoverflow.com/questions/24712639/android-texttospeech-initialization-blocks-freezes-ui-thread
if (mTts != null)
{
mTts.stop();
mTts.shutdown();
}
mTts = new TextToSpeech(mContext, new TextToSpeech.OnInitListener()
{
@Override
@ -82,11 +76,11 @@ public class TTSPlayer
});
}
public void speak(String textToSpeak)
private void speak(String textToSpeak)
{
if (mTts == null)
{
Log.w(TAG, "TTSPlayer.speak() is called while mTts == null.");
Log.w(TAG, "TtsPlayer.speak() is called while mTts == null.");
return;
}
// @TODO(vbykoianko) removes these two toasts below when the test period is finished.
@ -98,6 +92,15 @@ public class TTSPlayer
}
}
public void speakNotifications(String[] turnNotifications)
{
if (turnNotifications == null)
return;
for (String textToSpeak : turnNotifications)
speak(textToSpeak);
}
public void stop()
{
if(mTts != null)
@ -114,8 +117,8 @@ public class TTSPlayer
nativeEnableTurnNotifications(enabled);
}
public native static void nativeEnableTurnNotifications(boolean enable);
public native static boolean nativeAreTurnNotificationsEnabled();
public native static void nativeSetTurnNotificationsLocale(String locale);
public native static String nativeGetTurnNotificationsLocale();
private native static void nativeEnableTurnNotifications(boolean enable);
private native static boolean nativeAreTurnNotificationsEnabled();
private native static void nativeSetTurnNotificationsLocale(String locale);
private native static String nativeGetTurnNotificationsLocale();
}