Skip to content

Commit

Permalink
Apply settings on the fly
Browse files Browse the repository at this point in the history
  • Loading branch information
uvlad7 committed Jun 6, 2021
1 parent 9b2f0cb commit b32daf1
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
Expand Down Expand Up @@ -58,6 +59,9 @@ public class RecognizeFragment extends Fragment implements RecognitionListener {
private List<Integer> video;
private TextView textViewCommand;
private FloatingActionButton recognizeStart;
private SharedPreferences sharedPref;
private SharedPreferences.OnSharedPreferenceChangeListener listener;
private boolean settingsChanged;

private MultipleVideoView videoViewFragmentRecognize;

Expand Down Expand Up @@ -87,6 +91,16 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedPref = PreferenceManager.getDefaultSharedPreferences(getContext());
settingsChanged = false;
// cause the preference manager does not currently store a strong reference to the listener
listener = (sharedPreferences, key) -> {
// Toast.makeText(requireActivity().getApplicationContext(), R.string.restart_app, Toast.LENGTH_LONG).show();
if (key.startsWith("rec_")) {
settingsChanged = true;
}
};
sharedPref.registerOnSharedPreferenceChangeListener(listener);
// get permissions
permissionCheck = ContextCompat.checkSelfPermission(requireActivity().getApplicationContext(), Manifest.permission.RECORD_AUDIO);
AppDatabase database = ((MainActivity) requireActivity()).getDatabase();
Expand Down Expand Up @@ -116,21 +130,21 @@ private void startSetup() {
private void setupRecognizer(File assetsDir) throws IOException {
// The recognizer can be configured to perform multiple searches
// of different kind and switch between them
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int threshold = sharedPref.getInt("sensitivity_of_the_activation_phrase", 6);
String grammar_name = sharedPref.getString("grammar_name", getString(R.string.grammar_name_default_value));
// Log.e("Settings", Integer.toString(sharedPref.getInt("rec_sensitivity_of_the_activation_phrase", 6)) + " " +
// sharedPref.getString("rec_grammar_name", getString(R.string.grammar_name_default_value)) + " " +
// Boolean.toString(sharedPref.getBoolean("rec_save_logs", false)));
int threshold = sharedPref.getInt("rec_sensitivity_of_the_activation_phrase", 6);
String grammar_name = sharedPref.getString("rec_grammar_name", getString(R.string.grammar_name_default_value));
SpeechRecognizerSetup setup = SpeechRecognizerSetup.defaultSetup()
.setAcousticModel(new File(assetsDir, "ru-ru-ptm"))
.setDictionary(new File(assetsDir, "car.dict"))
.setBoolean("-remove_noise", true)
.setSampleRate(8000)
.setKeywordThreshold((float) Math.pow(10, -threshold));

if (sharedPref.getBoolean("save_logs", false)) {
if (sharedPref.getBoolean("rec_save_logs", false)) {
setup.setRawLogDir(assetsDir); // To disable logging of raw audio comment out this call (takes a lot of space on the device)
}


recognizer = setup.getRecognizer();
recognizer.addListener(this);

Expand Down Expand Up @@ -194,7 +208,15 @@ public void onStop() {
public void onStart() {
super.onStart();
if (recognizer != null) {
switchSearch(KWS_SEARCH);
if (settingsChanged) {
recognizeStart.setEnabled(false);
settingsChanged = false;
recognizer.cancel();
recognizer.shutdown();
startSetup();
} else {
switchSearch(KWS_SEARCH);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@

public class SettingsFragment extends PreferenceFragmentCompat {

// @Override
// public void onCreate(@Nullable Bundle savedInstanceState) {
// super.onCreate(savedInstanceState);
// SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getContext());
// sharedPref.registerOnSharedPreferenceChangeListener((prefs, key) -> Toast.makeText(requireActivity().getApplicationContext(), R.string.restart_app, Toast.LENGTH_LONG).show());
// }

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.settings);
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<string name="grammar_name_summary">Выбор грамматики, используемой для распознавания. "Long" означает возможность распознавания нескольких фраз подряд</string>
<string name="save_logs">Сохранять логи</string>
<string name="save_logs_summary">Активировать сохранение логов распознавания. Занимает много места на устройстве!</string>
<string name="restart_app">Перезапустите приложение, чтобы применить настройки</string>

<string name="recognizer">Переводчик</string>
<string name="prompt_command">Здесь будет отображаться ваша голосовая команда</string>
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/res/xml/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
app:key="recognition_category"
app:title="@string/recognition_category">
<SeekBarPreference
android:key="sensitivity_of_the_activation_phrase"
android:key="rec_sensitivity_of_the_activation_phrase"
android:summary="@string/sensitivity_of_the_activation_phrase_summary"
android:title="@string/sensitivity_of_the_activation_phrase"
android:max="10"
Expand All @@ -15,7 +15,7 @@
android:progress="1"
app:showSeekBarValue="true" />
<ListPreference
android:key="grammar_name"
android:key="rec_grammar_name"
android:summary="@string/grammar_name_summary"
android:title="@string/grammar_name"
android:entries="@array/grammar_name_names"
Expand All @@ -26,7 +26,7 @@
app:key="debug_category"
app:title="@string/debug_category">
<CheckBoxPreference
android:key="save_logs"
android:key="rec_save_logs"
android:summary="@string/save_logs_summary"
android:title="@string/save_logs"
android:defaultValue="false"/>
Expand Down

0 comments on commit b32daf1

Please sign in to comment.