-
Notifications
You must be signed in to change notification settings - Fork 215
/
VoiceSearchLanguageOptionsView.java
111 lines (85 loc) · 4.21 KB
/
VoiceSearchLanguageOptionsView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.vrbrowser.ui.widgets.settings;
import android.content.Context;
import android.graphics.Point;
import android.view.LayoutInflater;
import androidx.databinding.DataBindingUtil;
import org.mozilla.vrbrowser.R;
import org.mozilla.vrbrowser.browser.SettingsStore;
import org.mozilla.vrbrowser.browser.engine.SessionStore;
import org.mozilla.vrbrowser.databinding.OptionsLanguageVoiceBinding;
import org.mozilla.vrbrowser.ui.views.settings.RadioGroupSetting;
import org.mozilla.vrbrowser.ui.widgets.WidgetManagerDelegate;
import org.mozilla.vrbrowser.ui.widgets.WidgetPlacement;
import org.mozilla.vrbrowser.utils.LocaleUtils;
class VoiceSearchLanguageOptionsView extends SettingsView {
private OptionsLanguageVoiceBinding mBinding;
public VoiceSearchLanguageOptionsView(Context aContext, WidgetManagerDelegate aWidgetManager) {
super(aContext, aWidgetManager);
initialize(aContext);
}
private void initialize(Context aContext) {
updateUI();
}
@Override
protected void updateUI() {
super.updateUI();
LayoutInflater inflater = LayoutInflater.from(getContext());
// Inflate this data binding layout
mBinding = DataBindingUtil.inflate(inflater, R.layout.options_language_voice, this, true);
mScrollbar = mBinding.scrollbar;
// Header
mBinding.headerLayout.setBackClickListener(view -> {
mDelegate.showView(new LanguageOptionsView(getContext(), mWidgetManager));
});
mBinding.headerLayout.setHelpClickListener(view -> {
SessionStore.get().getActiveSession().loadUri(getResources().getString(R.string.sumo_language_voice_url));
mDelegate.exitWholeSettings();
});
// Footer
mBinding.footerLayout.setFooterButtonClickListener(mResetListener);
mBinding.languageRadio.setOptions(LocaleUtils.getSupportedLocalizedLanguages(getContext()));
String locale = LocaleUtils.getVoiceSearchLocale(getContext());
mBinding.languageRadio.setOnCheckedChangeListener(mLanguageListener);
setLanguage(LocaleUtils.getIndexForSupportedLocale(locale), false);
}
@Override
protected boolean reset() {
String systemLocale = LocaleUtils.getClosestSupportedLocale(getContext(), LocaleUtils.getDeviceLanguage().getId());
String currentLocale = LocaleUtils.getVoiceSearchLanguage(getContext()).getId();
if (currentLocale.equalsIgnoreCase(systemLocale)) {
setLanguage(LocaleUtils.getIndexForSupportedLocale(systemLocale), false);
} else {
setLanguage(LocaleUtils.getIndexForSupportedLocale(systemLocale), true);
SettingsStore.getInstance(getContext()).setVoiceSearchLocale(null);
}
return false;
}
private RadioGroupSetting.OnCheckedChangeListener mLanguageListener = (radioGroup, checkedId, doApply) -> {
String currentLocale = LocaleUtils.getVoiceSearchLocale(getContext());
String locale = LocaleUtils.getSupportedLocaleForIndex(mBinding.languageRadio.getCheckedRadioButtonId());
if (!locale.equalsIgnoreCase(currentLocale)) {
setLanguage(checkedId, true);
}
};
private OnClickListener mResetListener = (view) -> {
reset();
};
private void setLanguage(int checkedId, boolean doApply) {
mBinding.languageRadio.setOnCheckedChangeListener(null);
mBinding.languageRadio.setChecked(checkedId, doApply);
mBinding.languageRadio.setOnCheckedChangeListener(mLanguageListener);
if (doApply) {
String locale = LocaleUtils.getSupportedLocaleForIndex(checkedId);
LocaleUtils.setVoiceSearchLocale(getContext(), locale);
}
}
@Override
public Point getDimensions() {
return new Point( WidgetPlacement.dpDimension(getContext(), R.dimen.settings_dialog_width),
WidgetPlacement.dpDimension(getContext(), R.dimen.settings_dialog_height));
}
}